perf(EthBuiltPayload): Arc SealedBlock (#12351)

This commit is contained in:
Hai | RISE
2024-11-07 03:33:49 +07:00
committed by GitHub
parent fe2b02828d
commit 302ed291e4
4 changed files with 18 additions and 13 deletions

View File

@ -13,7 +13,7 @@ use reth_primitives::{SealedBlock, Withdrawals};
use reth_rpc_types_compat::engine::payload::{
block_to_payload_v1, block_to_payload_v3, convert_block_to_payload_field_v2,
};
use std::convert::Infallible;
use std::{convert::Infallible, sync::Arc};
/// Contains the built payload.
///
@ -25,7 +25,7 @@ pub struct EthBuiltPayload {
/// Identifier of the payload
pub(crate) id: PayloadId,
/// The built block
pub(crate) block: SealedBlock,
pub(crate) block: Arc<SealedBlock>,
/// Block execution data for the payload, if any.
pub(crate) executed_block: Option<ExecutedBlock>,
/// The fees of the block
@ -45,7 +45,7 @@ impl EthBuiltPayload {
/// Caution: This does not set any [`BlobTransactionSidecar`].
pub const fn new(
id: PayloadId,
block: SealedBlock,
block: Arc<SealedBlock>,
fees: U256,
executed_block: Option<ExecutedBlock>,
requests: Option<Requests>,
@ -59,7 +59,7 @@ impl EthBuiltPayload {
}
/// Returns the built block(sealed)
pub const fn block(&self) -> &SealedBlock {
pub fn block(&self) -> &SealedBlock {
&self.block
}
@ -127,7 +127,7 @@ impl BuiltPayload for &EthBuiltPayload {
// V1 engine_getPayloadV1 response
impl From<EthBuiltPayload> for ExecutionPayloadV1 {
fn from(value: EthBuiltPayload) -> Self {
block_to_payload_v1(value.block)
block_to_payload_v1(Arc::unwrap_or_clone(value.block))
}
}
@ -136,7 +136,10 @@ impl From<EthBuiltPayload> for ExecutionPayloadEnvelopeV2 {
fn from(value: EthBuiltPayload) -> Self {
let EthBuiltPayload { block, fees, .. } = value;
Self { block_value: fees, execution_payload: convert_block_to_payload_field_v2(block) }
Self {
block_value: fees,
execution_payload: convert_block_to_payload_field_v2(Arc::unwrap_or_clone(block)),
}
}
}
@ -145,7 +148,7 @@ impl From<EthBuiltPayload> for ExecutionPayloadEnvelopeV3 {
let EthBuiltPayload { block, fees, sidecars, .. } = value;
Self {
execution_payload: block_to_payload_v3(block),
execution_payload: block_to_payload_v3(Arc::unwrap_or_clone(block)),
block_value: fees,
// From the engine API spec:
//
@ -166,7 +169,7 @@ impl From<EthBuiltPayload> for ExecutionPayloadEnvelopeV4 {
let EthBuiltPayload { block, fees, sidecars, requests, .. } = value;
Self {
execution_payload: block_to_payload_v3(block),
execution_payload: block_to_payload_v3(Arc::unwrap_or_clone(block)),
block_value: fees,
// From the engine API spec:
//

View File

@ -439,12 +439,12 @@ where
body: BlockBody { transactions: executed_txs, ommers: vec![], withdrawals },
};
let sealed_block = block.seal_slow();
let sealed_block = Arc::new(block.seal_slow());
debug!(target: "payload_builder", ?sealed_block, "sealed built block");
// create the executed block data
let executed = ExecutedBlock {
block: Arc::new(sealed_block.clone()),
block: sealed_block.clone(),
senders: Arc::new(executed_senders),
execution_output: Arc::new(execution_outcome),
hashed_state: Arc::new(hashed_state),

View File

@ -26,6 +26,7 @@
//! ```
//! use std::future::Future;
//! use std::pin::Pin;
//! use std::sync::Arc;
//! use std::task::{Context, Poll};
//! use alloy_primitives::U256;
//! use reth_payload_builder::{EthBuiltPayload, PayloadBuilderError, KeepPayloadJobAlive, EthPayloadBuilderAttributes, PayloadJob, PayloadJobGenerator, PayloadKind};
@ -56,7 +57,7 @@
//!
//! fn best_payload(&self) -> Result<EthBuiltPayload, PayloadBuilderError> {
//! // NOTE: some fields are omitted here for brevity
//! let payload = Block {
//! let block = Block {
//! header: Header {
//! parent_hash: self.attributes.parent,
//! timestamp: self.attributes.timestamp,
@ -65,7 +66,7 @@
//! },
//! ..Default::default()
//! };
//! let payload = EthBuiltPayload::new(self.attributes.id, payload.seal_slow(), U256::ZERO, None, None);
//! let payload = EthBuiltPayload::new(self.attributes.id, Arc::new(block.seal_slow()), U256::ZERO, None, None);
//! Ok(payload)
//! }
//!

View File

@ -13,6 +13,7 @@ use reth_provider::CanonStateNotification;
use std::{
future::Future,
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
@ -86,7 +87,7 @@ impl PayloadJob for TestPayloadJob {
fn best_payload(&self) -> Result<EthBuiltPayload, PayloadBuilderError> {
Ok(EthBuiltPayload::new(
self.attr.payload_id(),
Block::default().seal_slow(),
Arc::new(Block::default().seal_slow()),
U256::ZERO,
Some(ExecutedBlock::default()),
Some(Default::default()),