Skip to content

Commit

Permalink
Initialize genesis state form genesis files. (#972)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
bkolad authored Oct 4, 2023
1 parent 306dc1f commit 7a05e1f
Show file tree
Hide file tree
Showing 33 changed files with 445 additions and 111 deletions.
63 changes: 59 additions & 4 deletions adapters/celestia/src/verifier/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,11 +14,41 @@ const VARIANT: bech32::Variant = bech32::Variant::Bech32;
/// <https://github.com/celestiaorg/celestia-specs/blob/e59efd63a2165866584833e91e1cb8a6ed8c8203/src/specs/data_structures.md#address>
/// 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: <https://github.com/Sovereign-Labs/sovereign-sdk/issues/646>
#[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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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<D>(deserializer: D) -> Result<Self, D::Error>
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()
Expand Down Expand Up @@ -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::<CelestiaAddress>(&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::<CelestiaAddress>(&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::<u8>(), 0..100)) {
Expand Down
6 changes: 3 additions & 3 deletions examples/demo-prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -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"]
13 changes: 12 additions & 1 deletion examples/demo-prover/benches/prover_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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(),
);
Expand Down
20 changes: 18 additions & 2 deletions examples/demo-prover/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
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;
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
Expand Down Expand Up @@ -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(())
Expand Down
5 changes: 2 additions & 3 deletions examples/demo-rollup/benches/rng_xfers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]
Expand Down
22 changes: 18 additions & 4 deletions examples/demo-rollup/benches/rollup_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,9 +55,11 @@ fn rollup_bench(_bench: &mut Criterion) {
let demo_runner = App::<Risc0Verifier, RngDaSpec>::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(),
);
Expand Down
22 changes: 18 additions & 4 deletions examples/demo-rollup/benches/rollup_coarse_measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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,
Expand Down Expand Up @@ -100,9 +112,11 @@ async fn main() -> Result<(), anyhow::Error> {
let demo_runner = App::<Risc0Verifier, RngDaSpec>::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(),
);
Expand Down
35 changes: 32 additions & 3 deletions examples/demo-rollup/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -38,12 +59,20 @@ async fn main() -> Result<(), anyhow::Error> {

match args.da_layer.as_str() {
"mock" => {
let rollup = new_rollup_with_mock_da::<Risc0Host<'static>>(rollup_config_path, None)?;
let rollup = new_rollup_with_mock_da::<Risc0Host<'static>, _>(
rollup_config_path,
None,
&TEST_GENESIS_PATHS,
)?;
rollup.run().await
}
"celestia" => {
let rollup =
new_rollup_with_celestia_da::<Risc0Host<'static>>(rollup_config_path, None).await?;
let rollup = new_rollup_with_celestia_da::<Risc0Host<'static>, _>(
rollup_config_path,
None,
&DEMO_GENESIS_PATHS,
)
.await?;
rollup.run().await
}
da => panic!("DA Layer not supported: {}", da),
Expand Down
Loading

0 comments on commit 7a05e1f

Please sign in to comment.