Skip to content

Commit e9a87bf

Browse files
committed
refactor(aggregator-fake): use gen methods from mithril-build-script
... to generate cardano stake distribution & cardano database lists per epoch. Note: this means adding `mithril-build-script` as a straight dependency for `mithril-aggregator-fake`, this is fine since it's a test only tool.
1 parent a2bd270 commit e9a87bf

File tree

2 files changed

+12
-118
lines changed

2 files changed

+12
-118
lines changed

mithril-test-lab/mithril-aggregator-fake/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ anyhow = { workspace = true }
1414
axum = { version = "0.8.6", features = ["tokio", "http1"] }
1515
clap = { workspace = true }
1616
futures = "0.3.31"
17+
mithril-build-script = { path = "../../internal/mithril-build-script" }
1718
serde = { workspace = true }
1819
serde_json = { workspace = true }
1920
signal-hook = "0.3.18"

mithril-test-lab/mithril-aggregator-fake/src/shared_state.rs

Lines changed: 11 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use anyhow::{Context, anyhow};
88
use tokio::sync::RwLock;
99
use tracing::{debug, trace};
1010

11+
use mithril_build_script::fake_aggregator::{extract_item_by_epoch, extract_item_list_per_epoch};
12+
1113
use crate::{StdResult, default_values};
1214

