Skip to content

Commit 52fc9d9

Browse files
elijahhamptonarya2
authored andcommitted
Maintains invalidate_blocks cleanup in reconsider_block and finalize(). Removes unused ReconsiderError variant. Opts to refuse block consideration if parent_chain does not exist. Adds db handle to reconsider_block function. Edits max blocks constant documentation
1 parent 0253460 commit 52fc9d9

File tree

4 files changed

+24
-44
lines changed

4 files changed

+24
-44
lines changed

zebra-state/src/constants.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ pub const MAX_FIND_BLOCK_HEADERS_RESULTS: u32 = 160;
117117
/// These database versions can be recreated from their directly preceding versions.
118118
pub const RESTORABLE_DB_VERSIONS: [u64; 1] = [26];
119119

120-
/// The maximum number of invalidated blocks per entry.
120+
/// The maximum number of invalidated block records.
121121
///
122-
/// This limits the memory for each entry to around:
123-
/// `256 blocks * 2 MB per block = 0.5 GB`
124-
pub const MAX_INVALIDATED_BLOCKS: usize = 256;
122+
/// This limits the memory use to around:
123+
/// `100 entries * up to 99 blocks * 2 MB per block = 20 GB`
124+
pub const MAX_INVALIDATED_BLOCKS: usize = 100;
125125

126126
lazy_static! {
127127
/// Regex that matches the RocksDB error when its lock file is already open.

zebra-state/src/error.rs

-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ pub enum ReconsiderError {
5959
#[error("Invalidated blocks list is empty when it should contain at least one block")]
6060
InvalidatedBlocksEmpty,
6161

62-
#[error("Invalid height {0:?}: parent height would be negative")]
63-
InvalidHeight(block::Height),
64-
6562
#[error("{0}")]
6663
ValidationError(#[from] ValidateContextError),
6764
}

zebra-state/src/service/non_finalized_state.rs

+19-36
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use zebra_chain::{
1414
parameters::Network,
1515
sprout::{self},
1616
transparent,
17-
value_balance::ValueBalance,
1817
};
1918

2019
use crate::{
@@ -239,7 +238,7 @@ impl NonFinalizedState {
239238

240239
// Remove all invalidated_blocks at or below the finalized height
241240
self.invalidated_blocks
242-
.retain(|height, _blocks| *height > best_chain_root.height);
241+
.retain(|height, _blocks| *height >= best_chain_root.height);
243242

244243
self.update_metrics_for_chains();
245244

@@ -318,7 +317,11 @@ impl NonFinalizedState {
318317
/// Reconsiders a previously invalidated block and its descendants into the non-finalized state
319318
/// based on a block_hash. Reconsidered blocks are inserted into the previous chain and re-inserted
320319
/// into the chain_set.
321-
pub fn reconsider_block(&mut self, block_hash: block::Hash) -> Result<(), ReconsiderError> {
320+
pub fn reconsider_block(
321+
&mut self,
322+
block_hash: block::Hash,
323+
finalized_state: &ZebraDb,
324+
) -> Result<(), ReconsiderError> {
322325
// Get the invalidated blocks that were invalidated by the given block_hash
323326
let height = self
324327
.invalidated_blocks
@@ -348,44 +351,24 @@ impl NonFinalizedState {
348351
let root_parent_hash = invalidated_root.block.header.previous_block_hash;
349352
let parent_chain = self
350353
.parent_chain(root_parent_hash)
351-
.map_err(|_| ReconsiderError::ParentChainNotFound(block_hash));
354+
.map_err(|_| ReconsiderError::ParentChainNotFound(block_hash))?;
352355

353-
let modified_chain = match parent_chain {
354-
// 1. If a parent chain exist use the parent chain
355-
Ok(parent_chain) => {
356-
let mut chain = Arc::unwrap_or_clone(parent_chain);
357-
358-
for block in Arc::unwrap_or_clone(invalidated_blocks) {
359-
chain = chain.push(block)?;
360-
}
361-
362-
Arc::new(chain)
363-
}
364-
// 2. If a parent chain does not exist create a new chain from the non finalized state tip
365-
Err(_) => {
366-
let mut chain = Chain::new(
367-
&self.network,
368-
invalidated_root.height.previous().map_err(|_| {
369-
ReconsiderError::InvalidHeight(Height(invalidated_root.height.0 - 1))
370-
})?,
371-
Default::default(),
372-
Default::default(),
373-
Default::default(),
374-
Default::default(),
375-
ValueBalance::zero(),
376-
);
377-
378-
for block in Arc::unwrap_or_clone(invalidated_blocks) {
379-
chain = chain.push(block)?;
380-
}
356+
let mut modified_chain = Arc::unwrap_or_clone(parent_chain);
381357

382-
Arc::new(chain)
383-
}
384-
};
358+
for block in Arc::unwrap_or_clone(invalidated_blocks) {
359+
modified_chain = modified_chain.push(block)?;
360+
}
385361

386362
let (height, hash) = modified_chain.non_finalized_tip();
387363

388-
self.insert_with(modified_chain, |chain_set| {
364+
// Only track invalidated_blocks that are not yet finalized. Once blocks are finalized (below the best_chain_root_height)
365+
// we can discard the block.
366+
if let Some(best_chain_root_height) = finalized_state.finalized_tip_height() {
367+
self.invalidated_blocks
368+
.retain(|height, _blocks| *height >= best_chain_root_height);
369+
}
370+
371+
self.insert_with(Arc::new(modified_chain), |chain_set| {
389372
chain_set.retain(|chain| chain.non_finalized_tip_hash() != root_parent_hash)
390373
});
391374

zebra-state/src/service/non_finalized_state/tests/vectors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ fn reconsider_block_inserts_block_and_descendants_into_chain_for_network(
381381

382382
// Reconsider block2 and check that both block2 and block3 were `reconsidered` into the
383383
// best chain
384-
state.reconsider_block(block2.hash())?;
384+
state.reconsider_block(block2.hash(), &finalized_state.db)?;
385385

386386
let best_chain = state.best_chain().unwrap();
387387

0 commit comments

Comments
 (0)