Skip to content

Commit

Permalink
Handling additional arguments passed by JSON RPC from proposer
Browse files Browse the repository at this point in the history
  • Loading branch information
mskrzypkows committed Jul 3, 2024
1 parent 87c244b commit 288bd76
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 62 deletions.
1 change: 1 addition & 0 deletions Node/Cargo.lock

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

2 changes: 1 addition & 1 deletion Node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ anyhow = "1.0.86"
k256 = "0.13"
elliptic-curve = "0.13"
reqwest = "0.12"

hex = "0.4"
4 changes: 2 additions & 2 deletions Node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ async fn main() -> Result<(), Error> {
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 ethereum_l1 = ethereum_l1::EthereumL1::new("http://localhost:8545", "private_key")?;
let node = node::Node::new(node_rx, avs_p2p_tx, ethereum_l1);
node.entrypoint().await?;
Ok(())
}
Expand Down
12 changes: 10 additions & 2 deletions Node/src/node/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::ethereum_l1::EthereumL1;
use crate::taiko::Taiko;
use anyhow::{anyhow as any_err, Error, Ok};
use tokio::sync::mpsc::{Receiver, Sender};
Expand All @@ -7,16 +8,22 @@ pub struct Node {
node_rx: Option<Receiver<String>>,
avs_p2p_tx: Sender<String>,
gas_used: u64,
ethereum_l1: EthereumL1,
}

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

Expand Down Expand Up @@ -70,8 +77,9 @@ impl Node {
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?;
// self.ethereum_l1.propose_new_block(pending_tx_lists).await?;
Ok(())
}

Expand Down
117 changes: 117 additions & 0 deletions Node/src/taiko/l2_tx_lists.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use anyhow::Error;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value;

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
pub struct RPCReplyL2TxLists {
pub tx_lists: Value,
#[serde(deserialize_with = "deserialize_tx_lists_bytes")]
pub tx_list_bytes: Vec<Vec<u8>>,
#[serde(deserialize_with = "deserialize_parent_meta_hash")]
pub parent_meta_hash: [u8; 32],
}

fn deserialize_tx_lists_bytes<'de, D>(deserializer: D) -> Result<Vec<Vec<u8>>, D::Error>
where
D: Deserializer<'de>,
{
let vec: Vec<String> = Deserialize::deserialize(deserializer)?;
let result = vec
.iter()
.map(|s| s.as_bytes().to_vec())
.collect::<Vec<Vec<u8>>>();
Ok(result)
}

fn deserialize_parent_meta_hash<'de, D>(deserializer: D) -> Result<[u8; 32], D::Error>
where
D: Deserializer<'de>,
{
let s: String = Deserialize::deserialize(deserializer)?;
let s = s.trim_start_matches("0x");
let bytes = hex::decode(s).map_err(serde::de::Error::custom)?;
if bytes.len() != 32 {
return Err(serde::de::Error::custom(
"Invalid length for parent_meta_hash",
));
}
let mut array = [0u8; 32];
array.copy_from_slice(&bytes);
Ok(array)
}