1315
pub struct AppState {
@@ -42,17 +44,6 @@ impl From<AppState> for SharedState {
4244

4345
impl Default for AppState {
4446
fn default() -> Self {
45-
let cardano_stake_distributions = default_values::cardano_stake_distributions();
46-
let cardano_stake_distributions_per_epoch =
47-
extract_cardano_stake_distribution_by_epoch(&cardano_stake_distributions)
48-
.expect("Embedded default values are not valid JSON");
49-
50-
let cardano_database_snapshot_list =
51-
default_values::cardano_database_snapshot_list().to_owned();
52-
let cardano_database_snapshot_list_per_epoch =
53-
extract_cardano_database_snapshots_for_epoch(&cardano_database_snapshot_list)
54-
.expect("Embedded default values are not valid JSON");
55-
5647
Self {
5748
status: default_values::status().to_owned(),
5849
epoch_settings: default_values::epoch_settings().to_owned(),
@@ -69,10 +60,13 @@ impl Default for AppState {
6960
cardano_transaction_proofs: default_values::cardano_transaction_proofs(),
7061
cardano_stake_distribution_list: default_values::cardano_stake_distribution_list()
7162
.to_owned(),
72-
cardano_stake_distributions,
73-
cardano_stake_distributions_per_epoch,
74-
cardano_database_snapshot_list,
75-
cardano_database_snapshot_list_per_epoch,
63+
cardano_stake_distributions: default_values::cardano_stake_distributions(),
64+
cardano_stake_distributions_per_epoch:
65+
default_values::cardano_stake_distributions_per_epoch(),
66+
cardano_database_snapshot_list: default_values::cardano_database_snapshot_list()
67+
.to_owned(),
68+
cardano_database_snapshot_list_per_epoch:
69+
default_values::cardano_database_snapshot_list_per_epoch(),
7670
cardano_database_snapshots: default_values::cardano_database_snapshots(),
7771
}
7872
}
@@ -99,9 +93,9 @@ impl AppState {
9993

10094
// derived values
10195
let cardano_stake_distributions_per_epoch =
102-
extract_cardano_stake_distribution_by_epoch(&cardano_stake_distributions)?;
96+
extract_item_by_epoch(&cardano_stake_distributions, "/epoch");
10397
let cardano_database_snapshot_list_per_epoch =
104-
extract_cardano_database_snapshots_for_epoch(&cardano_database_snapshot_list)?;
98+
extract_item_list_per_epoch(&cardano_database_snapshot_list, "/beacon/epoch");
10599

106100
let instance = Self {
107101
status,
@@ -338,44 +332,6 @@ fn parse_epoch_offset(epoch: &str) -> StdResult<Option<usize>> {
338332
.with_context(|| "invalid epoch offset")
339333
}
340334

341-
fn extract_cardano_stake_distribution_by_epoch(
342-
source: &BTreeMap<String, String>,
343-
) -> StdResult<BTreeMap<u64, String>> {
344-
let mut res = BTreeMap::new();
345-
346-
for (key, value) in source {
347-
let parsed_json: serde_json::Value = serde_json::from_str(value)
348-
.with_context(|| format!("Could not parse JSON entity '{key}'"))?;
349-
let epoch = parsed_json
350-
.pointer("/epoch")
351-
.with_context(|| format!("missing epoch for JSON entity '{key}'"))?
352-
.as_u64()
353-
.with_context(|| format!("epoch is not a number for JSON entity '{key}'"))?;
354-
res.insert(epoch, value.clone());
355-
}
356-
357-
Ok(res)
358-
}
359-
360-
fn extract_cardano_database_snapshots_for_epoch(source: &str) -> StdResult<BTreeMap<u64, String>> {
361-
let parsed_json: Vec<serde_json::Value> = serde_json::from_str(source)?;
362-
let mut cardano_db_snapshots_per_epoch = BTreeMap::<u64, Vec<serde_json::Value>>::new();
363-
364-
for item in parsed_json {
365-
let epoch = item
366-
.pointer("/beacon/epoch")
367-
.with_context(|| "missing beacon.epoch for a json value")?
368-
.as_u64()
369-
.with_context(|| "beacon.epoch is not a number")?;
370-
cardano_db_snapshots_per_epoch.entry(epoch).or_default().push(item);
371-
}
372-
373-
Ok(cardano_db_snapshots_per_epoch
374-
.into_iter()
375-
.map(|(k, v)| (k, serde_json::to_string(&v).unwrap()))
376-
.collect())
377-
}
378-
379335
#[cfg(test)]
380336
mod tests {
381337
use super::*;
@@ -385,67 +341,4 @@ mod tests {
385341
AppState::from_directory(Path::new("./default_data"))
386342
.expect("Should be able to construct an AppState from the default_data");
387343
}
388-
389-
#[test]
390-
fn test_extract_cardano_stake_distribution_by_epoch() {
391-
let source = BTreeMap::from([
392-
(
393-
"hash1".to_string(),
394-
r#"{"bar":4,"epoch":3,"foo":"...","hash":"2"}"#.to_string(),
395-
),
396-
(
397-
"hash2".to_string(),
398-
r#"{"bar":7,"epoch":2,"foo":"...","hash":"1"}"#.to_string(),
399-
),
400-
]);
401-
let extracted = extract_cardano_stake_distribution_by_epoch(&source).unwrap();
402-
403-
// note: values are not re-serialized, so they are kept as is
404-
assert_eq!(
405-
BTreeMap::from([
406-
(3, source.get("hash1").unwrap().to_string()),
407-
(2, source.get("hash2").unwrap().to_string())
408-
]),
409-
extracted,
410-
)
411-
}
412-
413-
#[test]
414-
fn test_extract_cardano_database_snapshots_for_epoch() {
415-
let extracted = extract_cardano_database_snapshots_for_epoch(
416-
r#"[
417-
{ "beacon": { "epoch": 1, "bar": 4 }, "hash":"3","foo":"..." },
418-
{ "beacon": { "epoch": 2}, "hash":"2","foo":"..." },
419-
{ "beacon": { "epoch": 1}, "hash":"1","foo":"..." }
420-
]"#,
421-
)
422-
.unwrap();
423-
424-
// note: values are re-serialized, so serde_json reorders the keys
425-
assert_eq!(
426-
BTreeMap::from([
427-
(
428-
1,
429-
r#"[{"beacon":{"bar":4,"epoch":1},"foo":"...","hash":"3"},{"beacon":{"epoch":1},"foo":"...","hash":"1"}]"#
430-
.to_string()
431-
),
432-
(2, r#"[{"beacon":{"epoch":2},"foo":"...","hash":"2"}]"#.to_string()),
433-
]),
434-
extracted,
435-
)
436-
}
437-
438-
#[test]
439-
fn extract_cardano_stake_distribution_by_epoch_from_default_data_dont_panic() {
440-
extract_cardano_stake_distribution_by_epoch(&default_values::cardano_stake_distributions())
441-
.unwrap();
442-
}
443-
444-
#[test]
445-
fn extract_cardano_database_snapshots_for_epoch_from_default_data_dont_panic() {
446-
extract_cardano_database_snapshots_for_epoch(
447-
default_values::cardano_database_snapshot_list(),
448-
)
449-
.unwrap();
450-
}
451344
}

0 commit comments

Comments
 (0)