@@ -4,6 +4,7 @@ use alloy_consensus::transaction::TransactionMeta;
44use alloy_primitives:: { Address , Sealable , TxHash , U256 } ;
55use alloy_rpc_types:: Withdrawals ;
66use alloy_rpc_types:: { BlockTransactions , Header , TransactionInfo } ;
7+ use arc_swap:: ArcSwap ;
78use op_alloy_consensus:: OpTxEnvelope ;
89use op_alloy_network:: Optimism ;
910use op_alloy_rpc_types:: OpTransactionReceipt ;
@@ -20,11 +21,7 @@ use rollup_boost::{
2021 FlashblockBuilder , FlashblocksPayloadV1 , OpExecutionPayloadEnvelope , PayloadVersion ,
2122} ;
2223use serde:: { Deserialize , Serialize } ;
23- use std:: {
24- collections:: HashMap ,
25- str:: FromStr ,
26- sync:: { Arc , Mutex } ,
27- } ;
24+ use std:: { collections:: HashMap , str:: FromStr , sync:: Arc } ;
2825
2926#[ derive( Debug , Deserialize , Serialize , Clone , Default ) ]
3027pub struct Metadata {
@@ -35,37 +32,44 @@ pub struct Metadata {
3532
3633#[ derive( Clone ) ]
3734pub struct FlashblocksCache {
38- inner : Arc < Mutex < FlashblocksCacheInner > > ,
35+ inner : Arc < ArcSwap < FlashblocksCacheInner > > ,
36+ // TODO: add arc_swap::Cache to speed it up even more
3937}
4038
4139impl FlashblocksCache {
4240 pub fn new ( chain_spec : Arc < OpChainSpec > ) -> Self {
4341 Self {
44- inner : Arc :: new ( Mutex :: new ( FlashblocksCacheInner :: new ( chain_spec) ) ) ,
42+ inner : Arc :: new ( ArcSwap :: from_pointee ( FlashblocksCacheInner :: new (
43+ chain_spec,
44+ ) ) ) ,
4545 }
4646 }
4747
4848 pub fn get_block ( & self , full : bool ) -> Option < RpcBlock < Optimism > > {
49- self . inner . lock ( ) . unwrap ( ) . get_block ( full)
49+ ArcSwap :: load ( & self . inner ) . get_block ( full)
5050 }
5151
5252 pub fn get_transaction_count ( & self , address : Address ) -> Option < u64 > {
53- self . inner . lock ( ) . unwrap ( ) . get_nonce ( address)
53+ ArcSwap :: load ( & self . inner ) . get_nonce ( address)
5454 }
5555
5656 pub fn get_balance ( & self , address : Address ) -> Option < U256 > {
57- self . inner . lock ( ) . unwrap ( ) . get_balance ( address)
57+ ArcSwap :: load ( & self . inner ) . get_balance ( address)
5858 }
5959
6060 pub fn get_receipt ( & self , tx_hash : & TxHash ) -> Option < RpcReceipt < Optimism > > {
61- self . inner . lock ( ) . unwrap ( ) . get_receipt ( tx_hash)
61+ ArcSwap :: load ( & self . inner ) . get_receipt ( tx_hash)
6262 }
6363
6464 pub fn process_payload ( & self , payload : FlashblocksPayloadV1 ) -> eyre:: Result < ( ) > {
65- self . inner . lock ( ) . unwrap ( ) . process_payload ( payload)
65+ let mut new_state = FlashblocksCacheInner :: clone ( & self . inner . load_full ( ) ) ;
66+ new_state. process_payload ( payload) ?;
67+ self . inner . store ( Arc :: new ( new_state) ) ;
68+ Ok ( ( ) )
6669 }
6770}
6871
72+ #[ derive( Clone ) ]
6973struct FlashblocksCacheInner {
7074 chain_spec : Arc < OpChainSpec > ,
7175 builder : FlashblockBuilder ,
0 commit comments