Skip to content

Commit 8801176

Browse files
committed
add sync finish logic
1 parent edb1dd4 commit 8801176

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

flexidag/src/flexidag_service.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
};
55

66
use anyhow::{anyhow, bail, Error, Ok, Result};
7-
use starcoin_accumulator::{accumulator_info::AccumulatorInfo, Accumulator, MerkleAccumulator};
7+
use starcoin_accumulator::{accumulator_info::AccumulatorInfo, Accumulator, MerkleAccumulator, node::AccumulatorStoreType};
88
use starcoin_config::{NodeConfig, TimeService};
99
use starcoin_consensus::{dag::types::ghostdata::GhostdagData, BlockDAG};
1010
use starcoin_crypto::HashValue;
@@ -14,7 +14,7 @@ use starcoin_service_registry::{
1414
use starcoin_storage::{
1515
flexi_dag::{KTotalDifficulty, SyncFlexiDagSnapshot, SyncFlexiDagSnapshotHasher},
1616
storage::CodecKVStore,
17-
BlockStore, Storage, SyncFlexiDagStore, block_info::BlockInfoStore,
17+
BlockStore, Storage, SyncFlexiDagStore, block_info::BlockInfoStore, Store,
1818
};
1919
use starcoin_types::{block::BlockHeader, header::DagHeader, startup_info};
2020

@@ -127,6 +127,15 @@ impl ServiceRequest for ForkDagAccumulator {
127127
type Response = anyhow::Result<AccumulatorInfo>;
128128
}
129129

130+
#[derive(Debug, Clone)]
131+
pub struct FinishSync {
132+
pub dag_accumulator_info: AccumulatorInfo,
133+
}
134+
135+
impl ServiceRequest for FinishSync {
136+
type Response = anyhow::Result<()>;
137+
}
138+
130139
pub struct TipInfo {
131140
tips: Option<Vec<HashValue>>, // some is for dag or the state of the chain is still in old version
132141
k_total_difficulties: BTreeSet<KTotalDifficulty>,
@@ -536,8 +545,28 @@ impl ServiceHandler<Self, ForkDagAccumulator> for FlexidagService {
536545
} else {
537546
self.merge_from_small_dag(msg)
538547
}
539-
540-
541-
// append the ForkDagAccumulator.new_blocks and the fetched blocks above into the forked dag accumulator
542548
}
543549
}
550+
551+
impl ServiceHandler<Self, FinishSync> for FlexidagService {
552+
fn handle(
553+
&mut self,
554+
msg: FinishSync,
555+
_ctx: &mut ServiceContext<FlexidagService>,
556+
) -> Result<()> {
557+
let dag_accumulator = self.dag_accumulator.ok_or_else(|| anyhow!("the dag_accumulator is none when sync finish"))?;
558+
let local_info = dag_accumulator.get_info();
559+
if msg.dag_accumulator_info.get_num_leaves() < local_info.get_num_leaves() {
560+
let mut new_dag_accumulator = MerkleAccumulator::new_with_info(msg.dag_accumulator_info, self.storage.get_accumulator_store(AccumulatorStoreType::SyncDag));
561+
for index in msg.dag_accumulator_info.get_num_leaves()..local_info.get_num_leaves() {
562+
let key = dag_accumulator.get_leaf(index)?.ok_or_else(|| anyhow!("the dag_accumulator leaf is none when sync finish"))?;
563+
new_dag_accumulator.append(&[key])?;
564+
}
565+
self.dag_accumulator = Some(new_dag_accumulator);
566+
Ok(())
567+
} else {
568+
self.dag_accumulator = Some(MerkleAccumulator::new_with_info(msg.dag_accumulator_info, self.storage.get_accumulator_store(AccumulatorStoreType::SyncDag)));
569+
Ok(())
570+
}
571+
}
572+
}

sync/src/tasks/block_sync_task.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use starcoin_chain_api::{ChainReader, ChainWriter, ConnectBlockError, ExecutedBl
1515
use starcoin_config::{Connect, G_CRATE_VERSION};
1616
use starcoin_consensus::BlockDAG;
1717
use starcoin_crypto::HashValue;
18-
use starcoin_flexidag::flexidag_service::{AddToDag, GetDagTips, ForkDagAccumulator};
18+
use starcoin_flexidag::flexidag_service::{AddToDag, GetDagTips, ForkDagAccumulator, FinishSync};
1919
use starcoin_flexidag::FlexidagService;
2020
use starcoin_logger::prelude::*;
2121
use starcoin_service_registry::ServiceRef;
@@ -417,6 +417,11 @@ where
417417
dag_accumulator_index: start_index,
418418
block_header_id: self.chain.head_block().id(),
419419
}))??);
420+
if state == State::Enough {
421+
async_std::task::block_on(self.flexidag_service.send(FinishSync {
422+
dag_accumulator_info: self.new_dag_accumulator_info.clone(),
423+
}))??
424+
}
420425
return Ok(state);
421426
}
422427

0 commit comments

Comments
 (0)