From 44f24707c978acb0510f28c1e076919707a158bb Mon Sep 17 00:00:00 2001 From: dospore Date: Fri, 16 Feb 2024 08:54:24 +1000 Subject: [PATCH] Remove use of ethers_core::RlpStream --- Cargo.lock | 58 +++++++++++++----- beacon_node/execution_layer/Cargo.toml | 4 +- beacon_node/execution_layer/src/block_hash.rs | 47 ++++++--------- .../src/engine_api/json_structures.rs | 19 ++++++ beacon_node/execution_layer/src/keccak.rs | 3 +- consensus/types/Cargo.toml | 4 +- consensus/types/src/execution_block_header.rs | 60 +++++++++++++++++++ consensus/types/src/execution_transaction.rs | 0 consensus/types/src/lib.rs | 2 +- 9 files changed, 147 insertions(+), 50 deletions(-) create mode 100644 consensus/types/src/execution_transaction.rs diff --git a/Cargo.lock b/Cargo.lock index aa17e2f4ce..b4f1bd08ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -268,6 +268,19 @@ dependencies = [ "syn 2.0.49", ] +[[package]] +name = "alloy-rpc-types" +version = "0.1.0" +source = "git+https://github.com/alloy-rs/alloy.git#e127fed9118b53c9f6776ce7aed412788827e9a5" +dependencies = [ + "alloy-primitives", + "alloy-rlp", + "itertools 0.12.1", + "serde", + "serde_json", + "thiserror", +] + [[package]] name = "amcl" version = "0.3.0" @@ -348,7 +361,7 @@ dependencies = [ "ark-std 0.4.0", "derivative", "digest 0.10.7", - "itertools", + "itertools 0.10.5", "num-bigint", "num-traits", "paste", @@ -900,7 +913,7 @@ dependencies = [ "genesis", "hex", "int_to_bytes", - "itertools", + "itertools 0.10.5", "kzg", "lazy_static", "lighthouse_metrics", @@ -983,7 +996,7 @@ dependencies = [ "fnv", "futures", "hex", - "itertools", + "itertools 0.10.5", "lazy_static", "lighthouse_metrics", "lighthouse_network", @@ -1540,7 +1553,7 @@ name = "compare_fields" version = "0.2.0" dependencies = [ "compare_fields_derive", - "itertools", + "itertools 0.10.5", ] [[package]] @@ -1645,7 +1658,7 @@ dependencies = [ "clap", "criterion-plot", "csv", - "itertools", + "itertools 0.10.5", "lazy_static", "num-traits", "oorandom", @@ -1667,7 +1680,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2673cc8207403546f45f5fd319a974b1e6983ad1a3ee7e6041650013be041876" dependencies = [ "cast", - "itertools", + "itertools 0.10.5", ] [[package]] @@ -2783,7 +2796,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e61ffea29f26e8249d35128a82ec8d3bd4fbc80179ea5f5e5e3daafef6a80fcb" dependencies = [ "ethereum-types 0.14.1", - "itertools", + "itertools 0.10.5", "smallvec", ] @@ -3010,7 +3023,9 @@ name = "execution_layer" version = "0.1.0" dependencies = [ "alloy-consensus", + "alloy-primitives", "alloy-rlp", + "alloy-rpc-types", "arc-swap", "async-trait", "builder_client", @@ -4357,6 +4372,15 @@ dependencies = [ "either", ] +[[package]] +name = "itertools" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.10" @@ -5471,7 +5495,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37cb4045d5677b7da537f8cb5d0730d5b6414e3cc81c61e4b50e1f0cbdc73909" dependencies = [ "darling", - "itertools", + "itertools 0.10.5", "proc-macro2", "quote", "smallvec", @@ -5743,7 +5767,7 @@ dependencies = [ "hex", "if-addrs 0.6.7", "igd-next", - "itertools", + "itertools 0.10.5", "lazy_static", "lighthouse_metrics", "lighthouse_network", @@ -6060,7 +6084,7 @@ dependencies = [ "derivative", "ethereum_ssz", "ethereum_ssz_derive", - "itertools", + "itertools 0.10.5", "lazy_static", "lighthouse_metrics", "maplit", @@ -8128,7 +8152,7 @@ dependencies = [ "derivative", "ethereum_serde_utils", "ethereum_ssz", - "itertools", + "itertools 0.10.5", "serde", "serde_derive", "smallvec", @@ -8150,7 +8174,7 @@ dependencies = [ "ethereum_ssz_derive", "int_to_bytes", "integer-sqrt", - "itertools", + "itertools 0.10.5", "lazy_static", "lighthouse_metrics", "merkle_proof", @@ -8190,7 +8214,7 @@ dependencies = [ "directory", "ethereum_ssz", "ethereum_ssz_derive", - "itertools", + "itertools 0.10.5", "lazy_static", "leveldb", "lighthouse_metrics", @@ -8263,7 +8287,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "75b9e5728aa1a87141cefd4e7509903fc01fa0dcb108022b1e841a67c5159fc5" dependencies = [ "darling", - "itertools", + "itertools 0.10.5", "proc-macro2", "quote", "smallvec", @@ -8976,6 +9000,8 @@ checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" name = "types" version = "0.2.1" dependencies = [ + "alloy-primitives", + "alloy-rlp", "arbitrary", "beacon_chain", "bls", @@ -8992,7 +9018,7 @@ dependencies = [ "ethereum_ssz_derive", "hex", "int_to_bytes", - "itertools", + "itertools 0.10.5", "kzg", "lazy_static", "log", @@ -9212,7 +9238,7 @@ dependencies = [ "futures", "hex", "hyper 1.1.0", - "itertools", + "itertools 0.10.5", "lazy_static", "libsecp256k1", "lighthouse_metrics", diff --git a/beacon_node/execution_layer/Cargo.toml b/beacon_node/execution_layer/Cargo.toml index 7fee3721d8..d7d889ca73 100644 --- a/beacon_node/execution_layer/Cargo.toml +++ b/beacon_node/execution_layer/Cargo.toml @@ -51,5 +51,7 @@ hash-db = "0.15.2" pretty_reqwest_error = { workspace = true } arc-swap = "1.6.0" eth2_network_config = { workspace = true } -alloy-rlp = "0.3" +alloy-rlp = "0.3.4" alloy-consensus = { git = "https://github.com/alloy-rs/alloy.git", rev = "974d488bab5e21e9f17452a39a4bfa56677367b2" } +alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy.git" } +alloy-primitives = "0.6.2" diff --git a/beacon_node/execution_layer/src/block_hash.rs b/beacon_node/execution_layer/src/block_hash.rs index 074ef8b0c1..df448afc80 100644 --- a/beacon_node/execution_layer/src/block_hash.rs +++ b/beacon_node/execution_layer/src/block_hash.rs @@ -1,13 +1,13 @@ use crate::{ - json_structures::JsonWithdrawal, + json_structures::{EncodableJsonWithdrawal, JsonWithdrawal}, keccak::{keccak256, KeccakHasher}, }; -use ethers_core::utils::rlp::RlpStream; +use alloy_rlp::Encodable; use keccak_hash::KECCAK_EMPTY_LIST_RLP; use triehash::ordered_trie_root; use types::{ - map_execution_block_header_fields_base, Address, EthSpec, ExecutionBlockHash, - ExecutionBlockHeader, ExecutionPayloadRef, Hash256, Hash64, Uint256, + EncodableExecutionBlockHeader, EthSpec, ExecutionBlockHash, ExecutionBlockHeader, + ExecutionPayloadRef, Hash256 }; /// Calculate the block hash of an execution block. @@ -60,36 +60,22 @@ pub fn calculate_execution_block_hash( /// RLP encode a withdrawal. pub fn rlp_encode_withdrawal(withdrawal: &JsonWithdrawal) -> Vec { - let mut rlp_stream = RlpStream::new(); - rlp_stream.begin_list(4); - rlp_stream.append(&withdrawal.index); - rlp_stream.append(&withdrawal.validator_index); - rlp_stream.append(&withdrawal.address); - rlp_stream.append(&withdrawal.amount); - rlp_stream.out().into() + let mut out: Vec = vec![]; + EncodableJsonWithdrawal::from(withdrawal).encode(&mut out); + // rlp_stream.begin_list(4); + // rlp_stream.append(&withdrawal.index); + // rlp_stream.append(&withdrawal.validator_index); + // rlp_stream.append(&withdrawal.address); + // rlp_stream.append(&withdrawal.amount); + // rlp_stream.out().into() + out } /// RLP encode an execution block header. pub fn rlp_encode_block_header(header: &ExecutionBlockHeader) -> Vec { - let mut rlp_header_stream = RlpStream::new(); - rlp_header_stream.begin_unbounded_list(); - map_execution_block_header_fields_base!(&header, |_, field| { - rlp_header_stream.append(field); - }); - if let Some(withdrawals_root) = &header.withdrawals_root { - rlp_header_stream.append(withdrawals_root); - } - if let Some(blob_gas_used) = &header.blob_gas_used { - rlp_header_stream.append(blob_gas_used); - } - if let Some(excess_blob_gas) = &header.excess_blob_gas { - rlp_header_stream.append(excess_blob_gas); - } - if let Some(parent_beacon_block_root) = &header.parent_beacon_block_root { - rlp_header_stream.append(parent_beacon_block_root); - } - rlp_header_stream.finalize_unbounded_list(); - rlp_header_stream.out().into() + let mut out: Vec = vec![]; + EncodableExecutionBlockHeader::from(header).encode(&mut out); + out } #[cfg(test)] @@ -97,6 +83,7 @@ mod test { use super::*; use hex::FromHex; use std::str::FromStr; + use types::{Address, Hash256, Hash64}; fn test_rlp_encoding( header: &ExecutionBlockHeader, diff --git a/beacon_node/execution_layer/src/engine_api/json_structures.rs b/beacon_node/execution_layer/src/engine_api/json_structures.rs index e8641be795..5679d82c2b 100644 --- a/beacon_node/execution_layer/src/engine_api/json_structures.rs +++ b/beacon_node/execution_layer/src/engine_api/json_structures.rs @@ -1,4 +1,5 @@ use super::*; +use alloy_rlp::RlpEncodable; use serde::{Deserialize, Serialize}; use strum::EnumString; use superstruct::superstruct; @@ -362,6 +363,24 @@ impl From for Withdrawal { } } } +#[derive(Debug, PartialEq, Clone, RlpEncodable)] +pub struct EncodableJsonWithdrawal<'a> { + pub index: u64, + pub validator_index: u64, + pub address: &'a [u8], + pub amount: u64, +} + +impl<'a> From<&'a JsonWithdrawal> for EncodableJsonWithdrawal<'a> { + fn from(json_withdrawal: &'a JsonWithdrawal) -> Self { + Self { + index: json_withdrawal.index, + validator_index: json_withdrawal.validator_index, + address: json_withdrawal.address.as_bytes(), + amount: json_withdrawal.amount, + } + } +} #[superstruct( variants(V1, V2, V3), diff --git a/beacon_node/execution_layer/src/keccak.rs b/beacon_node/execution_layer/src/keccak.rs index c4c9689272..c78f1fcd37 100644 --- a/beacon_node/execution_layer/src/keccak.rs +++ b/beacon_node/execution_layer/src/keccak.rs @@ -16,7 +16,8 @@ use hash_db::Hasher; use types::Hash256; pub fn keccak256(bytes: &[u8]) -> Hash256 { - Hash256::from(ethers_core::utils::keccak256(bytes)) + let hash: [u8; 32] = alloy_primitives::utils::keccak256(bytes).into(); + Hash256::from(hash) } /// Keccak hasher. diff --git a/consensus/types/Cargo.toml b/consensus/types/Cargo.toml index db15f53537..e1c11e6b78 100644 --- a/consensus/types/Cargo.toml +++ b/consensus/types/Cargo.toml @@ -52,6 +52,8 @@ serde_json = { workspace = true } smallvec = { workspace = true } maplit = { workspace = true } strum = { workspace = true } +alloy-rlp = { version = "0.3.4", features = ["derive"] } +alloy-primitives = "0.6.2" [dev-dependencies] criterion = { workspace = true } @@ -68,4 +70,4 @@ sqlite = [] # The `arbitrary-fuzz` feature is a no-op provided for backwards compatibility. # For simplicity `Arbitrary` is now derived regardless of the feature's presence. arbitrary-fuzz = [] -portable = ["bls/supranational-portable"] \ No newline at end of file +portable = ["bls/supranational-portable"] diff --git a/consensus/types/src/execution_block_header.rs b/consensus/types/src/execution_block_header.rs index 945222a925..64f11dcfea 100644 --- a/consensus/types/src/execution_block_header.rs +++ b/consensus/types/src/execution_block_header.rs @@ -18,6 +18,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. use crate::{Address, EthSpec, ExecutionPayloadRef, Hash256, Hash64, Uint256}; +use alloy_rlp::RlpEncodable; use metastruct::metastruct; /// Execution block header as used for RLP encoding and Keccak hashing. @@ -89,3 +90,62 @@ impl ExecutionBlockHeader { } } } + +#[derive(Debug, Clone, PartialEq, Eq, RlpEncodable)] +#[rlp(trailing)] +pub struct EncodableExecutionBlockHeader<'a> { + pub parent_hash: &'a [u8], + pub ommers_hash: &'a [u8], + pub beneficiary: &'a [u8], + pub state_root: &'a [u8], + pub transactions_root: &'a [u8], + pub receipts_root: &'a [u8], + pub logs_bloom: &'a [u8], + pub difficulty: u64, + pub number: u64, + pub gas_limit: u64, + pub gas_used: u64, + pub timestamp: u64, + pub extra_data: &'a [u8], + pub mix_hash: &'a [u8], + pub nonce: &'a [u8], + pub base_fee_per_gas: u64, + pub withdrawals_root: Option<&'a [u8]>, + pub blob_gas_used: Option, + pub excess_blob_gas: Option, + pub parent_beacon_block_root: Option<&'a [u8]>, +} + +impl<'a> From<&'a ExecutionBlockHeader> for EncodableExecutionBlockHeader<'a> { + fn from(header: &'a ExecutionBlockHeader) -> Self { + let mut encodable = Self { + parent_hash: header.parent_hash.as_bytes(), + ommers_hash: header.ommers_hash.as_bytes(), + beneficiary: header.beneficiary.as_bytes(), + state_root: header.state_root.as_bytes(), + transactions_root: header.transactions_root.as_bytes(), + receipts_root: header.receipts_root.as_bytes(), + logs_bloom: header.logs_bloom.as_slice(), + difficulty: header.difficulty.as_u64(), // TODO this might panic + number: header.number.as_u64(), // TODO this might panic + gas_limit: header.gas_limit.as_u64(), // TODO this might panic + gas_used: header.gas_used.as_u64(), // TODO this might panic + timestamp: header.timestamp, + extra_data: header.extra_data.as_slice(), + mix_hash: header.mix_hash.as_bytes(), + nonce: header.nonce.as_bytes(), + base_fee_per_gas: header.base_fee_per_gas.as_u64(), // TODO this might panic + withdrawals_root: None, + blob_gas_used: header.blob_gas_used, + excess_blob_gas: header.excess_blob_gas, + parent_beacon_block_root: None, + }; + if let Some(withdrawals_root) = &header.withdrawals_root { + encodable.withdrawals_root = Some(withdrawals_root.as_bytes()); + } + if let Some(parent_beacon_block_root) = &header.parent_beacon_block_root { + encodable.parent_beacon_block_root = Some(parent_beacon_block_root.as_bytes()) + } + encodable + } +} diff --git a/consensus/types/src/execution_transaction.rs b/consensus/types/src/execution_transaction.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/consensus/types/src/lib.rs b/consensus/types/src/lib.rs index b07b497a2a..519cc9e59e 100644 --- a/consensus/types/src/lib.rs +++ b/consensus/types/src/lib.rs @@ -135,7 +135,7 @@ pub use crate::enr_fork_id::EnrForkId; pub use crate::eth1_data::Eth1Data; pub use crate::eth_spec::EthSpecId; pub use crate::execution_block_hash::ExecutionBlockHash; -pub use crate::execution_block_header::ExecutionBlockHeader; +pub use crate::execution_block_header::{EncodableExecutionBlockHeader, ExecutionBlockHeader}; pub use crate::execution_payload::{ ExecutionPayload, ExecutionPayloadCapella, ExecutionPayloadDeneb, ExecutionPayloadMerge, ExecutionPayloadRef, Transaction, Transactions, Withdrawals,