From 455359e2a2c3c10a50454abad1cee8226dc4aaee 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 | 10 +++++- Cargo.toml | 4 ++- compat/Cargo.toml | 14 ++++++++ compat/src/lib.rs | 67 +++++++++++++++++++++++++++++++++++++ trace_decoder/Cargo.toml | 1 - trace_decoder/src/types.rs | 68 -------------------------------------- zero_bin/rpc/Cargo.toml | 3 +- zero_bin/rpc/src/lib.rs | 3 +- 8 files changed, 97 insertions(+), 73 deletions(-) create mode 100644 compat/Cargo.toml create mode 100644 compat/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 35f703587..ec1768bd2 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", @@ -5128,7 +5137,6 @@ checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" name = "trace_decoder" version = "0.4.0" dependencies = [ - "alloy", "bytes", "ciborium", "ciborium-io", diff --git a/Cargo.toml b/Cargo.toml index 30ee73a25..22c4f20bc 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] @@ -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" 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/Cargo.toml b/trace_decoder/Cargo.toml index ba0f0041b..d4a3629b3 100644 --- a/trace_decoder/Cargo.toml +++ b/trace_decoder/Cargo.toml @@ -10,7 +10,6 @@ homepage.workspace = true keywords.workspace = true [dependencies] -alloy = {workspace = true } bytes = { workspace = true } ciborium = { workspace = true } ciborium-io = { workspace = true } 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;