@@ -6,7 +6,7 @@ use edr_eth::{
6
6
use parking_lot:: { RwLock , RwLockUpgradableReadGuard , RwLockWriteGuard } ;
7
7
use revm:: primitives:: { HashMap , HashSet } ;
8
8
9
- use super :: { sparse, SparseBlockchainStorage } ;
9
+ use super :: { sparse, InsertError , SparseBlockchainStorage } ;
10
10
use crate :: { state:: StateDiff , Block , LocalBlock } ;
11
11
12
12
/// A reservation for a sequence of blocks that have not yet been inserted into
@@ -225,24 +225,21 @@ impl<BlockT: Block + Clone> ReservableSparseBlockchainStorage<BlockT> {
225
225
impl < BlockT : Block + Clone + From < LocalBlock > > ReservableSparseBlockchainStorage < BlockT > {
226
226
/// Retrieves the block by number, if it exists.
227
227
#[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all) ) ]
228
- pub fn block_by_number ( & self , number : u64 ) -> Option < BlockT > {
229
- self . try_fulfilling_reservation ( number)
230
- . or_else ( || self . storage . read ( ) . block_by_number ( number) . cloned ( ) )
228
+ pub fn block_by_number ( & self , number : u64 ) -> Result < Option < BlockT > , InsertError > {
229
+ Ok ( self
230
+ . try_fulfilling_reservation ( number) ?
231
+ . or_else ( || self . storage . read ( ) . block_by_number ( number) . cloned ( ) ) )
231
232
}
232
233
233
- /// Inserts a block without checking its validity.
234
- ///
235
- /// # Safety
236
- ///
237
- /// Ensure that the instance does not contain a block with the same hash or
238
- /// number, nor any transactions with the same hash.
234
+ /// Insert a block into the storage. Errors if a block with the same hash or
235
+ /// number already exists.
239
236
#[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all) ) ]
240
- pub unsafe fn insert_block_unchecked (
237
+ pub fn insert_block (
241
238
& mut self ,
242
239
block : LocalBlock ,
243
240
state_diff : StateDiff ,
244
241
total_difficulty : U256 ,
245
- ) -> & BlockT {
242
+ ) -> Result < & BlockT , InsertError > {
246
243
self . last_block_number = block. header ( ) . number ;
247
244
self . number_to_diff_index
248
245
. insert ( self . last_block_number , self . state_diffs . len ( ) ) ;
@@ -252,17 +249,13 @@ impl<BlockT: Block + Clone + From<LocalBlock>> ReservableSparseBlockchainStorage
252
249
let receipts: Vec < _ > = block. transaction_receipts ( ) . to_vec ( ) ;
253
250
let block = BlockT :: from ( block) ;
254
251
255
- self . storage
256
- . get_mut ( )
257
- . insert_receipts_unchecked ( receipts, block. clone ( ) ) ;
252
+ self . storage . get_mut ( ) . insert_receipts ( receipts) ?;
258
253
259
- self . storage
260
- . get_mut ( )
261
- . insert_block_unchecked ( block, total_difficulty)
254
+ self . storage . get_mut ( ) . insert_block ( block, total_difficulty)
262
255
}
263
256
264
257
#[ cfg_attr( feature = "tracing" , tracing:: instrument( skip_all) ) ]
265
- fn try_fulfilling_reservation ( & self , block_number : u64 ) -> Option < BlockT > {
258
+ fn try_fulfilling_reservation ( & self , block_number : u64 ) -> Result < Option < BlockT > , InsertError > {
266
259
let reservations = self . reservations . upgradable_read ( ) ;
267
260
268
261
reservations
@@ -321,17 +314,14 @@ impl<BlockT: Block + Clone + From<LocalBlock>> ReservableSparseBlockchainStorage
321
314
} ,
322
315
) ;
323
316
324
- let mut storage = RwLockUpgradableReadGuard :: upgrade ( storage) ;
325
-
326
- // SAFETY: Reservations are guaranteed to not overlap with other reservations or
327
- // blocks, so the generated block must have a unique block
328
- // number and thus hash.
329
- unsafe {
330
- storage
331
- . insert_block_unchecked ( block. into ( ) , reservation. previous_total_difficulty )
317
+ {
318
+ let mut storage = RwLockUpgradableReadGuard :: upgrade ( storage) ;
319
+ Ok ( storage
320
+ . insert_block ( block. into ( ) , reservation. previous_total_difficulty ) ?
321
+ . clone ( ) )
332
322
}
333
- . clone ( )
334
323
} )
324
+ . transpose ( )
335
325
}
336
326
}
337
327
0 commit comments