Skip to content

Commit

Permalink
feat: Allow overriding the chain id when starting era-test-node local…
Browse files Browse the repository at this point in the history
…ly (#361)

* feat: Allow overriding the chain id when starting era-test-node locally

* use the chain_id in net_version too

---------

Co-authored-by: Dustin Brickwood <[email protected]>
  • Loading branch information
mm-zk and dutterbutter authored Oct 30, 2024
1 parent 28cad46 commit 6f4cc22
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 5 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ etc/**/*.zbin
.idea

*.log
.cache
.cache

zkout/
cache/
example.json
4 changes: 4 additions & 0 deletions src/config/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ pub struct Cli {

#[arg(long)]
pub override_bytecodes_dir: Option<String>,

/// Use a given chain id. If not set uses 260 (or the one from the forked network).
#[arg(long)]
pub chain_id: Option<u32>,
}

#[derive(Debug, Subcommand)]
Expand Down
6 changes: 6 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ impl TestNodeConfig {
self.node.resolve_hashes = *resolve_hashes;
}

if opt.chain_id.is_some() {
self.node.chain_id = opt.chain_id;
}

if let Some(contract_options) = &opt.dev_system_contracts {
self.node.system_contracts_options = match contract_options {
DevSystemContracts::BuiltIn => system_contracts::Options::BuiltIn,
Expand Down Expand Up @@ -149,6 +153,7 @@ pub mod node {
pub resolve_hashes: bool,
pub system_contracts_options: system_contracts::Options,
pub use_evm_emulator: bool,
pub chain_id: Option<u32>,
}

impl Default for InMemoryNodeConfig {
Expand All @@ -163,6 +168,7 @@ pub mod node {
resolve_hashes: Default::default(),
system_contracts_options: Default::default(),
use_evm_emulator: false,
chain_id: Default::default(),
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,14 @@ impl<S: ForkSource> ForkStorage<S> {
fork: Option<ForkDetails>,
system_contracts_options: &system_contracts::Options,
use_evm_emulator: bool,
override_chain_id: Option<u32>,
) -> Self {
let chain_id = fork
.as_ref()
.and_then(|d| d.overwrite_chain_id)
.unwrap_or(L2ChainId::from(TEST_NODE_NETWORK_ID));
.unwrap_or(L2ChainId::from(
override_chain_id.unwrap_or(TEST_NODE_NETWORK_ID),
));
tracing::info!("Starting network with chain id: {:?}", chain_id);

ForkStorage {
Expand Down Expand Up @@ -771,7 +774,7 @@ mod tests {
};

let mut fork_storage: ForkStorage<testing::ExternalStorage> =
ForkStorage::new(Some(fork_details), &options, false);
ForkStorage::new(Some(fork_details), &options, false, None);

assert!(fork_storage.is_write_initial(&never_written_key));
assert!(!fork_storage.is_write_initial(&key_with_some_value));
Expand Down
2 changes: 2 additions & 0 deletions src/node/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ impl<S: std::fmt::Debug + ForkSource> InMemoryNodeInner<S> {
fork,
&config.system_contracts_options,
config.use_evm_emulator,
config.chain_id,
),
config,
console_log_handler: ConsoleLogHandler::default(),
Expand Down Expand Up @@ -276,6 +277,7 @@ impl<S: std::fmt::Debug + ForkSource> InMemoryNodeInner<S> {
fork,
&config.system_contracts_options,
config.use_evm_emulator,
config.chain_id,
),
config,
console_log_handler: ConsoleLogHandler::default(),
Expand Down
2 changes: 1 addition & 1 deletion src/node/in_memory_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ mod tests {
blocks: Default::default(),
block_hashes: Default::default(),
filters: Default::default(),
fork_storage: ForkStorage::new(None, &old_system_contracts_options, false),
fork_storage: ForkStorage::new(None, &old_system_contracts_options, false, None),
config: Default::default(),
console_log_handler: Default::default(),
system_contracts: Default::default(),
Expand Down
7 changes: 6 additions & 1 deletion src/node/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> NetNamespa
for InMemoryNode<S>
{
fn net_version(&self) -> Result<String> {
Ok(TEST_NODE_NETWORK_ID.to_string())
let chain_id = self
.get_inner()
.read()
.map(|reader| reader.config.chain_id.unwrap_or(TEST_NODE_NETWORK_ID))
.expect("Failed to get lock");
Ok(chain_id.to_string())
}

fn net_peer_count(&self) -> Result<U256> {
Expand Down

0 comments on commit 6f4cc22

Please sign in to comment.