From 0addbf72fb67ea78baf723e1a71e64f79c65f0ea Mon Sep 17 00:00:00 2001 From: jackzhhuang Date: Wed, 9 Oct 2024 17:11:38 +0800 Subject: [PATCH] remove the used codes --- flexidag/src/blockdag.rs | 4 ++ flexidag/src/ghostdag/protocol.rs | 2 +- sync/src/block_connector/write_block_chain.rs | 38 ++++++++++++++----- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/flexidag/src/blockdag.rs b/flexidag/src/blockdag.rs index 0f77d14934..8635fce5d5 100644 --- a/flexidag/src/blockdag.rs +++ b/flexidag/src/blockdag.rs @@ -559,6 +559,10 @@ impl BlockDAG { self.storage.reachability_store.clone() } + pub fn reachability_service(&self) -> MTReachabilityService { + self.pruning_point_manager().reachability_service() + } + pub fn verify_and_ghostdata( &self, blue_blocks: &[BlockHeader], diff --git a/flexidag/src/ghostdag/protocol.rs b/flexidag/src/ghostdag/protocol.rs index 30567d473a..9e3005a1d8 100644 --- a/flexidag/src/ghostdag/protocol.rs +++ b/flexidag/src/ghostdag/protocol.rs @@ -22,7 +22,7 @@ pub struct GhostdagManager< pub(super) ghostdag_store: T, pub(super) relations_store: Arc>, pub(super) headers_store: V, - pub(super) reachability_service: U, + pub reachability_service: U, } impl< diff --git a/sync/src/block_connector/write_block_chain.rs b/sync/src/block_connector/write_block_chain.rs index 1802b713de..c8035fb6ab 100644 --- a/sync/src/block_connector/write_block_chain.rs +++ b/sync/src/block_connector/write_block_chain.rs @@ -377,18 +377,36 @@ where self.dag.clone(), )?; - // delete block since from block.number + 1 to latest. - let start = new_head_block.header().number().saturating_add(1); - let latest = self.main.status().head.number(); - for block_number in start..latest { - if let Some(block) = self.main.get_block_by_number(block_number)? { - info!("Delete block({:?})", block.header); - self.storage.delete_block(block.id())?; - self.storage.delete_block_info(block.id())?; - } else { - warn!("Can not find block by number:{}", block_number); + let start = new_head_block.header().id(); + let lastest = self.main.status().head.id(); + let reachability_service = self.main.dag().reachability_service(); + for child in reachability_service.forward_chain_iterator(start, lastest, true) { + if child == start { + continue; + } + + let child_block = self + .main + .get_storage() + .get_block_header_by_hash(child)? + .ok_or_else(|| format_err!("Cannot find block header by hash: {:?}", child))?; + + self.storage.delete_block(child)?; + self.storage.delete_block_info(child)?; + + for parent in child_block.parents_hash().into_iter() { + if parent == start { + continue; + } + if self.main.dag().check_ancestor_of(parent, vec![start])? { + continue; + } + + self.storage.delete_block(parent)?; + self.storage.delete_block_info(parent)?; } } + let executed_block = new_branch.head_block(); self.main = new_branch;