Skip to content

Commit

Permalink
chore(engine): fix wrong log type in deposit decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed May 1, 2024
1 parent 783b18f commit 4ece161
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 38 deletions.
21 changes: 9 additions & 12 deletions src/derive/stages/attributes.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -427,28 +427,25 @@ impl UserDeposited {
}
}

impl TryFrom<Log> for UserDeposited {
impl TryFrom<alloy_rpc_types::Log> for UserDeposited {
type Error = eyre::Report;

/// Converts the emitted L1 deposit event log into [UserDeposited]
fn try_from(log: Log) -> Result<Self, Self::Error> {
let opaque_data = decode(&[ParamType::Bytes], &log.data)?[0]
fn try_from(log: alloy_rpc_types::Log) -> Result<Self, Self::Error> {
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();
Expand All @@ -462,8 +459,8 @@ impl TryFrom<Log> 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(),
})
}
}
45 changes: 19 additions & 26 deletions src/l1/chain_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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::<Vec<UserDeposited>>();

// 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::<Result<Vec<UserDeposited>>>()?;
let deposits = deposit_logs
.iter()
.filter(|d| d.l1_block_num == num)
.cloned()
.collect();

self.deposits.insert(num, deposits);
}
Expand Down

0 comments on commit 4ece161

Please sign in to comment.