Skip to content

Commit

Permalink
add test log
Browse files Browse the repository at this point in the history
use inquirer is_dag_ancestor_of
  • Loading branch information
jackzhhuang committed Dec 16, 2024
1 parent 78ad69e commit c096925
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 24 deletions.
2 changes: 1 addition & 1 deletion chain/api/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub trait ChainReader {
uncles: &[BlockHeader],
header: &BlockHeader,
) -> Result<GhostdagData>;
fn is_dag_ancestor_of(&self, ancestor: HashValue, descendants: Vec<HashValue>) -> Result<bool>;
fn is_dag_ancestor_of(&self, ancestor: HashValue, descendant: HashValue) -> Result<bool>;
fn get_pruning_height(&self) -> BlockNumber;
fn get_pruning_config(&self) -> (u64, u64);
}
Expand Down
6 changes: 3 additions & 3 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1379,8 +1379,8 @@ impl ChainReader for BlockChain {
Ok(self.dag().verify_and_ghostdata(uncles, header)?)
}

fn is_dag_ancestor_of(&self, ancestor: HashValue, descendants: Vec<HashValue>) -> Result<bool> {
self.dag().check_ancestor_of(ancestor, descendants)
fn is_dag_ancestor_of(&self, ancestor: HashValue, descendant: HashValue) -> Result<bool> {
self.dag().check_ancestor_of(ancestor, descendant)
}

fn get_pruning_height(&self) -> BlockNumber {
Expand Down Expand Up @@ -1522,7 +1522,7 @@ impl BlockChain {

let mut new_tips = vec![];
for hash in tips {
if !dag.check_ancestor_of(hash, vec![new_tip_block.id()])? {
if !dag.check_ancestor_of(hash, new_tip_block.id())? {
new_tips.push(hash);
}
}
Expand Down
2 changes: 1 addition & 1 deletion chain/src/verifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ impl BasicDagVerifier {
parents_hash.iter().try_for_each(|parent_hash| {
verify_block!(
VerifyBlockField::Header,
current_chain.is_dag_ancestor_of(new_block_header.pruning_point(), vec![*parent_hash]).map_err(|e| {
current_chain.is_dag_ancestor_of(new_block_header.pruning_point(), *parent_hash).map_err(|e| {
ConnectBlockError::VerifyBlockFailed(
VerifyBlockField::Header,
anyhow::anyhow!(
Expand Down
34 changes: 30 additions & 4 deletions flexidag/src/blockdag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,15 @@ impl BlockDAG {
Ok(self.storage.header_store.has(hash)?)
}

pub fn check_ancestor_of(&self, ancestor: Hash, descendant: Vec<Hash>) -> anyhow::Result<bool> {
self.ghostdag_manager
.check_ancestor_of(ancestor, descendant)
pub fn check_ancestor_of(&self, ancestor: Hash, descendant: Hash) -> anyhow::Result<bool> {
// self.ghostdag_manager
// .check_ancestor_of(ancestor, descendant)
inquirer::is_dag_ancestor_of(
&*self.storage.reachability_store.read(),
ancestor,
descendant,
)
.map_err(|e| e.into())
}

pub fn init_with_genesis(&mut self, genesis: BlockHeader) -> anyhow::Result<HashValue> {
Expand Down Expand Up @@ -211,6 +217,8 @@ impl BlockDAG {
}
};

info!("jacktest: start to commit via batch");

// Create a DB batch writer
let mut batch = WriteBatch::default();

Expand All @@ -223,13 +231,18 @@ impl BlockDAG {
self.storage.reachability_store.upgradable_read(),
);

info!("jacktest: start to add block into the ghostdata store");
// Store ghostdata
process_key_already_error(self.storage.ghost_dag_store.insert_batch(
&mut batch,
header.id(),
ghostdata.clone(),
))
.expect("failed to ghostdata in batch");
info!(
"jacktest: finish adding block into the ghostdata store, data: {:?}",
ghostdata
);

// Update reachability store
debug!(
Expand All @@ -238,11 +251,16 @@ impl BlockDAG {
header.number()
);

info!("jacktest: start to prepare mergeset into the reachability store");
let mut merge_set = ghostdata
.unordered_mergeset_without_selected_parent()
.filter(|hash| self.storage.reachability_store.read().has(*hash).unwrap())
.collect::<Vec<_>>()
.into_iter();
info!(
"jacktest: finish preparing mergeset into the reachability store, mergeset: {:?}",
merge_set
);
match inquirer::add_block(
&mut stage,
header.id(),
Expand All @@ -263,6 +281,7 @@ impl BlockDAG {
}
},
}
info!("jacktest: finish adding block into reachability store and start to insert relations into the relations store, data: {:?}", parents);

process_key_already_error(self.storage.relations_store.write().insert_batch(
&mut batch,
Expand All @@ -271,6 +290,8 @@ impl BlockDAG {
))
.expect("failed to insert relations in batch");

info!("jacktest: finish inserting relations into the relations store, start to insert header into the header store");

// Store header store
process_key_already_error(self.storage.header_store.insert(
header.id(),
Expand All @@ -279,17 +300,22 @@ impl BlockDAG {
))
.expect("failed to insert header in batch");

info!("jacktest: finish inserting header into the header store, start to commit the stage");

// the read lock will be updated to the write lock
// and then write the batch
// and then release the lock
stage
.commit(&mut batch)
.expect("failed to write the stage reachability in batch");

info!("jacktest: finish committing the stage, start to write the batch");

// write the data just one time
self.storage
.write_batch(batch)
.expect("failed to write dag data in batch");
info!("jacktest: finish writing the batch");
Ok(())
}

Expand Down Expand Up @@ -621,7 +647,7 @@ impl BlockDAG {
let de = descendants
.into_iter()
.filter(|descendant| {
self.check_ancestor_of(ancestor, vec![*descendant])
self.check_ancestor_of(ancestor, *descendant)
.unwrap_or(false)
})
.collect::<Vec<_>>();
Expand Down
46 changes: 32 additions & 14 deletions flexidag/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,48 +467,66 @@ fn test_reachability_check_ancestor() -> anyhow::Result<()> {
// origin.....target_parent-target.....parent-child
// ancestor
assert!(
dag.check_ancestor_of(target, vec![parent, child])?,
dag.check_ancestor_of(target, parent)?,
"failed to check target is the ancestor of its descendant"
);
assert!(
dag.check_ancestor_of(origin, vec![target, parent, child])?,
dag.check_ancestor_of(target, child)?,
"failed to check target is the ancestor of its descendant"
);
assert!(
dag.check_ancestor_of(origin, target)?,
"failed to check origin is the parent of its child"
);
assert!(
dag.check_ancestor_of(origin, parent)?,
"failed to check origin is the parent of its child"
);
assert!(
dag.check_ancestor_of(parent, vec![child])?,
dag.check_ancestor_of(origin, child)?,
"failed to check origin is the parent of its child"
);
assert!(
dag.check_ancestor_of(parent, child)?,
"failed to check target, parent is the parent of its child"
);
assert!(
dag.check_ancestor_of(target_parent, vec![target])?,
dag.check_ancestor_of(target_parent, target)?,
"failed to check target parent, parent is the parent of its child"
);

// not ancestor
assert!(
!dag.check_ancestor_of(child, vec![target])?,
!dag.check_ancestor_of(child, target)?,
"failed to check child is not the ancestor of its descendant"
);
assert!(
!dag.check_ancestor_of(parent, vec![target])?,
!dag.check_ancestor_of(parent, target)?,
"failed to check child is not the ancestor of its descendant"
);
assert!(
!dag.check_ancestor_of(child, vec![parent])?,
!dag.check_ancestor_of(child, parent)?,
"failed to check target, child is the child of its parent"
);
assert!(
!dag.check_ancestor_of(target, vec![target_parent])?,
!dag.check_ancestor_of(target, target_parent)?,
"failed to check target is the child of its parent"
);

assert!(
dag.check_ancestor_of(target, vec![Hash::random(), Hash::random(),])
.is_err(),
dag.check_ancestor_of(target, Hash::random()).is_err(),
"failed to check not the ancestor of descendants"
);
assert!(
dag.check_ancestor_of(Hash::random(), vec![target, parent, child])
.is_err(),
dag.check_ancestor_of(Hash::random(), target).is_err(),
"failed to check not the descendant of parents"
);
assert!(
dag.check_ancestor_of(Hash::random(), parent).is_err(),
"failed to check not the descendant of parents"
);
assert!(
dag.check_ancestor_of(Hash::random(), child).is_err(),
"failed to check not the descendant of parents"
);

Expand Down Expand Up @@ -589,7 +607,7 @@ fn test_reachability_not_ancestor() -> anyhow::Result<()> {
hashes.push(child3);
print_reachability_data(reachability_store.read().deref(), &hashes);

let result = dag.check_ancestor_of(child1, vec![child3]);
let result = dag.check_ancestor_of(child1, child3);
println!("dag.check_ancestor_of() result = {:?}", result);

Ok(())
Expand Down Expand Up @@ -692,7 +710,7 @@ fn test_reachability_algorithm() -> anyhow::Result<()> {
print_reachability_data(reachability_store.read().deref(), &hashes);

assert!(
dag.check_ancestor_of(origin, vec![child5])?,
dag.check_ancestor_of(origin, child5)?,
"child 5 must be origin's child"
);

Expand Down
2 changes: 1 addition & 1 deletion sync/src/block_connector/write_block_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ where
if descendant == start {
continue;
}
if self.main.dag().check_ancestor_of(descendant, vec![start])? {
if self.main.dag().check_ancestor_of(descendant, start)? {
continue;
}

Expand Down

0 comments on commit c096925

Please sign in to comment.