Skip to content

Commit

Permalink
feat: enable setting the socket address for the Magi RPC (#206)
Browse files Browse the repository at this point in the history
Co-authored-by: Noah Citron <[email protected]>
  • Loading branch information
zilayo and ncitron authored Feb 27, 2024
1 parent c70ee4a commit 073ddb4
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 1 deletion.
3 changes: 3 additions & 0 deletions bin/magi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ pub struct Cli {
#[clap(short = 'p', long)]
rpc_port: Option<u16>,
#[clap(long)]
rpc_addr: Option<String>,
#[clap(long)]
logs_dir: Option<String>,
#[clap(long)]
logs_rotation: Option<String>,
Expand Down Expand Up @@ -112,6 +114,7 @@ impl From<Cli> for CliConfig {
jwt_secret,
checkpoint_sync_url: value.checkpoint_sync_url,
rpc_port: value.rpc_port,
rpc_addr: value.rpc_addr,
devnet: value.devnet,
}
}
Expand Down
3 changes: 3 additions & 0 deletions docker/.env.default
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ JWT_SECRET=bf549f5188556ce0951048ef467ec93067bc4ea21acebe46ef675cd4e8e015ff
# Magi's external rpc service port
RPC_PORT=9545

# Magi's external rpc service socket address
RPC_ADDR="0.0.0.0"

# Execution client: can be either `op-geth` or `op-erigon`
EXECUTION_CLIENT=op-geth

Expand Down
2 changes: 2 additions & 0 deletions docker/start-magi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ then
--l2-rpc-url http://${EXECUTION_CLIENT}:8545 \
--l2-engine-url http://${EXECUTION_CLIENT}:8551 \
--rpc-port $RPC_PORT \
--rpc-addr $RPC_ADDR \
$DEVNET \
--sync-mode $SYNC_MODE
elif [ $SYNC_MODE = "checkpoint" ]
Expand All @@ -32,6 +33,7 @@ then
--l2-rpc-url http://${EXECUTION_CLIENT}:8545 \
--l2-engine-url http://${EXECUTION_CLIENT}:8551 \
--rpc-port $RPC_PORT \
--rpc-addr $RPC_ADDR \
$DEVNET \
--sync-mode $SYNC_MODE \
--checkpoint-sync-url $CHECKPOINT_SYNC_URL \
Expand Down
1 change: 1 addition & 0 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ The [Config](../src/config/mod.rs) object contains the system configuration for
- `jwt_secret`: A hex-encoded secret string used to authenticate requests to the engine API.
- `checkpoint_sync_url`: The URL of the trusted L2 RPC endpoint to use for checkpoint syncing.
- `rpc_port`: The port to use for the Magi RPC server.
- `rpc_addr`: The socket address to use for the Magi RPC server.

**ChainConfig**
- `network`: The network name.
Expand Down
7 changes: 7 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub struct Config {
pub checkpoint_sync_url: Option<String>,
/// The port of the `Magi` RPC server
pub rpc_port: u16,
/// The socket address of RPC server
pub rpc_addr: String,
/// The devnet mode.
/// If devnet is enabled.
pub devnet: bool,
}
Expand Down Expand Up @@ -115,6 +118,8 @@ pub struct CliConfig {
/// The port to serve the Magi RPC on.
#[serde(skip_serializing_if = "Option::is_none")]
pub rpc_port: Option<u16>,
#[serde(skip_serializing_if = "Option::is_none")]
pub rpc_addr: Option<String>,
/// If Magi is running in devnet mode.
#[serde(default)]
pub devnet: bool,
Expand Down Expand Up @@ -227,6 +232,7 @@ struct DefaultsProvider {
l2_engine_url: String,
/// The port to serve the Magi RPC server on
rpc_port: u16,
rpc_addr: String,
}

impl Default for DefaultsProvider {
Expand All @@ -236,6 +242,7 @@ impl Default for DefaultsProvider {
l2_rpc_url: "http://127.0.0.1:8545".to_string(),
l2_engine_url: "http://127.0.0.1:8551".to_string(),
rpc_port: 9545,
rpc_addr: "127.0.0.1".to_string(),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/derive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ mod tests {
jwt_secret: String::new(),
checkpoint_sync_url: None,
rpc_port: 9545,
rpc_addr: "127.0.0.1".to_string(),
devnet: false,
});

Expand Down
1 change: 1 addition & 0 deletions src/derive/stages/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ mod tests {
l2_engine_url: String::new(),
jwt_secret: String::new(),
rpc_port: 9545,
rpc_addr: "127.0.0.1".to_string(),
chain: ChainConfig::optimism_goerli(),
checkpoint_sync_url: None,
devnet: false,
Expand Down
1 change: 1 addition & 0 deletions src/driver/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ mod test_utils {
jwt_secret: Default::default(),
checkpoint_sync_url: Default::default(),
rpc_port: Default::default(),
rpc_addr: Default::default(),
devnet: false,
}
}
Expand Down
1 change: 1 addition & 0 deletions src/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ mod tests {
),
checkpoint_sync_url: Some(l2_rpc.to_owned()),
rpc_port: None,
rpc_addr: None,
devnet: false,
};
let config = Config::new(&config_path, cli_config, ChainConfig::optimism_goerli());
Expand Down
83 changes: 82 additions & 1 deletion src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ fn compute_l2_output_root(block: Block<H256>, storage_root: H256) -> H256 {
/// Starts the Magi RPC server
pub async fn run_server(config: Arc<Config>) -> Result<SocketAddr> {
let port = config.rpc_port;
let addr = config.rpc_addr.clone();

let server = ServerBuilder::default()
.build(format!("127.0.0.1:{}", port))
.build(format!("{}:{}", addr, port))
.await?;
let addr = server.local_addr()?;
let rpc_impl = RpcServerImpl {
Expand Down Expand Up @@ -157,3 +159,82 @@ pub struct OutputRootResponse {
/// The 32 byte storage root of the `L2toL1MessagePasser` contract address
pub withdrawal_storage_root: H256,
}

#[cfg(test)]
mod tests {
use super::*;
use crate::config::{ChainConfig, CliConfig, ExternalChainConfig};
use reqwest;
use serde_json::json;
use std::{path::PathBuf, str::FromStr};
use tokio::time::{sleep, Duration};
use tracing_subscriber;

#[derive(Serialize, Deserialize, Debug)]
struct RpcResponse {
jsonrpc: String,
result: ExternalChainConfig,
id: u64,
}

#[tokio::test]
async fn test_run_server() -> Result<()> {
std::env::set_var("RUST_LOG", "trace");
let cli_config = CliConfig {
l1_rpc_url: Some("".to_string()),
l2_rpc_url: None,
l2_engine_url: None,
jwt_secret: Some("".to_string()),
checkpoint_sync_url: None,
rpc_port: Some(8080),
rpc_addr: Some("127.0.0.1".to_string()),
devnet: false,
};

tracing_subscriber::fmt().init();

let config_path = PathBuf::from_str("config.toml")?;
let config = Arc::new(Config::new(
&config_path,
cli_config,
ChainConfig::optimism_sepolia(),
));

let addr = run_server(config.clone())
.await
.expect("Failed to start server");

sleep(Duration::from_millis(100)).await;

let client = reqwest::Client::new();

let request_body = json!({
"jsonrpc": "2.0",
"method": "optimism_rollupConfig",
"params": [],
"id": 1,
});

let response = client
.post(format!("http://{}", addr))
.json(&request_body)
.send()
.await
.expect("Failed to send request");

assert!(response.status().is_success());

let rpc_response: RpcResponse = response.json().await.expect("Failed to parse response");

let rpc_chain_config: ChainConfig = rpc_response.result.into();

assert_eq!(config.chain.l2_genesis, rpc_chain_config.l2_genesis);
assert_eq!(
config.chain.l2_to_l1_message_passer,
rpc_chain_config.l2_to_l1_message_passer
);

println!("{:#?}", rpc_chain_config);
Ok(())
}
}

0 comments on commit 073ddb4

Please sign in to comment.