From 738d9ea1683dad94350e26e50357ba5c81f9fedf Mon Sep 17 00:00:00 2001 From: brechtpd Date: Sat, 17 Aug 2024 00:25:51 -0700 Subject: [PATCH] payload built --- bin/reth/src/main.rs | 48 ++++++++++++------- bin/reth/src/node.rs | 8 ++-- bin/reth/src/payload.rs | 8 ++-- .../ethereum/engine-primitives/src/payload.rs | 10 ++-- crates/ethereum/payload/src/lib.rs | 8 ++++ 5 files changed, 55 insertions(+), 27 deletions(-) diff --git a/bin/reth/src/main.rs b/bin/reth/src/main.rs index 68b5c3b2a6c6..634fb149ebae 100644 --- a/bin/reth/src/main.rs +++ b/bin/reth/src/main.rs @@ -59,6 +59,8 @@ use transaction::TransactionTestContext; use wallet::Wallet; use std::{future::Future, marker::PhantomData, pin::Pin, sync::Arc}; +use alloy_rlp::Decodable; + //use alloy_primitives::{Address, B256}; use reth::rpc::types::engine::PayloadAttributes; //use reth_e2e_test_utils::NodeHelperType; @@ -81,7 +83,7 @@ mod traits; //pub(crate) type EthNode = NodeHelperType; /// Helper function to create a new eth payload attributes -pub(crate) fn eth_payload_attributes(timestamp: u64) -> EthPayloadBuilderAttributes { +pub(crate) fn eth_payload_attributes(timestamp: u64, transactions: Vec) -> EthPayloadBuilderAttributes { let attributes = PayloadAttributes { timestamp, prev_randao: B256::ZERO, @@ -89,7 +91,7 @@ pub(crate) fn eth_payload_attributes(timestamp: u64) -> EthPayloadBuilderAttribu withdrawals: Some(vec![]), parent_beacon_block_root: Some(B256::ZERO), }; - EthPayloadBuilderAttributes::new(B256::ZERO, attributes) + EthPayloadBuilderAttributes::new(B256::ZERO, attributes, Some(transactions)) } @@ -110,6 +112,15 @@ static CHAIN_SPEC: Lazy> = Lazy::new(|| { ) }); +pub fn decode_transactions(tx_list: &[u8]) -> Vec { + #[allow(clippy::useless_asref)] + Vec::::decode(&mut tx_list.as_ref()).unwrap_or_else(|e| { + // If decoding fails we need to make an empty block + println!("decode_transactions not successful: {e:?}, use empty tx_list"); + vec![] + }) +} + struct Rollup { ctx: ExExContext, node: TestNodeContext, @@ -174,22 +185,27 @@ impl Rollup { println!("tx_list: {:?}", tx_list); let _call = RollupContractCalls::abi_decode(tx.input(), true)?; - let wallet = Wallet::default(); - let raw_tx = TransactionTestContext::transfer_tx_bytes(1, wallet.inner).await; + let transactions: Vec = decode_transactions(&tx_list); + self.node.payload.transactions = transactions.clone(); + + println!("transactions: {:?}", transactions); + + // let wallet = Wallet::default(); + // let raw_tx = TransactionTestContext::transfer_tx_bytes(1, wallet.inner).await; - // make the node advance - //let tx_hash = self.node.rpc.inject_tx(raw_tx).await?; + // // make the node advance + // //let tx_hash = self.node.rpc.inject_tx(raw_tx).await?; - println!("tx_hash start"); + // println!("tx_hash start"); - let tx_hash = tokio::task::block_in_place(|| { - tokio::runtime::Handle::current().block_on({ - self.node.rpc.inject_tx(raw_tx) - }) - }).unwrap(); + // let tx_hash = tokio::task::block_in_place(|| { + // tokio::runtime::Handle::current().block_on({ + // self.node.rpc.inject_tx(raw_tx) + // }) + // }).unwrap(); - println!("tx_hash start"); + // println!("tx_hash start"); // make the node advance //let (payload, _): (EthBuiltPayload, _) = self.node.advance_block(vec![], eth_payload_attributes).await?; @@ -226,7 +242,7 @@ impl Rollup { tokio::runtime::Handle::current().block_on({ // do something async println!("assert_new_block"); - self.node.assert_new_block(tx_hash, block_hash, block_number) + self.node.assert_new_block(transactions[0].hash(), block_hash, block_number) }) }); @@ -514,7 +530,7 @@ fn main() -> eyre::Result<()> { }; let chain_spec = ChainSpecBuilder::default() - .chain(MAINNET.chain) + .chain(CHAIN_ID.into()) .genesis(serde_json::from_str(include_str!("../../../crates/ethereum/node/tests/assets/genesis.json")).unwrap()) .cancun_activated() .build(); @@ -524,7 +540,7 @@ fn main() -> eyre::Result<()> { .with_network(network_config.clone()) .with_unused_ports() .with_rpc(RpcServerArgs::default().with_unused_ports().with_http()) - .set_dev(false); + .set_dev(true); let NodeHandle { node, node_exit_future: _ } = NodeBuilder::new(node_config.clone()) .testing_node(exec.clone()) diff --git a/bin/reth/src/node.rs b/bin/reth/src/node.rs index 45e6f55cb4b5..8d0c06ef43d4 100644 --- a/bin/reth/src/node.rs +++ b/bin/reth/src/node.rs @@ -14,7 +14,7 @@ use reth::{ }, }; use reth_node_builder::{NodeAddOns, NodeTypes}; -use reth_primitives::{BlockHash, BlockNumber, Bytes, B256}; +use reth_primitives::{BlockHash, BlockNumber, Bytes, TransactionSigned, B256}; use reth_stages_types::StageId; use tokio_stream::StreamExt; @@ -78,7 +78,7 @@ where &mut self, length: u64, tx_generator: impl Fn(u64) -> Pin>>, - attributes_generator: impl Fn(u64) -> ::PayloadBuilderAttributes + attributes_generator: impl Fn(u64, Vec) -> ::PayloadBuilderAttributes + Copy, ) -> eyre::Result< Vec<( @@ -110,7 +110,7 @@ where /// It triggers the resolve payload via engine api and expects the built payload event. pub async fn new_payload( &mut self, - attributes_generator: impl Fn(u64) -> ::PayloadBuilderAttributes, + attributes_generator: impl Fn(u64, Vec) -> ::PayloadBuilderAttributes, ) -> eyre::Result<( <::Engine as PayloadTypes>::BuiltPayload, <::Engine as PayloadTypes>::PayloadBuilderAttributes, @@ -135,7 +135,7 @@ where pub async fn advance_block( &mut self, versioned_hashes: Vec, - attributes_generator: impl Fn(u64) -> ::PayloadBuilderAttributes, + attributes_generator: impl Fn(u64, Vec) -> ::PayloadBuilderAttributes, ) -> eyre::Result<( ::BuiltPayload, <::Engine as PayloadTypes>::PayloadBuilderAttributes, diff --git a/bin/reth/src/payload.rs b/bin/reth/src/payload.rs index c29eccef923d..24343843fa7d 100644 --- a/bin/reth/src/payload.rs +++ b/bin/reth/src/payload.rs @@ -1,6 +1,7 @@ use futures_util::StreamExt; use reth::api::{BuiltPayload, EngineTypes, PayloadBuilderAttributes}; use reth_payload_builder::{Events, PayloadBuilderHandle, PayloadId}; +use reth_primitives::TransactionSigned; use tokio_stream::wrappers::BroadcastStream; /// Helper for payload operations @@ -8,6 +9,7 @@ pub struct PayloadTestContext { pub payload_event_stream: BroadcastStream>, payload_builder: PayloadBuilderHandle, pub timestamp: u64, + pub transactions: Vec, } impl PayloadTestContext { @@ -16,16 +18,16 @@ impl PayloadTestContext { let payload_events = payload_builder.subscribe().await?; let payload_event_stream = payload_events.into_stream(); // Cancun timestamp - Ok(Self { payload_event_stream, payload_builder, timestamp: 1710338135 }) + Ok(Self { payload_event_stream, payload_builder, timestamp: 1710338135, transactions: Vec::new() }) } /// Creates a new payload job from static attributes pub async fn new_payload( &mut self, - attributes_generator: impl Fn(u64) -> E::PayloadBuilderAttributes, + attributes_generator: impl Fn(u64, Vec) -> E::PayloadBuilderAttributes, ) -> eyre::Result { self.timestamp += 1; - let attributes: E::PayloadBuilderAttributes = attributes_generator(self.timestamp); + let attributes: E::PayloadBuilderAttributes = attributes_generator(self.timestamp, self.transactions.clone()); self.payload_builder.new_payload(attributes.clone()).await.unwrap(); Ok(attributes) } diff --git a/crates/ethereum/engine-primitives/src/payload.rs b/crates/ethereum/engine-primitives/src/payload.rs index f9fde7028e32..e56de578298f 100644 --- a/crates/ethereum/engine-primitives/src/payload.rs +++ b/crates/ethereum/engine-primitives/src/payload.rs @@ -5,8 +5,7 @@ use reth_chainspec::ChainSpec; use reth_evm_ethereum::revm_spec_by_timestamp_after_merge; use reth_payload_primitives::{BuiltPayload, PayloadBuilderAttributes}; use reth_primitives::{ - constants::EIP1559_INITIAL_BASE_FEE, Address, BlobTransactionSidecar, EthereumHardfork, Header, - SealedBlock, Withdrawals, B256, U256, + constants::EIP1559_INITIAL_BASE_FEE, Address, BlobTransactionSidecar, EthereumHardfork, Header, SealedBlock, TransactionSigned, Withdrawals, B256, U256 }; use reth_rpc_types::engine::{ ExecutionPayloadEnvelopeV2, ExecutionPayloadEnvelopeV3, ExecutionPayloadEnvelopeV4, @@ -168,6 +167,8 @@ pub struct EthPayloadBuilderAttributes { pub withdrawals: Withdrawals, /// Root of the parent beacon block pub parent_beacon_block_root: Option, + /// Fixed transaction list + pub transactions: Option>, } // === impl EthPayloadBuilderAttributes === @@ -181,7 +182,7 @@ impl EthPayloadBuilderAttributes { /// Creates a new payload builder for the given parent block and the attributes. /// /// Derives the unique [`PayloadId`] for the given parent and attributes - pub fn new(parent: B256, attributes: PayloadAttributes) -> Self { + pub fn new(parent: B256, attributes: PayloadAttributes, transactions: Option>) -> Self { let id = payload_id(&parent, &attributes); Self { @@ -192,6 +193,7 @@ impl EthPayloadBuilderAttributes { prev_randao: attributes.prev_randao, withdrawals: attributes.withdrawals.unwrap_or_default().into(), parent_beacon_block_root: attributes.parent_beacon_block_root, + transactions, } } } @@ -204,7 +206,7 @@ impl PayloadBuilderAttributes for EthPayloadBuilderAttributes { /// /// Derives the unique [`PayloadId`] for the given parent and attributes fn try_new(parent: B256, attributes: PayloadAttributes) -> Result { - Ok(Self::new(parent, attributes)) + Ok(Self::new(parent, attributes, None)) } fn payload_id(&self) -> PayloadId { diff --git a/crates/ethereum/payload/src/lib.rs b/crates/ethereum/payload/src/lib.rs index 2d95f90fdbcb..5120dc2c212a 100644 --- a/crates/ethereum/payload/src/lib.rs +++ b/crates/ethereum/payload/src/lib.rs @@ -297,6 +297,8 @@ where let block_number = initialized_block_env.number.to::(); + println!("brecht: payload builder: {:?}", attributes.transactions); + // apply eip-4788 pre block contract call pre_block_beacon_root_contract_call( &mut db, @@ -435,6 +437,12 @@ where executed_txs.push(tx.into_signed()); } + // Using fixed transaction list if it's set + if attributes.transactions.is_some() { + println!("Using fixed tx list"); + executed_txs = attributes.transactions.unwrap(); + } + // check if we have a better block if !is_better_payload(best_payload.as_ref(), total_fees) { // can skip building the block