Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(l1): Migrate to Alloy #240

Merged
merged 3 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/derive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ impl Pipeline {
}

/// Sends [BatcherTransactions] & the L1 block they were received in to the [BatcherTransactions] receiver.
pub fn push_batcher_transactions(&self, txs: Vec<Bytes>, l1_origin: u64) -> Result<()> {
pub fn push_batcher_transactions(
&self,
txs: Vec<alloy_primitives::Bytes>,
l1_origin: u64,
) -> Result<()> {
let txs = txs.into_iter().map(Bytes::from).collect();
self.batcher_transaction_sender
.send(BatcherTransactionMessage { txs, l1_origin })?;
Ok(())
Expand Down
42 changes: 39 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 Expand Up @@ -392,6 +392,42 @@ pub struct UserDeposited {
pub log_index: U256,
}

impl UserDeposited {
/// Creates a new [UserDeposited] from the given data.
pub fn new(
log: alloy_rpc_types::Log,
l1_block_num: u64,
l1_block_hash: alloy_primitives::B256,
log_index: alloy_primitives::U256,
) -> Result<Self> {
let opaque_data = decode(&[ParamType::Bytes], &log.data().data)?[0]
.clone()
.into_bytes()
.ok_or(eyre::eyre!("invalid data"))?;

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();

Ok(Self {
from,
to,
mint,
value,
gas,
is_creation,
data,
l1_block_num,
l1_block_hash: H256::from_slice(l1_block_hash.as_slice()),
log_index: U256::from_big_endian(&log_index.to_be_bytes::<32>()),
})
}
}

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

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
2 changes: 1 addition & 1 deletion src/l1/blob_encoding.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bytes::Bytes;
use alloy_primitives::Bytes;
use eyre::Result;

const MAX_BLOB_DATA_SIZE: usize = (4 * 31 + 3) * 1024 - 4;
Expand Down
Loading
Loading