Skip to content

Commit ff594da

Browse files
committed
Replace KeyHash with StakeAddress for consistency and unified handling across modules.
- Update reward processing, delegation, and state management to use `StakeAddress`. - Simplify APIs and adjust implementations to process `StakeAddress` directly. - Refactor tests to reflect new `StakeAddress`-based structure. - Enhance error logging to include full stake address information.
1 parent cb6b823 commit ff594da

File tree

8 files changed

+386
-308
lines changed

8 files changed

+386
-308
lines changed

common/src/address.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
//! Cardano address definitions for Acropolis
22
// We don't use these types in the acropolis_common crate itself
33
#![allow(dead_code)]
4+
45
use crate::cip19::{VarIntDecoder, VarIntEncoder};
56
use crate::types::{KeyHash, NetworkId, ScriptHash};
67
use anyhow::{anyhow, bail, Result};
78
use serde_with::{hex::Hex, serde_as};
9+
use std::borrow::Borrow;
10+
use std::hash::{Hash, Hasher};
811

912
/// a Byron-era address
1013
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
@@ -174,7 +177,7 @@ impl ShelleyAddress {
174177

175178
/// Payload of a stake address
176179
#[serde_as]
177-
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
180+
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, Hash)]
178181
pub enum StakeAddressPayload {
179182
/// Stake key
180183
StakeKeyHash(#[serde_as(as = "Hex")] Vec<u8>),
@@ -196,7 +199,7 @@ impl StakeAddressPayload {
196199
}
197200

198201
/// A stake address
199-
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
202+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
200203
pub struct StakeAddress {
201204
/// Network id
202205
pub network: AddressNetwork,
@@ -206,6 +209,10 @@ pub struct StakeAddress {
206209
}
207210

208211
impl StakeAddress {
212+
pub fn new(network: AddressNetwork, payload: StakeAddressPayload) -> Self {
213+
StakeAddress { network, payload }
214+
}
215+
209216
/// Get either hash of the payload
210217
pub fn get_hash(&self) -> &[u8] {
211218
match &self.payload {
@@ -292,6 +299,26 @@ impl StakeAddress {
292299
}
293300
}
294301

302+
impl Hash for StakeAddress {
303+
fn hash<H: Hasher>(&self, state: &mut H) {
304+
self.get_hash().hash(state);
305+
}
306+
}
307+
308+
impl PartialEq for StakeAddress {
309+
fn eq(&self, other: &Self) -> bool {
310+
self.get_hash() == other.get_hash()
311+
}
312+
}
313+
314+
impl Eq for StakeAddress {}
315+
316+
impl Borrow<[u8]> for StakeAddress {
317+
fn borrow(&self) -> &[u8] {
318+
self.get_hash()
319+
}
320+
}
321+
295322
impl<C> minicbor::Encode<C> for StakeAddress {
296323
fn encode<W: minicbor::encode::Write>(
297324
&self,
@@ -653,7 +680,7 @@ mod tests {
653680

654681
fn testnet_script_address() -> StakeAddress {
655682
let binary =
656-
hex::decode("e0558f3ee09b26d88fac2eddc772a9eda94cce6dbadbe9fee439bd6001").unwrap();
683+
hex::decode("f0558f3ee09b26d88fac2eddc772a9eda94cce6dbadbe9fee439bd6001").unwrap();
657684
StakeAddress::from_binary(&binary).unwrap()
658685
}
659686

0 commit comments

Comments
 (0)