-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Upgrade code to rust 2018 edtion to use async await * Create test case to ensure struct is send * Replace all references from Rc to Arc
- Loading branch information
Showing
6 changed files
with
82 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ documentation = "https://docs.rs/merkle-tree-stream" | |
authors = ["Yoshua Wuyts <[email protected]>"] | ||
license = "MIT OR Apache-2.0" | ||
readme = "README.md" | ||
edition = "2018" | ||
|
||
[dependencies] | ||
flat-tree = "4.0.1" | ||
|
@@ -15,3 +16,4 @@ flat-tree = "4.0.1" | |
hex = "0.4.0" | ||
quickcheck = "0.9.0" | ||
crypto-hash = "0.3.1" | ||
async-std = "1.5.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use async_std::task; | ||
use merkle_tree_stream::{ | ||
DefaultNode, HashMethods, MerkleTreeStream, Node, NodeKind, PartialNode, | ||
}; | ||
use std::sync::Arc; | ||
use std::vec::Vec; | ||
|
||
struct XorHashMethods; | ||
impl HashMethods for XorHashMethods { | ||
type Node = DefaultNode; | ||
type Hash = Vec<u8>; | ||
|
||
fn leaf(&self, leaf: &PartialNode, _roots: &[Arc<Self::Node>]) -> Self::Hash { | ||
// bitwise XOR the data into u8 | ||
let hash = match leaf.data() { | ||
NodeKind::Parent => 0, | ||
NodeKind::Leaf(data) => data.iter().fold(0, |acc, x| acc ^ x), | ||
}; | ||
vec![hash] | ||
} | ||
|
||
fn parent(&self, a: &Self::Node, b: &Self::Node) -> Self::Hash { | ||
let hash = Node::hash(a) | ||
.iter() | ||
.chain(Node::hash(b).iter()) | ||
.fold(0, |acc, x| acc ^ x); | ||
vec![hash] | ||
} | ||
} | ||
|
||
async fn append<H: HashMethods>( | ||
mts: &mut MerkleTreeStream<H>, | ||
content: &[u8], | ||
nodes: &mut Vec<Arc<H::Node>>, | ||
) { | ||
mts.next(content, nodes); | ||
} | ||
|
||
fn main() { | ||
task::block_on(task::spawn(async { | ||
let mut mts = MerkleTreeStream::new(XorHashMethods, Vec::new()); | ||
let mut nodes = Vec::new(); | ||
append(&mut mts, b"hello", &mut nodes).await; | ||
append(&mut mts, b"hashed", &mut nodes).await; | ||
mts.next(b"world", &mut nodes); | ||
|
||
println!("{:?}", nodes); | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters