Remove a wrong double check when we fill the mpt with valid proof data. (#117)

This commit is contained in:
peilun-conflux 2024-07-11 14:07:20 +08:00 committed by GitHub
parent 3e23e81ae6
commit 0a69d0f56c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -7,7 +7,7 @@ use std::cmp::Ordering;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt::Debug; use std::fmt::Debug;
use std::marker::PhantomData; use std::marker::PhantomData;
use tracing::warn; use tracing::{trace, warn};
pub use crate::merkle_tree::{ pub use crate::merkle_tree::{
Algorithm, HashElement, MerkleTreeInitialData, MerkleTreeRead, ZERO_HASHES, Algorithm, HashElement, MerkleTreeInitialData, MerkleTreeRead, ZERO_HASHES,
@ -275,7 +275,8 @@ impl<E: HashElement, A: Algorithm<E>> AppendMerkleTree<E, A> {
layer[position] = data.clone(); layer[position] = data.clone();
updated_nodes.push((i, position, data)) updated_nodes.push((i, position, data))
} else if layer[position] != data { } else if layer[position] != data {
bail!( // The last node in each layer may have changed in the tree.
trace!(
"conflict data layer={} position={} tree_data={:?} proof_data={:?}", "conflict data layer={} position={} tree_data={:?} proof_data={:?}",
i, i,
position, position,
@ -724,6 +725,35 @@ mod tests {
} }
} }
#[test]
fn test_proof_against_modified_merkle() {
let n = [1, 2, 6, 1025];
for entry_len in n {
let mut data = Vec::new();
for _ in 0..entry_len {
data.push(H256::random());
}
let mut merkle =
AppendMerkleTree::<H256, Sha3Algorithm>::new(vec![H256::zero()], 0, None);
merkle.append_list(data.clone());
merkle.commit(Some(0));
for i in (0..data.len()).step_by(6) {
let end = std::cmp::min(i + 3, data.len());
let range_proof = merkle.gen_range_proof(i + 1, end + 1).unwrap();
let mut new_data = Vec::new();
for _ in 0..3 {
new_data.push(H256::random());
}
merkle.append_list(new_data);
merkle.commit(Some(i as u64 / 6 + 1));
let r = range_proof.validate::<Sha3Algorithm>(&data[i..end], i + 1);
assert!(r.is_ok(), "{:?}", r);
merkle.fill_with_range_proof(range_proof).unwrap();
}
}
}
fn verify(data: &[H256], merkle: &mut AppendMerkleTree<H256, Sha3Algorithm>) { fn verify(data: &[H256], merkle: &mut AppendMerkleTree<H256, Sha3Algorithm>) {
for (i, item) in data.iter().enumerate() { for (i, item) in data.iter().enumerate() {
let proof = merkle.gen_proof(i + 1).unwrap(); let proof = merkle.gen_proof(i + 1).unwrap();