pub fn decompose_pending_lists_json(json: Value) -> Result<RPCReplyL2TxLists, Error> {
// Deserialize the JSON string into the struct
let rpc_reply: RPCReplyL2TxLists = serde_json::from_value(json)?;
Ok(rpc_reply)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_decompose_pending_lists_json() {
let json_data = serde_json::json!(
{
"TxLists": [
[
{
"type": "0x0",
"chainId": "0x28c61",
"nonce": "0x8836",
"to": "0x8b14d287b4150ff22ac73df8be720e933f659abc",
"gas": "0x35f30",
"gasPrice": "0xf4b87001",
"maxPriorityFeePerGas": null,
"maxFeePerGas": null,
"value": "0x0",
"input": "0x3161b7f60000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000027e9000000000000000000000000000000000000000000000005751b6fc9babade290000000000000000000000000000000000000000000000000000000008f811330000000000000000000000000000000000000000000000000000000000000010",
"v": "0x518e5",
"r": "0xb7b4b5540e08775ebb3c077ca7d572378cdb6ed55e3387173f8248578cc073e9",
"s": "0x1f8860f90b61202d4070d1eba494d38c2cb02c749ea1ec542c8d02e5ceeb6439",
"hash": "0xc653e446eafe51eea1f46e6e351adbd1cc8a3271e6935f1441f613a58d441f6a"
},
{
"type": "0x2",
"chainId": "0x28c61",
"nonce": "0x26d0",
"to": "0x2f6ef5baae08ae9df23528c217a77f03f93b690e",
"gas": "0x1a09b",
"gasPrice": null,
"maxPriorityFeePerGas": "0xcbef0801",
"maxFeePerGas": "0xcbef0801",
"value": "0x0",
"input": "0xbb2cd728000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000ae2c46ddb314b9ba743c6dee4878f151881333d90000000000000000000000007d16e966c879ed6ad9972ddd5376c41a427d2c2a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000004cd712049c0000000000000000000000000000000000000000000000000000057b3ac2af84",
"accessList": [],
"v": "0x0",
"r": "0xbfc407896ee6ed36962d1a591b41aa9a17fe19e176e61cb54a1cd69e3a337508",
"s": "0x621b2448dda4d797447935c051e3908f65dfb3729415280a04cb02c4474baefe",
"yParity": "0x0",
"hash": "0xffbcd2fab90f1bf314ca2da1bf83eeab3d17fd58a0393d29a697b2ff05d0e65c"
}
]
],
"TxListBytes": [
"eJz6ybTuJ6NeU4dZy5cdBYzNzPEGU7pFLrVvEeX/pHXc9se+Ir7J9qmz9jTsOGKYuP0bA36gQECeEb+0+kscEqyl0vknd+26p4lPN8cPQWMC9gs0s0o8XbB9y9YQPo7yuN027DXLrxaZ99zOuxpn3C5u3+QR3nOg+OUC+Y6En9yJCroOBRdfL5lyuUdng07JvIVvQnR6mZ6ee51iuZOxiuknY1kzU09ik9qFltPvORjBRDPjgtlT9PO+7lrHsW7uJ1ONQ+LL65l/WmfyNexkZNmtc12DgPMcCMgvICDPhMxZp+N2d7PIzl0lNrnvPCo+BnYIG99Elq8Ve5l2ovJt1s3puneDy45IOdXqaJFiPhrwuS7EMge3NGu11aH1LQcaFuw/wt6Z9+yt2TRdqUhpx1WzxP9JPix7JrPVS+baPCvjUo4FSdIqHneXXJ/uUml6IPDxhP7U+5uLpohqcLGcZjri7r3uHyAAAP//huiQHQ=="
],
"ParentMetaHash": "0x2bcf3b1bb0c4066aa46ba6e99b79d7602f124d5ae8fcffd2977b1c2138aa61bc"
}
);

let result = decompose_pending_lists_json(json_data).unwrap();

assert_eq!(result.tx_lists.as_array().unwrap().len(), 1);
assert_eq!(
result.tx_lists.as_array().unwrap()[0]
.as_array()
.unwrap()
.len(),
2
);
assert_eq!(result.tx_list_bytes.len(), 1);
assert_eq!(result.tx_list_bytes[0].len(), 492);
assert_eq!(result.parent_meta_hash.len(), 32);
}
}
37 changes: 18 additions & 19 deletions Node/src/taiko/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::utils::rpc_client::RpcClient;
use anyhow::Error;
use serde_json::Value;

pub mod l2_tx_lists;

pub struct Taiko {
rpc_proposer: RpcClient,
rpc_driver: RpcClient,
Expand All @@ -15,11 +17,13 @@ impl Taiko {
}
}

