diff --git a/chain/src/chain.rs b/chain/src/chain.rs index f48ad34251..7cc49f75a8 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -53,7 +53,7 @@ pub struct ChainStatusWithBlock { } impl ChainStatusWithBlock { pub fn dag_tips(&self) -> &Option> { - &self.status.tips_hash + &self.status.tips_hash() } } pub struct BlockChain { @@ -1305,7 +1305,7 @@ impl ChainWriter for BlockChain { Self::calculate_dag_accumulator_key( self.status .status - .tips_hash + .tips_hash() .as_ref() .expect("dag blocks must have tips") .clone(), @@ -1334,18 +1334,18 @@ impl ChainWriter for BlockChain { ); self.statedb = ChainStateDB::new(self.storage.clone().into_super_arc(), Some(state_root)); - let tips = self.status.status.tips_hash.clone(); - let next_tips = match tips { - Some(mut tips) => { - if !tips.contains(&block.header().id()) { - tips.push(block.header().id()); - } - Some(tips) - } - None => None, - }; + // let tips = self.status.status.tips_hash.clone(); + // let next_tips = match tips { + // Some(mut tips) => { + // if !tips.contains(&block.header().id()) { + // tips.push(block.header().id()); + // } + // Some(tips) + // } + // None => None, + // }; self.status = ChainStatusWithBlock { - status: ChainStatus::new(block.header().clone(), block_info.clone(), next_tips), + status: ChainStatus::new(block.header().clone(), block_info.clone(), self.status().tips_hash().clone()), head: block.clone(), }; if self.epoch.end_block_number() == block.header().number() { @@ -1369,7 +1369,7 @@ impl ChainWriter for BlockChain { } fn update_tips(&mut self, new_tips: Vec) -> Result<()> { - self.status.status.tips_hash = Some(new_tips); + self.status.status.update_tips(Some(new_tips)); Ok(()) } } diff --git a/chain/src/verifier/mod.rs b/chain/src/verifier/mod.rs index ce54de696b..e3173779da 100644 --- a/chain/src/verifier/mod.rs +++ b/chain/src/verifier/mod.rs @@ -198,9 +198,9 @@ impl BlockVerifier for BasicVerifier { // todo: For a dag block // 1. It could be the first dag block, the chain is just a normal single chain // 2. Or, both block and dag are created for flexidag - if chain_status.tips_hash.is_some() { + if chain_status.tips_hash().is_some() { // todo: make sure current block is a dag block - let mut tips_hash = chain_status.tips_hash.clone().unwrap(); + let mut tips_hash = chain_status.tips_hash().clone().unwrap(); tips_hash.sort(); // if it is a dag block diff --git a/consensus/src/consensusdb/consensus_ghostdag.rs b/consensus/src/consensusdb/consensus_ghostdag.rs index ca99a0b8ea..f0cb7758f5 100644 --- a/consensus/src/consensusdb/consensus_ghostdag.rs +++ b/consensus/src/consensusdb/consensus_ghostdag.rs @@ -6,6 +6,7 @@ use super::{ writer::BatchDbWriter, }; use crate::define_schema; +use starcoin_logger::prelude::info; use starcoin_types::blockhash::{ BlockHashMap, BlockHashes, BlockLevel, BlueWorkType, HashKTypeMap, }; diff --git a/consensus/src/dag/blockdag.rs b/consensus/src/dag/blockdag.rs index 744dad1d1d..1310bbf6e9 100644 --- a/consensus/src/dag/blockdag.rs +++ b/consensus/src/dag/blockdag.rs @@ -19,6 +19,7 @@ use starcoin_accumulator::node::AccumulatorStoreType; use starcoin_accumulator::{Accumulator, MerkleAccumulator}; use starcoin_config::{ChainNetworkID, NodeConfig, RocksdbConfig}; use starcoin_crypto::{HashValue as Hash, HashValue}; +use starcoin_logger::prelude::info; use starcoin_storage::flexi_dag::SyncFlexiDagSnapshotHasher; use starcoin_storage::storage::CodecKVStore; use starcoin_storage::Store; diff --git a/miner/src/create_block_template/mod.rs b/miner/src/create_block_template/mod.rs index 539b19b050..511a7a7ea0 100644 --- a/miner/src/create_block_template/mod.rs +++ b/miner/src/create_block_template/mod.rs @@ -27,6 +27,7 @@ use starcoin_types::{ system_events::{NewBranch, NewHeadBlock}, }; use starcoin_vm_types::transaction::SignedUserTransaction; +use std::backtrace::Backtrace; use std::cmp::min; use std::{collections::HashMap, sync::Arc}; @@ -375,7 +376,7 @@ where let epoch = self.chain.epoch(); let strategy = epoch.strategy(); let difficulty = strategy.calculate_next_difficulty(&self.chain)?; - let tips_hash = self.chain.status().tips_hash; + let tips_hash = self.chain.status().tips_hash().clone(); let (uncles, blue_blocks) = { match &tips_hash { None => (self.find_uncles(), None), @@ -384,10 +385,8 @@ where let mut blues = dag.ghostdata(tips).mergeset_blues.to_vec(); assert!(blues.len() > 0, "the count of the blue block should be larger than 0"); let mut blue_blocks = vec![]; - if !self.is_dag_genesis(blues[0])? { - let selected_parent = blues.remove(0); - assert_eq!(previous_header.id(), selected_parent); - } + let selected_parent = blues.remove(0); // 5 + // assert_eq!(previous_header.id(), selected_parent);// 4, 5 for blue in &blues { let block = self .storage @@ -409,6 +408,7 @@ where } } }; + info!( "[CreateBlockTemplate] previous_header: {:?}, block_gas_limit: {}, max_txns: {}, txn len: {}, uncles len: {}, timestamp: {}", previous_header, @@ -434,7 +434,6 @@ where )?; let excluded_txns = opened_block.push_txns(txns)?; - let template = opened_block.finalize()?; for invalid_txn in excluded_txns.discarded_txns { self.tx_provider.remove_invalid_txn(invalid_txn.id()); diff --git a/miner/src/task.rs b/miner/src/task.rs index ac42091d47..e0b0f56f05 100644 --- a/miner/src/task.rs +++ b/miner/src/task.rs @@ -3,6 +3,7 @@ use crate::metrics::MinerMetrics; use crate::BlockHeaderExtra; +use starcoin_logger::prelude::info; use starcoin_metrics::HistogramTimer; use starcoin_types::block::{Block, BlockTemplate}; diff --git a/sync/src/block_connector/write_block_chain.rs b/sync/src/block_connector/write_block_chain.rs index d093770d69..bcfd44f1da 100644 --- a/sync/src/block_connector/write_block_chain.rs +++ b/sync/src/block_connector/write_block_chain.rs @@ -107,7 +107,6 @@ where .map(|metrics| metrics.chain_block_connect_time.start_timer()); let result = self.connect_inner(block.clone()); - if let Some(metrics) = self.metrics.as_ref() { let result = match result.as_ref() { std::result::Result::Ok(connect) => format!("Ok_{}", connect), @@ -265,7 +264,7 @@ where self.update_startup_info(new_branch.head_block().header())?; ctx.broadcast(NewHeadBlock { executed_block: Arc::new(new_branch.head_block()), - tips: self.main.status().tips_hash.clone(), + // tips: self.main.status().tips_hash.clone(), }); Ok(()) } else { @@ -516,7 +515,7 @@ where if let Err(e) = self.bus.broadcast(NewHeadBlock { executed_block: Arc::new(block), - tips: self.main.status().tips_hash.clone(), + // tips: self.main.status().tips_hash.clone(), }) { error!("Broadcast NewHeadBlock error: {:?}", e); } @@ -535,9 +534,10 @@ where } pub fn update_tips(&mut self, block_header: BlockHeader) -> Result<()> { - self.main.status().tips_hash.map(|mut tips| { + self.main.status().tips_hash().clone().map(|mut tips| { if !tips.contains(&block_header.id()) { - tips.push(block_header.id()) + tips.push(block_header.id()); + self.main.status().update_tips(Some(tips)); } }); async_std::task::block_on(self.flexidag_service.send(UpdateDagTips { @@ -551,10 +551,7 @@ where } pub fn dump_tips(&mut self, block_header: BlockHeader) -> Result<()> { - self.main.status().tips_hash.map(|mut tips| { - tips.clear(); - tips.push(block_header.id()); - }); + self.main.status().update_tips(Some(vec![block_header.id()])); async_std::task::block_on(self.flexidag_service.send(DumpTipsToAccumulator { block_header: block_header.clone(), current_head_block_id: self.main.status().head().id(), @@ -579,6 +576,7 @@ where return Ok(ConnectOk::MainDuplicate); } + let parent_hash = block.parent_hash()?; if self.main.current_header().id() == parent_hash && !self.block_exist(block_id)? { diff --git a/sync/src/sync.rs b/sync/src/sync.rs index 3bfc080c33..1934e4cbcb 100644 --- a/sync/src/sync.rs +++ b/sync/src/sync.rs @@ -718,7 +718,7 @@ impl EventHandler for SyncService { if self.sync_status.update_chain_status(ChainStatus::new( msg.executed_block.header().clone(), msg.executed_block.block_info.clone(), - msg.tips, + self.sync_status.chain_status().tips_hash().clone(), )) { self.sync_status.update_dag_accumulator_info( self.storage diff --git a/types/src/block.rs b/types/src/block.rs index 624138d8cc..5ef975ba9e 100644 --- a/types/src/block.rs +++ b/types/src/block.rs @@ -774,13 +774,13 @@ impl Block { // } pub fn parent_hash(&self) -> anyhow::Result { - if self.header().parents_hash.is_some() { - self.dag_parent_and_tips() - .map(|dag| dag.0.id()) - .ok_or_else(|| format_err!("missing parent and tips for dag block")) - } else { + // if self.header().parents_hash.is_some() { + // self.dag_parent_and_tips() + // .map(|dag| dag.0.id()) + // .ok_or_else(|| format_err!("missing parent and tips for dag block")) + // } else { Ok(self.header().parent_hash()) - } + // } } pub fn id(&self) -> HashValue { diff --git a/types/src/startup_info.rs b/types/src/startup_info.rs index 664017c6da..156edf1f82 100644 --- a/types/src/startup_info.rs +++ b/types/src/startup_info.rs @@ -133,7 +133,7 @@ pub struct ChainStatus { /// Chain block info pub info: BlockInfo, /// tips - pub tips_hash:Option> + tips_hash:Option> } impl ChainStatus { @@ -174,6 +174,10 @@ impl ChainStatus { &self.info } + pub fn tips_hash(&self) -> &Option> { + &self.tips_hash + } + pub fn total_difficulty(&self) -> U256 { self.info.total_difficulty } @@ -181,6 +185,10 @@ impl ChainStatus { pub fn into_inner(self) -> (BlockHeader, BlockInfo) { (self.head, self.info) } + + pub fn update_tips(&mut self, tips: Option>) { + self.tips_hash = tips; + } } impl Sample for ChainStatus { diff --git a/types/src/system_events.rs b/types/src/system_events.rs index a52cf009c3..138a3948c6 100644 --- a/types/src/system_events.rs +++ b/types/src/system_events.rs @@ -12,7 +12,7 @@ use std::sync::Arc; #[derive(Clone, Debug)] pub struct NewHeadBlock { pub executed_block: Arc, - pub tips: Option>, + // pub tips: Option>, } /// may be uncle block diff --git a/vm/starcoin-transactional-test-harness/src/fork_chain.rs b/vm/starcoin-transactional-test-harness/src/fork_chain.rs index 03f6781d9f..b40abdd81f 100644 --- a/vm/starcoin-transactional-test-harness/src/fork_chain.rs +++ b/vm/starcoin-transactional-test-harness/src/fork_chain.rs @@ -118,7 +118,7 @@ impl ForkBlockChain { let tips = self .status .as_ref() - .and_then(|status| status.status.tips_hash.clone()); + .and_then(|status| status.status.tips_hash().clone()); let parents = block.header.parents_hash(); match (tips, parents) { (Some(mut tips), Some(parents)) => {