Skip to content

Commit

Permalink
migrate compat to micro crate
Browse files Browse the repository at this point in the history
  • Loading branch information
frisitano committed Jun 23, 2024
1 parent 7e716e7 commit 455359e
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 73 deletions.
10 changes: 9 additions & 1 deletion Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ members = ["mpt_trie",
"zero_bin/ops",
"zero_bin/verifier",
"zero_bin/rpc",
"zero_bin/prover"]
"zero_bin/prover",
"compat"]
resolver = "2"

[workspace.package]
Expand Down Expand Up @@ -42,6 +43,7 @@ bytes = "1.5.0"
ciborium = "0.2.1"
ciborium-io = "0.2.1"
clap = { version = "4.4.6", features = ["derive", "env"] }
compat = { path = "compat" }
__compat_primitive_types = "0.12.2"
criterion = "0.5.1"
dotenvy = "0.15.7"
Expand Down
14 changes: 14 additions & 0 deletions compat/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "compat"
version = "0.1.0"
publish = false
edition.workspace = true
license.workspace = true
repository.workspace = true
homepage.workspace = true
keywords.workspace = true
categories.workspace = true

[dependencies]
alloy = {workspace = true }
ethereum-types = { workspace = true }
67 changes: 67 additions & 0 deletions compat/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/// A trait to convert between alloy and ethereum types.
pub trait Compat<Out> {
/// Convert the type to another type
fn compat(self) -> Out;
}

impl Compat<ethereum_types::H160> for alloy::primitives::Address {
fn compat(self) -> ethereum_types::H160 {
let alloy::primitives::Address(alloy::primitives::FixedBytes(arr)) = self;
ethereum_types::H160(arr)
}
}

impl Compat<ethereum_types::H256> for alloy::primitives::B256 {
fn compat(self) -> ethereum_types::H256 {
let alloy::primitives::FixedBytes(arr) = self;
ethereum_types::H256(arr)
}
}

impl Compat<[ethereum_types::U256; 8]> for alloy::primitives::Bloom {
fn compat(self) -> [ethereum_types::U256; 8] {
let alloy::primitives::Bloom(alloy::primitives::FixedBytes(src)) = self;
// have u8 * 256
// want U256 * 8
// (no unsafe, no unstable)
let mut chunks = src.chunks_exact(32);
let dst = core::array::from_fn(|_ix| {
// This is a bit spicy because we're going from an uninterpeted array of bytes
// to wide integers, but we trust this `From` impl to do the right thing
ethereum_types::U256::from(<[u8; 32]>::try_from(chunks.next().unwrap()).unwrap())
});
assert_eq!(chunks.len(), 0);
dst
}
}

impl Compat<ethereum_types::U256> for alloy::primitives::U256 {
fn compat(self) -> ethereum_types::U256 {
ethereum_types::U256(self.into_limbs())
}
}

impl Compat<Vec<Vec<u8>>> for Vec<alloy::primitives::Bytes> {
fn compat(self) -> Vec<Vec<u8>> {
self.into_iter().map(|x| x.to_vec()).collect()
}
}

impl Compat<alloy::primitives::Address> for ethereum_types::H160 {
fn compat(self) -> alloy::primitives::Address {
let ethereum_types::H160(arr) = self;
alloy::primitives::Address(alloy::primitives::FixedBytes(arr))
}
}

impl Compat<alloy::primitives::StorageKey> for ethereum_types::H256 {
fn compat(self) -> alloy::primitives::StorageKey {
let ethereum_types::H256(arr) = self;
alloy::primitives::FixedBytes(arr)
}
}

#[test]
fn bloom() {
let _did_not_panic = alloy::primitives::Bloom::ZERO.compat();
}
1 change: 0 additions & 1 deletion trace_decoder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ homepage.workspace = true
keywords.workspace = true

[dependencies]
alloy = {workspace = true }
bytes = { workspace = true }
ciborium = { workspace = true }
ciborium-io = { workspace = true }
Expand Down
68 changes: 0 additions & 68 deletions trace_decoder/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,71 +73,3 @@ pub struct BlockLevelData {
/// Block withdrawal addresses and values.
pub withdrawals: Vec<(Address, U256)>,
}

/// A trait to convert between alloy and ethereum types.
pub trait Compat<Out> {
/// Convert the type to another type
fn compat(self) -> Out;
}

impl Compat<ethereum_types::H160> for alloy::primitives::Address {
fn compat(self) -> ethereum_types::H160 {
let alloy::primitives::Address(alloy::primitives::FixedBytes(arr)) = self;
ethereum_types::H160(arr)
}
}

impl Compat<ethereum_types::H256> for alloy::primitives::B256 {
fn compat(self) -> ethereum_types::H256 {
let alloy::primitives::FixedBytes(arr) = self;
ethereum_types::H256(arr)
}
}

impl Compat<[ethereum_types::U256; 8]> for alloy::primitives::Bloom {
fn compat(self) -> [ethereum_types::U256; 8] {
let alloy::primitives::Bloom(alloy::primitives::FixedBytes(src)) = self;
// have u8 * 256
// want U256 * 8
// (no unsafe, no unstable)
let mut chunks = src.chunks_exact(32);
let dst = core::array::from_fn(|_ix| {
// This is a bit spicy because we're going from an uninterpeted array of bytes
// to wide integers, but we trust this `From` impl to do the right thing
ethereum_types::U256::from(<[u8; 32]>::try_from(chunks.next().unwrap()).unwrap())
});
assert_eq!(chunks.len(), 0);
dst
}
}

impl Compat<ethereum_types::U256> for alloy::primitives::U256 {
fn compat(self) -> ethereum_types::U256 {
ethereum_types::U256(self.into_limbs())
}
}

impl Compat<Vec<Vec<u8>>> for Vec<alloy::primitives::Bytes> {
fn compat(self) -> Vec<Vec<u8>> {
self.into_iter().map(|x| x.to_vec()).collect()
}
}

impl Compat<alloy::primitives::Address> for ethereum_types::H160 {
fn compat(self) -> alloy::primitives::Address {
let ethereum_types::H160(arr) = self;
alloy::primitives::Address(alloy::primitives::FixedBytes(arr))
}
}

impl Compat<alloy::primitives::StorageKey> for ethereum_types::H256 {
fn compat(self) -> alloy::primitives::StorageKey {
let ethereum_types::H256(arr) = self;
alloy::primitives::FixedBytes(arr)
}
}

#[test]
fn bloom() {
let _did_not_panic = alloy::primitives::Bloom::ZERO.compat();
}
3 changes: 2 additions & 1 deletion zero_bin/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ __compat_primitive_types = { version = "0.12.2", package = "primitive-types" }
tower = { workspace = true, features = ["retry"] }

# Local dependencies
zero_bin_common ={ workspace = true }
compat = { workspace = true }
zero_bin_common = { workspace = true }
prover = { workspace = true }

3 changes: 2 additions & 1 deletion zero_bin/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use alloy::{
};
use anyhow::Context as _;
use clap::ValueEnum;
use compat::Compat;
use evm_arithmetization::proof::{BlockHashes, BlockMetadata};
use futures::{StreamExt as _, TryStreamExt as _};
use prover::ProverInput;
use trace_decoder::types::{BlockLevelData, Compat, OtherBlockData};
use trace_decoder::types::{BlockLevelData, OtherBlockData};
use zero_bin_common::block_interval::BlockInterval;

pub mod jerigon;
Expand Down

0 comments on commit 455359e

Please sign in to comment.