From 23b51657a4d44e36ce22a599706dae9769d80781 Mon Sep 17 00:00:00 2001 From: Nikolai Golub Date: Wed, 23 Aug 2023 20:07:33 +0200 Subject: [PATCH] Split AddressTrait (#720) * Removing extra bound on RollupAddress where it is not needed * Update demo-simple-stf docs * Public export * Remove unused H160 --- adapters/celestia/src/celestia.rs | 53 --------------- adapters/celestia/src/verifier/address.rs | 21 +----- examples/demo-rollup/src/lib.rs | 8 +-- examples/demo-simple-stf/README.md | 42 ++++++------ examples/demo-simple-stf/tests/stf_test.rs | 64 ++----------------- .../sov-blob-storage/src/lib.rs | 2 +- .../sov-sequencer-registry/src/lib.rs | 2 +- .../sov-modules-api/src/default_context.rs | 4 +- module-system/sov-modules-api/src/lib.rs | 13 ++-- .../tests/module_info/use_address_trait.rs | 2 +- .../src/app_template.rs | 6 +- .../sov-modules-stf-template/src/lib.rs | 4 +- .../utils/sov-data-generators/src/lib.rs | 4 +- rollup-interface/src/state_machine/da.rs | 4 +- .../src/state_machine/mocks/da.rs | 32 +++++----- rollup-interface/src/state_machine/mod.rs | 21 +++--- rollup-interface/src/state_machine/zk/mod.rs | 4 +- 17 files changed, 83 insertions(+), 203 deletions(-) diff --git a/adapters/celestia/src/celestia.rs b/adapters/celestia/src/celestia.rs index 14daa8c10..67f5a476a 100644 --- a/adapters/celestia/src/celestia.rs +++ b/adapters/celestia/src/celestia.rs @@ -1,6 +1,4 @@ -use std::fmt::{Display, Formatter}; use std::ops::Range; -use std::str::FromStr; use std::sync::{Arc, Mutex}; use base64::engine::general_purpose::STANDARD as B64_ENGINE; @@ -12,7 +10,6 @@ use prost::Message; use serde::{Deserialize, Serialize}; use sov_rollup_interface::da::{BlockHeaderTrait as BlockHeader, CountedBufReader}; use sov_rollup_interface::services::da::SlotData; -use sov_rollup_interface::AddressTrait; pub use tendermint::block::Header as TendermintHeader; use tendermint::block::Height; use tendermint::crypto::default::Sha256; @@ -348,56 +345,6 @@ impl AsRef<[u8]> for Sha2Hash { } } -#[derive(Deserialize, Serialize, PartialEq, Debug, Clone, Eq, Hash)] -pub struct H160(#[serde(deserialize_with = "hex::deserialize")] pub [u8; 20]); - -impl AsRef<[u8]> for H160 { - fn as_ref(&self) -> &[u8] { - self.0.as_ref() - } -} - -impl<'a> TryFrom<&'a [u8]> for H160 { - type Error = anyhow::Error; - - fn try_from(value: &'a [u8]) -> Result { - if value.len() == 20 { - let mut addr = [0u8; 20]; - addr.copy_from_slice(value); - return Ok(Self(addr)); - } - anyhow::bail!("Adress is not exactly 20 bytes"); - } -} - -impl From<[u8; 32]> for H160 { - fn from(value: [u8; 32]) -> Self { - let mut addr = [0u8; 20]; - addr.copy_from_slice(&value[12..]); - Self(addr) - } -} - -impl FromStr for H160 { - type Err = hex::FromHexError; - - fn from_str(s: &str) -> Result { - // Remove the "0x" prefix, if it exists. - let s = s.strip_prefix("0x").unwrap_or(s); - let mut output = [0u8; 20]; - hex::decode_to_slice(s, &mut output)?; - Ok(H160(output)) - } -} - -impl Display for H160 { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "0x{}", hex::encode(self.0)) - } -} - -impl AddressTrait for H160 {} - pub fn parse_pfb_namespace( group: NamespaceGroup, ) -> Result, BoxError> { diff --git a/adapters/celestia/src/verifier/address.rs b/adapters/celestia/src/verifier/address.rs index 4c74b1331..f8ea135d9 100644 --- a/adapters/celestia/src/verifier/address.rs +++ b/adapters/celestia/src/verifier/address.rs @@ -4,7 +4,6 @@ use std::str::FromStr; use bech32::WriteBase32; use borsh::{BorshDeserialize, BorshSerialize}; use serde::{Deserialize, Serialize}; -use sov_rollup_interface::AddressTrait; use thiserror::Error; /// Human Readable Part: "celestia" for Celestia network @@ -52,17 +51,6 @@ impl<'a> TryFrom<&'a [u8]> for CelestiaAddress { } } -/// Panics if any element is not in range 0..32 (u5) -/// TODO: Will be removed after https://github.com/Sovereign-Labs/sovereign-sdk/issues/493 -impl From<[u8; 32]> for CelestiaAddress { - fn from(value: [u8; 32]) -> Self { - for item in value { - bech32::u5::try_from_u8(item).unwrap(); - } - Self(value) - } -} - impl Display for CelestiaAddress { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { let mut w = bech32::Bech32Writer::new(HRP, VARIANT, f)?; @@ -116,7 +104,7 @@ impl FromStr for CelestiaAddress { } } -impl AddressTrait for CelestiaAddress {} +impl sov_rollup_interface::BasicAddress for CelestiaAddress {} #[cfg(test)] mod tests { @@ -232,12 +220,5 @@ mod tests { fn test_borsh(input in proptest::array::uniform20(0u8..=255)) { check_borsh(input); } - - #[test] - fn test_try_from_array(arr in proptest::array::uniform32(0u8..32)) { - let address = CelestiaAddress::from(arr); - let output = format!("{}", address); - prop_assert!(output.starts_with("celestia")); - } } } diff --git a/examples/demo-rollup/src/lib.rs b/examples/demo-rollup/src/lib.rs index 79db7d12d..6f7cdcab6 100644 --- a/examples/demo-rollup/src/lib.rs +++ b/examples/demo-rollup/src/lib.rs @@ -12,7 +12,7 @@ use demo_stf::runtime::GenesisConfig; pub use rollup::{new_rollup_with_celestia_da, Rollup}; use sov_db::ledger_db::LedgerDB; use sov_modules_api::default_context::DefaultContext; -use sov_rollup_interface::AddressTrait; +use sov_rollup_interface::BasicAddress; /// The rollup stores its data in the namespace b"sov-test" on Celestia /// You can change this constant to point your rollup at a different namespace @@ -38,10 +38,10 @@ pub struct HexKey { /// address, simply change the value of the SEQUENCER_DA_ADDRESS to your own address. /// For example: /// ```rust,no_run -/// const SEQUENCER_DA_ADDRESS: [u8;47] = *b"celestia1qp09ysygcx6npted5yc0au6k9lner05yvs9208"; +/// const SEQUENCER_DA_ADDRESS: &str = "celestia1qp09ysygcx6npted5yc0au6k9lner05yvs9208"; /// ``` -pub fn get_genesis_config( - sequencer_da_address: D, +pub fn get_genesis_config( + sequencer_da_address: A, ) -> GenesisConfig { let hex_key: HexKey = serde_json::from_slice(include_bytes!( "../../test-data/keys/token_deployer_private_key.json" diff --git a/examples/demo-simple-stf/README.md b/examples/demo-simple-stf/README.md index 933d0a9aa..9b990249a 100644 --- a/examples/demo-simple-stf/README.md +++ b/examples/demo-simple-stf/README.md @@ -154,37 +154,39 @@ The first transaction that finds the correct hash would break the loop and retur The `sov_rollup_interface::mocks` crate provides two utilities that are useful for testing: 1. The `MockZkvm` is an implementation of the `Zkvm` trait that can be used in tests. -1. The `TestBlob` is an implementation of the `BlobTransactionTrait` trait that can be used in tests. It accepts an `Address` as a generic parameter. For testing purposes, we implement our own `Address` type as follows: - -```rust, ignore -#[derive(PartialEq, Debug, Clone, Eq, serde::Serialize, serde::Deserialize)] -pub struct DaAddress { - pub addr: [u8; 32], -} - -impl AddressTrait for DaAddress {} - -``` +1. The `MockBlob` is an implementation of the `BlobTransactionTrait` trait that can be used in tests. It accepts an `A: BasicAddress` as a generic parameter. For testing purposes, we use `MockAddress` struct from the same `mocks` module You can find more details in the `stf_test.rs` file. The following test checks the rollup logic. In the test, we call `init_chain, begin_slot, and end_slot` for completeness, even though these methods do nothing. -```rust, ignore +```rust +use demo_simple_stf::{ApplySlotResult, CheckHashPreimageStf}; +use sov_rollup_interface::mocks::{MockAddress, MockBlob, MockBlock, MockValidityCond, MockZkvm}; +use sov_rollup_interface::stf::StateTransitionFunction; + #[test] fn test_stf() { - let address = DaAddress { addr: [1; 32] }; + let address = MockAddress { addr: [1; 32] }; let preimage = vec![0; 32]; - let test_blob = TestBlob::::new(preimage, address); - let stf = &mut CheckHashPreimageStf {}; + let test_blob = MockBlob::::new(preimage, address, [0; 32]); + let stf = &mut CheckHashPreimageStf::::default(); + + let data = MockBlock::default(); + let mut blobs = [test_blob]; - StateTransitionFunction::::init_chain(stf, ()); - StateTransitionFunction::::begin_slot(stf, ()); + StateTransitionFunction::>::init_chain(stf, ()); - let receipt = StateTransitionFunction::::apply_blob(stf, test_blob, None); - assert_eq!(receipt.inner, ApplyBlobResult::Success); + let result = StateTransitionFunction::>::apply_slot( + stf, + (), + &data, + &mut blobs, + ); - StateTransitionFunction::::end_slot(stf); + assert_eq!(1, result.batch_receipts.len()); + let receipt = result.batch_receipts[0].clone(); + assert_eq!(receipt.inner, ApplySlotResult::Success); } ``` diff --git a/examples/demo-simple-stf/tests/stf_test.rs b/examples/demo-simple-stf/tests/stf_test.rs index e61b01f19..b8ef8c1f9 100644 --- a/examples/demo-simple-stf/tests/stf_test.rs +++ b/examples/demo-simple-stf/tests/stf_test.rs @@ -1,75 +1,21 @@ -use std::fmt::Display; -use std::str::FromStr; - use demo_simple_stf::{ApplySlotResult, CheckHashPreimageStf}; -use sov_rollup_interface::mocks::{MockBlob, MockBlock, MockValidityCond, MockZkvm}; +use sov_rollup_interface::mocks::{MockAddress, MockBlob, MockBlock, MockValidityCond, MockZkvm}; use sov_rollup_interface::stf::StateTransitionFunction; -use sov_rollup_interface::AddressTrait; - -#[derive(PartialEq, Debug, Clone, Eq, serde::Serialize, serde::Deserialize, Hash)] -pub struct DaAddress { - pub addr: [u8; 32], -} - -impl AddressTrait for DaAddress {} - -impl AsRef<[u8]> for DaAddress { - fn as_ref(&self) -> &[u8] { - &self.addr - } -} - -impl From<[u8; 32]> for DaAddress { - fn from(addr: [u8; 32]) -> Self { - DaAddress { addr } - } -} - -impl FromStr for DaAddress { - type Err = hex::FromHexError; - - fn from_str(s: &str) -> Result { - // Remove the "0x" prefix, if it exists. - let s = s.strip_prefix("0x").unwrap_or(s); - let mut addr = [0u8; 32]; - hex::decode_to_slice(s, &mut addr)?; - Ok(DaAddress { addr }) - } -} - -impl<'a> TryFrom<&'a [u8]> for DaAddress { - type Error = anyhow::Error; - - fn try_from(addr: &'a [u8]) -> Result { - if addr.len() != 32 { - anyhow::bail!("Address must be 32 bytes long"); - } - let mut addr_bytes = [0u8; 32]; - addr_bytes.copy_from_slice(addr); - Ok(Self { addr: addr_bytes }) - } -} - -impl Display for DaAddress { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{:?}", self.addr) - } -} #[test] fn test_stf() { - let address = DaAddress { addr: [1; 32] }; + let address = MockAddress { addr: [1; 32] }; let preimage = vec![0; 32]; - let test_blob = MockBlob::::new(preimage, address, [0; 32]); + let test_blob = MockBlob::::new(preimage, address, [0; 32]); let stf = &mut CheckHashPreimageStf::::default(); let data = MockBlock::default(); let mut blobs = [test_blob]; - StateTransitionFunction::>::init_chain(stf, ()); + StateTransitionFunction::>::init_chain(stf, ()); - let result = StateTransitionFunction::>::apply_slot( + let result = StateTransitionFunction::>::apply_slot( stf, (), &data, diff --git a/module-system/module-implementations/sov-blob-storage/src/lib.rs b/module-system/module-implementations/sov-blob-storage/src/lib.rs index 2bf367af3..19a3ae77f 100644 --- a/module-system/module-implementations/sov-blob-storage/src/lib.rs +++ b/module-system/module-implementations/sov-blob-storage/src/lib.rs @@ -67,7 +67,7 @@ impl BlobStorage { .collect() } - // TODO: Migrate to AddressTrait generic: https://github.com/Sovereign-Labs/sovereign-sdk/issues/622 + // TODO: Migrate to generic: https://github.com/Sovereign-Labs/sovereign-sdk/issues/622 pub(crate) fn get_preferred_sequencer( &self, working_set: &mut WorkingSet, diff --git a/module-system/module-implementations/sov-sequencer-registry/src/lib.rs b/module-system/module-implementations/sov-sequencer-registry/src/lib.rs index bcc8659ca..0321bec39 100644 --- a/module-system/module-implementations/sov-sequencer-registry/src/lib.rs +++ b/module-system/module-implementations/sov-sequencer-registry/src/lib.rs @@ -180,7 +180,7 @@ impl SequencerRegistry { } /// Checks whether `sender` is a registered sequencer. - pub fn is_sender_allowed( + pub fn is_sender_allowed( &self, sender: &T, working_set: &mut WorkingSet, diff --git a/module-system/sov-modules-api/src/default_context.rs b/module-system/sov-modules-api/src/default_context.rs index 4296572a4..c5593e7f6 100644 --- a/module-system/sov-modules-api/src/default_context.rs +++ b/module-system/sov-modules-api/src/default_context.rs @@ -1,7 +1,7 @@ #[cfg(feature = "native")] use serde::{Deserialize, Serialize}; use sha2::Digest; -use sov_rollup_interface::AddressTrait; +use sov_rollup_interface::RollupAddress; #[cfg(feature = "native")] use sov_state::ProverStorage; use sov_state::{ArrayWitness, DefaultStorageSpec, ZkStorage}; @@ -67,7 +67,7 @@ impl Context for ZkDefaultContext { } impl PublicKey for DefaultPublicKey { - fn to_address(&self) -> A { + fn to_address(&self) -> A { let pub_key_hash = { let mut hasher = ::Hasher::new(); hasher.update(self.pub_key); diff --git a/module-system/sov-modules-api/src/lib.rs b/module-system/sov-modules-api/src/lib.rs index d75ff8238..773c8c098 100644 --- a/module-system/sov-modules-api/src/lib.rs +++ b/module-system/sov-modules-api/src/lib.rs @@ -54,7 +54,7 @@ pub use error::Error; pub use prefix::Prefix; pub use response::CallResponse; use serde::{Deserialize, Serialize}; -pub use sov_rollup_interface::{digest, AddressTrait}; +pub use sov_rollup_interface::{digest, BasicAddress, RollupAddress}; use sov_state::{Storage, Witness, WorkingSet}; use thiserror::Error; @@ -66,7 +66,8 @@ impl AsRef<[u8]> for Address { } } -impl AddressTrait for Address {} +impl BasicAddress for Address {} +impl RollupAddress for Address {} #[cfg_attr(feature = "native", derive(schemars::JsonSchema))] #[derive(PartialEq, Clone, Copy, Eq, borsh::BorshDeserialize, borsh::BorshSerialize, Hash)] @@ -143,7 +144,7 @@ pub enum NonInstantiable {} /// PublicKey used in the Module System. pub trait PublicKey { - fn to_address(&self) -> A; + fn to_address(&self) -> A; } /// A PrivateKey used in the Module System. @@ -154,7 +155,7 @@ pub trait PrivateKey { fn generate() -> Self; fn pub_key(&self) -> Self::PublicKey; fn sign(&self, msg: &[u8]) -> Self::Signature; - fn to_address(&self) -> A { + fn to_address(&self) -> A { self.pub_key().to_address::() } } @@ -171,7 +172,7 @@ pub trait PrivateKey { pub trait Spec { /// The Address type used on the rollup. Typically calculated as the hash of a public key. #[cfg(feature = "native")] - type Address: AddressTrait + type Address: RollupAddress + BorshSerialize + BorshDeserialize + Sync @@ -184,7 +185,7 @@ pub trait Spec { /// The Address type used on the rollup. Typically calculated as the hash of a public key. #[cfg(not(feature = "native"))] - type Address: AddressTrait + BorshSerialize + BorshDeserialize; + type Address: RollupAddress + BorshSerialize + BorshDeserialize; /// Authenticated state storage used by the rollup. Typically some variant of a merkle-patricia trie. type Storage: Storage + Clone + Send + Sync; diff --git a/module-system/sov-modules-macros/tests/module_info/use_address_trait.rs b/module-system/sov-modules-macros/tests/module_info/use_address_trait.rs index 6948cd397..641d3f338 100644 --- a/module-system/sov-modules-macros/tests/module_info/use_address_trait.rs +++ b/module-system/sov-modules-macros/tests/module_info/use_address_trait.rs @@ -2,7 +2,7 @@ #![allow(unused_imports)] -use sov_modules_api::{AddressTrait, Context, ModuleInfo}; +use sov_modules_api::{Context, ModuleInfo, RollupAddress}; #[derive(ModuleInfo)] struct TestModule { diff --git a/module-system/sov-modules-stf-template/src/app_template.rs b/module-system/sov-modules-stf-template/src/app_template.rs index 7a99771d8..8766d3465 100644 --- a/module-system/sov-modules-stf-template/src/app_template.rs +++ b/module-system/sov-modules-stf-template/src/app_template.rs @@ -4,7 +4,7 @@ use borsh::BorshDeserialize; use sov_modules_api::{Context, DispatchCall}; use sov_rollup_interface::da::{BlobReaderTrait, CountedBufReader, DaSpec}; use sov_rollup_interface::stf::{BatchReceipt, TransactionReceipt}; -use sov_rollup_interface::{AddressTrait, Buf}; +use sov_rollup_interface::{BasicAddress, Buf}; use sov_state::StateCheckpoint; use tracing::{debug, error}; @@ -39,7 +39,7 @@ pub struct AppTemplate< phantom_da: PhantomData, } -pub(crate) enum ApplyBatchError { +pub(crate) enum ApplyBatchError { // Contains batch hash Ignored([u8; 32]), Slashed { @@ -50,7 +50,7 @@ pub(crate) enum ApplyBatchError { }, } -impl From> for BatchReceipt, TxEffect> { +impl From> for BatchReceipt, TxEffect> { fn from(value: ApplyBatchError) -> Self { match value { ApplyBatchError::Ignored(hash) => BatchReceipt { diff --git a/module-system/sov-modules-stf-template/src/lib.rs b/module-system/sov-modules-stf-template/src/lib.rs index 421b1d1a6..54bd73265 100644 --- a/module-system/sov-modules-stf-template/src/lib.rs +++ b/module-system/sov-modules-stf-template/src/lib.rs @@ -13,7 +13,7 @@ use sov_rollup_interface::da::{BlobReaderTrait, DaSpec}; use sov_rollup_interface::services::da::SlotData; use sov_rollup_interface::stf::{SlotResult, StateTransitionFunction}; use sov_rollup_interface::zk::{ValidityCondition, Zkvm}; -use sov_rollup_interface::AddressTrait; +use sov_rollup_interface::BasicAddress; use sov_state::{StateCheckpoint, Storage, WorkingSet}; use tracing::info; pub use tx_verifier::RawTx; @@ -42,7 +42,7 @@ pub enum TxEffect { #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] /// Represents the different outcomes that can occur for a sequencer after batch processing. -pub enum SequencerOutcome { +pub enum SequencerOutcome { /// Sequencer receives reward amount in defined token and can withdraw its deposit Rewarded(u64), /// Sequencer loses its deposit and receives no reward diff --git a/module-system/utils/sov-data-generators/src/lib.rs b/module-system/utils/sov-data-generators/src/lib.rs index 9aa37ebb3..021eefd58 100644 --- a/module-system/utils/sov-data-generators/src/lib.rs +++ b/module-system/utils/sov-data-generators/src/lib.rs @@ -8,7 +8,7 @@ use sov_modules_stf_template::{Batch, RawTx, SequencerOutcome, TxEffect}; use sov_rollup_interface::da::DaSpec; use sov_rollup_interface::mocks::{MockAddress, MockBlob, MockDaSpec}; use sov_rollup_interface::stf::BatchReceipt; -use sov_rollup_interface::AddressTrait; +use sov_rollup_interface::RollupAddress; pub mod bank_data; pub mod election_data; @@ -24,7 +24,7 @@ pub fn new_test_blob_from_batch( MockBlob::new(data, address, hash) } -pub fn has_tx_events( +pub fn has_tx_events( apply_blob_outcome: &BatchReceipt, TxEffect>, ) -> bool { let events = apply_blob_outcome diff --git a/rollup-interface/src/state_machine/da.rs b/rollup-interface/src/state_machine/da.rs index b66f620fd..a59463735 100644 --- a/rollup-interface/src/state_machine/da.rs +++ b/rollup-interface/src/state_machine/da.rs @@ -11,7 +11,7 @@ use serde::de::DeserializeOwned; use serde::{Deserialize, Serialize}; use crate::zk::ValidityCondition; -use crate::AddressTrait; +use crate::BasicAddress; /// A specification for the types used by a DA layer. pub trait DaSpec { @@ -166,7 +166,7 @@ pub trait BlobReaderTrait: Serialize + DeserializeOwned + Send + Sync + 'static type Data: Buf; /// The type used to represent addresses on the DA layer. - type Address: AddressTrait; + type Address: BasicAddress; /// Returns the address (on the DA layer) of the entity which submitted the blob transaction fn sender(&self) -> Self::Address; diff --git a/rollup-interface/src/state_machine/mocks/da.rs b/rollup-interface/src/state_machine/mocks/da.rs index e91218df8..97edc0222 100644 --- a/rollup-interface/src/state_machine/mocks/da.rs +++ b/rollup-interface/src/state_machine/mocks/da.rs @@ -1,7 +1,6 @@ use std::fmt::Display; use std::sync::Arc; -use anyhow::{bail, Error}; use async_trait::async_trait; use bytes::Bytes; use serde::{Deserialize, Serialize}; @@ -11,7 +10,7 @@ use crate::da::{BlobReaderTrait, BlockHashTrait, BlockHeaderTrait, CountedBufRea use crate::mocks::MockValidityCond; use crate::services::batch_builder::BatchBuilder; use crate::services::da::{DaService, SlotData}; -use crate::AddressTrait; +use crate::{BasicAddress, RollupAddress}; /// A mock address type used for testing. Internally, this type is standard 32 byte array. #[derive( @@ -38,7 +37,7 @@ impl core::str::FromStr for MockAddress { } impl<'a> TryFrom<&'a [u8]> for MockAddress { - type Error = Error; + type Error = anyhow::Error; fn try_from(addr: &'a [u8]) -> Result { if addr.len() != 32 { @@ -68,7 +67,8 @@ impl Display for MockAddress { } } -impl AddressTrait for MockAddress {} +impl BasicAddress for MockAddress {} +impl RollupAddress for MockAddress {} #[derive( Debug, @@ -81,24 +81,20 @@ impl AddressTrait for MockAddress {} )] /// A mock BlobTransaction from a DA layer used for testing. -pub struct MockBlob
{ - address: Address, +pub struct MockBlob { + address: A, hash: [u8; 32], data: CountedBufReader, } -impl BlobReaderTrait for MockBlob
{ +impl BlobReaderTrait for MockBlob { type Data = Bytes; - type Address = Address; + type Address = A; fn sender(&self) -> Self::Address { self.address.clone() } - fn hash(&self) -> [u8; 32] { - self.hash - } - fn data_mut(&mut self) -> &mut CountedBufReader { &mut self.data } @@ -106,11 +102,15 @@ impl BlobReaderTrait for MockBlob
{ fn data(&self) -> &CountedBufReader { &self.data } + + fn hash(&self) -> [u8; 32] { + self.hash + } } -impl MockBlob
{ +impl MockBlob { /// Creates a new mock blob with the given data, claiming to have been published by the provided address. - pub fn new(data: Vec, address: Address, hash: [u8; 32]) -> Self { + pub fn new(data: Vec, address: A, hash: [u8; 32]) -> Self { Self { address, data: CountedBufReader::new(bytes::Bytes::from(data)), @@ -201,9 +201,9 @@ pub struct MockDaSpec; impl DaSpec for MockDaSpec { type SlotHash = MockHash; - type ValidityCondition = MockValidityCond; type BlockHeader = MockBlockHeader; type BlobTransaction = MockBlob; + type ValidityCondition = MockValidityCond; type InclusionMultiProof = [u8; 32]; type CompletenessProof = (); type ChainParams = (); @@ -294,7 +294,7 @@ impl BatchBuilder for MockBatchBuilder { fn get_next_blob(&mut self) -> anyhow::Result>> { if self.mempool.is_empty() { - bail!("Mock mempool is empty"); + anyhow::bail!("Mock mempool is empty"); } let txs = std::mem::take(&mut self.mempool) .into_iter() diff --git a/rollup-interface/src/state_machine/mod.rs b/rollup-interface/src/state_machine/mod.rs index 170ee09a4..e5242e2e1 100644 --- a/rollup-interface/src/state_machine/mod.rs +++ b/rollup-interface/src/state_machine/mod.rs @@ -14,21 +14,24 @@ pub mod mocks; pub mod optimistic; -/// A marker trait for addresses. -pub trait AddressTrait: - PartialEq +/// A marker trait for general addresses. +pub trait BasicAddress: + Eq + + PartialEq + core::fmt::Debug + + core::fmt::Display + + Send + + Sync + Clone + + std::hash::Hash + AsRef<[u8]> + for<'a> TryFrom<&'a [u8], Error = anyhow::Error> - + Eq + + std::str::FromStr + Serialize + DeserializeOwned - + From<[u8; 32]> - + Send - + Sync - + core::fmt::Display - + std::hash::Hash + 'static { } + +/// An address used inside rollup +pub trait RollupAddress: BasicAddress + From<[u8; 32]> {} diff --git a/rollup-interface/src/state_machine/zk/mod.rs b/rollup-interface/src/state_machine/zk/mod.rs index f022ffd3a..89a555607 100644 --- a/rollup-interface/src/state_machine/zk/mod.rs +++ b/rollup-interface/src/state_machine/zk/mod.rs @@ -13,7 +13,7 @@ use digest::Digest; use serde::de::DeserializeOwned; use serde::{Deserialize, Serialize}; -use crate::AddressTrait; +use crate::RollupAddress; /// A trait implemented by the prover ("host") of a zkVM program. pub trait ZkvmHost: Zkvm { @@ -46,7 +46,7 @@ pub trait Zkvm { /// TODO: specify a deserializer for the output fn verify_and_extract_output< C: ValidityCondition, - Add: AddressTrait + BorshDeserialize + BorshSerialize, + Add: RollupAddress + BorshDeserialize + BorshSerialize, >( serialized_proof: &[u8], code_commitment: &Self::CodeCommitment,