From 6dfd42169859867216953537f9c1240b047c6f13 Mon Sep 17 00:00:00 2001 From: sanlee42 Date: Fri, 22 Sep 2023 23:11:20 +0000 Subject: [PATCH] Update --- chain/src/chain.rs | 29 +++++++------------ cmd/db-exporter/src/verify_header.rs | 2 +- consensus/src/dag/blockdag.rs | 18 ++++++++---- sync/src/block_connector/write_block_chain.rs | 27 +++++++++++++---- types/src/block.rs | 7 +---- 5 files changed, 46 insertions(+), 37 deletions(-) diff --git a/chain/src/chain.rs b/chain/src/chain.rs index 3330fa0e7c..a957515ceb 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -47,6 +47,7 @@ use std::cmp::min; use std::iter::Extend; use std::option::Option::{None, Some}; use std::{collections::HashMap, sync::Arc}; +use starcoin_consensus::dag::types::ghostdata::GhostdagData; pub struct ChainStatusWithBlock { pub status: ChainStatus, @@ -65,7 +66,6 @@ pub struct BlockChain { epoch: Epoch, vm_metrics: Option, dag_accumulator: Option, - dag: BlockDAG, } impl BlockChain { @@ -74,12 +74,11 @@ impl BlockChain { head_block_hash: HashValue, storage: Arc, vm_metrics: Option, - dag: BlockDAG, ) -> Result { let head = storage .get_block_by_hash(head_block_hash)? .ok_or_else(|| format_err!("Can not find block by hash {:?}", head_block_hash))?; - Self::new_with_uncles(time_service, head, None, storage, vm_metrics, dag) + Self::new_with_uncles(time_service, head, None, storage, vm_metrics) } fn new_with_uncles( @@ -88,7 +87,6 @@ impl BlockChain { uncles: Option>, storage: Arc, vm_metrics: Option, - dag: BlockDAG, ) -> Result { let block_info = storage .get_block_info(head_block.id())? @@ -139,7 +137,6 @@ impl BlockChain { epoch, vm_metrics, dag_accumulator, - dag, }; watch(CHAIN_WATCH_NAME, "n1251"); match uncles { @@ -155,7 +152,6 @@ impl BlockChain { storage: Arc, genesis_epoch: Epoch, genesis_block: Block, - dag_store: FlexiDagStorage, ) -> Result { debug_assert!(genesis_block.header().is_genesis()); let txn_accumulator = MerkleAccumulator::new_empty( @@ -191,9 +187,7 @@ impl BlockChain { new_tips, dag_accumulator.get_info(), )?; - let mut dag = BlockDAG::new(genesis_id, 16, dag_store); - dag.init_with_genesis(DagHeader::new_genesis(genesis_header))?; - Self::new(time_service, executed_block.block.id(), storage, None, dag) + Self::new(time_service, executed_block.block.id(), storage, None) } pub fn current_epoch_uncles_size(&self) -> u64 { @@ -371,8 +365,8 @@ impl BlockChain { } pub fn verify_with_verifier(&mut self, block: Block) -> Result - where - V: BlockVerifier, + where + V: BlockVerifier, { V::verify_block(self, block) } @@ -383,8 +377,8 @@ impl BlockChain { dag_block_parent: Option, next_tips: &mut Option>, ) -> Result - where - V: BlockVerifier, + where + V: BlockVerifier, { let verified_block = self.verify_with_verifier::(block)?; watch(CHAIN_WATCH_NAME, "n1"); @@ -660,9 +654,9 @@ impl BlockChain { return Ok(next_tips_info == self - .dag_accumulator - .as_ref() - .map(|accumulator| accumulator.get_info())); + .dag_accumulator + .as_ref() + .map(|accumulator| accumulator.get_info())); } } @@ -887,7 +881,6 @@ impl ChainReader for BlockChain { self.storage.clone(), self.vm_metrics.clone(), //TODO: check missing blocks need to be clean - self.dag.clone(), ) } @@ -1155,7 +1148,7 @@ impl ChainWriter for BlockChain { .expect("dag blocks must have tips") .clone(), ) - .expect("failed to calculate the tips hash") + .expect("failed to calculate the tips hash") == executed_block.block().header().parent_hash(); } } diff --git a/cmd/db-exporter/src/verify_header.rs b/cmd/db-exporter/src/verify_header.rs index b49baffcdc..900e4bb71f 100644 --- a/cmd/db-exporter/src/verify_header.rs +++ b/cmd/db-exporter/src/verify_header.rs @@ -48,4 +48,4 @@ impl BatchCmdExec for Block { } } } -} +} \ No newline at end of file diff --git a/consensus/src/dag/blockdag.rs b/consensus/src/dag/blockdag.rs index 60bb1a1f74..9487158d3c 100644 --- a/consensus/src/dag/blockdag.rs +++ b/consensus/src/dag/blockdag.rs @@ -2,6 +2,7 @@ use super::ghostdag::protocol::{ColoringOutput, GhostdagManager}; use super::reachability::{inquirer, reachability_service::MTReachabilityService}; use super::types::ghostdata::GhostdagData; use crate::consensusdb::prelude::StoreError; +use crate::consensusdb::schemadb::GhostdagStoreReader; use crate::consensusdb::{ prelude::FlexiDagStorage, schemadb::{ @@ -80,10 +81,11 @@ impl BlockDAG { self.relations_store .insert(Hash::new(ORIGIN), BlockHashes::new(vec![])) .unwrap(); - self.commit_header(genesis) + let _ = self.addToDag(genesis); + Ok(()) } - pub fn commit_header(&mut self, header: DagHeader) -> anyhow::Result<()> { + pub fn addToDag(&mut self, header: DagHeader) -> anyhow::Result { //TODO:check genesis // Generate ghostdag data let parents_hash = header.parents_hash(); @@ -117,7 +119,7 @@ impl BlockDAG { let _ = self .header_store .insert(header.hash(), Arc::new(header.to_owned()), 0)?; - Ok(()) + return Ok(ghostdag_data.clone()) } fn is_in_dag(&self, _hash: Hash) -> anyhow::Result { @@ -134,7 +136,7 @@ impl BlockDAG { if is_orphan_block { return Ok(()); } - self.commit_header(header.clone()); + self.addToDag(header.clone()); self.check_missing_block(header)?; Ok(()) } @@ -144,7 +146,7 @@ impl BlockDAG { for orphan in orphans.iter() { let is_orphan = self.is_orphan(&orphan)?; if !is_orphan { - self.commit_header(header.clone()); + self.addToDag(header.clone()); } } } @@ -158,6 +160,10 @@ impl BlockDAG { } return Ok(true); } + pub fn get_ghostdag_data(&self, hash: Hash)->anyhow::Result>{ + let ghostdata = self.ghostdag_store.get_data(hash)?; + return Ok(ghostdata) + } fn update_orphans(&mut self, block_header: &DagHeader) -> anyhow::Result { let mut is_orphan = false; @@ -249,6 +255,6 @@ mod tests { let mut dag = BlockDAG::new(genesis_hash, k, db); dag.init_with_genesis(genesis); let block = DagHeader::new(BlockHeader::random(), vec![genesis_hash]); - dag.commit_header(block); + dag.addToDag(block); } } diff --git a/sync/src/block_connector/write_block_chain.rs b/sync/src/block_connector/write_block_chain.rs index 5e96f31f4b..f40a60773b 100644 --- a/sync/src/block_connector/write_block_chain.rs +++ b/sync/src/block_connector/write_block_chain.rs @@ -27,12 +27,14 @@ use starcoin_types::{ }; use std::fmt::Formatter; use std::sync::{Arc, Mutex}; +use starcoin_types::header::DagHeader; +use crate::block_connector::write_block_chain::ConnectOk::{DagConnected, ExeConnectMain}; const MAX_ROLL_BACK_BLOCK: usize = 10; pub struct WriteBlockChainService

-where - P: TxPoolSyncService, + where + P: TxPoolSyncService, { config: Arc, startup_info: StartupInfo, @@ -92,8 +94,8 @@ impl std::fmt::Display for ConnectOk { } impl

WriteableChainService for WriteBlockChainService

-where - P: TxPoolSyncService + 'static, + where + P: TxPoolSyncService + 'static, { fn try_connect(&mut self, block: Block, tips_headers: Option>) -> Result<()> { let _timer = self @@ -124,8 +126,8 @@ where } impl

WriteBlockChainService

-where - P: TxPoolSyncService + 'static, + where + P: TxPoolSyncService + 'static, { pub fn new( config: Arc, @@ -791,6 +793,19 @@ where return Ok(ConnectOk::ExeConnectMain(executed_block)); } + fn connect_dag_inner(&mut self, block: Block, parents_hash: Vec, + ) -> Result { + let ghost_dag_data = self.dag.lock().unwrap().addToDag(DagHeader::new(block.header, parents_hash))?; + let past_header = ghost_dag_data.selected_parent; + let mut chain = self.main.fork(past_header)?; + for blue_hash in ghost_dag_data.mergeset_blues{ + chain.apply(blue_hash); + } + + //self.broadcast_new_head(); + Ok(DagConnected) + } + fn connect_inner( &mut self, block: Block, diff --git a/types/src/block.rs b/types/src/block.rs index c681bb3333..2d5b11bfae 100644 --- a/types/src/block.rs +++ b/types/src/block.rs @@ -730,12 +730,7 @@ impl Block { .map(|uncles| uncles.len() as u64) .unwrap_or(0); - let parent = if dag_block_parent.is_some() { - dag_block_parent.unwrap() - } else { - self.header.parent_hash() - }; - + let parent = dag_block_parent.unwrap_or(self.header.parent_hash()); BlockMetadata::new( parent, self.header.timestamp,