Skip to content

Commit

Permalink
chore(l1): migrate types to alloy
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Apr 30, 2024
1 parent ef19167 commit 56677e7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 62 deletions.
6 changes: 3 additions & 3 deletions src/derive/stages/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 10 additions & 6 deletions src/derive/state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{collections::BTreeMap, sync::Arc};

use alloy_primitives::B256;
use ethers::{
providers::{Http, Middleware, Provider},
types::H256,
Expand Down Expand Up @@ -77,7 +76,7 @@ impl State {
pub fn epoch_by_hash(&self, hash: H256) -> Option<Epoch> {
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,
})
}
Expand All @@ -86,7 +85,7 @@ impl State {
pub fn epoch_by_number(&self, num: u64) -> Option<Epoch> {
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,
})
}
Expand All @@ -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();
}
Expand Down
18 changes: 7 additions & 11 deletions src/l1/chain_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<u64> {
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<Block> {
Expand Down Expand Up @@ -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
Expand Down
51 changes: 9 additions & 42 deletions src/l1/l1_info.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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<H256>,
pub parent_beacon_block_root: Option<B256>,
}

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<Self, Self::Error> {
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
Expand All @@ -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<Transaction>> for L1BlockInfo {
type Error = eyre::Error;

fn try_from(value: &Block<Transaction>) -> std::result::Result<Self, Self::Error> {
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,
})
Expand Down

0 comments on commit 56677e7

Please sign in to comment.