Skip to content

Commit

Permalink
Configuration parameters take form the ENV
Browse files Browse the repository at this point in the history
  • Loading branch information
mskrzypkows committed Jul 4, 2024
1 parent 752e169 commit 762300d
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 22 deletions.
26 changes: 15 additions & 11 deletions Node/src/ethereum_l1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::str::FromStr;
pub struct EthereumL1 {
rpc_url: reqwest::Url,
wallet: EthereumWallet,
new_block_proposal_contract_address: Address,
}

sol!(
Expand Down Expand Up @@ -46,19 +47,23 @@ sol! {
}

impl EthereumL1 {
pub fn new(rpc_url: &str, private_key: &str) -> Result<Self, Error> {
pub fn new(
rpc_url: &str,
private_key: &str,
new_block_proposal_contract_address: &str,
) -> Result<Self, Error> {
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<u8>,
parent_meta_hash: [u8; 32],
) -> Result<Vec<u8>, Error> {
Expand All @@ -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,
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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();

Expand Down
10 changes: 6 additions & 4 deletions Node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
5 changes: 1 addition & 4 deletions Node/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(());
}

Expand All @@ -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,
)
Expand Down
6 changes: 3 additions & 3 deletions Node/src/taiko/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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(""));
}
Expand Down
50 changes: 50 additions & 0 deletions Node/src/utils/config.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
1 change: 1 addition & 0 deletions Node/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod config;
pub mod rpc_client;
pub mod rpc_server;

0 comments on commit 762300d

Please sign in to comment.