Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(provider): use get_in_memory_or_storage_by_block on fn block_body_indices #11452

Merged
merged 4 commits into from
Oct 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions crates/storage/provider/src/providers/blockchain_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,34 +696,34 @@ impl<N: ProviderNodeTypes> BlockReader for BlockchainProvider2<N> {
&self,
number: BlockNumber,
) -> ProviderResult<Option<StoredBlockBodyIndices>> {
if let Some(indices) = self.database.block_body_indices(number)? {
Ok(Some(indices))
} else if let Some(state) = self.canonical_in_memory_state.state_by_number(number) {
// we have to construct the stored indices for the in memory blocks
//
// To calculate this we will fetch the anchor block and walk forward from all parents
let mut parent_chain = state.parent_state_chain();
parent_chain.reverse();
let anchor_num = state.anchor().number;
let mut stored_indices = self
.database
.block_body_indices(anchor_num)?
.ok_or(ProviderError::BlockBodyIndicesNotFound(anchor_num))?;
stored_indices.first_tx_num = stored_indices.next_tx_num();

for state in parent_chain {
let txs = state.block().block.body.transactions.len() as u64;
if state.block().block().number == number {
stored_indices.tx_count = txs;
} else {
stored_indices.first_tx_num += txs;
self.get_in_memory_or_storage_by_block(
number.into(),
|db_provider| db_provider.block_body_indices(number),
|block_state| {
// Find the last block indices on database
let last_storage_block_number = block_state.anchor().number;
let mut stored_indices = self
.database
.block_body_indices(last_storage_block_number)?
.ok_or(ProviderError::BlockBodyIndicesNotFound(last_storage_block_number))?;

// Prepare our block indices
stored_indices.first_tx_num = stored_indices.next_tx_num();
stored_indices.tx_count = 0;

// Iterate from the lowest block in memory until our target block
for state in block_state.chain().into_iter().rev() {
let block_tx_count = state.block().block.body.transactions.len() as u64;
if state.block_ref().block().number == number {
stored_indices.tx_count = block_tx_count;
} else {
stored_indices.first_tx_num += block_tx_count;
}
}
}

Ok(Some(stored_indices))
} else {
Ok(None)
}
Ok(Some(stored_indices))
},
)
}

/// Returns the block with senders with matching number or hash from database.
Expand Down
Loading