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(config): Migrate more types to Alloy #226

Merged
merged 1 commit into from
Apr 29, 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
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ futures-timer = "0.3.0"
again = "0.1"

# Alloy types
alloy-primitives = { version = "0.7.1", default-features = false }
alloy-primitives = { version = "0.7.1", default-features = false, features = ["serde"] }

# Logging and Metrics
chrono = "0.4.22"
Expand Down
6 changes: 3 additions & 3 deletions src/common/attributes_deposited.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ impl AttributesDepositedCall {
let hash = B256::from_slice(&calldata[cursor..cursor + 32]);
cursor += 32;

let seq_num = U256::from_be_slice(calldata[cursor..cursor + 32].try_into()?);
let sequence_number = U256::from_be_slice(calldata[cursor..cursor + 32].try_into()?);
// down-casting to u64 is safe for the sequence number
let seq_num = seq_num
let sequence_number = sequence_number
.try_into()
.map_err(|_| eyre::eyre!("invalid sequence number"))?;
cursor += 32;
Expand All @@ -116,7 +116,7 @@ impl AttributesDepositedCall {
timestamp,
basefee,
hash,
sequence_number: seq_num,
sequence_number,
batcher_hash,
fee_overhead,
fee_scalar,
Expand Down
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
92 changes: 39 additions & 53 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{fmt, iter, path::PathBuf, process::exit, str::FromStr};

use ethers::types::{Address, H256, U256};
use alloy_primitives::{Address, B256, U256};
use figment::{
providers::{Format, Serialized, Toml},
Figment,
Expand Down 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 @@ -445,7 +445,7 @@ impl ChainConfig {
l2_genesis: BlockInfo {
hash: hash("0xf712aa9241cc24369b143cf6dce85f0902a9731e70d66818a3a5845b296c73dd"),
number: 0,
parent_hash: H256::zero(),
parent_hash: B256::ZERO,
timestamp: 1686789347,
},
system_config: SystemConfig {
Expand Down Expand Up @@ -485,7 +485,7 @@ impl ChainConfig {
l2_genesis: BlockInfo {
hash: hash("0xa3ab140f15ea7f7443a4702da64c10314eb04d488e72974e02e2d728096b4f76"),
number: 0,
parent_hash: H256::zero(),
parent_hash: B256::ZERO,
timestamp: 1675193616,
},
system_config: SystemConfig {
Expand Down Expand Up @@ -525,7 +525,7 @@ impl ChainConfig {
l2_genesis: BlockInfo {
hash: hash("0x0dcc9e089e30b90ddfc55be9a37dd15bc551aeee999d2e2b51414c54eaf934e4"),
number: 0,
parent_hash: H256::zero(),
parent_hash: B256::ZERO,
timestamp: 1695768288,
},
system_config: SystemConfig {
Expand Down Expand Up @@ -569,8 +569,8 @@ fn addr(s: &str) -> Address {
}

/// Converts a [str] to a [H256].
fn hash(s: &str) -> H256 {
H256::from_str(s).unwrap()
fn hash(s: &str) -> B256 {
B256::from_str(s).unwrap()
}

/// Returns default blocktime of 2 (seconds).
Expand Down Expand Up @@ -635,9 +635,9 @@ struct SystemConfigInfo {
#[serde(rename = "batcherAddr")]
batcher_addr: Address,
/// The current L1 fee overhead to apply to L2 transactions cost computation. Unused after Ecotone hard fork.
overhead: H256,
overhead: B256,
/// The current L1 fee scalar to apply to L2 transactions cost computation. Unused after Ecotone hard fork.
scalar: H256,
scalar: B256,
/// The gas limit for L2 blocks
#[serde(rename = "gasLimit")]
gas_limit: u64,
Expand All @@ -647,7 +647,7 @@ struct SystemConfigInfo {
#[derive(Debug, Clone, Serialize, Deserialize)]
struct ChainGenesisInfo {
/// Genesis block number
hash: H256,
hash: B256,
/// Genesis block hash
number: u64,
}
Expand All @@ -667,15 +667,15 @@ 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(),
unsafe_block_signer: Address::zero(),
l1_fee_overhead: external.genesis.system_config.overhead.into(),
l1_fee_scalar: external.genesis.system_config.scalar.into(),
unsafe_block_signer: Address::ZERO,
},
batch_inbox: external.batch_inbox_address,
deposit_contract: external.deposit_contract_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,9 +711,9 @@ impl From<ChainConfig> for ExternalChainConfig {
l2_time: chain_config.l2_genesis.timestamp,
system_config: SystemConfigInfo {
batcher_addr: chain_config.system_config.batch_sender,
overhead: H256::from_slice(&overhead),
scalar: H256::from_slice(&scalar),
gas_limit: chain_config.system_config.gas_limit.as_u64(),
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(),
},
},
block_time: chain_config.blocktime,
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_big_endian(&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,
H256::from_slice(&overhead),
);
assert_eq!(
external_config.genesis.system_config.scalar,
H256::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.as_u64()
);
assert_eq!(chain_config.system_config.gas_limit, gas_limit,);
}

#[test]
Expand Down
Loading
Loading