pub async fn get_pending_l2_tx_lists(&self) -> Result<Value, Error> {
pub async fn get_pending_l2_tx_lists(&self) -> Result<l2_tx_lists::RPCReplyL2TxLists, Error> {
tracing::debug!("Getting L2 tx lists");
self.rpc_proposer
.call_method("RPC.GetL2TxLists", vec![])
.await
l2_tx_lists::decompose_pending_lists_json(
self.rpc_proposer
.call_method("RPC.GetL2TxLists", vec![])
.await?,
)
}

pub async fn advance_head_to_new_l2_block(
Expand All @@ -29,7 +33,7 @@ impl Taiko {
) -> Result<Value, Error> {
tracing::debug!("Submitting new L2 blocks");
let payload = serde_json::json!({
"TxLists": tx_lists["TxLists"],
"TxLists": tx_lists,
"gasUsed": gas_used,
});
self.rpc_driver
Expand All @@ -49,24 +53,19 @@ mod test {
tracing_subscriber::fmt::init();

let (mut rpc_server, taiko) = setup_rpc_server_and_taiko(3030).await;
let json = taiko.get_pending_l2_tx_lists().await.unwrap();
let json = taiko.get_pending_l2_tx_lists().await.unwrap().tx_lists;

assert_eq!(json["TxLists"].as_array().unwrap().len(), 1);
assert_eq!(json["TxLists"][0].as_array().unwrap().len(), 3);
assert_eq!(json["TxLists"][0][0]["type"], "0x0");
assert_eq!(
json["TxLists"][0][0]["hash"],
"0x7c76b9906579e54df54fe77ad1706c47aca706b3eb5cfd8a30ccc3c5a19e8ecd"
);
assert_eq!(json["TxLists"][0][1]["type"], "0x2");
assert_eq!(json.as_array().unwrap().len(), 1);
assert_eq!(json[0].as_array().unwrap().len(), 2);
assert_eq!(json[0][0]["type"], "0x0");
assert_eq!(
json["TxLists"][0][1]["hash"],
"0xece2a3c6ca097cfe5d97aad4e79393240f63865210f9c763703d1136f065298b"
json[0][0]["hash"],
"0xc653e446eafe51eea1f46e6e351adbd1cc8a3271e6935f1441f613a58d441f6a"
);
assert_eq!(json["TxLists"][0][2]["type"], "0x2");
assert_eq!(json[0][1]["type"], "0x2");
assert_eq!(
json["TxLists"][0][2]["hash"],
"0xb105d9f16e8fb913093c8a2c595bf4257328d256f218a05be8dcc626ddeb4193"
json[0][1]["hash"],
"0xffbcd2fab90f1bf314ca2da1bf83eeab3d17fd58a0393d29a697b2ff05d0e65c"
);
rpc_server.stop().await;
}
Expand Down
62 changes: 24 additions & 38 deletions Node/src/utils/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,57 +61,43 @@ pub mod test {
{
"type": "0x0",
"chainId": "0x28c61",
"nonce": "0x1",
"to": "0xbfadd5365bb2890ad832038837115e60b71f7cbb",
"gas": "0x267ac",
"gasPrice": "0x5e76e0800",
"nonce": "0x8836",
"to": "0x8b14d287b4150ff22ac73df8be720e933f659abc",
"gas": "0x35f30",
"gasPrice": "0xf4b87001",
"maxPriorityFeePerGas": null,
"maxFeePerGas": null,
"value": "0x0",
"input": "0x40d097c30000000000000000000000004cea2c7d358e313f5d0287c475f9ae943fe1a913",
"v": "0x518e6",
"r": "0xb22da5cdc4c091ec85d2dda9054aa497088e55bd9f0335f39864ae1c598dd35",
"s": "0x6eee1bcfe6a1855e89dd23d40942c90a036f273159b4c4fd217d58169493f055",
"hash": "0x7c76b9906579e54df54fe77ad1706c47aca706b3eb5cfd8a30ccc3c5a19e8ecd"
"input": "0x3161b7f60000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000027e9000000000000000000000000000000000000000000000005751b6fc9babade290000000000000000000000000000000000000000000000000000000008f811330000000000000000000000000000000000000000000000000000000000000010",
"v": "0x518e5",
"r": "0xb7b4b5540e08775ebb3c077ca7d572378cdb6ed55e3387173f8248578cc073e9",
"s": "0x1f8860f90b61202d4070d1eba494d38c2cb02c749ea1ec542c8d02e5ceeb6439",
"hash": "0xc653e446eafe51eea1f46e6e351adbd1cc8a3271e6935f1441f613a58d441f6a"
},
{
"type": "0x2",
"chainId": "0x28c61",
"nonce": "0x3f",
"to": "0x380a5ba81efe70fe98ab56613ebf9244a2f3d4c9",
"gas": "0x2c2c8",
"nonce": "0x26d0",
"to": "0x2f6ef5baae08ae9df23528c217a77f03f93b690e",
"gas": "0x1a09b",
"gasPrice": null,
"maxPriorityFeePerGas": "0x1",
"maxFeePerGas": "0x3",
"value": "0x5af3107a4000",
"input": "0x3593564c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000006672d0a400000000000000000000000000000000000000000000000000000000000000020b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000005af3107a40000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000005af3107a400000000000000000000000000000000000000000000000000000000353ca3e629a00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002bae2c46ddb314b9ba743c6dee4878f151881333d9000bb8ebf1f662bf092ff0d913a9fe9d7179b0efef1611000000000000000000000000000000000000000000",
"accessList": [],
"v": "0x1",
"r": "0x36517a175a60d3026380318917976fa32c82e542850357a611af05d2212ab9a4",
"s": "0x32d89dce30d76287ddba907b0c662cd09dc30891b1c9c2ef644edfc53160b298",
"yParity": "0x1",
"hash": "0xece2a3c6ca097cfe5d97aad4e79393240f63865210f9c763703d1136f065298b"
},
{
"type": "0x2",
"chainId": "0x28c61",
"nonce": "0x39",
"to": "0x380a5ba81efe70fe98ab56613ebf9244a2f3d4c9",
"gas": "0x2c2c8",
"gasPrice": null,
"maxPriorityFeePerGas": "0x1",
"maxFeePerGas": "0x3",
"value": "0x5af3107a4000",
"input": "0x3593564c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000006672d0d400000000000000000000000000000000000000000000000000000000000000020b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000005af3107a40000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000005af3107a400000000000000000000000000000000000000000000000000000000353ca3e629a00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002bae2c46ddb314b9ba743c6dee4878f151881333d9000bb8ebf1f662bf092ff0d913a9fe9d7179b0efef1611000000000000000000000000000000000000000000",
"maxPriorityFeePerGas": "0xcbef0801",
"maxFeePerGas": "0xcbef0801",
"value": "0x0",
"input": "0xbb2cd728000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000ae2c46ddb314b9ba743c6dee4878f151881333d90000000000000000000000007d16e966c879ed6ad9972ddd5376c41a427d2c2a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000004cd712049c0000000000000000000000000000000000000000000000000000057b3ac2af84",
"accessList": [],
"v": "0x0",
"r": "0xc779421d1ee81dbd3dfbfad5fd632b45303b4513ea1b8ac0bc647f5430cd97b9",
"s": "0x13cedef844bf5a954183182992ffbf9b8b23331de255157528be7da6614618b2",
"r": "0xbfc407896ee6ed36962d1a591b41aa9a17fe19e176e61cb54a1cd69e3a337508",
"s": "0x621b2448dda4d797447935c051e3908f65dfb3729415280a04cb02c4474baefe",
"yParity": "0x0",
"hash": "0xb105d9f16e8fb913093c8a2c595bf4257328d256f218a05be8dcc626ddeb4193"
"hash": "0xffbcd2fab90f1bf314ca2da1bf83eeab3d17fd58a0393d29a697b2ff05d0e65c"
}
]
]
],
"TxListBytes": [
"eJz6ybTuJ6NeU4dZy5cdBYzNzPEGU7pFLrVvEeX/pHXc9se+Ir7J9qmz9jTsOGKYuP0bA36gQECeEb+0+kscEqyl0vknd+26p4lPN8cPQWMC9gs0s0o8XbB9y9YQPo7yuN027DXLrxaZ99zOuxpn3C5u3+QR3nOg+OUC+Y6En9yJCroOBRdfL5lyuUdng07JvIVvQnR6mZ6ee51iuZOxiuknY1kzU09ik9qFltPvORjBRDPjgtlT9PO+7lrHsW7uJ1ONQ+LL65l/WmfyNexkZNmtc12DgPMcCMgvICDPhMxZp+N2d7PIzl0lNrnvPCo+BnYIG99Elq8Ve5l2ovJt1s3puneDy45IOdXqaJFiPhrwuS7EMge3NGu11aH1LQcaFuw/wt6Z9+yt2TRdqUhpx1WzxP9JPix7JrPVS+baPCvjUo4FSdIqHneXXJ/uUml6IPDxhP7U+5uLpohqcLGcZjri7r3uHyAAAP//huiQHQ=="
],
"ParentMetaHash": "0x2bcf3b1bb0c4066aa46ba6e99b79d7602f124d5ae8fcffd2977b1c2138aa61bc"
});
}
}

0 comments on commit 288bd76

Please sign in to comment.