This repository has been 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.6k
Fix the undeterministic storage proof recorded for the same execution #10915
Merged
paritytech-processbot
merged 19 commits into
paritytech:master
from
liuchengxu:fix-proof-recorded-undeterministic
Mar 5, 2022
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ea9c8f2
Add a test case for the determinism of recorded proof
liuchengxu 3051028
Replace HashMap with BTreeMap for the actual proof records
liuchengxu 3254cda
cargo +nightly fmt --all
liuchengxu 56aaa02
Store the trie nodes in BTreeSet for StorageProof
liuchengxu f98796b
Merge branch 'master' of https://github.com/paritytech/substrate into…
liuchengxu 4d1b6fa
Nit
liuchengxu 7320c43
Revert the BTreeMap changes and sort when converting to storage proof
liuchengxu 40067d3
Remove PartialEq from StorageProof
liuchengxu 1213c52
Remove unnecessary change
liuchengxu 8ce692b
Add `compare` method to StorageProof
liuchengxu b225f0b
Merge branch 'master' of https://github.com/paritytech/substrate into…
liuchengxu 76669a8
FMT
liuchengxu 5508aef
Dummy change to trigger CI
liuchengxu e1aa977
Merge branch 'master' of https://github.com/paritytech/substrate into…
liuchengxu 9701cde
Use `BTreeSet` for StorageProof and keep using `Vec` for CompactProof
liuchengxu d3506bd
Update comment on `iter_nodes`
liuchengxu 3e5afce
Revert `PartialEq` removal
liuchengxu bd48df1
Merge branch 'master' of https://github.com/paritytech/substrate into…
liuchengxu 89e312a
Merge branch 'master' of https://github.com/paritytech/substrate into…
liuchengxu 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
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
use codec::{Decode, Encode}; | ||
use hash_db::{HashDB, Hasher}; | ||
use scale_info::TypeInfo; | ||
use sp_std::vec::Vec; | ||
use sp_std::{collections::btree_set::BTreeSet, iter::IntoIterator, vec::Vec}; | ||
// Note that `LayoutV1` usage here (proof compaction) is compatible | ||
// with `LayoutV0`. | ||
use crate::LayoutV1 as Layout; | ||
|
@@ -32,36 +32,36 @@ use crate::LayoutV1 as Layout; | |
/// the serialized nodes and performing the key lookups. | ||
#[derive(Debug, PartialEq, Eq, Clone, Encode, Decode, TypeInfo)] | ||
pub struct StorageProof { | ||
trie_nodes: Vec<Vec<u8>>, | ||
trie_nodes: BTreeSet<Vec<u8>>, | ||
} | ||
|
||
impl StorageProof { | ||
/// Constructs a storage proof from a subset of encoded trie nodes in a storage backend. | ||
pub fn new(trie_nodes: Vec<Vec<u8>>) -> Self { | ||
StorageProof { trie_nodes } | ||
pub fn new(trie_nodes: impl IntoIterator<Item = Vec<u8>>) -> Self { | ||
StorageProof { trie_nodes: BTreeSet::from_iter(trie_nodes) } | ||
} | ||
|
||
/// Returns a new empty proof. | ||
/// | ||
/// An empty proof is capable of only proving trivial statements (ie. that an empty set of | ||
/// key-value pairs exist in storage). | ||
pub fn empty() -> Self { | ||
StorageProof { trie_nodes: Vec::new() } | ||
StorageProof { trie_nodes: BTreeSet::new() } | ||
} | ||
|
||
/// Returns whether this is an empty proof. | ||
pub fn is_empty(&self) -> bool { | ||
self.trie_nodes.is_empty() | ||
} | ||
|
||
/// Create an iterator over trie nodes constructed from the proof. The nodes are not guaranteed | ||
/// to be traversed in any particular order. | ||
/// Create an iterator over encoded trie nodes in lexicographical order constructed | ||
/// from the proof. | ||
pub fn iter_nodes(self) -> StorageProofNodeIterator { | ||
StorageProofNodeIterator::new(self) | ||
} | ||
|
||
/// Convert into plain node vector. | ||
pub fn into_nodes(self) -> Vec<Vec<u8>> { | ||
pub fn into_nodes(self) -> BTreeSet<Vec<u8>> { | ||
self.trie_nodes | ||
} | ||
|
||
|
@@ -138,12 +138,13 @@ impl CompactProof { | |
expected_root, | ||
)?; | ||
Ok(( | ||
StorageProof::new( | ||
db.drain() | ||
.into_iter() | ||
.filter_map(|kv| if (kv.1).1 > 0 { Some((kv.1).0) } else { None }) | ||
.collect(), | ||
), | ||
StorageProof::new(db.drain().into_iter().filter_map(|kv| { | ||
if (kv.1).1 > 0 { | ||
Some((kv.1).0) | ||
} else { | ||
None | ||
} | ||
})), | ||
root, | ||
)) | ||
} | ||
|
@@ -171,7 +172,7 @@ impl CompactProof { | |
/// An iterator over trie nodes constructed from a storage proof. The nodes are not guaranteed to | ||
/// be traversed in any particular order. | ||
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. Comment could be updated : "in lexicographical order of encoded nodes.", or something like that. 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. Let's keep it that way, because we then can change this later. |
||
pub struct StorageProofNodeIterator { | ||
inner: <Vec<Vec<u8>> as IntoIterator>::IntoIter, | ||
inner: <BTreeSet<Vec<u8>> as IntoIterator>::IntoIter, | ||
} | ||
|
||
impl StorageProofNodeIterator { | ||
|
This file contains 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So btreeset will produce always ordered proof, but it will also allow decoding proof with non ordered node.
Actually forcing order on decode will break existing code, so it is probably better this way.