1+ use std:: sync:: Arc ;
2+
13use alloy_consensus:: { Header , Sealed } ;
24use alloy_eips:: BlockNumberOrTag ;
35use alloy_primitives:: {
6+ Address , B256 , BlockNumber , TxHash , U256 ,
47 map:: foldhash:: { HashMap , HashMapExt } ,
5- Address , BlockNumber , TxHash , B256 , U256 ,
68} ;
79use alloy_provider:: network:: TransactionResponse ;
8- use alloy_rpc_types:: { state:: StateOverride , BlockTransactions } ;
10+ use alloy_rpc_types:: { BlockTransactions , state:: StateOverride } ;
911use alloy_rpc_types_eth:: { Filter , Header as RPCHeader , Log } ;
12+ use arc_swap:: Guard ;
1013use eyre:: eyre;
1114use op_alloy_network:: Optimism ;
1215use op_alloy_rpc_types:: { OpTransactionReceipt , Transaction } ;
1316use reth:: revm:: { db:: Cache , state:: EvmState } ;
14- use reth_rpc_eth_api:: RpcBlock ;
17+ use reth_rpc_convert:: RpcTransaction ;
18+ use reth_rpc_eth_api:: { RpcBlock , RpcReceipt } ;
1519
16- use crate :: subscription:: Flashblock ;
20+ use crate :: { rpc :: PendingBlocksAPI , subscription:: Flashblock } ;
1721
1822pub struct PendingBlocksBuilder {
1923 flashblocks : Vec < Flashblock > ,
@@ -25,6 +29,7 @@ pub struct PendingBlocksBuilder {
2529 transaction_receipts : HashMap < B256 , OpTransactionReceipt > ,
2630 transactions_by_hash : HashMap < B256 , Transaction > ,
2731 transaction_state : HashMap < B256 , EvmState > ,
32+ transaction_senders : HashMap < B256 , Address > ,
2833 state_overrides : Option < StateOverride > ,
2934
3035 db_cache : Cache ,
@@ -41,6 +46,7 @@ impl PendingBlocksBuilder {
4146 transaction_receipts : HashMap :: new ( ) ,
4247 transactions_by_hash : HashMap :: new ( ) ,
4348 transaction_state : HashMap :: new ( ) ,
49+ transaction_senders : HashMap :: new ( ) ,
4450 state_overrides : None ,
4551 db_cache : Cache :: default ( ) ,
4652 }
@@ -77,6 +83,12 @@ impl PendingBlocksBuilder {
7783 self
7884 }
7985
86+ #[ inline]
87+ pub ( crate ) fn with_transaction_sender ( & mut self , hash : B256 , sender : Address ) -> & Self {
88+ self . transaction_senders . insert ( hash, sender) ;
89+ self
90+ }
91+
8092 #[ inline]
8193 pub ( crate ) fn increment_nonce ( & mut self , sender : Address ) -> & Self {
8294 let zero = U256 :: from ( 0 ) ;
@@ -122,6 +134,7 @@ impl PendingBlocksBuilder {
122134 transaction_receipts : self . transaction_receipts ,
123135 transactions_by_hash : self . transactions_by_hash ,
124136 transaction_state : self . transaction_state ,
137+ transaction_senders : self . transaction_senders ,
125138 state_overrides : self . state_overrides ,
126139 db_cache : self . db_cache ,
127140 } )
@@ -139,6 +152,7 @@ pub struct PendingBlocks {
139152 transaction_receipts : HashMap < B256 , OpTransactionReceipt > ,
140153 transactions_by_hash : HashMap < B256 , Transaction > ,
141154 transaction_state : HashMap < B256 , EvmState > ,
155+ transaction_senders : HashMap < B256 , Address > ,
142156 state_overrides : Option < StateOverride > ,
143157
144158 db_cache : Cache ,
@@ -153,6 +167,10 @@ impl PendingBlocks {
153167 BlockNumberOrTag :: Number ( self . headers . first ( ) . unwrap ( ) . number - 1 )
154168 }
155169
170+ pub fn earliest_block_number ( & self ) -> BlockNumber {
171+ self . headers . first ( ) . unwrap ( ) . number
172+ }
173+
156174 pub fn latest_flashblock_index ( & self ) -> u64 {
157175 self . flashblocks . last ( ) . unwrap ( ) . index
158176 }
@@ -165,8 +183,12 @@ impl PendingBlocks {
165183 self . flashblocks . clone ( )
166184 }
167185
168- pub fn get_transaction_state ( & self , hash : B256 ) -> Option < EvmState > {
169- self . transaction_state . get ( & hash) . cloned ( )
186+ pub fn get_transaction_state ( & self , hash : & B256 ) -> Option < EvmState > {
187+ self . transaction_state . get ( hash) . cloned ( )
188+ }
189+
190+ pub fn get_transaction_sender ( & self , tx_hash : & B256 ) -> Option < Address > {
191+ self . transaction_senders . get ( tx_hash) . cloned ( )
170192 }
171193
172194 pub fn get_db_cache ( & self ) -> Cache {
@@ -236,3 +258,43 @@ impl PendingBlocks {
236258 logs
237259 }
238260}
261+
262+ impl PendingBlocksAPI for Guard < Option < Arc < PendingBlocks > > > {
263+ fn get_canonical_block_number ( & self ) -> BlockNumberOrTag {
264+ self . as_ref ( ) . map ( |pb| pb. canonical_block_number ( ) ) . unwrap_or ( BlockNumberOrTag :: Latest )
265+ }
266+
267+ fn get_transaction_count ( & self , address : Address ) -> U256 {
268+ self . as_ref ( ) . map ( |pb| pb. get_transaction_count ( address) ) . unwrap_or_else ( || U256 :: from ( 0 ) )
269+ }
270+
271+ fn get_block ( & self , full : bool ) -> Option < RpcBlock < Optimism > > {
272+ self . as_ref ( ) . map ( |pb| pb. get_latest_block ( full) )
273+ }
274+
275+ fn get_transaction_receipt (
276+ & self ,
277+ tx_hash : alloy_primitives:: TxHash ,
278+ ) -> Option < RpcReceipt < Optimism > > {
279+ self . as_ref ( ) . and_then ( |pb| pb. get_receipt ( tx_hash) )
280+ }
281+
282+ fn get_transaction_by_hash (
283+ & self ,
284+ tx_hash : alloy_primitives:: TxHash ,
285+ ) -> Option < RpcTransaction < Optimism > > {
286+ self . as_ref ( ) . and_then ( |pb| pb. get_transaction_by_hash ( tx_hash) )
287+ }
288+
289+ fn get_balance ( & self , address : Address ) -> Option < U256 > {
290+ self . as_ref ( ) . and_then ( |pb| pb. get_balance ( address) )
291+ }
292+
293+ fn get_state_overrides ( & self ) -> Option < StateOverride > {
294+ self . as_ref ( ) . map ( |pb| pb. get_state_overrides ( ) ) . unwrap_or_default ( )
295+ }
296+
297+ fn get_pending_logs ( & self , filter : & Filter ) -> Vec < Log > {
298+ self . as_ref ( ) . map ( |pb| pb. get_pending_logs ( filter) ) . unwrap_or_default ( )
299+ }
300+ }
0 commit comments