Skip to content

Commit

Permalink
chore: use native conversions for payload to block (#13608)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Jan 2, 2025
1 parent 1b9bdcc commit b69c2eb
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 281 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion crates/payload/validator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ workspace = true
reth-chainspec.workspace = true
reth-primitives.workspace = true
reth-primitives-traits.workspace = true
reth-rpc-types-compat.workspace = true

# alloy
alloy-rpc-types = { workspace = true, features = ["engine"] }
3 changes: 1 addition & 2 deletions crates/payload/validator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use alloy_rpc_types::engine::{
use reth_chainspec::EthereumHardforks;
use reth_primitives::{BlockBody, BlockExt, Header, SealedBlock};
use reth_primitives_traits::SignedTransaction;
use reth_rpc_types_compat::engine::payload::try_into_block;
use std::sync::Arc;

/// Execution payload validator.
Expand Down Expand Up @@ -121,7 +120,7 @@ impl<ChainSpec: EthereumHardforks> ExecutionPayloadValidator<ChainSpec> {
let expected_hash = payload.block_hash();

// First parse the block
let sealed_block = try_into_block(payload, &sidecar)?.seal_slow();
let sealed_block = payload.try_into_block_with_sidecar(&sidecar)?.seal_slow();

// Ensure the hash included in the payload matches the block hash
if expected_hash != sealed_block.hash() {
Expand Down
6 changes: 2 additions & 4 deletions crates/rpc/rpc-engine-api/src/engine_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ use reth_payload_primitives::{
};
use reth_primitives::EthereumHardfork;
use reth_rpc_api::EngineApiServer;
use reth_rpc_types_compat::engine::payload::{
convert_payload_input_v2_to_payload, convert_to_payload_body_v1,
};
use reth_rpc_types_compat::engine::payload::convert_to_payload_body_v1;
use reth_storage_api::{BlockReader, HeaderProvider, StateProviderFactory};
use reth_tasks::TaskSpawner;
use reth_transaction_pool::TransactionPool;
Expand Down Expand Up @@ -176,7 +174,7 @@ where
&self,
payload: ExecutionPayloadInputV2,
) -> EngineApiResult<PayloadStatus> {
let payload = convert_payload_input_v2_to_payload(payload);
let payload = payload.into_payload();
let payload_or_attrs =
PayloadOrAttributes::<'_, EngineT::PayloadAttributes>::from_execution_payload(
&payload, None,
Expand Down
56 changes: 10 additions & 46 deletions crates/rpc/rpc-engine-api/tests/it/payload.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Some payload tests
use alloy_eips::eip4895::Withdrawals;
use alloy_primitives::{Bytes, U256};
use alloy_primitives::Bytes;
use alloy_rlp::{Decodable, Error as RlpError};
use alloy_rpc_types_engine::{
ExecutionPayload, ExecutionPayloadBodyV1, ExecutionPayloadSidecar, ExecutionPayloadV1,
Expand All @@ -10,11 +10,10 @@ use alloy_rpc_types_engine::{
use assert_matches::assert_matches;
use reth_primitives::{proofs, Block, SealedBlock, SealedHeader, TransactionSigned};
use reth_rpc_types_compat::engine::payload::{
block_to_payload, block_to_payload_v1, convert_to_payload_body_v1, try_into_sealed_block,
try_payload_v1_to_block,
block_to_payload, block_to_payload_v1, convert_to_payload_body_v1,
};
use reth_testing_utils::generators::{
self, random_block, random_block_range, random_header, BlockParams, BlockRangeParams, Rng,
self, random_block, random_block_range, BlockParams, BlockRangeParams, Rng,
};

fn transform_block<F: FnOnce(Block) -> Block>(src: SealedBlock, f: F) -> ExecutionPayload {
Expand Down Expand Up @@ -56,7 +55,7 @@ fn payload_body_roundtrip() {
}

#[test]
fn payload_validation() {
fn payload_validation_conversion() {
let mut rng = generators::rng();
let parent = rng.gen();
let block = random_block(
Expand All @@ -77,7 +76,8 @@ fn payload_validation() {
});

assert_matches!(
try_into_sealed_block(block_with_valid_extra_data, &ExecutionPayloadSidecar::none()),
block_with_valid_extra_data
.try_into_block_with_sidecar::<TransactionSigned>(&ExecutionPayloadSidecar::none()),
Ok(_)
);

Expand All @@ -88,7 +88,7 @@ fn payload_validation() {
b
});
assert_matches!(
try_into_sealed_block(invalid_extra_data_block, &ExecutionPayloadSidecar::none()),
invalid_extra_data_block.try_into_block_with_sidecar::<TransactionSigned>(&ExecutionPayloadSidecar::none()),
Err(PayloadError::ExtraData(data)) if data == block_with_invalid_extra_data
);

Expand All @@ -98,52 +98,16 @@ fn payload_validation() {
b
});
assert_matches!(
try_into_sealed_block(block_with_zero_base_fee, &ExecutionPayloadSidecar::none()),
block_with_zero_base_fee.try_into_block_with_sidecar::<TransactionSigned>(&ExecutionPayloadSidecar::none()),
Err(PayloadError::BaseFee(val)) if val.is_zero()
);

// Invalid encoded transactions
let mut payload_with_invalid_txs: ExecutionPayloadV1 = block_to_payload_v1(block.clone());
let mut payload_with_invalid_txs: ExecutionPayloadV1 = block_to_payload_v1(block);

payload_with_invalid_txs.transactions.iter_mut().for_each(|tx| {
*tx = Bytes::new();
});
let payload_with_invalid_txs =
try_payload_v1_to_block::<TransactionSigned>(payload_with_invalid_txs);
let payload_with_invalid_txs = payload_with_invalid_txs.try_into_block::<TransactionSigned>();
assert_matches!(payload_with_invalid_txs, Err(PayloadError::Decode(RlpError::InputTooShort)));

// Non empty ommers
let block_with_ommers = transform_block(block.clone(), |mut b| {
b.body.ommers.push(random_header(&mut rng, 100, None).unseal());
b
});
assert_matches!(
try_into_sealed_block(block_with_ommers.clone(), &ExecutionPayloadSidecar::none()),
Err(PayloadError::BlockHash { consensus, .. })
if consensus == block_with_ommers.block_hash()
);

// None zero difficulty
let block_with_difficulty = transform_block(block.clone(), |mut b| {
b.header.difficulty = U256::from(1);
b
});
assert_matches!(
try_into_sealed_block(block_with_difficulty.clone(), &ExecutionPayloadSidecar::none()),
Err(PayloadError::BlockHash { consensus, .. }) if consensus == block_with_difficulty.block_hash()
);

// None zero nonce
let block_with_nonce = transform_block(block.clone(), |mut b| {
b.header.nonce = 1u64.into();
b
});
assert_matches!(
try_into_sealed_block(block_with_nonce.clone(), &ExecutionPayloadSidecar::none()),
Err(PayloadError::BlockHash { consensus, .. }) if consensus == block_with_nonce.block_hash()
);

// Valid block
let valid_block = block;
assert_matches!(TryInto::<SealedBlock>::try_into(valid_block), Ok(_));
}
1 change: 0 additions & 1 deletion crates/rpc/rpc-types-compat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ reth-primitives-traits.workspace = true
# ethereum
alloy-eips.workspace = true
alloy-primitives.workspace = true
alloy-rlp.workspace = true
alloy-rpc-types-eth = { workspace = true, default-features = false, features = ["serde"] }
alloy-rpc-types-engine.workspace = true
alloy-consensus.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc/rpc-types-compat/src/engine/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//! Standalone functions for engine specific rpc type conversions
pub mod payload;
pub use payload::{block_to_payload_v1, try_into_sealed_block, try_payload_v1_to_block};
pub use payload::block_to_payload_v1;
Loading

0 comments on commit b69c2eb

Please sign in to comment.