Skip to content

Commit 05307d0

Browse files
authored
perf(chain-state): executed_block_receipts_ref (#20227)
1 parent 245cca7 commit 05307d0

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

crates/chain-state/src/in_memory.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use reth_primitives_traits::{
1818
};
1919
use reth_storage_api::StateProviderBox;
2020
use reth_trie::{updates::TrieUpdatesSorted, HashedPostStateSorted, TrieInputSorted};
21-
use std::{collections::BTreeMap, sync::Arc, time::Instant};
21+
use std::{collections::BTreeMap, ops::Deref, sync::Arc, time::Instant};
2222
use tokio::sync::{broadcast, watch};
2323

2424
/// Size of the broadcast channel used to notify canonical state events.
@@ -634,6 +634,8 @@ impl<N: NodePrimitives> BlockState<N> {
634634
/// We assume that the `Receipts` in the executed block `ExecutionOutcome`
635635
/// has only one element corresponding to the executed block associated to
636636
/// the state.
637+
///
638+
/// This clones the vector of receipts. To avoid it, use [`Self::executed_block_receipts_ref`].
637639
pub fn executed_block_receipts(&self) -> Vec<N::Receipt> {
638640
let receipts = self.receipts();
639641

@@ -646,6 +648,22 @@ impl<N: NodePrimitives> BlockState<N> {
646648
receipts.first().cloned().unwrap_or_default()
647649
}
648650

651+
/// Returns a slice of `Receipt` of executed block that determines the state.
652+
/// We assume that the `Receipts` in the executed block `ExecutionOutcome`
653+
/// has only one element corresponding to the executed block associated to
654+
/// the state.
655+
pub fn executed_block_receipts_ref(&self) -> &[N::Receipt] {
656+
let receipts = self.receipts();
657+
658+
debug_assert!(
659+
receipts.len() <= 1,
660+
"Expected at most one block's worth of receipts, found {}",
661+
receipts.len()
662+
);
663+
664+
receipts.first().map(|receipts| receipts.deref()).unwrap_or_default()
665+
}
666+
649667
/// Returns a vector of __parent__ `BlockStates`.
650668
///
651669
/// The block state order in the output vector is newest to oldest (highest to lowest):

crates/storage/provider/src/providers/consistent.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ impl<N: ProviderNodeTypes> ReceiptProvider for ConsistentProvider<N> {
10461046
id.into(),
10471047
|provider| provider.receipt(id),
10481048
|tx_index, _, block_state| {
1049-
Ok(block_state.executed_block_receipts().get(tx_index).cloned())
1049+
Ok(block_state.executed_block_receipts_ref().get(tx_index).cloned())
10501050
},
10511051
)
10521052
}
@@ -1055,7 +1055,7 @@ impl<N: ProviderNodeTypes> ReceiptProvider for ConsistentProvider<N> {
10551055
for block_state in self.head_block.iter().flat_map(|b| b.chain()) {
10561056
let executed_block = block_state.block_ref();
10571057
let block = executed_block.recovered_block();
1058-
let receipts = block_state.executed_block_receipts();
1058+
let receipts = block_state.executed_block_receipts_ref();
10591059

10601060
// assuming 1:1 correspondence between transactions and receipts
10611061
debug_assert_eq!(

0 commit comments

Comments
 (0)