From 3b04e982c3f7f698f276d71049cf06086a868a2d Mon Sep 17 00:00:00 2001 From: sanlee42 Date: Thu, 15 Feb 2024 12:35:20 +0800 Subject: [PATCH] Fix get_work panic --- chain/src/chain.rs | 4 ++-- flexidag/dag/src/blockdag.rs | 21 ++++++++++------- flexidag/dag/src/ghostdag/protocol.rs | 31 +++++++++++++++++--------- kube/manifest/starcoin-proxima.yaml | 6 ++--- miner/src/create_block_template/mod.rs | 2 +- 5 files changed, 40 insertions(+), 24 deletions(-) diff --git a/chain/src/chain.rs b/chain/src/chain.rs index 5852d9ca6e..b6e2af4b56 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -280,7 +280,7 @@ impl BlockChain { match &tips_hash { None => (uncles, None), Some(tips) => { - let mut blues = self.dag.ghostdata(tips).mergeset_blues.to_vec(); + let mut blues = self.dag.ghostdata(tips)?.mergeset_blues.to_vec(); info!( "create block template with tips:{:?}, ghostdata blues:{:?}", &tips_hash, blues @@ -1255,7 +1255,7 @@ impl BlockChain { // Caculate the ghostdata of the virutal node created by all tips. // And the ghostdata.selected of the tips will be the latest head. let block_hash = { - let ghost_of_tips = dag.ghostdata(tips.as_slice()); + let ghost_of_tips = dag.ghostdata(tips.as_slice())?; ghost_of_tips.selected_parent }; debug!( diff --git a/flexidag/dag/src/blockdag.rs b/flexidag/dag/src/blockdag.rs index 9c7cc13362..ce22015f09 100644 --- a/flexidag/dag/src/blockdag.rs +++ b/flexidag/dag/src/blockdag.rs @@ -86,7 +86,7 @@ impl BlockDAG { self.commit(genesis)?; Ok(()) } - pub fn ghostdata(&self, parents: &[HashValue]) -> GhostdagData { + pub fn ghostdata(&self, parents: &[HashValue]) -> anyhow::Result { self.ghostdag_manager.ghostdag(parents) } @@ -101,13 +101,18 @@ impl BlockDAG { pub fn commit(&self, header: BlockHeader) -> anyhow::Result<()> { // Generate ghostdag data let parents = header.parents(); - let ghostdata = self.ghostdata_by_hash(header.id())?.unwrap_or_else(|| { - Arc::new(if header.is_dag_genesis() { - self.ghostdag_manager.genesis_ghostdag_data(&header) - } else { - self.ghostdag_manager.ghostdag(&parents) - }) - }); + let ghostdata = match self.ghostdata_by_hash(header.id())? { + None => { + if header.is_dag_genesis() { + Arc::new(self.ghostdag_manager.genesis_ghostdag_data(&header)) + } else { + let ghostdata = self.ghostdag_manager.ghostdag(&parents)?; + Arc::new(ghostdata) + } + } + Some(ghostdata) => ghostdata, + }; + // Store ghostdata self.storage .ghost_dag_store diff --git a/flexidag/dag/src/ghostdag/protocol.rs b/flexidag/dag/src/ghostdag/protocol.rs index 40a3537f43..0b11711583 100644 --- a/flexidag/dag/src/ghostdag/protocol.rs +++ b/flexidag/dag/src/ghostdag/protocol.rs @@ -2,11 +2,11 @@ use super::util::Refs; use crate::consensusdb::schemadb::{GhostdagStoreReader, HeaderStoreReader, RelationsStoreReader}; use crate::reachability::reachability_service::ReachabilityService; use crate::types::{ghostdata::GhostdagData, ordering::*}; +use anyhow::{Context, Result}; use starcoin_crypto::HashValue as Hash; use starcoin_types::block::BlockHeader; use starcoin_types::blockhash::{BlockHashMap, BlockHashes, BlueWorkType, HashKTypeMap, KType}; use std::sync::Arc; - #[derive(Clone)] pub struct GhostdagManager< T: GhostdagStoreReader, @@ -66,16 +66,27 @@ impl< )) } - pub fn find_selected_parent(&self, parents: impl IntoIterator) -> Hash { + pub fn find_selected_parent( + &self, + parents: impl IntoIterator, + ) -> anyhow::Result { parents .into_iter() - .map(|parent| SortableBlock { - hash: parent, - blue_work: self.ghostdag_store.get_blue_work(parent).unwrap(), + .map(|parent| { + let blue_work = self + .ghostdag_store + .get_blue_work(parent) + .with_context(|| format!("Failed to get blue work for parent {:?}", parent))?; + Ok(SortableBlock { + hash: parent, + blue_work, + }) }) + .collect::>>()? + .into_iter() .max() - .unwrap() - .hash + .map(|sortable_block| sortable_block.hash) + .ok_or_else(|| anyhow::Error::msg("No parent found")) } /// Runs the GHOSTDAG protocol and calculates the block GhostdagData by the given parents. @@ -96,13 +107,13 @@ impl< /// blues_anticone_sizes. /// /// For further details see the article https://eprint.iacr.org/2018/104.pdf - pub fn ghostdag(&self, parents: &[Hash]) -> GhostdagData { + pub fn ghostdag(&self, parents: &[Hash]) -> Result { assert!( !parents.is_empty(), "genesis must be added via a call to init" ); // Run the GHOSTDAG parent selection algorithm - let selected_parent = self.find_selected_parent(parents.iter().copied()); + let selected_parent = self.find_selected_parent(parents.iter().copied())?; // Initialize new GHOSTDAG block data with the selected parent let mut new_block_data = GhostdagData::new_with_selected_parent(selected_parent, self.k); // Get the mergeset in consensus-agreed topological order (topological here means forward in time from blocks to children) @@ -147,7 +158,7 @@ impl< new_block_data.finalize_score_and_work(blue_score, blue_work); - new_block_data + Ok(new_block_data) } fn check_blue_candidate_with_chain_block( diff --git a/kube/manifest/starcoin-proxima.yaml b/kube/manifest/starcoin-proxima.yaml index fbe67d333f..a0636f1fa8 100644 --- a/kube/manifest/starcoin-proxima.yaml +++ b/kube/manifest/starcoin-proxima.yaml @@ -11,7 +11,7 @@ spec: matchLabels: app: starcoin serviceName: starcoin-svc - replicas: 1 + replicas: 2 template: metadata: name: starcoin @@ -23,13 +23,13 @@ spec: starcoin/node-pool: seed-pool containers: - name: starcoin - image: ghcr.io/starcoin/starcoin:v1.13.8 + image: ghcr.io/starcoinorg/starcoin:v2.0.3-alpha imagePullPolicy: Always command: - bash - -c args: - - rm -rf /sc-data/proxima/starcoin.ipc /sc-data/proxima/starcoindb/db/starcoindb/LOCK; + - rm -rf /sc-data/proxima/ /sc-data/proxima/starcoindb/db/starcoindb/LOCK; id=$(echo -e $POD_NAME|awk -F'-' '{print $2}') && IFS='; ' read -r -a node_keys <<< $NODE_KEYS && node_key=${node_keys[$id]}; if [ ! -z $node_key ]; then diff --git a/miner/src/create_block_template/mod.rs b/miner/src/create_block_template/mod.rs index 861461617c..850ecab720 100644 --- a/miner/src/create_block_template/mod.rs +++ b/miner/src/create_block_template/mod.rs @@ -347,7 +347,7 @@ where match &tips_hash { None => (self.find_uncles(), None), Some(tips) => { - let mut blues = self.dag.ghostdata(tips).mergeset_blues.to_vec(); + let mut blues = self.dag.ghostdata(tips)?.mergeset_blues.to_vec(); info!( "create block template with tips:{:?},ghostdata blues:{:?}", &tips_hash, blues