-
Notifications
You must be signed in to change notification settings - Fork 38
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
Implement OneshotMerkleTree #43
Implement OneshotMerkleTree #43
Conversation
common/src/lib.rs
Outdated
Ok(()) | ||
} else { | ||
Err(MerkleProofError::UnmatchedRoot( | ||
std::str::from_utf8(&root.hash).unwrap().to_string(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hash is not utf8-encoded. use hex::encode()
instead to create a readable string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Applied.
merkle-tree/src/oneshot.rs
Outdated
first_hash | ||
}; | ||
|
||
Hash256::hash([first_hash, second_hash].concat()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add Hash256::aggregate(&self, other: &Self) -> Self
and use it here and in MerkleProof
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Applied.
merkle-tree/src/oneshot.rs
Outdated
pub fn create_merkle_proof(&self, mut key: Hash256) -> MerkleProof { | ||
let mut merkle_proof: MerkleProof = MerkleProof { proof: Vec::new() }; | ||
|
||
// Self::merkle_proof(&self.hash_list, key, proof) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this comment for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary comment. It was a code supposed to be deleted due to refactoring. Deleted.
merkle-tree/src/oneshot.rs
Outdated
merkle_tree | ||
} | ||
|
||
fn hash_pair(pair: &[Hash256]) -> Hash256 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this function do? Why does this takes slice while its name is 'pair'?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added comments explaining what the function does. Please check.
bf62723
to
cfd9903
Compare
merkle-tree/src/oneshot.rs
Outdated
mod test { | ||
use simperby_common::{crypto::Hash256, MerkleProof}; | ||
|
||
use super::OneshotMerkleTree; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the new line between use
s.
Also use use super::*
instead, for a test module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
merkle-tree/src/oneshot.rs
Outdated
/// 5 6 | ||
/// 1 2 3 4 | ||
/// ``` | ||
/// which is represented as [[1, 2, 3, 4], [5, 6], [7]]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you update the example with [1,2,3
]? (general case)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Applied
common/src/lib.rs
Outdated
} else { | ||
calculated_root = Hash256::aggregate(&calculated_root, hash); | ||
} | ||
println!("{:?}", hex::encode(calculated_root.hash)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
common/src/lib.rs
Outdated
@@ -102,7 +102,7 @@ impl BlockHeader { | |||
|
|||
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] | |||
pub struct MerkleProof { | |||
// TODO | |||
pub proof: Vec<(Hash256, u8)>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- use bool
- put doc-comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
65d8e82
to
0354eba
Compare
d1095b0
to
a63893c
Compare
common/src/lib.rs
Outdated
pub enum MerkleTree { | ||
// (pair_hash, is_right_node) | ||
HasSibling(Hash256, bool), | ||
HasNoSibling, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for my miscommunication, but what I was think of is
HasRightSibling
and HasLeftSibling
. This will improve the overall readability .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understood. Fixed.
merkle-tree/src/oneshot.rs
Outdated
unimplemented!() | ||
/// | ||
/// Given a tree [[1, 2, 3], [4, 5], [6]], | ||
/// Merkle proof for 2 is [(1, false), (5, true)] and Merkle proof for 3 is [(OnlyChildNode, true), (4, false)]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update the description (no boolean)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
9733768
to
676930d
Compare
common/src/lib.rs
Outdated
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] | ||
pub enum MerkleTree { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it supposed be MerkleProofEntry
?
MerkleTree
is more like Vec<Vec<Hash256>>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
common/src/lib.rs
Outdated
pub fn verify(&self, root: Hash256, data: &[u8]) -> Result<(), MerkleProofError> { | ||
let mut calculated_root: Hash256 = Hash256::hash(data); | ||
for node in &self.proof { | ||
calculated_root = if let MerkleTree::LeftChild(pair_hash) = node { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using match
will look better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
676930d
to
ffcc6e6
Compare
No description provided.