Skip to content

Commit

Permalink
add tips update
Browse files Browse the repository at this point in the history
  • Loading branch information
jackzhhuang committed Dec 5, 2023
1 parent 596b809 commit ad21c67
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 41 deletions.
28 changes: 14 additions & 14 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub struct ChainStatusWithBlock {
}
impl ChainStatusWithBlock {
pub fn dag_tips(&self) -> &Option<Vec<HashValue>> {
&self.status.tips_hash
&self.status.tips_hash()
}
}
pub struct BlockChain {
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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() {
Expand All @@ -1369,7 +1369,7 @@ impl ChainWriter for BlockChain {
}

fn update_tips(&mut self, new_tips: Vec<HashValue>) -> Result<()> {
self.status.status.tips_hash = Some(new_tips);
self.status.status.update_tips(Some(new_tips));
Ok(())
}
}
Expand Down
4 changes: 2 additions & 2 deletions chain/src/verifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions consensus/src/consensusdb/consensus_ghostdag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
1 change: 1 addition & 0 deletions consensus/src/dag/blockdag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 5 additions & 6 deletions miner/src/create_block_template/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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),
Expand All @@ -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
Expand All @@ -409,6 +408,7 @@ where
}
}
};

info!(
"[CreateBlockTemplate] previous_header: {:?}, block_gas_limit: {}, max_txns: {}, txn len: {}, uncles len: {}, timestamp: {}",
previous_header,
Expand All @@ -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());
Expand Down
1 change: 1 addition & 0 deletions miner/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
16 changes: 7 additions & 9 deletions sync/src/block_connector/write_block_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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 {
Expand All @@ -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(),
Expand All @@ -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)? {
Expand Down
2 changes: 1 addition & 1 deletion sync/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ impl EventHandler<Self, NewHeadBlock> 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
Expand Down
12 changes: 6 additions & 6 deletions types/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,13 +774,13 @@ impl Block {
// }

pub fn parent_hash(&self) -> anyhow::Result<HashValue> {
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 {
Expand Down
10 changes: 9 additions & 1 deletion types/src/startup_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub struct ChainStatus {
/// Chain block info
pub info: BlockInfo,
/// tips
pub tips_hash:Option<Vec<HashValue>>
tips_hash:Option<Vec<HashValue>>
}

impl ChainStatus {
Expand Down Expand Up @@ -174,13 +174,21 @@ impl ChainStatus {
&self.info
}

pub fn tips_hash(&self) -> &Option<Vec<HashValue>> {
&self.tips_hash
}

pub fn total_difficulty(&self) -> U256 {
self.info.total_difficulty
}

pub fn into_inner(self) -> (BlockHeader, BlockInfo) {
(self.head, self.info)
}

pub fn update_tips(&mut self, tips: Option<Vec<HashValue>>) {
self.tips_hash = tips;
}
}

impl Sample for ChainStatus {
Expand Down
2 changes: 1 addition & 1 deletion types/src/system_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::sync::Arc;
#[derive(Clone, Debug)]
pub struct NewHeadBlock {
pub executed_block: Arc<ExecutedBlock>,
pub tips: Option<Vec<HashValue>>,
// pub tips: Option<Vec<HashValue>>,
}

/// may be uncle block
Expand Down
2 changes: 1 addition & 1 deletion vm/starcoin-transactional-test-harness/src/fork_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) => {
Expand Down

0 comments on commit ad21c67

Please sign in to comment.