Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug: no store the sync dag block #4259

Open
wants to merge 16 commits into
base: dag-master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions flexidag/src/blockdag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,13 @@ impl BlockDAG {
let mut merge_set = ghostdata
.unordered_mergeset_without_selected_parent()
.filter(|hash| self.storage.reachability_store.read().has(*hash).unwrap())
.collect::<Vec<_>>()
.chain(
header
.parents_hash()
.into_iter()
.filter(|parent_id| *parent_id != ghostdata.selected_parent),
)
.collect::<HashSet<_>>()
.into_iter();
let add_block_result = {
let mut reachability_writer = reachability_store.write();
Expand Down Expand Up @@ -482,8 +488,8 @@ impl BlockDAG {
let dag_state = self.get_dag_state(previous_pruning_point)?;
let next_ghostdata = self.ghostdata(&dag_state.tips)?;
info!(
"start to calculate the mergeset and tips for tips: {:?}, and last pruning point: {:?} and next ghostdata: {:?}",
dag_state.tips, previous_pruning_point, next_ghostdata,
"start to calculate the mergeset and tips for tips: {:?}, and last pruning point: {:?} and next ghostdata's selected parents: {:?} and blues set are {:?}",
dag_state.tips, previous_pruning_point, next_ghostdata.selected_parent, next_ghostdata.mergeset_blues,
);
let next_pruning_point = self.pruning_point_manager().next_pruning_point(
previous_pruning_point,
Expand Down
35 changes: 27 additions & 8 deletions flexidag/src/ghostdag/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,22 +210,29 @@ impl<
}
}

let remote_blue_set = blue_blocks
.iter()
.map(|header| header.id())
.collect::<HashSet<_>>();
if new_block_data
.mergeset_blues
.iter()
.skip(1)
.cloned()
.collect::<HashSet<_>>()
!= blue_blocks
!= remote_blue_set
{
warn!("The data of blue set is not equal when executing the block: {:?}, for {:?}, checking data: {:?}", header.id(), blue_blocks.iter().map(|header| header.id()).collect::<Vec<_>>(), new_block_data.mergeset_blues);
let ghostdata = self.ghostdag(&header.parents_hash())?;
if ghostdata
.mergeset_blues
.iter()
.map(|header| header.id())
.skip(1)
.cloned()
.collect::<HashSet<_>>()
{
if header.number() < 10000000 {
// no bail before 10000000
warn!("The data of blue set is not equal when executing the block: {:?}, for {:?}, checking data: {:?}", header.id(), blue_blocks.iter().map(|header| header.id()).collect::<Vec<_>>(), new_block_data.mergeset_blues);
} else {
bail!("The data of blue set is not equal when executing the block: {:?}, for {:?}, checking data: {:?}", header.id(), blue_blocks.iter().map(|header| header.id()).collect::<Vec<_>>(), new_block_data.mergeset_blues);
!= remote_blue_set
{
bail!("The ghost data of blue set is not equal when executing the block: {:?}, for {:?}, checking data: {:?}", header.id(), blue_blocks.iter().map(|header| header.id()).collect::<Vec<_>>(), ghostdata.mergeset_blues);
Comment on lines +213 to +235
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve blue set verification logic and error handling

The current implementation has several issues:

  1. Silently falling back to ghostdag when blue sets don't match could mask underlying problems
  2. The warning message could be more descriptive about the blue set mismatch
  3. The second verification step could mask underlying issues

Consider this improved implementation:

        let remote_blue_set = blue_blocks
            .iter()
            .map(|header| header.id())
            .collect::<HashSet<_>>();
-        if new_block_data
+        let calculated_blue_set = new_block_data
            .mergeset_blues
            .iter()
            .skip(1)
            .cloned()
-            .collect::<HashSet<_>>()
-            != remote_blue_set
-        {
-            warn!("The data of blue set is not equal when executing the block: {:?}, for {:?}, checking data: {:?}", header.id(), blue_blocks.iter().map(|header| header.id()).collect::<Vec<_>>(), new_block_data.mergeset_blues);
-            let ghostdata = self.ghostdag(&header.parents_hash())?;
-            if ghostdata
-                .mergeset_blues
-                .iter()
-                .skip(1)
-                .cloned()
-                .collect::<HashSet<_>>()
-                != remote_blue_set
-            {
-                bail!("The ghost data of blue set is not equal when executing the block: {:?}, for {:?}, checking data: {:?}", header.id(), blue_blocks.iter().map(|header| header.id()).collect::<Vec<_>>(), ghostdata.mergeset_blues);
-            }
-        }
+            .collect::<HashSet<_>>();
+        
+        if calculated_blue_set != remote_blue_set {
+            let missing_blues: Vec<_> = remote_blue_set.difference(&calculated_blue_set).collect();
+            let extra_blues: Vec<_> = calculated_blue_set.difference(&remote_blue_set).collect();
+            
+            bail!(
+                "Blue set mismatch for block {:?}:\nExpected blues: {:?}\nCalculated blues: {:?}\nMissing blues: {:?}\nExtra blues: {:?}",
+                header.id(),
+                remote_blue_set,
+                calculated_blue_set,
+                missing_blues,
+                extra_blues
+            );
+        }

This change:

  1. Provides more detailed error messages showing the exact differences
  2. Removes the redundant second verification step
  3. Fails fast when a mismatch is detected
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let remote_blue_set = blue_blocks
.iter()
.map(|header| header.id())
.collect::<HashSet<_>>();
if new_block_data
.mergeset_blues
.iter()
.skip(1)
.cloned()
.collect::<HashSet<_>>()
!= blue_blocks
!= remote_blue_set
{
warn!("The data of blue set is not equal when executing the block: {:?}, for {:?}, checking data: {:?}", header.id(), blue_blocks.iter().map(|header| header.id()).collect::<Vec<_>>(), new_block_data.mergeset_blues);
let ghostdata = self.ghostdag(&header.parents_hash())?;
if ghostdata
.mergeset_blues
.iter()
.map(|header| header.id())
.skip(1)
.cloned()
.collect::<HashSet<_>>()
{
if header.number() < 10000000 {
// no bail before 10000000
warn!("The data of blue set is not equal when executing the block: {:?}, for {:?}, checking data: {:?}", header.id(), blue_blocks.iter().map(|header| header.id()).collect::<Vec<_>>(), new_block_data.mergeset_blues);
} else {
bail!("The data of blue set is not equal when executing the block: {:?}, for {:?}, checking data: {:?}", header.id(), blue_blocks.iter().map(|header| header.id()).collect::<Vec<_>>(), new_block_data.mergeset_blues);
!= remote_blue_set
{
bail!("The ghost data of blue set is not equal when executing the block: {:?}, for {:?}, checking data: {:?}", header.id(), blue_blocks.iter().map(|header| header.id()).collect::<Vec<_>>(), ghostdata.mergeset_blues);
let remote_blue_set = blue_blocks
.iter()
.map(|header| header.id())
.collect::<HashSet<_>>();
let calculated_blue_set = new_block_data
.mergeset_blues
.iter()
.skip(1)
.cloned()
.collect::<HashSet<_>>();
if calculated_blue_set != remote_blue_set {
let missing_blues: Vec<_> = remote_blue_set.difference(&calculated_blue_set).collect();
let extra_blues: Vec<_> = calculated_blue_set.difference(&remote_blue_set).collect();
bail!(
"Blue set mismatch for block {:?}:\nExpected blues: {:?}\nCalculated blues: {:?}\nMissing blues: {:?}\nExtra blues: {:?}",
header.id(),
remote_blue_set,
calculated_blue_set,
missing_blues,
extra_blues
);
}

}
}

Expand Down Expand Up @@ -377,12 +384,20 @@ impl<
*candidate_blue_anticone_size = (*candidate_blue_anticone_size).checked_add(1).unwrap();
if *candidate_blue_anticone_size > self.k {
// k-cluster violation: The candidate's blue anticone exceeded k
info!(
"Checking blue candidate: {} failed, blue anticone exceeded k",
blue_candidate
);
return Ok(ColoringState::Red);
}

if *candidate_blues_anticone_sizes.get(&block).unwrap() == self.k {
// k-cluster violation: A block in candidate's blue anticone already
// has k blue blocks in its own anticone
info!(
"Checking blue candidate: {} failed, block {} has k blue blocks in its anticone",
blue_candidate, block
);
return Ok(ColoringState::Red);
}

Expand Down Expand Up @@ -431,6 +446,10 @@ impl<
// The maximum length of new_block_data.mergeset_blues can be K+1 because
// it contains the selected parent.
if new_block_data.mergeset_blues.len() as KType == self.k.checked_add(1).unwrap() {
info!(
"Checking blue candidate: {} failed, mergeset blues size is K+1",
blue_candidate
);
return Ok(ColoringOutput::Red);
}

Expand Down
5 changes: 5 additions & 0 deletions flexidag/src/prune/pruning_point_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ impl<T: ReachabilityStoreReader + Clone> PruningPointManagerT<T> {
min_required_blue_score_for_next_pruning_point
);

debug!("previous_pruning_point: {:?}, previous_ghostdata: {:?}, next_ghostdata: {:?}, pruning_depth: {:?}, pruning_finality: {:?}",
previous_pruning_point, previous_ghostdata, next_ghostdata,
pruning_depth, pruning_finality,
);

let mut latest_pruning_ghost_data = previous_ghostdata.to_compact();
if min_required_blue_score_for_next_pruning_point + pruning_depth
<= next_ghostdata.blue_score
Expand Down
224 changes: 220 additions & 4 deletions flexidag/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ use starcoin_logger::prelude::debug;
use starcoin_types::{
block::{BlockHeader, BlockHeaderBuilder, BlockNumber},
blockhash::{BlockHashMap, HashKTypeMap, KType},
consensus_header::ConsensusHeader,
U256,
};

use std::{
collections::HashSet,
collections::{HashMap, HashSet},
ops::{Deref, DerefMut},
sync::Arc,
time::Instant,
Expand Down Expand Up @@ -737,6 +739,31 @@ fn add_and_print_with_ghostdata(
Ok(header)
}

fn add_and_print_with_difficulty(
number: BlockNumber,
parent: Hash,
parents: Vec<Hash>,
difficulty: U256,
) -> anyhow::Result<BlockHeader> {
let header_builder = BlockHeaderBuilder::random();
let header = header_builder
.with_parent_hash(parent)
.with_parents_hash(parents)
.with_number(number)
.with_difficulty(difficulty)
.build();
let start = Instant::now();
let duration = start.elapsed();
println!(
"commit header: {:?}, number: {:?}, duration: {:?}",
header.id(),
header.number(),
duration
);

Ok(header)
}
Comment on lines +784 to +807
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix timing measurement or remove unused timing code.

The function includes timing measurement code that doesn't measure any meaningful operation:

let start = Instant::now();
let duration = start.elapsed();

Since no significant operation occurs between start and elapsed(), this timing is not useful.

Apply this diff to fix:

 fn add_and_print_with_difficulty(
     number: BlockNumber,
     parent: Hash,
     parents: Vec<Hash>,
     difficulty: U256,
 ) -> anyhow::Result<BlockHeader> {
     let header_builder = BlockHeaderBuilder::random();
     let header = header_builder
         .with_parent_hash(parent)
         .with_parents_hash(parents)
         .with_number(number)
         .with_difficulty(difficulty)
         .build();
-    let start = Instant::now();
-    let duration = start.elapsed();
     println!(
         "commit header: {:?}, number: {:?}, duration: {:?}",
         header.id(),
         header.number(),
-        duration
+        Duration::from_secs(0)
     );
     Ok(header)
 }


fn add_and_print_with_pruning_point(
number: BlockNumber,
parent: Hash,
Expand All @@ -751,6 +778,7 @@ fn add_and_print_with_pruning_point(
.with_parents_hash(parents)
.with_number(number)
.with_pruning_point(pruning_point)
.with_difficulty(U256::from(10))
.build();
let start = Instant::now();
dag.commit(header.to_owned(), origin)?;
Expand All @@ -761,10 +789,11 @@ fn add_and_print_with_pruning_point(
header.number(),
duration
);
let _ghostdata = dag.ghostdata(&[header.id()])?;
// let ghostdata = dag.ghostdata(&[header.id()])?;
// let ghostdata = dag.ghostdata_by_hash(header.id())?.unwrap();
// println!(
// "add a header: {:?}, blue set: {:?}, red set: {:?}, blue anticone size: {:?}",
// header, ghostdata.mergeset_blues, ghostdata.mergeset_reds, ghostdata.blues_anticone_sizes
// "add a header: {:?}, selected_parent: {:?}, blue set: {:?}, red set: {:?}, blue anticone size: {:?}",
// header, ghostdata.selected_parent, ghostdata.mergeset_blues, ghostdata.mergeset_reds, ghostdata.blues_anticone_sizes
// );
Ok(header)
}
Expand Down Expand Up @@ -1069,6 +1098,193 @@ fn test_prune() -> anyhow::Result<()> {
anyhow::Result::Ok(())
}

#[test]
fn test_verification_blue_block_inconsistent() -> anyhow::Result<()> {
loop_to_blue()?;
anyhow::Result::Ok(())
}
Comment on lines +1143 to +1147
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance test coverage with assertions

The test function is a simple wrapper around loop_to_blue() without any assertions or verification of the test results. Consider adding specific test cases and assertions to verify the expected behavior.

Example improvement:

 #[test]
 fn test_verification_blue_block_inconsistent() -> anyhow::Result<()> {
-    loop_to_blue()?;
+    let (dag, last_block) = loop_to_blue()?;
+    
+    // Add assertions to verify the expected state
+    let ghost_data = dag.ghostdata(&[last_block.id()])?;
+    assert!(ghost_data.blue_score > 0, "Blue score should be positive");
+    assert!(!ghost_data.mergeset_blues.is_empty(), "Blue set should not be empty");
+    
     anyhow::Result::Ok(())
 }

Committable suggestion skipped: line range outside the PR's diff.


fn loop_to_blue() -> anyhow::Result<()> {
// initialzie the dag firstly
let k = 2;

let mut dag = BlockDAG::create_for_testing_with_parameters(k).unwrap();

let origin = BlockHeaderBuilder::random().with_number(0).build();
let genesis = BlockHeader::dag_genesis_random_with_parent(origin)?;

dag.init_with_genesis(genesis.clone()).unwrap();

let mut storage = HashMap::new();

let block1 =
add_and_print_with_difficulty(1, genesis.id(), vec![genesis.id()], U256::from(10))?;
storage.insert(block1.id(), block1.clone());
let ghost = dag.ghostdata(&block1.parents())?;
let verified_ghost = dag.verify_and_ghostdata(
&ghost
.mergeset_blues
.iter()
.skip(1)
.cloned()
.map(|x| storage.get(&x).unwrap().clone())
.collect::<Vec<_>>(),
&block1,
)?;
dag.commit_trusted_block(
block1.clone(),
genesis.parent_hash(),
Arc::new(verified_ghost),
)?;

let mut bottom = vec![];
let mut last = block1.clone();
for i in 0..500 {
let block2 =
add_and_print_with_difficulty(1 + i, last.id(), vec![last.id()], U256::from(10))?;
last = block2.clone();
storage.insert(block2.id(), block2.clone());
let ghost = dag.ghostdata(&block2.parents())?;
let verified_ghost = dag.verify_and_ghostdata(
&ghost
.mergeset_blues
.iter()
.skip(1)
.cloned()
.map(|x| storage.get(&x).unwrap().clone())
.collect::<Vec<_>>(),
&block2,
)?;
dag.commit_trusted_block(
block2.clone(),
genesis.parent_hash(),
Arc::new(verified_ghost),
)?;
bottom.push(block2);
}

let mut top = vec![];
let mut iter = bottom.iter().peekable();
while let Some(first) = iter.next() {
if let Some(second) = iter.next() {
let block = add_and_print_with_difficulty(
3,
first.id(),
vec![first.id(), second.id()],
U256::from(10),
)?;
storage.insert(block.id(), block.clone());
let ghost = dag.ghostdata(&block.parents())?;
let verified_ghost = dag.verify_and_ghostdata(
&ghost
.mergeset_blues
.iter()
.skip(1)
.cloned()
.map(|x| storage.get(&x).unwrap().clone())
.collect::<Vec<_>>(),
&block,
)?;
dag.commit_trusted_block(
block.clone(),
genesis.parent_hash(),
Arc::new(verified_ghost),
)?;

last = block.clone();
top.push(block);
} else {
let block = add_and_print_with_difficulty(
3,
first.id(),
vec![first.id(), last.id()],
U256::from(10),
)?;
storage.insert(block.id(), block.clone());
let ghost = dag.ghostdata(&block.parents())?;
let verified_ghost = dag.verify_and_ghostdata(
&ghost
.mergeset_blues
.iter()
.skip(1)
.cloned()
.map(|x| storage.get(&x).unwrap().clone())
.collect::<Vec<_>>(),
&block,
)?;
dag.commit_trusted_block(
block.clone(),
genesis.parent_hash(),
Arc::new(verified_ghost),
)?;

top.push(block);
if top.len() == 1 {
last = top[0].clone();
break;
} else {
bottom.clone_from(&top);
iter = bottom.iter().peekable();
top.clear();
}
}
}

let block1_1 = add_and_print_with_difficulty(
1,
genesis.id(),
vec![last.id(), block1.id()],
U256::from(99999999),
)?;
storage.insert(block1_1.id(), block1_1.clone());
let ghost = dag.ghostdata(&block1_1.parents())?;
let verified_ghost = dag.verify_and_ghostdata(
&ghost
.mergeset_blues
.iter()
.skip(1)
.cloned()
.map(|x| storage.get(&x).unwrap().clone())
.collect::<Vec<_>>(),
&block1_1,
)?;
dag.commit_trusted_block(
block1_1.clone(),
genesis.parent_hash(),
Arc::new(verified_ghost),
)?;

let block3 = add_and_print_with_difficulty(
3,
block1_1.id(),
vec![block1_1.id(), last.id()],
U256::from(10),
)?;

let ghostdata = dag.ghostdata(&block3.parents())?;
println!(
"add a header: {:?}, selected_parent: {:?}, blue set: {:?}, red set: {:?}, blue anticone size: {:?}",
block3, ghostdata.selected_parent, ghostdata.mergeset_blues, ghostdata.mergeset_reds, ghostdata.blues_anticone_sizes
);
let verified_ghostdata = dag.verify_and_ghostdata(
&ghostdata
.mergeset_blues
.iter()
.skip(1)
.map(|x| dag.storage.header_store.get_header(*x).unwrap())
.collect::<Vec<_>>(),
&block3,
)?;
println!(
"after verification: selected_parent: {:?}, blue set: {:?}, red set: {:?}, blue anticone size: {:?}",
verified_ghostdata.selected_parent, verified_ghostdata.mergeset_blues, verified_ghostdata.mergeset_reds, verified_ghostdata.blues_anticone_sizes
);

assert_eq!(ghostdata.mergeset_blues, verified_ghostdata.mergeset_blues);

anyhow::Ok(())
}

#[test]
fn test_verification_blue_block() -> anyhow::Result<()> {
// initialzie the dag firstly
Expand Down
2 changes: 1 addition & 1 deletion kube/manifest/starcoin-vega.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ spec:
- -c
args:
-
rm -rf /sc-data/vega/starcoin.ipc /sc-data/vega/starcoindb/db/starcoindb/LOCK /sc-data/vega/genesis_config.json;
rm -rf /sc-data/vega/sync /sc-data/vega/starcoin.ipc /sc-data/vega/starcoindb/db/starcoindb/LOCK /sc-data/vega/genesis_config.json;
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 sync/src/tasks/block_sync_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ where
if block_header.number() % ASYNC_BLOCK_COUNT == 0
|| block_header.number() >= self.target.target_id.number()
{
self.sync_dag_store.delete_all_dag_sync_block()?;
self.find_absent_ancestor(vec![block_header.clone()])
.await?;

Expand All @@ -474,7 +475,6 @@ where
block: block.clone(),
children: vec![],
})?;
self.sync_dag_store.save_block(block)?;
anyhow::Ok(ParallelSign::NeedMoreBlocks)
}
};
Expand Down
Loading