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(common): Migrate AttributesDeposited to Alloy Types #225

Merged
merged 4 commits 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
51 changes: 51 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ futures = "0.3.28"
futures-timer = "0.3.0"
again = "0.1"

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

# Logging and Metrics
chrono = "0.4.22"
tracing = "0.1.36"
Expand Down
59 changes: 33 additions & 26 deletions src/common/attributes_deposited.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use ethers::{
types::{Bytes, H256, U256},
utils::keccak256,
};
use eyre::Result;
use lazy_static::lazy_static;

use alloy_primitives::{keccak256, Bytes, B256, U256};

/// Represents the attributes deposited transcation call
#[derive(Debug)]
pub struct AttributesDepositedCall {
Expand All @@ -15,11 +13,11 @@ pub struct AttributesDepositedCall {
/// base fee
pub basefee: U256,
/// block hash
pub hash: H256,
pub hash: B256,
/// sequence number of the L2 block
pub sequence_number: u64,
/// batcher hash (should contain an address)
pub batcher_hash: H256,
pub batcher_hash: B256,
/// L1 fee overhead
pub fee_overhead: U256,
/// L1 fee scalar
Expand Down Expand Up @@ -78,38 +76,47 @@ impl AttributesDepositedCall {
}
cursor += 4;

let number = U256::from_big_endian(calldata[cursor..cursor + 32].try_into()?);
let number = number.as_u64(); // down-casting to u64 is safe for the block number
let number = U256::from_be_slice(calldata[cursor..cursor + 32].try_into()?);
// down-casting to u64 is safe for the block number
let number = number
.try_into()
.map_err(|_| eyre::eyre!("invalid block number"))?;
cursor += 32;

let timestamp = U256::from_big_endian(calldata[cursor..cursor + 32].try_into()?);
let timestamp = timestamp.as_u64(); // down-casting to u64 is safe for UNIX timestamp
let timestamp = U256::from_be_slice(calldata[cursor..cursor + 32].try_into()?);
// down-casting to u64 is safe for UNIX timestamp
let timestamp = timestamp
.try_into()
.map_err(|_| eyre::eyre!("invalid timestamp"))?;
cursor += 32;

let basefee = U256::from_big_endian(&calldata[cursor..cursor + 32]);
let basefee = U256::from_be_slice(&calldata[cursor..cursor + 32]);
cursor += 32;

let hash = H256::from_slice(&calldata[cursor..cursor + 32]);
let hash = B256::from_slice(&calldata[cursor..cursor + 32]);
cursor += 32;

let sequence_number = U256::from_big_endian(calldata[cursor..cursor + 32].try_into()?);
let sequence_number = sequence_number.as_u64(); // down-casting to u64 is safe for the sequence number
let seq_num = 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
.try_into()
.map_err(|_| eyre::eyre!("invalid sequence number"))?;
cursor += 32;

let batcher_hash = H256::from_slice(&calldata[cursor..cursor + 32]);
let batcher_hash = B256::from_slice(&calldata[cursor..cursor + 32]);
cursor += 32;

let fee_overhead = U256::from_big_endian(&calldata[cursor..cursor + 32]);
let fee_overhead = U256::from_be_slice(&calldata[cursor..cursor + 32]);
cursor += 32;

let fee_scalar = U256::from_big_endian(&calldata[cursor..cursor + 32]);
let fee_scalar = U256::from_be_slice(&calldata[cursor..cursor + 32]);

Ok(Self {
number,
timestamp,
basefee,
hash,
sequence_number,
sequence_number: seq_num,
batcher_hash,
fee_overhead,
fee_scalar,
Expand Down Expand Up @@ -167,16 +174,16 @@ impl AttributesDepositedCall {
let number = u64::from_be_bytes(calldata[cursor..cursor + 8].try_into()?);
cursor += 8;

let basefee = U256::from_big_endian(&calldata[cursor..cursor + 32]);
let basefee = U256::from_be_slice(&calldata[cursor..cursor + 32]);
cursor += 32;

let blob_base_fee = Some(U256::from_big_endian(&calldata[cursor..cursor + 32]));
let blob_base_fee = Some(U256::from_be_slice(&calldata[cursor..cursor + 32]));
cursor += 32;

let hash = H256::from_slice(&calldata[cursor..cursor + 32]);
let hash = B256::from_slice(&calldata[cursor..cursor + 32]);
cursor += 32;

let batcher_hash = H256::from_slice(&calldata[cursor..cursor + 32]);
let batcher_hash = B256::from_slice(&calldata[cursor..cursor + 32]);

Ok(Self {
number,
Expand All @@ -190,7 +197,7 @@ impl AttributesDepositedCall {
blob_base_fee_scalar,

// The pre-Ecotone L1 fee overhead value is dropped in Ecotone
fee_overhead: U256::zero(),
fee_overhead: U256::ZERO,
})
}
}
Expand All @@ -200,7 +207,7 @@ mod tests {
mod attributed_deposited_call {
use std::str::FromStr;

use ethers::types::{Bytes, H256, U256};
use alloy_primitives::{Bytes, B256, U256};

use crate::common::AttributesDepositedCall;

Expand All @@ -210,7 +217,7 @@ mod tests {
let calldata = "0x015d8eb900000000000000000000000000000000000000000000000000000000008768240000000000000000000000000000000000000000000000000000000064443450000000000000000000000000000000000000000000000000000000000000000e0444c991c5fe1d7291ff34b3f5c3b44ee861f021396d33ba3255b83df30e357d00000000000000000000000000000000000000000000000000000000000000050000000000000000000000007431310e026b69bfc676c0013e12a1a11411eec9000000000000000000000000000000000000000000000000000000000000083400000000000000000000000000000000000000000000000000000000000f4240";

let expected_hash =
H256::from_str("0444c991c5fe1d7291ff34b3f5c3b44ee861f021396d33ba3255b83df30e357d")?;
B256::from_str("0444c991c5fe1d7291ff34b3f5c3b44ee861f021396d33ba3255b83df30e357d")?;
let expected_block_number = 8874020;
let expected_timestamp = 1682191440;

Expand All @@ -234,7 +241,7 @@ mod tests {
// https://goerli-optimism.etherscan.io/tx/0xc2288c5d1f6123406bfe8662bdbc1a3c999394da2e6f444f5aa8df78136f36ba
let calldata = "0x440a5e2000001db0000d273000000000000000050000000065c8ad6c0000000000a085a20000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000041dfd80f2c8af7d7ba1c1a3962026e5c96b9105d528f8fed65c56cfa731a8751c7f712eb70000000000000000000000007431310e026b69bfc676c0013e12a1a11411eec9";

let expected_hash = H256::from_str(
let expected_hash = B256::from_str(
"0xc8af7d7ba1c1a3962026e5c96b9105d528f8fed65c56cfa731a8751c7f712eb7",
);
let expected_block_number = 10519970;
Expand Down
2 changes: 1 addition & 1 deletion src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl From<&AttributesDepositedCall> for Epoch {
Self {
number: call.number,
timestamp: call.timestamp,
hash: call.hash,
hash: H256::from_slice(call.hash.as_slice()),
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/driver/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ impl HeadInfo {
));
};

let tx_calldata = first_tx.input.clone();
let call = AttributesDepositedCall::try_from_bedrock(tx_calldata)?;
let tx_calldata = first_tx.input.to_vec();
let call = AttributesDepositedCall::try_from_bedrock(tx_calldata.into())?;

Ok(Self {
l2_block_info: BlockInfo::try_from(block)?,
Expand All @@ -61,8 +61,8 @@ impl HeadInfo {
));
};

let tx_calldata = first_tx.input.clone();
let call = AttributesDepositedCall::try_from_ecotone(tx_calldata)?;
let tx_calldata = first_tx.input.to_vec();
let call = AttributesDepositedCall::try_from_ecotone(tx_calldata.into())?;

Ok(Self {
l2_block_info: BlockInfo::try_from(block)?,
Expand Down
Loading