Skip to content

Commit

Permalink
feat: improve handling of max size (#203)
Browse files Browse the repository at this point in the history
Description
---
improve the handling of size, allowing a node to do extra sync to higher
nodes
Cleanup lower not used blocks
  • Loading branch information
SWvheerden authored Dec 5, 2024
1 parent 86f04e1 commit c3d0651
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/sharechain/p2chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,23 @@ impl P2Chain {
}

pub fn is_full(&self) -> bool {
let first_index = self.levels.back().map(|level| level.height).unwrap_or(0);
let current_chain_length = self.current_tip.saturating_sub(first_index);
// let see if we are the limit for the current chain
if current_chain_length >= self.total_size + SAFETY_MARGIN {
return true;
}
// lets check to see if we are over the max sync length
// Ideally this limit should not be reached ever
self.levels.len() as u64 >= self.total_size + SAFETY_MARGIN + MAX_EXTRA_SYNC
}

fn cleanup_chain(&mut self) -> Result<(), ShareChainError>{
let mut first_index = self.levels.back().map(|level| level.height).unwrap_or(0);
let mut current_chain_length = self.current_tip.saturating_sub(first_index);
// let see if we are the limit for the current chain
while current_chain_length > self.total_size + SAFETY_MARGIN{
self.levels.pop_back().ok_or(ShareChainError::BlockLevelNotFound)?;
first_index = self.levels.back().map(|level| level.height).unwrap_or(0);
current_chain_length = self.current_tip.saturating_sub(first_index);
}
Ok(())
}

fn set_new_tip(&mut self, new_height: u64, hash: FixedHash) -> Result<(), ShareChainError> {
let block = self
.get_block_at_height(new_height, &hash)
Expand All @@ -222,7 +228,7 @@ impl P2Chain {
level.chain_block = hash;
self.current_tip = level.height;

Ok(())
self.cleanup_chain()
}

fn verify_chain(&mut self, new_block_height: u64, hash: FixedHash) -> Result<ChainAddResult, ShareChainError> {
Expand Down Expand Up @@ -638,7 +644,6 @@ impl P2Chain {
}
},
}

// so the chain is full, we should not add below the height of the lowest block
Ok(ChainAddResult::default())
}
Expand Down

0 comments on commit c3d0651

Please sign in to comment.