From c7e6ab978bb082c6a7929cfc5d7f489b9dfefce6 Mon Sep 17 00:00:00 2001 From: Matthew Hounslow Date: Thu, 23 Oct 2025 14:39:38 -0700 Subject: [PATCH 1/4] Refactor: Replace `StakeAddressPayload` with `StakeCredential` for consistency across modules. --- codec/src/map_parameters.rs | 14 +- common/src/address.rs | 131 +++++++++--------- common/src/stake_addresses.rs | 4 +- common/src/types.rs | 11 ++ modules/accounts_state/src/snapshot.rs | 4 +- modules/accounts_state/src/state.rs | 4 +- .../rest_blockfrost/src/handlers/epochs.rs | 8 +- modules/stake_delta_filter/src/utils.rs | 18 +-- 8 files changed, 102 insertions(+), 92 deletions(-) diff --git a/codec/src/map_parameters.rs b/codec/src/map_parameters.rs index d757d354..882cfd54 100644 --- a/codec/src/map_parameters.rs +++ b/codec/src/map_parameters.rs @@ -69,12 +69,12 @@ pub fn map_address(address: &addresses::Address) -> Result
{ addresses::Address::Stake(stake_address) => Ok(Address::Stake(StakeAddress { network: map_network(stake_address.network())?, - payload: match stake_address.payload() { + credential: match stake_address.payload() { addresses::StakePayload::Stake(hash) => { - StakeAddressPayload::StakeKeyHash(hash.to_vec()) + StakeCredential::AddrKeyHash(hash.to_vec()) } addresses::StakePayload::Script(hash) => { - StakeAddressPayload::ScriptHash(hash.to_vec()) + StakeCredential::ScriptHash(hash.to_vec()) } }, })), @@ -97,10 +97,10 @@ pub fn map_stake_credential(cred: &PallasStakeCredential) -> StakeCredential { pub fn map_stake_address(cred: &PallasStakeCredential, network_id: NetworkId) -> StakeAddress { let payload = match cred { PallasStakeCredential::AddrKeyhash(key_hash) => { - StakeAddressPayload::StakeKeyHash(key_hash.to_vec()) + StakeCredential::AddrKeyHash(key_hash.to_vec()) } PallasStakeCredential::ScriptHash(script_hash) => { - StakeAddressPayload::ScriptHash(script_hash.to_vec()) + StakeCredential::ScriptHash(script_hash.to_vec()) } }; @@ -265,7 +265,7 @@ pub fn map_certificate( .iter() .map(|v| { StakeAddress::new( - StakeAddressPayload::StakeKeyHash(v.to_vec()), + StakeCredential::AddrKeyHash(v.to_vec()), network_id.clone().into(), ) }) @@ -375,7 +375,7 @@ pub fn map_certificate( .into_iter() .map(|v| { StakeAddress::new( - StakeAddressPayload::StakeKeyHash(v.to_vec()), + StakeCredential::AddrKeyHash(v.to_vec()), network_id.clone().into(), ) }) diff --git a/common/src/address.rs b/common/src/address.rs index 984af40e..cc8b52c2 100644 --- a/common/src/address.rs +++ b/common/src/address.rs @@ -4,11 +4,10 @@ use crate::cip19::{VarIntDecoder, VarIntEncoder}; use crate::types::{KeyHash, ScriptHash}; -use crate::{Credential, NetworkId}; +use crate::{Credential, NetworkId, StakeCredential}; use anyhow::{anyhow, bail, Result}; use crc::{Crc, CRC_32_ISO_HDLC}; use minicbor::data::IanaTag; -use serde_with::{hex::Hex, serde_as}; use std::cmp::Ordering; use std::fmt::{Display, Formatter}; @@ -196,7 +195,7 @@ pub enum ShelleyAddressDelegationPart { #[n(1)] StakeKeyHash(#[n(0)] Vec), - /// Delegation to script key + /// Delegation to script key hash #[n(2)] ScriptHash(#[n(0)] ScriptHash), @@ -395,27 +394,27 @@ impl ShelleyAddress { } /// Payload of a stake address -#[serde_as] -#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, Hash)] -pub enum StakeAddressPayload { - /// Stake key - StakeKeyHash(#[serde_as(as = "Hex")] KeyHash), - - /// Script hash - ScriptHash(#[serde_as(as = "Hex")] ScriptHash), -} - -impl StakeAddressPayload { - // Convert to string - note different encoding from when used as part of a StakeAddress - pub fn to_string(&self) -> Result { - let (hrp, data) = match &self { - Self::StakeKeyHash(data) => (bech32::Hrp::parse("stake_vkh")?, data), - Self::ScriptHash(data) => (bech32::Hrp::parse("script")?, data), - }; - - Ok(bech32::encode::(hrp, data)?) - } -} +// #[serde_as] +// #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, Hash)] +// pub enum StakeAddressPayload { +// /// Stake key +// StakeKeyHash(#[serde_as(as = "Hex")] KeyHash), +// +// /// Script hash +// ScriptHash(#[serde_as(as = "Hex")] ScriptHash), +// } +// +// impl StakeAddressPayload { +// // Convert to string - note different encoding from when used as part of a StakeAddress +// pub fn to_string(&self) -> Result { +// let (hrp, data) = match &self { +// Self::StakeKeyHash(data) => (bech32::Hrp::parse("stake_vkh")?, data), +// Self::ScriptHash(data) => (bech32::Hrp::parse("script")?, data), +// }; +// +// Ok(bech32::encode::(hrp, data)?) +// } +// } /// A stake address #[derive(Debug, Clone, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)] @@ -423,26 +422,26 @@ pub struct StakeAddress { /// Network id pub network: AddressNetwork, - /// Payload - pub payload: StakeAddressPayload, + /// Credential + pub credential: StakeCredential, // payload: StakePayload? } impl StakeAddress { - pub fn new(payload: StakeAddressPayload, network: AddressNetwork) -> Self { - StakeAddress { network, payload } + pub fn new(credential: StakeCredential, network: AddressNetwork) -> Self { + StakeAddress { network, credential } } pub fn get_hash(&self) -> &[u8] { - match &self.payload { - StakeAddressPayload::StakeKeyHash(hash) => hash, - StakeAddressPayload::ScriptHash(hash) => hash, + match &self.credential { + StakeCredential::AddrKeyHash(hash) => hash, + StakeCredential::ScriptHash(hash) => hash, } } pub fn get_credential(&self) -> Credential { - match &self.payload { - StakeAddressPayload::StakeKeyHash(hash) => Credential::AddrKeyHash(hash.clone()), - StakeAddressPayload::ScriptHash(hash) => Credential::ScriptHash(hash.clone()), + match &self.credential { + StakeCredential::AddrKeyHash(hash) => Credential::AddrKeyHash(hash.clone()), + StakeCredential::ScriptHash(hash) => Credential::ScriptHash(hash.clone()), } } @@ -466,13 +465,13 @@ impl StakeAddress { false => AddressNetwork::Main, }; - let payload = match (header >> 4) & 0x0Fu8 { - 0b1110 => StakeAddressPayload::StakeKeyHash(data[1..].to_vec()), - 0b1111 => StakeAddressPayload::ScriptHash(data[1..].to_vec()), + let credential = match (header >> 4) & 0x0Fu8 { + 0b1110 => StakeCredential::AddrKeyHash(data[1..].to_vec()), + 0b1111 => StakeCredential::ScriptHash(data[1..].to_vec()), _ => return Err(anyhow!("Unknown header {header} in stake address")), }; - return Ok(StakeAddress { network, payload }); + return Ok(StakeAddress { network, credential }); } Err(anyhow!("Empty stake address data")) @@ -485,9 +484,9 @@ impl StakeAddress { AddressNetwork::Test => 0b0u8, }; - let (stake_bits, stake_hash): (u8, &Vec) = match &self.payload { - StakeAddressPayload::StakeKeyHash(data) => (0b1110, data), - StakeAddressPayload::ScriptHash(data) => (0b1111, data), + let (stake_bits, stake_hash): (u8, &Vec) = match &self.credential { + StakeCredential::AddrKeyHash(data) => (0b1110, data), + StakeCredential::ScriptHash(data) => (0b1111, data), }; let mut data = vec![network_bits | (stake_bits << 4)]; @@ -506,20 +505,20 @@ impl StakeAddress { _ => AddressNetwork::Test, }; - let payload = match (data[0] >> 4) & 0x0F { - 0b1110 => StakeAddressPayload::StakeKeyHash(data[1..].to_vec()), - 0b1111 => StakeAddressPayload::ScriptHash(data[1..].to_vec()), + let credential = match (data[0] >> 4) & 0x0F { + 0b1110 => StakeCredential::AddrKeyHash(data[1..].to_vec()), + 0b1111 => StakeCredential::ScriptHash(data[1..].to_vec()), _ => bail!("Unknown header byte {:x} in stake address", data[0]), }; - Ok(StakeAddress { network, payload }) + Ok(StakeAddress { network, credential }) } pub fn to_bytes_key(&self) -> Result> { let mut out = Vec::new(); - let (bits, hash): (u8, &[u8]) = match &self.payload { - StakeAddressPayload::StakeKeyHash(h) => (0b1110, h), - StakeAddressPayload::ScriptHash(h) => (0b1111, h), + let (bits, hash): (u8, &[u8]) = match &self.credential { + StakeCredential::AddrKeyHash(h) => (0b1110, h), + StakeCredential::ScriptHash(h) => (0b1111, h), }; let net_bit = match self.network { @@ -566,7 +565,7 @@ impl Default for StakeAddress { fn default() -> Self { StakeAddress { network: AddressNetwork::Main, - payload: StakeAddressPayload::StakeKeyHash(vec![0u8; 28]), + credential: StakeCredential::AddrKeyHash(vec![0u8; 28]), } } } @@ -648,9 +647,9 @@ impl Address { ShelleyAddressPaymentPart::PaymentKeyHash(_) => false, ShelleyAddressPaymentPart::ScriptHash(_) => true, }, - Address::Stake(stake) => match stake.payload { - StakeAddressPayload::StakeKeyHash(_) => false, - StakeAddressPayload::ScriptHash(_) => true, + Address::Stake(stake) => match stake.credential { + StakeCredential::AddrKeyHash(_) => false, + StakeCredential::ScriptHash(_) => true, }, Address::Byron(_) | Address::None => false, } @@ -865,7 +864,7 @@ mod tests { fn shelley_type_14() { let address = Address::Stake(StakeAddress { network: AddressNetwork::Main, - payload: StakeAddressPayload::StakeKeyHash(test_stake_key_hash()), + credential: StakeCredential::AddrKeyHash(test_stake_key_hash()), }); let text = address.to_string().unwrap(); @@ -882,7 +881,7 @@ mod tests { fn shelley_type_15() { let address = Address::Stake(StakeAddress { network: AddressNetwork::Main, - payload: StakeAddressPayload::ScriptHash(test_script_hash()), + credential: StakeCredential::ScriptHash(test_script_hash()), }); let text = address.to_string().unwrap(); @@ -927,8 +926,8 @@ mod tests { let sa = StakeAddress::from_binary(&binary).unwrap(); assert_eq!(sa.network, AddressNetwork::Main); assert_eq!( - match sa.payload { - StakeAddressPayload::StakeKeyHash(key) => hex::encode(&key), + match sa.credential { + StakeCredential::AddrKeyHash(key) => hex::encode(&key), _ => "SCRIPT".to_string(), }, "558f3ee09b26d88fac2eddc772a9eda94cce6dbadbe9fee439bd6001" @@ -943,8 +942,8 @@ mod tests { let sa = StakeAddress::from_binary(&binary).unwrap(); assert_eq!(sa.network, AddressNetwork::Main); assert_eq!( - match sa.payload { - StakeAddressPayload::ScriptHash(key) => hex::encode(&key), + match sa.credential { + StakeCredential::ScriptHash(key) => hex::encode(&key), _ => "STAKE".to_string(), }, "558f3ee09b26d88fac2eddc772a9eda94cce6dbadbe9fee439bd6001" @@ -959,8 +958,8 @@ mod tests { let sa = StakeAddress::from_binary(&binary).unwrap(); assert_eq!(sa.network, AddressNetwork::Test); assert_eq!( - match sa.payload { - StakeAddressPayload::StakeKeyHash(key) => hex::encode(&key), + match sa.credential { + StakeCredential::AddrKeyHash(key) => hex::encode(&key), _ => "SCRIPT".to_string(), }, "558f3ee09b26d88fac2eddc772a9eda94cce6dbadbe9fee439bd6001" @@ -1012,8 +1011,8 @@ mod tests { assert_eq!(decoded.network, AddressNetwork::Main); assert_eq!( - match decoded.payload { - StakeAddressPayload::StakeKeyHash(key) => hex::encode(&key), + match decoded.credential { + StakeCredential::AddrKeyHash(key) => hex::encode(&key), _ => "STAKE".to_string(), }, "558f3ee09b26d88fac2eddc772a9eda94cce6dbadbe9fee439bd6001" @@ -1035,8 +1034,8 @@ mod tests { assert_eq!(decoded.network, AddressNetwork::Main); assert_eq!( - match decoded.payload { - StakeAddressPayload::ScriptHash(key) => hex::encode(&key), + match decoded.credential { + StakeCredential::ScriptHash(key) => hex::encode(&key), _ => "STAKE".to_string(), }, "558f3ee09b26d88fac2eddc772a9eda94cce6dbadbe9fee439bd6001" @@ -1056,8 +1055,8 @@ mod tests { assert_eq!(decoded.network, AddressNetwork::Test); assert_eq!( - match decoded.payload { - StakeAddressPayload::ScriptHash(key) => hex::encode(&key), + match decoded.credential { + StakeCredential::ScriptHash(key) => hex::encode(&key), _ => "SCRIPT".to_string(), }, "558f3ee09b26d88fac2eddc772a9eda94cce6dbadbe9fee439bd6001" diff --git a/common/src/stake_addresses.rs b/common/src/stake_addresses.rs index e2bf773d..67a51f71 100644 --- a/common/src/stake_addresses.rs +++ b/common/src/stake_addresses.rs @@ -544,7 +544,7 @@ impl StakeAddressMap { #[cfg(test)] mod tests { - use crate::{AddressNetwork, StakeAddress, StakeAddressPayload}; + use crate::{AddressNetwork, StakeAddress, StakeCredential}; use super::*; @@ -558,7 +558,7 @@ mod tests { fn create_stake_address(hash: &[u8]) -> StakeAddress { StakeAddress::new( - StakeAddressPayload::StakeKeyHash( + StakeCredential::AddrKeyHash( hash.to_vec().try_into().expect("Invalid hash length"), ), AddressNetwork::Main, diff --git a/common/src/types.rs b/common/src/types.rs index e82a729b..3ae6a323 100644 --- a/common/src/types.rs +++ b/common/src/types.rs @@ -718,6 +718,17 @@ impl Credential { pub type StakeCredential = Credential; +impl StakeCredential { + pub fn to_string(&self) -> Result { + let (hrp, data) = match &self { + Self::AddrKeyHash(data) => (Hrp::parse("stake_vkh")?, data), + Self::ScriptHash(data) => (Hrp::parse("script")?, data), + }; + + Ok(bech32::encode::(hrp, data)?) + } +} + /// Relay single host address #[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize, Eq, PartialEq)] pub struct SingleHostAddr { diff --git a/modules/accounts_state/src/snapshot.rs b/modules/accounts_state/src/snapshot.rs index a6f959c8..dd6305b3 100644 --- a/modules/accounts_state/src/snapshot.rs +++ b/modules/accounts_state/src/snapshot.rs @@ -187,7 +187,7 @@ mod tests { use super::*; use acropolis_common::stake_addresses::StakeAddressState; use acropolis_common::AddressNetwork::Main; - use acropolis_common::{StakeAddress, StakeAddressPayload}; + use acropolis_common::{StakeAddress, StakeCredential}; // Helper function to create stake addresses for testing fn create_test_stake_address(id: u8) -> StakeAddress { @@ -195,7 +195,7 @@ mod tests { hash[0] = id; StakeAddress { network: Main, - payload: StakeAddressPayload::StakeKeyHash( + credential: StakeCredential::AddrKeyHash( hash.try_into().expect("Invalid hash length"), ), } diff --git a/modules/accounts_state/src/state.rs b/modules/accounts_state/src/state.rs index 0f7f1fc9..2909a1bc 100644 --- a/modules/accounts_state/src/state.rs +++ b/modules/accounts_state/src/state.rs @@ -993,7 +993,7 @@ mod tests { use acropolis_common::{ protocol_params::ConwayParams, rational_number::RationalNumber, AddressNetwork, Anchor, Committee, Constitution, CostModel, DRepVotingThresholds, PoolVotingThresholds, Pot, - PotDelta, Ratio, Registration, StakeAddress, StakeAddressDelta, StakeAddressPayload, + PotDelta, Ratio, Registration, StakeAddress, StakeAddressDelta, StakeCredential, StakeAndVoteDelegation, StakeRegistrationAndStakeAndVoteDelegation, StakeRegistrationAndVoteDelegation, VoteDelegation, Withdrawal, }; @@ -1004,7 +1004,7 @@ mod tests { full_hash[..hash.len().min(28)].copy_from_slice(&hash[..hash.len().min(28)]); StakeAddress { network: AddressNetwork::Main, - payload: StakeAddressPayload::StakeKeyHash(full_hash), + credential: StakeCredential::AddrKeyHash(full_hash), } } diff --git a/modules/rest_blockfrost/src/handlers/epochs.rs b/modules/rest_blockfrost/src/handlers/epochs.rs index ddb7b003..22db63bb 100644 --- a/modules/rest_blockfrost/src/handlers/epochs.rs +++ b/modules/rest_blockfrost/src/handlers/epochs.rs @@ -15,7 +15,7 @@ use acropolis_common::{ utils::query_state, }, serialization::Bech32WithHrp, - AddressNetwork, StakeAddress, StakeAddressPayload, + AddressNetwork, StakeAddress, StakeCredential, }; use anyhow::{anyhow, Result}; use caryatid_sdk::Context; @@ -508,7 +508,7 @@ pub async fn handle_epoch_total_stakes_blockfrost( .map(|(pool_id, stake_key_hash, amount)| { let stake_address = StakeAddress { network: network.clone(), - payload: StakeAddressPayload::StakeKeyHash(stake_key_hash), + credential: StakeCredential::AddrKeyHash(stake_key_hash), } .to_string() .map_err(|e| anyhow::anyhow!("Failed to convert stake address to string: {e}"))?; @@ -642,10 +642,10 @@ pub async fn handle_epoch_pool_stakes_blockfrost( .await?; let spdd_response = spdd .into_iter() - .map(|(stake_key_hash, amount)| { + .map(|(key_hash, amount)| { let stake_address = StakeAddress { network: network.clone(), - payload: StakeAddressPayload::StakeKeyHash(stake_key_hash), + credential: StakeCredential::AddrKeyHash(key_hash), } .to_string() .map_err(|e| anyhow::anyhow!("Failed to convert stake address to string: {e}"))?; diff --git a/modules/stake_delta_filter/src/utils.rs b/modules/stake_delta_filter/src/utils.rs index 8ad6ae24..74fdd428 100644 --- a/modules/stake_delta_filter/src/utils.rs +++ b/modules/stake_delta_filter/src/utils.rs @@ -1,7 +1,7 @@ use acropolis_common::{ messages::{AddressDeltasMessage, StakeAddressDeltasMessage}, Address, AddressDelta, BlockInfo, Era, ShelleyAddressDelegationPart, ShelleyAddressPointer, - StakeAddress, StakeAddressDelta, StakeAddressPayload, + StakeAddress, StakeAddressDelta, StakeCredential, }; use anyhow::{anyhow, Result}; use serde_with::serde_as; @@ -347,12 +347,12 @@ pub fn process_message( // Base addresses (stake delegated to itself) ShelleyAddressDelegationPart::StakeKeyHash(keyhash) => StakeAddress { network: shelley.network.clone(), - payload: StakeAddressPayload::StakeKeyHash(keyhash.clone()), + credential: StakeCredential::AddrKeyHash(keyhash.clone()), }, ShelleyAddressDelegationPart::ScriptHash(scripthash) => StakeAddress { network: shelley.network.clone(), - payload: StakeAddressPayload::ScriptHash(scripthash.clone()), + credential: StakeCredential::ScriptHash(scripthash.clone()), }, // Shelley addresses (stake delegated to some different address) @@ -402,7 +402,7 @@ mod test { use acropolis_common::{ messages::AddressDeltasMessage, Address, AddressDelta, BlockHash, BlockInfo, BlockStatus, ByronAddress, Era, ShelleyAddress, ShelleyAddressDelegationPart, ShelleyAddressPaymentPart, - ShelleyAddressPointer, StakeAddress, StakeAddressPayload, UTxOIdentifier, ValueDelta, + ShelleyAddressPointer, StakeAddress, StakeCredential, UTxOIdentifier, ValueDelta, }; use bech32::{Bech32, Hrp}; @@ -467,12 +467,12 @@ mod test { addresses::Address::Stake(stake_address) => Ok(Address::Stake(StakeAddress { network: map_network(stake_address.network())?, - payload: match stake_address.payload() { + credential: match stake_address.payload() { addresses::StakePayload::Stake(hash) => { - StakeAddressPayload::StakeKeyHash(hash.to_vec()) + StakeCredential::AddrKeyHash(hash.to_vec()) } addresses::StakePayload::Script(hash) => { - StakeAddressPayload::ScriptHash(hash.to_vec()) + StakeCredential::ScriptHash(hash.to_vec()) } }, })), @@ -583,11 +583,11 @@ mod test { // additional check: payload conversion correctness assert_eq!( - stake_delta.deltas.get(0).unwrap().address.payload.to_string().unwrap(), + stake_delta.deltas.get(0).unwrap().address.credential.to_string().unwrap(), stake_key_hash ); assert_eq!( - stake_delta.deltas.get(2).unwrap().address.payload.to_string().unwrap(), + stake_delta.deltas.get(2).unwrap().address.credential.to_string().unwrap(), script_hash ); From 43486b90ce34c8612bef1dba114a42fd4b82b236 Mon Sep 17 00:00:00 2001 From: Matthew Hounslow Date: Fri, 24 Oct 2025 08:11:18 -0700 Subject: [PATCH 2/4] Remove StakeAddressPayload --- common/src/address.rs | 38 ++++++++++++-------------------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/common/src/address.rs b/common/src/address.rs index 1052af7a..7c5e6b3a 100644 --- a/common/src/address.rs +++ b/common/src/address.rs @@ -356,29 +356,6 @@ impl ShelleyAddress { } } -/// Payload of a stake address -// #[serde_as] -// #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, Hash)] -// pub enum StakeAddressPayload { -// /// Stake key -// StakeKeyHash(#[serde_as(as = "Hex")] KeyHash), -// -// /// Script hash -// ScriptHash(#[serde_as(as = "Hex")] ScriptHash), -// } -// -// impl StakeAddressPayload { -// // Convert to string - note different encoding from when used as part of a StakeAddress -// pub fn to_string(&self) -> Result { -// let (hrp, data) = match &self { -// Self::StakeKeyHash(data) => (bech32::Hrp::parse("stake_vkh")?, data), -// Self::ScriptHash(data) => (bech32::Hrp::parse("script")?, data), -// }; -// -// Ok(bech32::encode::(hrp, data)?) -// } -// } - /// A stake address #[derive(Debug, Clone, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)] pub struct StakeAddress { @@ -391,7 +368,10 @@ pub struct StakeAddress { impl StakeAddress { pub fn new(credential: StakeCredential, network: NetworkId) -> Self { - StakeAddress { network, credential } + StakeAddress { + network, + credential, + } } pub fn get_hash(&self) -> &[u8] { @@ -434,7 +414,10 @@ impl StakeAddress { _ => return Err(anyhow!("Unknown header {header} in stake address")), }; - return Ok(StakeAddress { network, credential }); + return Ok(StakeAddress { + network, + credential, + }); } Err(anyhow!("Empty stake address data")) @@ -474,7 +457,10 @@ impl StakeAddress { _ => bail!("Unknown header byte {:x} in stake address", data[0]), }; - Ok(StakeAddress { network, credential }) + Ok(StakeAddress { + network, + credential, + }) } pub fn to_bytes_key(&self) -> Result> { From 0e84a10566ccb65aa6ac533a52607a696a60713b Mon Sep 17 00:00:00 2001 From: Matthew Hounslow Date: Fri, 24 Oct 2025 12:43:04 -0700 Subject: [PATCH 3/4] Remove redundant `StakeAddress` imports across modules --- codec/src/map_parameters.rs | 8 ++------ common/src/stake_addresses.rs | 4 +--- modules/accounts_state/src/snapshot.rs | 5 +---- modules/accounts_state/src/state.rs | 7 ++----- modules/stake_delta_filter/src/predefined.rs | 12 ++++++------ modules/stake_delta_filter/src/utils.rs | 5 +---- 6 files changed, 13 insertions(+), 28 deletions(-) diff --git a/codec/src/map_parameters.rs b/codec/src/map_parameters.rs index 85daed71..b46d5c69 100644 --- a/codec/src/map_parameters.rs +++ b/codec/src/map_parameters.rs @@ -70,12 +70,8 @@ pub fn map_address(address: &addresses::Address) -> Result
{ addresses::Address::Stake(stake_address) => Ok(Address::Stake(StakeAddress { network: map_network(stake_address.network())?, credential: match stake_address.payload() { - addresses::StakePayload::Stake(hash) => { - StakeCredential::AddrKeyHash(hash.to_vec()) - } - addresses::StakePayload::Script(hash) => { - StakeCredential::ScriptHash(hash.to_vec()) - } + addresses::StakePayload::Stake(hash) => StakeCredential::AddrKeyHash(hash.to_vec()), + addresses::StakePayload::Script(hash) => StakeCredential::ScriptHash(hash.to_vec()), }, })), } diff --git a/common/src/stake_addresses.rs b/common/src/stake_addresses.rs index a88358fb..b4daf076 100644 --- a/common/src/stake_addresses.rs +++ b/common/src/stake_addresses.rs @@ -558,9 +558,7 @@ mod tests { fn create_stake_address(hash: &[u8]) -> StakeAddress { StakeAddress::new( - StakeCredential::AddrKeyHash( - hash.to_vec().try_into().expect("Invalid hash length"), - ), + StakeCredential::AddrKeyHash(hash.to_vec().try_into().expect("Invalid hash length")), NetworkId::Mainnet, ) } diff --git a/modules/accounts_state/src/snapshot.rs b/modules/accounts_state/src/snapshot.rs index 9e7d2801..8d1a9d3d 100644 --- a/modules/accounts_state/src/snapshot.rs +++ b/modules/accounts_state/src/snapshot.rs @@ -187,7 +187,6 @@ mod tests { use super::*; use acropolis_common::stake_addresses::StakeAddressState; use acropolis_common::NetworkId::Mainnet; - use acropolis_common::{StakeAddress}; use acropolis_common::{StakeAddress, StakeCredential}; // Helper function to create stake addresses for testing @@ -196,9 +195,7 @@ mod tests { hash[0] = id; StakeAddress { network: Mainnet, - credential: StakeCredential::AddrKeyHash( - hash.try_into().expect("Invalid hash length"), - ), + credential: StakeCredential::AddrKeyHash(hash.try_into().expect("Invalid hash length")), } } diff --git a/modules/accounts_state/src/state.rs b/modules/accounts_state/src/state.rs index 2cd20c77..c59be443 100644 --- a/modules/accounts_state/src/state.rs +++ b/modules/accounts_state/src/state.rs @@ -993,11 +993,8 @@ mod tests { use acropolis_common::{ protocol_params::ConwayParams, rational_number::RationalNumber, Anchor, Committee, Constitution, CostModel, DRepVotingThresholds, NetworkId, PoolVotingThresholds, Pot, - PotDelta, Ratio, Registration, StakeAddress, StakeAddressDelta, - protocol_params::ConwayParams, rational_number::RationalNumber, Anchor, - Committee, Constitution, CostModel, DRepVotingThresholds, PoolVotingThresholds, Pot, - PotDelta, Ratio, Registration, StakeAddress, StakeAddressDelta, StakeCredential, - StakeAndVoteDelegation, StakeRegistrationAndStakeAndVoteDelegation, + PotDelta, Ratio, Registration, StakeAddress, StakeAddressDelta, StakeAndVoteDelegation, + StakeCredential, StakeRegistrationAndStakeAndVoteDelegation, StakeRegistrationAndVoteDelegation, VoteDelegation, Withdrawal, }; diff --git a/modules/stake_delta_filter/src/predefined.rs b/modules/stake_delta_filter/src/predefined.rs index 36debe29..f1a2ef92 100644 --- a/modules/stake_delta_filter/src/predefined.rs +++ b/modules/stake_delta_filter/src/predefined.rs @@ -59,8 +59,8 @@ pub const POINTER_CACHE: [(&str, &str); 1] = [( }, { "network": "Mainnet", - "payload": { - "StakeKeyHash": "bc1597ad71c55d2d009a9274b3831ded155118dd769f5376decc1369" + "credential": { + "AddrKeyHash": "bc1597ad71c55d2d009a9274b3831ded155118dd769f5376decc1369" } } ], @@ -80,8 +80,8 @@ pub const POINTER_CACHE: [(&str, &str); 1] = [( }, { "network": "Mainnet", - "payload": { - "StakeKeyHash": "e46c33afa9ca60cfeb3b7452a415c271772020b3f57ac90c496a6127" + "credential": { + "AddrKeyHash": "e46c33afa9ca60cfeb3b7452a415c271772020b3f57ac90c496a6127" } } ], @@ -117,8 +117,8 @@ pub const POINTER_CACHE: [(&str, &str); 1] = [( }, { "network": "Mainnet", - "payload": { - "StakeKeyHash": "1332d859dd71f5b1089052a049690d81f7367eac9fafaef80b4da395" + "credential": { + "AddrKeyHash": "1332d859dd71f5b1089052a049690d81f7367eac9fafaef80b4da395" } } ], diff --git a/modules/stake_delta_filter/src/utils.rs b/modules/stake_delta_filter/src/utils.rs index 73f10b72..41431f1b 100644 --- a/modules/stake_delta_filter/src/utils.rs +++ b/modules/stake_delta_filter/src/utils.rs @@ -403,11 +403,8 @@ mod test { use crate::*; use acropolis_common::{ messages::AddressDeltasMessage, Address, AddressDelta, BlockHash, BlockInfo, BlockStatus, - ByronAddress, Era, ShelleyAddress, ShelleyAddressDelegationPart, - ShelleyAddressPaymentPart, ShelleyAddressPointer, StakeAddress, StakeCredential, - UTxOIdentifier, ValueDelta, ByronAddress, Era, ShelleyAddress, ShelleyAddressDelegationPart, ShelleyAddressPaymentPart, - ShelleyAddressPointer, StakeAddress, UTxOIdentifier, ValueDelta, + ShelleyAddressPointer, StakeAddress, StakeCredential, UTxOIdentifier, ValueDelta, }; use bech32::{Bech32, Hrp}; From f738fee5bd8d065348aa5c656a98a73f7a36504b Mon Sep 17 00:00:00 2001 From: Matthew Hounslow Date: Fri, 24 Oct 2025 13:12:18 -0700 Subject: [PATCH 4/4] Add `serde_as` attribute for `Credential` variants to enforce Hex serialization --- common/src/types.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/common/src/types.rs b/common/src/types.rs index 3df30875..5030fcbd 100644 --- a/common/src/types.rs +++ b/common/src/types.rs @@ -625,15 +625,16 @@ pub struct PotDelta { pub delta: LovelaceDelta, } +#[serde_as] #[derive( Debug, Clone, Ord, Eq, PartialEq, PartialOrd, Hash, serde::Serialize, serde::Deserialize, )] pub enum Credential { /// Script hash. NOTE: Order matters when parsing Haskell Node Snapshot data. - ScriptHash(KeyHash), + ScriptHash(#[serde_as(as = "Hex")] KeyHash), /// Address key hash - AddrKeyHash(KeyHash), + AddrKeyHash(#[serde_as(as = "Hex")] KeyHash), } impl Credential {