Skip to content

Commit 997802d

Browse files
authored
Merge pull request #40 from code-monad/fix-shortcut-merkle-path-zero-forkheight-
fix: shortcut into merkle_path insertion should be MergeValue::Value
2 parents 7c70731 + b7f2d5d commit 997802d

File tree

3 files changed

+132
-5
lines changed

3 files changed

+132
-5
lines changed

src/merge.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ impl MergeValue {
8080
#[cfg(feature = "trie")]
8181
pub fn into_merge_value<H: Hasher + Default>(key: H256, value: H256, height: u8) -> MergeValue {
8282
// try keep hash same with MergeWithZero
83-
if value.is_zero() {
84-
MergeValue::from_h256(H256::zero())
83+
if value.is_zero() || height == 0 {
84+
MergeValue::from_h256(value)
8585
} else {
8686
let base_key = key.parent_path(0);
8787
let base_node = hash_base_node::<H>(0, &base_key, &value);

src/tests/tree.rs

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,136 @@ fn test_trie_broken_sample_02() {
794794
assert_eq!(smt.root(), &root1);
795795
}
796796

797+
#[test]
798+
fn test_trie_broken_sample_03() {
799+
let mut smt = SMT::default();
800+
smt.update(
801+
[
802+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
803+
0, 0, 0,
804+
]
805+
.into(),
806+
[
807+
231, 17, 197, 236, 8, 0, 141, 194, 15, 253, 234, 189, 224, 53, 255, 173, 117, 8, 221,
808+
5, 34, 5, 198, 250, 99, 32, 229, 13, 222, 40, 203, 90,
809+
]
810+
.into(),
811+
)
812+
.unwrap();
813+
smt.update(
814+
[
815+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
816+
0, 0, 0,
817+
]
818+
.into(),
819+
[
820+
231, 17, 197, 236, 8, 0, 141, 194, 15, 253, 234, 189, 224, 53, 255, 173, 117, 8, 221,
821+
5, 34, 5, 198, 250, 99, 32, 229, 13, 222, 40, 203, 90,
822+
]
823+
.into(),
824+
)
825+
.unwrap();
826+
smt.update(
827+
[
828+
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
829+
0, 0, 0,
830+
]
831+
.into(),
832+
[
833+
105, 112, 48, 175, 83, 217, 158, 108, 243, 136, 9, 21, 192, 91, 211, 190, 218, 240, 89,
834+
241, 63, 137, 128, 133, 65, 169, 51, 33, 49, 123, 118, 132,
835+
]
836+
.into(),
837+
)
838+
.unwrap();
839+
smt.update(
840+
[
841+
2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
842+
0, 0, 0,
843+
]
844+
.into(),
845+
[
846+
189, 150, 22, 8, 143, 248, 180, 169, 68, 195, 31, 28, 34, 180, 182, 223, 195, 37, 117,
847+
197, 229, 144, 229, 64, 230, 250, 21, 205, 225, 32, 135, 195,
848+
]
849+
.into(),
850+
)
851+
.unwrap();
852+
smt.update(
853+
[
854+
3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
855+
0, 0, 0,
856+
]
857+
.into(),
858+
[
859+
153, 75, 31, 235, 146, 228, 224, 228, 237, 250, 34, 227, 139, 8, 213, 118, 25, 114, 82,
860+
242, 215, 172, 184, 100, 205, 85, 47, 116, 140, 238, 175, 190,
861+
]
862+
.into(),
863+
)
864+
.unwrap();
865+
smt.update(
866+
[
867+
4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
868+
0, 0, 0,
869+
]
870+
.into(),
871+
[
872+
242, 174, 6, 108, 205, 74, 137, 57, 15, 248, 35, 35, 255, 58, 93, 74, 183, 47, 194, 40,
873+
134, 3, 215, 100, 80, 51, 28, 251, 96, 19, 201, 170,
874+
]
875+
.into(),
876+
)
877+
.unwrap();
878+
smt.update(
879+
[
880+
5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
881+
0, 0, 0,
882+
]
883+
.into(),
884+
[
885+
88, 83, 226, 107, 201, 255, 207, 189, 197, 145, 113, 95, 209, 238, 110, 9, 82, 215,
886+
232, 183, 203, 220, 194, 167, 21, 189, 239, 238, 178, 149, 153, 44,
887+
]
888+
.into(),
889+
)
890+
.unwrap();
891+
smt.update(
892+
[
893+
6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
894+
0, 0, 0,
895+
]
896+
.into(),
897+
[
898+
80, 177, 52, 81, 182, 121, 67, 120, 77, 19, 201, 42, 75, 136, 19, 238, 112, 23, 204,
899+
103, 20, 157, 53, 235, 80, 70, 126, 79, 9, 35, 11, 158,
900+
]
901+
.into(),
902+
)
903+
.unwrap();
904+
let key7 = H256::from([
905+
7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
906+
0, 0,
907+
]);
908+
let v7 = H256::from([
909+
103, 245, 93, 107, 47, 213, 28, 173, 216, 92, 109, 17, 151, 57, 101, 4, 44, 145, 116, 215,
910+
185, 218, 144, 244, 131, 160, 148, 58, 247, 226, 240, 55,
911+
]);
912+
let proof = smt
913+
.merkle_proof(vec![key7])
914+
.unwrap()
915+
.compile(vec![key7])
916+
.unwrap();
917+
// Compute root with different value than actually in smt.
918+
let root = proof
919+
.compute_root::<Blake2bHasher>(vec![(key7, v7)])
920+
.unwrap();
921+
// Compute root by updating smt.
922+
smt.update(key7, v7).unwrap();
923+
// Expect them to be the same.
924+
assert_eq!(*smt.root(), root);
925+
}
926+
797927
#[test]
798928
fn test_replay_to_pass_proof() {
799929
let key1: H256 = [

src/trie_tree.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,6 @@ impl<H: Hasher + Default, V: Value, S: StoreReadOps<V>> SparseMerkleTree<H, V, S
373373
fork_height,
374374
))
375375
}
376-
if fork_height == 1 && leaves_bitmap[leaf_index].get_bit(0) {
377-
proof_result.push(MergeValue::from_h256(value));
378-
}
379376
}
380377
break;
381378
}

0 commit comments

Comments
 (0)