diff --git a/primitives/trie/src/lib.rs b/primitives/trie/src/lib.rs index 173321a9b27c2..57a93335efecf 100644 --- a/primitives/trie/src/lib.rs +++ b/primitives/trie/src/lib.rs @@ -130,16 +130,16 @@ pub mod trie_types { /// /// [`verify_trie_proof`]: ./fn.verify_trie_proof.html pub fn generate_trie_proof<'a, L: TrieConfiguration, I, K, DB>( - db: &DB, - root: TrieHash, - keys: I + db: &DB, + root: TrieHash, + keys: I ) -> Result>, Box>> where - I: IntoIterator, - K: 'a + AsRef<[u8]>, - DB: hash_db::HashDBRef, + I: IntoIterator, + K: 'a + AsRef<[u8]>, + DB: hash_db::HashDBRef, { - let trie = TrieDB::::new(db, &root)?; - generate_proof(&trie, keys) + let trie = TrieDB::::new(db, &root)?; + generate_proof(&trie, keys) } /// Verify a set of key-value pairs against a trie root and a proof. @@ -153,15 +153,15 @@ pub fn generate_trie_proof<'a, L: TrieConfiguration, I, K, DB>( /// /// [`generate_trie_proof`]: ./fn.generate_trie_proof.html pub fn verify_trie_proof<'a, L: TrieConfiguration, I, K, V>( - root: &TrieHash, - proof: &[Vec], - items: I, + root: &TrieHash, + proof: &[Vec], + items: I, ) -> Result<(), VerifyError, error::Error>> where - I: IntoIterator)>, - K: 'a + AsRef<[u8]>, - V: 'a + AsRef<[u8]>, + I: IntoIterator)>, + K: 'a + AsRef<[u8]>, + V: 'a + AsRef<[u8]>, { - verify_proof::, _, _, _>(root, proof, items) + verify_proof::, _, _, _>(root, proof, items) } /// Determine a trie root given a hash DB and delta values. @@ -773,88 +773,88 @@ mod tests { assert_eq!(pairs, iter_pairs); } - #[test] - fn proof_non_inclusion_works() { - let pairs = vec![ - (hex!("0102").to_vec(), hex!("01").to_vec()), - (hex!("0203").to_vec(), hex!("0405").to_vec()), - ]; - - let mut memdb = MemoryDB::default(); - let mut root = Default::default(); - populate_trie::(&mut memdb, &mut root, &pairs); - - let non_included_key: Vec = hex!("0909").to_vec(); - let proof = generate_trie_proof::( - &memdb, - root, - &[non_included_key.clone()] - ).unwrap(); - - // Verifying that the K was not included into the trie should work. - assert!(verify_trie_proof::>( - &root, - &proof, - &[(non_included_key.clone(), None)], - ).is_ok() - ); - - // Verifying that the K was included into the trie should fail. - assert!(verify_trie_proof::>( - &root, - &proof, - &[(non_included_key, Some(hex!("1010").to_vec()))], - ).is_err() - ); - } - - #[test] - fn proof_inclusion_works() { - let pairs = vec![ - (hex!("0102").to_vec(), hex!("01").to_vec()), - (hex!("0203").to_vec(), hex!("0405").to_vec()), - ]; - - let mut memdb = MemoryDB::default(); - let mut root = Default::default(); - populate_trie::(&mut memdb, &mut root, &pairs); - - let proof = generate_trie_proof::( - &memdb, - root, - &[pairs[0].0.clone()] - ).unwrap(); - - // Check that a K, V included into the proof are verified. - assert!(verify_trie_proof::( - &root, - &proof, - &[(pairs[0].0.clone(), Some(pairs[0].1.clone()))] - ).is_ok() - ); - - // Absence of the V is not verified with the proof that has K, V included. - assert!(verify_trie_proof::>( - &root, - &proof, - &[(pairs[0].0.clone(), None)] - ).is_err() - ); - - // K not included into the trie is not verified. - assert!(verify_trie_proof::( - &root, - &proof, - &[(hex!("4242").to_vec(), Some(pairs[0].1.clone()))] - ).is_err() - ); - - // K included into the trie but not included into the proof is not verified. - assert!(verify_trie_proof::( - &root, - &proof, - &[(pairs[1].0.clone(), Some(pairs[1].1.clone()))] - ).is_err() - ); - } + #[test] + fn proof_non_inclusion_works() { + let pairs = vec![ + (hex!("0102").to_vec(), hex!("01").to_vec()), + (hex!("0203").to_vec(), hex!("0405").to_vec()), + ]; + + let mut memdb = MemoryDB::default(); + let mut root = Default::default(); + populate_trie::(&mut memdb, &mut root, &pairs); + + let non_included_key: Vec = hex!("0909").to_vec(); + let proof = generate_trie_proof::( + &memdb, + root, + &[non_included_key.clone()] + ).unwrap(); + + // Verifying that the K was not included into the trie should work. + assert!(verify_trie_proof::>( + &root, + &proof, + &[(non_included_key.clone(), None)], + ).is_ok() + ); + + // Verifying that the K was included into the trie should fail. + assert!(verify_trie_proof::>( + &root, + &proof, + &[(non_included_key, Some(hex!("1010").to_vec()))], + ).is_err() + ); + } + + #[test] + fn proof_inclusion_works() { + let pairs = vec![ + (hex!("0102").to_vec(), hex!("01").to_vec()), + (hex!("0203").to_vec(), hex!("0405").to_vec()), + ]; + + let mut memdb = MemoryDB::default(); + let mut root = Default::default(); + populate_trie::(&mut memdb, &mut root, &pairs); + + let proof = generate_trie_proof::( + &memdb, + root, + &[pairs[0].0.clone()] + ).unwrap(); + + // Check that a K, V included into the proof are verified. + assert!(verify_trie_proof::( + &root, + &proof, + &[(pairs[0].0.clone(), Some(pairs[0].1.clone()))] + ).is_ok() + ); + + // Absence of the V is not verified with the proof that has K, V included. + assert!(verify_trie_proof::>( + &root, + &proof, + &[(pairs[0].0.clone(), None)] + ).is_err() + ); + + // K not included into the trie is not verified. + assert!(verify_trie_proof::( + &root, + &proof, + &[(hex!("4242").to_vec(), Some(pairs[0].1.clone()))] + ).is_err() + ); + + // K included into the trie but not included into the proof is not verified. + assert!(verify_trie_proof::( + &root, + &proof, + &[(pairs[1].0.clone(), Some(pairs[1].1.clone()))] + ).is_err() + ); + } }