Skip to content

Commit

Permalink
fix(devnet): loosened devnet chain id restrictions (#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
Trantorian1 authored Jan 8, 2025
1 parent 448bef5 commit 9290378
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 36 deletions.
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ You can choose between different build modes:

Start the Madara client with a basic set of arguments depending on your chosen mode:

> [!NOTE]
> Head to the [Configuration](#%EF%B8%8F-configuration) section to learn more about
> customizing your node.
#### Full Node

Synchronizes the state of the chain from genesis.
Expand Down Expand Up @@ -125,16 +129,16 @@ cargo run --release -- \
A node in a private local network.

```bash
cargo run --release -- \
--name Madara \
--devnet \
--base-path /var/lib/madara \
--preset sepolia
cargo run --release -- \
--name Madara \
--devnet \
--base-path ../madara_db \
--chain-config-override=chain_id=MY_CUSTOM_DEVNET
```

> [!NOTE]
> Head to the [Configuration](#%EF%B8%8F-configuration) section to learn more about
> customizing your node.
> [!CAUTION]
> Make sure to use a unique `chain_id` for your devnet to avoid potential replay
> attacks in other chains with the same chain id!
#### 4. Presets

Expand Down
5 changes: 0 additions & 5 deletions crates/madara/node/src/cli/block_production.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ pub struct BlockProductionParams {
#[arg(env = "MADARA_BLOCK_PRODUCTION_DISABLED", long, alias = "no-block-production")]
pub block_production_disabled: bool,

/// Launch a devnet with a production chain id (like SN_MAINNET, SN_SEPOLIA).
/// This in unsafe because your devnet transactions can be replayed on the actual network.
#[arg(env = "MADARA_OVERRIDE_DEVNET_CHAIN_ID", long, default_value_t = false)]
pub override_devnet_chain_id: bool,

/// Create this number of contracts in the genesis block for the devnet configuration.
#[arg(env = "MADARA_DEVNET_CONTRACTS", long, default_value_t = 10)]
pub devnet_contracts: u64,
Expand Down
16 changes: 4 additions & 12 deletions crates/madara/node/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub use db::*;
pub use gateway::*;
pub use l2::*;
pub use rpc::*;
use starknet_api::core::ChainId;
use std::str::FromStr;
pub use telemetry::*;

Expand Down Expand Up @@ -188,6 +187,10 @@ pub struct RunCmd {
#[arg(env = "MADARA_DEVNET", long, group = "mode")]
pub devnet: bool,

/// Allows a devnet to have SN_MAIN or SN_SEPOLIA as its chain ID.
#[arg(env = "MADARA_DEVNET_UNSAFE", long, requires = "devnet")]
pub devnet_unsafe: bool,

/// The network chain configuration.
#[clap(env = "MADARA_NETWORK", long, short, group = "full_mode_config")]
pub network: Option<NetworkType>,
Expand Down Expand Up @@ -313,17 +316,6 @@ pub enum NetworkType {
Devnet,
}

impl NetworkType {
pub fn chain_id(&self) -> ChainId {
match self {
NetworkType::Main => ChainId::Mainnet,
NetworkType::Test => ChainId::Sepolia,
NetworkType::Integration => ChainId::IntegrationSepolia,
NetworkType::Devnet => ChainId::Other("MADARA_DEVNET".to_string()),
}
}
}

#[derive(Debug, Clone, clap::ValueEnum)]
#[value(rename_all = "kebab-case")]
pub enum ChainPreset {
Expand Down
22 changes: 11 additions & 11 deletions crates/madara/node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod util;

use anyhow::{bail, Context};
use clap::Parser;
use cli::{NetworkType, RunCmd};
use cli::RunCmd;
use http::{HeaderName, HeaderValue};
use mc_analytics::Analytics;
use mc_block_import::BlockImporter;
Expand All @@ -20,6 +20,7 @@ use mc_telemetry::{SysInfo, TelemetryService};
use mp_oracle::pragma::PragmaOracleBuilder;
use mp_utils::service::{MadaraServiceId, ServiceMonitor};
use service::{BlockProductionService, GatewayService, L1SyncService, L2SyncService, RpcService};
use starknet_api::core::ChainId;
use std::sync::Arc;

const GREET_IMPL_NAME: &str = "Madara";
Expand Down Expand Up @@ -50,16 +51,15 @@ async fn main() -> anyhow::Result<()> {
run_cmd.chain_config()?
};

// Check if the devnet is running with the correct chain id.
if run_cmd.devnet && chain_config.chain_id != NetworkType::Devnet.chain_id() {
if !run_cmd.block_production_params.override_devnet_chain_id {
tracing::error!("You're running a devnet with the network config of {:?}. This means that devnet transactions can be replayed on the actual network. Use `--network=devnet` instead. Or if this is the expected behavior please pass `--override-devnet-chain-id`", chain_config.chain_name);
panic!();
} else {
// This log is immediately flooded with devnet accounts and so this can be missed.
// Should we add a delay here to make this clearly visisble?
tracing::warn!("You're running a devnet with the network config of {:?}. This means that devnet transactions can be replayed on the actual network.", run_cmd.network);
}
// Check if the devnet is running with the correct chain id. This is purely
// to avoid accidental setups which would allow for replay attacks. This is
// possible if the devnet has the same chain id as another popular chain,
// allowing txs which occur on it to also be replayed on that other chain.
if run_cmd.devnet
&& (chain_config.chain_id == ChainId::Mainnet || chain_config.chain_id == ChainId::Sepolia)
&& !run_cmd.devnet_unsafe
{
anyhow::bail!("You're running a devnet with the network config of {0}. This means that devnet transactions can be replayed on the actual {0} network. Use `--network=devnet` instead or force this configuration with `--devnet-unsafe`.", chain_config.chain_name);
}

let node_name = run_cmd.node_name_or_provide().await.to_string();
Expand Down

0 comments on commit 9290378

Please sign in to comment.