Skip to content

Commit

Permalink
update dag db
Browse files Browse the repository at this point in the history
  • Loading branch information
jackzhhuang committed Aug 8, 2024
1 parent f6177e1 commit 0666a53
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 37 deletions.
8 changes: 3 additions & 5 deletions chain/service/src/chain_service.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -455,13 +455,11 @@ impl ReadableChainService for ChainReaderServiceInner {
}

fn get_dag_state(&self) -> Result<DagStateView> {
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,
})
}

Expand Down
5 changes: 1 addition & 4 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
33 changes: 20 additions & 13 deletions flexidag/src/blockdag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(())
}
}
6 changes: 1 addition & 5 deletions flexidag/src/consensusdb/consenses_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use std::sync::Arc;
#[derive(Eq, PartialEq, Hash, Deserialize, Serialize, Clone, Debug, Default)]
pub struct DagState {
pub tips: Vec<Hash>,
pub pruning_point: Hash,
}

pub(crate) const DAG_STATE_STORE_CF: &str = "dag-state-store";
Expand Down Expand Up @@ -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 }
}
}
15 changes: 7 additions & 8 deletions flexidag/src/prune/pruning_point_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ impl<T: ReachabilityStoreReader + Clone> PruningPointManagerT<T> {
pub fn prune(
&self,
dag_state: &DagState,
current_pruning_point: HashValue,
next_pruning_point: HashValue,
) -> anyhow::Result<Vec<HashValue>> {
if dag_state.pruning_point == HashValue::zero() {
if current_pruning_point == HashValue::zero() {
return Ok(dag_state.tips.clone());
}
anyhow::Ok(
Expand All @@ -60,12 +61,12 @@ impl<T: ReachabilityStoreReader + Clone> PruningPointManagerT<T> {

pub(crate) fn next_pruning_point(
&self,
dag_state: &DagState,
pruning_point: HashValue,
ghostdata: &GhostdagData,
pruning_depth: u64,
pruning_finality: u64,
) -> anyhow::Result<HashValue> {
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;
Expand All @@ -74,12 +75,10 @@ impl<T: ReachabilityStoreReader + Clone> PruningPointManagerT<T> {
"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,
) {
Expand All @@ -106,7 +105,7 @@ impl<T: ReachabilityStoreReader + Clone> PruningPointManagerT<T> {
}

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)
}
Expand Down
2 changes: 0 additions & 2 deletions flexidag/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ fn test_dag_tips_store() {

let state = DagState {
tips: vec![Hash::random()],
pruning_point: Hash::random(),
};
dag.storage
.state_store
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0666a53

Please sign in to comment.