Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build Eip1559 transaction #45

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Node/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions Node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
alloy = { version = "0.1.2", features = [
"full",
"node-bindings",
] }
alloy = { version = "0.1.2", features = ["full", "node-bindings"] }
tokio = { version = "1.38", features = ["full"] }
tracing = "0.1.40"
tracing-subscriber = "0.3"
Expand All @@ -21,4 +18,6 @@ anyhow = "1.0.86"
k256 = "0.13"
elliptic-curve = "0.13"
reqwest = "0.12"

hex = "0.4"
tiny-keccak = "2.0"
secp256k1 = "0.29"
73 changes: 49 additions & 24 deletions Node/src/ethereum_l1/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#![allow(unused)] //TODO remove after the EthereumL1 is used in release code

use alloy::{
consensus::transaction::{TxLegacy, TypedTransaction},
network::{Ethereum, EthereumWallet, NetworkWallet},
primitives::{Address, Bytes, FixedBytes, U256, U32, U64},
primitives::{Address, Bytes, FixedBytes, TxKind, U256, U32, U64},
providers::ProviderBuilder,
rpc::types::{TransactionInput, TransactionRequest},
signers::local::PrivateKeySigner,
sol,
sol_types::SolValue,
Expand All @@ -14,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 @@ -44,28 +47,32 @@ 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 propose_new_block(
pub async fn create_propose_new_block_tx(
&self,
contract_address: Address,
tx_list: Vec<u8>,
parent_meta_hash: [u8; 32],
) -> Result<(), Error> {
) -> Result<Vec<u8>, Error> {
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.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 All @@ -84,17 +91,34 @@ impl EthereumL1 {

let tx_list = Bytes::from(tx_list);
let lookahead_set_param: Vec<PreconfTaskManager::LookaheadSetParam> = Vec::new();
let builder = contract.newBlockProposal(
encoded_block_params,
tx_list,
U256::from(0),
lookahead_set_param,
);

let tx_hash = builder.send().await?.watch().await?;
tracing::debug!("Proposed new block: {tx_hash}");
let builder = contract
.newBlockProposal(
encoded_block_params,
tx_list,
U256::from(0),
lookahead_set_param,
)
.nonce(1) //TODO how to get it?
.gas(100000)
.max_fee_per_gas(10000000000000000)
.max_priority_fee_per_gas(10000000000000000);

let tx = builder.as_ref().clone().build_typed_tx();
let Ok(TypedTransaction::Eip1559(mut tx)) = tx else {
return Err(anyhow::anyhow!("expect tx in EIP1559"));
};

Ok(())
let signature = self
.wallet
.default_signer()
.sign_transaction(&mut tx)
.await?;

let mut buf = vec![];
tx.encode_with_signature(&signature, &mut buf, false);

Ok(buf)
}

#[cfg(test)]
Expand All @@ -105,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 @@ -177,15 +206,11 @@ mod tests {
let ethereum_l1 = EthereumL1::new_from_pk(rpc_url, private_key).unwrap();

// some random address for test
ethereum_l1
.propose_new_block(
"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
.parse()
.unwrap(),
vec![0; 32],
[0; 32],
)
let encoded_tx = ethereum_l1
.create_propose_new_block_tx(vec![0; 32], [0; 32])
.await
.unwrap();

assert!(encoded_tx.len() > 0);
}
}
11 changes: 9 additions & 2 deletions Node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@ 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 node = node::Node::new(node_rx, avs_p2p_tx);
let taiko = taiko::Taiko::new(&config.taiko_proposer_url, &config.taiko_driver_url);
let ethereum_l1 = ethereum_l1::EthereumL1::new(
&config.mev_boost_url,
&config.ethereum_private_key,
&config.new_block_proposal_contract_address,
)?;
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
17 changes: 17 additions & 0 deletions Node/src/mev_boost/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
use crate::utils::rpc_client::RpcClient;

pub struct MevBoost {
_rpc_client: RpcClient,
}

impl MevBoost {
pub fn new(rpc_url: &str) -> Self {
let rpc_client = RpcClient::new(rpc_url);
Self {
_rpc_client: rpc_client,
}
}

pub fn send_transaction(&self, _tx: &[u8], _validator_index: u64, _slot: u64) {
//TODO: implement
}
}
29 changes: 25 additions & 4 deletions Node/src/node/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::taiko::Taiko;
use crate::{ethereum_l1::EthereumL1, mev_boost::MevBoost, taiko::Taiko};
use anyhow::{anyhow as any_err, Error, Ok};
use tokio::sync::mpsc::{Receiver, Sender};

Expand All @@ -7,16 +7,25 @@ pub struct Node {
node_rx: Option<Receiver<String>>,
avs_p2p_tx: Sender<String>,
gas_used: u64,
ethereum_l1: EthereumL1,
mev_boost: MevBoost,
}

impl Node {
pub fn new(node_rx: Receiver<String>, avs_p2p_tx: Sender<String>) -> Self {
let taiko = Taiko::new("http://127.0.0.1:1234", "http://127.0.0.1:1235");
pub fn new(
node_rx: Receiver<String>,
avs_p2p_tx: Sender<String>,
taiko: Taiko,
ethereum_l1: EthereumL1,
mev_boost: MevBoost,
) -> Self {
Self {
taiko,
node_rx: Some(node_rx),
avs_p2p_tx,
gas_used: 0,
ethereum_l1,
mev_boost,
}
}

Expand Down Expand Up @@ -67,11 +76,23 @@ impl Node {
.get_pending_l2_tx_lists()
.await
.map_err(Error::from)?;
if pending_tx_lists.tx_list_bytes.is_empty() {
return Ok(());
}

self.commit_to_the_tx_lists();
self.send_preconfirmations_to_the_avs_p2p().await?;
self.taiko
.advance_head_to_new_l2_block(pending_tx_lists, self.gas_used)
.advance_head_to_new_l2_block(pending_tx_lists.tx_lists, self.gas_used)
.await?;
let tx = self
.ethereum_l1
.create_propose_new_block_tx(
pending_tx_lists.tx_list_bytes[0].clone(), //TODO: handle rest tx lists
pending_tx_lists.parent_meta_hash,
)
.await?;
self.mev_boost.send_transaction(&tx, 1, 1);
Ok(())
}

Expand Down
Loading