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

[SPIKE]: Ethers Removal #242

Draft
wants to merge 5 commits into
base: alloy
Choose a base branch
from
Draft
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
1,616 changes: 121 additions & 1,495 deletions Cargo.lock

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ path = "./bin/network.rs"
[dependencies]
tokio = { version = "1.28.0", features = ["full"] }
async-trait = "0.1.73"
eyre = "0.6.8"
ethers = { version = "2.0.14", features = ["optimism"] }
anyhow = "1.0"
hex = "0.4.3"
libflate = "1.2.0"
openssl = { version = "0.10", features = ["vendored"] }
Expand All @@ -26,14 +25,15 @@ jsonrpsee = {version = "0.17.0", features = ["server", "macros"]}
futures = "0.3.28"
futures-timer = "0.3.0"
again = "0.1"
spin = { version = "0.9.8", features = ["mutex"] }

# Alloy types
alloy-rlp = { version = "0.3", default-features = false }
alloy-primitives = { version = "0.7.1", default-features = false, features = ["serde"] }
alloy-provider = { git = "https://github.com/alloy-rs/alloy" }
alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy" }
alloy-consensus = { git = "https://github.com/alloy-rs/alloy" }
alloy-eips = { git = "https://github.com/alloy-rs/alloy" }
alloy-provider = { git = "https://github.com/alloy-rs/alloy", rev = "e3f2f07", features = [ "reqwest" ] }
alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy", rev = "e3f2f07" }
op-alloy-consensus = { git = "https://github.com/clabby/op-alloy", branch = "refcell/consensus-port", default-features = false }
alloy-eips = { git = "https://github.com/alloy-rs/alloy", rev = "e3f2f07", default-features = false }

# Logging and Metrics
chrono = "0.4.22"
Expand All @@ -56,7 +56,7 @@ serde_json = "1.0.93"
# Backend Crates
uuid = { version = "1.3.0", features = ["v4"] }
bytes = "1.4.0"
reqwest = "0.11.14"
reqwest = { version = "0.11.14", features = ["json"] }
jsonwebtoken = "8.2.0"
rand = "0.8.5"

Expand Down
2 changes: 1 addition & 1 deletion bin/magi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{env::current_dir, process};

use clap::Parser;
use dirs::home_dir;
use eyre::Result;
use anyhow::Result;

