8000 feat: generic snapshots by jeluard · Pull Request #233 · pragma-org/amaru · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: generic snapshots #233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion conformance-tests/scripts/generate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ const dreps = Object.keys(drepsDelegations)
});

// Relative source of the snapshot test in the target crate.
const source = "crates/amaru/src/ledger/rewards.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

Expand Down
11 changes: 11 additions & 0 deletions crates/amaru-kernel/src/network.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -308,6 +309,16 @@ impl std::str::FromStr for NetworkName {
}
}

impl From<NetworkName> 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 {
Expand Down
1 change: 1 addition & 0 deletions crates/amaru/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ rocksdb.workspace = true

[build-dependencies]
built = { workspace = true, features = ["git2"] }
amaru-kernel.workspace = true
22 changes: 22 additions & 0 deletions crates/amaru/tests/generated_compare_snapshot_test_cases.incl
Original file line number Diff line number Diff line change
@@ -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)
}
58 changes: 27 additions & 31 deletions crates/amaru/tests/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -65,51 +65,40 @@ fn db(epoch: Epoch) -> Arc<impl Snapshot + Send + Sync> {
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);

Expand All @@ -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;
}

Expand Down
91 changes: 91 additions & 0 deletions scripts/generate-snapshot-test-cases
Original file line number Diff line number Diff line change
@@ -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"
Loading
0