Skip to content

Commit

Permalink
fix: more alloy migration
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Apr 29, 2024
1 parent e52eceb commit 3803e1d
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 133 deletions.
25 changes: 13 additions & 12 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::fmt::Debug;

use alloy_primitives::B256;
use ethers::{
types::{Block, Transaction, H256},
types::{Block, Transaction},
utils::rlp::{Decodable, DecoderError, Rlp},
};
use eyre::Result;
Expand All @@ -18,11 +19,11 @@ pub use attributes_deposited::AttributesDepositedCall;
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default, Serialize, Deserialize)]
pub struct BlockInfo {
/// The block hash
pub hash: H256,
pub hash: B256,
/// The block number
pub number: u64,
/// The parent block hash
pub parent_hash: H256,
pub parent_hash: B256,
/// The block timestamp
pub timestamp: u64,
}
Expand All @@ -37,20 +38,20 @@ pub struct Epoch {
/// The block number
pub number: u64,
/// The block hash
pub hash: H256,
pub hash: B256,
/// The block timestamp
pub timestamp: u64,
}

impl From<BlockInfo> for Value {
fn from(value: BlockInfo) -> Value {
let mut dict = Dict::new();
dict.insert("hash".to_string(), Value::from(value.hash.as_bytes()));
dict.insert("hash".to_string(), Value::from(value.hash.as_slice()));
dict.insert("number".to_string(), Value::from(value.number));
dict.insert("timestamp".to_string(), Value::from(value.timestamp));
dict.insert(
"parent_hash".to_string(),
Value::from(value.parent_hash.as_bytes()),
Value::from(value.parent_hash.as_slice()),
);
Value::Dict(Tag::Default, dict)
}
Expand All @@ -70,8 +71,8 @@ impl TryFrom<Block<Transaction>> for BlockInfo {

Ok(BlockInfo {
number,
hash,
parent_hash: block.parent_hash,
hash: B256::from_slice(hash.as_bytes()),
parent_hash: B256::from_slice(block.parent_hash.as_bytes()),
timestamp: block.timestamp.as_u64(),
})
}
Expand All @@ -80,7 +81,7 @@ impl TryFrom<Block<Transaction>> for BlockInfo {
impl From<Epoch> for Value {
fn from(value: Epoch) -> Self {
let mut dict = Dict::new();
dict.insert("hash".to_string(), Value::from(value.hash.as_bytes()));
dict.insert("hash".to_string(), Value::from(value.hash.as_slice()));
dict.insert("number".to_string(), Value::from(value.number));
dict.insert("timestamp".to_string(), Value::from(value.timestamp));
Value::Dict(Tag::Default, dict)
Expand All @@ -92,8 +93,8 @@ impl From<&ExecutionPayload> for BlockInfo {
fn from(value: &ExecutionPayload) -> Self {
Self {
number: value.block_number.as_u64(),
hash: value.block_hash,
parent_hash: value.parent_hash,
hash: B256::from_slice(value.block_hash.as_bytes()),
parent_hash: B256::from_slice(value.parent_hash.as_bytes()),
timestamp: value.timestamp.as_u64(),
}
}
Expand All @@ -105,7 +106,7 @@ impl From<&AttributesDepositedCall> for Epoch {
Self {
number: call.number,
timestamp: call.timestamp,
hash: H256::from_slice(call.hash.as_slice()),
hash: B256::from_slice(call.hash.as_slice()),
}
}
}
Expand Down
70 changes: 28 additions & 42 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,12 @@ pub struct SystemConfig {
}

impl SystemConfig {
/// Encodes batch sender as a H256
pub fn batcher_hash(&self) -> H256 {
let mut batch_sender_bytes = self.batch_sender.as_bytes().to_vec();
/// Encodes batch sender as a [B256].
pub fn batcher_hash(&self) -> B256 {
let mut batch_sender_bytes = self.batch_sender.as_slice().to_vec();
let mut batcher_hash = iter::repeat(0).take(12).collect::<Vec<_>>();
batcher_hash.append(&mut batch_sender_bytes);
H256::from_slice(&batcher_hash)
B256::from_slice(&batcher_hash)
}
}

Expand Down Expand Up @@ -667,14 +667,14 @@ impl From<ExternalChainConfig> for ChainConfig {
l2_genesis: BlockInfo {
hash: external.genesis.l2.hash,
number: external.genesis.l2.number,
parent_hash: H256::zero(),
parent_hash: B256::ZERO,
timestamp: external.genesis.l2_time,
},
system_config: SystemConfig {
batch_sender: external.genesis.system_config.batcher_addr,
gas_limit: U256::from(external.genesis.system_config.gas_limit),
l1_fee_overhead: external.genesis.system_config.overhead.0.into(),
l1_fee_scalar: external.genesis.system_config.scalar.0.into(),
l1_fee_overhead: external.genesis.system_config.overhead.try_into().unwrap(),

Check failure on line 676 in src/config/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

use of a fallible conversion when an infallible one could be used
l1_fee_scalar: external.genesis.system_config.scalar.try_into().unwrap(),

Check failure on line 677 in src/config/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

use of a fallible conversion when an infallible one could be used
unsafe_block_signer: Address::ZERO,
},
batch_inbox: external.batch_inbox_address,
Expand All @@ -698,18 +698,6 @@ impl From<ChainConfig> for ExternalChainConfig {
/// Converts [ChainConfig] into [ExternalChainConfig]
/// which is the format used in ``rollup.json`` by `op-node`
fn from(chain_config: ChainConfig) -> Self {
let mut overhead = [0; 32];
let mut scalar = [0; 32];

chain_config
.system_config
.l1_fee_overhead
.to_big_endian(&mut overhead);
chain_config
.system_config
.l1_fee_scalar
.to_big_endian(&mut scalar);

Self {
genesis: ExternalGenesisInfo {
l1: ChainGenesisInfo {
Expand All @@ -723,8 +711,8 @@ impl From<ChainConfig> for ExternalChainConfig {
l2_time: chain_config.l2_genesis.timestamp,
system_config: SystemConfigInfo {
batcher_addr: chain_config.system_config.batch_sender,
overhead: B256::from_slice(&overhead),
scalar: B256::from_slice(&scalar),
overhead: chain_config.system_config.l1_fee_overhead.into(),
scalar: chain_config.system_config.l1_fee_scalar.into(),
gas_limit: chain_config.system_config.gas_limit.try_into().unwrap(),
},
},
Expand Down Expand Up @@ -809,31 +797,29 @@ mod test {
chain_config.system_config.batch_sender
);

let mut overhead = [0; 32];
let mut scalar = [0; 32];

chain_config
let overhead: U256 = external_config
.genesis
.system_config
.overhead
.try_into()
.unwrap();
let scalar: U256 = external_config
.genesis
.system_config
.l1_fee_overhead
.to_be_slice(&mut overhead);
chain_config
.scalar
.try_into()
.unwrap();
let gas_limit: U256 = external_config
.genesis
.system_config
.l1_fee_scalar
.to_big_endian(&mut scalar);
.gas_limit
.try_into()
.unwrap();

assert_eq!(
external_config.genesis.system_config.overhead,
B256::from_slice(&overhead),
);
assert_eq!(
external_config.genesis.system_config.scalar,
B256::from_slice(&scalar),
);
assert_eq!(chain_config.system_config.l1_fee_overhead, overhead,);
assert_eq!(chain_config.system_config.l1_fee_scalar, scalar,);

assert_eq!(
external_config.genesis.system_config.gas_limit,
chain_config.system_config.gas_limit.try_into().unwrap()
);
assert_eq!(chain_config.system_config.gas_limit, gas_limit,);
}

#[test]
Expand Down
27 changes: 16 additions & 11 deletions src/derive/stages/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ impl PurgeableIterator for Attributes {
fn purge(&mut self) {
self.block_input_iter.purge();
self.sequence_number = 0;
self.epoch_hash = self.state.read().unwrap().safe_epoch.hash;
self.epoch_hash =
ethers::types::H256::from_slice(self.state.read().unwrap().safe_epoch.hash.as_slice());
}
}

Expand All @@ -64,7 +65,7 @@ impl Attributes {
block_input_iter,
state,
sequence_number: seq,
epoch_hash,
epoch_hash: H256::from_slice(epoch_hash.as_slice()),
config,
}
}
Expand All @@ -76,10 +77,11 @@ impl Attributes {
tracing::debug!("deriving attributes from block: {}", input.epoch.number);
tracing::debug!("batch epoch hash: {:?}", input.epoch.hash);

self.update_sequence_number(input.epoch.hash);
let epoch_hash = H256::from_slice(input.epoch.hash.as_slice());
self.update_sequence_number(epoch_hash);

let state = self.state.read().unwrap();
let l1_info = state.l1_info_by_hash(input.epoch.hash).unwrap();
let l1_info = state.l1_info_by_hash(epoch_hash).unwrap();

let withdrawals = if input.timestamp >= self.config.chain.canyon_time {
Some(Vec::new())
Expand All @@ -98,10 +100,10 @@ impl Attributes {
PayloadAttributes {
timestamp,
prev_randao,
suggested_fee_recipient,
suggested_fee_recipient: Address::from_slice(suggested_fee_recipient.as_slice()),
transactions,
no_tx_pool: true,
gas_limit: U64([l1_info.system_config.gas_limit.as_u64()]),
gas_limit: U64([l1_info.system_config.gas_limit.try_into().unwrap()]),
withdrawals,
epoch,
l1_inclusion_block,
Expand Down Expand Up @@ -233,8 +235,8 @@ impl From<AttributesDeposited> for DepositedTransaction {

Self {
source_hash,
from,
to,
from: Address::from_slice(from.as_slice()),
to: to.map(|t| Address::from_slice(t.as_slice())),
mint: U256::zero(),
value: U256::zero(),
gas: attributes_deposited.gas,
Expand Down Expand Up @@ -328,15 +330,18 @@ impl AttributesDeposited {

let gas = if is_regolith { 1_000_000 } else { 150_000_000 };

let batcher_hash = H256::from_slice(l1_info.system_config.batcher_hash().as_slice());
let fee_overhead = U256::from(l1_info.system_config.l1_fee_overhead.to_be_bytes());
let fee_scalar = U256::from(l1_info.system_config.l1_fee_scalar.to_be_bytes());
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,
sequence_number: seq,
batcher_hash: l1_info.system_config.batcher_hash(),
fee_overhead: l1_info.system_config.l1_fee_overhead,
fee_scalar: l1_info.system_config.l1_fee_scalar,
batcher_hash,
fee_overhead,
fee_scalar,
gas,
is_system_tx,
}
Expand Down
9 changes: 5 additions & 4 deletions src/derive/stages/batches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ where
}

// check that block builds on existing chain
if batch.parent_hash != head.hash {
if alloy_primitives::B256::from_slice(batch.parent_hash.as_bytes()) != head.hash {
tracing::warn!("invalid parent hash");
return BatchStatus::Drop;
}
Expand All @@ -226,7 +226,8 @@ where
};

if let Some(batch_origin) = batch_origin {
if batch.epoch_hash != batch_origin.hash {
if alloy_primitives::B256::from_slice(batch.epoch_hash.as_bytes()) != batch_origin.hash
{
tracing::warn!("invalid epoch hash");
return BatchStatus::Drop;
}
Expand Down Expand Up @@ -323,7 +324,7 @@ where

// check that block builds on existing chain

if prev_l2_block.hash.as_bytes()[..20] != batch.parent_check {
if prev_l2_block.hash.as_slice()[..20] != batch.parent_check {
tracing::warn!("batch parent check failed");
return BatchStatus::Drop;
}
Expand All @@ -341,7 +342,7 @@ where
}

if let Some(l1_origin) = state.epoch_by_number(end_epoch_num) {
if batch.l1_origin_check != l1_origin.hash.as_bytes()[..20] {
if batch.l1_origin_check != l1_origin.hash.as_slice()[..20] {
tracing::warn!("origin check failed");
return BatchStatus::Drop;
}
Expand Down
5 changes: 3 additions & 2 deletions src/derive/state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{collections::BTreeMap, sync::Arc};

use alloy_primitives::B256;
use ethers::{
providers::{Http, Middleware, Provider},
types::H256,
Expand Down Expand Up @@ -76,7 +77,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: info.block_info.hash,
hash: B256::from_slice(info.block_info.hash.as_bytes()),
timestamp: info.block_info.timestamp,
})
}
Expand All @@ -85,7 +86,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: info.block_info.hash,
hash: B256::from_slice(info.block_info.hash.as_bytes()),
timestamp: info.block_info.timestamp,
})
}
Expand Down
11 changes: 6 additions & 5 deletions src/driver/engine_driver.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;

use alloy_primitives::B256;
use ethers::providers::{Http, Middleware, Provider};
use ethers::types::Transaction;
use ethers::{
Expand Down Expand Up @@ -100,8 +101,8 @@ impl<E: Engine> EngineDriver<E> {

let new_head = BlockInfo {
number: payload.block_number.as_u64(),
hash: payload.block_hash,
parent_hash: payload.parent_hash,
hash: B256::from_slice(payload.block_hash.as_bytes()),
parent_hash: B256::from_slice(payload.parent_hash.as_bytes()),
timestamp: payload.timestamp.as_u64(),
};

Expand Down Expand Up @@ -198,9 +199,9 @@ impl<E: Engine> EngineDriver<E> {
/// - `finalized_block` = `finalized_head`
fn create_forkchoice_state(&self) -> ForkchoiceState {
ForkchoiceState {
head_block_hash: self.unsafe_head.hash,
safe_block_hash: self.safe_head.hash,
finalized_block_hash: self.finalized_head.hash,
head_block_hash: H256::from_slice(self.unsafe_head.hash.as_slice()),
safe_block_hash: H256::from_slice(self.safe_head.hash.as_slice()),
finalized_block_hash: H256::from_slice(self.finalized_head.hash.as_slice()),
}
}

Expand Down
Loading

0 comments on commit 3803e1d

Please sign in to comment.