use magi::{
config::{ChainConfig, CliConfig, Config, SyncMode},
Expand Down
2 changes: 1 addition & 1 deletion bin/network.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloy_primitives::address;
use eyre::Result;
use anyhow::Result;

use magi::{
network::{handlers::block_handler::BlockHandler, service::Service},
Expand Down
21 changes: 11 additions & 10 deletions src/common/attributes_deposited.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use eyre::Result;
use anyhow::Result;
use lazy_static::lazy_static;

use alloy_primitives::{keccak256, Bytes, B256, U256};
Expand Down Expand Up @@ -67,27 +67,27 @@ impl AttributesDepositedCall {
let mut cursor = 0;

if calldata.len() != L1_INFO_BEDROCK_LEN {
eyre::bail!("invalid calldata length");
anyhow::bail!("invalid calldata length");
}

let selector = &calldata[cursor..cursor + 4];
if selector != *SET_L1_BLOCK_VALUES_BEDROCK_SELECTOR {
eyre::bail!("invalid selector");
anyhow::bail!("invalid selector");
}
cursor += 4;

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"))?;
.map_err(|_| anyhow::anyhow!("invalid block number"))?;
cursor += 32;

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"))?;
.map_err(|_| anyhow::anyhow!("invalid timestamp"))?;
cursor += 32;

let basefee = U256::from_be_slice(&calldata[cursor..cursor + 32]);
Expand All @@ -100,7 +100,7 @@ impl AttributesDepositedCall {
// down-casting to u64 is safe for the sequence number
let sequence_number = sequence_number
.try_into()
.map_err(|_| eyre::eyre!("invalid sequence number"))?;
.map_err(|_| anyhow::anyhow!("invalid sequence number"))?;
cursor += 32;

let batcher_hash = B256::from_slice(&calldata[cursor..cursor + 32]);
Expand Down Expand Up @@ -148,12 +148,12 @@ impl AttributesDepositedCall {
let mut cursor = 0;

if calldata.len() != L1_INFO_ECOTONE_LEN {
eyre::bail!("invalid calldata length");
anyhow::bail!("invalid calldata length");
}

let selector = &calldata[cursor..cursor + 4];
if selector != *SET_L1_BLOCK_VALUES_ECOTONE_SELECTOR {
eyre::bail!("invalid selector");
anyhow::bail!("invalid selector");
}
cursor += 4;

Expand Down Expand Up @@ -206,13 +206,14 @@ impl AttributesDepositedCall {
mod tests {
mod attributed_deposited_call {
use std::str::FromStr;
use anyhow::Result;

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

use crate::common::AttributesDepositedCall;

#[test]
fn decode_from_bytes_bedrock() -> eyre::Result<()> {
fn decode_from_bytes_bedrock() -> Result<()> {
// Arrange
let calldata = "0x015d8eb900000000000000000000000000000000000000000000000000000000008768240000000000000000000000000000000000000000000000000000000064443450000000000000000000000000000000000000000000000000000000000000000e0444c991c5fe1d7291ff34b3f5c3b44ee861f021396d33ba3255b83df30e357d00000000000000000000000000000000000000000000000000000000000000050000000000000000000000007431310e026b69bfc676c0013e12a1a11411eec9000000000000000000000000000000000000000000000000000000000000083400000000000000000000000000000000000000000000000000000000000f4240";

Expand All @@ -236,7 +237,7 @@ mod tests {
}

#[test]
fn decode_from_bytes_ecotone() -> eyre::Result<()> {
fn decode_from_bytes_ecotone() -> Result<()> {
// Arrange
// https://goerli-optimism.etherscan.io/tx/0xc2288c5d1f6123406bfe8662bdbc1a3c999394da2e6f444f5aa8df78136f36ba
let calldata = "0x440a5e2000001db0000d273000000000000000050000000065c8ad6c0000000000a085a20000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000041dfd80f2c8af7d7ba1c1a3962026e5c96b9105d528f8fed65c56cfa731a8751c7f712eb70000000000000000000000007431310e026b69bfc676c0013e12a1a11411eec9";
Expand Down
47 changes: 23 additions & 24 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//! Module containing common types and functions used throughout the crate.

use std::fmt::Debug;

use alloy_primitives::B256;
use ethers::{
types::{Block, Transaction},
utils::rlp::{Decodable, DecoderError, Rlp},
};
use eyre::Result;
use alloy_primitives::{Bytes, B256};
use alloy_rpc_types::Block;
use anyhow::Result;
use figment::value::{Dict, Tag, Value};
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};

Expand All @@ -29,9 +28,15 @@ pub struct BlockInfo {
}

/// A raw transaction
#[derive(Clone, PartialEq, Eq)]
#[derive(Clone, alloy_rlp::RlpDecodable, alloy_rlp::RlpEncodable, PartialEq, Eq)]
pub struct RawTransaction(pub Vec<u8>);

impl<T: Into<Bytes>> From<T> for RawTransaction {
fn from(bytes: T) -> Self {
Self(bytes.into().to_vec())
}
}

/// L1 epoch block
#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct Epoch {
Expand All @@ -57,23 +62,25 @@ impl From<BlockInfo> for Value {
}
}

impl TryFrom<Block<Transaction>> for BlockInfo {
type Error = eyre::Report;
impl TryFrom<Block> for BlockInfo {
type Error = anyhow::Error;

/// Converts a [Block] to [BlockInfo]
fn try_from(block: Block<Transaction>) -> Result<Self> {
fn try_from(block: Block) -> Result<Self> {
let number = block
.header
.number
.ok_or(eyre::eyre!("block not included"))?
.as_u64();
.ok_or(anyhow::anyhow!("block not included"))?
.try_into()?;

let hash = block.hash.ok_or(eyre::eyre!("block not included"))?;
let hash = block.header.hash.ok_or(anyhow::anyhow!("block not included"))?;
let timestamp = block.header.timestamp.try_into()?;

Ok(BlockInfo {
number,
hash: B256::from_slice(hash.as_bytes()),
parent_hash: B256::from_slice(block.parent_hash.as_bytes()),
timestamp: block.timestamp.as_u64(),
hash,
parent_hash: block.header.parent_hash,
timestamp,
})
}
}
Expand Down Expand Up @@ -111,14 +118,6 @@ impl From<&AttributesDepositedCall> for Epoch {
}
}

impl Decodable for RawTransaction {
/// Decodes RLP encoded bytes into [RawTransaction] bytes
fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
let tx_bytes: Vec<u8> = rlp.as_val()?;
Ok(Self(tx_bytes))
}
}

impl Debug for RawTransaction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "0x{}", hex::encode(&self.0))
Expand Down
22 changes: 12 additions & 10 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use std::{fmt, iter, path::PathBuf, process::exit, str::FromStr};
//! Module contains Configuration types.

use alloy_primitives::{Address, B256, U256};
use alloy_primitives::{Address, B256, U256, U64};
use figment::{
providers::{Format, Serialized, Toml},
Figment,
};
use serde::{Deserialize, Serialize};
use std::{fmt, iter, path::PathBuf, process::exit, str::FromStr};

use crate::common::{BlockInfo, Epoch};

/// Sync Mode Specifies how `magi` should sync the L2 chain
/// Sync Mode Specifies how to sync the L2 chain
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum SyncMode {
/// Fast sync mode
Expand Down Expand Up @@ -47,7 +48,7 @@ impl fmt::Display for SyncMode {
}
}

/// The global `Magi` configuration.
/// The global configuration.
#[derive(Debug, Clone, Deserialize, Default)]
pub struct Config {
/// The L1 chain RPC URL
Expand Down Expand Up @@ -293,16 +294,17 @@ impl ChainConfig {
}

/// Returns true if the block is the first block subject to the Ecotone hardfork
pub fn is_ecotone_activation_block(&self, l2_block_timestamp: u64) -> bool {
l2_block_timestamp == self.ecotone_time
pub fn is_ecotone_activation_block(&self, l2_block_timestamp: &U64) -> bool {
*l2_block_timestamp == U64::from(self.ecotone_time)
}

/// Returns true if Ecotone hardfork is active but the block is not the
/// first block subject to the hardfork. Ecotone activation at genesis does not count.
pub fn is_ecotone_but_not_first_block(&self, l2_block_timestamp: u64) -> bool {
let is_ecotone = l2_block_timestamp >= self.ecotone_time;

is_ecotone && !self.is_ecotone_activation_block(l2_block_timestamp)
pub fn is_ecotone_but_not_first_block(&self, l2_block_timestamp: impl Into<U64>) -> bool {
let l2_block_timestamp = l2_block_timestamp.into();
let is_activation_block = self.is_ecotone_activation_block(&l2_block_timestamp);
let is_ecotone = l2_block_timestamp >= U64::from(self.ecotone_time);
is_ecotone && !is_activation_block
}

/// [ChainConfig] for Optimism
Expand Down
Loading
Loading