Skip to content
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
92 changes: 76 additions & 16 deletions common/examples/test_streaming_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,28 +275,88 @@ impl SnapshotCallbacks for CountingCallbacks {
impl SnapshotsCallback for CountingCallbacks {
fn on_snapshots(&mut self, snapshots: RawSnapshotsContainer) -> Result<()> {
eprintln!("Raw Snapshots Data:");
eprintln!();

// Calculate total stakes and delegator counts from VMap data
let mark_total: i64 = snapshots.mark.0.iter().map(|(_, amount)| amount).sum();
let set_total: i64 = snapshots.set.0.iter().map(|(_, amount)| amount).sum();
let go_total: i64 = snapshots.go.0.iter().map(|(_, amount)| amount).sum();

eprintln!(
" Mark snapshot: {} delegators, {} total stake (ADA)",
snapshots.mark.0.len(),
mark_total as f64 / 1_000_000.0
);
eprintln!(
" Set snapshot: {} delegators, {} total stake (ADA)",
snapshots.set.0.len(),
set_total as f64 / 1_000_000.0
);
eprintln!(
" Go snapshot: {} delegators, {} total stake (ADA)",
snapshots.go.0.len(),
go_total as f64 / 1_000_000.0
);
eprintln!(" Fee: {} ADA", snapshots.fee as f64 / 1_000_000.0);
eprintln!("Mark Snapshot:");
eprintln!(" Delegators: {}", snapshots.mark.0.len());
eprintln!(" Total stake: {:.2} ADA", mark_total as f64 / 1_000_000.0);
if !snapshots.mark.0.is_empty() {
eprintln!(" Sample stakes (first 5):");
for (i, (cred, amount)) in snapshots.mark.0.iter().take(5).enumerate() {
let cred_str = match cred {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I know this is just for testing, but the StakeCredential type has a to_string() implementation that you could use instead of doing this matching on the type of credential in a few places.

impl StakeCredential {
    pub fn to_string(&self) -> Result<String> {
        let (hrp, data) = match &self {
            Self::AddrKeyHash(data) => (Hrp::parse("stake_vkh")?, data.as_slice()),
            Self::ScriptHash(data) => (Hrp::parse("script")?, data.as_slice()),
        };

        Ok(bech32::encode::<Bech32>(hrp, data)?)
    }
}

I think the whole bech-32 encoding would be more useful for debugging than using 8 hex characters in this case.

acropolis_common::StakeCredential::AddrKeyHash(h) => {
format!("KeyHash({}...)", &hex::encode(&h[..4]))
}
acropolis_common::StakeCredential::ScriptHash(h) => {
format!("ScriptHash({}...)", &hex::encode(&h[..4]))
}
};
eprintln!(
" [{}] {} -> {:.2} ADA",
i + 1,
cred_str,
*amount as f64 / 1_000_000.0
);
}
}
eprintln!();

eprintln!("Set Snapshot:");
eprintln!(" Delegators: {}", snapshots.set.0.len());
eprintln!(" Total stake: {:.2} ADA", set_total as f64 / 1_000_000.0);
if !snapshots.set.0.is_empty() {
eprintln!(" Sample stakes (first 5):");
for (i, (cred, amount)) in snapshots.set.0.iter().take(5).enumerate() {
let cred_str = match cred {
acropolis_common::StakeCredential::AddrKeyHash(h) => {
format!("KeyHash({}...)", &hex::encode(&h[..4]))
}
acropolis_common::StakeCredential::ScriptHash(h) => {
format!("ScriptHash({}...)", &hex::encode(&h[..4]))
}
};
eprintln!(
" [{}] {} -> {:.2} ADA",
i + 1,
cred_str,
*amount as f64 / 1_000_000.0
);
}
}
eprintln!();

eprintln!("Go Snapshot:");
eprintln!(" Delegators: {}", snapshots.go.0.len());
eprintln!(" Total stake: {:.2} ADA", go_total as f64 / 1_000_000.0);
if !snapshots.go.0.is_empty() {
eprintln!(" Sample stakes (first 5):");
for (i, (cred, amount)) in snapshots.go.0.iter().take(5).enumerate() {
let cred_str = match cred {
acropolis_common::StakeCredential::AddrKeyHash(h) => {
format!("KeyHash({}...)", &hex::encode(&h[..4]))
}
acropolis_common::StakeCredential::ScriptHash(h) => {
format!("ScriptHash({}...)", &hex::encode(&h[..4]))
}
};
Comment on lines +291 to +346
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] There is duplicated code for formatting stake credentials across the three snapshot sections (Mark, Set, Go). The same pattern is repeated at lines 291-298, 315-322, and 339-346.

Consider extracting this into a helper function to reduce code duplication:

fn format_credential(cred: &StakeCredential) -> String {
    match cred {
        acropolis_common::StakeCredential::AddrKeyHash(h) => {
            format!("KeyHash({}...)", &hex::encode(&h[..4]))
        }
        acropolis_common::StakeCredential::ScriptHash(h) => {
            format!("ScriptHash({}...)", &hex::encode(&h[..4]))
        }
    }
}

Copilot uses AI. Check for mistakes.
eprintln!(
" [{}] {} -> {:.2} ADA",
i + 1,
cred_str,
*amount as f64 / 1_000_000.0
);
}
}
eprintln!();

eprintln!("Fee: {:.2} ADA", snapshots.fee as f64 / 1_000_000.0);
eprintln!();

Ok(())
}
}
Expand Down
Loading