From e86b1ae1009a19c6e2f649a055a80735a344c8a0 Mon Sep 17 00:00:00 2001 From: joshieDo <93316087+joshieDo@users.noreply.github.com> Date: Thu, 3 Oct 2024 14:28:49 +0200 Subject: [PATCH 1/4] get_in_memory_or_storage_by_block on block_body_indices --- .../src/providers/blockchain_provider.rs | 53 ++++++++++--------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/crates/storage/provider/src/providers/blockchain_provider.rs b/crates/storage/provider/src/providers/blockchain_provider.rs index f5f838293eb4..534345655b8a 100644 --- a/crates/storage/provider/src/providers/blockchain_provider.rs +++ b/crates/storage/provider/src/providers/blockchain_provider.rs @@ -696,34 +696,35 @@ impl BlockReader for BlockchainProvider2 { &self, number: BlockNumber, ) -> ProviderResult> { - 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| { + // 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 = block_state.parent_state_chain(); + parent_chain.reverse(); + let anchor_num = block_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; + } } - } - Ok(Some(stored_indices)) - } else { - Ok(None) - } + Ok(Some(stored_indices)) + }, + ) } /// Returns the block with senders with matching number or hash from database. From 96e627beab57934af40ca575078b6bee3c0205f5 Mon Sep 17 00:00:00 2001 From: joshieDo <93316087+joshieDo@users.noreply.github.com> Date: Thu, 3 Oct 2024 14:53:04 +0200 Subject: [PATCH 2/4] fix block_body_indices --- .../src/providers/blockchain_provider.rs | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/crates/storage/provider/src/providers/blockchain_provider.rs b/crates/storage/provider/src/providers/blockchain_provider.rs index 534345655b8a..5ad5088cc0e1 100644 --- a/crates/storage/provider/src/providers/blockchain_provider.rs +++ b/crates/storage/provider/src/providers/blockchain_provider.rs @@ -700,25 +700,21 @@ impl BlockReader for BlockchainProvider2 { number.into(), |db_provider| db_provider.block_body_indices(number), |block_state| { - // 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 = block_state.parent_state_chain(); - parent_chain.reverse(); - let anchor_num = block_state.anchor().number; + // 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(anchor_num)? - .ok_or(ProviderError::BlockBodyIndicesNotFound(anchor_num))?; - stored_indices.first_tx_num = stored_indices.next_tx_num(); + .block_body_indices(last_storage_block_number)? + .ok_or(ProviderError::BlockBodyIndicesNotFound(last_storage_block_number))?; - 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; + // Iterate from the lowest block in memory to find the stored indices of the requested block + stored_indices.first_tx_num = stored_indices.next_tx_num(); + 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 += txs; + stored_indices.first_tx_num += block_tx_count; } } From ee862339af7d303e77ebfa380add415caf915887 Mon Sep 17 00:00:00 2001 From: joshieDo <93316087+joshieDo@users.noreply.github.com> Date: Thu, 3 Oct 2024 14:54:56 +0200 Subject: [PATCH 3/4] init tx_count as 0 --- crates/storage/provider/src/providers/blockchain_provider.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/storage/provider/src/providers/blockchain_provider.rs b/crates/storage/provider/src/providers/blockchain_provider.rs index 5ad5088cc0e1..8c4fa3276bb3 100644 --- a/crates/storage/provider/src/providers/blockchain_provider.rs +++ b/crates/storage/provider/src/providers/blockchain_provider.rs @@ -707,8 +707,11 @@ impl BlockReader for BlockchainProvider2 { .block_body_indices(last_storage_block_number)? .ok_or(ProviderError::BlockBodyIndicesNotFound(last_storage_block_number))?; - // Iterate from the lowest block in memory to find the stored indices of the requested block + // Prepare our block indices + stored_indices.tx_count = 0; stored_indices.first_tx_num = stored_indices.next_tx_num(); + + // 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 { From 5d50fbc5ff56e9f46f06dec450673a2605bd9422 Mon Sep 17 00:00:00 2001 From: joshieDo <93316087+joshieDo@users.noreply.github.com> Date: Thu, 3 Oct 2024 15:04:48 +0200 Subject: [PATCH 4/4] swap order --- crates/storage/provider/src/providers/blockchain_provider.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/storage/provider/src/providers/blockchain_provider.rs b/crates/storage/provider/src/providers/blockchain_provider.rs index 8c4fa3276bb3..542a4f6077a6 100644 --- a/crates/storage/provider/src/providers/blockchain_provider.rs +++ b/crates/storage/provider/src/providers/blockchain_provider.rs @@ -708,8 +708,8 @@ impl BlockReader for BlockchainProvider2 { .ok_or(ProviderError::BlockBodyIndicesNotFound(last_storage_block_number))?; // Prepare our block indices - stored_indices.tx_count = 0; 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() {