From 2e2adb05d31043bf8ab33becbffaf8d22a3b0362 Mon Sep 17 00:00:00 2001 From: frisitano Date: Sun, 23 Jun 2024 13:58:22 +0800 Subject: [PATCH] migrate compat to micro crate --- Cargo.lock | 9 +++++ Cargo.toml | 4 ++- compat/Cargo.toml | 14 ++++++++ compat/src/lib.rs | 67 +++++++++++++++++++++++++++++++++++++ trace_decoder/src/types.rs | 68 -------------------------------------- zero_bin/rpc/Cargo.toml | 3 +- zero_bin/rpc/src/lib.rs | 3 +- 7 files changed, 97 insertions(+), 71 deletions(-) create mode 100644 compat/Cargo.toml create mode 100644 compat/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 35f703587..e1f3d15a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1336,6 +1336,14 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" +[[package]] +name = "compat" +version = "0.1.0" +dependencies = [ + "alloy", + "ethereum-types", +] + [[package]] name = "concurrent-queue" version = "2.5.0" @@ -4171,6 +4179,7 @@ dependencies = [ "alloy", "anyhow", "clap", + "compat", "evm_arithmetization", "futures", "hex", diff --git a/Cargo.toml b/Cargo.toml index 30ee73a25..6c3d93c9d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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] @@ -98,6 +99,7 @@ uint = "0.9.5" url = "2.5.0" # zero-bin related dependencies +compat = { path = "compat" } ops = { path = "zero_bin/ops" } prover = { path = "zero_bin/prover" } rpc = { path = "zero_bin/rpc" } diff --git a/compat/Cargo.toml b/compat/Cargo.toml new file mode 100644 index 000000000..56bbcef9d --- /dev/null +++ b/compat/Cargo.toml @@ -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 } \ No newline at end of file diff --git a/compat/src/lib.rs b/compat/src/lib.rs new file mode 100644 index 000000000..e9561079f --- /dev/null +++ b/compat/src/lib.rs @@ -0,0 +1,67 @@ +/// A trait to convert between alloy and ethereum types. +pub trait Compat { + /// Convert the type to another type + fn compat(self) -> Out; +} + +impl Compat 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 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 for alloy::primitives::U256 { + fn compat(self) -> ethereum_types::U256 { + ethereum_types::U256(self.into_limbs()) + } +} + +impl Compat>> for Vec { + fn compat(self) -> Vec> { + self.into_iter().map(|x| x.to_vec()).collect() + } +} + +impl Compat 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 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(); +} diff --git a/trace_decoder/src/types.rs b/trace_decoder/src/types.rs index cbe5d91e3..3078ac22e 100644 --- a/trace_decoder/src/types.rs +++ b/trace_decoder/src/types.rs @@ -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 { - /// Convert the type to another type - fn compat(self) -> Out; -} - -impl Compat 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 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 for alloy::primitives::U256 { - fn compat(self) -> ethereum_types::U256 { - ethereum_types::U256(self.into_limbs()) - } -} - -impl Compat>> for Vec { - fn compat(self) -> Vec> { - self.into_iter().map(|x| x.to_vec()).collect() - } -} - -impl Compat 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 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(); -} diff --git a/zero_bin/rpc/Cargo.toml b/zero_bin/rpc/Cargo.toml index 9551348b8..ed5a12224 100644 --- a/zero_bin/rpc/Cargo.toml +++ b/zero_bin/rpc/Cargo.toml @@ -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 } diff --git a/zero_bin/rpc/src/lib.rs b/zero_bin/rpc/src/lib.rs index c4fbe7357..3b69fa218 100644 --- a/zero_bin/rpc/src/lib.rs +++ b/zero_bin/rpc/src/lib.rs @@ -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;