reth_rpc_eth_types/
pending_block.rs1use std::{sync::Arc, time::Instant};
6
7use crate::block::{BlockAndReceipts, SharedReceipts};
8use alloy_consensus::BlockHeader;
9use alloy_eips::{BlockId, BlockNumberOrTag};
10use alloy_primitives::{BlockHash, TxHash, B256};
11use derive_more::Constructor;
12use reth_chain_state::{BlockState, ExecutedBlock};
13use reth_ethereum_primitives::Receipt;
14use reth_evm::{ConfigureEvm, EvmEnvFor};
15use reth_primitives_traits::{
16 Block, BlockTy, IndexedTx, NodePrimitives, ReceiptTy, RecoveredBlock, SealedHeader,
17};
18use reth_rpc_convert::{RpcConvert, RpcTypes};
19
20#[derive(Debug, Clone, Constructor)]
22pub struct PendingBlockEnv<Evm: ConfigureEvm> {
23 pub evm_env: EvmEnvFor<Evm>,
25 pub origin: PendingBlockEnvOrigin<BlockTy<Evm::Primitives>, ReceiptTy<Evm::Primitives>>,
27}
28
29#[derive(Clone, Debug)]
31pub enum PendingBlockEnvOrigin<B: Block = reth_ethereum_primitives::Block, R = Receipt> {
32 ActualPending(Arc<RecoveredBlock<B>>, SharedReceipts<R>),
34 DerivedFromLatest(SealedHeader<B::Header>),
41}
42
43impl<B: Block, R> PendingBlockEnvOrigin<B, R> {
44 pub const fn is_actual_pending(&self) -> bool {
46 matches!(self, Self::ActualPending(_, _))
47 }
48
49 pub fn into_actual_pending(self) -> Option<Arc<RecoveredBlock<B>>> {
51 match self {
52 Self::ActualPending(block, _) => Some(block),
53 _ => None,
54 }
55 }
56
57 pub fn state_block_id(&self) -> BlockId {
62 match self {
63 Self::ActualPending(_, _) => BlockNumberOrTag::Pending.into(),
64 Self::DerivedFromLatest(latest) => BlockId::Hash(latest.hash().into()),
65 }
66 }
67
68 pub fn build_target_hash(&self) -> B256 {
74 match self {
75 Self::ActualPending(block, _) => block.header().parent_hash(),
76 Self::DerivedFromLatest(latest) => latest.hash(),
77 }
78 }
79}
80
81pub type PendingBlockAndReceipts<N> = BlockAndReceipts<N>;
83
84#[derive(Debug, Clone, Constructor)]
86pub struct PendingBlock<N: NodePrimitives> {
87 pub expires_at: Instant,
89 pub receipts: SharedReceipts<ReceiptTy<N>>,
91 pub executed_block: ExecutedBlock<N>,
93}
94
95impl<N: NodePrimitives> PendingBlock<N> {
96 pub fn with_executed_block(expires_at: Instant, executed_block: ExecutedBlock<N>) -> Self {
99 Self {
100 expires_at,
101 receipts: Arc::clone(&executed_block.execution_output).into(),
102 executed_block,
103 }
104 }
105
106 pub const fn block(&self) -> &Arc<RecoveredBlock<BlockTy<N>>> {
108 &self.executed_block.recovered_block
109 }
110
111 pub fn into_block_and_receipts(self) -> PendingBlockAndReceipts<N> {
114 BlockAndReceipts { block: self.executed_block.recovered_block, receipts: self.receipts }
115 }
116
117 pub fn to_block_and_receipts(&self) -> PendingBlockAndReceipts<N> {
120 BlockAndReceipts {
121 block: self.executed_block.recovered_block.clone(),
122 receipts: self.receipts.clone(),
123 }
124 }
125
126 pub fn parent_hash(&self) -> BlockHash {
128 self.executed_block.recovered_block().parent_hash()
129 }
130
131 pub fn find_transaction_and_receipt_by_hash(
135 &self,
136 tx_hash: TxHash,
137 ) -> Option<(IndexedTx<'_, N::Block>, &N::Receipt)> {
138 let indexed_tx = self.executed_block.recovered_block().find_indexed(tx_hash)?;
139 let receipt = self.receipts.get(indexed_tx.index())?;
140 Some((indexed_tx, receipt))
141 }
142
143 pub fn find_and_convert_transaction_receipt<C>(
148 &self,
149 tx_hash: TxHash,
150 converter: &C,
151 ) -> Option<Result<<C::Network as RpcTypes>::Receipt, C::Error>>
152 where
153 C: RpcConvert<Primitives = N>,
154 {
155 self.to_block_and_receipts().find_and_convert_transaction_receipt(tx_hash, converter)
156 }
157}
158
159impl<N: NodePrimitives> From<PendingBlock<N>> for BlockState<N> {
160 fn from(pending_block: PendingBlock<N>) -> Self {
161 Self::new(pending_block.executed_block)
162 }
163}