From 7a05e1f6914c60b51633234e093fae9611681d17 Mon Sep 17 00:00:00 2001 From: Blazej Kolad Date: Wed, 4 Oct 2023 18:31:25 +0200 Subject: [PATCH] Initialize genesis state form genesis files. (#972) * Custom serde for MockAddress * Unit tests * fix lint * sequencer_registry config * remove priv key reading * genesis_paths * GenesisPaths in tests * move tests files * fix demo-stf tests * Fix benches * Custom serializatin for CelestiaAddress * supprt demo configuration * Fix benches * fix lint * take paths by reference * cleanup * Add docs * Add experimental flag * Adressing code review * cleanup * fix ci * fix features --- adapters/celestia/src/verifier/address.rs | 63 +++++++++++++- examples/demo-prover/Cargo.toml | 6 +- examples/demo-prover/benches/prover_bench.rs | 13 ++- examples/demo-prover/src/main.rs | 20 ++++- examples/demo-rollup/benches/rng_xfers.rs | 5 +- examples/demo-rollup/benches/rollup_bench.rs | 22 ++++- .../benches/rollup_coarse_measure.rs | 22 ++++- examples/demo-rollup/src/main.rs | 35 +++++++- examples/demo-rollup/src/rollup.rs | 22 +++-- examples/demo-rollup/tests/bank/mod.rs | 13 ++- examples/demo-rollup/tests/evm/mod.rs | 13 ++- examples/demo-rollup/tests/test_helpers.rs | 9 +- examples/demo-stf/src/genesis_config.rs | 87 +++++++++++-------- examples/demo-stf/src/tests/mod.rs | 19 ++-- examples/demo-stf/src/tests/stf_tests.rs | 13 ++- .../demo-stf/src/tests/tx_revert_tests.rs | 15 ++-- .../genesis/{ => demo-tests}/accounts.json | 0 .../genesis/{ => demo-tests}/bank.json | 0 .../genesis/{ => demo-tests}/chain_state.json | 0 .../genesis/{ => demo-tests}/evm.json | 0 .../demo-tests/sequencer_registry.json | 9 ++ .../{ => demo-tests}/value_setter.json | 0 .../genesis/integration-tests/accounts.json | 3 + .../genesis/integration-tests/bank.json | 10 +++ .../integration-tests/chain_state.json | 7 ++ .../genesis/integration-tests/evm.json | 24 +++++ .../integration-tests/sequencer_registry.json | 9 ++ .../integration-tests/value_setter.json | 3 + .../sov-bank/src/token.rs | 9 +- .../sov-sequencer-registry/src/lib.rs | 6 +- .../sov-sequencer-registry/src/tests.rs | 51 +++++++++++ .../src/state_machine/mocks/da.rs | 46 +++++++--- .../src/state_machine/mocks/mod.rs | 2 +- 33 files changed, 445 insertions(+), 111 deletions(-) rename examples/test-data/genesis/{ => demo-tests}/accounts.json (100%) rename examples/test-data/genesis/{ => demo-tests}/bank.json (100%) rename examples/test-data/genesis/{ => demo-tests}/chain_state.json (100%) rename examples/test-data/genesis/{ => demo-tests}/evm.json (100%) create mode 100644 examples/test-data/genesis/demo-tests/sequencer_registry.json rename examples/test-data/genesis/{ => demo-tests}/value_setter.json (100%) create mode 100644 examples/test-data/genesis/integration-tests/accounts.json create mode 100644 examples/test-data/genesis/integration-tests/bank.json create mode 100644 examples/test-data/genesis/integration-tests/chain_state.json create mode 100644 examples/test-data/genesis/integration-tests/evm.json create mode 100644 examples/test-data/genesis/integration-tests/sequencer_registry.json create mode 100644 examples/test-data/genesis/integration-tests/value_setter.json create mode 100644 module-system/module-implementations/sov-sequencer-registry/src/tests.rs diff --git a/adapters/celestia/src/verifier/address.rs b/adapters/celestia/src/verifier/address.rs index 0750a36ff..064422ec4 100644 --- a/adapters/celestia/src/verifier/address.rs +++ b/adapters/celestia/src/verifier/address.rs @@ -3,7 +3,6 @@ use std::str::FromStr; use bech32::WriteBase32; use borsh::{BorshDeserialize, BorshSerialize}; -use serde::{Deserialize, Serialize}; use thiserror::Error; /// Human Readable Part: "celestia" for Celestia network @@ -15,11 +14,41 @@ const VARIANT: bech32::Variant = bech32::Variant::Bech32; /// /// Spec says: "Addresses have a length of 32 bytes.", but in reality it is 32 `u5` elements, which can be compressed as 20 bytes. /// TODO: Switch to bech32::u5 when it has repr transparent: -#[derive( - Debug, PartialEq, Clone, Eq, Serialize, Deserialize, BorshDeserialize, BorshSerialize, Hash, -)] +#[derive(Debug, PartialEq, Clone, Eq, BorshDeserialize, BorshSerialize, Hash)] pub struct CelestiaAddress([u8; 32]); +impl serde::Serialize for CelestiaAddress { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + if serializer.is_human_readable() { + let serialized = format!("{}", &self); + serializer.serialize_str(&serialized) + } else { + serde::Serialize::serialize(&self.0, serializer) + } + } +} + +impl<'de> serde::Deserialize<'de> for CelestiaAddress { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + if deserializer.is_human_readable() { + let address_bech32: String = serde::Deserialize::deserialize(deserializer)?; + address_bech32 + .as_bytes() + .try_into() + .map_err(serde::de::Error::custom) + } else { + let addr = <[u8; 32] as serde::Deserialize>::deserialize(deserializer)?; + Ok(CelestiaAddress(addr)) + } + } +} + impl AsRef<[u8]> for CelestiaAddress { fn as_ref(&self) -> &[u8] { self.0.as_ref() @@ -180,6 +209,32 @@ mod tests { assert_eq!(address_str2, address_str); } + #[test] + fn test_human_readable_address_serialization() { + let address = CelestiaAddress([11; 32]); + let data: String = serde_json::to_string(&address).unwrap(); + let deserialized_address = serde_json::from_str::(&data).unwrap(); + + assert_eq!(address, deserialized_address); + assert_eq!( + deserialized_address.to_string(), + "celestia1tttttttttttttttttttttttttttttttt9grhcq" + ); + } + + #[test] + fn test_binary_address_serialization() { + let address = CelestiaAddress([11; 32]); + let data = postcard::to_allocvec(&address).unwrap(); + let deserialized_address = postcard::from_bytes::(&data).unwrap(); + + assert_eq!(address, deserialized_address); + assert_eq!( + deserialized_address.to_string(), + "celestia1tttttttttttttttttttttttttttttttt9grhcq" + ); + } + proptest! { #[test] fn test_try_from_any_slice(input in prop::collection::vec(any::(), 0..100)) { diff --git a/examples/demo-prover/Cargo.toml b/examples/demo-prover/Cargo.toml index 444267b6d..a85b5e378 100644 --- a/examples/demo-prover/Cargo.toml +++ b/examples/demo-prover/Cargo.toml @@ -22,7 +22,7 @@ tracing-subscriber = "0.3.16" sov-celestia-adapter = { path = "../../adapters/celestia", features = ["native", "bench"] } sov-demo-rollup = { path = "../demo-rollup", features = ["native"] } -demo-stf = { path = "../demo-stf", optional = true } +demo-stf = { path = "../demo-stf" } sov-rollup-interface = { path = "../../rollup-interface" } sov-risc0-adapter = { path = "../../adapters/risc0", features = ["native"] } const-rollup-config = { path = "../const-rollup-config" } @@ -50,5 +50,5 @@ harness = false required-features = ["bench"] [features] -bench = ["sov-risc0-adapter/bench", "sov-zk-cycle-macros/bench", "methods/bench", "demo-stf"] -experimental = ["sov-demo-rollup/experimental"] +bench = ["sov-risc0-adapter/bench", "sov-zk-cycle-macros/bench", "methods/bench", "experimental"] +experimental = ["sov-demo-rollup/experimental", "demo-stf/experimental"] diff --git a/examples/demo-prover/benches/prover_bench.rs b/examples/demo-prover/benches/prover_bench.rs index b2c2b8228..da19fb691 100644 --- a/examples/demo-prover/benches/prover_bench.rs +++ b/examples/demo-prover/benches/prover_bench.rs @@ -9,7 +9,7 @@ use std::sync::{Arc, Mutex}; use anyhow::Context; use const_rollup_config::{ROLLUP_NAMESPACE_RAW, SEQUENCER_DA_ADDRESS}; use demo_stf::app::App; -use demo_stf::genesis_config::get_genesis_config; +use demo_stf::genesis_config::{get_genesis_config, GenesisPaths}; use log4rs::config::{Appender, Config, Root}; use methods::ROLLUP_ELF; use regex::Regex; @@ -25,6 +25,16 @@ use sov_rollup_interface::zk::ZkvmHost; use sov_stf_runner::{from_toml_path, RollupConfig}; use tempfile::TempDir; +const GENESIS_PATHS: GenesisPaths<&str> = GenesisPaths { + bank_genesis_path: "../test-data/genesis/demo-tests/bank.json", + sequencer_genesis_path: "../test-data/genesis/demo-tests/sequencer_registry.json", + value_setter_genesis_path: "../test-data/genesis/demo-tests/value_setter.json", + accounts_genesis_path: "../test-data/genesis/demo-tests/accounts.json", + chain_state_genesis_path: "../test-data/genesis/demo-tests/chain_state.json", + #[cfg(feature = "experimental")] + evm_genesis_path: "../test-data/genesis/demo-tests/evm.json", +}; + #[derive(Debug)] struct RegexAppender { regex: Regex, @@ -169,6 +179,7 @@ async fn main() -> Result<(), anyhow::Error> { let genesis_config = get_genesis_config( sequencer_da_address, + &GENESIS_PATHS, #[cfg(feature = "experimental")] Default::default(), ); diff --git a/examples/demo-prover/src/main.rs b/examples/demo-prover/src/main.rs index c37d91d1d..80725a834 100644 --- a/examples/demo-prover/src/main.rs +++ b/examples/demo-prover/src/main.rs @@ -1,5 +1,6 @@ use std::env; +use demo_stf::genesis_config::GenesisPaths; use methods::ROLLUP_ELF; use sov_demo_rollup::{new_rollup_with_celestia_da, DemoProverConfig}; use sov_risc0_adapter::host::Risc0Host; @@ -7,6 +8,16 @@ use tracing::info; use tracing_subscriber::filter::LevelFilter; use tracing_subscriber::EnvFilter; +const GENESIS_PATHS: GenesisPaths<&str> = GenesisPaths { + bank_genesis_path: "../test-data/genesis/demo-tests/bank.json", + sequencer_genesis_path: "../test-data/genesis/demo-tests/sequencer_registry.json", + value_setter_genesis_path: "../test-data/genesis/demo-tests/value_setter.json", + accounts_genesis_path: "../test-data/genesis/demo-tests/accounts.json", + chain_state_genesis_path: "../test-data/genesis/demo-tests/chain_state.json", + #[cfg(feature = "experimental")] + evm_genesis_path: "../test-data/genesis/demo-tests/evm.json", +}; + #[tokio::main] async fn main() -> Result<(), anyhow::Error> { // If SKIP_PROVER is set, We still compile and run the zkVM code inside of an emulator without generating @@ -39,8 +50,13 @@ async fn main() -> Result<(), anyhow::Error> { // Initialize the rollup. For this demo, we use Risc0 and Celestia. let prover = Risc0Host::new(ROLLUP_ELF); - let rollup = - new_rollup_with_celestia_da(&rollup_config_path, Some((prover, prover_config))).await?; + + let rollup = new_rollup_with_celestia_da( + &rollup_config_path, + Some((prover, prover_config)), + &GENESIS_PATHS, + ) + .await?; rollup.run().await?; Ok(()) diff --git a/examples/demo-rollup/benches/rng_xfers.rs b/examples/demo-rollup/benches/rng_xfers.rs index 544a03e4e..0121c3bcc 100644 --- a/examples/demo-rollup/benches/rng_xfers.rs +++ b/examples/demo-rollup/benches/rng_xfers.rs @@ -11,11 +11,10 @@ use sov_modules_api::{Address, AddressBech32, EncodeCall, PrivateKey, PublicKey, use sov_rollup_interface::da::{DaSpec, DaVerifier}; use sov_rollup_interface::mocks::{ MockAddress, MockBlob, MockBlock, MockBlockHeader, MockHash, MockValidityCond, + MOCK_SEQUENCER_DA_ADDRESS, }; use sov_rollup_interface::services::da::DaService; -pub const SEQUENCER_DA_ADDRESS: [u8; 32] = [99; 32]; - #[derive(Clone)] /// A simple DaService for a random number generator. pub struct RngDaService; @@ -154,7 +153,7 @@ impl DaService for RngDaService { generate_transfers(num_txns, (block.header.height - 1) * (num_txns as u64)) }; - let address = MockAddress::from(SEQUENCER_DA_ADDRESS); + let address = MockAddress::from(MOCK_SEQUENCER_DA_ADDRESS); let blob = MockBlob::new(data, address, [0u8; 32]); vec![blob] diff --git a/examples/demo-rollup/benches/rollup_bench.rs b/examples/demo-rollup/benches/rollup_bench.rs index 53d33daef..243db48fb 100644 --- a/examples/demo-rollup/benches/rollup_bench.rs +++ b/examples/demo-rollup/benches/rollup_bench.rs @@ -7,16 +7,28 @@ use std::time::Duration; use anyhow::Context; use criterion::{criterion_group, criterion_main, Criterion}; use demo_stf::app::App; -use demo_stf::genesis_config::get_genesis_config; -use rng_xfers::{RngDaService, RngDaSpec, SEQUENCER_DA_ADDRESS}; +use demo_stf::genesis_config::{get_genesis_config, GenesisPaths}; +use rng_xfers::{RngDaService, RngDaSpec}; use sov_db::ledger_db::{LedgerDB, SlotCommit}; use sov_risc0_adapter::host::Risc0Verifier; -use sov_rollup_interface::mocks::{MockAddress, MockBlock, MockBlockHeader}; +use sov_rollup_interface::mocks::{ + MockAddress, MockBlock, MockBlockHeader, MOCK_SEQUENCER_DA_ADDRESS, +}; use sov_rollup_interface::services::da::DaService; use sov_rollup_interface::stf::StateTransitionFunction; use sov_stf_runner::{from_toml_path, RollupConfig}; use tempfile::TempDir; +const TEST_GENESIS_PATHS: GenesisPaths<&str> = GenesisPaths { + bank_genesis_path: "../test-data/genesis/integration-tests/bank.json", + sequencer_genesis_path: "../test-data/genesis/integration-tests/sequencer_registry.json", + value_setter_genesis_path: "../test-data/genesis/integration-tests/value_setter.json", + accounts_genesis_path: "../test-data/genesis/integration-tests/accounts.json", + chain_state_genesis_path: "../test-data/genesis/integration-tests/chain_state.json", + #[cfg(feature = "experimental")] + evm_genesis_path: "../test-data/genesis/integration-tests/evm.json", +}; + fn rollup_bench(_bench: &mut Criterion) { let start_height: u64 = 0u64; let mut end_height: u64 = 100u64; @@ -43,9 +55,11 @@ fn rollup_bench(_bench: &mut Criterion) { let demo_runner = App::::new(rollup_config.storage); let mut demo = demo_runner.stf; - let sequencer_da_address = MockAddress::from(SEQUENCER_DA_ADDRESS); + let sequencer_da_address = MockAddress::from(MOCK_SEQUENCER_DA_ADDRESS); + let demo_genesis_config = get_genesis_config( sequencer_da_address, + &TEST_GENESIS_PATHS, #[cfg(feature = "experimental")] Default::default(), ); diff --git a/examples/demo-rollup/benches/rollup_coarse_measure.rs b/examples/demo-rollup/benches/rollup_coarse_measure.rs index cab4055b6..05c6a3ea5 100644 --- a/examples/demo-rollup/benches/rollup_coarse_measure.rs +++ b/examples/demo-rollup/benches/rollup_coarse_measure.rs @@ -6,12 +6,14 @@ use std::time::{Duration, Instant}; use anyhow::Context; use demo_stf::app::App; -use demo_stf::genesis_config::get_genesis_config; +use demo_stf::genesis_config::{get_genesis_config, GenesisPaths}; use prometheus::{Histogram, HistogramOpts, Registry}; -use rng_xfers::{RngDaService, RngDaSpec, SEQUENCER_DA_ADDRESS}; +use rng_xfers::{RngDaService, RngDaSpec}; use sov_db::ledger_db::{LedgerDB, SlotCommit}; use sov_risc0_adapter::host::Risc0Verifier; -use sov_rollup_interface::mocks::{MockAddress, MockBlock, MockBlockHeader}; +use sov_rollup_interface::mocks::{ + MockAddress, MockBlock, MockBlockHeader, MOCK_SEQUENCER_DA_ADDRESS, +}; use sov_rollup_interface::services::da::DaService; use sov_rollup_interface::stf::StateTransitionFunction; use sov_stf_runner::{from_toml_path, RollupConfig}; @@ -23,6 +25,16 @@ extern crate prettytable; use prettytable::Table; use sov_modules_stf_template::TxEffect; +const TEST_GENESIS_PATHS: GenesisPaths<&str> = GenesisPaths { + bank_genesis_path: "../test-data/genesis/integration-tests/bank.json", + sequencer_genesis_path: "../test-data/genesis/integration-tests/sequencer_registry.json", + value_setter_genesis_path: "../test-data/genesis/integration-tests/value_setter.json", + accounts_genesis_path: "../test-data/genesis/integration-tests/accounts.json", + chain_state_genesis_path: "../test-data/genesis/integration-tests/chain_state.json", + #[cfg(feature = "experimental")] + evm_genesis_path: "../test-data/genesis/integration-tests/evm.json", +}; + fn print_times( total: Duration, apply_block_time: Duration, @@ -100,9 +112,11 @@ async fn main() -> Result<(), anyhow::Error> { let demo_runner = App::::new(rollup_config.storage); let mut demo = demo_runner.stf; - let sequencer_da_address = MockAddress::from(SEQUENCER_DA_ADDRESS); + let sequencer_da_address = MockAddress::from(MOCK_SEQUENCER_DA_ADDRESS); + let demo_genesis_config = get_genesis_config( sequencer_da_address, + &TEST_GENESIS_PATHS, #[cfg(feature = "experimental")] Default::default(), ); diff --git a/examples/demo-rollup/src/main.rs b/examples/demo-rollup/src/main.rs index cfccceff3..b354d3fc8 100644 --- a/examples/demo-rollup/src/main.rs +++ b/examples/demo-rollup/src/main.rs @@ -1,11 +1,32 @@ use std::str::FromStr; use clap::Parser; +use demo_stf::genesis_config::GenesisPaths; use sov_demo_rollup::{new_rollup_with_celestia_da, new_rollup_with_mock_da}; use sov_risc0_adapter::host::Risc0Host; use tracing_subscriber::prelude::*; use tracing_subscriber::{fmt, EnvFilter}; +const DEMO_GENESIS_PATHS: GenesisPaths<&str> = GenesisPaths { + bank_genesis_path: "../test-data/genesis/demo-tests/bank.json", + sequencer_genesis_path: "../test-data/genesis/demo-tests/sequencer_registry.json", + value_setter_genesis_path: "../test-data/genesis/demo-tests/value_setter.json", + accounts_genesis_path: "../test-data/genesis/demo-tests/accounts.json", + chain_state_genesis_path: "../test-data/genesis/demo-tests/chain_state.json", + #[cfg(feature = "experimental")] + evm_genesis_path: "../test-data/genesis/demo-tests/evm.json", +}; + +const TEST_GENESIS_PATHS: GenesisPaths<&str> = GenesisPaths { + bank_genesis_path: "../test-data/genesis/integration-tests/bank.json", + sequencer_genesis_path: "../test-data/genesis/integration-tests/sequencer_registry.json", + value_setter_genesis_path: "../test-data/genesis/integration-tests/value_setter.json", + accounts_genesis_path: "../test-data/genesis/integration-tests/accounts.json", + chain_state_genesis_path: "../test-data/genesis/integration-tests/chain_state.json", + #[cfg(feature = "experimental")] + evm_genesis_path: "../test-data/genesis/integration-tests/evm.json", +}; + #[cfg(test)] mod test_rpc; @@ -38,12 +59,20 @@ async fn main() -> Result<(), anyhow::Error> { match args.da_layer.as_str() { "mock" => { - let rollup = new_rollup_with_mock_da::>(rollup_config_path, None)?; + let rollup = new_rollup_with_mock_da::, _>( + rollup_config_path, + None, + &TEST_GENESIS_PATHS, + )?; rollup.run().await } "celestia" => { - let rollup = - new_rollup_with_celestia_da::>(rollup_config_path, None).await?; + let rollup = new_rollup_with_celestia_da::, _>( + rollup_config_path, + None, + &DEMO_GENESIS_PATHS, + ) + .await?; rollup.run().await } da => panic!("DA Layer not supported: {}", da), diff --git a/examples/demo-rollup/src/rollup.rs b/examples/demo-rollup/src/rollup.rs index bcad9f83b..c5c3204d9 100644 --- a/examples/demo-rollup/src/rollup.rs +++ b/examples/demo-rollup/src/rollup.rs @@ -1,4 +1,5 @@ use std::net::SocketAddr; +use std::path::Path; use std::str::FromStr; use anyhow::Context; @@ -6,7 +7,7 @@ use const_rollup_config::SEQUENCER_DA_ADDRESS; #[cfg(feature = "experimental")] use demo_stf::app::DefaultPrivateKey; use demo_stf::app::{create_zk_app_template, App, DefaultContext}; -use demo_stf::genesis_config::get_genesis_config; +use demo_stf::genesis_config::{get_genesis_config, GenesisPaths}; use demo_stf::runtime::{get_rpc_methods, GenesisConfig, Runtime}; use demo_stf::AppVerifier; #[cfg(feature = "experimental")] @@ -21,7 +22,9 @@ use sov_db::ledger_db::LedgerDB; use sov_ethereum::experimental::EthRpcConfig; use sov_modules_api::default_context::ZkDefaultContext; use sov_modules_stf_template::AppTemplate; -use sov_rollup_interface::mocks::{MockAddress, MockDaConfig, MockDaService}; +use sov_rollup_interface::mocks::{ + MockAddress, MockDaConfig, MockDaService, MOCK_SEQUENCER_DA_ADDRESS, +}; use sov_rollup_interface::services::da::DaService; use sov_rollup_interface::zk::ZkvmHost; use sov_stf_runner::{ @@ -86,9 +89,10 @@ pub enum DemoProverConfig { } /// Creates celestia based rollup. -pub async fn new_rollup_with_celestia_da( +pub async fn new_rollup_with_celestia_da>( rollup_config_path: &str, prover: Option<(Vm, DemoProverConfig)>, + genesis_paths: &GenesisPaths

, ) -> Result, anyhow::Error> { debug!( "Starting demo celestia rollup with config {}", @@ -114,6 +118,7 @@ pub async fn new_rollup_with_celestia_da( let eth_signer = read_eth_tx_signers(); let genesis_config = demo_stf::genesis_config::get_genesis_config( sequencer_da_address, + genesis_paths, #[cfg(feature = "experimental")] eth_signer.signers(), ); @@ -144,25 +149,27 @@ pub async fn new_rollup_with_celestia_da( } /// Creates MockDa based rollup. -pub fn new_rollup_with_mock_da( +pub fn new_rollup_with_mock_da>( rollup_config_path: &str, prover: Option<(Vm, DemoProverConfig)>, + genesis_paths: &GenesisPaths

, ) -> Result, anyhow::Error> { debug!("Starting mock rollup with config {}", rollup_config_path); let rollup_config: RollupConfig = from_toml_path(rollup_config_path).context("Failed to read rollup configuration")?; - new_rollup_with_mock_da_from_config(rollup_config, prover) + new_rollup_with_mock_da_from_config(rollup_config, prover, genesis_paths) } /// Creates MockDa based rollup. -pub fn new_rollup_with_mock_da_from_config( +pub fn new_rollup_with_mock_da_from_config>( rollup_config: RollupConfig, prover: Option<(Vm, DemoProverConfig)>, + genesis_paths: &GenesisPaths

, ) -> Result, anyhow::Error> { let ledger_db = initialize_ledger(&rollup_config.storage.path); - let sequencer_da_address = MockAddress::from([0u8; 32]); + let sequencer_da_address = MockAddress::from(MOCK_SEQUENCER_DA_ADDRESS); let da_service = MockDaService::new(sequencer_da_address); #[cfg(feature = "experimental")] @@ -170,6 +177,7 @@ pub fn new_rollup_with_mock_da_from_config( let app = App::new(rollup_config.storage); let genesis_config = get_genesis_config( sequencer_da_address, + genesis_paths, #[cfg(feature = "experimental")] eth_signer.signers(), ); diff --git a/examples/demo-rollup/tests/bank/mod.rs b/examples/demo-rollup/tests/bank/mod.rs index 8db0f08e5..0d13ffc08 100644 --- a/examples/demo-rollup/tests/bank/mod.rs +++ b/examples/demo-rollup/tests/bank/mod.rs @@ -2,6 +2,7 @@ use std::net::SocketAddr; use borsh::BorshSerialize; use demo_stf::app::DefaultPrivateKey; +use demo_stf::genesis_config::GenesisPaths; use demo_stf::runtime::RuntimeCall; use jsonrpsee::core::client::{Subscription, SubscriptionClientT}; use jsonrpsee::rpc_params; @@ -18,6 +19,16 @@ use super::test_helpers::start_rollup; const TOKEN_SALT: u64 = 0; const TOKEN_NAME: &str = "test_token"; +const TEST_GENESIS_PATHS: GenesisPaths<&str> = GenesisPaths { + bank_genesis_path: "../test-data/genesis/integration-tests/bank.json", + sequencer_genesis_path: "../test-data/genesis/integration-tests/sequencer_registry.json", + value_setter_genesis_path: "../test-data/genesis/integration-tests/value_setter.json", + accounts_genesis_path: "../test-data/genesis/integration-tests/accounts.json", + chain_state_genesis_path: "../test-data/genesis/integration-tests/chain_state.json", + #[cfg(feature = "experimental")] + evm_genesis_path: "../test-data/genesis/integration-tests/evm.json", +}; + async fn send_test_create_token_tx(rpc_address: SocketAddr) -> Result<(), anyhow::Error> { let key = DefaultPrivateKey::generate(); let user_address: ::Address = key.to_address(); @@ -74,7 +85,7 @@ async fn bank_tx_tests() -> Result<(), anyhow::Error> { let config = DemoProverConfig::Execute; let rollup_task = tokio::spawn(async { - start_rollup(port_tx, Some((prover, config))).await; + start_rollup(port_tx, Some((prover, config)), &TEST_GENESIS_PATHS).await; }); // Wait for rollup task to start: diff --git a/examples/demo-rollup/tests/evm/mod.rs b/examples/demo-rollup/tests/evm/mod.rs index b9942c14b..4c503f17c 100644 --- a/examples/demo-rollup/tests/evm/mod.rs +++ b/examples/demo-rollup/tests/evm/mod.rs @@ -1,6 +1,7 @@ use std::net::SocketAddr; use std::str::FromStr; +use demo_stf::genesis_config::GenesisPaths; use ethereum_types::H160; use ethers_core::abi::Address; use ethers_core::k256::ecdsa::SigningKey; @@ -22,6 +23,16 @@ use super::test_helpers::start_rollup; const MAX_FEE_PER_GAS: u64 = 100000001; +const TEST_GENESIS_PATHS: GenesisPaths<&str> = GenesisPaths { + bank_genesis_path: "../test-data/genesis/integration-tests/bank.json", + sequencer_genesis_path: "../test-data/genesis/integration-tests/sequencer_registry.json", + value_setter_genesis_path: "../test-data/genesis/integration-tests/value_setter.json", + accounts_genesis_path: "../test-data/genesis/integration-tests/accounts.json", + chain_state_genesis_path: "../test-data/genesis/integration-tests/chain_state.json", + #[cfg(feature = "experimental")] + evm_genesis_path: "../test-data/genesis/integration-tests/evm.json", +}; + struct TestClient { chain_id: u64, from_addr: Address, @@ -478,7 +489,7 @@ async fn evm_tx_tests() -> Result<(), anyhow::Error> { let rollup_task = tokio::spawn(async { // Don't provide a prover since the EVM is not currently provable - start_rollup::>(port_tx, None).await; + start_rollup::, _>(port_tx, None, &TEST_GENESIS_PATHS).await; }); // Wait for rollup task to start: diff --git a/examples/demo-rollup/tests/test_helpers.rs b/examples/demo-rollup/tests/test_helpers.rs index 531267a88..4b769fc01 100644 --- a/examples/demo-rollup/tests/test_helpers.rs +++ b/examples/demo-rollup/tests/test_helpers.rs @@ -1,14 +1,17 @@ use std::net::SocketAddr; +use std::path::Path; +use demo_stf::genesis_config::GenesisPaths; use sov_demo_rollup::{new_rollup_with_mock_da_from_config, DemoProverConfig}; use sov_rollup_interface::mocks::MockDaConfig; use sov_rollup_interface::zk::ZkvmHost; use sov_stf_runner::{RollupConfig, RpcConfig, RunnerConfig, StorageConfig}; use tokio::sync::oneshot; -pub async fn start_rollup( +pub async fn start_rollup>( rpc_reporting_channel: oneshot::Sender, prover: Option<(Vm, DemoProverConfig)>, + genesis_paths: &GenesisPaths

, ) { let temp_dir = tempfile::tempdir().unwrap(); let temp_path = temp_dir.path(); @@ -27,8 +30,8 @@ pub async fn start_rollup( da: MockDaConfig {}, }; - let rollup = - new_rollup_with_mock_da_from_config(rollup_config, prover).expect("Rollup config is valid"); + let rollup = new_rollup_with_mock_da_from_config(rollup_config, prover, genesis_paths) + .expect("Rollup config is valid"); rollup .run_and_report_rpc_port(Some(rpc_reporting_channel)) .await diff --git a/examples/demo-stf/src/genesis_config.rs b/examples/demo-stf/src/genesis_config.rs index 01551e9b6..b6f997220 100644 --- a/examples/demo-stf/src/genesis_config.rs +++ b/examples/demo-stf/src/genesis_config.rs @@ -1,7 +1,7 @@ use std::convert::AsRef; use std::path::Path; -use anyhow::Context as AnyhowContext; +use anyhow::{bail, Context as AnyhowContext}; use serde::de::DeserializeOwned; use sov_accounts::AccountConfig; use sov_bank::BankConfig; @@ -13,6 +13,7 @@ pub use sov_modules_api::default_context::DefaultContext; use sov_modules_api::Context; use sov_nft_module::NonFungibleTokenConfig; use sov_rollup_interface::da::DaSpec; +use sov_sequencer_registry::SequencerConfig; pub use sov_state::config::Config as StorageConfig; use sov_value_setter::ValueSetterConfig; @@ -22,6 +23,17 @@ use crate::runtime::GenesisConfig; pub const LOCKED_AMOUNT: u64 = 50; pub const DEMO_TOKEN_NAME: &str = "sov-demo-token"; +/// Paths pointing to genesis files. +pub struct GenesisPaths> { + pub bank_genesis_path: P, + pub sequencer_genesis_path: P, + pub value_setter_genesis_path: P, + pub accounts_genesis_path: P, + pub chain_state_genesis_path: P, + #[cfg(feature = "experimental")] + pub evm_genesis_path: P, +} + /// Configure our rollup with a centralized sequencer using the SEQUENCER_DA_ADDRESS /// address constant. Since the centralize sequencer's address is consensus critical, /// it has to be hardcoded as a constant, rather than read from the config at runtime. @@ -32,63 +44,62 @@ pub const DEMO_TOKEN_NAME: &str = "sov-demo-token"; /// ```rust,no_run /// const SEQUENCER_DA_ADDRESS: &str = "celestia1qp09ysygcx6npted5yc0au6k9lner05yvs9208"; /// ``` -pub fn get_genesis_config( +pub fn get_genesis_config>( sequencer_da_address: Da::Address, + genesis_paths: &GenesisPaths

, #[cfg(feature = "experimental")] eth_signers: Vec, ) -> GenesisConfig { - let token_deployer: PrivateKeyAndAddress = read_private_key(); - create_genesis_config( - token_deployer.address.clone(), sequencer_da_address, + genesis_paths, #[cfg(feature = "experimental")] eth_signers, ) .expect("Unable to read genesis configuration") } -fn create_genesis_config( - sequencer_address: C::Address, - sequencer_da_address: Da::Address, +fn create_genesis_config>( + seq_da_address: Da::Address, + genesis_paths: &GenesisPaths

, #[cfg(feature = "experimental")] eth_signers: Vec, ) -> anyhow::Result> { - // This path will be injected as a parameter: #872 - let bank_genesis_path = "../test-data/genesis/bank.json"; - let bank_config: BankConfig = read_json_file(bank_genesis_path)?; - // This will be read from a file: #872 - let token_address = sov_bank::get_genesis_token_address::( - &bank_config.tokens[0].token_name, - bank_config.tokens[0].salt, - ); - - // This will be read from a file: #872 - let sequencer_registry_config = sov_sequencer_registry::SequencerConfig { - seq_rollup_address: sequencer_address, - seq_da_address: sequencer_da_address, - coins_to_lock: sov_bank::Coins { - amount: LOCKED_AMOUNT, - token_address, - }, - is_preferred_sequencer: true, - }; + let bank_config: BankConfig = read_json_file(&genesis_paths.bank_genesis_path)?; + + let mut sequencer_registry_config: SequencerConfig = + read_json_file(&genesis_paths.sequencer_genesis_path)?; + + // The `seq_da_address` is overridden with the value from rollup binary. + sequencer_registry_config.seq_da_address = seq_da_address; + + // Sanity check: `token_address` in `sequencer_registry_config` match `token_address` from the bank module. + { + let token_address = &sov_bank::get_genesis_token_address::( + &bank_config.tokens[0].token_name, + bank_config.tokens[0].salt, + ); + + let coins_token_addr = &sequencer_registry_config.coins_to_lock.token_address; + if coins_token_addr != token_address { + bail!( + "Wrong token address in `sequencer_registry_config` expected {} but found {}", + token_address, + coins_token_addr + ) + } + } - // This path will be injected as a parameter: #872 - let value_setter_genesis_path = "../test-data/genesis/value_setter.json"; - let value_setter_config: ValueSetterConfig = read_json_file(value_setter_genesis_path)?; + let value_setter_config: ValueSetterConfig = + read_json_file(&genesis_paths.value_setter_genesis_path)?; - let accounts_genesis_path = "../test-data/genesis/accounts.json"; - let accounts_config: AccountConfig = read_json_file(accounts_genesis_path)?; + let accounts_config: AccountConfig = read_json_file(&genesis_paths.accounts_genesis_path)?; let nft_config: NonFungibleTokenConfig = NonFungibleTokenConfig {}; - let chain_state_path = "../test-data/genesis/chain_state.json"; - let chain_state_config: ChainStateConfig = read_json_file(chain_state_path)?; - - #[cfg(feature = "experimental")] - let evm_path = "../test-data/genesis/evm.json"; + let chain_state_config: ChainStateConfig = + read_json_file(&genesis_paths.chain_state_genesis_path)?; #[cfg(feature = "experimental")] - let evm_config = get_evm_config(evm_path, eth_signers)?; + let evm_config = get_evm_config(&genesis_paths.evm_genesis_path, eth_signers)?; Ok(GenesisConfig::new( bank_config, diff --git a/examples/demo-stf/src/tests/mod.rs b/examples/demo-stf/src/tests/mod.rs index cb4180984..df89b5e46 100644 --- a/examples/demo-stf/src/tests/mod.rs +++ b/examples/demo-stf/src/tests/mod.rs @@ -3,10 +3,10 @@ use std::path::Path; use sov_modules_api::default_context::DefaultContext; use sov_modules_api::DaSpec; use sov_modules_stf_template::AppTemplate; -use sov_rollup_interface::mocks::MockDaSpec; +use sov_rollup_interface::mocks::{MockDaSpec, MOCK_SEQUENCER_DA_ADDRESS}; use sov_state::ProverStorage; -use crate::genesis_config::get_genesis_config; +use crate::genesis_config::{get_genesis_config, GenesisPaths}; use crate::runtime::{GenesisConfig, Runtime}; mod da_simulation; @@ -15,7 +15,15 @@ mod tx_revert_tests; pub(crate) type C = DefaultContext; pub(crate) type Da = MockDaSpec; -pub(crate) const TEST_SEQUENCER_DA_ADDRESS: [u8; 32] = [1; 32]; +const TEST_GENESIS_PATHS: GenesisPaths<&str> = GenesisPaths { + bank_genesis_path: "../test-data/genesis/integration-tests/bank.json", + sequencer_genesis_path: "../test-data/genesis/integration-tests/sequencer_registry.json", + value_setter_genesis_path: "../test-data/genesis/integration-tests/value_setter.json", + accounts_genesis_path: "../test-data/genesis/integration-tests/accounts.json", + chain_state_genesis_path: "../test-data/genesis/integration-tests/chain_state.json", + #[cfg(feature = "experimental")] + evm_genesis_path: "../test-data/genesis/integration-tests/evm.json", +}; pub(crate) fn create_new_app_template_for_tests( path: impl AsRef, @@ -31,8 +39,9 @@ pub(crate) fn create_new_app_template_for_tests( } pub(crate) fn get_genesis_config_for_tests() -> GenesisConfig { - get_genesis_config::( - Da::Address::try_from(&TEST_SEQUENCER_DA_ADDRESS).unwrap(), + get_genesis_config::( + Da::Address::try_from(&MOCK_SEQUENCER_DA_ADDRESS).unwrap(), + &TEST_GENESIS_PATHS, #[cfg(feature = "experimental")] Vec::default(), ) diff --git a/examples/demo-stf/src/tests/stf_tests.rs b/examples/demo-stf/src/tests/stf_tests.rs index 0cead9ef4..70077abab 100644 --- a/examples/demo-stf/src/tests/stf_tests.rs +++ b/examples/demo-stf/src/tests/stf_tests.rs @@ -7,17 +7,14 @@ pub mod test { use sov_modules_api::default_signature::private_key::DefaultPrivateKey; use sov_modules_api::{PrivateKey, WorkingSet}; use sov_modules_stf_template::{Batch, SequencerOutcome}; - use sov_rollup_interface::mocks::{MockBlock, MockDaSpec}; + use sov_rollup_interface::mocks::{MockBlock, MockDaSpec, MOCK_SEQUENCER_DA_ADDRESS}; use sov_rollup_interface::stf::StateTransitionFunction; use sov_state::ProverStorage; use crate::genesis_config::read_private_key; use crate::runtime::Runtime; use crate::tests::da_simulation::simulate_da; - use crate::tests::{ - create_new_app_template_for_tests, get_genesis_config_for_tests, C, - TEST_SEQUENCER_DA_ADDRESS, - }; + use crate::tests::{create_new_app_template_for_tests, get_genesis_config_for_tests, C}; #[test] fn test_demo_values_in_db() { @@ -32,7 +29,7 @@ pub mod test { let priv_key = read_private_key::().private_key; let txs = simulate_da(priv_key); - let blob = new_test_blob_from_batch(Batch { txs }, &TEST_SEQUENCER_DA_ADDRESS, [0; 32]); + let blob = new_test_blob_from_batch(Batch { txs }, &MOCK_SEQUENCER_DA_ADDRESS, [0; 32]); let mut blobs = [blob]; @@ -90,7 +87,7 @@ pub mod test { let private_key = read_private_key::().private_key; let txs = simulate_da(private_key); - let blob = new_test_blob_from_batch(Batch { txs }, &TEST_SEQUENCER_DA_ADDRESS, [0; 32]); + let blob = new_test_blob_from_batch(Batch { txs }, &MOCK_SEQUENCER_DA_ADDRESS, [0; 32]); let mut blobs = [blob]; let data = MockBlock::default(); @@ -141,7 +138,7 @@ pub mod test { let genesis_root = demo.init_chain(config); let txs = simulate_da(value_setter_admin_private_key); - let blob = new_test_blob_from_batch(Batch { txs }, &TEST_SEQUENCER_DA_ADDRESS, [0; 32]); + let blob = new_test_blob_from_batch(Batch { txs }, &MOCK_SEQUENCER_DA_ADDRESS, [0; 32]); let mut blobs = [blob]; let data = MockBlock::default(); diff --git a/examples/demo-stf/src/tests/tx_revert_tests.rs b/examples/demo-stf/src/tests/tx_revert_tests.rs index d0934abe1..119116015 100644 --- a/examples/demo-stf/src/tests/tx_revert_tests.rs +++ b/examples/demo-stf/src/tests/tx_revert_tests.rs @@ -5,7 +5,7 @@ use sov_modules_api::default_context::DefaultContext; use sov_modules_api::{PrivateKey, WorkingSet}; use sov_modules_stf_template::{Batch, SequencerOutcome, SlashingReason, TxEffect}; use sov_rollup_interface::da::BlobReaderTrait; -use sov_rollup_interface::mocks::{MockAddress, MockBlock, MockDaSpec}; +use sov_rollup_interface::mocks::{MockAddress, MockBlock, MockDaSpec, MOCK_SEQUENCER_DA_ADDRESS}; use sov_rollup_interface::stf::StateTransitionFunction; use sov_state::ProverStorage; @@ -15,7 +15,6 @@ use crate::tests::da_simulation::{ simulate_da_with_bad_nonce, simulate_da_with_bad_serialization, simulate_da_with_bad_sig, simulate_da_with_revert_msg, }; -use crate::tests::TEST_SEQUENCER_DA_ADDRESS; // Assume there was proper address and we converted it to bytes already. const SEQUENCER_DA_ADDRESS: [u8; 32] = [1; 32]; @@ -35,7 +34,7 @@ fn test_tx_revert() { let genesis_root = demo.init_chain(config); let txs = simulate_da_with_revert_msg(); - let blob = new_test_blob_from_batch(Batch { txs }, &TEST_SEQUENCER_DA_ADDRESS, [0; 32]); + let blob = new_test_blob_from_batch(Batch { txs }, &MOCK_SEQUENCER_DA_ADDRESS, [0; 32]); let mut blobs = [blob]; let data = MockBlock::default(); @@ -85,7 +84,7 @@ fn test_tx_revert() { let resp = runtime .sequencer_registry .sequencer_address( - MockAddress::from(TEST_SEQUENCER_DA_ADDRESS), + MockAddress::from(MOCK_SEQUENCER_DA_ADDRESS), &mut working_set, ) .unwrap(); @@ -107,7 +106,7 @@ fn test_nonce_incremented_on_revert() { let genesis_root = demo.init_chain(config); let txs = simulate_da_with_revert_msg(); - let blob = new_test_blob_from_batch(Batch { txs }, &TEST_SEQUENCER_DA_ADDRESS, [0; 32]); + let blob = new_test_blob_from_batch(Batch { txs }, &MOCK_SEQUENCER_DA_ADDRESS, [0; 32]); let mut blobs = [blob]; let data = MockBlock::default(); @@ -175,7 +174,7 @@ fn test_tx_bad_sig() { let txs = simulate_da_with_bad_sig(); - let blob = new_test_blob_from_batch(Batch { txs }, &TEST_SEQUENCER_DA_ADDRESS, [0; 32]); + let blob = new_test_blob_from_batch(Batch { txs }, &MOCK_SEQUENCER_DA_ADDRESS, [0; 32]); let blob_sender = blob.sender(); let mut blobs = [blob]; @@ -220,7 +219,7 @@ fn test_tx_bad_nonce() { let txs = simulate_da_with_bad_nonce(); - let blob = new_test_blob_from_batch(Batch { txs }, &TEST_SEQUENCER_DA_ADDRESS, [0; 32]); + let blob = new_test_blob_from_batch(Batch { txs }, &MOCK_SEQUENCER_DA_ADDRESS, [0; 32]); let mut blobs = [blob]; let data = MockBlock::default(); @@ -286,7 +285,7 @@ fn test_tx_bad_serialization() { let mut demo = create_new_app_template_for_tests(path); let txs = simulate_da_with_bad_serialization(); - let blob = new_test_blob_from_batch(Batch { txs }, &TEST_SEQUENCER_DA_ADDRESS, [0; 32]); + let blob = new_test_blob_from_batch(Batch { txs }, &MOCK_SEQUENCER_DA_ADDRESS, [0; 32]); let blob_sender = blob.sender(); let mut blobs = [blob]; diff --git a/examples/test-data/genesis/accounts.json b/examples/test-data/genesis/demo-tests/accounts.json similarity index 100% rename from examples/test-data/genesis/accounts.json rename to examples/test-data/genesis/demo-tests/accounts.json diff --git a/examples/test-data/genesis/bank.json b/examples/test-data/genesis/demo-tests/bank.json similarity index 100% rename from examples/test-data/genesis/bank.json rename to examples/test-data/genesis/demo-tests/bank.json diff --git a/examples/test-data/genesis/chain_state.json b/examples/test-data/genesis/demo-tests/chain_state.json similarity index 100% rename from examples/test-data/genesis/chain_state.json rename to examples/test-data/genesis/demo-tests/chain_state.json diff --git a/examples/test-data/genesis/evm.json b/examples/test-data/genesis/demo-tests/evm.json similarity index 100% rename from examples/test-data/genesis/evm.json rename to examples/test-data/genesis/demo-tests/evm.json diff --git a/examples/test-data/genesis/demo-tests/sequencer_registry.json b/examples/test-data/genesis/demo-tests/sequencer_registry.json new file mode 100644 index 000000000..690301648 --- /dev/null +++ b/examples/test-data/genesis/demo-tests/sequencer_registry.json @@ -0,0 +1,9 @@ +{ + "seq_rollup_address":"sov1l6n2cku82yfqld30lanm2nfw43n2auc8clw7r5u5m6s7p8jrm4zqrr8r94", + "seq_da_address":"celestia1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzf30as", + "coins_to_lock":{ + "amount":50, + "token_address":"sov1zsnx7n2wjvtkr0ttscfgt06pjca3v2e6stxeu49qwynavmk7a8xqlxkkjp" + }, + "is_preferred_sequencer":true +} \ No newline at end of file diff --git a/examples/test-data/genesis/value_setter.json b/examples/test-data/genesis/demo-tests/value_setter.json similarity index 100% rename from examples/test-data/genesis/value_setter.json rename to examples/test-data/genesis/demo-tests/value_setter.json diff --git a/examples/test-data/genesis/integration-tests/accounts.json b/examples/test-data/genesis/integration-tests/accounts.json new file mode 100644 index 000000000..3a134d04d --- /dev/null +++ b/examples/test-data/genesis/integration-tests/accounts.json @@ -0,0 +1,3 @@ +{ + "pub_keys":[] +} \ No newline at end of file diff --git a/examples/test-data/genesis/integration-tests/bank.json b/examples/test-data/genesis/integration-tests/bank.json new file mode 100644 index 000000000..35b065659 --- /dev/null +++ b/examples/test-data/genesis/integration-tests/bank.json @@ -0,0 +1,10 @@ +{ + "tokens":[ + { + "token_name":"sov-demo-token", + "address_and_balances":[["sov1l6n2cku82yfqld30lanm2nfw43n2auc8clw7r5u5m6s7p8jrm4zqrr8r94",100000000]], + "authorized_minters":["sov1l6n2cku82yfqld30lanm2nfw43n2auc8clw7r5u5m6s7p8jrm4zqrr8r94"] + ,"salt":0 + } + ] +} \ No newline at end of file diff --git a/examples/test-data/genesis/integration-tests/chain_state.json b/examples/test-data/genesis/integration-tests/chain_state.json new file mode 100644 index 000000000..2e7f97e93 --- /dev/null +++ b/examples/test-data/genesis/integration-tests/chain_state.json @@ -0,0 +1,7 @@ +{ + "initial_slot_height":0, + "current_time":{ + "secs":0, + "nanos":0 + } +} \ No newline at end of file diff --git a/examples/test-data/genesis/integration-tests/evm.json b/examples/test-data/genesis/integration-tests/evm.json new file mode 100644 index 000000000..2f8db7a64 --- /dev/null +++ b/examples/test-data/genesis/integration-tests/evm.json @@ -0,0 +1,24 @@ +{ + "data":[ + { + "address":"0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266", + "balance":"0xffffffffffffffff", + "code_hash":"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", + "code":"0x", + "nonce":0 + }], + "chain_id":1, + "limit_contract_code_size":null, + "spec":{ + "0":"SHANGHAI" + }, + "coinbase":"0x0000000000000000000000000000000000000000", + "starting_base_fee":7, + "block_gas_limit":30000000, + "genesis_timestamp":0, + "block_timestamp_delta":1, + "base_fee_params":{ + "max_change_denominator":8, + "elasticity_multiplier":2 + } +} \ No newline at end of file diff --git a/examples/test-data/genesis/integration-tests/sequencer_registry.json b/examples/test-data/genesis/integration-tests/sequencer_registry.json new file mode 100644 index 000000000..fdee012ec --- /dev/null +++ b/examples/test-data/genesis/integration-tests/sequencer_registry.json @@ -0,0 +1,9 @@ +{ + "seq_rollup_address":"sov1l6n2cku82yfqld30lanm2nfw43n2auc8clw7r5u5m6s7p8jrm4zqrr8r94", + "seq_da_address":"0000000000000000000000000000000000000000000000000000000000000000", + "coins_to_lock":{ + "amount":50, + "token_address":"sov1zsnx7n2wjvtkr0ttscfgt06pjca3v2e6stxeu49qwynavmk7a8xqlxkkjp" + }, + "is_preferred_sequencer":true +} \ No newline at end of file diff --git a/examples/test-data/genesis/integration-tests/value_setter.json b/examples/test-data/genesis/integration-tests/value_setter.json new file mode 100644 index 000000000..35c01dd23 --- /dev/null +++ b/examples/test-data/genesis/integration-tests/value_setter.json @@ -0,0 +1,3 @@ +{ + "admin":"sov1l6n2cku82yfqld30lanm2nfw43n2auc8clw7r5u5m6s7p8jrm4zqrr8r94" +} \ No newline at end of file diff --git a/module-system/module-implementations/sov-bank/src/token.rs b/module-system/module-implementations/sov-bank/src/token.rs index 8ad62943a..ea17c6c84 100644 --- a/module-system/module-implementations/sov-bank/src/token.rs +++ b/module-system/module-implementations/sov-bank/src/token.rs @@ -27,7 +27,14 @@ pub type Amount = u64; schemars(bound = "C::Address: ::schemars::JsonSchema", rename = "Coins") )] #[derive( - borsh::BorshDeserialize, borsh::BorshSerialize, Debug, PartialEq, Clone, Serialize, Deserialize, + borsh::BorshDeserialize, + borsh::BorshSerialize, + Debug, + Clone, + Serialize, + Deserialize, + PartialEq, + Eq, )] pub struct Coins { /// An `amount` of coins stored. 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 a74447e31..40b382213 100644 --- a/module-system/module-implementations/sov-sequencer-registry/src/lib.rs +++ b/module-system/module-implementations/sov-sequencer-registry/src/lib.rs @@ -12,7 +12,8 @@ mod genesis; mod hooks; #[cfg(feature = "native")] mod query; - +#[cfg(test)] +mod tests; pub use call::CallMessage; #[cfg(feature = "native")] pub use query::*; @@ -26,7 +27,8 @@ use sov_state::codec::BcsCodec; /// [`Module::genesis`](sov_modules_api::Module::genesis). /// // TODO: Should we allow multiple sequencers in genesis? -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)] +#[serde(bound = "C::Address: serde::Serialize + serde::de::DeserializeOwned")] pub struct SequencerConfig { /// The rollup address of the sequencer. pub seq_rollup_address: C::Address, diff --git a/module-system/module-implementations/sov-sequencer-registry/src/tests.rs b/module-system/module-implementations/sov-sequencer-registry/src/tests.rs new file mode 100644 index 000000000..31d3e9a35 --- /dev/null +++ b/module-system/module-implementations/sov-sequencer-registry/src/tests.rs @@ -0,0 +1,51 @@ +use std::str::FromStr; + +use sov_bank::Coins; +use sov_modules_api::default_context::DefaultContext; +use sov_modules_api::{AddressBech32, Spec}; +use sov_rollup_interface::mocks::{MockAddress, MockDaSpec}; + +use crate::SequencerConfig; + +#[test] +fn test_config_serialization() { + let seq_rollup_address: ::Address = + AddressBech32::from_str("sov1l6n2cku82yfqld30lanm2nfw43n2auc8clw7r5u5m6s7p8jrm4zqrr8r94") + .unwrap() + .into(); + + let token_address: ::Address = + AddressBech32::from_str("sov1zsnx7n2wjvtkr0ttscfgt06pjca3v2e6stxeu49qwynavmk7a8xqlxkkjp") + .unwrap() + .into(); + + let coins = Coins:: { + amount: 50, + token_address, + }; + + let seq_da_addreess = + MockAddress::from_str("0000000000000000000000000000000000000000000000000000000000000000") + .unwrap(); + + let config = SequencerConfig:: { + seq_rollup_address, + seq_da_address: seq_da_addreess, + coins_to_lock: coins, + is_preferred_sequencer: true, + }; + + let data = r#"{ + "seq_rollup_address":"sov1l6n2cku82yfqld30lanm2nfw43n2auc8clw7r5u5m6s7p8jrm4zqrr8r94", + "seq_da_address":"0000000000000000000000000000000000000000000000000000000000000000", + "coins_to_lock":{ + "amount":50, + "token_address":"sov1zsnx7n2wjvtkr0ttscfgt06pjca3v2e6stxeu49qwynavmk7a8xqlxkkjp" + }, + "is_preferred_sequencer":true + }"#; + + let parsed_config: SequencerConfig = + serde_json::from_str(data).unwrap(); + assert_eq!(config, parsed_config) +} diff --git a/rollup-interface/src/state_machine/mocks/da.rs b/rollup-interface/src/state_machine/mocks/da.rs index e216c63f5..bc53b88df 100644 --- a/rollup-interface/src/state_machine/mocks/da.rs +++ b/rollup-interface/src/state_machine/mocks/da.rs @@ -1,4 +1,5 @@ use std::fmt::Display; +use std::str::FromStr; #[cfg(feature = "native")] use std::sync::Arc; @@ -19,26 +20,47 @@ use crate::{BasicAddress, RollupAddress}; const JAN_1_2023: i64 = 1672531200; +/// Sequencer DA address used in tests. +pub const MOCK_SEQUENCER_DA_ADDRESS: [u8; 32] = [0u8; 32]; + /// A mock address type used for testing. Internally, this type is standard 32 byte array. #[derive( - Debug, - PartialEq, - Clone, - Eq, - Copy, - serde::Serialize, - serde::Deserialize, - Hash, - Default, - borsh::BorshDeserialize, - borsh::BorshSerialize, + Debug, PartialEq, Clone, Eq, Copy, Hash, Default, borsh::BorshDeserialize, borsh::BorshSerialize, )] pub struct MockAddress { /// Underlying mock address. pub addr: [u8; 32], } -impl core::str::FromStr for MockAddress { +impl serde::Serialize for MockAddress { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + if serializer.is_human_readable() { + serde::Serialize::serialize(&hex::encode(self.addr), serializer) + } else { + serde::Serialize::serialize(&self.addr, serializer) + } + } +} + +impl<'de> serde::Deserialize<'de> for MockAddress { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + if deserializer.is_human_readable() { + let hex_addr: String = serde::Deserialize::deserialize(deserializer)?; + Ok(MockAddress::from_str(&hex_addr).map_err(serde::de::Error::custom)?) + } else { + let addr = <[u8; 32] as serde::Deserialize>::deserialize(deserializer)?; + Ok(MockAddress { addr }) + } + } +} + +impl FromStr for MockAddress { type Err = anyhow::Error; fn from_str(s: &str) -> Result { diff --git a/rollup-interface/src/state_machine/mocks/mod.rs b/rollup-interface/src/state_machine/mocks/mod.rs index a489035ea..3afbfd835 100644 --- a/rollup-interface/src/state_machine/mocks/mod.rs +++ b/rollup-interface/src/state_machine/mocks/mod.rs @@ -8,7 +8,7 @@ mod zk_vm; pub use da::MockDaService; pub use da::{ MockAddress, MockBlob, MockBlock, MockBlockHeader, MockDaConfig, MockDaSpec, MockDaVerifier, - MockHash, + MockHash, MOCK_SEQUENCER_DA_ADDRESS, }; pub use validity_condition::{MockValidityCond, MockValidityCondChecker}; pub use zk_vm::{MockCodeCommitment, MockProof, MockZkvm};