@@ -2166,12 +2166,8 @@ impl Backend {
2166
2166
}
2167
2167
2168
2168
for number in from..=to {
2169
- let storage = self . blockchain . storage . read ( ) ;
2170
- if let Some ( & block_hash) = storage. hashes . get ( & number)
2171
- && let Some ( block) = storage. blocks . get ( & block_hash) . cloned ( )
2172
- {
2173
- drop ( storage) ;
2174
- all_logs. extend ( self . mined_logs_for_block ( filter. clone ( ) , block, block_hash) ) ;
2169
+ if let Some ( ( block, hash) ) = self . get_block_with_hash ( number) {
2170
+ all_logs. extend ( self . mined_logs_for_block ( filter. clone ( ) , block, hash) ) ;
2175
2171
}
2176
2172
}
2177
2173
@@ -2229,7 +2225,7 @@ impl Backend {
2229
2225
2230
2226
fn mined_block_by_hash ( & self , hash : B256 ) -> Option < AnyRpcBlock > {
2231
2227
let block = self . blockchain . get_block_by_hash ( & hash) ?;
2232
- Some ( self . convert_block ( block) )
2228
+ Some ( self . convert_block_with_hash ( block, Some ( hash ) ) )
2233
2229
}
2234
2230
2235
2231
pub ( crate ) async fn mined_transactions_by_block_number (
@@ -2298,7 +2294,8 @@ impl Backend {
2298
2294
Ok ( None )
2299
2295
}
2300
2296
2301
- pub fn get_block ( & self , id : impl Into < BlockId > ) -> Option < Block > {
2297
+ /// Returns the block and its hash for the given id
2298
+ fn get_block_with_hash ( & self , id : impl Into < BlockId > ) -> Option < ( Block , B256 ) > {
2302
2299
let hash = match id. into ( ) {
2303
2300
BlockId :: Hash ( hash) => hash. block_hash ,
2304
2301
BlockId :: Number ( number) => {
@@ -2326,36 +2323,46 @@ impl Backend {
2326
2323
}
2327
2324
}
2328
2325
} ;
2329
- self . get_block_by_hash ( hash)
2326
+ let block = self . get_block_by_hash ( hash) ?;
2327
+ Some ( ( block, hash) )
2328
+ }
2329
+
2330
+ pub fn get_block ( & self , id : impl Into < BlockId > ) -> Option < Block > {
2331
+ self . get_block_with_hash ( id) . map ( |( block, _) | block)
2330
2332
}
2331
2333
2332
2334
pub fn get_block_by_hash ( & self , hash : B256 ) -> Option < Block > {
2333
2335
self . blockchain . get_block_by_hash ( & hash)
2334
2336
}
2335
2337
2336
2338
pub fn mined_block_by_number ( & self , number : BlockNumber ) -> Option < AnyRpcBlock > {
2337
- let block = self . get_block ( number) ?;
2338
- let mut block = self . convert_block ( block) ;
2339
+ let ( block, hash ) = self . get_block_with_hash ( number) ?;
2340
+ let mut block = self . convert_block_with_hash ( block, Some ( hash ) ) ;
2339
2341
block. transactions . convert_to_hashes ( ) ;
2340
2342
Some ( block)
2341
2343
}
2342
2344
2343
2345
pub fn get_full_block ( & self , id : impl Into < BlockId > ) -> Option < AnyRpcBlock > {
2344
- let block = self . get_block ( id) ?;
2346
+ let ( block, hash ) = self . get_block_with_hash ( id) ?;
2345
2347
let transactions = self . mined_transactions_in_block ( & block) ?;
2346
- let mut block = self . convert_block ( block) ;
2348
+ let mut block = self . convert_block_with_hash ( block, Some ( hash ) ) ;
2347
2349
block. inner . transactions = BlockTransactions :: Full ( transactions) ;
2348
-
2349
2350
Some ( block)
2350
2351
}
2351
2352
2352
2353
/// Takes a block as it's stored internally and returns the eth api conform block format.
2353
2354
pub fn convert_block ( & self , block : Block ) -> AnyRpcBlock {
2355
+ self . convert_block_with_hash ( block, None )
2356
+ }
2357
+
2358
+ /// Takes a block as it's stored internally and returns the eth api conform block format.
2359
+ /// If `known_hash` is provided, it will be used instead of computing `hash_slow()`.
2360
+ pub fn convert_block_with_hash ( & self , block : Block , known_hash : Option < B256 > ) -> AnyRpcBlock {
2354
2361
let size = U256 :: from ( alloy_rlp:: encode ( & block) . len ( ) as u32 ) ;
2355
2362
2356
2363
let Block { header, transactions, .. } = block;
2357
2364
2358
- let hash = header. hash_slow ( ) ;
2365
+ let hash = known_hash . unwrap_or_else ( || header. hash_slow ( ) ) ;
2359
2366
let Header { number, withdrawals_root, .. } = header;
2360
2367
2361
2368
let block = AlloyBlock {
0 commit comments