Skip to content

Commit

Permalink
[genesis] Introduce GenesisConfig (#1672)
Browse files Browse the repository at this point in the history
  • Loading branch information
jolestar authored May 14, 2024
1 parent 1761b7d commit 5060ec9
Show file tree
Hide file tree
Showing 24 changed files with 547 additions and 636 deletions.
14 changes: 4 additions & 10 deletions crates/rooch-benchmarks/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ use rooch_sequencer::proxy::SequencerProxy;
use rooch_store::RoochStore;
use rooch_test_transaction_builder::TestTransactionBuilder;
use rooch_types::address::RoochAddress;
use rooch_types::bitcoin::genesis::BitcoinGenesisContext;
use rooch_types::bitcoin::network::Network;
use rooch_types::chain_id::RoochChainID;
use rooch_types::crypto::RoochKeyPair;
use rooch_types::multichain_id::RoochMultiChainID;
use rooch_types::rooch_network::{BuiltinChainID, RoochNetwork};
use rooch_types::transaction::rooch::RoochTransaction;
use rooch_types::transaction::L1BlockWithBody;

Expand All @@ -77,7 +75,6 @@ pub async fn setup_service(
let _ = tracing_subscriber::fmt::try_init();

let actor_system = ActorSystem::global_system();
let chain_id = RoochChainID::LOCAL;

// init storage
let (mut moveos_store, rooch_store) = init_storage(datadir)?;
Expand All @@ -98,12 +95,9 @@ pub async fn setup_service(
let _relayer_account = RoochAddress::from(&relayer_keypair.public());

// Init executor
let btc_network = Network::default().to_num();
let _data_import_flag = false;

let genesis_ctx = chain_id.genesis_ctx(rooch_account);
let bitcoin_genesis_ctx = BitcoinGenesisContext::new(btc_network);
let genesis: RoochGenesis = RoochGenesis::build(genesis_ctx, bitcoin_genesis_ctx)?;
let mut network: RoochNetwork = BuiltinChainID::Dev.into();
network.set_sequencer_account(rooch_account.into());
let genesis: RoochGenesis = RoochGenesis::build(network)?;
let root = genesis.init_genesis(&mut moveos_store)?;

let executor_actor =
Expand Down
4 changes: 2 additions & 2 deletions crates/rooch-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use serde::{Deserialize, Serialize};

use moveos_config::{temp_dir, DataDirPath};
use rooch_types::bitcoin::network::Network;
use rooch_types::chain_id::RoochChainID;
use rooch_types::crypto::RoochKeyPair;
use rooch_types::rooch_network::{BuiltinChainID, RoochChainID};

use crate::da_config::DAConfig;
use crate::store_config::StoreConfig;
Expand Down Expand Up @@ -164,7 +164,7 @@ impl RoochOpt {

RoochOpt {
base_data_dir: Some(random_dir),
chain_id: Some(RoochChainID::LOCAL),
chain_id: Some(BuiltinChainID::Local.into()),
store: StoreConfig::default(),
port: None,
eth_rpc_url: None,
Expand Down
19 changes: 6 additions & 13 deletions crates/rooch-framework-tests/src/binding_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ use rooch_executor::actor::reader_executor::ReaderExecutorActor;
use rooch_executor::actor::{executor::ExecutorActor, messages::ExecuteTransactionResult};
use rooch_genesis::RoochGenesis;
use rooch_store::RoochStore;
use rooch_types::address::RoochAddress;
use rooch_types::bitcoin::genesis::BitcoinGenesisContext;
use rooch_types::bitcoin::network::Network;
use rooch_types::chain_id::RoochChainID;
use rooch_types::rooch_network::{BuiltinChainID, RoochNetwork};
use rooch_types::transaction::{L1BlockWithBody, RoochTransaction};
use std::env;
use std::path::Path;
Expand All @@ -42,7 +39,7 @@ pub fn get_data_dir() -> DataDirPath {
pub struct RustBindingTest {
//we should keep data_dir to make sure the temp dir is not deleted.
data_dir: DataDirPath,
sequencer: RoochAddress,
sequencer: AccountAddress,
pub executor: ExecutorActor,
pub reader_executor: ReaderExecutorActor,
root: RootObjectEntity,
Expand All @@ -66,13 +63,9 @@ impl RustBindingTest {
let mut moveos_store =
MoveOSStore::mock_moveos_store_with_data_dir(moveos_db_path.as_path())?;
let rooch_store = RoochStore::mock_rooch_store(rooch_db_path.as_path())?;
let sequencer = AccountAddress::ONE.into();

let genesis = RoochGenesis::build(
RoochChainID::LOCAL.genesis_ctx(sequencer),
BitcoinGenesisContext::new(Network::NetworkTestnet.to_num()),
// BitcoinGenesisContext::new(Network::default().to_num()),
)?;
let network: RoochNetwork = BuiltinChainID::Local.into();
let sequencer = network.genesis_config.sequencer_account;
let genesis = RoochGenesis::build(network)?;
let root = genesis.init_genesis(&mut moveos_store)?;

let executor = ExecutorActor::new(root.clone(), moveos_store.clone(), rooch_store.clone())?;
Expand Down Expand Up @@ -135,7 +128,7 @@ impl RustBindingTest {
let tx_hash = l1_block.block.tx_hash();
let tx_size = l1_block.block.tx_size();
TxContext::new(
self.sequencer.into(),
self.sequencer,
sequence_number,
max_gas_amount,
tx_hash,
Expand Down
4 changes: 4 additions & 0 deletions crates/rooch-framework-tests/src/tests/bitcoin_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ fn test_submit_block() {
//RUST_LOG=debug cargo test --release --package rooch-framework-tests --lib -- --include-ignored tests::bitcoin_test::test_utxo_progress
#[test]
fn test_utxo_progress() {
if cfg!(debug_assertions) {
println!("test_utxo_progress is ignored in debug mode, please run it in release mode");
return;
}
let _ = tracing_subscriber::fmt::try_init();
let mut binding_test = binding_test::RustBindingTest::new().unwrap();

Expand Down
4 changes: 2 additions & 2 deletions crates/rooch-framework-tests/src/tests/chain_id_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::binding_test;
use moveos_types::{module_binding::MoveFunctionCaller, state_resolver::StateResolver};
use rooch_types::{chain_id::RoochChainID, framework::chain_id::ChainID};
use rooch_types::{framework::chain_id::ChainID, rooch_network::BuiltinChainID};

#[test]
fn test_chain_id() {
Expand All @@ -15,5 +15,5 @@ fn test_chain_id() {
let chain_id_module =
binding_test.as_module_binding::<rooch_types::framework::chain_id::ChainIDModule>();
let chain_id = chain_id_module.chain_id().unwrap();
assert_eq!(chain_id, RoochChainID::LOCAL.chain_id().id());
assert_eq!(chain_id, BuiltinChainID::Local.chain_id().id());
}
15 changes: 0 additions & 15 deletions crates/rooch-genesis-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,6 @@ use framework_builder::{Stdlib, StdlibBuildConfig};
use move_package::BuildConfig;
use once_cell::sync::Lazy;

pub const ALL_STDLIB_PACKAGE_NAMES: [&str; 5] = [
"MoveStdlib",
"MoveosStdlib",
"RoochFramework",
"BitcoinMove",
"RoochNursery",
];

pub const ALL_STDLIB_PACKAGE_NAMES_STABLE: [&str; 4] = [
"MoveStdlib",
"MoveosStdlib",
"RoochFramework",
"BitcoinMove",
];

static STDLIB_BUILD_CONFIGS: Lazy<Vec<StdlibBuildConfig>> = Lazy::new(|| {
let move_stdlib_path = path_in_crate("../../frameworks/move-stdlib")
.canonicalize()
Expand Down
65 changes: 19 additions & 46 deletions crates/rooch-genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ use rooch_framework::natives::gas_parameter::gas_member::{
FromOnChainGasSchedule, InitialGasSchedule, ToOnChainGasSchedule,
};
use rooch_framework::ROOCH_FRAMEWORK_ADDRESS;
use rooch_genesis_builder::{ALL_STDLIB_PACKAGE_NAMES, ALL_STDLIB_PACKAGE_NAMES_STABLE};
use rooch_types::bitcoin::genesis::BitcoinGenesisContext;
use rooch_types::bitcoin::network::Network;
use rooch_types::error::GenesisError;
use rooch_types::framework::genesis::GenesisContext;
use rooch_types::rooch_network::{BuiltinChainID, RoochNetwork};
use rooch_types::transaction::rooch::RoochTransaction;
use rooch_types::{address::RoochAddress, chain_id::RoochChainID};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::str::FromStr;
Expand All @@ -37,19 +35,8 @@ use std::{
};

pub static ROOCH_LOCAL_GENESIS: Lazy<RoochGenesis> = Lazy::new(|| {
// TODO: For now, ROOCH_LOCAL_GENESIS in only used in integration-test.
// There is no need to upgrade framework, so we set sequencer to 0x0.
// Setup sequencer for local genesis if there is only demands.
let mock_sequencer = RoochAddress::from_str("0x0").expect("parse sequencer address failed");
// genesis for integration test, we need to build the stdlib every time for `private_generic` check
// see moveos/moveos-verifier/src/metadata.rs#L27-L30
let bitcoin_genesis_ctx = BitcoinGenesisContext::new(Network::NetworkRegtest.to_num());
RoochGenesis::build_with_option(
RoochChainID::LOCAL.genesis_ctx(mock_sequencer),
bitcoin_genesis_ctx,
BuildOption::Release,
)
.expect("build rooch genesis failed")
let network: RoochNetwork = BuiltinChainID::Local.into();
RoochGenesis::build(network).expect("build rooch genesis failed")
});

pub struct FrameworksGasParameters {
Expand Down Expand Up @@ -136,36 +123,30 @@ pub enum BuildOption {
}

impl RoochGenesis {
pub fn build(
genesis_ctx: GenesisContext,
bitcoin_genesis_ctx: BitcoinGenesisContext,
) -> Result<Self> {
Self::build_with_option(genesis_ctx, bitcoin_genesis_ctx, BuildOption::Release)
pub fn build(network: RoochNetwork) -> Result<Self> {
Self::build_with_option(network, BuildOption::Release)
}

pub fn build_with_option(
genesis_ctx: GenesisContext,
bitcoin_genesis_ctx: BitcoinGenesisContext,
option: BuildOption,
) -> Result<Self> {
pub fn build_with_option(network: RoochNetwork, option: BuildOption) -> Result<Self> {
let stdlib = match option {
BuildOption::Fresh => Self::build_stdlib()?,
BuildOption::Release => Self::load_stdlib()?,
};
//TODO put the stdlib package names to RoochChainID
let stdlib_package_names = if genesis_ctx.chain_id == RoochChainID::LOCAL.chain_id().id()
|| genesis_ctx.chain_id == RoochChainID::DEV.chain_id().id()
{
ALL_STDLIB_PACKAGE_NAMES.to_vec()
} else {
ALL_STDLIB_PACKAGE_NAMES_STABLE.to_vec()
};
let genesis_config = network.genesis_config;
let genesis_ctx = GenesisContext::new(
network.chain_id.id,
genesis_config.timestamp,
genesis_config.sequencer_account,
);
let bitcoin_genesis_ctx = BitcoinGenesisContext::new(genesis_config.bitcoin_network);

let stdlib_package_names = genesis_config.stdlib_package_names.clone();

let bundles = stdlib.module_bundles(stdlib_package_names.as_slice())?;

let genesis_tx = RoochTransaction::new_genesis_tx(
ROOCH_FRAMEWORK_ADDRESS.into(),
genesis_ctx.chain_id,
network.chain_id.id,
//merge all the module bundles into one
MoveAction::ModuleBundle(
bundles
Expand Down Expand Up @@ -339,24 +320,19 @@ pub fn rooch_framework_error_descriptions() -> &'static [u8] {

#[cfg(test)]
mod tests {
use move_core_types::account_address::AccountAddress;
use moveos_store::MoveOSStore;
use moveos_types::moveos_std::move_module::ModuleStore;
use moveos_types::state_resolver::{RootObjectResolver, StateResolver};
use rooch_types::bitcoin::genesis::BitcoinGenesisContext;
use rooch_types::bitcoin::network::{BitcoinNetwork, Network};
use rooch_types::chain_id::{BuiltinChainID, RoochChainID};
use rooch_types::rooch_network::BuiltinChainID;

use crate::FrameworksGasParameters;

#[test]
fn test_genesis_init() {
let _ = tracing_subscriber::fmt::try_init();
let sequencer = AccountAddress::ONE.into();
let bitcoin_genesis_ctx = BitcoinGenesisContext::new(Network::NetworkRegtest.to_num());
let genesis = super::RoochGenesis::build_with_option(
RoochChainID::LOCAL.genesis_ctx(sequencer),
bitcoin_genesis_ctx,
BuiltinChainID::Local.into(),
crate::BuildOption::Fresh,
)
.expect("build rooch genesis failed");
Expand Down Expand Up @@ -399,9 +375,6 @@ mod tests {
.unwrap()
.into_object::<BitcoinNetwork>()
.unwrap();
assert_eq!(
bitcoin_network.value.network,
Network::NetworkRegtest.to_num()
);
assert_eq!(bitcoin_network.value.network, Network::Regtest.to_num());
}
}
8 changes: 4 additions & 4 deletions crates/rooch-rpc-client/src/client_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use anyhow::anyhow;
use rooch_config::config::Config;
use rooch_config::server_config::ServerConfig;
use rooch_types::address::RoochAddress;
use rooch_types::chain_id::RoochChainID;
use rooch_types::rooch_network::BuiltinChainID;
use serde::Deserialize;
use serde::Serialize;
use std::fmt::{Display, Formatter, Write};
Expand Down Expand Up @@ -93,15 +93,15 @@ impl Env {

pub fn new_dev_env() -> Self {
Self {
alias: RoochChainID::DEV.chain_name().to_lowercase(),
alias: BuiltinChainID::Dev.chain_name(),
rpc: ROOCH_DEV_NET_URL.into(),
ws: None,
}
}

pub fn new_test_env() -> Self {
Self {
alias: RoochChainID::TEST.chain_name().to_lowercase(),
alias: BuiltinChainID::Test.chain_name(),
rpc: ROOCH_TEST_NET_URL.into(),
ws: None,
}
Expand All @@ -111,7 +111,7 @@ impl Env {
impl Default for Env {
fn default() -> Self {
Env {
alias: RoochChainID::LOCAL.chain_name().to_lowercase(),
alias: BuiltinChainID::Dev.chain_name(),
rpc: ServerConfig::default().url(false),
ws: None,
}
Expand Down
Loading

0 comments on commit 5060ec9

Please sign in to comment.