Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix get_work panic
  • Loading branch information
sanlee42 committed Feb 15, 2024
1 parent 8b5a80f commit 3b04e98
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 24 deletions.
4 changes: 2 additions & 2 deletions chain/src/chain.rs
Expand Up @@ -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
Expand Down Expand Up @@ -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!(
Expand Down
21 changes: 13 additions & 8 deletions flexidag/dag/src/blockdag.rs
Expand Up @@ -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<GhostdagData> {
self.ghostdag_manager.ghostdag(parents)
}

Expand All @@ -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
Expand Down
31 changes: 21 additions & 10 deletions flexidag/dag/src/ghostdag/protocol.rs
Expand Up @@ -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,
Expand Down Expand Up @@ -66,16 +66,27 @@ impl<
))
}

pub fn find_selected_parent(&self, parents: impl IntoIterator<Item = Hash>) -> Hash {
pub fn find_selected_parent(
&self,
parents: impl IntoIterator<Item = Hash>,
) -> anyhow::Result<Hash> {
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::<Result<Vec<_>>>()?
.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.
Expand All @@ -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<GhostdagData> {
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)
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions kube/manifest/starcoin-proxima.yaml
Expand Up @@ -11,7 +11,7 @@ spec:
matchLabels:
app: starcoin
serviceName: starcoin-svc
replicas: 1
replicas: 2
template:
metadata:
name: starcoin
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion miner/src/create_block_template/mod.rs
Expand Up @@ -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
Expand Down

0 comments on commit 3b04e98

Please sign in to comment.