diff --git a/substrate/frame/session/src/historical/mod.rs b/substrate/frame/session/src/historical/mod.rs index b9cecea1a7f7..3c30a3126660 100644 --- a/substrate/frame/session/src/historical/mod.rs +++ b/substrate/frame/session/src/historical/mod.rs @@ -32,8 +32,9 @@ mod shared; use codec::{Decode, Encode}; use sp_runtime::{ + proving_trie::ProvingTrie, traits::{Convert, OpaqueKeys}, - KeyTypeId, + DispatchError, KeyTypeId, }; use sp_session::{MembershipProof, ValidatorCount}; use sp_staking::SessionIndex; @@ -176,7 +177,7 @@ impl> NoteHi if let Some(new_validators) = new_validators_and_id { let count = new_validators.len() as ValidatorCount; - match ProvingTrie::::generate_for(new_validators) { + match ValidatorProvingTrie::::generate_for(new_validators) { Ok(trie) => >::insert(new_index, &(trie.root, count)), Err(reason) => { print("Failed to generate historical ancestry-inclusion proof."); @@ -221,13 +222,15 @@ pub type IdentificationTuple = (::ValidatorId, ::FullIdentification); /// A trie instance for checking and generating proofs. -pub struct ProvingTrie { +pub struct ValidatorProvingTrie { db: MemoryDB, root: T::Hash, } -impl ProvingTrie { - fn generate_for(validators: I) -> Result +impl ProvingTrie> + for ValidatorProvingTrie +{ + fn generate_for(validators: I) -> Result where I: IntoIterator, { @@ -261,22 +264,11 @@ impl ProvingTrie { } } - Ok(ProvingTrie { db, root }) - } - - fn from_nodes(root: T::Hash, nodes: &[Vec]) -> Self { - use sp_trie::HashDBT; - - let mut memory_db = MemoryDB::default(); - for node in nodes { - HashDBT::insert(&mut memory_db, EMPTY_PREFIX, &node[..]); - } - - ProvingTrie { db: memory_db, root } + Ok(Self { db, root }) } /// Prove the full verification data for a given key and key ID. - pub fn prove(&self, key_id: KeyTypeId, key_data: &[u8]) -> Option>> { + fn prove(&self, key_id: KeyTypeId, key_data: &[u8]) -> Option>> { let mut recorder = Recorder::>::new(); { let trie = @@ -296,7 +288,7 @@ impl ProvingTrie { } /// Access the underlying trie root. - pub fn root(&self) -> &T::Hash { + fn root(&self) -> &T::Hash { &self.root } @@ -316,6 +308,19 @@ impl ProvingTrie { } } +impl ValidatorProvingTrie { + fn from_nodes(root: T::Hash, nodes: &[Vec]) -> Self { + use sp_trie::HashDBT; + + let mut memory_db = MemoryDB::default(); + for node in nodes { + HashDBT::insert(&mut memory_db, EMPTY_PREFIX, &node[..]); + } + + Self { db: memory_db, root } + } +} + impl> KeyOwnerProofSystem<(KeyTypeId, D)> for Pallet { type Proof = MembershipProof; type IdentificationTuple = IdentificationTuple; @@ -332,7 +337,7 @@ impl> KeyOwnerProofSystem<(KeyTypeId, D)> for Pallet::generate_for(validators).ok()?; + let trie = ValidatorProvingTrie::::generate_for(validators).ok()?; let (id, data) = key; trie.prove(id, data.as_ref()).map(|trie_nodes| MembershipProof { @@ -364,7 +369,7 @@ impl> KeyOwnerProofSystem<(KeyTypeId, D)> for Pallet::from_nodes(root, &proof.trie_nodes); + let trie = ValidatorProvingTrie::::from_nodes(root, &proof.trie_nodes); trie.query(id, data.as_ref()) } } diff --git a/substrate/frame/session/src/historical/offchain.rs b/substrate/frame/session/src/historical/offchain.rs index 95f4d762949e..5a5a1114b9bf 100644 --- a/substrate/frame/session/src/historical/offchain.rs +++ b/substrate/frame/session/src/historical/offchain.rs @@ -20,17 +20,18 @@ //! Validator-set extracting an iterator from an off-chain worker stored list containing historical //! validator-sets. Based on the logic of historical slashing, but the validation is done off-chain. //! Use [`fn store_current_session_validator_set_to_offchain()`](super::onchain) to store the -//! required data to the offchain validator set. This is used in conjunction with [`ProvingTrie`] -//! and the off-chain indexing API. +//! required data to the offchain validator set. This is used in conjunction with +//! [`ValidatorProvingTrie`] and the off-chain indexing API. use sp_runtime::{ offchain::storage::{MutateStorageError, StorageRetrievalError, StorageValueRef}, + proving_trie::ProvingTrie, KeyTypeId, }; use sp_session::MembershipProof; use sp_std::prelude::*; -use super::{shared, Config, IdentificationTuple, ProvingTrie}; +use super::{shared, Config, IdentificationTuple, ValidatorProvingTrie}; use crate::{Pallet as SessionModule, SessionIndex}; /// A set of validators, which was used for a fixed session index. @@ -59,7 +60,7 @@ impl ValidatorSet { } /// Implement conversion into iterator for usage -/// with [ProvingTrie](super::ProvingTrie::generate_for). +/// with [ValidatorProvingTrie](super::ValidatorProvingTrie::generate_for). impl sp_std::iter::IntoIterator for ValidatorSet { type Item = (T::ValidatorId, T::FullIdentification); type IntoIter = sp_std::vec::IntoIter; @@ -79,7 +80,7 @@ pub fn prove_session_membership>( ) -> Option { let validators = ValidatorSet::::load_from_offchain_db(session_index)?; let count = validators.len() as u32; - let trie = ProvingTrie::::generate_for(validators.into_iter()).ok()?; + let trie = ValidatorProvingTrie::::generate_for(validators.into_iter()).ok()?; let (id, data) = session_key; trie.prove(id, data.as_ref()).map(|trie_nodes| MembershipProof {