Skip to content
Merged
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
13 changes: 11 additions & 2 deletions linera-base/src/identifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,27 @@ use crate::{
};

/// An account owner.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, WitLoad, WitStore, WitType)]
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd, WitLoad, WitStore, WitType)]
#[cfg_attr(with_testing, derive(test_strategy::Arbitrary))]
pub enum AccountOwner {
/// Short addresses reserved for the protocol.
Reserved(u8),
/// 32-byte account address.
Address32(CryptoHash),
/// 20-byte account EVM-compatible address.
#[debug(with = "hex_debug")]
Address20([u8; 20]),
}

impl fmt::Debug for AccountOwner {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Reserved(byte) => f.debug_tuple("Reserved").field(byte).finish(),
Self::Address32(hash) => write!(f, "Address32({:?}..)", hash),
Self::Address20(bytes) => write!(f, "Address20({}..)", hex::encode(&bytes[..8])),
}
}
}

impl AccountOwner {
/// Returns the default chain address.
pub const CHAIN: AccountOwner = AccountOwner::Reserved(0);
Expand Down
11 changes: 11 additions & 0 deletions linera-base/src/unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,14 @@ fn chain_ownership_test_case() -> ChainOwnership {
},
}
}

#[test]
fn account_owner_debug_format() {
assert_eq!(&format!("{:?}", AccountOwner::Reserved(10)), "Reserved(10)");
let addr32 = AccountOwner::Address32(CryptoHash::from([10u8; 32]));
let debug32 = "Address32(0a0a0a0a0a0a0a0a..)";
assert_eq!(&format!("{addr32:?}"), debug32);
let addr20 = AccountOwner::Address20([10u8; 20]);
let debug20 = "Address20(0a0a0a0a0a0a0a0a..)";
assert_eq!(&format!("{addr20:?}"), debug20);
}
Loading