Remove use of ethers_core::RlpStream

This commit is contained in:
dospore
2024-02-16 08:54:24 +10:00
parent 13956a0741
commit 44f24707c9
9 changed files with 147 additions and 50 deletions

58
Cargo.lock generated
View File

@@ -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",

View File

@@ -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"

View File

@@ -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<T: EthSpec>(
/// RLP encode a withdrawal.
pub fn rlp_encode_withdrawal(withdrawal: &JsonWithdrawal) -> Vec<u8> {
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<u8> = 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<u8> {
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<u8> = 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,

View File

@@ -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<JsonWithdrawal> 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),

View File

@@ -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.

View File

@@ -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"]
portable = ["bls/supranational-portable"]

View File

@@ -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<u64>,
pub excess_blob_gas: Option<u64>,
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
}
}

View File

@@ -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,