@@ -2229,7 +2229,7 @@ impl Backend {
2229
2229
2230
2230
fn mined_block_by_hash ( & self , hash : B256 ) -> Option < AnyRpcBlock > {
2231
2231
let block = self . blockchain . get_block_by_hash ( & hash) ?;
2232
- Some ( self . convert_block ( block) )
2232
+ Some ( self . convert_block_with_hash ( block, Some ( hash ) ) )
2233
2233
}
2234
2234
2235
2235
pub ( crate ) async fn mined_transactions_by_block_number (
@@ -2298,7 +2298,8 @@ impl Backend {
2298
2298
Ok ( None )
2299
2299
}
2300
2300
2301
- pub fn get_block ( & self , id : impl Into < BlockId > ) -> Option < Block > {
2301
+ /// Returns the block and its hash for the given id
2302
+ fn get_block_with_hash ( & self , id : impl Into < BlockId > ) -> Option < ( Block , B256 ) > {
2302
2303
let hash = match id. into ( ) {
2303
2304
BlockId :: Hash ( hash) => hash. block_hash ,
2304
2305
BlockId :: Number ( number) => {
@@ -2326,36 +2327,46 @@ impl Backend {
2326
2327
}
2327
2328
}
2328
2329
} ;
2329
- self . get_block_by_hash ( hash)
2330
+ let block = self . get_block_by_hash ( hash) ?;
2331
+ Some ( ( block, hash) )
2332
+ }
2333
+
2334
+ pub fn get_block ( & self , id : impl Into < BlockId > ) -> Option < Block > {
2335
+ self . get_block_with_hash ( id) . map ( |( block, _) | block)
2330
2336
}
2331
2337
2332
2338
pub fn get_block_by_hash ( & self , hash : B256 ) -> Option < Block > {
2333
2339
self . blockchain . get_block_by_hash ( & hash)
2334
2340
}
2335
2341
2336
2342
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) ;
2343
+ let ( block, hash ) = self . get_block_with_hash ( number) ?;
2344
+ let mut block = self . convert_block_with_hash ( block, Some ( hash ) ) ;
2339
2345
block. transactions . convert_to_hashes ( ) ;
2340
2346
Some ( block)
2341
2347
}
2342
2348
2343
2349
pub fn get_full_block ( & self , id : impl Into < BlockId > ) -> Option < AnyRpcBlock > {
2344
- let block = self . get_block ( id) ?;
2350
+ let ( block, hash ) = self . get_block_with_hash ( id) ?;
2345
2351
let transactions = self . mined_transactions_in_block ( & block) ?;
2346
- let mut block = self . convert_block ( block) ;
2352
+ let mut block = self . convert_block_with_hash ( block, Some ( hash ) ) ;
2347
2353
block. inner . transactions = BlockTransactions :: Full ( transactions) ;
2348
-
2349
2354
Some ( block)
2350
2355
}
2351
2356
2352
2357
/// Takes a block as it's stored internally and returns the eth api conform block format.
2353
2358
pub fn convert_block ( & self , block : Block ) -> AnyRpcBlock {
2359
+ self . convert_block_with_hash ( block, None )
2360
+ }
2361
+
2362
+ /// Takes a block as it's stored internally and returns the eth api conform block format.
2363
+ /// If `known_hash` is provided, it will be used instead of computing `hash_slow()`.
2364
+ pub fn convert_block_with_hash ( & self , block : Block , known_hash : Option < B256 > ) -> AnyRpcBlock {
2354
2365
let size = U256 :: from ( alloy_rlp:: encode ( & block) . len ( ) as u32 ) ;
2355
2366
2356
2367
let Block { header, transactions, .. } = block;
2357
2368
2358
- let hash = header. hash_slow ( ) ;
2369
+ let hash = known_hash . unwrap_or_else ( || header. hash_slow ( ) ) ;
2359
2370
let Header { number, withdrawals_root, .. } = header;
2360
2371
2361
2372
let block = AlloyBlock {
0 commit comments