mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 05:47:58 +00:00
Redesign Democracy pallet (#5294)
* Repot a bit of democracy code * Basic logic is drafted * Lazy democracy builds. * Add non-locked split-voting and instant-scheduling. * Introduce delegation that works. * Builds again. * Indentation * Building. * Docs and migration * Fix half of the tests * Fix up & repot tests * Fix runtime build * Update docs * Docs * Nits. * Turnout counts full capital * Delegations could towards capital * proxy delegation & proxy unvoting * Fix * Tests for split-voting * Add missing file * Persistent locking.
This commit is contained in:
@@ -72,7 +72,7 @@ pub use self::hash::{
|
||||
};
|
||||
pub use self::storage::{
|
||||
StorageValue, StorageMap, StorageDoubleMap, StoragePrefixedMap, IterableStorageMap,
|
||||
IterableStorageDoubleMap,
|
||||
IterableStorageDoubleMap, migration
|
||||
};
|
||||
pub use self::dispatch::{Parameter, Callable, IsSubType};
|
||||
pub use sp_runtime::{self, ConsensusEngineId, print, traits::Printable};
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
use sp_std::prelude::*;
|
||||
use codec::{Encode, Decode};
|
||||
use crate::{StorageHasher, Twox128};
|
||||
use crate::hash::ReversibleStorageHasher;
|
||||
|
||||
/// Utility to iterate through raw items in storage.
|
||||
pub struct StorageIterator<T> {
|
||||
@@ -78,6 +79,72 @@ impl<T: Decode + Sized> Iterator for StorageIterator<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Utility to iterate through raw items in storage.
|
||||
pub struct StorageKeyIterator<K, T, H: ReversibleStorageHasher> {
|
||||
prefix: Vec<u8>,
|
||||
previous_key: Vec<u8>,
|
||||
drain: bool,
|
||||
_phantom: ::sp_std::marker::PhantomData<(K, T, H)>,
|
||||
}
|
||||
|
||||
impl<K, T, H: ReversibleStorageHasher> StorageKeyIterator<K, T, H> {
|
||||
/// Construct iterator to iterate over map items in `module` for the map called `item`.
|
||||
pub fn new(module: &[u8], item: &[u8]) -> Self {
|
||||
Self::with_suffix(module, item, &[][..])
|
||||
}
|
||||
|
||||
/// Construct iterator to iterate over map items in `module` for the map called `item`.
|
||||
pub fn with_suffix(module: &[u8], item: &[u8], suffix: &[u8]) -> Self {
|
||||
let mut prefix = Vec::new();
|
||||
prefix.extend_from_slice(&Twox128::hash(module));
|
||||
prefix.extend_from_slice(&Twox128::hash(item));
|
||||
prefix.extend_from_slice(suffix);
|
||||
let previous_key = prefix.clone();
|
||||
Self { prefix, previous_key, drain: false, _phantom: Default::default() }
|
||||
}
|
||||
|
||||
/// Mutate this iterator into a draining iterator; items iterated are removed from storage.
|
||||
pub fn drain(mut self) -> Self {
|
||||
self.drain = true;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<K: Decode + Sized, T: Decode + Sized, H: ReversibleStorageHasher> Iterator
|
||||
for StorageKeyIterator<K, T, H>
|
||||
{
|
||||
type Item = (K, T);
|
||||
|
||||
fn next(&mut self) -> Option<(K, T)> {
|
||||
loop {
|
||||
let maybe_next = sp_io::storage::next_key(&self.previous_key)
|
||||
.filter(|n| n.starts_with(&self.prefix));
|
||||
break match maybe_next {
|
||||
Some(next) => {
|
||||
self.previous_key = next.clone();
|
||||
let mut key_material = H::reverse(&next[self.prefix.len()..]);
|
||||
match K::decode(&mut key_material) {
|
||||
Ok(key) => {
|
||||
let maybe_value = frame_support::storage::unhashed::get::<T>(&next);
|
||||
match maybe_value {
|
||||
Some(value) => {
|
||||
if self.drain {
|
||||
frame_support::storage::unhashed::kill(&next);
|
||||
}
|
||||
Some((key, value))
|
||||
}
|
||||
None => continue,
|
||||
}
|
||||
}
|
||||
Err(_) => continue,
|
||||
}
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a particular value in storage by the `module`, the map's `item` name and the key `hash`.
|
||||
pub fn have_storage_value(module: &[u8], item: &[u8], hash: &[u8]) -> bool {
|
||||
get_storage_value::<()>(module, item, hash).is_some()
|
||||
@@ -109,3 +176,12 @@ pub fn put_storage_value<T: Encode>(module: &[u8], item: &[u8], hash: &[u8], val
|
||||
key[32..].copy_from_slice(hash);
|
||||
frame_support::storage::unhashed::put(&key, &value);
|
||||
}
|
||||
|
||||
/// Get a particular value in storage by the `module`, the map's `item` name and the key `hash`.
|
||||
pub fn remove_storage_prefix(module: &[u8], item: &[u8], hash: &[u8]) {
|
||||
let mut key = vec![0u8; 32 + hash.len()];
|
||||
key[0..16].copy_from_slice(&Twox128::hash(module));
|
||||
key[16..32].copy_from_slice(&Twox128::hash(item));
|
||||
key[32..].copy_from_slice(hash);
|
||||
frame_support::storage::unhashed::kill_prefix(&key)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user