From 4ece1615db124d31aa7db79a854d443a65d15d30 Mon Sep 17 00:00:00 2001 From: refcell Date: Wed, 1 May 2024 07:35:54 -0700 Subject: [PATCH] chore(engine): fix wrong log type in deposit decoding --- src/derive/stages/attributes.rs | 21 +++++++-------- src/l1/chain_watcher.rs | 45 ++++++++++++++------------------- 2 files changed, 28 insertions(+), 38 deletions(-) diff --git a/src/derive/stages/attributes.rs b/src/derive/stages/attributes.rs index bc1685e..617c6a6 100644 --- a/src/derive/stages/attributes.rs +++ b/src/derive/stages/attributes.rs @@ -1,7 +1,7 @@ use std::sync::{Arc, RwLock}; use ethers::abi::{decode, encode, ParamType, Token}; -use ethers::types::{Address, Log, H256, U256}; +use ethers::types::{Address, H256, U256}; use ethers::utils::{keccak256, rlp::Encodable, rlp::RlpStream}; use eyre::Result; @@ -427,28 +427,25 @@ impl UserDeposited { } } -impl TryFrom for UserDeposited { +impl TryFrom for UserDeposited { type Error = eyre::Report; /// Converts the emitted L1 deposit event log into [UserDeposited] - fn try_from(log: Log) -> Result { - let opaque_data = decode(&[ParamType::Bytes], &log.data)?[0] + fn try_from(log: alloy_rpc_types::Log) -> Result { + let opaque_data = decode(&[ParamType::Bytes], &log.data().data)?[0] .clone() .into_bytes() .unwrap(); - let from = Address::from(log.topics[1]); - let to = Address::from(log.topics[2]); + let from = Address::from_slice(log.topics()[1].as_slice()); + let to = Address::from_slice(log.topics()[2].as_slice()); let mint = U256::from_big_endian(&opaque_data[0..32]); let value = U256::from_big_endian(&opaque_data[32..64]); let gas = u64::from_be_bytes(opaque_data[64..72].try_into()?); let is_creation = opaque_data[72] != 0; let data = opaque_data[73..].to_vec(); - let l1_block_num = log - .block_number - .ok_or(eyre::eyre!("block num not found"))? - .as_u64(); + let l1_block_num = log.block_number.ok_or(eyre::eyre!("block num not found"))?; let l1_block_hash = log.block_hash.ok_or(eyre::eyre!("block hash not found"))?; let log_index = log.log_index.unwrap(); @@ -462,8 +459,8 @@ impl TryFrom for UserDeposited { is_creation, data, l1_block_num, - l1_block_hash, - log_index, + l1_block_hash: H256::from_slice(l1_block_hash.as_slice()), + log_index: log_index.into(), }) } } diff --git a/src/l1/chain_watcher.rs b/src/l1/chain_watcher.rs index 3ae0665..90a7b36 100644 --- a/src/l1/chain_watcher.rs +++ b/src/l1/chain_watcher.rs @@ -4,7 +4,7 @@ use eyre::Result; use once_cell::sync::Lazy; use tokio::{spawn, sync::mpsc, task::JoinHandle, time::sleep}; -use alloy_primitives::{keccak256, Address, Bytes, B256, U256}; +use alloy_primitives::{keccak256, Address, Bytes, B256}; use alloy_provider::{Provider, ProviderBuilder, ReqwestProvider}; use alloy_rpc_types::{Block, BlockId, BlockNumberOrTag, BlockTransactions, Filter, Transaction}; @@ -397,33 +397,26 @@ impl InnerWatcher { Some(deposits) => Ok(deposits), None => { let end_block = self.head_block.min(block_num + 1000); + let deposit_filter = Filter::new() + .address(self.config.chain.deposit_contract) + .event_signature(*TRANSACTION_DEPOSITED_TOPIC) + .from_block(block_num) + .to_block(end_block); + + let deposit_logs = self + .provider + .get_logs(&deposit_filter) + .await? + .into_iter() + .map(|log| UserDeposited::try_from(log).unwrap()) + .collect::>(); - // TODO: Optimize this to batch requests. - // Right now we need to do this since logs don't contain the block data. for num in block_num..=end_block { - let deposit_filter = Filter::new() - .address(self.config.chain.deposit_contract) - .event_signature(*TRANSACTION_DEPOSITED_TOPIC) - .select(num); - - let block = self - .provider - .get_block(BlockId::Number(BlockNumberOrTag::Number(block_num)), false) - .await? - .ok_or(eyre::eyre!("block not found"))?; - let block_hash = block - .header - .hash - .ok_or(eyre::eyre!("block hash not found"))?; - - let deposits = self - .provider - .get_logs(&deposit_filter) - .await? - .into_iter() - .enumerate() - .map(|(i, log)| UserDeposited::new(log, num, block_hash, U256::from(i))) - .collect::>>()?; + let deposits = deposit_logs + .iter() + .filter(|d| d.l1_block_num == num) + .cloned() + .collect(); self.deposits.insert(num, deposits); }