mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-16 20:11:03 +00:00
Run cargo fmt on the whole code base (#9394)
* Run cargo fmt on the whole code base * Second run * Add CI check * Fix compilation * More unnecessary braces * Handle weights * Use --all * Use correct attributes... * Fix UI tests * AHHHHHHHHH * 🤦 * Docs * Fix compilation * 🤷 * Please stop * 🤦 x 2 * More * make rustfmt.toml consistent with polkadot Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
@@ -26,22 +26,27 @@
|
||||
//! These roots and proofs of inclusion can be generated at any time during the current session.
|
||||
//! Afterwards, the proofs can be fed to a consensus module when reporting misbehavior.
|
||||
|
||||
use sp_std::prelude::*;
|
||||
use codec::{Encode, Decode};
|
||||
use sp_runtime::KeyTypeId;
|
||||
use sp_runtime::traits::{Convert, OpaqueKeys};
|
||||
use sp_session::{MembershipProof, ValidatorCount};
|
||||
use super::{Module as SessionModule, SessionIndex};
|
||||
use codec::{Decode, Encode};
|
||||
use frame_support::{
|
||||
decl_module, decl_storage, Parameter, print,
|
||||
decl_module, decl_storage, print,
|
||||
traits::{ValidatorSet, ValidatorSetWithIdentification},
|
||||
Parameter,
|
||||
};
|
||||
use sp_runtime::{
|
||||
traits::{Convert, OpaqueKeys},
|
||||
KeyTypeId,
|
||||
};
|
||||
use sp_session::{MembershipProof, ValidatorCount};
|
||||
use sp_std::prelude::*;
|
||||
use sp_trie::{
|
||||
trie_types::{TrieDB, TrieDBMut},
|
||||
MemoryDB, Recorder, Trie, TrieMut, EMPTY_PREFIX,
|
||||
};
|
||||
use sp_trie::{MemoryDB, Trie, TrieMut, Recorder, EMPTY_PREFIX};
|
||||
use sp_trie::trie_types::{TrieDBMut, TrieDB};
|
||||
use super::{SessionIndex, Module as SessionModule};
|
||||
|
||||
mod shared;
|
||||
pub mod offchain;
|
||||
pub mod onchain;
|
||||
mod shared;
|
||||
|
||||
/// Config necessary for the historical module.
|
||||
pub trait Config: super::Config {
|
||||
@@ -165,7 +170,7 @@ impl<T: Config, I: SessionManager<T::ValidatorId, T::FullIdentification>> NoteHi
|
||||
Err(reason) => {
|
||||
print("Failed to generate historical ancestry-inclusion proof.");
|
||||
print(reason);
|
||||
}
|
||||
},
|
||||
};
|
||||
} else {
|
||||
let previous_index = new_index.saturating_sub(1);
|
||||
@@ -201,7 +206,8 @@ where
|
||||
}
|
||||
|
||||
/// A tuple of the validator's ID and their full identification.
|
||||
pub type IdentificationTuple<T> = (<T as crate::Config>::ValidatorId, <T as Config>::FullIdentification);
|
||||
pub type IdentificationTuple<T> =
|
||||
(<T as crate::Config>::ValidatorId, <T as Config>::FullIdentification);
|
||||
|
||||
/// A trie instance for checking and generating proofs.
|
||||
pub struct ProvingTrie<T: Config> {
|
||||
@@ -211,7 +217,8 @@ pub struct ProvingTrie<T: Config> {
|
||||
|
||||
impl<T: Config> ProvingTrie<T> {
|
||||
fn generate_for<I>(validators: I) -> Result<Self, &'static str>
|
||||
where I: IntoIterator<Item=(T::ValidatorId, T::FullIdentification)>
|
||||
where
|
||||
I: IntoIterator<Item = (T::ValidatorId, T::FullIdentification)>,
|
||||
{
|
||||
let mut db = MemoryDB::default();
|
||||
let mut root = Default::default();
|
||||
@@ -230,23 +237,20 @@ impl<T: Config> ProvingTrie<T> {
|
||||
// map each key to the owner index.
|
||||
for key_id in T::Keys::key_ids() {
|
||||
let key = keys.get_raw(*key_id);
|
||||
let res = (key_id, key).using_encoded(|k|
|
||||
i.using_encoded(|v| trie.insert(k, v))
|
||||
);
|
||||
let res =
|
||||
(key_id, key).using_encoded(|k| i.using_encoded(|v| trie.insert(k, v)));
|
||||
|
||||
let _ = res.map_err(|_| "failed to insert into trie")?;
|
||||
}
|
||||
|
||||
// map each owner index to the full identification.
|
||||
let _ = i.using_encoded(|k| full_id.using_encoded(|v| trie.insert(k, v)))
|
||||
let _ = i
|
||||
.using_encoded(|k| full_id.using_encoded(|v| trie.insert(k, v)))
|
||||
.map_err(|_| "failed to insert into trie")?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(ProvingTrie {
|
||||
db,
|
||||
root,
|
||||
})
|
||||
Ok(ProvingTrie { db, root })
|
||||
}
|
||||
|
||||
fn from_nodes(root: T::Hash, nodes: &[Vec<u8>]) -> Self {
|
||||
@@ -257,10 +261,7 @@ impl<T: Config> ProvingTrie<T> {
|
||||
HashDBT::insert(&mut memory_db, EMPTY_PREFIX, &node[..]);
|
||||
}
|
||||
|
||||
ProvingTrie {
|
||||
db: memory_db,
|
||||
root,
|
||||
}
|
||||
ProvingTrie { db: memory_db, root }
|
||||
}
|
||||
|
||||
/// Prove the full verification data for a given key and key ID.
|
||||
@@ -291,11 +292,13 @@ impl<T: Config> ProvingTrie<T> {
|
||||
// nodes within the current `MemoryDB` are insufficient to query the item.
|
||||
fn query(&self, key_id: KeyTypeId, key_data: &[u8]) -> Option<IdentificationTuple<T>> {
|
||||
let trie = TrieDB::new(&self.db, &self.root).ok()?;
|
||||
let val_idx = (key_id, key_data).using_encoded(|s| trie.get(s))
|
||||
let val_idx = (key_id, key_data)
|
||||
.using_encoded(|s| trie.get(s))
|
||||
.ok()?
|
||||
.and_then(|raw| u32::decode(&mut &*raw).ok())?;
|
||||
|
||||
val_idx.using_encoded(|s| trie.get(s))
|
||||
val_idx
|
||||
.using_encoded(|s| trie.get(s))
|
||||
.ok()?
|
||||
.and_then(|raw| <IdentificationTuple<T>>::decode(&mut &*raw).ok())
|
||||
}
|
||||
@@ -322,12 +325,11 @@ impl<T: Config, D: AsRef<[u8]>> frame_support::traits::KeyOwnerProofSystem<(KeyT
|
||||
let trie = ProvingTrie::<T>::generate_for(validators).ok()?;
|
||||
|
||||
let (id, data) = key;
|
||||
trie.prove(id, data.as_ref())
|
||||
.map(|trie_nodes| MembershipProof {
|
||||
session,
|
||||
trie_nodes,
|
||||
validator_count: count,
|
||||
})
|
||||
trie.prove(id, data.as_ref()).map(|trie_nodes| MembershipProof {
|
||||
session,
|
||||
trie_nodes,
|
||||
validator_count: count,
|
||||
})
|
||||
}
|
||||
|
||||
fn check_proof(key: (KeyTypeId, D), proof: Self::Proof) -> Option<IdentificationTuple<T>> {
|
||||
@@ -339,7 +341,7 @@ impl<T: Config, D: AsRef<[u8]>> frame_support::traits::KeyOwnerProofSystem<(KeyT
|
||||
let count = <SessionModule<T>>::validators().len() as ValidatorCount;
|
||||
|
||||
if count != proof.validator_count {
|
||||
return None;
|
||||
return None
|
||||
}
|
||||
|
||||
Some((owner, id))
|
||||
@@ -349,7 +351,7 @@ impl<T: Config, D: AsRef<[u8]>> frame_support::traits::KeyOwnerProofSystem<(KeyT
|
||||
let (root, count) = <HistoricalSessions<T>>::get(&proof.session)?;
|
||||
|
||||
if count != proof.validator_count {
|
||||
return None;
|
||||
return None
|
||||
}
|
||||
|
||||
let trie = ProvingTrie::<T>::from_nodes(root, &proof.trie_nodes);
|
||||
@@ -361,22 +363,22 @@ impl<T: Config, D: AsRef<[u8]>> frame_support::traits::KeyOwnerProofSystem<(KeyT
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests {
|
||||
use super::*;
|
||||
use sp_runtime::key_types::DUMMY;
|
||||
use sp_runtime::testing::UintAuthorityId;
|
||||
use crate::mock::{
|
||||
NEXT_VALIDATORS, force_new_session,
|
||||
set_next_validators, Test, System, Session,
|
||||
force_new_session, set_next_validators, Session, System, Test, NEXT_VALIDATORS,
|
||||
};
|
||||
use frame_support::traits::{KeyOwnerProofSystem, OnInitialize};
|
||||
use frame_support::BasicExternalities;
|
||||
use frame_support::{
|
||||
traits::{KeyOwnerProofSystem, OnInitialize},
|
||||
BasicExternalities,
|
||||
};
|
||||
use sp_runtime::{key_types::DUMMY, testing::UintAuthorityId};
|
||||
|
||||
type Historical = Module<Test>;
|
||||
|
||||
pub(crate) fn new_test_ext() -> sp_io::TestExternalities {
|
||||
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
let keys: Vec<_> = NEXT_VALIDATORS.with(|l|
|
||||
let keys: Vec<_> = NEXT_VALIDATORS.with(|l| {
|
||||
l.borrow().iter().cloned().map(|i| (i, i, UintAuthorityId(i).into())).collect()
|
||||
);
|
||||
});
|
||||
BasicExternalities::execute_with_storage(&mut t, || {
|
||||
for (ref k, ..) in &keys {
|
||||
frame_system::Pallet::<Test>::inc_providers(k);
|
||||
@@ -430,7 +432,6 @@ pub(crate) mod tests {
|
||||
|
||||
System::set_block_number(i);
|
||||
Session::on_initialize(i);
|
||||
|
||||
}
|
||||
|
||||
assert_eq!(StoredRange::get(), Some((0, 100)));
|
||||
@@ -461,7 +462,6 @@ pub(crate) mod tests {
|
||||
|
||||
System::set_block_number(i);
|
||||
Session::on_initialize(i);
|
||||
|
||||
}
|
||||
|
||||
assert_eq!(StoredRange::get(), Some((100, 200)));
|
||||
|
||||
@@ -27,17 +27,18 @@
|
||||
|
||||
use sp_runtime::{
|
||||
offchain::storage::{MutateStorageError, StorageRetrievalError, StorageValueRef},
|
||||
KeyTypeId
|
||||
KeyTypeId,
|
||||
};
|
||||
use sp_session::MembershipProof;
|
||||
|
||||
use super::super::{Pallet as SessionModule, SessionIndex};
|
||||
use super::{IdentificationTuple, ProvingTrie, Config};
|
||||
use super::{
|
||||
super::{Pallet as SessionModule, SessionIndex},
|
||||
Config, IdentificationTuple, ProvingTrie,
|
||||
};
|
||||
|
||||
use super::shared;
|
||||
use sp_std::prelude::*;
|
||||
|
||||
|
||||
/// A set of validators, which was used for a fixed session index.
|
||||
struct ValidatorSet<T: Config> {
|
||||
validator_set: Vec<IdentificationTuple<T>>,
|
||||
@@ -87,15 +88,13 @@ pub fn prove_session_membership<T: Config, D: AsRef<[u8]>>(
|
||||
let trie = ProvingTrie::<T>::generate_for(validators.into_iter()).ok()?;
|
||||
|
||||
let (id, data) = session_key;
|
||||
trie.prove(id, data.as_ref())
|
||||
.map(|trie_nodes| MembershipProof {
|
||||
session: session_index,
|
||||
trie_nodes,
|
||||
validator_count: count,
|
||||
})
|
||||
trie.prove(id, data.as_ref()).map(|trie_nodes| MembershipProof {
|
||||
session: session_index,
|
||||
trie_nodes,
|
||||
validator_count: count,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
/// Attempt to prune anything that is older than `first_to_keep` session index.
|
||||
///
|
||||
/// Due to re-organisation it could be that the `first_to_keep` might be less
|
||||
@@ -104,18 +103,20 @@ pub fn prove_session_membership<T: Config, D: AsRef<[u8]>>(
|
||||
pub fn prune_older_than<T: Config>(first_to_keep: SessionIndex) {
|
||||
let derived_key = shared::LAST_PRUNE.to_vec();
|
||||
let entry = StorageValueRef::persistent(derived_key.as_ref());
|
||||
match entry.mutate(|current: Result<Option<SessionIndex>, StorageRetrievalError>| -> Result<_, ()> {
|
||||
match current {
|
||||
Ok(Some(current)) if current < first_to_keep => Ok(first_to_keep),
|
||||
// do not move the cursor, if the new one would be behind ours
|
||||
Ok(Some(current)) => Ok(current),
|
||||
Ok(None) => Ok(first_to_keep),
|
||||
// if the storage contains undecodable data, overwrite with current anyways
|
||||
// which might leak some entries being never purged, but that is acceptable
|
||||
// in this context
|
||||
Err(_) => Ok(first_to_keep),
|
||||
}
|
||||
}) {
|
||||
match entry.mutate(
|
||||
|current: Result<Option<SessionIndex>, StorageRetrievalError>| -> Result<_, ()> {
|
||||
match current {
|
||||
Ok(Some(current)) if current < first_to_keep => Ok(first_to_keep),
|
||||
// do not move the cursor, if the new one would be behind ours
|
||||
Ok(Some(current)) => Ok(current),
|
||||
Ok(None) => Ok(first_to_keep),
|
||||
// if the storage contains undecodable data, overwrite with current anyways
|
||||
// which might leak some entries being never purged, but that is acceptable
|
||||
// in this context
|
||||
Err(_) => Ok(first_to_keep),
|
||||
}
|
||||
},
|
||||
) {
|
||||
Ok(new_value) => {
|
||||
// on a re-org this is not necessarily true, with the above they might be equal
|
||||
if new_value < first_to_keep {
|
||||
@@ -124,9 +125,9 @@ pub fn prune_older_than<T: Config>(first_to_keep: SessionIndex) {
|
||||
let _ = StorageValueRef::persistent(derived_key.as_ref()).clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(MutateStorageError::ConcurrentModification(_)) => {}
|
||||
Err(MutateStorageError::ValueFunctionFailed(_)) => {}
|
||||
},
|
||||
Err(MutateStorageError::ConcurrentModification(_)) => {},
|
||||
Err(MutateStorageError::ValueFunctionFailed(_)) => {},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,23 +142,22 @@ pub fn keep_newest<T: Config>(n_to_keep: usize) {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::{onchain, Module};
|
||||
use super::*;
|
||||
use super::{
|
||||
super::{onchain, Module},
|
||||
*,
|
||||
};
|
||||
use crate::mock::{
|
||||
force_new_session, set_next_validators, Session, System, Test, NEXT_VALIDATORS,
|
||||
};
|
||||
use codec::Encode;
|
||||
use frame_support::traits::{KeyOwnerProofSystem, OnInitialize};
|
||||
use sp_core::crypto::key_types::DUMMY;
|
||||
use sp_core::offchain::{
|
||||
testing::TestOffchainExt,
|
||||
OffchainDbExt,
|
||||
OffchainWorkerExt,
|
||||
StorageKind,
|
||||
use sp_core::{
|
||||
crypto::key_types::DUMMY,
|
||||
offchain::{testing::TestOffchainExt, OffchainDbExt, OffchainWorkerExt, StorageKind},
|
||||
};
|
||||
|
||||
use sp_runtime::testing::UintAuthorityId;
|
||||
use frame_support::BasicExternalities;
|
||||
use sp_runtime::testing::UintAuthorityId;
|
||||
|
||||
type Historical = Module<Test>;
|
||||
|
||||
@@ -166,16 +166,16 @@ mod tests {
|
||||
.build_storage::<Test>()
|
||||
.expect("Failed to create test externalities.");
|
||||
|
||||
let keys: Vec<_> = NEXT_VALIDATORS.with(|l|
|
||||
let keys: Vec<_> = NEXT_VALIDATORS.with(|l| {
|
||||
l.borrow().iter().cloned().map(|i| (i, i, UintAuthorityId(i).into())).collect()
|
||||
);
|
||||
});
|
||||
BasicExternalities::execute_with_storage(&mut t, || {
|
||||
for (ref k, ..) in &keys {
|
||||
frame_system::Pallet::<Test>::inc_providers(k);
|
||||
}
|
||||
});
|
||||
|
||||
crate::GenesisConfig::<Test>{ keys }.assimilate_storage(&mut t).unwrap();
|
||||
crate::GenesisConfig::<Test> { keys }.assimilate_storage(&mut t).unwrap();
|
||||
|
||||
let mut ext = sp_io::TestExternalities::new(t);
|
||||
|
||||
@@ -193,13 +193,13 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn encode_decode_roundtrip() {
|
||||
use super::super::{super::Config as SessionConfig, Config as HistoricalConfig};
|
||||
use codec::{Decode, Encode};
|
||||
use super::super::super::Config as SessionConfig;
|
||||
use super::super::Config as HistoricalConfig;
|
||||
|
||||
let sample = (
|
||||
22u32 as <Test as SessionConfig>::ValidatorId,
|
||||
7_777_777 as <Test as HistoricalConfig>::FullIdentification);
|
||||
22u32 as <Test as SessionConfig>::ValidatorId,
|
||||
7_777_777 as <Test as HistoricalConfig>::FullIdentification,
|
||||
);
|
||||
|
||||
let encoded = sample.encode();
|
||||
let decoded = Decode::decode(&mut encoded.as_slice()).expect("Must decode");
|
||||
@@ -210,7 +210,7 @@ mod tests {
|
||||
fn onchain_to_offchain() {
|
||||
let mut ext = new_test_ext();
|
||||
|
||||
const DATA: &[u8] = &[7,8,9,10,11];
|
||||
const DATA: &[u8] = &[7, 8, 9, 10, 11];
|
||||
ext.execute_with(|| {
|
||||
b"alphaomega"[..].using_encoded(|key| sp_io::offchain_index::set(key, DATA));
|
||||
});
|
||||
@@ -218,15 +218,13 @@ mod tests {
|
||||
ext.persist_offchain_overlay();
|
||||
|
||||
ext.execute_with(|| {
|
||||
let data =
|
||||
b"alphaomega"[..].using_encoded(|key| {
|
||||
let data = b"alphaomega"[..].using_encoded(|key| {
|
||||
sp_io::offchain::local_storage_get(StorageKind::PERSISTENT, key)
|
||||
});
|
||||
assert_eq!(data, Some(DATA.to_vec()));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn historical_proof_offchain() {
|
||||
let mut ext = new_test_ext();
|
||||
@@ -251,8 +249,6 @@ mod tests {
|
||||
ext.persist_offchain_overlay();
|
||||
|
||||
ext.execute_with(|| {
|
||||
|
||||
|
||||
System::set_block_number(2);
|
||||
Session::on_initialize(2);
|
||||
assert_eq!(<SessionModule<Test>>::current_index(), 2);
|
||||
|
||||
@@ -20,9 +20,10 @@
|
||||
use codec::Encode;
|
||||
use sp_runtime::traits::Convert;
|
||||
|
||||
use super::super::Config as SessionConfig;
|
||||
use super::super::{Pallet as SessionModule, SessionIndex};
|
||||
use super::Config as HistoricalConfig;
|
||||
use super::{
|
||||
super::{Config as SessionConfig, Pallet as SessionModule, SessionIndex},
|
||||
Config as HistoricalConfig,
|
||||
};
|
||||
|
||||
use super::shared;
|
||||
use sp_std::prelude::*;
|
||||
|
||||
@@ -18,10 +18,9 @@
|
||||
//! Shared logic between on-chain and off-chain components used for slashing using an off-chain
|
||||
//! worker.
|
||||
|
||||
|
||||
use super::SessionIndex;
|
||||
use sp_std::prelude::*;
|
||||
use codec::Encode;
|
||||
use sp_std::prelude::*;
|
||||
|
||||
pub(super) const PREFIX: &[u8] = b"session_historical";
|
||||
pub(super) const LAST_PRUNE: &[u8] = b"session_historical_last_prune";
|
||||
@@ -30,10 +29,11 @@ pub(super) const LAST_PRUNE: &[u8] = b"session_historical_last_prune";
|
||||
pub(super) fn derive_key<P: AsRef<[u8]>>(prefix: P, session_index: SessionIndex) -> Vec<u8> {
|
||||
let prefix: &[u8] = prefix.as_ref();
|
||||
session_index.using_encoded(|encoded_session_index| {
|
||||
prefix.into_iter()
|
||||
prefix
|
||||
.into_iter()
|
||||
.chain(b"/".into_iter())
|
||||
.chain(encoded_session_index.into_iter())
|
||||
.copied()
|
||||
.collect::<Vec<u8>>()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user