Aggregate all liquidity restrictions in a single place (#1921)

* Clean up session key rotation

* Fix build

* Bump version

* Introduce feature to balances.

* Move staking locking logic over to central point

* ^^^ rest

* First part of assimilation

* More assimilation

* More assimilation

* Fix most tests

* Fix build

* Move Balances to new locking system

* :q!

* Bump runtime version

* Build runtime

* Convenience function

* Test fix.

* Whitespace

* Improve type legibility.

* Fix comment.

* More tests.

* More tests.

* Bump version

* Caps

* Whitespace

* Whitespace

* Remove unneeded function.
This commit is contained in:
Gav Wood
2019-03-06 12:46:17 +01:00
committed by GitHub
parent 46541ec73c
commit ccc11974ee
62 changed files with 795 additions and 346 deletions
+29 -11
View File
@@ -27,7 +27,7 @@ pub use parity_codec as codec;
pub use serde_derive;
#[cfg(feature = "std")]
use std::collections::HashMap;
pub use runtime_io::{StorageOverlay, ChildrenStorageOverlay};
use rstd::prelude::*;
use substrate_primitives::hash::{H256, H512};
@@ -87,17 +87,9 @@ pub use serde::{Serialize, de::DeserializeOwned};
#[cfg(feature = "std")]
use serde_derive::{Serialize, Deserialize};
/// A set of key value pairs for storage.
#[cfg(feature = "std")]
pub type StorageOverlay = HashMap<Vec<u8>, Vec<u8>>;
/// A set of key value pairs for children storage;
#[cfg(feature = "std")]
pub type ChildrenStorageOverlay = HashMap<Vec<u8>, StorageOverlay>;
/// Complex storage builder stuff.
#[cfg(feature = "std")]
pub trait BuildStorage {
pub trait BuildStorage: Sized {
/// Hash given slice.
///
/// Default to xx128 hashing.
@@ -107,7 +99,21 @@ pub trait BuildStorage {
r
}
/// Build the storage out of this builder.
fn build_storage(self) -> Result<(StorageOverlay, ChildrenStorageOverlay), String>;
fn build_storage(self) -> Result<(StorageOverlay, ChildrenStorageOverlay), String> {
let mut storage = Default::default();
let mut child_storage = Default::default();
self.assimilate_storage(&mut storage, &mut child_storage)?;
Ok((storage, child_storage))
}
/// Assimilate the storage for this module into pre-existing overlays.
fn assimilate_storage(self, storage: &mut StorageOverlay, child_storage: &mut ChildrenStorageOverlay) -> Result<(), String> {
let (s, cs) = self.build_storage()?;
storage.extend(s);
for (other_child_key, other_child_map) in cs {
child_storage.entry(other_child_key).or_default().extend(other_child_map);
}
Ok(())
}
}
#[cfg(feature = "std")]
@@ -115,6 +121,10 @@ impl BuildStorage for StorageOverlay {
fn build_storage(self) -> Result<(StorageOverlay, ChildrenStorageOverlay), String> {
Ok((self, Default::default()))
}
fn assimilate_storage(self, storage: &mut StorageOverlay, _child_storage: &mut ChildrenStorageOverlay) -> Result<(), String> {
storage.extend(self);
Ok(())
}
}
/// Permill is parts-per-million (i.e. after multiplying by this, divide by 1000000).
@@ -411,6 +421,14 @@ macro_rules! impl_outer_config {
}
#[cfg(any(feature = "std", test))]
impl $crate::BuildStorage for $main {
fn assimilate_storage(self, top: &mut $crate::StorageOverlay, children: &mut $crate::ChildrenStorageOverlay) -> ::std::result::Result<(), String> {
$(
if let Some(extra) = self.$snake {
extra.assimilate_storage(top, children)?;
}
)*
Ok(())
}
fn build_storage(self) -> ::std::result::Result<($crate::StorageOverlay, $crate::ChildrenStorageOverlay), String> {
let mut top = $crate::StorageOverlay::new();
let mut children = $crate::ChildrenStorageOverlay::new();
+12 -5
View File
@@ -23,8 +23,7 @@ use runtime_io;
#[cfg(feature = "std")] use serde::{Serialize, de::DeserializeOwned};
#[cfg(feature = "std")]
use serde_derive::{Serialize, Deserialize};
use substrate_primitives;
use substrate_primitives::Blake2Hasher;
use substrate_primitives::{self, Hasher, Blake2Hasher};
use crate::codec::{Codec, Encode, HasCompact};
pub use integer_sqrt::IntegerSquareRoot;
pub use num_traits::{
@@ -84,6 +83,8 @@ pub trait StaticLookup {
type Target;
/// Attempt a lookup.
fn lookup(s: Self::Source) -> result::Result<Self::Target, &'static str>;
/// Convert from Target back to Source.
fn unlookup(t: Self::Target) -> Self::Source;
}
/// A lookup implementation returning the input value.
@@ -93,6 +94,7 @@ impl<T: Codec + Clone + PartialEq + MaybeDebug> StaticLookup for IdentityLookup<
type Source = T;
type Target = T;
fn lookup(x: T) -> result::Result<T, &'static str> { Ok(x) }
fn unlookup(x: T) -> T { x }
}
impl<T> Lookup for IdentityLookup<T> {
type Source = T;
@@ -184,7 +186,7 @@ pub trait SimpleArithmetic:
CheckedMul +
CheckedDiv +
Saturating +
PartialOrd<Self> + Ord +
PartialOrd<Self> + Ord + Bounded +
HasCompact
{}
impl<T:
@@ -202,7 +204,7 @@ impl<T:
CheckedMul +
CheckedDiv +
Saturating +
PartialOrd<Self> + Ord +
PartialOrd<Self> + Ord + Bounded +
HasCompact
> SimpleArithmetic for T {}
@@ -298,7 +300,10 @@ tuple_impl!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W,
pub trait Hash: 'static + MaybeSerializeDebug + Clone + Eq + PartialEq { // Stupid bug in the Rust compiler believes derived
// traits must be fulfilled by all type parameters.
/// The hash type produced.
type Output: Member + MaybeSerializeDebug + AsRef<[u8]> + AsMut<[u8]>;
type Output: Member + MaybeSerializeDebug + rstd::hash::Hash + AsRef<[u8]> + AsMut<[u8]> + Copy + Default;
/// The associated hash_db Hasher type.
type Hasher: Hasher<Out=Self::Output>;
/// Produce the hash of some byte-slice.
fn hash(s: &[u8]) -> Self::Output;
@@ -338,6 +343,7 @@ pub struct BlakeTwo256;
impl Hash for BlakeTwo256 {
type Output = substrate_primitives::H256;
type Hasher = Blake2Hasher;
fn hash(s: &[u8]) -> Self::Output {
runtime_io::blake2_256(s).into()
}
@@ -480,6 +486,7 @@ pub trait MaybeHash {}
#[cfg(not(feature = "std"))]
impl<T> MaybeHash for T {}
/// A type that can be used in runtime structures.
pub trait Member: Send + Sync + Sized + MaybeDebug + Eq + PartialEq + Clone + 'static {}
impl<T: Send + Sync + Sized + MaybeDebug + Eq + PartialEq + Clone + 'static> Member for T {}