diff --git a/chain/service/src/chain_service.rs b/chain/service/src/chain_service.rs index bb16d37ce0..422a70130e 100644 --- a/chain/service/src/chain_service.rs +++ b/chain/service/src/chain_service.rs @@ -1,7 +1,7 @@ // Copyright (c) The Starcoin Core Contributors // SPDX-License-Identifier: Apache-2.0 -use anyhow::{bail, format_err, Error, Result}; +use anyhow::{format_err, Error, Result}; use starcoin_chain::BlockChain; use starcoin_chain_api::message::{ChainRequest, ChainResponse}; use starcoin_chain_api::{ @@ -455,13 +455,11 @@ impl ReadableChainService for ChainReaderServiceInner { } fn get_dag_state(&self) -> Result { - if self.main.check_chain_type()? != ChainType::Dag { - bail!("The dag block is not built yet."); - } let state = self.main.get_dag_state()?; + let pruning_point = self.main.status().head().pruning_point(); Ok(DagStateView { tips: state.tips, - pruning_point: state.pruning_point, + pruning_point, }) } diff --git a/chain/src/chain.rs b/chain/src/chain.rs index 8e133e0c03..d326000b6d 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -2052,10 +2052,7 @@ impl BlockChain { if self.epoch.end_block_number() == block.header().number() { self.epoch = get_epoch_from_statedb(&self.statedb)?; } - self.dag.save_dag_state(DagState { - tips, - pruning_point: block.header().pruning_point(), - })?; + self.dag.save_dag_state(DagState { tips })?; Ok(executed_block) } } diff --git a/flexidag/src/blockdag.rs b/flexidag/src/blockdag.rs index bb8a02d49c..730c38e3c9 100644 --- a/flexidag/src/blockdag.rs +++ b/flexidag/src/blockdag.rs @@ -115,7 +115,6 @@ impl BlockDAG { self.commit(genesis, origin)?; self.save_dag_state(DagState { tips: vec![genesis_id], - pruning_point: genesis_id, })?; Ok(origin) } @@ -337,13 +336,9 @@ impl BlockDAG { block_header: &BlockHeader, genesis_id: HashValue, ) -> anyhow::Result<()> { - let dag_state = DagState { - tips: block_header.parents(), - pruning_point: block_header.pruning_point(), - }; let ghostdata = self.ghost_dag_manager().ghostdag(&block_header.parents())?; let next_pruning_point = self.pruning_point_manager().next_pruning_point( - &dag_state, + block_header.pruning_point(), &ghostdata, pruning_depth, pruning_finality, @@ -388,24 +383,36 @@ impl BlockDAG { info, storage.get_accumulator_store(AccumulatorStoreType::Block), ); - match self.storage.state_store.read().get_state_by_hash( + + let read_guard = self.storage.state_store.read(); + + let update_dag_state = match read_guard.get_state_by_hash( accumulator .get_leaf(0)? .ok_or_else(|| format_err!("no leaf when upgrading dag db"))?, ) { - anyhow::Result::Ok(dag_state) => match self.storage.state_store.read().get_state() { + anyhow::Result::Ok(dag_state) => match read_guard.get_state() { anyhow::Result::Ok(saved_dag_state) => { info!("The dag state is {:?}", saved_dag_state); + None } - Err(_) => { - info!("The dag state will be saved as {:?}", dag_state); - self.storage.state_store.write().insert(dag_state)?; - } + Err(_) => Some(dag_state), }, Err(_) => { - warn!("Cannot get the dag state by genesis id. Might be it is a new node. The dag state will be: {:?}", self.storage.state_store.read().get_state()?); + warn!("Cannot get the dag state by genesis id. Might be it is a new node. The dag state will be: {:?}", read_guard.get_state()?); + None } + }; + + drop(read_guard); + + if let Some(dag_state) = update_dag_state { + let write_guard = self.storage.state_store.write(); + info!("The dag state will be saved as {:?}", dag_state); + write_guard.insert(dag_state)?; + drop(write_guard); } + anyhow::Ok(()) } } diff --git a/flexidag/src/consensusdb/consenses_state.rs b/flexidag/src/consensusdb/consenses_state.rs index a6b0f3cf09..481c415fdb 100644 --- a/flexidag/src/consensusdb/consenses_state.rs +++ b/flexidag/src/consensusdb/consenses_state.rs @@ -9,7 +9,6 @@ use std::sync::Arc; #[derive(Eq, PartialEq, Hash, Deserialize, Serialize, Clone, Debug, Default)] pub struct DagState { pub tips: Vec, - pub pruning_point: Hash, } pub(crate) const DAG_STATE_STORE_CF: &str = "dag-state-store"; @@ -88,9 +87,6 @@ pub struct DagStateView { impl DagStateView { pub fn into_state(self) -> DagState { - DagState { - tips: self.tips, - pruning_point: self.pruning_point, - } + DagState { tips: self.tips } } } diff --git a/flexidag/src/prune/pruning_point_manager.rs b/flexidag/src/prune/pruning_point_manager.rs index 4af1a2b404..b7496e456a 100644 --- a/flexidag/src/prune/pruning_point_manager.rs +++ b/flexidag/src/prune/pruning_point_manager.rs @@ -40,9 +40,10 @@ impl PruningPointManagerT { pub fn prune( &self, dag_state: &DagState, + current_pruning_point: HashValue, next_pruning_point: HashValue, ) -> anyhow::Result> { - if dag_state.pruning_point == HashValue::zero() { + if current_pruning_point == HashValue::zero() { return Ok(dag_state.tips.clone()); } anyhow::Ok( @@ -60,12 +61,12 @@ impl PruningPointManagerT { pub(crate) fn next_pruning_point( &self, - dag_state: &DagState, + pruning_point: HashValue, ghostdata: &GhostdagData, pruning_depth: u64, pruning_finality: u64, ) -> anyhow::Result { - let pruning_ghostdata = self.ghost_dag_store.get_data(dag_state.pruning_point)?; + let pruning_ghostdata = self.ghost_dag_store.get_data(pruning_point)?; let min_required_blue_score_for_next_pruning_point = (self.finality_score(pruning_ghostdata.blue_score, pruning_finality) + 1) * pruning_finality; @@ -74,12 +75,10 @@ impl PruningPointManagerT { "min_required_blue_score_for_next_pruning_point: {:?}", min_required_blue_score_for_next_pruning_point ); - let mut latest_pruning_ghost_data = self - .ghost_dag_store - .get_compact_data(dag_state.pruning_point)?; + let mut latest_pruning_ghost_data = self.ghost_dag_store.get_compact_data(pruning_point)?; if min_required_blue_score_for_next_pruning_point + pruning_depth <= ghostdata.blue_score { for child in self.reachability_service().forward_chain_iterator( - dag_state.pruning_point, + pruning_point, ghostdata.selected_parent, true, ) { @@ -106,7 +105,7 @@ impl PruningPointManagerT { } if latest_pruning_ghost_data.selected_parent == HashValue::new(ORIGIN) { - anyhow::Ok(dag_state.pruning_point) // still genesis + anyhow::Ok(pruning_point) // still genesis } else { anyhow::Ok(latest_pruning_ghost_data.selected_parent) } diff --git a/flexidag/tests/tests.rs b/flexidag/tests/tests.rs index df89f47fa5..6483bfe933 100644 --- a/flexidag/tests/tests.rs +++ b/flexidag/tests/tests.rs @@ -316,7 +316,6 @@ fn test_dag_tips_store() { let state = DagState { tips: vec![Hash::random()], - pruning_point: Hash::random(), }; dag.storage .state_store @@ -936,7 +935,6 @@ fn test_prune() -> anyhow::Result<()> { // prunning process begins dag.save_dag_state(DagState { tips: vec![block_red_3.id(), block_main_5.id()], - pruning_point: genesis.id(), })?; let MineNewDagBlockInfo {