Skip to content

Commit

Permalink
Unify genesis initialization (#888)
Browse files Browse the repository at this point in the history
* update demo-stf

* Update demo-rollup

* Update prover

* cargo fix

* update tests

* get_genesis_config

* Update tests

* move DEMO_SEQUENCER_DA_ADDRESS to tests

* Renaming

* Remove DemoGenesisConfig

* Add todos

* fox merge conflicts

* Fix benches

* fix cargo lint

* cargo fix
  • Loading branch information
bkolad authored Sep 19, 2023
1 parent 0259c64 commit 9b9b027
Show file tree
Hide file tree
Showing 12 changed files with 5,005 additions and 150 deletions.
4,872 changes: 4,872 additions & 0 deletions examples/demo-prover/Cargo.lock

Large diffs are not rendered by default.

13 changes: 4 additions & 9 deletions examples/demo-prover/benches/prover_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ use std::sync::{Arc, Mutex};

use anyhow::Context;
use const_rollup_config::{ROLLUP_NAMESPACE_RAW, SEQUENCER_DA_ADDRESS};
use demo_stf::app::{App, DefaultPrivateKey};
use demo_stf::genesis_config::create_demo_genesis_config;
use demo_stf::app::App;
use demo_stf::genesis_config::get_genesis_config;
use log4rs::config::{Appender, Config, Root};
use methods::ROLLUP_ELF;
use regex::Regex;
use sov_celestia_adapter::types::{FilteredCelestiaBlock, NamespaceId};
use sov_celestia_adapter::verifier::address::CelestiaAddress;
use sov_celestia_adapter::verifier::{CelestiaSpec, RollupParams};
use sov_celestia_adapter::CelestiaService;
use sov_modules_api::{PrivateKey, SlotData};
use sov_modules_api::SlotData;
use sov_risc0_adapter::host::Risc0Host;
use sov_rollup_interface::services::da::DaService;
use sov_rollup_interface::stf::StateTransitionFunction;
Expand Down Expand Up @@ -164,17 +164,12 @@ async fn main() -> Result<(), anyhow::Error> {
)
.await;

let sequencer_private_key = DefaultPrivateKey::generate();

let mut app: App<Risc0Host, CelestiaSpec> = App::new(rollup_config.storage.clone());

let sequencer_da_address = CelestiaAddress::from_str(SEQUENCER_DA_ADDRESS).unwrap();

let genesis_config = create_demo_genesis_config(
100000000,
sequencer_private_key.default_address(),
let genesis_config = get_genesis_config(
sequencer_da_address.as_ref().to_vec(),
&sequencer_private_key,
#[cfg(feature = "experimental")]
Default::default(),
);
Expand Down
10 changes: 2 additions & 8 deletions examples/demo-rollup/benches/rollup_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ use std::time::Duration;
use anyhow::Context;
use criterion::{criterion_group, criterion_main, Criterion};
use demo_stf::app::App;
use demo_stf::genesis_config::create_demo_genesis_config;
use demo_stf::genesis_config::get_genesis_config;
use rng_xfers::{RngDaService, RngDaSpec, SEQUENCER_DA_ADDRESS};
use sov_db::ledger_db::{LedgerDB, SlotCommit};
use sov_modules_api::default_signature::private_key::DefaultPrivateKey;
use sov_modules_api::PrivateKey;
use sov_risc0_adapter::host::Risc0Verifier;
use sov_rollup_interface::mocks::{MockAddress, MockBlock, MockBlockHeader};
use sov_rollup_interface::services::da::DaService;
Expand Down Expand Up @@ -45,13 +43,9 @@ fn rollup_bench(_bench: &mut Criterion) {
let demo_runner = App::<Risc0Verifier, RngDaSpec>::new(rollup_config.storage);

let mut demo = demo_runner.stf;
let sequencer_private_key = DefaultPrivateKey::generate();
let sequencer_da_address = MockAddress::from(SEQUENCER_DA_ADDRESS);
let demo_genesis_config = create_demo_genesis_config(
100000000,
sequencer_private_key.default_address(),
let demo_genesis_config = get_genesis_config(
sequencer_da_address.as_ref().to_vec(),
&sequencer_private_key,
#[cfg(feature = "experimental")]
Default::default(),
);
Expand Down
10 changes: 2 additions & 8 deletions examples/demo-rollup/benches/rollup_coarse_measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ use std::time::{Duration, Instant};

use anyhow::Context;
use demo_stf::app::App;
use demo_stf::genesis_config::create_demo_genesis_config;
use demo_stf::genesis_config::get_genesis_config;
use prometheus::{Histogram, HistogramOpts, Registry};
use rng_xfers::{RngDaService, RngDaSpec, SEQUENCER_DA_ADDRESS};
use sov_db::ledger_db::{LedgerDB, SlotCommit};
use sov_modules_api::default_signature::private_key::DefaultPrivateKey;
use sov_modules_api::PrivateKey;
use sov_risc0_adapter::host::Risc0Verifier;
use sov_rollup_interface::mocks::{MockAddress, MockBlock, MockBlockHeader};
use sov_rollup_interface::services::da::DaService;
Expand Down Expand Up @@ -102,13 +100,9 @@ async fn main() -> Result<(), anyhow::Error> {
let demo_runner = App::<Risc0Verifier, RngDaSpec>::new(rollup_config.storage);

let mut demo = demo_runner.stf;
let sequencer_private_key = DefaultPrivateKey::generate();
let sequencer_da_address = MockAddress::from(SEQUENCER_DA_ADDRESS);
let demo_genesis_config = create_demo_genesis_config(
100000000,
sequencer_private_key.default_address(),
let demo_genesis_config = get_genesis_config(
sequencer_da_address.as_ref().to_vec(),
&sequencer_private_key,
#[cfg(feature = "experimental")]
Default::default(),
);
Expand Down
47 changes: 0 additions & 47 deletions examples/demo-rollup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ pub mod register_rpc;
mod rollup;

use const_rollup_config::ROLLUP_NAMESPACE_RAW;
use demo_stf::genesis_config::create_demo_genesis_config;
use demo_stf::runtime::GenesisConfig;
pub use rollup::{
new_rollup_with_celestia_da, new_rollup_with_mock_da, new_rollup_with_mock_da_from_config,
DemoProverConfig, Rollup,
};
use sov_celestia_adapter::types::NamespaceId;
use sov_cli::wallet_state::PrivateKeyAndAddress;
use sov_db::ledger_db::LedgerDB;
use sov_modules_api::default_context::DefaultContext;
use sov_rollup_interface::da::{BlobReaderTrait, DaSpec};

/// 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
Expand All @@ -25,45 +20,3 @@ pub const ROLLUP_NAMESPACE: NamespaceId = NamespaceId(ROLLUP_NAMESPACE_RAW);
pub fn initialize_ledger(path: impl AsRef<std::path::Path>) -> LedgerDB {
LedgerDB::with_path(path).expect("Ledger DB failed to open")
}

/// 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.
///
/// If you want to customize the rollup to accept transactions from your own celestia
/// address, simply change the value of the SEQUENCER_DA_ADDRESS to your own address.
/// For example:
/// ```rust,no_run
/// const SEQUENCER_DA_ADDRESS: &str = "celestia1qp09ysygcx6npted5yc0au6k9lner05yvs9208";
/// ```
pub fn get_genesis_config<Da: DaSpec>(
sequencer_da_address: <<Da as DaSpec>::BlobTransaction as BlobReaderTrait>::Address,
#[cfg(feature = "experimental")] eth_accounts: Vec<reth_primitives::Address>,
) -> GenesisConfig<DefaultContext, Da> {
let token_deployer_data =
std::fs::read_to_string("../test-data/keys/token_deployer_private_key.json")
.expect("Unable to read file to string");

let token_deployer: PrivateKeyAndAddress<DefaultContext> =
serde_json::from_str(&token_deployer_data).unwrap_or_else(|_| {
panic!(
"Unable to convert data {} to PrivateKeyAndAddress",
&token_deployer_data
)
});

assert!(
token_deployer.is_matching_to_default(),
"Inconsistent key data"
);

// TODO: #840
create_demo_genesis_config(
100000000,
token_deployer.address,
sequencer_da_address.as_ref().to_vec(),
&token_deployer.private_key,
#[cfg(feature = "experimental")]
eth_accounts,
)
}
9 changes: 5 additions & 4 deletions examples/demo-rollup/src/rollup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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::runtime::{get_rpc_methods, GenesisConfig, Runtime};
#[cfg(feature = "experimental")]
use secp256k1::SecretKey;
Expand Down Expand Up @@ -34,7 +35,7 @@ use tracing::debug;
#[cfg(feature = "experimental")]
use crate::register_rpc::register_ethereum;
use crate::register_rpc::{register_ledger, register_sequencer};
use crate::{get_genesis_config, initialize_ledger, ROLLUP_NAMESPACE};
use crate::{initialize_ledger, ROLLUP_NAMESPACE};

#[cfg(feature = "experimental")]
const TX_SIGNER_PRIV_KEY_PATH: &str = "../test-data/keys/tx_signer_private_key.json";
Expand Down Expand Up @@ -125,8 +126,8 @@ pub async fn new_rollup_with_celestia_da<Vm: ZkvmHost>(

#[cfg(feature = "experimental")]
let eth_signer = read_eth_tx_signers();
let genesis_config = get_genesis_config(
sequencer_da_address,
let genesis_config = demo_stf::genesis_config::get_genesis_config(
sequencer_da_address.as_ref().to_vec(),
#[cfg(feature = "experimental")]
eth_signer.signers(),
);
Expand Down Expand Up @@ -182,7 +183,7 @@ pub fn new_rollup_with_mock_da_from_config<Vm: ZkvmHost>(
let eth_signer = read_eth_tx_signers();
let app = App::new(rollup_config.storage);
let genesis_config = get_genesis_config(
sequencer_da_address,
sequencer_da_address.as_ref().to_vec(),
#[cfg(feature = "experimental")]
eth_signer.signers(),
);
Expand Down
75 changes: 57 additions & 18 deletions examples/demo-stf/src/genesis_config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use sov_chain_state::ChainStateConfig;
use sov_cli::wallet_state::PrivateKeyAndAddress;
#[cfg(feature = "experimental")]
use sov_evm::{AccountData, EvmConfig, SpecId};
pub use sov_modules_api::default_context::DefaultContext;
use sov_modules_api::default_signature::private_key::DefaultPrivateKey;
use sov_modules_api::utils::generate_address;
use sov_modules_api::{Context, PrivateKey, PublicKey};
use sov_rollup_interface::da::DaSpec;
pub use sov_state::config::Config as StorageConfig;
Expand All @@ -12,34 +11,64 @@ use sov_value_setter::ValueSetterConfig;
/// Creates config for a rollup with some default settings, the config is used in demos and tests.
use crate::runtime::GenesisConfig;

pub const DEMO_SEQUENCER_DA_ADDRESS: [u8; 32] = [1; 32];
pub const LOCKED_AMOUNT: u64 = 50;
pub const DEMO_SEQ_PUB_KEY_STR: &str = "seq_pub_key";
pub const DEMO_TOKEN_NAME: &str = "sov-demo-token";

pub fn create_demo_genesis_config<C: Context, Da: DaSpec>(
/// 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.
///
/// If you want to customize the rollup to accept transactions from your own celestia
/// address, simply change the value of the SEQUENCER_DA_ADDRESS to your own address.
/// For example:
/// ```rust,no_run
/// const SEQUENCER_DA_ADDRESS: &str = "celestia1qp09ysygcx6npted5yc0au6k9lner05yvs9208";
/// ```
pub fn get_genesis_config<C: Context, Da: DaSpec>(
sequencer_da_address: Vec<u8>,
#[cfg(feature = "experimental")] evm_genesis_addresses: Vec<reth_primitives::Address>,
) -> GenesisConfig<C, Da> {
// This will be read from a file: #872
let initial_sequencer_balance = 100000000;
let token_deployer: PrivateKeyAndAddress<C> = read_private_key();

create_genesis_config(
initial_sequencer_balance,
token_deployer.address.clone(),
sequencer_da_address,
&token_deployer.private_key,
#[cfg(feature = "experimental")]
evm_genesis_addresses,
)
}

fn create_genesis_config<C: Context, Da: DaSpec>(
initial_sequencer_balance: u64,
sequencer_address: C::Address,
sequencer_da_address: Vec<u8>,
value_setter_admin_private_key: &DefaultPrivateKey,
value_setter_admin_private_key: &C::PrivateKey,
#[cfg(feature = "experimental")] evm_genesis_addresses: Vec<reth_primitives::Address>,
) -> GenesisConfig<C, Da> {
// This will be read from a file: #872
let token_config: sov_bank::TokenConfig<C> = sov_bank::TokenConfig {
token_name: DEMO_TOKEN_NAME.to_owned(),
address_and_balances: vec![(sequencer_address.clone(), initial_sequencer_balance)],
authorized_minters: vec![sequencer_address.clone()],
salt: 0,
};

// This will be read from a file: #872
let bank_config = sov_bank::BankConfig {
tokens: vec![token_config],
};

// This will be read from a file: #872
let token_address = sov_bank::get_genesis_token_address::<C>(
&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,
Expand All @@ -50,10 +79,12 @@ pub fn create_demo_genesis_config<C: Context, Da: DaSpec>(
is_preferred_sequencer: true,
};

// This will be read from a file: #872
let value_setter_config = ValueSetterConfig {
admin: value_setter_admin_private_key.pub_key().to_address(),
};

// This will be read from a file: #872
let chain_state_config = ChainStateConfig {
// TODO: Put actual value
initial_slot_height: 0,
Expand Down Expand Up @@ -95,16 +126,24 @@ fn get_evm_config(genesis_addresses: Vec<reth_primitives::Address>) -> EvmConfig
}
}

pub fn create_demo_config<Da: DaSpec>(
initial_sequencer_balance: u64,
value_setter_admin_private_key: &DefaultPrivateKey,
) -> GenesisConfig<DefaultContext, Da> {
create_demo_genesis_config::<DefaultContext, Da>(
initial_sequencer_balance,
generate_address::<DefaultContext>(DEMO_SEQ_PUB_KEY_STR),
DEMO_SEQUENCER_DA_ADDRESS.to_vec(),
value_setter_admin_private_key,
#[cfg(feature = "experimental")]
Vec::default(),
)
pub fn read_private_key<C: Context>() -> PrivateKeyAndAddress<C> {
// TODO fix the hardcoded path: #872
let token_deployer_data =
std::fs::read_to_string("../test-data/keys/token_deployer_private_key.json")
.expect("Unable to read file to string");

let token_deployer: PrivateKeyAndAddress<C> = serde_json::from_str(&token_deployer_data)
.unwrap_or_else(|_| {
panic!(
"Unable to convert data {} to PrivateKeyAndAddress",
&token_deployer_data
)
});

assert!(
token_deployer.is_matching_to_default(),
"Inconsistent key data"
);

token_deployer
}
19 changes: 16 additions & 3 deletions examples/demo-stf/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
use std::path::Path;

use sov_modules_api::default_context::DefaultContext;
use sov_modules_api::{default_context::DefaultContext, DaSpec};
use sov_modules_stf_template::AppTemplate;
use sov_rollup_interface::mocks::MockDaSpec;
use sov_state::ProverStorage;

use crate::runtime::Runtime;
use crate::{
genesis_config::get_genesis_config,
runtime::{GenesisConfig, Runtime},
};

mod da_simulation;
mod stf_tests;
mod tx_revert_tests;
pub(crate) type C = DefaultContext;
pub(crate) type Da = MockDaSpec;

pub fn create_new_demo(
pub(crate) const TEST_SEQUENCER_DA_ADDRESS: [u8; 32] = [1; 32];

pub(crate) fn create_new_app_template_for_tests(
path: impl AsRef<Path>,
) -> AppTemplate<
DefaultContext,
Expand All @@ -25,3 +30,11 @@ pub fn create_new_demo(
let storage = ProverStorage::with_path(path).unwrap();
AppTemplate::new(storage, runtime)
}

pub(crate) fn get_genesis_config_for_tests<Da: DaSpec>() -> GenesisConfig<DefaultContext, Da> {
get_genesis_config::<DefaultContext, Da>(
TEST_SEQUENCER_DA_ADDRESS.to_vec(),
#[cfg(feature = "experimental")]
Vec::default(),
)
}
Loading

0 comments on commit 9b9b027

Please sign in to comment.