From e60f9875b129eaf28be1beabb8416383bf184fbd Mon Sep 17 00:00:00 2001 From: Eugene Gostkin Date: Tue, 23 Jan 2024 18:20:23 +0100 Subject: [PATCH] Multi-era dump exts --- .../rust/src/builders/certificate_builder.rs | 4 +- chain/rust/src/certs/cbor_encodings.rs | 10 +- chain/rust/src/certs/mod.rs | 79 +++++-------- chain/rust/src/certs/serialization.rs | 104 ++++++++---------- chain/rust/src/plutus/mod.rs | 7 +- chain/wasm/src/certs/mod.rs | 81 ++++++-------- multi-era/rust/src/byron/block/mod.rs | 2 + multi-era/rust/src/utils.rs | 82 ++++++++++++-- multi-era/wasm/src/utils.rs | 4 +- 9 files changed, 193 insertions(+), 180 deletions(-) diff --git a/chain/rust/src/builders/certificate_builder.rs b/chain/rust/src/builders/certificate_builder.rs index 7b015ea6..b7a529da 100644 --- a/chain/rust/src/builders/certificate_builder.rs +++ b/chain/rust/src/builders/certificate_builder.rs @@ -41,7 +41,7 @@ pub fn cert_required_wits(cert: &Certificate, required_witnesses: &mut RequiredW required_witnesses.add_vkey_key_hash(cert.pool_params.operator); } Certificate::PoolRetirement(cert) => { - required_witnesses.add_vkey_key_hash(cert.ed25519_key_hash); + required_witnesses.add_vkey_key_hash(cert.pool); } Certificate::RegCert(cert) => { required_witnesses.add_from_credential(cert.stake_credential.clone()); @@ -114,7 +114,7 @@ pub fn add_cert_vkeys( vkeys.insert(cert.pool_params.operator); } Certificate::PoolRetirement(cert) => { - vkeys.insert(cert.ed25519_key_hash); + vkeys.insert(cert.pool); } Certificate::RegCert(_cert) => { // does not require a witness diff --git a/chain/rust/src/certs/cbor_encodings.rs b/chain/rust/src/certs/cbor_encodings.rs index 8f265196..fe2fcbe5 100644 --- a/chain/rust/src/certs/cbor_encodings.rs +++ b/chain/rust/src/certs/cbor_encodings.rs @@ -58,7 +58,7 @@ pub struct PoolRegistrationEncoding { pub struct PoolRetirementEncoding { pub len_encoding: LenEncoding, pub tag_encoding: Option, - pub ed25519_key_hash_encoding: StringEncoding, + pub pool_encoding: StringEncoding, pub epoch_encoding: Option, } @@ -100,7 +100,7 @@ pub struct SingleHostNameEncoding { pub struct StakeDelegationEncoding { pub len_encoding: LenEncoding, pub tag_encoding: Option, - pub ed25519_key_hash_encoding: StringEncoding, + pub pool_encoding: StringEncoding, } #[derive(Clone, Debug, Default)] @@ -113,7 +113,7 @@ pub struct StakeDeregistrationEncoding { pub struct StakeRegDelegCertEncoding { pub len_encoding: LenEncoding, pub tag_encoding: Option, - pub ed25519_key_hash_encoding: StringEncoding, + pub pool_encoding: StringEncoding, pub coin_encoding: Option, } @@ -127,14 +127,14 @@ pub struct StakeRegistrationEncoding { pub struct StakeVoteDelegCertEncoding { pub len_encoding: LenEncoding, pub tag_encoding: Option, - pub ed25519_key_hash_encoding: StringEncoding, + pub pool_encoding: StringEncoding, } #[derive(Clone, Debug, Default)] pub struct StakeVoteRegDelegCertEncoding { pub len_encoding: LenEncoding, pub tag_encoding: Option, - pub ed25519_key_hash_encoding: StringEncoding, + pub pool_encoding: StringEncoding, pub coin_encoding: Option, } diff --git a/chain/rust/src/certs/mod.rs b/chain/rust/src/certs/mod.rs index 362eb1eb..ba395f2c 100644 --- a/chain/rust/src/certs/mod.rs +++ b/chain/rust/src/certs/mod.rs @@ -78,19 +78,16 @@ impl Certificate { Self::StakeDeregistration(StakeDeregistration::new(stake_credential)) } - pub fn new_stake_delegation( - stake_credential: StakeCredential, - ed25519_key_hash: Ed25519KeyHash, - ) -> Self { - Self::StakeDelegation(StakeDelegation::new(stake_credential, ed25519_key_hash)) + pub fn new_stake_delegation(stake_credential: StakeCredential, pool: Ed25519KeyHash) -> Self { + Self::StakeDelegation(StakeDelegation::new(stake_credential, pool)) } pub fn new_pool_registration(pool_params: PoolParams) -> Self { Self::PoolRegistration(PoolRegistration::new(pool_params)) } - pub fn new_pool_retirement(ed25519_key_hash: Ed25519KeyHash, epoch: Epoch) -> Self { - Self::PoolRetirement(PoolRetirement::new(ed25519_key_hash, epoch)) + pub fn new_pool_retirement(pool: Ed25519KeyHash, epoch: Epoch) -> Self { + Self::PoolRetirement(PoolRetirement::new(pool, epoch)) } pub fn new_reg_cert(stake_credential: StakeCredential, coin: Coin) -> Self { @@ -107,26 +104,18 @@ impl Certificate { pub fn new_stake_vote_deleg_cert( stake_credential: StakeCredential, - ed25519_key_hash: Ed25519KeyHash, + pool: Ed25519KeyHash, d_rep: DRep, ) -> Self { - Self::StakeVoteDelegCert(StakeVoteDelegCert::new( - stake_credential, - ed25519_key_hash, - d_rep, - )) + Self::StakeVoteDelegCert(StakeVoteDelegCert::new(stake_credential, pool, d_rep)) } pub fn new_stake_reg_deleg_cert( stake_credential: StakeCredential, - ed25519_key_hash: Ed25519KeyHash, + pool: Ed25519KeyHash, coin: Coin, ) -> Self { - Self::StakeRegDelegCert(StakeRegDelegCert::new( - stake_credential, - ed25519_key_hash, - coin, - )) + Self::StakeRegDelegCert(StakeRegDelegCert::new(stake_credential, pool, coin)) } pub fn new_vote_reg_deleg_cert( @@ -139,13 +128,13 @@ impl Certificate { pub fn new_stake_vote_reg_deleg_cert( stake_credential: StakeCredential, - ed25519_key_hash: Ed25519KeyHash, + pool: Ed25519KeyHash, d_rep: DRep, coin: Coin, ) -> Self { Self::StakeVoteRegDelegCert(StakeVoteRegDelegCert::new( stake_credential, - ed25519_key_hash, + pool, d_rep, coin, )) @@ -278,13 +267,13 @@ impl Credential { #[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)] pub enum DRep { Key { - ed25519_key_hash: Ed25519KeyHash, + pool: Ed25519KeyHash, #[serde(skip)] len_encoding: LenEncoding, #[serde(skip)] index_0_encoding: Option, #[serde(skip)] - ed25519_key_hash_encoding: StringEncoding, + pool_encoding: StringEncoding, }, Script { script_hash: ScriptHash, @@ -310,12 +299,12 @@ pub enum DRep { } impl DRep { - pub fn new_key(ed25519_key_hash: Ed25519KeyHash) -> Self { + pub fn new_key(pool: Ed25519KeyHash) -> Self { Self::Key { - ed25519_key_hash, + pool, len_encoding: LenEncoding::default(), index_0_encoding: None, - ed25519_key_hash_encoding: StringEncoding::default(), + pool_encoding: StringEncoding::default(), } } @@ -566,16 +555,16 @@ impl PoolRegistration { #[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)] pub struct PoolRetirement { - pub ed25519_key_hash: Ed25519KeyHash, + pub pool: Ed25519KeyHash, pub epoch: Epoch, #[serde(skip)] pub encodings: Option, } impl PoolRetirement { - pub fn new(ed25519_key_hash: Ed25519KeyHash, epoch: Epoch) -> Self { + pub fn new(pool: Ed25519KeyHash, epoch: Epoch) -> Self { Self { - ed25519_key_hash, + pool, epoch, encodings: None, } @@ -704,16 +693,16 @@ pub type StakeCredential = Credential; #[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)] pub struct StakeDelegation { pub stake_credential: StakeCredential, - pub ed25519_key_hash: Ed25519KeyHash, + pub pool: Ed25519KeyHash, #[serde(skip)] pub encodings: Option, } impl StakeDelegation { - pub fn new(stake_credential: StakeCredential, ed25519_key_hash: Ed25519KeyHash) -> Self { + pub fn new(stake_credential: StakeCredential, pool: Ed25519KeyHash) -> Self { Self { stake_credential, - ed25519_key_hash, + pool, encodings: None, } } @@ -738,21 +727,17 @@ impl StakeDeregistration { #[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)] pub struct StakeRegDelegCert { pub stake_credential: StakeCredential, - pub ed25519_key_hash: Ed25519KeyHash, + pub pool: Ed25519KeyHash, pub coin: Coin, #[serde(skip)] pub encodings: Option, } impl StakeRegDelegCert { - pub fn new( - stake_credential: StakeCredential, - ed25519_key_hash: Ed25519KeyHash, - coin: Coin, - ) -> Self { + pub fn new(stake_credential: StakeCredential, pool: Ed25519KeyHash, coin: Coin) -> Self { Self { stake_credential, - ed25519_key_hash, + pool, coin, encodings: None, } @@ -778,21 +763,17 @@ impl StakeRegistration { #[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)] pub struct StakeVoteDelegCert { pub stake_credential: StakeCredential, - pub ed25519_key_hash: Ed25519KeyHash, + pub pool: Ed25519KeyHash, pub d_rep: DRep, #[serde(skip)] pub encodings: Option, } impl StakeVoteDelegCert { - pub fn new( - stake_credential: StakeCredential, - ed25519_key_hash: Ed25519KeyHash, - d_rep: DRep, - ) -> Self { + pub fn new(stake_credential: StakeCredential, pool: Ed25519KeyHash, d_rep: DRep) -> Self { Self { stake_credential, - ed25519_key_hash, + pool, d_rep, encodings: None, } @@ -802,7 +783,7 @@ impl StakeVoteDelegCert { #[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)] pub struct StakeVoteRegDelegCert { pub stake_credential: StakeCredential, - pub ed25519_key_hash: Ed25519KeyHash, + pub pool: Ed25519KeyHash, pub d_rep: DRep, pub coin: Coin, #[serde(skip)] @@ -812,13 +793,13 @@ pub struct StakeVoteRegDelegCert { impl StakeVoteRegDelegCert { pub fn new( stake_credential: StakeCredential, - ed25519_key_hash: Ed25519KeyHash, + pool: Ed25519KeyHash, d_rep: DRep, coin: Coin, ) -> Self { Self { stake_credential, - ed25519_key_hash, + pool, d_rep, coin, encodings: None, diff --git a/chain/rust/src/certs/serialization.rs b/chain/rust/src/certs/serialization.rs index 0f5f5ac6..ab67eb0e 100644 --- a/chain/rust/src/certs/serialization.rs +++ b/chain/rust/src/certs/serialization.rs @@ -561,10 +561,10 @@ impl Serialize for DRep { ) -> cbor_event::Result<&'se mut Serializer> { match self { DRep::Key { - ed25519_key_hash, + pool, len_encoding, index_0_encoding, - ed25519_key_hash_encoding, + pool_encoding, } => { serializer.write_array_sz(len_encoding.to_len_sz(2, force_canonical))?; serializer.write_unsigned_integer_sz( @@ -572,11 +572,8 @@ impl Serialize for DRep { fit_sz(0u64, *index_0_encoding, force_canonical), )?; serializer.write_bytes_sz( - ed25519_key_hash.to_raw_bytes(), - ed25519_key_hash_encoding.to_str_len_sz( - ed25519_key_hash.to_raw_bytes().len() as u64, - force_canonical, - ), + pool.to_raw_bytes(), + pool_encoding.to_str_len_sz(pool.to_raw_bytes().len() as u64, force_canonical), )?; len_encoding.end(serializer, force_canonical)?; Ok(serializer) @@ -649,7 +646,7 @@ impl Deserialize for DRep { Ok(Some(index_0_encoding)) })() .map_err(|e| e.annotate("index_0"))?; - let (ed25519_key_hash, ed25519_key_hash_encoding) = raw + let (pool, pool_encoding) = raw .bytes_sz() .map_err(Into::::into) .and_then(|(bytes, enc)| { @@ -657,7 +654,7 @@ impl Deserialize for DRep { .map(|bytes| (bytes, StringEncoding::from(enc))) .map_err(|e| DeserializeFailure::InvalidStructure(Box::new(e)).into()) }) - .map_err(|e: DeserializeError| e.annotate("ed25519_key_hash"))?; + .map_err(|e: DeserializeError| e.annotate("pool"))?; match len { cbor_event::LenSz::Len(_, _) => (), cbor_event::LenSz::Indefinite => match raw.special()? { @@ -666,10 +663,10 @@ impl Deserialize for DRep { }, } Ok(Self::Key { - ed25519_key_hash, + pool, len_encoding, index_0_encoding, - ed25519_key_hash_encoding, + pool_encoding, }) })(raw) { @@ -1471,15 +1468,12 @@ impl SerializeEmbeddedGroup for PoolRetirement { ), )?; serializer.write_bytes_sz( - self.ed25519_key_hash.to_raw_bytes(), + self.pool.to_raw_bytes(), self.encodings .as_ref() - .map(|encs| encs.ed25519_key_hash_encoding.clone()) + .map(|encs| encs.pool_encoding.clone()) .unwrap_or_default() - .to_str_len_sz( - self.ed25519_key_hash.to_raw_bytes().len() as u64, - force_canonical, - ), + .to_str_len_sz(self.pool.to_raw_bytes().len() as u64, force_canonical), )?; serializer.write_unsigned_integer_sz( self.epoch, @@ -1538,7 +1532,7 @@ impl DeserializeEmbeddedGroup for PoolRetirement { Ok(Some(tag_encoding)) })() .map_err(|e| e.annotate("tag"))?; - let (ed25519_key_hash, ed25519_key_hash_encoding) = raw + let (pool, pool_encoding) = raw .bytes_sz() .map_err(Into::::into) .and_then(|(bytes, enc)| { @@ -1546,19 +1540,19 @@ impl DeserializeEmbeddedGroup for PoolRetirement { .map(|bytes| (bytes, StringEncoding::from(enc))) .map_err(|e| DeserializeFailure::InvalidStructure(Box::new(e)).into()) }) - .map_err(|e: DeserializeError| e.annotate("ed25519_key_hash"))?; + .map_err(|e: DeserializeError| e.annotate("pool"))?; let (epoch, epoch_encoding) = raw .unsigned_integer_sz() .map(|(x, enc)| (x, Some(enc))) .map_err(Into::::into) .map_err(|e: DeserializeError| e.annotate("epoch"))?; Ok(PoolRetirement { - ed25519_key_hash, + pool, epoch, encodings: Some(PoolRetirementEncoding { len_encoding, tag_encoding, - ed25519_key_hash_encoding, + pool_encoding, epoch_encoding, }), }) @@ -2305,15 +2299,12 @@ impl SerializeEmbeddedGroup for StakeDelegation { self.stake_credential .serialize(serializer, force_canonical)?; serializer.write_bytes_sz( - self.ed25519_key_hash.to_raw_bytes(), + self.pool.to_raw_bytes(), self.encodings .as_ref() - .map(|encs| encs.ed25519_key_hash_encoding.clone()) + .map(|encs| encs.pool_encoding.clone()) .unwrap_or_default() - .to_str_len_sz( - self.ed25519_key_hash.to_raw_bytes().len() as u64, - force_canonical, - ), + .to_str_len_sz(self.pool.to_raw_bytes().len() as u64, force_canonical), )?; self.encodings .as_ref() @@ -2363,7 +2354,7 @@ impl DeserializeEmbeddedGroup for StakeDelegation { .map_err(|e| e.annotate("tag"))?; let stake_credential = Credential::deserialize(raw) .map_err(|e: DeserializeError| e.annotate("stake_credential"))?; - let (ed25519_key_hash, ed25519_key_hash_encoding) = raw + let (pool, pool_encoding) = raw .bytes_sz() .map_err(Into::::into) .and_then(|(bytes, enc)| { @@ -2371,14 +2362,14 @@ impl DeserializeEmbeddedGroup for StakeDelegation { .map(|bytes| (bytes, StringEncoding::from(enc))) .map_err(|e| DeserializeFailure::InvalidStructure(Box::new(e)).into()) }) - .map_err(|e: DeserializeError| e.annotate("ed25519_key_hash"))?; + .map_err(|e: DeserializeError| e.annotate("pool"))?; Ok(StakeDelegation { stake_credential, - ed25519_key_hash, + pool, encodings: Some(StakeDelegationEncoding { len_encoding, tag_encoding, - ed25519_key_hash_encoding, + pool_encoding, }), }) })() @@ -2519,15 +2510,12 @@ impl SerializeEmbeddedGroup for StakeRegDelegCert { self.stake_credential .serialize(serializer, force_canonical)?; serializer.write_bytes_sz( - self.ed25519_key_hash.to_raw_bytes(), + self.pool.to_raw_bytes(), self.encodings .as_ref() - .map(|encs| encs.ed25519_key_hash_encoding.clone()) + .map(|encs| encs.pool_encoding.clone()) .unwrap_or_default() - .to_str_len_sz( - self.ed25519_key_hash.to_raw_bytes().len() as u64, - force_canonical, - ), + .to_str_len_sz(self.pool.to_raw_bytes().len() as u64, force_canonical), )?; serializer.write_unsigned_integer_sz( self.coin, @@ -2588,7 +2576,7 @@ impl DeserializeEmbeddedGroup for StakeRegDelegCert { .map_err(|e| e.annotate("tag"))?; let stake_credential = Credential::deserialize(raw) .map_err(|e: DeserializeError| e.annotate("stake_credential"))?; - let (ed25519_key_hash, ed25519_key_hash_encoding) = raw + let (pool, pool_encoding) = raw .bytes_sz() .map_err(Into::::into) .and_then(|(bytes, enc)| { @@ -2596,7 +2584,7 @@ impl DeserializeEmbeddedGroup for StakeRegDelegCert { .map(|bytes| (bytes, StringEncoding::from(enc))) .map_err(|e| DeserializeFailure::InvalidStructure(Box::new(e)).into()) }) - .map_err(|e: DeserializeError| e.annotate("ed25519_key_hash"))?; + .map_err(|e: DeserializeError| e.annotate("pool"))?; let (coin, coin_encoding) = raw .unsigned_integer_sz() .map(|(x, enc)| (x, Some(enc))) @@ -2604,12 +2592,12 @@ impl DeserializeEmbeddedGroup for StakeRegDelegCert { .map_err(|e: DeserializeError| e.annotate("coin"))?; Ok(StakeRegDelegCert { stake_credential, - ed25519_key_hash, + pool, coin, encodings: Some(StakeRegDelegCertEncoding { len_encoding, tag_encoding, - ed25519_key_hash_encoding, + pool_encoding, coin_encoding, }), }) @@ -2751,15 +2739,12 @@ impl SerializeEmbeddedGroup for StakeVoteDelegCert { self.stake_credential .serialize(serializer, force_canonical)?; serializer.write_bytes_sz( - self.ed25519_key_hash.to_raw_bytes(), + self.pool.to_raw_bytes(), self.encodings .as_ref() - .map(|encs| encs.ed25519_key_hash_encoding.clone()) + .map(|encs| encs.pool_encoding.clone()) .unwrap_or_default() - .to_str_len_sz( - self.ed25519_key_hash.to_raw_bytes().len() as u64, - force_canonical, - ), + .to_str_len_sz(self.pool.to_raw_bytes().len() as u64, force_canonical), )?; self.d_rep.serialize(serializer, force_canonical)?; self.encodings @@ -2810,7 +2795,7 @@ impl DeserializeEmbeddedGroup for StakeVoteDelegCert { .map_err(|e| e.annotate("tag"))?; let stake_credential = Credential::deserialize(raw) .map_err(|e: DeserializeError| e.annotate("stake_credential"))?; - let (ed25519_key_hash, ed25519_key_hash_encoding) = raw + let (pool, pool_encoding) = raw .bytes_sz() .map_err(Into::::into) .and_then(|(bytes, enc)| { @@ -2818,17 +2803,17 @@ impl DeserializeEmbeddedGroup for StakeVoteDelegCert { .map(|bytes| (bytes, StringEncoding::from(enc))) .map_err(|e| DeserializeFailure::InvalidStructure(Box::new(e)).into()) }) - .map_err(|e: DeserializeError| e.annotate("ed25519_key_hash"))?; + .map_err(|e: DeserializeError| e.annotate("pool"))?; let d_rep = DRep::deserialize(raw).map_err(|e: DeserializeError| e.annotate("d_rep"))?; Ok(StakeVoteDelegCert { stake_credential, - ed25519_key_hash, + pool, d_rep, encodings: Some(StakeVoteDelegCertEncoding { len_encoding, tag_encoding, - ed25519_key_hash_encoding, + pool_encoding, }), }) })() @@ -2873,15 +2858,12 @@ impl SerializeEmbeddedGroup for StakeVoteRegDelegCert { self.stake_credential .serialize(serializer, force_canonical)?; serializer.write_bytes_sz( - self.ed25519_key_hash.to_raw_bytes(), + self.pool.to_raw_bytes(), self.encodings .as_ref() - .map(|encs| encs.ed25519_key_hash_encoding.clone()) + .map(|encs| encs.pool_encoding.clone()) .unwrap_or_default() - .to_str_len_sz( - self.ed25519_key_hash.to_raw_bytes().len() as u64, - force_canonical, - ), + .to_str_len_sz(self.pool.to_raw_bytes().len() as u64, force_canonical), )?; self.d_rep.serialize(serializer, force_canonical)?; serializer.write_unsigned_integer_sz( @@ -2943,7 +2925,7 @@ impl DeserializeEmbeddedGroup for StakeVoteRegDelegCert { .map_err(|e| e.annotate("tag"))?; let stake_credential = Credential::deserialize(raw) .map_err(|e: DeserializeError| e.annotate("stake_credential"))?; - let (ed25519_key_hash, ed25519_key_hash_encoding) = raw + let (pool, pool_encoding) = raw .bytes_sz() .map_err(Into::::into) .and_then(|(bytes, enc)| { @@ -2951,7 +2933,7 @@ impl DeserializeEmbeddedGroup for StakeVoteRegDelegCert { .map(|bytes| (bytes, StringEncoding::from(enc))) .map_err(|e| DeserializeFailure::InvalidStructure(Box::new(e)).into()) }) - .map_err(|e: DeserializeError| e.annotate("ed25519_key_hash"))?; + .map_err(|e: DeserializeError| e.annotate("pool"))?; let d_rep = DRep::deserialize(raw).map_err(|e: DeserializeError| e.annotate("d_rep"))?; let (coin, coin_encoding) = raw @@ -2961,13 +2943,13 @@ impl DeserializeEmbeddedGroup for StakeVoteRegDelegCert { .map_err(|e: DeserializeError| e.annotate("coin"))?; Ok(StakeVoteRegDelegCert { stake_credential, - ed25519_key_hash, + pool, d_rep, coin, encodings: Some(StakeVoteRegDelegCertEncoding { len_encoding, tag_encoding, - ed25519_key_hash_encoding, + pool_encoding, coin_encoding, }), }) diff --git a/chain/rust/src/plutus/mod.rs b/chain/rust/src/plutus/mod.rs index 1288cdbb..b8987f8d 100644 --- a/chain/rust/src/plutus/mod.rs +++ b/chain/rust/src/plutus/mod.rs @@ -14,8 +14,9 @@ use cbor_encodings::{ PlutusV2ScriptEncoding, RedeemerEncoding, }; -use cml_core::serialization::{LenEncoding, StringEncoding}; +use cml_core::serialization::{LenEncoding, Serialize, StringEncoding}; use cml_core::Int; +use cml_crypto::blake2b256; pub use utils::{ConstrPlutusData, PlutusMap, PlutusScript}; @@ -168,6 +169,10 @@ impl PlutusData { bytes_encoding: StringEncoding::default(), } } + + pub fn hash(&self) -> [u8; 32] { + blake2b256(&self.to_cbor_bytes()) + } } #[derive( diff --git a/chain/wasm/src/certs/mod.rs b/chain/wasm/src/certs/mod.rs index a178d183..01ddd59b 100644 --- a/chain/wasm/src/certs/mod.rs +++ b/chain/wasm/src/certs/mod.rs @@ -60,13 +60,10 @@ impl Certificate { )) } - pub fn new_stake_delegation( - stake_credential: &StakeCredential, - ed25519_key_hash: &Ed25519KeyHash, - ) -> Self { + pub fn new_stake_delegation(stake_credential: &StakeCredential, pool: &Ed25519KeyHash) -> Self { Self(cml_chain::certs::Certificate::new_stake_delegation( stake_credential.clone().into(), - ed25519_key_hash.clone().into(), + pool.clone().into(), )) } @@ -76,9 +73,9 @@ impl Certificate { )) } - pub fn new_pool_retirement(ed25519_key_hash: &Ed25519KeyHash, epoch: Epoch) -> Self { + pub fn new_pool_retirement(pool: &Ed25519KeyHash, epoch: Epoch) -> Self { Self(cml_chain::certs::Certificate::new_pool_retirement( - ed25519_key_hash.clone().into(), + pool.clone().into(), epoch, )) } @@ -106,24 +103,24 @@ impl Certificate { pub fn new_stake_vote_deleg_cert( stake_credential: &StakeCredential, - ed25519_key_hash: &Ed25519KeyHash, + pool: &Ed25519KeyHash, d_rep: &DRep, ) -> Self { Self(cml_chain::certs::Certificate::new_stake_vote_deleg_cert( stake_credential.clone().into(), - ed25519_key_hash.clone().into(), + pool.clone().into(), d_rep.clone().into(), )) } pub fn new_stake_reg_deleg_cert( stake_credential: &StakeCredential, - ed25519_key_hash: &Ed25519KeyHash, + pool: &Ed25519KeyHash, coin: Coin, ) -> Self { Self(cml_chain::certs::Certificate::new_stake_reg_deleg_cert( stake_credential.clone().into(), - ed25519_key_hash.clone().into(), + pool.clone().into(), coin, )) } @@ -142,14 +139,14 @@ impl Certificate { pub fn new_stake_vote_reg_deleg_cert( stake_credential: &StakeCredential, - ed25519_key_hash: &Ed25519KeyHash, + pool: &Ed25519KeyHash, d_rep: &DRep, coin: Coin, ) -> Self { Self( cml_chain::certs::Certificate::new_stake_vote_reg_deleg_cert( stake_credential.clone().into(), - ed25519_key_hash.clone().into(), + pool.clone().into(), d_rep.clone().into(), coin, ), @@ -473,10 +470,8 @@ impl_wasm_conversions!(cml_chain::certs::DRep, DRep); #[wasm_bindgen] impl DRep { - pub fn new_key(ed25519_key_hash: &Ed25519KeyHash) -> Self { - Self(cml_chain::certs::DRep::new_key( - ed25519_key_hash.clone().into(), - )) + pub fn new_key(pool: &Ed25519KeyHash) -> Self { + Self(cml_chain::certs::DRep::new_key(pool.clone().into())) } pub fn new_script(script_hash: &ScriptHash) -> Self { @@ -504,9 +499,7 @@ impl DRep { pub fn as_key(&self) -> Option { match &self.0 { - cml_chain::certs::DRep::Key { - ed25519_key_hash, .. - } => Some((*ed25519_key_hash).into()), + cml_chain::certs::DRep::Key { pool, .. } => Some((*pool).into()), _ => None, } } @@ -723,17 +716,17 @@ impl_wasm_conversions!(cml_chain::certs::PoolRetirement, PoolRetirement); #[wasm_bindgen] impl PoolRetirement { - pub fn ed25519_key_hash(&self) -> Ed25519KeyHash { - self.0.ed25519_key_hash.into() + pub fn pool(&self) -> Ed25519KeyHash { + self.0.pool.into() } pub fn epoch(&self) -> Epoch { self.0.epoch } - pub fn new(ed25519_key_hash: &Ed25519KeyHash, epoch: Epoch) -> Self { + pub fn new(pool: &Ed25519KeyHash, epoch: Epoch) -> Self { Self(cml_chain::certs::PoolRetirement::new( - ed25519_key_hash.clone().into(), + pool.clone().into(), epoch, )) } @@ -971,14 +964,14 @@ impl StakeDelegation { self.0.stake_credential.clone().into() } - pub fn ed25519_key_hash(&self) -> Ed25519KeyHash { - self.0.ed25519_key_hash.into() + pub fn pool(&self) -> Ed25519KeyHash { + self.0.pool.into() } - pub fn new(stake_credential: &StakeCredential, ed25519_key_hash: &Ed25519KeyHash) -> Self { + pub fn new(stake_credential: &StakeCredential, pool: &Ed25519KeyHash) -> Self { Self(cml_chain::certs::StakeDelegation::new( stake_credential.clone().into(), - ed25519_key_hash.clone().into(), + pool.clone().into(), )) } } @@ -1018,22 +1011,18 @@ impl StakeRegDelegCert { self.0.stake_credential.clone().into() } - pub fn ed25519_key_hash(&self) -> Ed25519KeyHash { - self.0.ed25519_key_hash.into() + pub fn pool(&self) -> Ed25519KeyHash { + self.0.pool.into() } pub fn coin(&self) -> Coin { self.0.coin } - pub fn new( - stake_credential: &StakeCredential, - ed25519_key_hash: &Ed25519KeyHash, - coin: Coin, - ) -> Self { + pub fn new(stake_credential: &StakeCredential, pool: &Ed25519KeyHash, coin: Coin) -> Self { Self(cml_chain::certs::StakeRegDelegCert::new( stake_credential.clone().into(), - ed25519_key_hash.clone().into(), + pool.clone().into(), coin, )) } @@ -1074,22 +1063,18 @@ impl StakeVoteDelegCert { self.0.stake_credential.clone().into() } - pub fn ed25519_key_hash(&self) -> Ed25519KeyHash { - self.0.ed25519_key_hash.into() + pub fn pool(&self) -> Ed25519KeyHash { + self.0.pool.into() } pub fn d_rep(&self) -> DRep { self.0.d_rep.clone().into() } - pub fn new( - stake_credential: &StakeCredential, - ed25519_key_hash: &Ed25519KeyHash, - d_rep: &DRep, - ) -> Self { + pub fn new(stake_credential: &StakeCredential, pool: &Ed25519KeyHash, d_rep: &DRep) -> Self { Self(cml_chain::certs::StakeVoteDelegCert::new( stake_credential.clone().into(), - ed25519_key_hash.clone().into(), + pool.clone().into(), d_rep.clone().into(), )) } @@ -1112,8 +1097,8 @@ impl StakeVoteRegDelegCert { self.0.stake_credential.clone().into() } - pub fn ed25519_key_hash(&self) -> Ed25519KeyHash { - self.0.ed25519_key_hash.into() + pub fn pool(&self) -> Ed25519KeyHash { + self.0.pool.into() } pub fn d_rep(&self) -> DRep { @@ -1126,13 +1111,13 @@ impl StakeVoteRegDelegCert { pub fn new( stake_credential: &StakeCredential, - ed25519_key_hash: &Ed25519KeyHash, + pool: &Ed25519KeyHash, d_rep: &DRep, coin: Coin, ) -> Self { Self(cml_chain::certs::StakeVoteRegDelegCert::new( stake_credential.clone().into(), - ed25519_key_hash.clone().into(), + pool.clone().into(), d_rep.clone().into(), coin, )) diff --git a/multi-era/rust/src/byron/block/mod.rs b/multi-era/rust/src/byron/block/mod.rs index 33255ab1..94338553 100644 --- a/multi-era/rust/src/byron/block/mod.rs +++ b/multi-era/rust/src/byron/block/mod.rs @@ -10,7 +10,9 @@ use crate::byron::mpc::{Ssc, SscProof}; use crate::byron::transaction::{ByronAttributes, ByronTx, ByronTxProof, ByronTxWitness}; use crate::byron::update::{ByronBlockVersion, ByronSoftwareVersion, ByronUpdate}; use crate::byron::{Blake2b256, ByronBlockId, ByronPubKey, ByronSignature, ByronSlotId, EpochId}; + use cml_chain::byron::StakeholderId; + use std::collections::BTreeMap; #[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)] diff --git a/multi-era/rust/src/utils.rs b/multi-era/rust/src/utils.rs index 4c707488..58ce0636 100644 --- a/multi-era/rust/src/utils.rs +++ b/multi-era/rust/src/utils.rs @@ -15,6 +15,7 @@ use crate::{ mary::MaryBlock, shelley::ShelleyBlock, }; use crate::{MultiEraBlock, MultiEraTransactionBody}; +use cbor_event::cbor; use cbor_event::de::Deserializer; use cml_chain::address::Address; use cml_chain::assets::{Mint, PositiveCoin}; @@ -39,10 +40,10 @@ use cml_chain::{ ProtocolParamUpdate, Rational, UnitInterval, Value, Withdrawals, }; use cml_core::error::{DeserializeError, DeserializeFailure}; -use cml_core::serialization::{CBORReadLen, Deserialize}; +use cml_core::serialization::*; use cml_core::{Epoch, Int, TransactionIndex}; use cml_crypto::{ - AuxiliaryDataHash, BlockBodyHash, BlockHeaderHash, GenesisHash, RawBytesEncoding, + blake2b256, AuxiliaryDataHash, BlockBodyHash, BlockHeaderHash, GenesisHash, RawBytesEncoding, ScriptDataHash, TransactionHash, VRFVkey, }; @@ -233,6 +234,39 @@ impl MultiEraBlock { Self::Conway(block) => block.invalid_transactions.clone(), } } + + pub fn hash(&self) -> [u8; 32] { + let bytes = match self { + Self::Byron(block) => match block { + // why there's no to_bytes or sth for byron block headers? + ByronBlock::EpochBoundary(ebb) => cbor!(ebb.header).unwrap(), + ByronBlock::Main(mb) => cbor!(mb.header).unwrap(), + }, + Self::Shelley(block) => block.header.to_cbor_bytes(), + Self::Allegra(block) => block.header.to_cbor_bytes(), + Self::Mary(block) => block.header.to_cbor_bytes(), + Self::Alonzo(block) => block.header.to_cbor_bytes(), + Self::Babbage(block) => block.header.to_cbor_bytes(), + Self::Conway(block) => block.header.to_cbor_bytes(), + }; + + blake2b256(&bytes) + } + + pub fn is_empty(&self) -> bool { + match self { + MultiEraBlock::Byron(b) => match b { + ByronBlock::EpochBoundary(_) => true, + ByronBlock::Main(block) => block.body.tx_payload.is_empty(), + }, + MultiEraBlock::Shelley(block) => block.transaction_bodies.is_empty(), + MultiEraBlock::Allegra(block) => block.transaction_bodies.is_empty(), + MultiEraBlock::Mary(block) => block.transaction_bodies.is_empty(), + MultiEraBlock::Alonzo(block) => block.transaction_bodies.is_empty(), + MultiEraBlock::Babbage(block) => block.transaction_bodies.is_empty(), + MultiEraBlock::Conway(block) => block.transaction_bodies.is_empty(), + } + } } #[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)] @@ -244,21 +278,24 @@ pub enum MultiEraBlockHeader { } impl MultiEraBlockHeader { - pub fn block_number(&self) -> Option { + pub fn block_number(&self) -> u64 { match self { - Self::ByronEB(_) => None, - Self::Byron(_) => None, - Self::Shelley(header) => Some(header.body.block_number), - Self::Babbage(header) => Some(header.header_body.block_number), + Self::ByronEB(eb) => eb.consensus_data.byron_difficulty.u64, + Self::Byron(b) => b.consensus_data.byron_difficulty.u64, + Self::Shelley(header) => header.body.block_number, + Self::Babbage(header) => header.header_body.block_number, } } - pub fn slot(&self) -> Option { + pub fn slot(&self) -> u64 { match self { - Self::ByronEB(_) => None, - Self::Byron(_) => None, - Self::Shelley(header) => Some(header.body.slot), - Self::Babbage(header) => Some(header.header_body.slot), + Self::ByronEB(eb) => byron_epoch_slot_to_absolute(eb.consensus_data.epoch_id, 0), + Self::Byron(b) => byron_epoch_slot_to_absolute( + b.consensus_data.byron_slot_id.epoch, + b.consensus_data.byron_slot_id.slot, + ), + Self::Shelley(header) => header.body.slot, + Self::Babbage(header) => header.header_body.slot, } } @@ -735,6 +772,20 @@ impl MultiEraTransactionBody { Self::Conway(tx) => tx.donation, } } + + pub fn hash(&self) -> [u8; 32] { + let bytes = match self { + MultiEraTransactionBody::Byron(tx) => cbor!(tx).unwrap(), + MultiEraTransactionBody::Shelley(tx) => tx.to_cbor_bytes(), + MultiEraTransactionBody::Allegra(tx) => tx.to_cbor_bytes(), + MultiEraTransactionBody::Mary(tx) => tx.to_cbor_bytes(), + MultiEraTransactionBody::Alonzo(tx) => tx.to_cbor_bytes(), + MultiEraTransactionBody::Babbage(tx) => tx.to_cbor_bytes(), + MultiEraTransactionBody::Conway(tx) => tx.to_cbor_bytes(), + }; + + blake2b256(&bytes) + } } #[allow(clippy::large_enum_variant)] @@ -1298,6 +1349,13 @@ impl From for CostModels { } } +const KNOWN_SLOT_LENGTH_SECS: u64 = 20; // 20 secs +const KNOWN_EPOCH_LENGTH_SECS: u64 = 5 * 24 * 60 * 60; // 5 days + +fn byron_epoch_slot_to_absolute(epoch: u64, sub_epoch_slot: u64) -> u64 { + ((epoch * KNOWN_EPOCH_LENGTH_SECS) / KNOWN_SLOT_LENGTH_SECS) + sub_epoch_slot +} + #[cfg(test)] mod test { use super::*; diff --git a/multi-era/wasm/src/utils.rs b/multi-era/wasm/src/utils.rs index 0bdbf21f..7cc6841b 100644 --- a/multi-era/wasm/src/utils.rs +++ b/multi-era/wasm/src/utils.rs @@ -100,11 +100,11 @@ impl_wasm_conversions!( #[wasm_bindgen] impl MultiEraBlockHeader { - pub fn block_number(&self) -> Option { + pub fn block_number(&self) -> u64 { self.0.block_number() } - pub fn slot(&self) -> Option { + pub fn slot(&self) -> u64 { self.0.slot() }