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

feat: replace ethers-rs by alloy-rs #1366

Draft
wants to merge 1 commit into
base: develop
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
362 changes: 354 additions & 8 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ edition = "2021"
license = "MIT OR Apache-2.0"

[workspace.dependencies]
alloy = { version = "0.2", default-features = false }
anyhow = "1.0"
ark-std = "0.3"
base64 = "0.13.0"
Expand Down
2 changes: 1 addition & 1 deletion aggregator/src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl ChunkInfo {
let post_state_root = traces
.last()
.expect("at least 1 block needed")
.header
.eth_block
.state_root;
let withdraw_root = traces.last().unwrap().withdraw_trie_root;
let chain_id = traces.first().unwrap().chain_id;
Expand Down
4 changes: 2 additions & 2 deletions bus-mapping/src/circuit_input_builder/l2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl CircuitInputBuilder {
fn apply_l2_trace(&mut self, block_trace: BlockTrace) -> Result<(), Error> {
log::trace!(
"apply_l2_trace start, block num {:?}",
block_trace.header.number
block_trace.eth_block.number
);
//self.sdb.list_accounts();
//dump_code_db(&self.code_db);
Expand Down Expand Up @@ -120,7 +120,7 @@ impl CircuitInputBuilder {
let old_root = l2_trace.storage_trace.root_before;
log::debug!(
"building zktrie state for block {:?}, old root {}",
l2_trace.header.number,
l2_trace.eth_block.number,
hex::encode(old_root),
);

Expand Down
12 changes: 12 additions & 0 deletions eth-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ edition.workspace = true
license.workspace = true

[dependencies]
alloy = { workspace = true, features = [
"consensus",
"eips",
"rlp",
"rpc-types-eth",
"signer-local",
"std"
] }
ethers-core.workspace = true
ethers-signers.workspace = true
hex.workspace = true
Expand All @@ -27,6 +35,10 @@ base64.workspace = true
revm-precompile.workspace = true
revm-primitives.workspace = true

[dev-dependencies]
ethers-core.workspace = true
ethers-signers.workspace = true

[features]
default = ["warn-unimplemented"]
warn-unimplemented = []
Expand Down
8 changes: 4 additions & 4 deletions eth-types/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ impl Bytecode {
// Write the op code
self.write_op((OpcodeId::push_n(n)).expect("valid push size"));

let mut bytes = [0u8; 32];
value.to_little_endian(&mut bytes);
let mut bytes = value.to_le_bytes();
// Write the bytes MSB to LSB
for i in 0..n {
self.write(bytes[(n - 1 - i) as usize], false);
Expand Down Expand Up @@ -275,7 +274,8 @@ impl<'a> Iterator for BytecodeIterator<'a> {
*value_byte = self.0.next().unwrap().value;
}

OpcodeWithData::PushWithData(n as u8, Word::from(value.as_slice()))
OpcodeWithData::PushWithData(n as u8, Word::from_le_slice(value.as_slice()))
// FIXME: le or be?
} else {
OpcodeWithData::Opcode(op)
}
Expand Down Expand Up @@ -355,7 +355,7 @@ macro_rules! bytecode_internal {
impl Bytecode {
/// Helper function for `PUSH0`
pub fn op_push0(&mut self) -> &mut Self {
self.push(0, Word::zero())
self.push(0, Word::ZERO)
}
}

Expand Down
14 changes: 6 additions & 8 deletions eth-types/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

use std::str::FromStr;

use ethers_core::types::{Address, U256};

use crate::l2_types::BlockTrace;
use crate::{address, l2_types::BlockTrace, Address, U256};

/// Read env var with default value
pub fn read_env_var<T: Clone + FromStr>(var_name: &'static str, default: T) -> T {
Expand All @@ -14,14 +12,14 @@ pub fn read_env_var<T: Clone + FromStr>(var_name: &'static str, default: T) -> T
}

/// Scroll coinbase
pub const SCROLL_COINBASE: &str = "0x5300000000000000000000000000000000000005";
pub const SCROLL_COINBASE: Address = address!("5300000000000000000000000000000000000005");

/// Get COINBASE constant used for circuit
pub fn get_coinbase_constant() -> Address {
let default_coinbase = if cfg!(feature = "scroll") {
Address::from_str(SCROLL_COINBASE).unwrap()
SCROLL_COINBASE
} else {
Address::zero()
Address::ZERO
};
read_env_var("COINBASE", default_coinbase)
}
Expand All @@ -35,12 +33,12 @@ pub fn set_env_coinbase(coinbase: &Address) -> String {

/// Get DIFFICULTY constant used for circuit
pub fn get_difficulty_constant() -> U256 {
read_env_var("DIFFICULTY", U256::zero())
read_env_var("DIFFICULTY", U256::ZERO)
}

/// Set scroll block constants using trace
pub fn set_scroll_block_constants_with_trace(trace: &BlockTrace) {
set_scroll_block_constants(&trace.coinbase.address, trace.chain_id, U256::zero())
set_scroll_block_constants(&trace.coinbase.address, trace.chain_id, U256::ZERO)
}

/// Set scroll block constants
Expand Down
13 changes: 7 additions & 6 deletions eth-types/src/evm_types/block_utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Helper constants and utility functions for block

use crate::{U256, U64};
use ethers_core::utils::keccak256;
use crate::{keccak256, U256};

/// Maximum range of previous blocks allowed inside BLOCKHASH opcode
pub const NUM_PREV_BLOCK_ALLOWED: u64 = 256;
Expand All @@ -11,10 +10,12 @@ pub const NUM_PREV_BLOCK_ALLOWED: u64 = 256;
pub fn calculate_block_hash(chain_id: u64, block_number: U256) -> (Vec<u8>, U256) {
let mut input = vec![0; 16];

U64([chain_id]).to_big_endian(&mut input[..8]);
U64([block_number.low_u64()]).to_big_endian(&mut input[8..]);
let chain_id = chain_id.to_be_bytes();
let block_number = block_number.to::<u64>().to_be_bytes();
input[..8].copy_from_slice(&chain_id);
input[8..].copy_from_slice(&block_number);

let output = U256::from_big_endian(&keccak256(&input));
let output = U256::from_be_slice(keccak256(&input).as_slice());

(input, output)
}
Expand All @@ -24,6 +25,6 @@ pub fn is_valid_block_number(block_number: U256, current_block_number: U256) ->
block_number < current_block_number
&& block_number
>= current_block_number
.checked_sub(NUM_PREV_BLOCK_ALLOWED.into())
.checked_sub(U256::from_limbs([NUM_PREV_BLOCK_ALLOWED, 0, 0, 0]))
.unwrap_or_default()
}
3 changes: 1 addition & 2 deletions eth-types/src/evm_types/gas_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ pub fn memory_copier_gas_cost(
pub fn eip150_gas(gas_left: u64, gas_specified: Word) -> u64 {
let capped_gas = gas_left - gas_left / 64;

if gas_specified.bits() <= 64 {
let gas_specified = gas_specified.low_u64();
if let Ok(gas_specified) = u64::try_from(gas_specified) {
if gas_specified < capped_gas {
return gas_specified;
}
Expand Down
19 changes: 9 additions & 10 deletions eth-types/src/evm_types/memory.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Doc this
use crate::{DebugByte, Error, ToBigEndian, Word};
use crate::{DebugByte, Error, Word};
use core::{
ops::{Add, AddAssign, Index, IndexMut, Mul, MulAssign, Range, Sub, SubAssign},
str::FromStr,
Expand Down Expand Up @@ -74,10 +74,9 @@ impl TryFrom<Word> for MemoryAddress {
type Error = Error;

fn try_from(word: Word) -> Result<Self, Self::Error> {
if word.bits() > core::mem::size_of::<usize>() * 8 {
return Err(Error::WordToMemAddr);
}
Ok(MemoryAddress(word.as_usize()))
usize::try_from(word)
.map(MemoryAddress)
.map_err(|_| Error::WordToMemAddr)
}
}

Expand Down Expand Up @@ -297,7 +296,7 @@ impl Memory {
/// Reads an entire [`Word`] which starts at the provided [`MemoryAddress`]
/// `addr` and finnishes at `addr + 32`.
pub fn read_word(&self, addr: MemoryAddress) -> Word {
Word::from_big_endian(&self.read_chunk(MemoryRange::new_with_length(addr, 32)))
Word::from_be_slice(&self.read_chunk(MemoryRange::new_with_length(addr, 32)))
}

/// Reads an chunk of memory[offset..offset+length]. Zeros will be padded if
Expand Down Expand Up @@ -341,10 +340,10 @@ impl Memory {
/// then do nothing.
pub fn extend_for_range(&mut self, offset: Word, length: Word) {
// `length` should be checked for overflow during gas cost calculation.
let length = length.as_usize();
let length: usize = length.to();
if length != 0 {
// `dst_offset` should be within range if length is non-zero.
let offset = offset.as_usize();
let offset: usize = offset.to();
self.extend_at_least(offset + length);
}
}
Expand All @@ -356,11 +355,11 @@ impl Memory {

// `length` should be checked for overflow during gas cost calculation.
// Otherwise should return an out of gas error previously.
let length = length.as_usize();
let length: usize = length.to();
if length != 0 {
// `dst_offset` should be within range if length is non-zero.
// https://github.com/ethereum/go-ethereum/blob/bb4ac2d396de254898a5f44b1ea2086bfe5bd193/core/vm/common.go#L37
let dst_offset = dst_offset.as_u64();
let dst_offset: u64 = dst_offset.to();

// Reset data offset to the maximum value of Uint64 if overflow.
let src_offset = u64::try_from(src_offset).unwrap_or(u64::MAX);
Expand Down
2 changes: 1 addition & 1 deletion eth-types/src/evm_types/stack.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Doc this
use crate::{DebugWord, Error, ToBigEndian, Word};
use crate::{DebugWord, Error, Word};
use core::str::FromStr;
use serde::{ser::SerializeSeq, Deserialize, Serialize, Serializer};
use std::fmt;
Expand Down
2 changes: 1 addition & 1 deletion eth-types/src/evm_types/storage.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Doc this
use crate::{DebugWord, Error, ToBigEndian, Word};
use crate::{DebugWord, Error, Word};
use serde::{ser::SerializeMap, Serialize, Serializer};
use std::{collections::HashMap, fmt};

Expand Down
2 changes: 1 addition & 1 deletion eth-types/src/evm_types/transient_storage.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Doc this
use crate::{DebugWord, Error, ToBigEndian, Word};
use crate::{DebugWord, Error, Word};
use serde::{ser::SerializeMap, Serialize, Serializer};
use std::{collections::HashMap, fmt};

Expand Down
Loading
Loading