Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ resolver = "2"
edition = "2021"

[workspace.dependencies]
alloy-primitives = { version = "0.7.7" }
anyhow = "1"
arbitrary = { version = "1", features = ["derive"] }
async-channel = "1.9.0"
Expand Down
3 changes: 2 additions & 1 deletion beacon_node/execution_layer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = { workspace = true }
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
alloy-primitives = { workspace = true }
types = { workspace = true }
tokio = { workspace = true }
slog = { workspace = true }
Expand Down Expand Up @@ -48,6 +49,6 @@ 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" }
lighthouse_version = { workspace = true }
41 changes: 11 additions & 30 deletions beacon_node/execution_layer/src/block_hash.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -60,43 +60,24 @@ pub fn calculate_execution_block_hash<E: 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);
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)]
mod test {
use super::*;
use hex::FromHex;
use std::str::FromStr;
use types::{Address, Hash256, Hash64};

fn test_rlp_encoding(
header: &ExecutionBlockHeader,
Expand Down
19 changes: 19 additions & 0 deletions beacon_node/execution_layer/src/engine_api/json_structures.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use alloy_rlp::RlpEncodable;
use serde::{Deserialize, Serialize};
use strum::EnumString;
use superstruct::superstruct;
Expand Down Expand Up @@ -463,6 +464,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),
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/execution_layer/src/keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use hash_db::Hasher;
use types::Hash256;

pub fn keccak256(bytes: &[u8]) -> Hash256 {
Hash256::from(ethers_core::utils::keccak256(bytes))
Hash256::from(alloy_primitives::utils::keccak256(bytes).as_ref())
}

/// Keccak hasher.
Expand Down
2 changes: 2 additions & 0 deletions consensus/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ name = "benches"
harness = false

[dependencies]
alloy-primitives = { workspace = true, features = ["rlp"] }
merkle_proof = { workspace = true }
bls = { workspace = true, features = ["arbitrary"] }
kzg = { workspace = true }
Expand Down Expand Up @@ -50,6 +51,7 @@ metastruct = "0.1.0"
serde_json = { workspace = true }
smallvec = { workspace = true }
maplit = { workspace = true }
alloy-rlp = { version = "0.3.4", features = ["derive"] }
milhouse = { workspace = true }
rpds = { workspace = true }

Expand Down
72 changes: 72 additions & 0 deletions consensus/types/src/execution_block_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -89,3 +90,74 @@ 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: alloy_primitives::U256,
pub number: alloy_primitives::U256,
pub gas_limit: alloy_primitives::U256,
pub gas_used: alloy_primitives::U256,
pub timestamp: u64,
pub extra_data: &'a [u8],
pub mix_hash: &'a [u8],
pub nonce: &'a [u8],
pub base_fee_per_gas: alloy_primitives::U256,
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: U256Shim(header.difficulty).into(),
number: U256Shim(header.number).into(),
gas_limit: U256Shim(header.gas_limit).into(),
gas_used: U256Shim(header.gas_used).into(),
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: U256Shim(header.base_fee_per_gas).into(),
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
}
}

// TODO(alloy) this shim can be removed once we fully migrate
// from ethereum types to alloy primitives
struct U256Shim(Uint256);

impl From<U256Shim> for alloy_primitives::U256 {
fn from(value: U256Shim) -> Self {
let mut buffer: [u8; 32] = [0; 32];
value.0.to_little_endian(&mut buffer);
Self::from_le_slice(&buffer)
}
}
Empty file.
2 changes: 1 addition & 1 deletion consensus/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pub use crate::epoch_cache::{EpochCache, EpochCacheError, EpochCacheKey};
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_layer_withdrawal_request::ExecutionLayerWithdrawalRequest;
pub use crate::execution_payload::{
ExecutionPayload, ExecutionPayloadBellatrix, ExecutionPayloadCapella, ExecutionPayloadDeneb,
Expand Down
Loading