This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expose proof generation and verifying api. #4646
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9f581f6
Expose proof generation and verifying api.
montekki 4c3f72f
tabs to spaces
montekki d1ebb74
bring back license comment
montekki 91f9157
Revert "tabs to spaces"
montekki d0958fd
Formatting and docs nits
montekki f4dc4b6
Bump deps versions
montekki c0190f6
Merge branch 'master' into fs-triedb-proofs
montekki b080af6
Upadte Cargo.lock
montekki a582662
into -> in
montekki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Expose proof generation and verifying api.
- Loading branch information
commit 9f581f653ec4866c8bf04549d4148573b6122914
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| // Copyright 2015-2020 Parity Technologies (UK) Ltd. | ||
| // This file is part of Substrate. | ||
|
|
||
| // Parity is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
|
|
@@ -27,6 +25,8 @@ use sp_std::boxed::Box; | |
| use sp_std::marker::PhantomData; | ||
| use sp_std::vec::Vec; | ||
| use hash_db::{Hasher, Prefix}; | ||
| use trie_db::proof::{generate_proof, verify_proof}; | ||
| pub use trie_db::proof::VerifyError; | ||
| /// Our `NodeCodec`-specific error. | ||
| pub use error::Error; | ||
| /// The Substrate format implementation of `TrieStream`. | ||
|
|
@@ -119,6 +119,51 @@ pub mod trie_types { | |
| pub type TrieError<H> = trie_db::TrieError<H, super::Error>; | ||
| } | ||
|
|
||
| /// Create a proof for a subset of keys in a trie. | ||
| /// | ||
| /// The `keys` may contain any set of keys regardless of each one of them is included | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: "included into" should be "included in" - here and in other docs. |
||
| /// into the `db`. | ||
| /// | ||
| /// For a key `K` that is included into the `db` a proof of inclusion is generated. | ||
| /// For a key `K` that is not included into the `db` a proof of non-inclusion is generated. | ||
| /// These can be later checked in [`verify_trie_proof`]. | ||
| /// | ||
| /// [`verify_trie_proof`]: ./fn.verify_trie_proof.html | ||
montekki marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pub fn generate_trie_proof<'a, L: TrieConfiguration, I, K, DB>( | ||
| db: &DB, | ||
| root: TrieHash<L>, | ||
| keys: I | ||
| ) -> Result<Vec<Vec<u8>>, Box<TrieError<L>>> where | ||
| I: IntoIterator<Item=&'a K>, | ||
| K: 'a + AsRef<[u8]>, | ||
| DB: hash_db::HashDBRef<L::Hash, trie_db::DBValue>, | ||
| { | ||
| let trie = TrieDB::<L>::new(db, &root)?; | ||
| generate_proof(&trie, keys) | ||
| } | ||
|
|
||
| /// Verify a set of key-value pairs against a trie root and a proof. | ||
| /// | ||
| /// Checks a set of keys with optional values for inclusion into the proof that was generated by | ||
| /// [`generate_trie_proof`]. | ||
| /// If the value in the pair is supplied (`(key, Some(value))`), this key-value pair will be | ||
| /// checked for inclusion into the proof. | ||
| /// If the value is omitted (`(key, None)`), this key will be checked for non-inclusion into the | ||
| /// proof. | ||
| /// | ||
| /// [`generate_trie_proof`]: ./fn.generate_trie_proof.html | ||
montekki marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pub fn verify_trie_proof<'a, L: TrieConfiguration, I, K, V>( | ||
| root: &TrieHash<L>, | ||
| proof: &[Vec<u8>], | ||
| items: I, | ||
| ) -> Result<(), VerifyError<TrieHash<L>, error::Error>> where | ||
| I: IntoIterator<Item=&'a (K, Option<V>)>, | ||
| K: 'a + AsRef<[u8]>, | ||
| V: 'a + AsRef<[u8]>, | ||
| { | ||
| verify_proof::<Layout<L::Hash>, _, _, _>(root, proof, items) | ||
| } | ||
|
|
||
| /// Determine a trie root given a hash DB and delta values. | ||
| pub fn delta_trie_root<L: TrieConfiguration, I, A, B, DB>( | ||
| db: &mut DB, | ||
|
|
@@ -727,4 +772,89 @@ 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::<Layout>(&mut memdb, &mut root, &pairs); | ||
|
|
||
| let non_included_key: Vec<u8> = hex!("0909").to_vec(); | ||
| let proof = generate_trie_proof::<Layout, _, _, _>( | ||
| &memdb, | ||
| root, | ||
| &[non_included_key.clone()] | ||
| ).unwrap(); | ||
|
|
||
| // Verifying that the K was not included into the trie should work. | ||
| assert!(verify_trie_proof::<Layout, _, _, Vec<u8>>( | ||
| &root, | ||
| &proof, | ||
| &[(non_included_key.clone(), None)], | ||
| ).is_ok() | ||
| ); | ||
|
|
||
| // Verifying that the K was included into the trie should fail. | ||
| assert!(verify_trie_proof::<Layout, _, _, Vec<u8>>( | ||
| &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::<Layout>(&mut memdb, &mut root, &pairs); | ||
|
|
||
| let proof = generate_trie_proof::<Layout, _, _, _>( | ||
| &memdb, | ||
| root, | ||
| &[pairs[0].0.clone()] | ||
| ).unwrap(); | ||
|
|
||
| // Check that a K, V included into the proof are verified. | ||
| assert!(verify_trie_proof::<Layout, _, _, _>( | ||
| &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::<Layout, _, _, Vec<u8>>( | ||
| &root, | ||
| &proof, | ||
| &[(pairs[0].0.clone(), None)] | ||
| ).is_err() | ||
| ); | ||
|
|
||
| // K not included into the trie is not verified. | ||
| assert!(verify_trie_proof::<Layout, _, _, _>( | ||
| &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::<Layout, _, _, _>( | ||
| &root, | ||
| &proof, | ||
| &[(pairs[1].0.clone(), Some(pairs[1].1.clone()))] | ||
| ).is_err() | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.