introduce a shim to convert between the two u256 types

This commit is contained in:
Eitan Seri-Levi
2024-07-22 10:00:00 -07:00
parent 9a935b4626
commit 94f64c0d05
3 changed files with 56 additions and 19 deletions

View File

@@ -9,6 +9,7 @@ name = "benches"
harness = false
[dependencies]
alloy-primitives = "0.7.7"
merkle_proof = { workspace = true }
bls = { workspace = true, features = ["arbitrary"] }
kzg = { workspace = true }

View File

@@ -101,15 +101,15 @@ pub struct EncodableExecutionBlockHeader<'a> {
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 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: u64,
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>,
@@ -126,15 +126,15 @@ impl<'a> From<&'a ExecutionBlockHeader> for EncodableExecutionBlockHeader<'a> {
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
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: header.base_fee_per_gas.as_u64(), // TODO this might panic
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,
@@ -149,3 +149,16 @@ impl<'a> From<&'a ExecutionBlockHeader> for EncodableExecutionBlockHeader<'a> {
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)
}
}