From f3f6f5c5cd7ff0b31999bdc5bba152894cd2728e Mon Sep 17 00:00:00 2001 From: Roman Krasiuk Date: Fri, 21 Apr 2023 14:00:41 +0300 Subject: [PATCH 1/2] perf(l1): avoid event topic rehashing --- Cargo.lock | 1 + Cargo.toml | 1 + src/l1/mod.rs | 18 +++++++++++------- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 237d2938..45c7e201 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1795,6 +1795,7 @@ dependencies = [ "jsonwebtoken", "lazy_static", "libflate", + "once_cell", "openssl", "prometheus_exporter", "rand", diff --git a/Cargo.toml b/Cargo.toml index dc01ae16..63e59ee0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ ethers= "1.0.2" hex = "0.4.3" libflate = "1.2.0" openssl = { version = "0.10", features = ["vendored"] } +once_cell = "1" # Logging and Metrics chrono = "0.4.22" diff --git a/src/l1/mod.rs b/src/l1/mod.rs index 31dbef4a..09eafe54 100644 --- a/src/l1/mod.rs +++ b/src/l1/mod.rs @@ -18,6 +18,7 @@ use ethers::{ }; use eyre::Result; +use once_cell::sync::Lazy; use tokio::{spawn, task::JoinHandle, time::sleep}; use crate::{ @@ -170,6 +171,14 @@ impl ChainWatcher { } impl InnerWatcher { + const CONFIG_UPDATE_TOPIC: Lazy = + Lazy::new(|| H256::from_slice(&keccak256("ConfigUpdate(uint256,uint8,bytes)"))); + const TRANSACTION_DEPOSITED_TOPIC: Lazy = Lazy::new(|| { + H256::from_slice(&keccak256( + "TransactionDeposited(address,address,uint256,bytes)", + )) + }); + async fn new( config: Arc, block_update_sender: SyncSender, @@ -284,11 +293,9 @@ impl InnerWatcher { if last_update_block < self.current_block { let to_block = last_update_block + 1000; - let update_event = "ConfigUpdate(uint256,uint8,bytes)"; - let update_topic = H256::from_slice(&keccak256(update_event)); let filter = Filter::new() .address(self.config.chain.system_config_contract) - .topic0(update_topic) + .topic0(*Self::CONFIG_UPDATE_TOPIC) .from_block(last_update_block + 1) .to_block(to_block); @@ -376,14 +383,11 @@ impl InnerWatcher { match self.deposits.remove(&block_num) { Some(deposits) => Ok(deposits), None => { - let deposit_event = "TransactionDeposited(address,address,uint256,bytes)"; - let deposit_topic = H256::from_slice(&keccak256(deposit_event)); - let end_block = self.head_block.min(block_num + 1000); let deposit_filter = Filter::new() .address(self.config.chain.deposit_contract) - .topic0(deposit_topic) + .topic0(*Self::TRANSACTION_DEPOSITED_TOPIC) .from_block(block_num) .to_block(end_block); From 0947d80c1e716f0c1e2f6bd78b76535d88a6e8a2 Mon Sep 17 00:00:00 2001 From: Roman Krasiuk Date: Fri, 21 Apr 2023 17:48:29 +0300 Subject: [PATCH 2/2] clippy --- src/l1/mod.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/l1/mod.rs b/src/l1/mod.rs index 09eafe54..d511c6cf 100644 --- a/src/l1/mod.rs +++ b/src/l1/mod.rs @@ -27,6 +27,15 @@ use crate::{ derive::stages::attributes::UserDeposited, }; +static CONFIG_UPDATE_TOPIC: Lazy = + Lazy::new(|| H256::from_slice(&keccak256("ConfigUpdate(uint256,uint8,bytes)"))); + +static TRANSACTION_DEPOSITED_TOPIC: Lazy = Lazy::new(|| { + H256::from_slice(&keccak256( + "TransactionDeposited(address,address,uint256,bytes)", + )) +}); + /// Handles watching the L1 chain and monitoring for new blocks, deposits, /// and batcher transactions. The monitoring loop is spawned in a seperate /// task and communication happens via the internal channels. When ChainWatcher @@ -171,14 +180,6 @@ impl ChainWatcher { } impl InnerWatcher { - const CONFIG_UPDATE_TOPIC: Lazy = - Lazy::new(|| H256::from_slice(&keccak256("ConfigUpdate(uint256,uint8,bytes)"))); - const TRANSACTION_DEPOSITED_TOPIC: Lazy = Lazy::new(|| { - H256::from_slice(&keccak256( - "TransactionDeposited(address,address,uint256,bytes)", - )) - }); - async fn new( config: Arc, block_update_sender: SyncSender, @@ -295,7 +296,7 @@ impl InnerWatcher { let to_block = last_update_block + 1000; let filter = Filter::new() .address(self.config.chain.system_config_contract) - .topic0(*Self::CONFIG_UPDATE_TOPIC) + .topic0(*CONFIG_UPDATE_TOPIC) .from_block(last_update_block + 1) .to_block(to_block); @@ -387,7 +388,7 @@ impl InnerWatcher { let deposit_filter = Filter::new() .address(self.config.chain.deposit_contract) - .topic0(*Self::TRANSACTION_DEPOSITED_TOPIC) + .topic0(*TRANSACTION_DEPOSITED_TOPIC) .from_block(block_num) .to_block(end_block);