Skip to content

Commit f5322d9

Browse files
authored
refactor(anvil): introduce get_block_with_hash and convert_block_with_hash (#11939)
* add get_block_with_hash and convert_block_with_hash * refactor: simplify the logic in #11896
1 parent b43f5bb commit f5322d9

File tree

1 file changed

+22
-15
lines changed
  • crates/anvil/src/eth/backend/mem

1 file changed

+22
-15
lines changed

crates/anvil/src/eth/backend/mem/mod.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,12 +2166,8 @@ impl Backend {
21662166
}
21672167

21682168
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));
21752171
}
21762172
}
21772173

@@ -2229,7 +2225,7 @@ impl Backend {
22292225

22302226
fn mined_block_by_hash(&self, hash: B256) -> Option<AnyRpcBlock> {
22312227
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)))
22332229
}
22342230

22352231
pub(crate) async fn mined_transactions_by_block_number(
@@ -2298,7 +2294,8 @@ impl Backend {
22982294
Ok(None)
22992295
}
23002296

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)> {
23022299
let hash = match id.into() {
23032300
BlockId::Hash(hash) => hash.block_hash,
23042301
BlockId::Number(number) => {
@@ -2326,36 +2323,46 @@ impl Backend {
23262323
}
23272324
}
23282325
};
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)
23302332
}
23312333

23322334
pub fn get_block_by_hash(&self, hash: B256) -> Option<Block> {
23332335
self.blockchain.get_block_by_hash(&hash)
23342336
}
23352337

23362338
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));
23392341
block.transactions.convert_to_hashes();
23402342
Some(block)
23412343
}
23422344

23432345
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)?;
23452347
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));
23472349
block.inner.transactions = BlockTransactions::Full(transactions);
2348-
23492350
Some(block)
23502351
}
23512352

23522353
/// Takes a block as it's stored internally and returns the eth api conform block format.
23532354
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 {
23542361
let size = U256::from(alloy_rlp::encode(&block).len() as u32);
23552362

23562363
let Block { header, transactions, .. } = block;
23572364

2358-
let hash = header.hash_slow();
2365+
let hash = known_hash.unwrap_or_else(|| header.hash_slow());
23592366
let Header { number, withdrawals_root, .. } = header;
23602367

23612368
let block = AlloyBlock {

0 commit comments

Comments
 (0)