From 56677e761fce73c31cf11789dcc480c0feeaa1c1 Mon Sep 17 00:00:00 2001 From: refcell Date: Tue, 30 Apr 2024 09:34:30 -0700 Subject: [PATCH] chore(l1): migrate types to alloy --- src/derive/stages/attributes.rs | 6 ++-- src/derive/state.rs | 16 +++++++---- src/l1/chain_watcher.rs | 18 +++++------- src/l1/l1_info.rs | 51 ++++++--------------------------- 4 files changed, 29 insertions(+), 62 deletions(-) diff --git a/src/derive/stages/attributes.rs b/src/derive/stages/attributes.rs index 6c906d0..bc99287 100644 --- a/src/derive/stages/attributes.rs +++ b/src/derive/stages/attributes.rs @@ -99,7 +99,7 @@ impl Attributes { PayloadAttributes { timestamp, - prev_randao, + prev_randao: H256::from_slice(prev_randao.as_slice()), suggested_fee_recipient: Address::from_slice(suggested_fee_recipient.as_slice()), transactions, no_tx_pool: true, @@ -336,8 +336,8 @@ impl AttributesDeposited { Self { number: l1_info.block_info.number, timestamp: l1_info.block_info.timestamp, - base_fee: l1_info.block_info.base_fee, - hash: l1_info.block_info.hash, + base_fee: U256::from_big_endian(&l1_info.block_info.base_fee.to_be_bytes::<32>()), + hash: H256::from_slice(l1_info.block_info.hash.as_slice()), sequence_number: seq, batcher_hash, fee_overhead, diff --git a/src/derive/state.rs b/src/derive/state.rs index c3d43c2..3cf3ab9 100644 --- a/src/derive/state.rs +++ b/src/derive/state.rs @@ -1,6 +1,5 @@ use std::{collections::BTreeMap, sync::Arc}; -use alloy_primitives::B256; use ethers::{ providers::{Http, Middleware, Provider}, types::H256, @@ -77,7 +76,7 @@ impl State { pub fn epoch_by_hash(&self, hash: H256) -> Option { self.l1_info_by_hash(hash).map(|info| Epoch { number: info.block_info.number, - hash: B256::from_slice(info.block_info.hash.as_bytes()), + hash: info.block_info.hash, timestamp: info.block_info.timestamp, }) } @@ -86,7 +85,7 @@ impl State { pub fn epoch_by_number(&self, num: u64) -> Option { self.l1_info_by_number(num).map(|info| Epoch { number: info.block_info.number, - hash: B256::from_slice(info.block_info.hash.as_bytes()), + hash: info.block_info.hash, timestamp: info.block_info.timestamp, }) } @@ -97,9 +96,14 @@ impl State { pub fn update_l1_info(&mut self, l1_info: L1Info) { self.current_epoch_num = l1_info.block_info.number; - self.l1_hashes - .insert(l1_info.block_info.number, l1_info.block_info.hash); - self.l1_info.insert(l1_info.block_info.hash, l1_info); + self.l1_hashes.insert( + l1_info.block_info.number, + H256::from_slice(l1_info.block_info.hash.as_slice()), + ); + self.l1_info.insert( + H256::from_slice(l1_info.block_info.hash.as_slice()), + l1_info, + ); self.prune(); } diff --git a/src/l1/chain_watcher.rs b/src/l1/chain_watcher.rs index f797d6b..6f9ff2f 100644 --- a/src/l1/chain_watcher.rs +++ b/src/l1/chain_watcher.rs @@ -269,7 +269,7 @@ impl InnerWatcher { if l1_info.block_info.number >= self.finalized_block { let block_info = BlockInfo { - hash: B256::from_slice(l1_info.block_info.hash.as_bytes()), + hash: l1_info.block_info.hash, number: l1_info.block_info.number, timestamp: l1_info.block_info.timestamp, parent_hash: B256::from_slice(block.header.parent_hash.as_slice()), @@ -331,7 +331,7 @@ impl InnerWatcher { } } - let update_block: u64 = update_block.try_into()?; + let update_block: u64 = update_block; self.system_config_update = (update_block, Some(config)); } else { self.system_config_update = (to_block, None); @@ -368,27 +368,23 @@ impl InnerWatcher { true => BlockNumberOrTag::Latest, }; - Ok(self - .provider + self.provider .get_block(BlockId::Number(block_number), false) .await? .ok_or(eyre::eyre!("block not found"))? .header .number - .ok_or(eyre::eyre!("block pending"))? - .try_into()?) + .ok_or(eyre::eyre!("block pending")) } async fn get_head(&self) -> Result { - Ok(self - .provider + self.provider .get_block(BlockId::Number(BlockNumberOrTag::Latest), false) .await? .ok_or(eyre::eyre!("block not found"))? .header .number - .ok_or(eyre::eyre!("block pending"))? - .try_into()?) + .ok_or(eyre::eyre!("block pending")) } async fn get_block(&self, block_num: u64) -> Result { @@ -484,7 +480,7 @@ impl InnerWatcher { return Ok(batcher_transactions_data); } - let timestamp: u64 = block.header.timestamp.try_into()?; + let timestamp: u64 = block.header.timestamp; let slot = self.blob_fetcher.get_slot_from_time(timestamp).await?; // perf: fetch only the required indexes instead of all diff --git a/src/l1/l1_info.rs b/src/l1/l1_info.rs index 483471c..794415e 100644 --- a/src/l1/l1_info.rs +++ b/src/l1/l1_info.rs @@ -1,4 +1,5 @@ -use ethers::types::{Block, Transaction, H256, U256}; +use alloy_primitives::{B256, U256}; +use alloy_rpc_types::Block; use crate::{config::SystemConfig, derive::stages::attributes::UserDeposited}; @@ -25,30 +26,29 @@ pub struct L1BlockInfo { /// L1 block number pub number: u64, /// L1 block hash - pub hash: H256, + pub hash: B256, /// L1 block timestamp pub timestamp: u64, /// L1 base fee per gas pub base_fee: U256, /// L1 mix hash (prevrandao) - pub mix_hash: H256, + pub mix_hash: B256, /// Post-Ecotone beacon block root - pub parent_beacon_block_root: Option, + pub parent_beacon_block_root: Option, } -impl TryFrom<&alloy_rpc_types::Block> for L1BlockInfo { +impl TryFrom<&Block> for L1BlockInfo { type Error = eyre::Error; fn try_from(value: &alloy_rpc_types::Block) -> std::result::Result { let number = value .header .number - .ok_or(eyre::eyre!("block not included"))? - .try_into()?; + .ok_or(eyre::eyre!("block not included"))?; let hash = value.header.hash.ok_or(eyre::eyre!("block not included"))?; - let timestamp = value.header.timestamp.try_into()?; + let timestamp = value.header.timestamp; let base_fee = value .header @@ -62,44 +62,11 @@ impl TryFrom<&alloy_rpc_types::Block> for L1BlockInfo { let parent_beacon_block_root = value.header.parent_beacon_block_root; - Ok(L1BlockInfo { - number, - hash: H256::from_slice(&hash.as_slice()), - timestamp, - base_fee: base_fee.into(), - mix_hash: H256::from_slice(&mix_hash.as_slice()), - parent_beacon_block_root: parent_beacon_block_root - .map(|x| H256::from_slice(&x.as_slice())), - }) - } -} - -impl TryFrom<&Block> for L1BlockInfo { - type Error = eyre::Error; - - fn try_from(value: &Block) -> std::result::Result { - let number = value - .number - .ok_or(eyre::eyre!("block not included"))? - .as_u64(); - - let hash = value.hash.ok_or(eyre::eyre!("block not included"))?; - - let timestamp = value.timestamp.as_u64(); - - let base_fee = value - .base_fee_per_gas - .ok_or(eyre::eyre!("block is pre london"))?; - - let mix_hash = value.mix_hash.ok_or(eyre::eyre!("block not included"))?; - - let parent_beacon_block_root = value.parent_beacon_block_root; - Ok(L1BlockInfo { number, hash, timestamp, - base_fee, + base_fee: U256::from(base_fee), mix_hash, parent_beacon_block_root, })