Skip to content

Commit

Permalink
refactor: remove the compat crate (0xPolygonZero#795)
Browse files Browse the repository at this point in the history
* mark: 0xaatif/no-compat

* refactor: remove compat crate

* fix(ci): remove reference to `compat`
  • Loading branch information
0xaatif authored Nov 13, 2024
1 parent dd17ff3 commit b63c8e2
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 133 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
--- # Rust lint related checks
--- # Rust lint related checks

name: lint

Expand Down Expand Up @@ -47,7 +47,6 @@ jobs:
- run: >
RUSTDOCFLAGS='-D warnings -A rustdoc::private_intra_doc_links' cargo doc --no-deps --document-private-items
--package trace_decoder
--package compat
--package smt_trie
--package zk_evm_proc_macro
--package zk_evm_common
Expand Down
14 changes: 2 additions & 12 deletions Cargo.lock

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

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[workspace]
members = [
"common",
"compat",
"evm_arithmetization",
"mpt_trie",
"proc_macro",
Expand All @@ -21,7 +20,6 @@ keywords = ["cryptography", "STARK", "plonky2", "ethereum", "zk"]
categories = ["cryptography::cryptocurrencies"]

[workspace.dependencies]
__compat_primitive_types = { version = "0.12.2", package = "primitive-types" }
alloy = { version = '0.3.0', default-features = false, features = [
"consensus",
"reqwest",
Expand Down Expand Up @@ -105,7 +103,6 @@ url = "2.5.2"
winnow = "0.6.13"

# local dependencies
compat = { path = "compat" }
evm_arithmetization = { path = "evm_arithmetization", version = "0.4.0", default-features = false }
mpt_trie = { path = "mpt_trie", version = "0.4.1" }
smt_trie = { path = "smt_trie", version = "0.1.1" }
Expand Down
14 changes: 0 additions & 14 deletions compat/Cargo.toml

This file was deleted.

69 changes: 0 additions & 69 deletions compat/src/lib.rs

This file was deleted.

4 changes: 1 addition & 3 deletions zero/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ keywords.workspace = true
categories.workspace = true

[dependencies]
__compat_primitive_types.workspace = true
alloy.workspace = true
alloy-compat = "0.1.0"
alloy-compat = "0.1.1"
anyhow.workspace = true
async-stream.workspace = true
axum.workspace = true
cfg-if = "1.0.0"
clap = { workspace = true, features = ["derive", "string"] }
compat.workspace = true
directories = "5.0.1"
dotenvy.workspace = true
evm_arithmetization.workspace = true
Expand Down
23 changes: 16 additions & 7 deletions zero/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ zk_evm_common::check_chain_features!();

use std::sync::Arc;

use __compat_primitive_types::{H256, U256};
use alloy::{
primitives::{Address, Bytes, FixedBytes, B256},
primitives::{Address, Bloom, Bytes, FixedBytes, B256, U256},
providers::Provider,
rpc::types::eth::{BlockId, BlockTransactionsKind, Withdrawal},
transports::Transport,
};
use alloy_compat::Compat as _;
use anyhow::{anyhow, Context as _};
use clap::ValueEnum;
use compat::Compat;
use evm_arithmetization::{
proof::{consolidate_hashes, BlockHashes, BlockMetadata},
Field, Hasher,
Expand Down Expand Up @@ -264,15 +263,25 @@ where
.into()
},
block_gas_used: target_block.header.gas_used.into(),
block_bloom: target_block.header.logs_bloom.compat(),
block_bloom: {
const CHUNK: usize = 32;
let Bloom(FixedBytes(bytes)) = target_block.header.logs_bloom;
let chunks = bytes.chunks_exact(CHUNK);
assert!(chunks.remainder().is_empty());
let mut array = [U256::ZERO; 8];
for (ix, chunk) in chunks.enumerate() {
array[ix] = U256::from_le_bytes::<CHUNK>(chunk.try_into().unwrap());
}
array.map(alloy_compat::Compat::compat)
},
parent_beacon_block_root: if cfg!(feature = "eth_mainnet") {
target_block
.header
.parent_beacon_block_root
.context("target block is missing field `parent_beacon_block_root`")?
.compat()
} else {
H256::zero()
Default::default()
},
block_blob_gas_used: if cfg!(feature = "eth_mainnet") {
target_block
Expand All @@ -281,7 +290,7 @@ where
.context("target block is missing field `blob_gas_used`")?
.into()
} else {
U256::zero()
Default::default()
},
block_excess_blob_gas: if cfg!(feature = "eth_mainnet") {
target_block
Expand All @@ -290,7 +299,7 @@ where
.context("target block is missing field `excess_blob_gas`")?
.into()
} else {
U256::zero()
Default::default()
},
},
b_hashes: BlockHashes {
Expand Down
46 changes: 23 additions & 23 deletions zero/src/rpc/native/txn.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};

use __compat_primitive_types::{H256, U256};
use alloy::{
primitives::{keccak256, Address, B256},
primitives::{keccak256, Address, B256, U256},
providers::{
ext::DebugApi as _,
network::{eip2718::Encodable2718, Ethereum, Network},
Provider,
},
rpc::types::{
eth::Transaction,
eth::{AccessList, Block},
eth::{AccessList, Block, Transaction},
trace::geth::{
AccountState, DiffMode, GethDebugBuiltInTracerType, GethTrace, PreStateConfig,
PreStateFrame, PreStateMode,
AccountState, DiffMode, GethDebugBuiltInTracerType, GethDebugTracerType,
GethDebugTracingOptions, GethTrace, PreStateConfig, PreStateFrame, PreStateMode,
},
trace::geth::{GethDebugTracerType, GethDebugTracingOptions},
},
transports::Transport,
};
use alloy_compat::Compat;
use anyhow::Context as _;
use compat::Compat;
use futures::stream::{FuturesOrdered, TryStreamExt};
use trace_decoder::{ContractCodeUsage, TxnInfo, TxnMeta, TxnTrace};

Expand Down Expand Up @@ -123,15 +120,15 @@ where
}

/// Parse the access list data into a hashmap.
fn parse_access_list(access_list: Option<&AccessList>) -> HashMap<Address, HashSet<H256>> {
fn parse_access_list(access_list: Option<&AccessList>) -> HashMap<Address, HashSet<B256>> {
let mut result = HashMap::new();

if let Some(access_list) = access_list {
for item in access_list.0.clone() {
result
.entry(item.address)
.or_insert_with(HashSet::new)
.extend(item.storage_keys.into_iter().map(Compat::compat));
.extend(item.storage_keys);
}
}

Expand All @@ -140,7 +137,7 @@ fn parse_access_list(access_list: Option<&AccessList>) -> HashMap<Address, HashS

/// Processes the transaction traces and updates the accounts state.
async fn process_tx_traces(
mut access_list: HashMap<Address, HashSet<H256>>,
mut access_list: HashMap<Address, HashSet<B256>>,
read_trace: PreStateMode,
diff_trace: DiffMode,
) -> anyhow::Result<(CodeDb, BTreeMap<Address, TxnTrace>)> {
Expand All @@ -166,7 +163,7 @@ async fn process_tx_traces(
let pre_state = pre_trace.get(&address);
let post_state = post_trace.get(&address);

let balance = post_state.and_then(|x| x.balance.map(Compat::compat));
let balance = post_state.and_then(|x| x.balance);
let (storage_read, storage_written) = process_storage(
access_list.remove(&address).unwrap_or_default(),
read_state,
Expand All @@ -178,10 +175,13 @@ async fn process_tx_traces(
let self_destructed = process_self_destruct(post_state, pre_state);

let result = TxnTrace {
balance,
nonce,
storage_read,
storage_written,
balance: balance.map(Compat::compat),
nonce: nonce.map(Compat::compat),
storage_read: storage_read.into_iter().map(Compat::compat).collect(),
storage_written: storage_written
.into_iter()
.map(|(k, v)| (k.compat(), v.compat()))
.collect(),
code_usage: code,
self_destructed,
};
Expand All @@ -200,7 +200,7 @@ fn process_nonce(
code_usage: &Option<ContractCodeUsage>,
) -> Option<U256> {
post_state
.and_then(|x| x.nonce.map(Into::into))
.and_then(|x| x.nonce.map(U256::from))
.or_else(|| {
if let Some(ContractCodeUsage::Write(_)) = code_usage.as_ref() {
Some(U256::from(1))
Expand Down Expand Up @@ -239,31 +239,31 @@ fn process_self_destruct(
/// Returns the storage read and written for the given account in the
/// transaction and updates the storage keys.
fn process_storage(
access_list: HashSet<__compat_primitive_types::H256>,
access_list: HashSet<B256>,
acct_state: Option<&AccountState>,
post_acct: Option<&AccountState>,
pre_acct: Option<&AccountState>,
) -> (BTreeSet<H256>, BTreeMap<H256, U256>) {
) -> (BTreeSet<B256>, BTreeMap<B256, U256>) {
let mut storage_read = BTreeSet::from_iter(access_list);
storage_read.extend(
acct_state
.into_iter()
.flat_map(|acct| acct.storage.keys().copied().map(Compat::compat)),
.flat_map(|acct| acct.storage.keys().copied()),
);

let mut storage_written: BTreeMap<H256, U256> = post_acct
let mut storage_written: BTreeMap<B256, U256> = post_acct
.map(|x| {
x.storage
.iter()
.map(|(k, v)| ((*k).compat(), U256::from_big_endian(&v.0)))
.map(|(k, v)| (*k, U256::from_le_bytes((*v).into())))
.collect()
})
.unwrap_or_default();

// Add the deleted keys to the storage written
if let Some(pre_acct) = pre_acct {
for key in pre_acct.storage.keys() {
storage_written.entry((*key).compat()).or_default();
storage_written.entry(*key).or_default();
}
};

Expand Down

0 comments on commit b63c8e2

Please sign in to comment.