Skip to content

Commit

Permalink
refactor: modify build script to create a list of cardano stake distr…
Browse files Browse the repository at this point in the history
…ibution epochs
  • Loading branch information
dlachaume committed Aug 12, 2024
1 parent e9c84f4 commit 28e2586
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 10 deletions.
77 changes: 77 additions & 0 deletions internal/mithril-build-script/src/fake_aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ impl FakeAggregatorData {
"csd_hashes",
BTreeSet::from_iter(self.individual_csds.keys().cloned()),
),
generate_ids_array(
"csd_epochs",
BTreeSet::from_iter(extract_csd_epochs(&self.individual_csds)),
),
generate_ids_array(
"certificate_hashes",
BTreeSet::from_iter(self.individual_certificates.keys().cloned()),
Expand Down Expand Up @@ -143,6 +147,10 @@ impl FakeAggregatorData {
"csd_hashes",
BTreeSet::from_iter(self.individual_csds.keys().cloned()),
),
generate_ids_array(
"csd_epochs",
BTreeSet::from_iter(extract_csd_epochs(&self.individual_csds)),
),
generate_artifact_getter("csds", self.individual_csds),
generate_list_getter("csd_list", self.csds_list),
generate_ids_array(
Expand Down Expand Up @@ -200,6 +208,28 @@ impl FakeAggregatorData {
}
}

pub fn extract_csd_epochs(individual_csds: &BTreeMap<ArtifactId, FileContent>) -> Vec<String> {
individual_csds
.values()
.map(|content| {
let json_value: serde_json::Value =
serde_json::from_str(content).unwrap_or_else(|err| {
panic!(
"Failed to parse JSON in csd content: {}\nError: {}",
content, err
);
});

json_value
.get("epoch")
.and_then(|epoch| epoch.as_str().map(|s| s.to_owned()))
.unwrap_or_else(|| {
panic!("Epoch not found or invalid in csd content: {}", content);
})
})
.collect()
}

fn extract_artifact_id_and_content(
key: &String,
value: &serde_json::Value,
Expand Down Expand Up @@ -377,4 +407,51 @@ fn b() {}
]);
assert_eq!(expected, id_per_json);
}

#[test]
fn extract_csd_epochs_with_valid_data() {
let mut csds = BTreeMap::new();
csds.insert(
"csd-123".to_string(),
r#"{"hash": "csd-123", "epoch": "123"}"#.to_string(),
);
csds.insert(
"csd-456".to_string(),
r#"{"hash": "csd-456", "epoch": "456"}"#.to_string(),
);

let epochs = extract_csd_epochs(&csds);

assert_eq!(epochs, vec!["123".to_string(), "456".to_string()]);
}

#[test]
#[should_panic(expected = "Failed to parse JSON in csd content")]
fn extract_csd_epochs_with_invalid_json() {
let mut csds = BTreeMap::new();
csds.insert(
"csd-123".to_string(),
r#""hash": "csd-123", "epoch": "123""#.to_string(),
);

extract_csd_epochs(&csds);
}

#[test]
#[should_panic(expected = "Epoch not found or invalid in csd content")]
fn test_extract_csd_epochs_with_missing_epoch() {
let mut csds = BTreeMap::new();
csds.insert("csd-123".to_string(), r#"{"hash": "csd-123"}"#.to_string());

extract_csd_epochs(&csds);
}

#[test]
fn test_extract_csd_epochs_with_empty_map() {
let csds = BTreeMap::new();

let epochs = extract_csd_epochs(&csds);

assert!(epochs.is_empty());
}
}
52 changes: 52 additions & 0 deletions mithril-test-lab/mithril-aggregator-fake/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,4 +576,56 @@ mod tests {

test(task, PORT).await;
}

#[tokio::test]
async fn get_cardano_stake_distribution_by_epoch() {
const PORT: u16 = 3018;
let task = tokio::spawn(async move {
// Yield back to Tokio's scheduler to ensure the web server is ready before going on.
yield_now().await;

let path = "/artifact/cardano-stake-distribution/epoch/{epoch}";
let epoch = default_values::csd_epochs()[0];
let response = http_request(PORT, &path.replace("{epoch}", epoch)).await;

APISpec::verify_conformity(
get_spec_files(),
"GET",
path,
"application/json",
&Null,
&response,
&StatusCode::OK,
)
.map_err(|e| anyhow!(e))
});

test(task, PORT).await;
}

#[tokio::test]
async fn get_no_cardano_stake_distribution_by_epoch() {
const PORT: u16 = 3019;
let task = tokio::spawn(async move {
// Yield back to Tokio's scheduler to ensure the web server is ready before going on.
yield_now().await;

let path = "/artifact/cardano-stake-distribution/epoch/{epoch}";
let epoch = &u64::MAX.to_string();
let response = http_request(PORT, &path.replace("{epoch}", epoch)).await;

APISpec::verify_conformity(
get_spec_files(),
"GET",
path,
"application/json",
&Null,
&response,
&StatusCode::NOT_FOUND,
)
.map_err(|e| anyhow!(e))
});

test(task, PORT).await;
}
}
11 changes: 1 addition & 10 deletions mithril-test-lab/mithril-aggregator-fake/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,17 +452,8 @@ mod tests {

#[tokio::test]
async fn existing_csd_epoch() {
#[derive(Debug, serde::Deserialize)]
struct TmpCardanoStakeDistributionData {
epoch: u64,
}

let state: State<SharedState> = State(AppState::default().into());
let hash = default_values::csd_hashes()[0].to_string();
let csds = default_values::csds();
let tmp_csd_data: TmpCardanoStakeDistributionData =
serde_json::from_str(csds.get(&hash).unwrap()).unwrap();
let epoch = Path(tmp_csd_data.epoch.to_string());
let epoch = Path(default_values::csd_epochs()[0].to_string());

let response = csd_by_epoch(epoch, state)
.await
Expand Down

0 comments on commit 28e2586

Please sign in to comment.