Skip to content

Commit

Permalink
use [0u8; 32] as hash and vec![] as data for empty leaf
Browse files Browse the repository at this point in the history
  • Loading branch information
John Petterson committed Jan 15, 2024
1 parent c132477 commit 7bd1fac
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
29 changes: 15 additions & 14 deletions src/kvpair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,12 @@ impl Hash {
result.into()
}

pub const fn empty() -> Self {
Self([0u8; 32])
}

/// depth start from 0 up to Self::height(). Example 20 height MongoMerkle, root depth=0, leaf depth=20
pub fn get_default_hash(depth: usize) -> Result<Hash, MerkleError> {
pub fn get_default_hash_for_depth(depth: usize) -> Result<Hash, MerkleError> {
if depth <= MERKLE_TREE_HEIGHT {
Ok(DEFAULT_HASH_VEC[MERKLE_TREE_HEIGHT - depth])
} else {
Expand Down Expand Up @@ -515,11 +519,11 @@ impl MerkleRecord {

pub fn get_default_record(index: u64) -> Result<Self, MerkleError> {
let height = (index + 1).ilog2() as usize;
let default = Hash::get_default_hash(height)?;
let default = Hash::get_default_hash_for_depth(height)?;
let child_hash = if height == MERKLE_TREE_HEIGHT {
[0; 32].try_into().unwrap()
} else {
Hash::get_default_hash(height + 1)?
Hash::get_default_hash_for_depth(height + 1)?
};
Ok(MerkleRecord {
index,
Expand All @@ -539,21 +543,18 @@ pub struct DataHashRecord {
pub data: Vec<u8>,
}

impl Default for DataHashRecord {
fn default() -> Self {
let data = LeafData::default();
let hash = Hash::hash_data(&(data.0));
Self {
hash,
data: data.into(),
}
}
}

impl DataHashRecord {
pub fn new(hash: Hash, data: Vec<u8>) -> Self {
Self { hash, data }
}

pub const fn empty() -> Self {
Self {
// Note that we use the hash of [0u8; 32] as default hash, while empty vector to represent empty data
hash: Hash::empty(),
data: vec![],
}
}
}

impl MongoMerkle {
Expand Down
15 changes: 11 additions & 4 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ impl MongoCollection<MerkleRecord, DataHashRecord> {
index: u64,
hash: &Hash,
) -> Result<Option<MerkleRecord>, Error> {
dbg!(index, hash);
let mut filter = doc! {};
filter.insert("index", u64_to_bson(index));
filter.insert("hash", hash_to_bson(hash));
Expand All @@ -204,6 +205,7 @@ impl MongoCollection<MerkleRecord, DataHashRecord> {
return Ok(record);
}
let default_record = MerkleRecord::get_default_record(index)?;
dbg!(&default_record, hash);
if default_record.hash == *hash {
Ok(Some(default_record))
} else {
Expand Down Expand Up @@ -416,8 +418,8 @@ impl MongoCollection<MerkleRecord, DataHashRecord> {
hash: &Hash,
) -> Result<Option<DataHashRecord>, Error> {
dbg!(hash);
if *hash == DataHashRecord::default().hash {
return Ok(Some(DataHashRecord::default()));
if *hash == Hash::empty() {
return Ok(Some(DataHashRecord::empty()));
}
let mut filter = doc! {};
filter.insert("hash", hash_to_bson(hash));
Expand Down Expand Up @@ -591,7 +593,7 @@ impl KvPair for MongoKvPair {
let mut collection = self.new_collection(&contract_id, false).await?;
let index = request.index;
let proof_v0 = ProofType::ProofV0 as i32;
let (record, proof) = match (request.hash.as_ref(), request.proof_type) {
let (mut record, proof) = match (request.hash.as_ref(), request.proof_type) {
// Get merkle records in a faster way
(Some(hash), _) if request.proof_type != proof_v0 => {
let hash: Hash = hash.as_slice().try_into()?;
Expand All @@ -616,11 +618,16 @@ impl KvPair for MongoKvPair {
} else {
None
};
dbg!(&record, &proof_bytes);
(record, proof_bytes)
}
};
dbg!(&record, &proof);
// We now use [0u8; 32] to represent empty node hash, since
if record.hash == Hash::get_default_hash_for_depth(MERKLE_TREE_HEIGHT).unwrap() {
record.hash = [0u8; 32].try_into().unwrap();
}
let datahash_record = collection.get_datahash_record(&record.hash()).await?;
dbg!(&record, &proof, &datahash_record);
let node = match datahash_record {
Some(datahash_record) => (record, datahash_record).try_into()?,
// If the datahash record corresponding to this hash does not exists,
Expand Down
5 changes: 1 addition & 4 deletions tests/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,7 @@ async fn test_get_leaf() {
assert_eq!(node.node_type, NodeType::NodeLeaf as i32);
match node.node_data {
Some(NodeData::Data(data)) => {
assert_eq!(
LeafData::try_from(data.as_slice()).unwrap(),
LeafData::default()
)
assert_eq!(data, Vec::<u8>::new())
}
_ => panic!("Invalid node data"),
}
Expand Down

0 comments on commit 7bd1fac

Please sign in to comment.