Skip to content

Commit

Permalink
chore: use block_ref on CanonicalInMemoryState (#11467)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshieDo authored Oct 3, 2024
1 parent d72e438 commit b4d5ade
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
42 changes: 24 additions & 18 deletions crates/chain-state/src/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl CanonicalInMemoryState {
let in_memory_state = InMemoryState::new(blocks, numbers, pending);
let header = in_memory_state
.head_state()
.map_or_else(SealedHeader::default, |state| state.block().block().header.clone());
.map_or_else(SealedHeader::default, |state| state.block_ref().block().header.clone());
let chain_info_tracker = ChainInfoTracker::new(header, finalized);
let (canon_state_notification_sender, _) =
broadcast::channel(CANON_STATE_NOTIFICATION_CHANNEL_SIZE);
Expand Down Expand Up @@ -219,7 +219,7 @@ impl CanonicalInMemoryState {

/// Returns the header corresponding to the given hash.
pub fn header_by_hash(&self, hash: B256) -> Option<SealedHeader> {
self.state_by_hash(hash).map(|block| block.block().block.header.clone())
self.state_by_hash(hash).map(|block| block.block_ref().block.header.clone())
}

/// Clears all entries in the in memory state.
Expand Down Expand Up @@ -323,7 +323,7 @@ impl CanonicalInMemoryState {
// height)
let mut old_blocks = blocks
.drain()
.filter(|(_, b)| b.block().block().number > persisted_height)
.filter(|(_, b)| b.block_ref().block().number > persisted_height)
.map(|(_, b)| b.block.clone())
.collect::<Vec<_>>();

Expand All @@ -345,7 +345,7 @@ impl CanonicalInMemoryState {
// also shift the pending state if it exists
self.inner.in_memory_state.pending.send_modify(|p| {
if let Some(p) = p.as_mut() {
p.parent = blocks.get(&p.block().block.parent_hash).cloned();
p.parent = blocks.get(&p.block_ref().block.parent_hash).cloned();
}
});
}
Expand Down Expand Up @@ -452,7 +452,7 @@ impl CanonicalInMemoryState {

/// Returns the `SealedHeader` corresponding to the pending state.
pub fn pending_sealed_header(&self) -> Option<SealedHeader> {
self.pending_state().map(|h| h.block().block().header.clone())
self.pending_state().map(|h| h.block_ref().block().header.clone())
}

/// Returns the `Header` corresponding to the pending state.
Expand All @@ -462,20 +462,20 @@ impl CanonicalInMemoryState {

/// Returns the `SealedBlock` corresponding to the pending state.
pub fn pending_block(&self) -> Option<SealedBlock> {
self.pending_state().map(|block_state| block_state.block().block().clone())
self.pending_state().map(|block_state| block_state.block_ref().block().clone())
}

/// Returns the `SealedBlockWithSenders` corresponding to the pending state.
pub fn pending_block_with_senders(&self) -> Option<SealedBlockWithSenders> {
self.pending_state()
.and_then(|block_state| block_state.block().block().clone().seal_with_senders())
.and_then(|block_state| block_state.block_ref().block().clone().seal_with_senders())
}

/// Returns a tuple with the `SealedBlock` corresponding to the pending
/// state and a vector of its `Receipt`s.
pub fn pending_block_and_receipts(&self) -> Option<(SealedBlock, Vec<Receipt>)> {
self.pending_state().map(|block_state| {
(block_state.block().block().clone(), block_state.executed_block_receipts())
(block_state.block_ref().block().clone(), block_state.executed_block_receipts())
})
}

Expand Down Expand Up @@ -543,7 +543,7 @@ impl CanonicalInMemoryState {
pub fn transaction_by_hash(&self, hash: TxHash) -> Option<TransactionSigned> {
for block_state in self.canonical_chain() {
if let Some(tx) =
block_state.block().block().body.transactions().find(|tx| tx.hash() == hash)
block_state.block_ref().block().body.transactions().find(|tx| tx.hash() == hash)
{
return Some(tx.clone())
}
Expand All @@ -559,7 +559,7 @@ impl CanonicalInMemoryState {
) -> Option<(TransactionSigned, TransactionMeta)> {
for block_state in self.canonical_chain() {
if let Some((index, tx)) = block_state
.block()
.block_ref()
.block()
.body
.transactions()
Expand All @@ -570,10 +570,10 @@ impl CanonicalInMemoryState {
tx_hash,
index: index as u64,
block_hash: block_state.hash(),
block_number: block_state.block().block.number,
base_fee: block_state.block().block().header.base_fee_per_gas,
timestamp: block_state.block().block.timestamp,
excess_blob_gas: block_state.block().block.excess_blob_gas,
block_number: block_state.block_ref().block.number,
base_fee: block_state.block_ref().block.header.base_fee_per_gas,
timestamp: block_state.block_ref().block.timestamp,
excess_blob_gas: block_state.block_ref().block.excess_blob_gas,
};
return Some((tx.clone(), meta))
}
Expand Down Expand Up @@ -1156,13 +1156,19 @@ mod tests {
let block2 = test_block_builder.get_executed_block_with_number(0, B256::random());
let chain = NewCanonicalChain::Commit { new: vec![block1.clone()] };
state.update_chain(chain);
assert_eq!(state.head_state().unwrap().block().block().hash(), block1.block().hash());
assert_eq!(state.state_by_number(0).unwrap().block().block().hash(), block1.block().hash());
assert_eq!(state.head_state().unwrap().block_ref().block().hash(), block1.block().hash());
assert_eq!(
state.state_by_number(0).unwrap().block_ref().block().hash(),
block1.block().hash()
);

let chain = NewCanonicalChain::Reorg { new: vec![block2.clone()], old: vec![block1] };
state.update_chain(chain);
assert_eq!(state.head_state().unwrap().block().block().hash(), block2.block().hash());
assert_eq!(state.state_by_number(0).unwrap().block().block().hash(), block2.block().hash());
assert_eq!(state.head_state().unwrap().block_ref().block().hash(), block2.block().hash());
assert_eq!(
state.state_by_number(0).unwrap().block_ref().block().hash(),
block2.block().hash()
);

assert_eq!(state.inner.in_memory_state.block_count(), 1);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/storage/provider/src/providers/blockchain_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ impl<N: ProviderNodeTypes> TransactionsProvider for BlockchainProvider2<N> {
|provider| provider.transaction_by_id_no_hash(id),
|tx_index, _, block_state| {
Ok(block_state
.block()
.block_ref()
.block()
.body
.transactions
Expand Down Expand Up @@ -1427,7 +1427,7 @@ impl<N: ProviderNodeTypes> ChangeSetReader for BlockchainProvider2<N> {
) -> ProviderResult<Vec<AccountBeforeTx>> {
if let Some(state) = self.canonical_in_memory_state.state_by_number(block_number) {
let changesets = state
.block()
.block_ref()
.execution_output
.bundle
.reverts
Expand Down

0 comments on commit b4d5ade

Please sign in to comment.