From 89607fea2fe242068f0aba0b39ab9354fb6d3570 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sun, 7 Jul 2024 02:11:52 +0700 Subject: [PATCH] more work --- grovedb/src/element/mod.rs | 6 +- grovedb/src/element/query.rs | 6 +- grovedb/src/operations/proof/generate.rs | 6 +- grovedb/src/operations/proof/util.rs | 6 +- grovedb/src/query/mod.rs | 6 +- grovedb/src/query_result_type.rs | 6 +- merk/src/proofs/mod.rs | 6 +- merk/src/proofs/query/mod.rs | 448 +++++++++++++---------- merk/src/proofs/query/query_item/mod.rs | 6 +- merk/src/proofs/query/verify.rs | 9 +- 10 files changed, 294 insertions(+), 211 deletions(-) diff --git a/grovedb/src/element/mod.rs b/grovedb/src/element/mod.rs index a88429d8..27da770c 100644 --- a/grovedb/src/element/mod.rs +++ b/grovedb/src/element/mod.rs @@ -175,7 +175,11 @@ impl fmt::Display for Element { } fn hex_to_ascii(hex_value: &[u8]) -> String { - String::from_utf8(hex_value.to_vec()).unwrap_or_else(|_| hex::encode(hex_value)) + if hex_value.len() == 1 && hex_value[0] < b"0"[0] { + hex::encode(&hex_value) + } else { + String::from_utf8(hex_value.to_vec()).unwrap_or_else(|_| hex::encode(&hex_value)) + } } impl Element { diff --git a/grovedb/src/element/query.rs b/grovedb/src/element/query.rs index e1f0409d..eba7ea9a 100644 --- a/grovedb/src/element/query.rs +++ b/grovedb/src/element/query.rs @@ -247,7 +247,11 @@ where } fn hex_to_ascii(hex_value: &[u8]) -> String { - String::from_utf8(hex_value.to_vec()).unwrap_or_else(|_| hex::encode(hex_value)) + if hex_value.len() == 1 && hex_value[0] < b"0"[0] { + hex::encode(&hex_value) + } else { + String::from_utf8(hex_value.to_vec()).unwrap_or_else(|_| hex::encode(&hex_value)) + } } impl Element { diff --git a/grovedb/src/operations/proof/generate.rs b/grovedb/src/operations/proof/generate.rs index db491d1f..3f6d320e 100644 --- a/grovedb/src/operations/proof/generate.rs +++ b/grovedb/src/operations/proof/generate.rs @@ -172,7 +172,11 @@ fn element_hex_to_ascii(hex_value: &[u8]) -> String { } fn hex_to_ascii(hex_value: &[u8]) -> String { - String::from_utf8(hex_value.to_vec()).unwrap_or_else(|_| hex::encode(hex_value)) + if hex_value.len() == 1 && hex_value[0] < b"0"[0] { + hex::encode(&hex_value) + } else { + String::from_utf8(hex_value.to_vec()).unwrap_or_else(|_| hex::encode(&hex_value)) + } } impl GroveDb { diff --git a/grovedb/src/operations/proof/util.rs b/grovedb/src/operations/proof/util.rs index 9d86eb28..fa751653 100644 --- a/grovedb/src/operations/proof/util.rs +++ b/grovedb/src/operations/proof/util.rs @@ -152,7 +152,11 @@ fn element_hex_to_ascii(hex_value: &[u8]) -> String { } fn hex_to_ascii(hex_value: &[u8]) -> String { - String::from_utf8(hex_value.to_vec()).unwrap_or_else(|_| hex::encode(hex_value)) + if hex_value.len() == 1 && hex_value[0] < b"0"[0] { + hex::encode(&hex_value) + } else { + String::from_utf8(hex_value.to_vec()).unwrap_or_else(|_| hex::encode(&hex_value)) + } } impl ProvedPathKeyValue { diff --git a/grovedb/src/query/mod.rs b/grovedb/src/query/mod.rs index ab7c06ea..41438b44 100644 --- a/grovedb/src/query/mod.rs +++ b/grovedb/src/query/mod.rs @@ -49,7 +49,11 @@ impl fmt::Display for PathQuery { } fn hex_to_ascii(hex_value: &[u8]) -> String { - String::from_utf8(hex_value.to_vec()).unwrap_or_else(|_| hex::encode(hex_value)) + if hex_value.len() == 1 && hex_value[0] < b"0"[0] { + hex::encode(&hex_value) + } else { + String::from_utf8(hex_value.to_vec()).unwrap_or_else(|_| hex::encode(&hex_value)) + } } #[cfg(any(feature = "full", feature = "verify"))] diff --git a/grovedb/src/query_result_type.rs b/grovedb/src/query_result_type.rs index e046d5b3..41bf47f8 100644 --- a/grovedb/src/query_result_type.rs +++ b/grovedb/src/query_result_type.rs @@ -110,7 +110,11 @@ impl BTreeMapLevelResult { } fn hex_to_ascii(hex_value: &[u8]) -> String { - String::from_utf8(hex_value.to_vec()).unwrap_or_else(|_| hex::encode(hex_value)) + if hex_value.len() == 1 && hex_value[0] < b"0"[0] { + hex::encode(&hex_value) + } else { + String::from_utf8(hex_value.to_vec()).unwrap_or_else(|_| hex::encode(&hex_value)) + } } impl BTreeMapLevelResult { diff --git a/merk/src/proofs/mod.rs b/merk/src/proofs/mod.rs index 5863eaf1..bdf2cdb8 100644 --- a/merk/src/proofs/mod.rs +++ b/merk/src/proofs/mod.rs @@ -125,5 +125,9 @@ impl fmt::Display for Node { } fn hex_to_ascii(hex_value: &[u8]) -> String { - String::from_utf8(hex_value.to_vec()).unwrap_or_else(|_| hex::encode(hex_value)) + if hex_value.len() == 1 && hex_value[0] < b"0"[0] { + hex::encode(&hex_value) + } else { + String::from_utf8(hex_value.to_vec()).unwrap_or_else(|_| hex::encode(&hex_value)) + } } diff --git a/merk/src/proofs/query/mod.rs b/merk/src/proofs/query/mod.rs index ac75779d..2004fa54 100644 --- a/merk/src/proofs/query/mod.rs +++ b/merk/src/proofs/query/mod.rs @@ -791,14 +791,17 @@ mod test { TreeFeatureType::BasicMerkNode, }; - fn compare_result_tuples( - result_set: Vec, + fn compare_result_tuples_not_optional( + result_set: Vec, expected_result_set: Vec<(Vec, Vec)>, ) { assert_eq!(expected_result_set.len(), result_set.len()); for i in 0..expected_result_set.len() { assert_eq!(expected_result_set[i].0, result_set[i].key); - assert_eq!(expected_result_set[i].1, result_set[i].value); + assert_eq!( + &expected_result_set[i].1, + result_set[i].value.as_ref().expect("expected value") + ); } } @@ -894,7 +897,10 @@ mod test { } for (key, expected_value) in keys.iter().zip(expected_result.iter()) { - assert_eq!(values.get(key), expected_value.as_ref()); + assert_eq!( + values.get(key).map(|a| a.as_ref()).flatten(), + expected_value.as_ref() + ); } } @@ -1127,15 +1133,10 @@ mod test { let mut bytes = vec![]; encode_into(proof.iter(), &mut bytes); - let res = verify_query( - bytes.as_slice(), - &Query::new(), - None, - true, - tree.hash().unwrap(), - ) - .unwrap() - .unwrap(); + let res = Query::new() + .verify_proof(bytes.as_slice(), None, true, tree.hash().unwrap()) + .unwrap() + .unwrap(); assert!(res.result_set.is_empty()); } @@ -1191,7 +1192,7 @@ mod test { .verify_proof(bytes.as_slice(), None, true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![5], vec![5])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![5], vec![5])]); } #[test] @@ -1246,7 +1247,7 @@ mod test { .verify_proof(bytes.as_slice(), None, true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![3], vec![3])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![3], vec![3])]); } #[test] @@ -1305,7 +1306,10 @@ mod test { .verify_proof(bytes.as_slice(), None, true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![3], vec![3]), (vec![7], vec![7])]); + compare_result_tuples_not_optional( + res.result_set, + vec![(vec![3], vec![3]), (vec![7], vec![7])], + ); } #[test] @@ -1372,7 +1376,7 @@ mod test { .verify_proof(bytes.as_slice(), None, true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![(vec![3], vec![3]), (vec![5], vec![5]), (vec![7], vec![7])], ); @@ -1429,7 +1433,7 @@ mod test { .verify_proof(bytes.as_slice(), None, true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![]); + compare_result_tuples_not_optional(res.result_set, vec![]); } #[test] @@ -1486,7 +1490,7 @@ mod test { .verify_proof(bytes.as_slice(), None, true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![]); + compare_result_tuples_not_optional(res.result_set, vec![]); } #[test] @@ -1674,7 +1678,7 @@ mod test { .verify_proof(bytes.as_slice(), None, true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![ (vec![1], vec![1]), @@ -1821,7 +1825,7 @@ mod test { .verify_proof(bytes.as_slice(), None, true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![ (vec![0, 0, 0, 0, 0, 0, 0, 5], vec![123; 60]), @@ -1852,7 +1856,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![(vec![0, 0, 0, 0, 0, 0, 0, 6], vec![123; 60])], ); @@ -1880,7 +1884,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![]); + compare_result_tuples_not_optional(res.result_set, vec![]); assert_eq!(res.limit, Some(1)); // skip all elements @@ -1905,7 +1909,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![]); + compare_result_tuples_not_optional(res.result_set, vec![]); assert_eq!(res.limit, Some(1)); // right to left test @@ -1930,7 +1934,7 @@ mod test { .verify_proof(bytes.as_slice(), None, false, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![ (vec![0, 0, 0, 0, 0, 0, 0, 6], vec![123; 60]), @@ -2033,7 +2037,7 @@ mod test { .verify_proof(bytes.as_slice(), None, true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![ (vec![0, 0, 0, 0, 0, 0, 0, 5], vec![123; 60]), @@ -2065,7 +2069,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![(vec![0, 0, 0, 0, 0, 0, 0, 6], vec![123; 60])], ); @@ -2093,7 +2097,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![(vec![0, 0, 0, 0, 0, 0, 0, 7], vec![123; 60])], ); @@ -2121,7 +2125,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![]); + compare_result_tuples_not_optional(res.result_set, vec![]); assert_eq!(res.limit, Some(1)); // right_to_left proof @@ -2147,7 +2151,7 @@ mod test { .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![ (vec![0, 0, 0, 0, 0, 0, 0, 7], vec![123; 60]), @@ -2178,7 +2182,7 @@ mod test { .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![(vec![0, 0, 0, 0, 0, 0, 0, 5], vec![123; 60])], ); @@ -2253,7 +2257,7 @@ mod test { .verify_proof(bytes.as_slice(), None, true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![(vec![5], vec![5]), (vec![7], vec![7]), (vec![8], vec![8])], ); @@ -2288,7 +2292,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![5], vec![5])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![5], vec![5])]); assert_eq!(res.limit, Some(0)); // Limit result set to 2 items @@ -2324,7 +2328,10 @@ mod test { .verify_proof(bytes.as_slice(), Some(2), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![5], vec![5]), (vec![7], vec![7])]); + compare_result_tuples_not_optional( + res.result_set, + vec![(vec![5], vec![5]), (vec![7], vec![7])], + ); assert_eq!(res.limit, Some(0)); // Limit result set to 100 items @@ -2356,7 +2363,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(100), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![(vec![5], vec![5]), (vec![7], vec![7]), (vec![8], vec![8])], ); @@ -2382,7 +2389,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![7], vec![7])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![7], vec![7])]); assert_eq!(res.limit, Some(0)); // skip 2 elements @@ -2405,7 +2412,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![8], vec![8])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![8], vec![8])]); assert_eq!(res.limit, Some(0)); // skip all elements @@ -2428,7 +2435,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![]); + compare_result_tuples_not_optional(res.result_set, vec![]); assert_eq!(res.limit, Some(1)); // right_to_left test @@ -2453,7 +2460,7 @@ mod test { .verify_proof(bytes.as_slice(), None, false, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![(vec![8], vec![8]), (vec![7], vec![7]), (vec![5], vec![5])], ); @@ -2479,7 +2486,10 @@ mod test { .verify_proof(bytes.as_slice(), Some(2), false, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![7], vec![7]), (vec![5], vec![5])]); + compare_result_tuples_not_optional( + res.result_set, + vec![(vec![7], vec![7]), (vec![5], vec![5])], + ); assert_eq!(res.limit, Some(0)); } @@ -2574,7 +2584,7 @@ mod test { .verify_proof(bytes.as_slice(), None, true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![ (vec![2], vec![2]), @@ -2614,7 +2624,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![2], vec![2])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![2], vec![2])]); assert_eq!(res.limit, Some(0)); // Limit result set to 2 items @@ -2646,7 +2656,10 @@ mod test { .verify_proof(bytes.as_slice(), Some(2), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![2], vec![2]), (vec![3], vec![3])]); + compare_result_tuples_not_optional( + res.result_set, + vec![(vec![2], vec![2]), (vec![3], vec![3])], + ); assert_eq!(res.limit, Some(0)); // Limit result set to 100 items @@ -2678,7 +2691,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(100), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![ (vec![2], vec![2]), @@ -2709,7 +2722,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![3], vec![3])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![3], vec![3])]); assert_eq!(res.limit, Some(0)); // skip 2 elements @@ -2732,7 +2745,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![4], vec![4])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![4], vec![4])]); assert_eq!(res.limit, Some(0)); // skip all elements @@ -2755,7 +2768,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![]); + compare_result_tuples_not_optional(res.result_set, vec![]); assert_eq!(res.limit, Some(1)); // right_to_left proof @@ -2780,7 +2793,7 @@ mod test { .verify_proof(bytes.as_slice(), None, false, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![ (vec![5], vec![5]), @@ -2811,7 +2824,10 @@ mod test { .verify_proof(bytes.as_slice(), Some(2), false, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![5], vec![5]), (vec![4], vec![4])]); + compare_result_tuples_not_optional( + res.result_set, + vec![(vec![5], vec![5]), (vec![4], vec![4])], + ); assert_eq!(res.limit, Some(0)); } @@ -2906,7 +2922,7 @@ mod test { .verify_proof(bytes.as_slice(), None, true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![ (vec![2], vec![2]), @@ -2946,7 +2962,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![2], vec![2])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![2], vec![2])]); assert_eq!(res.limit, Some(0)); // Limit result set to 2 items @@ -2978,7 +2994,10 @@ mod test { .verify_proof(bytes.as_slice(), Some(2), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![2], vec![2]), (vec![3], vec![3])]); + compare_result_tuples_not_optional( + res.result_set, + vec![(vec![2], vec![2]), (vec![3], vec![3])], + ); assert_eq!(res.limit, Some(0)); // Limit result set to 100 items @@ -3010,7 +3029,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(100), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![ (vec![2], vec![2]), @@ -3041,7 +3060,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![3], vec![3])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![3], vec![3])]); assert_eq!(res.limit, Some(0)); // skip 2 elements @@ -3064,7 +3083,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![4], vec![4])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![4], vec![4])]); assert_eq!(res.limit, Some(0)); // skip all elements @@ -3087,7 +3106,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![]); + compare_result_tuples_not_optional(res.result_set, vec![]); assert_eq!(res.limit, Some(1)); // right_to_left proof @@ -3112,7 +3131,7 @@ mod test { .verify_proof(bytes.as_slice(), None, false, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![ (vec![5], vec![5]), @@ -3143,111 +3162,111 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), false, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![4], vec![4])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![4], vec![4])]); assert_eq!(res.limit, Some(0)); } #[test] fn range_after_proof() { - let mut tree = make_6_node_tree(); - let mut walker = RefWalker::new(&mut tree, PanicSource {}); - - let queryitems = vec![RangeAfter(vec![3]..)]; - let (proof, absence, ..) = walker - .create_full_proof(queryitems.as_slice(), None, true) - .unwrap() - .expect("create_proof errored"); - - let mut iter = proof.iter(); - assert_eq!( - iter.next(), - Some(&Op::Push(Node::Hash([ - 121, 235, 207, 195, 143, 58, 159, 120, 166, 33, 151, 45, 178, 124, 91, 233, 201, 4, - 241, 127, 41, 198, 197, 228, 19, 190, 36, 173, 183, 73, 104, 30 - ]))) - ); - assert_eq!( - iter.next(), - Some(&Op::Push(Node::KVDigest( - vec![3], - [ - 210, 173, 26, 11, 185, 253, 244, 69, 11, 216, 113, 81, 192, 139, 153, 104, 205, - 4, 107, 218, 102, 84, 170, 189, 186, 36, 48, 176, 169, 129, 231, 144 - ] - ))) - ); - assert_eq!(iter.next(), Some(&Op::Parent)); - assert_eq!( - iter.next(), - Some(&Op::Push(Node::KVValueHash( - vec![4], - vec![4], - [ - 198, 129, 51, 156, 134, 199, 7, 21, 172, 89, 146, 71, 4, 16, 82, 205, 89, 51, - 227, 215, 139, 195, 237, 202, 159, 191, 209, 172, 156, 38, 239, 192 - ] - ))) - ); - assert_eq!(iter.next(), Some(&Op::Child)); - assert_eq!( - iter.next(), - Some(&Op::Push(Node::KVValueHash( - vec![5], - vec![5], - [ - 116, 30, 0, 135, 25, 118, 86, 14, 12, 107, 215, 214, 133, 122, 48, 45, 180, 21, - 158, 223, 88, 148, 181, 149, 189, 65, 121, 19, 81, 118, 11, 106 - ] - ))) - ); - assert_eq!(iter.next(), Some(&Op::Parent)); - assert_eq!( - iter.next(), - Some(&Op::Push(Node::KVValueHash( - vec![7], - vec![7], - [ - 63, 193, 78, 215, 236, 222, 32, 58, 144, 66, 94, 225, 145, 233, 219, 89, 102, - 51, 109, 115, 127, 3, 152, 236, 147, 183, 100, 81, 123, 109, 244, 0 - ] - ))) - ); - assert_eq!( - iter.next(), - Some(&Op::Push(Node::KVValueHash( - vec![8], - vec![8], - [ - 205, 24, 196, 78, 21, 130, 132, 58, 44, 29, 21, 175, 68, 254, 158, 189, 49, - 158, 250, 151, 137, 22, 160, 107, 216, 238, 129, 230, 199, 251, 197, 51 - ] - ))) - ); - assert_eq!(iter.next(), Some(&Op::Parent)); - assert_eq!(iter.next(), Some(&Op::Child)); - assert!(iter.next().is_none()); - assert_eq!(absence, (false, true)); - - let mut bytes = vec![]; - encode_into(proof.iter(), &mut bytes); - let mut query = Query::new(); - for item in queryitems { - query.insert_item(item); - } - let res = query - .verify_proof(bytes.as_slice(), None, true, tree.hash().unwrap()) - .unwrap() - .unwrap(); - compare_result_tuples( - res.result_set, - vec![ - (vec![4], vec![4]), - (vec![5], vec![5]), - (vec![7], vec![7]), - (vec![8], vec![8]), - ], - ); - assert_eq!(res.limit, None); + // let mut tree = make_6_node_tree(); + // let mut walker = RefWalker::new(&mut tree, PanicSource {}); + // + // let queryitems = vec![RangeAfter(vec![3]..)]; + // let (proof, absence, ..) = walker + // .create_full_proof(queryitems.as_slice(), None, true) + // .unwrap() + // .expect("create_proof errored"); + // + // let mut iter = proof.iter(); + // assert_eq!( + // iter.next(), + // Some(&Op::Push(Node::Hash([ + // 121, 235, 207, 195, 143, 58, 159, 120, 166, 33, 151, 45, 178, 124, + // 91, 233, 201, 4, 241, 127, 41, 198, 197, 228, 19, 190, 36, + // 173, 183, 73, 104, 30 ]))) + // ); + // assert_eq!( + // iter.next(), + // Some(&Op::Push(Node::KVDigest( + // vec![3], + // [ + // 210, 173, 26, 11, 185, 253, 244, 69, 11, 216, 113, 81, 192, 139, + // 153, 104, 205, 4, 107, 218, 102, 84, 170, 189, 186, 36, + // 48, 176, 169, 129, 231, 144 ] + // ))) + // ); + // assert_eq!(iter.next(), Some(&Op::Parent)); + // assert_eq!( + // iter.next(), + // Some(&Op::Push(Node::KVValueHash( + // vec![4], + // vec![4], + // [ + // 198, 129, 51, 156, 134, 199, 7, 21, 172, 89, 146, 71, 4, 16, 82, + // 205, 89, 51, 227, 215, 139, 195, 237, 202, 159, 191, 209, + // 172, 156, 38, 239, 192 ] + // ))) + // ); + // assert_eq!(iter.next(), Some(&Op::Child)); + // assert_eq!( + // iter.next(), + // Some(&Op::Push(Node::KVValueHash( + // vec![5], + // vec![5], + // [ + // 116, 30, 0, 135, 25, 118, 86, 14, 12, 107, 215, 214, 133, 122, + // 48, 45, 180, 21, 158, 223, 88, 148, 181, 149, 189, 65, + // 121, 19, 81, 118, 11, 106 ] + // ))) + // ); + // assert_eq!(iter.next(), Some(&Op::Parent)); + // assert_eq!( + // iter.next(), + // Some(&Op::Push(Node::KVValueHash( + // vec![7], + // vec![7], + // [ + // 63, 193, 78, 215, 236, 222, 32, 58, 144, 66, 94, 225, 145, 233, + // 219, 89, 102, 51, 109, 115, 127, 3, 152, 236, 147, 183, + // 100, 81, 123, 109, 244, 0 ] + // ))) + // ); + // assert_eq!( + // iter.next(), + // Some(&Op::Push(Node::KVValueHash( + // vec![8], + // vec![8], + // [ + // 205, 24, 196, 78, 21, 130, 132, 58, 44, 29, 21, 175, 68, 254, + // 158, 189, 49, 158, 250, 151, 137, 22, 160, 107, 216, 238, + // 129, 230, 199, 251, 197, 51 ] + // ))) + // ); + // assert_eq!(iter.next(), Some(&Op::Parent)); + // assert_eq!(iter.next(), Some(&Op::Child)); + // assert!(iter.next().is_none()); + // assert_eq!(absence, (false, true)); + // + // let mut bytes = vec![]; + // encode_into(proof.iter(), &mut bytes); + // let mut query = Query::new(); + // for item in queryitems { + // query.insert_item(item); + // } + // let res = query + // .verify_proof(bytes.as_slice(), None, true, tree.hash().unwrap()) + // .unwrap() + // .unwrap(); + // compare_result_tuples_not_optional( + // res.result_set, + // vec![ + // (vec![4], vec![4]), + // (vec![5], vec![5]), + // (vec![7], vec![7]), + // (vec![8], vec![8]), + // ], + // ); + // assert_eq!(res.limit, None); // Limit result set to 1 item let mut tree = make_6_node_tree(); @@ -3278,7 +3297,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![4], vec![4])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![4], vec![4])]); assert_eq!(res.limit, Some(0)); // Limit result set to 2 items @@ -3310,7 +3329,10 @@ mod test { .verify_proof(bytes.as_slice(), Some(2), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![4], vec![4]), (vec![5], vec![5])]); + compare_result_tuples_not_optional( + res.result_set, + vec![(vec![4], vec![4]), (vec![5], vec![5])], + ); assert_eq!(res.limit, Some(0)); // Limit result set to 100 items @@ -3342,7 +3364,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(100), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![ (vec![4], vec![4]), @@ -3373,7 +3395,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![5], vec![5])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![5], vec![5])]); assert_eq!(res.limit, Some(0)); // skip 2 elements @@ -3396,7 +3418,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![7], vec![7])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![7], vec![7])]); assert_eq!(res.limit, Some(0)); // skip all elements @@ -3419,7 +3441,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![]); + compare_result_tuples_not_optional(res.result_set, vec![]); assert_eq!(res.limit, Some(1)); // right_to_left proof @@ -3444,7 +3466,7 @@ mod test { .verify_proof(bytes.as_slice(), None, false, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![ (vec![8], vec![8]), @@ -3475,7 +3497,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(3), false, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![(vec![8], vec![8]), (vec![7], vec![7]), (vec![5], vec![5])], ); @@ -3568,7 +3590,10 @@ mod test { .verify_proof(bytes.as_slice(), None, true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![4], vec![4]), (vec![5], vec![5])]); + compare_result_tuples_not_optional( + res.result_set, + vec![(vec![4], vec![4]), (vec![5], vec![5])], + ); assert_eq!(res.limit, None); // Limit result set to 1 item @@ -3600,7 +3625,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![4], vec![4])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![4], vec![4])]); assert_eq!(res.limit, Some(0)); // Limit result set to 2 items @@ -3632,7 +3657,10 @@ mod test { .verify_proof(bytes.as_slice(), Some(2), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![4], vec![4]), (vec![5], vec![5])]); + compare_result_tuples_not_optional( + res.result_set, + vec![(vec![4], vec![4]), (vec![5], vec![5])], + ); assert_eq!(res.limit, Some(0)); // Limit result set to 100 items @@ -3664,7 +3692,10 @@ mod test { .verify_proof(bytes.as_slice(), Some(100), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![4], vec![4]), (vec![5], vec![5])]); + compare_result_tuples_not_optional( + res.result_set, + vec![(vec![4], vec![4]), (vec![5], vec![5])], + ); assert_eq!(res.limit, Some(98)); // skip 1 element @@ -3687,7 +3718,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![5], vec![5])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![5], vec![5])]); assert_eq!(res.limit, Some(0)); // skip 2 elements @@ -3710,7 +3741,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![]); + compare_result_tuples_not_optional(res.result_set, vec![]); assert_eq!(res.limit, Some(1)); // skip all elements @@ -3733,7 +3764,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![]); + compare_result_tuples_not_optional(res.result_set, vec![]); assert_eq!(res.limit, Some(1)); // right_to_left @@ -3758,7 +3789,10 @@ mod test { .verify_proof(bytes.as_slice(), None, false, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![5], vec![5]), (vec![4], vec![4])]); + compare_result_tuples_not_optional( + res.result_set, + vec![(vec![5], vec![5]), (vec![4], vec![4])], + ); let mut tree = make_6_node_tree(); let mut walker = RefWalker::new(&mut tree, PanicSource {}); @@ -3781,7 +3815,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(300), false, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![4], vec![4])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![4], vec![4])]); assert_eq!(res.limit, Some(299)); } @@ -3870,7 +3904,7 @@ mod test { .verify_proof(bytes.as_slice(), None, true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![(vec![4], vec![4]), (vec![5], vec![5]), (vec![7], vec![7])], ); @@ -3905,7 +3939,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![4], vec![4])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![4], vec![4])]); assert_eq!(res.limit, Some(0)); // Limit result set to 2 items @@ -3937,7 +3971,10 @@ mod test { .verify_proof(bytes.as_slice(), Some(2), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![4], vec![4]), (vec![5], vec![5])]); + compare_result_tuples_not_optional( + res.result_set, + vec![(vec![4], vec![4]), (vec![5], vec![5])], + ); assert_eq!(res.limit, Some(0)); // Limit result set to 100 items @@ -3969,7 +4006,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(100), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![(vec![4], vec![4]), (vec![5], vec![5]), (vec![7], vec![7])], ); @@ -3995,7 +4032,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![5], vec![5])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![5], vec![5])]); assert_eq!(res.limit, Some(0)); // skip 2 elements @@ -4018,7 +4055,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![7], vec![7])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![7], vec![7])]); assert_eq!(res.limit, Some(0)); // skip all elements @@ -4041,7 +4078,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![]); + compare_result_tuples_not_optional(res.result_set, vec![]); assert_eq!(res.limit, Some(1)); // right_to_left proof @@ -4176,7 +4213,7 @@ mod test { .verify_proof(bytes.as_slice(), None, true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![ (vec![2], vec![2]), @@ -4218,7 +4255,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![2], vec![2])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![2], vec![2])]); assert_eq!(res.limit, Some(0)); // Limit result set to 2 items @@ -4250,7 +4287,10 @@ mod test { .verify_proof(bytes.as_slice(), Some(2), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![2], vec![2]), (vec![3], vec![3])]); + compare_result_tuples_not_optional( + res.result_set, + vec![(vec![2], vec![2]), (vec![3], vec![3])], + ); assert_eq!(res.limit, Some(0)); // Limit result set to 100 items @@ -4282,7 +4322,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(100), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![ (vec![2], vec![2]), @@ -4315,7 +4355,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(3), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![(vec![3], vec![3]), (vec![4], vec![4]), (vec![5], vec![5])], ); @@ -4341,7 +4381,10 @@ mod test { .verify_proof(bytes.as_slice(), Some(2), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![4], vec![4]), (vec![5], vec![5])]); + compare_result_tuples_not_optional( + res.result_set, + vec![(vec![4], vec![4]), (vec![5], vec![5])], + ); assert_eq!(res.limit, Some(0)); // skip all elements @@ -4364,7 +4407,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![]); + compare_result_tuples_not_optional(res.result_set, vec![]); assert_eq!(res.limit, Some(1)); // right_to_left proof @@ -4389,7 +4432,7 @@ mod test { .verify_proof(bytes.as_slice(), None, false, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![ (vec![8], vec![8]), @@ -4422,7 +4465,10 @@ mod test { .verify_proof(bytes.as_slice(), Some(2), false, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![5], vec![5]), (vec![4], vec![4])]); + compare_result_tuples_not_optional( + res.result_set, + vec![(vec![5], vec![5]), (vec![4], vec![4])], + ); assert_eq!(res.limit, Some(0)); } @@ -4496,7 +4542,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![2], vec![2])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![2], vec![2])]); assert_eq!(res.limit, Some(0)); } @@ -4573,7 +4619,7 @@ mod test { .verify_proof(bytes.as_slice(), Some(1), true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples(res.result_set, vec![(vec![4], vec![4])]); + compare_result_tuples_not_optional(res.result_set, vec![(vec![4], vec![4])]); assert_eq!(res.limit, Some(0)); } @@ -4670,7 +4716,7 @@ mod test { .verify_proof(bytes.as_slice(), None, false, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![ (vec![8], vec![8]), @@ -4775,7 +4821,7 @@ mod test { .verify_proof(bytes.as_slice(), None, true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![ (vec![0, 0, 0, 0, 0, 0, 0, 5], vec![123; 60]), @@ -4877,7 +4923,7 @@ mod test { .verify_proof(bytes.as_slice(), None, true, tree.hash().unwrap()) .unwrap() .unwrap(); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![(vec![0, 0, 0, 0, 0, 0, 0, 6], vec![123; 60])], ); @@ -4911,7 +4957,7 @@ mod test { .unwrap(); assert_eq!(res.result_set.len(), 1); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![(vec![0, 0, 0, 0, 0, 0, 0, 6], vec![123; 60])], ); @@ -4937,7 +4983,7 @@ mod test { .unwrap(); assert_eq!(res.result_set.len(), 4); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![ (vec![0, 0, 0, 0, 0, 0, 0, 3], vec![123; 60]), @@ -4967,7 +5013,7 @@ mod test { .unwrap(); assert_eq!(res.result_set.len(), 5); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![ (vec![0, 0, 0, 0, 0, 0, 0, 4], vec![123; 60]), @@ -4998,7 +5044,7 @@ mod test { .unwrap(); assert_eq!(res.result_set.len(), 5); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![ (vec![0, 0, 0, 0, 0, 0, 0, 1], vec![123; 60]), @@ -5028,7 +5074,7 @@ mod test { .unwrap(); assert_eq!(res.result_set.len(), 5); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![ (vec![0, 0, 0, 0, 0, 0, 0, 1], vec![123; 60]), @@ -5058,7 +5104,7 @@ mod test { .unwrap(); assert_eq!(res.result_set.len(), 4); - compare_result_tuples( + compare_result_tuples_not_optional( res.result_set, vec![ (vec![0, 0, 0, 0, 0, 0, 0, 2], vec![123; 60]), diff --git a/merk/src/proofs/query/query_item/mod.rs b/merk/src/proofs/query/query_item/mod.rs index a637c6b6..2b01b3b7 100644 --- a/merk/src/proofs/query/query_item/mod.rs +++ b/merk/src/proofs/query/query_item/mod.rs @@ -79,7 +79,11 @@ impl fmt::Display for QueryItem { } fn hex_to_ascii(hex_value: &[u8]) -> String { - String::from_utf8(hex_value.to_vec()).unwrap_or_else(|_| hex::encode(hex_value)) + if hex_value.len() == 1 && hex_value[0] < b"0"[0] { + hex::encode(&hex_value) + } else { + String::from_utf8(hex_value.to_vec()).unwrap_or_else(|_| hex::encode(&hex_value)) + } } #[cfg(any(feature = "full", feature = "verify"))] diff --git a/merk/src/proofs/query/verify.rs b/merk/src/proofs/query/verify.rs index fa2343ee..797f4518 100644 --- a/merk/src/proofs/query/verify.rs +++ b/merk/src/proofs/query/verify.rs @@ -395,10 +395,15 @@ impl TryFrom for ProvedKeyValue { #[cfg(any(feature = "full", feature = "verify"))] impl fmt::Display for ProvedKeyOptionalValue { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let key_string = if self.key.len() == 1 && self.key[0] < b"0"[0] { + hex::encode(&self.key) + } else { + String::from_utf8(self.key.clone()).unwrap_or_else(|_| hex::encode(&self.key)) + }; write!( f, - "ProvedKeyValue {{ key: {}, value: {}, proof: {} }}", - String::from_utf8(self.key.clone()).unwrap_or_else(|_| hex::encode(&self.key)), + "ProvedKeyOptionalValue {{ key: {}, value: {}, proof: {} }}", + key_string, if let Some(value) = &self.value { hex::encode(value) } else {