diff --git a/conformance-tests/scripts/generate.mjs b/conformance-tests/scripts/generate.mjs index fa9a7d6ff..34a532c42 100644 --- a/conformance-tests/scripts/generate.mjs +++ b/conformance-tests/scripts/generate.mjs @@ -67,7 +67,12 @@ const dreps = drepsInfo }); // Relative source of the snapshot test in the target crate. -const source = "crates/amaru/tests/summary.rs"; +const source = "crates/amaru-ledger/src/summary/rewards.rs"; +const exists = fs.existsSync(`../${source}`); +if (!exists) { + console.error(`Source file ${source} does not exist.`); + process.exit(1); +} // ---------- Rewards summary snapshot diff --git a/crates/amaru-kernel/src/network.rs b/crates/amaru-kernel/src/network.rs index e179bb65e..8e6a67e2b 100644 --- a/crates/amaru-kernel/src/network.rs +++ b/crates/amaru-kernel/src/network.rs @@ -14,6 +14,7 @@ use std::{fs::File, io::BufReader, path::Path, sync::LazyLock}; +use pallas_addresses::Network; pub use slot_arithmetic::{Bound, EraHistory, EraParams, Summary}; use slot_arithmetic::{Epoch, Slot}; @@ -308,6 +309,16 @@ impl std::str::FromStr for NetworkName { } } +impl From for Network { + fn from(value: NetworkName) -> Self { + if value == NetworkName::Mainnet { + Network::Mainnet + } else { + Network::Testnet + } + } +} + impl NetworkName { pub fn to_network_magic(self) -> u32 { match self { diff --git a/crates/amaru/Cargo.toml b/crates/amaru/Cargo.toml index 3abb73478..cb0258c60 100644 --- a/crates/amaru/Cargo.toml +++ b/crates/amaru/Cargo.toml @@ -60,3 +60,4 @@ rocksdb.workspace = true [build-dependencies] built = { workspace = true, features = ["git2"] } +amaru-kernel.workspace = true \ No newline at end of file diff --git a/crates/amaru/tests/generated_compare_snapshot_test_cases.incl b/crates/amaru/tests/generated_compare_snapshot_test_cases.incl new file mode 100644 index 000000000..b9e7a505d --- /dev/null +++ b/crates/amaru/tests/generated_compare_snapshot_test_cases.incl @@ -0,0 +1,22 @@ +#[test_case(NetworkName::Preprod, 163)] +#[test_case(NetworkName::Preprod, 164)] +#[test_case(NetworkName::Preprod, 165)] +#[test_case(NetworkName::Preprod, 166)] +#[test_case(NetworkName::Preprod, 167)] +#[test_case(NetworkName::Preprod, 168)] +#[test_case(NetworkName::Preprod, 169)] +#[test_case(NetworkName::Preprod, 170)] +#[test_case(NetworkName::Preprod, 171)] +#[test_case(NetworkName::Preprod, 172)] +#[test_case(NetworkName::Preprod, 173)] +#[test_case(NetworkName::Preprod, 174)] +#[test_case(NetworkName::Preprod, 175)] +#[test_case(NetworkName::Preprod, 176)] +#[test_case(NetworkName::Preprod, 177)] +#[test_case(NetworkName::Preprod, 178)] +#[test_case(NetworkName::Preprod, 179)] +#[ignore] +pub fn compare_snapshot_test_case(network_name: NetworkName, epoch: u64) { + let epoch = Epoch::from(epoch); + compare_snapshot(network_name, epoch) +} diff --git a/crates/amaru/tests/snapshots/summary__rewards_summary_163.snap b/crates/amaru/tests/snapshots/preprod/summary__rewards_summary_163.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__rewards_summary_163.snap rename to crates/amaru/tests/snapshots/preprod/summary__rewards_summary_163.snap diff --git a/crates/amaru/tests/snapshots/summary__rewards_summary_164.snap b/crates/amaru/tests/snapshots/preprod/summary__rewards_summary_164.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__rewards_summary_164.snap rename to crates/amaru/tests/snapshots/preprod/summary__rewards_summary_164.snap diff --git a/crates/amaru/tests/snapshots/summary__rewards_summary_165.snap b/crates/amaru/tests/snapshots/preprod/summary__rewards_summary_165.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__rewards_summary_165.snap rename to crates/amaru/tests/snapshots/preprod/summary__rewards_summary_165.snap diff --git a/crates/amaru/tests/snapshots/summary__rewards_summary_166.snap b/crates/amaru/tests/snapshots/preprod/summary__rewards_summary_166.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__rewards_summary_166.snap rename to crates/amaru/tests/snapshots/preprod/summary__rewards_summary_166.snap diff --git a/crates/amaru/tests/snapshots/summary__rewards_summary_167.snap b/crates/amaru/tests/snapshots/preprod/summary__rewards_summary_167.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__rewards_summary_167.snap rename to crates/amaru/tests/snapshots/preprod/summary__rewards_summary_167.snap diff --git a/crates/amaru/tests/snapshots/summary__rewards_summary_168.snap b/crates/amaru/tests/snapshots/preprod/summary__rewards_summary_168.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__rewards_summary_168.snap rename to crates/amaru/tests/snapshots/preprod/summary__rewards_summary_168.snap diff --git a/crates/amaru/tests/snapshots/summary__rewards_summary_169.snap b/crates/amaru/tests/snapshots/preprod/summary__rewards_summary_169.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__rewards_summary_169.snap rename to crates/amaru/tests/snapshots/preprod/summary__rewards_summary_169.snap diff --git a/crates/amaru/tests/snapshots/summary__rewards_summary_170.snap b/crates/amaru/tests/snapshots/preprod/summary__rewards_summary_170.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__rewards_summary_170.snap rename to crates/amaru/tests/snapshots/preprod/summary__rewards_summary_170.snap diff --git a/crates/amaru/tests/snapshots/summary__rewards_summary_171.snap b/crates/amaru/tests/snapshots/preprod/summary__rewards_summary_171.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__rewards_summary_171.snap rename to crates/amaru/tests/snapshots/preprod/summary__rewards_summary_171.snap diff --git a/crates/amaru/tests/snapshots/summary__rewards_summary_172.snap b/crates/amaru/tests/snapshots/preprod/summary__rewards_summary_172.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__rewards_summary_172.snap rename to crates/amaru/tests/snapshots/preprod/summary__rewards_summary_172.snap diff --git a/crates/amaru/tests/snapshots/summary__rewards_summary_173.snap b/crates/amaru/tests/snapshots/preprod/summary__rewards_summary_173.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__rewards_summary_173.snap rename to crates/amaru/tests/snapshots/preprod/summary__rewards_summary_173.snap diff --git a/crates/amaru/tests/snapshots/summary__rewards_summary_174.snap b/crates/amaru/tests/snapshots/preprod/summary__rewards_summary_174.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__rewards_summary_174.snap rename to crates/amaru/tests/snapshots/preprod/summary__rewards_summary_174.snap diff --git a/crates/amaru/tests/snapshots/summary__rewards_summary_175.snap b/crates/amaru/tests/snapshots/preprod/summary__rewards_summary_175.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__rewards_summary_175.snap rename to crates/amaru/tests/snapshots/preprod/summary__rewards_summary_175.snap diff --git a/crates/amaru/tests/snapshots/summary__rewards_summary_176.snap b/crates/amaru/tests/snapshots/preprod/summary__rewards_summary_176.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__rewards_summary_176.snap rename to crates/amaru/tests/snapshots/preprod/summary__rewards_summary_176.snap diff --git a/crates/amaru/tests/snapshots/summary__rewards_summary_177.snap b/crates/amaru/tests/snapshots/preprod/summary__rewards_summary_177.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__rewards_summary_177.snap rename to crates/amaru/tests/snapshots/preprod/summary__rewards_summary_177.snap diff --git a/crates/amaru/tests/snapshots/summary__rewards_summary_178.snap b/crates/amaru/tests/snapshots/preprod/summary__rewards_summary_178.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__rewards_summary_178.snap rename to crates/amaru/tests/snapshots/preprod/summary__rewards_summary_178.snap diff --git a/crates/amaru/tests/snapshots/summary__rewards_summary_179.snap b/crates/amaru/tests/snapshots/preprod/summary__rewards_summary_179.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__rewards_summary_179.snap rename to crates/amaru/tests/snapshots/preprod/summary__rewards_summary_179.snap diff --git a/crates/amaru/tests/snapshots/summary__stake_distribution_163.snap b/crates/amaru/tests/snapshots/preprod/summary__stake_distribution_163.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__stake_distribution_163.snap rename to crates/amaru/tests/snapshots/preprod/summary__stake_distribution_163.snap diff --git a/crates/amaru/tests/snapshots/summary__stake_distribution_164.snap b/crates/amaru/tests/snapshots/preprod/summary__stake_distribution_164.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__stake_distribution_164.snap rename to crates/amaru/tests/snapshots/preprod/summary__stake_distribution_164.snap diff --git a/crates/amaru/tests/snapshots/summary__stake_distribution_165.snap b/crates/amaru/tests/snapshots/preprod/summary__stake_distribution_165.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__stake_distribution_165.snap rename to crates/amaru/tests/snapshots/preprod/summary__stake_distribution_165.snap diff --git a/crates/amaru/tests/snapshots/summary__stake_distribution_166.snap b/crates/amaru/tests/snapshots/preprod/summary__stake_distribution_166.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__stake_distribution_166.snap rename to crates/amaru/tests/snapshots/preprod/summary__stake_distribution_166.snap diff --git a/crates/amaru/tests/snapshots/summary__stake_distribution_167.snap b/crates/amaru/tests/snapshots/preprod/summary__stake_distribution_167.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__stake_distribution_167.snap rename to crates/amaru/tests/snapshots/preprod/summary__stake_distribution_167.snap diff --git a/crates/amaru/tests/snapshots/summary__stake_distribution_168.snap b/crates/amaru/tests/snapshots/preprod/summary__stake_distribution_168.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__stake_distribution_168.snap rename to crates/amaru/tests/snapshots/preprod/summary__stake_distribution_168.snap diff --git a/crates/amaru/tests/snapshots/summary__stake_distribution_169.snap b/crates/amaru/tests/snapshots/preprod/summary__stake_distribution_169.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__stake_distribution_169.snap rename to crates/amaru/tests/snapshots/preprod/summary__stake_distribution_169.snap diff --git a/crates/amaru/tests/snapshots/summary__stake_distribution_170.snap b/crates/amaru/tests/snapshots/preprod/summary__stake_distribution_170.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__stake_distribution_170.snap rename to crates/amaru/tests/snapshots/preprod/summary__stake_distribution_170.snap diff --git a/crates/amaru/tests/snapshots/summary__stake_distribution_171.snap b/crates/amaru/tests/snapshots/preprod/summary__stake_distribution_171.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__stake_distribution_171.snap rename to crates/amaru/tests/snapshots/preprod/summary__stake_distribution_171.snap diff --git a/crates/amaru/tests/snapshots/summary__stake_distribution_172.snap b/crates/amaru/tests/snapshots/preprod/summary__stake_distribution_172.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__stake_distribution_172.snap rename to crates/amaru/tests/snapshots/preprod/summary__stake_distribution_172.snap diff --git a/crates/amaru/tests/snapshots/summary__stake_distribution_173.snap b/crates/amaru/tests/snapshots/preprod/summary__stake_distribution_173.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__stake_distribution_173.snap rename to crates/amaru/tests/snapshots/preprod/summary__stake_distribution_173.snap diff --git a/crates/amaru/tests/snapshots/summary__stake_distribution_174.snap b/crates/amaru/tests/snapshots/preprod/summary__stake_distribution_174.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__stake_distribution_174.snap rename to crates/amaru/tests/snapshots/preprod/summary__stake_distribution_174.snap diff --git a/crates/amaru/tests/snapshots/summary__stake_distribution_175.snap b/crates/amaru/tests/snapshots/preprod/summary__stake_distribution_175.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__stake_distribution_175.snap rename to crates/amaru/tests/snapshots/preprod/summary__stake_distribution_175.snap diff --git a/crates/amaru/tests/snapshots/summary__stake_distribution_176.snap b/crates/amaru/tests/snapshots/preprod/summary__stake_distribution_176.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__stake_distribution_176.snap rename to crates/amaru/tests/snapshots/preprod/summary__stake_distribution_176.snap diff --git a/crates/amaru/tests/snapshots/summary__stake_distribution_177.snap b/crates/amaru/tests/snapshots/preprod/summary__stake_distribution_177.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__stake_distribution_177.snap rename to crates/amaru/tests/snapshots/preprod/summary__stake_distribution_177.snap diff --git a/crates/amaru/tests/snapshots/summary__stake_distribution_178.snap b/crates/amaru/tests/snapshots/preprod/summary__stake_distribution_178.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__stake_distribution_178.snap rename to crates/amaru/tests/snapshots/preprod/summary__stake_distribution_178.snap diff --git a/crates/amaru/tests/snapshots/summary__stake_distribution_179.snap b/crates/amaru/tests/snapshots/preprod/summary__stake_distribution_179.snap similarity index 100% rename from crates/amaru/tests/snapshots/summary__stake_distribution_179.snap rename to crates/amaru/tests/snapshots/preprod/summary__stake_distribution_179.snap diff --git a/crates/amaru/tests/summary.rs b/crates/amaru/tests/summary.rs index de3cecd1a..d5763fb57 100644 --- a/crates/amaru/tests/summary.rs +++ b/crates/amaru/tests/summary.rs @@ -15,7 +15,7 @@ use amaru_kernel::{ network::NetworkName, protocol_parameters::{GlobalParameters, ProtocolParameters}, - Network, ProtocolVersion, PROTOCOL_VERSION_10, PROTOCOL_VERSION_9, + ProtocolVersion, PROTOCOL_VERSION_10, PROTOCOL_VERSION_9, }; use amaru_ledger::{ store::Snapshot, @@ -65,51 +65,40 @@ fn db(epoch: Epoch) -> Arc { handle } -#[test_case(163)] -#[test_case(164)] -#[test_case(165)] -#[test_case(166)] -#[test_case(167)] -#[test_case(168)] -#[test_case(169)] -#[test_case(170)] -#[test_case(171)] -#[test_case(172)] -#[test_case(173)] -#[test_case(174)] -#[test_case(175)] -#[test_case(176)] -#[test_case(177)] -#[test_case(178)] -#[test_case(179)] -#[ignore] +include!("generated_compare_snapshot_test_cases.incl"); + #[allow(clippy::unwrap_used)] -fn compare_preprod_snapshot(epoch: u64) { - let epoch = Epoch::from(epoch); +fn compare_snapshot(network_name: NetworkName, epoch: Epoch) { let network = NetworkName::Preprod; let snapshot = db(epoch); let global_parameters: &GlobalParameters = network.into(); let protocol_parameters = ProtocolParameters::default(); + let protocol_version = protocol_version(epoch, network_name); let dreps = GovernanceSummary::new( snapshot.as_ref(), - preprod_protocol_version(epoch), - network.into(), + protocol_version, + network_name.into(), &protocol_parameters, ) .unwrap(); let stake_distr = StakeDistribution::new( snapshot.as_ref(), - preprod_protocol_version(epoch), + protocol_version, dreps, &protocol_parameters, ) .unwrap(); - insta::assert_json_snapshot!( - format!("stake_distribution_{}", epoch), - stake_distr.for_network(Network::Testnet), - ); + + insta::with_settings!({ + snapshot_path => format!("snapshots/{}", network_name) + }, { + insta::assert_json_snapshot!( + format!("stake_distribution_{}", epoch), + stake_distr.for_network(network_name.into()), + ); + }); let snapshot_from_the_future = db(epoch + 2); @@ -123,11 +112,18 @@ fn compare_preprod_snapshot(epoch: u64) { .with_unclaimed_refunds(snapshot_from_the_future.as_ref(), &protocol_parameters) .unwrap(); - insta::assert_json_snapshot!(format!("rewards_summary_{}", epoch), rewards_summary); + insta::with_settings!({ + snapshot_path => format!("snapshots/{}", network_name) + }, { + insta::assert_json_snapshot!( + format!("rewards_summary_{}", epoch), + rewards_summary + ); + }); } -fn preprod_protocol_version(epoch: Epoch) -> ProtocolVersion { - if epoch <= Epoch::from(180) { +fn protocol_version(epoch: Epoch, network_name: NetworkName) -> ProtocolVersion { + if network_name == NetworkName::Preprod && epoch <= Epoch::from(180) { return PROTOCOL_VERSION_9; } diff --git a/scripts/generate-snapshot-test-cases b/scripts/generate-snapshot-test-cases new file mode 100755 index 000000000..a6fc824ef --- /dev/null +++ b/scripts/generate-snapshot-test-cases @@ -0,0 +1,91 @@ +#!/bin/bash + +# Must match what is included in `crates/amaru/tests/summary.rs` +DEST_PATH="crates/amaru/tests/generated_compare_snapshot_test_cases.incl" +if [[ ! -f "$DEST_PATH" ]]; then + echo "Error: File $DEST_PATH does not exist" + exit 1 +fi + +SNAPSHOTS_ROOT="crates/amaru/tests/snapshots" + +# Check if snapshots directory exists +if [[ ! -d "$SNAPSHOTS_ROOT" ]]; then + echo "Error: Directory $SNAPSHOTS_ROOT does not exist" + exit 1 +fi + +network_name_to_string() { + local network="$1" + case "$network" in + "mainnet") + echo "Mainnet" + ;; + "preprod") + echo "Preprod" + ;; + "preview") + echo "Preview" + ;; + testnet*) + # Extract magic number if it's a testnet variant + if [[ "$network" =~ ^testnet[_-]?([0-9]+)$ ]]; then + echo "Testnet:${BASH_REMATCH[1]}" + else + echo "Testnet:0" # Default magic number + fi + ;; + *) + # Fallback: capitalize first letter + echo "${network^}" + ;; + esac +} + +# Iterate through network directories +for NETWORK_PATH in "$SNAPSHOTS_ROOT"/*; do + if [[ -d "$NETWORK_PATH" ]]; then + NETWORK_NAME=$(basename "$NETWORK_PATH") + + # Iterate through files in network directory + for FILE_PATH in "$NETWORK_PATH"/*; do + if [[ -f "$FILE_PATH" ]]; then + FILE_NAME=$(basename "$FILE_PATH") + + # Check if filename matches pattern: summary__rewards_summary_*.snap + if [[ "$FILE_NAME" =~ ^summary__rewards_summary_([0-9]+)\.snap$ ]]; then + EPOCH="${BASH_REMATCH[1]}" + # Add to test_cases array + TEST_CASES+=("$NETWORK_NAME:$EPOCH") + fi + fi + done + fi +done + +# Sort test cases +IFS=$'\n' TEST_CASES=($(sort <<<"${TEST_CASES[*]}")) +unset IFS + +# Create/truncate the output file +> "$DEST_PATH" + +# Write test cases to file +for TEST_CASE in "${TEST_CASES[@]}"; do + NETWORK="${TEST_CASE%:*}" + EPOCH="${TEST_CASE#*:}" + + NETWORK_ENUM=$(network_name_to_string "$NETWORK") + echo "#[test_case(NetworkName::$NETWORK_ENUM, $EPOCH)]" >> "$DEST_PATH" +done + +# Append the test function content +cat >> "$DEST_PATH" << 'EOF' +#[ignore] +pub fn compare_snapshot_test_case(network_name: NetworkName, epoch: u64) { + let epoch = Epoch::from(epoch); + compare_snapshot(network_name, epoch) +} +EOF + +echo "Generated test cases written to: $DEST_PATH" \ No newline at end of file