Skip to content

Commit

Permalink
fix: optimize verify chain fetches
Browse files Browse the repository at this point in the history
  • Loading branch information
stringhandler committed Dec 19, 2024
1 parent 2d66140 commit e25b29a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ pub mod grpc;
pub mod http;
pub mod p2p;

pub const PROTOCOL_VERSION: u64 = 27;
pub const PROTOCOL_VERSION: u64 = 26;
20 changes: 13 additions & 7 deletions src/sharechain/p2chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ impl<T: BlockCache> P2Chain<T> {
level.get(hash)
}

pub fn block_exists(&self, height: u64, hash: &FixedHash) -> bool {
self.level_at_height(height).map_or(false, |level| level.contains(hash))
}

#[cfg(test)]
fn get_chain_block_at_height(&self, height: u64) -> Option<Arc<P2Block>> {
let level = self.level_at_height(height)?;
Expand Down Expand Up @@ -263,22 +267,19 @@ impl<T: BlockCache> P2Chain<T> {
// do we know of the parent
// we should not check the chain start for parents
if block.height != 0 {
if self
.get_block_at_height(new_block_height.saturating_sub(1), &block.prev_hash)
.is_none()
{
if !self.block_exists(new_block_height.saturating_sub(1), &block.prev_hash) {
// we dont know the parent
new_tip
.missing_blocks
.insert(block.prev_hash, new_block_height.saturating_sub(1));
}
// now lets check the uncles
for uncle in &block.uncles {
if let Some(uncle_block) = self.get_block_at_height(uncle.0, &uncle.1) {
if self.get_parent_block(&uncle_block).is_none() {
if let Some(uncle_parent_hash) = self.get_parent_of(uncle.0, &uncle.1) {
if !self.block_exists(uncle.0.saturating_sub(1), &uncle_parent_hash) {
new_tip
.missing_blocks
.insert(uncle_block.prev_hash, uncle_block.height.saturating_sub(1));
.insert(uncle_parent_hash, uncle.0.saturating_sub(1));
}
} else {
new_tip.missing_blocks.insert(uncle.1, uncle.0);
Expand Down Expand Up @@ -586,6 +587,11 @@ impl<T: BlockCache> P2Chain<T> {
self.add_block_inner(block)
}

pub fn get_parent_of(&self, height: u64, hash: &FixedHash) -> Option<FixedHash> {
let level = self.level_at_height(height)?;
level.get_prev_hash(hash)
}

pub fn get_parent_block(&self, block: &P2Block) -> Option<Arc<P2Block>> {
let parent_height = match block.height.checked_sub(1) {
Some(height) => height,
Expand Down
16 changes: 15 additions & 1 deletion src/sharechain/p2chain_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ impl<T: BlockCache> P2ChainLevel<T> {
res
}

pub fn get_prev_hash(&self, hash: &FixedHash) -> Option<FixedHash> {
let lock = self.block_headers.read().expect("could not lock");
for block in lock.iter() {
if &block.hash == hash {
return Some(block.prev_hash);
}
}
None
}

pub fn height(&self) -> u64 {
self.height
}
Expand Down Expand Up @@ -121,7 +131,11 @@ impl<T: BlockCache> P2ChainLevel<T> {
}

pub fn contains(&self, hash: &BlockHash) -> bool {
self.block_cache.contains(hash)
self.block_headers
.read()
.expect("could not lock")
.iter()
.any(|b| b.hash == *hash)
}

pub fn all_blocks(&self) -> Vec<Arc<P2Block>> {
Expand Down

0 comments on commit e25b29a

Please sign in to comment.