Skip to content

Commit

Permalink
[jellyfish_tree] update test for jellyfish tree (#1230)
Browse files Browse the repository at this point in the history
* [jellyfish_tree] update test for jellyfish tree

* [jellyfish_tree] fix clippy check
  • Loading branch information
ssyuan authored Sep 4, 2020
1 parent 38ed0e1 commit a36ee7f
Show file tree
Hide file tree
Showing 7 changed files with 614 additions and 572 deletions.
4 changes: 4 additions & 0 deletions core/forkable-jellyfish-merkle/src/blob.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0

#[cfg(any(test, feature = "fuzzing"))]
use proptest_derive::Arbitrary;
use serde::{Deserialize, Serialize};
use starcoin_crypto::hash::*;
use std::fmt;

#[derive(Clone, Eq, PartialEq, Serialize, Deserialize, CryptoHasher, CryptoHash)]
#[cfg_attr(any(test, feature = "fuzzing"), derive(Arbitrary))]
pub struct Blob {
blob: Vec<u8>,
}
Expand Down
27 changes: 19 additions & 8 deletions core/forkable-jellyfish-merkle/src/iterator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use crate::{
};
use anyhow::{format_err, Result};
use starcoin_crypto::HashValue;
use std::sync::Arc;

type Version = HashValue;
/// `NodeVisitInfo` keeps track of the status of an internal node during the iteration process. It
Expand Down Expand Up @@ -95,9 +94,9 @@ impl NodeVisitInfo {
}

/// The `JellyfishMerkleIterator` implementation.
pub struct JellyfishMerkleIterator<R: TreeReader> {
pub struct JellyfishMerkleIterator<'a, R: 'a + TreeReader> {
/// The storage engine from which we can read nodes using node keys.
reader: Arc<R>,
reader: &'a R,

/// The version of the tree this iterator is running on.
state_root_hash: Version,
Expand All @@ -111,14 +110,14 @@ pub struct JellyfishMerkleIterator<R: TreeReader> {
done: bool,
}

impl<R> JellyfishMerkleIterator<R>
impl<'a, R> JellyfishMerkleIterator<'a, R>
where
R: TreeReader,
R: 'a + TreeReader,
{
/// Constructs a new iterator. This puts the internal state in the correct position, so the
/// following `next` call will yield the smallest key that is greater or equal to
/// `starting_key`.
pub fn new(reader: Arc<R>, state_root_hash: Version, starting_key: HashValue) -> Result<Self> {
pub fn new(reader: &'a R, state_root_hash: Version, starting_key: HashValue) -> Result<Self> {
let mut parent_stack = vec![];
let mut done = false;

Expand Down Expand Up @@ -195,11 +194,23 @@ where
}
}
}

#[cfg(test)]
pub fn print(&self) -> Result<()> {
let nodes = &self.parent_stack;
for node in nodes {
println!("internal node key: {:?}", node.node_key.short_str());
if let Ok(Node::Internal(internal)) = self.reader.get_node(&node.node_key) {
println!("child: {:?}", internal.all_child());
}
}
Ok(())
}
}

impl<R> Iterator for JellyfishMerkleIterator<R>
impl<'a, R> Iterator for JellyfishMerkleIterator<'a, R>
where
R: TreeReader,
R: 'a + TreeReader,
{
type Item = Result<(HashValue, Blob)>;

Expand Down
Loading

0 comments on commit a36ee7f

Please sign in to comment.