From 762300d6e55f628f695aa3b3512e03caec94d153 Mon Sep 17 00:00:00 2001 From: Maciej Skrzypkowski Date: Thu, 4 Jul 2024 09:33:01 +0200 Subject: [PATCH] Configuration parameters take form the ENV --- Node/src/ethereum_l1/mod.rs | 26 +++++++++++-------- Node/src/main.rs | 10 +++++--- Node/src/node/mod.rs | 5 +--- Node/src/taiko/mod.rs | 6 ++--- Node/src/utils/config.rs | 50 +++++++++++++++++++++++++++++++++++++ Node/src/utils/mod.rs | 1 + 6 files changed, 76 insertions(+), 22 deletions(-) create mode 100644 Node/src/utils/config.rs diff --git a/Node/src/ethereum_l1/mod.rs b/Node/src/ethereum_l1/mod.rs index 5e0e542..e44a30a 100644 --- a/Node/src/ethereum_l1/mod.rs +++ b/Node/src/ethereum_l1/mod.rs @@ -16,6 +16,7 @@ use std::str::FromStr; pub struct EthereumL1 { rpc_url: reqwest::Url, wallet: EthereumWallet, + new_block_proposal_contract_address: Address, } sol!( @@ -46,19 +47,23 @@ sol! { } impl EthereumL1 { - pub fn new(rpc_url: &str, private_key: &str) -> Result { + pub fn new( + rpc_url: &str, + private_key: &str, + new_block_proposal_contract_address: &str, + ) -> Result { let signer = PrivateKeySigner::from_str(private_key)?; let wallet = EthereumWallet::from(signer); Ok(Self { rpc_url: rpc_url.parse()?, wallet, + new_block_proposal_contract_address: new_block_proposal_contract_address.parse()?, }) } pub async fn create_propose_new_block_tx( &self, - contract_address: Address, tx_list: Vec, parent_meta_hash: [u8; 32], ) -> Result, Error> { @@ -67,7 +72,7 @@ impl EthereumL1 { .wallet(self.wallet.clone()) .on_http(self.rpc_url.clone()); - let contract = PreconfTaskManager::new(contract_address, provider); + let contract = PreconfTaskManager::new(self.new_block_proposal_contract_address, provider); let block_params = BlockParams { assignedProver: Address::ZERO, @@ -124,7 +129,12 @@ impl EthereumL1 { let signer = PrivateKeySigner::from_signing_key(private_key.into()); let wallet = EthereumWallet::from(signer); - Ok(Self { rpc_url, wallet }) + Ok(Self { + rpc_url, + wallet, + new_block_proposal_contract_address: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" + .parse()?, + }) } #[cfg(test)] @@ -197,13 +207,7 @@ mod tests { // some random address for test let encoded_tx = ethereum_l1 - .create_propose_new_block_tx( - "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" - .parse() - .unwrap(), - vec![0; 32], - [0; 32], - ) + .create_propose_new_block_tx(vec![0; 32], [0; 32]) .await .unwrap(); diff --git a/Node/src/main.rs b/Node/src/main.rs index 0f9357e..0adc7c4 100644 --- a/Node/src/main.rs +++ b/Node/src/main.rs @@ -13,17 +13,19 @@ const MESSAGE_QUEUE_SIZE: usize = 100; #[tokio::main] async fn main() -> Result<(), Error> { init_logging(); + let config = utils::config::Config::read_env_variables(); let (avs_p2p_tx, avs_p2p_rx) = mpsc::channel(MESSAGE_QUEUE_SIZE); let (node_tx, node_rx) = mpsc::channel(MESSAGE_QUEUE_SIZE); let p2p = p2p_network::AVSp2p::new(node_tx.clone(), avs_p2p_rx); p2p.start(); - let taiko = taiko::Taiko::new("http://127.0.0.1:1234", "http://127.0.0.1:1235"); + let taiko = taiko::Taiko::new(&config.taiko_proposer_url, &config.taiko_driver_url); let ethereum_l1 = ethereum_l1::EthereumL1::new( - "http://localhost:8545", - "0x4c0883a69102937d6231471b5dbb6204fe512961708279f2e3e8a5d4b8e3e3e8", + &config.mev_boost_url, + &config.ethereum_private_key, + &config.new_block_proposal_contract_address, )?; - let mev_boost = mev_boost::MevBoost::new("http://localhost:8545"); + let mev_boost = mev_boost::MevBoost::new(&config.mev_boost_url); let node = node::Node::new(node_rx, avs_p2p_tx, taiko, ethereum_l1, mev_boost); node.entrypoint().await?; Ok(()) diff --git a/Node/src/node/mod.rs b/Node/src/node/mod.rs index 61d5262..32b2768 100644 --- a/Node/src/node/mod.rs +++ b/Node/src/node/mod.rs @@ -76,7 +76,7 @@ impl Node { .get_pending_l2_tx_lists() .await .map_err(Error::from)?; - if pending_tx_lists.tx_list_bytes.len() == 0 { + if pending_tx_lists.tx_list_bytes.is_empty() { return Ok(()); } @@ -88,9 +88,6 @@ impl Node { let tx = self .ethereum_l1 .create_propose_new_block_tx( - "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" - .parse() - .unwrap(), pending_tx_lists.tx_list_bytes[0].clone(), //TODO: handle rest tx lists pending_tx_lists.parent_meta_hash, ) diff --git a/Node/src/taiko/mod.rs b/Node/src/taiko/mod.rs index d24490a..3c41a11 100644 --- a/Node/src/taiko/mod.rs +++ b/Node/src/taiko/mod.rs @@ -25,7 +25,7 @@ impl Taiko { .await?, )?; - if result.tx_list_bytes.len() > 0 { + if !result.tx_list_bytes.is_empty() { Self::print_number_of_received_txs(&result); } @@ -35,9 +35,9 @@ impl Taiko { fn print_number_of_received_txs(result: &l2_tx_lists::RPCReplyL2TxLists) { if let Some(tx_lists) = result.tx_lists.as_array() { let mut hashes = Vec::new(); - for (_, tx_list) in tx_lists.iter().enumerate() { + for tx_list in tx_lists { if let Some(tx_list_array) = tx_list.as_array() { - for (_, tx) in tx_list_array.iter().enumerate() { + for tx in tx_list_array { if let Some(hash) = tx.get("hash") { hashes.push(hash.as_str().unwrap_or("").get(0..8).unwrap_or("")); } diff --git a/Node/src/utils/config.rs b/Node/src/utils/config.rs new file mode 100644 index 0000000..b93b86d --- /dev/null +++ b/Node/src/utils/config.rs @@ -0,0 +1,50 @@ +use tracing::{info, warn}; + +pub struct Config { + pub taiko_proposer_url: String, + pub taiko_driver_url: String, + pub ethereum_private_key: String, + pub mev_boost_url: String, + pub new_block_proposal_contract_address: String, +} + +impl Config { + pub fn read_env_variables() -> Self { + const ETHEREUM_PRIVATE_KEY: &str = "ETHEREUM_PRIVATE_KEY"; + const NEW_BLOCK_PROPOSAL_CONTRACT_ADDRESS: &str = "NEW_BLOCK_PROPOSAL_CONTRACT_ADDRESS"; + + let config = Self { + taiko_proposer_url: std::env::var("TAIKO_PROPOSER_URL") + .unwrap_or_else(|_| "http://127.0.0.1:1234".to_string()), + taiko_driver_url: std::env::var("TAIKO_DRIVER_URL") + .unwrap_or_else(|_| "http://127.0.0.1:1235".to_string()), + new_block_proposal_contract_address: std::env::var(NEW_BLOCK_PROPOSAL_CONTRACT_ADDRESS) + .unwrap_or_else(|_| { + warn!( + "No new block proposal contract address found in {} env var, using default", + NEW_BLOCK_PROPOSAL_CONTRACT_ADDRESS + ); + "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2".to_string() + }), + ethereum_private_key: std::env::var(ETHEREUM_PRIVATE_KEY).unwrap_or_else(|_| { + warn!( + "No Ethereum private key found in {} env var, using default", + ETHEREUM_PRIVATE_KEY + ); + "0x4c0883a69102937d6231471b5dbb6204fe512961708279f2e3e8a5d4b8e3e3e8".to_string() + }), + mev_boost_url: std::env::var("MEV_BOOST_URL") + .unwrap_or_else(|_| "http://127.0.0.1:8080".to_string()), + }; + + info!( + "\nConfiguration: \nTaiko proposer URL: {}, \nTaiko driver URL: {}, \nMEV Boost URL: {}, \nNew block proposal contract address: {}", + config.taiko_proposer_url, + config.taiko_driver_url, + config.mev_boost_url, + config.new_block_proposal_contract_address + ); + + config + } +} diff --git a/Node/src/utils/mod.rs b/Node/src/utils/mod.rs index e0ca3dc..29186bd 100644 --- a/Node/src/utils/mod.rs +++ b/Node/src/utils/mod.rs @@ -1,2 +1,3 @@ +pub mod config; pub mod rpc_client; pub mod rpc_server;