Skip to content

Commit 017fbe8

Browse files
committed
Swapped out the hash::StakeCredential for the one in types and changed the order of it's elements to match Haskell Node CBOR format
1 parent fd7d2ac commit 017fbe8

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

common/src/snapshot/streaming_snapshot.rs

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use std::io::{Read, Seek, SeekFrom};
3030

3131
pub use crate::hash::{AddrKeyhash, Hash, ScriptHash};
3232
pub use crate::stake_addresses::{AccountState, StakeAddressState};
33+
pub use crate::StakeCredential;
3334

3435
// -----------------------------------------------------------------------------
3536
// Cardano Ledger Types (for decoding with minicbor)
@@ -38,22 +39,41 @@ pub use crate::stake_addresses::{AccountState, StakeAddressState};
3839
pub type Epoch = u64;
3940
pub type Lovelace = u64;
4041

42+
/*
43+
* This was replaced with the StakeCredential defined in types.rs, but the implementation here is much
44+
* cleaner for parsing CBOR files from Haskell Node, using hash.rs types. For CBOR parsing, we need to
45+
* change the decode from using d.decode_with(ctx) (which expects arrays) tousing d.bytes() which
46+
* expects raw bytes.
4147
/// Stake credential - can be a key hash or script hash
4248
/// Order matters for Ord/PartialOrd - ScriptHash must come first for compatibility with Haskell
4349
#[derive(Serialize, Deserialize, Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Hash)]
4450
pub enum StakeCredential {
4551
ScriptHash(ScriptHash),
46-
AddrKeyhash(AddrKeyhash),
52+
AddrKeyhash(AddrKeyhash), // NOTE: lower case h from hash.rs version
4753
}
54+
*/
4855

4956
impl<'b, C> minicbor::decode::Decode<'b, C> for StakeCredential {
50-
fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> {
57+
fn decode(
58+
d: &mut minicbor::Decoder<'b>,
59+
_ctx: &mut C,
60+
) -> Result<Self, minicbor::decode::Error> {
5161
d.array()?;
5262
let variant = d.u16()?;
5363

5464
match variant {
55-
0 => Ok(StakeCredential::AddrKeyhash(d.decode_with(ctx)?)),
56-
1 => Ok(StakeCredential::ScriptHash(d.decode_with(ctx)?)),
65+
0 => {
66+
// ScriptHash variant (first in enum) - decode bytes directly
67+
let bytes = d.bytes()?;
68+
let key_hash = bytes.to_vec();
69+
Ok(StakeCredential::ScriptHash(key_hash))
70+
}
71+
1 => {
72+
// AddrKeyHash variant (second in enum) - decode bytes directly
73+
let bytes = d.bytes()?;
74+
let key_hash = bytes.to_vec();
75+
Ok(StakeCredential::AddrKeyHash(key_hash))
76+
}
5777
_ => Err(minicbor::decode::Error::message(
5878
"invalid variant id for StakeCredential",
5979
)),
@@ -68,18 +88,18 @@ impl<C> minicbor::encode::Encode<C> for StakeCredential {
6888
ctx: &mut C,
6989
) -> Result<(), minicbor::encode::Error<W::Error>> {
7090
match self {
71-
StakeCredential::AddrKeyhash(a) => {
91+
StakeCredential::ScriptHash(key_hash) => {
92+
// ScriptHash is variant 0 (first in enum)
7293
e.array(2)?;
7394
e.encode_with(0, ctx)?;
74-
e.encode_with(a, ctx)?;
75-
95+
e.encode_with(key_hash, ctx)?;
7696
Ok(())
7797
}
78-
StakeCredential::ScriptHash(a) => {
98+
StakeCredential::AddrKeyHash(key_hash) => {
99+
// AddrKeyHash is variant 1 (second in enum)
79100
e.array(2)?;
80101
e.encode_with(1, ctx)?;
81-
e.encode_with(a, ctx)?;
82-
102+
e.encode_with(key_hash, ctx)?;
83103
Ok(())
84104
}
85105
}
@@ -1009,7 +1029,7 @@ impl StreamingSnapshotParser {
10091029
.map(|(credential, account)| {
10101030
// Convert StakeCredential to stake address representation
10111031
let stake_address = match &credential {
1012-
StakeCredential::AddrKeyhash(hash) => {
1032+
StakeCredential::AddrKeyHash(hash) => {
10131033
format!("stake_key_{}", hex::encode(hash))
10141034
}
10151035
StakeCredential::ScriptHash(hash) => {
@@ -1398,7 +1418,7 @@ impl StreamingSnapshotParser {
13981418
.map(|(credential, account)| {
13991419
// Convert StakeCredential to stake address representation
14001420
let stake_address = match &credential {
1401-
StakeCredential::AddrKeyhash(hash) => {
1421+
StakeCredential::AddrKeyHash(hash) => {
14021422
format!("stake_key_{}", hex::encode(hash))
14031423
}
14041424
StakeCredential::ScriptHash(hash) => {
@@ -1806,8 +1826,10 @@ impl StreamingSnapshotParser {
18061826
.into_iter()
18071827
.map(|(cred, state)| {
18081828
let drep_id = match cred {
1809-
StakeCredential::AddrKeyhash(hash) => format!("drep_{}", hash),
1810-
StakeCredential::ScriptHash(hash) => format!("drep_script_{}", hash),
1829+
StakeCredential::AddrKeyHash(hash) => format!("drep_{}", hex::encode(hash)),
1830+
StakeCredential::ScriptHash(hash) => {
1831+
format!("drep_script_{}", hex::encode(hash))
1832+
}
18111833
};
18121834

18131835
let anchor = match state.anchor {

common/src/types.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -600,11 +600,11 @@ pub struct PotDelta {
600600
Debug, Clone, Ord, Eq, PartialEq, PartialOrd, Hash, serde::Serialize, serde::Deserialize,
601601
)]
602602
pub enum Credential {
603+
/// Script hash. NOTE: Order matters when parsing Haskell Node Snapshot data.
604+
ScriptHash(KeyHash),
605+
603606
/// Address key hash
604607
AddrKeyHash(KeyHash),
605-
606-
/// Script hash
607-
ScriptHash(KeyHash),
608608
}
609609

610610
impl Credential {

modules/accounts_state/src/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,7 @@ mod tests {
14321432
DRepDelegationDistribution {
14331433
abstain: 10_000,
14341434
no_confidence: 100_000,
1435-
dreps: vec![(drep_addr_cred, 1_000_100), (drep_script_cred, 2_001_000),],
1435+
dreps: vec![(drep_script_cred, 2_001_000), (drep_addr_cred, 1_000_100)],
14361436
}
14371437
);
14381438

0 commit comments

Comments
 (0)