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
-1
View File
@@ -189,7 +189,6 @@ dependencies = [
"parity-codec 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"primitive-types 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-hex 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-std 0.1.0",
]
+2 -2
View File
@@ -8,7 +8,7 @@ edition = "2018"
rstd = { package = "sr-std", path = "../sr-std", default-features = false }
parity-codec = { version = "3.1", default-features = false, features = ["derive"] }
rustc-hex = { version = "2.0", default-features = false }
serde = { version = "1.0", default-features = false }
serde = { version = "1.0", optional = true }
serde_derive = { version = "1.0", optional = true }
twox-hash = { version = "1.1.0", optional = true }
byteorder = { version = "1.1", default-features = false }
@@ -46,7 +46,7 @@ std = [
"hash256-std-hasher/std",
"hash-db/std",
"rstd/std",
"serde/std",
"serde",
"rustc-hex/std",
"twox-hash",
"blake2-rfc",
+1 -1
View File
@@ -5,5 +5,5 @@ authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
serde = { version = "1.0", default-features = false }
serde = "1.0"
serde_json = "1.0"
+30 -9
View File
@@ -17,19 +17,29 @@
#[doc(hidden)]
pub use parity_codec as codec;
// re-export hashing functions.
pub use primitives::{blake2_256, twox_128, twox_256, ed25519, Blake2Hasher, sr25519};
pub use primitives::{
blake2_256, twox_128, twox_256, ed25519, Blake2Hasher, sr25519
};
pub use tiny_keccak::keccak256 as keccak_256;
// Switch to this after PoC-3
// pub use primitives::BlakeHasher;
pub use substrate_state_machine::{Externalities, TestExternalities};
pub use substrate_state_machine::{Externalities, BasicExternalities, TestExternalities};
use environmental::{environmental, thread_local_impl};
use primitives::hexdisplay::HexDisplay;
use primitives::H256;
use primitives::{hexdisplay::HexDisplay, H256};
use hash_db::Hasher;
#[cfg(feature = "std")]
use std::collections::HashMap;
environmental!(ext: trait Externalities<Blake2Hasher>);
/// A set of key value pairs for storage.
pub type StorageOverlay = HashMap<Vec<u8>, Vec<u8>>;
/// A set of key value pairs for children storage;
pub type ChildrenStorageOverlay = HashMap<Vec<u8>, StorageOverlay>;
/// Get `key` from storage and return a `Vec`, empty if there's a problem.
pub fn storage(key: &[u8]) -> Option<Vec<u8>> {
ext::with(|ext| ext.storage(key).map(|s| s.to_vec()))
@@ -196,7 +206,7 @@ pub fn sr25519_verify<P: AsRef<[u8]>>(sig: &[u8; 64], msg: &[u8], pubkey: P) ->
/// Verify and recover a SECP256k1 ECDSA signature.
/// - `sig` is passed in RSV format. V should be either 0/1 or 27/28.
/// - returns `Err` if the signatue is bad, otherwise the 64-byte pubkey (doesn't include the 0x04 prefix).
/// - returns `Err` if the signature is bad, otherwise the 64-byte pubkey (doesn't include the 0x04 prefix).
pub fn secp256k1_ecdsa_recover(sig: &[u8; 65], msg: &[u8; 32]) -> Result<[u8; 64], EcdsaVerifyError> {
let rs = secp256k1::Signature::parse_slice(&sig[0..64]).map_err(|_| EcdsaVerifyError::BadRS)?;
let v = secp256k1::RecoveryId::parse(if sig[64] > 26 { sig[64] - 27 } else { sig[64] } as u8).map_err(|_| EcdsaVerifyError::BadV)?;
@@ -213,6 +223,17 @@ pub fn with_externalities<R, F: FnOnce() -> R>(ext: &mut Externalities<Blake2Has
ext::using(ext, f)
}
/// Execute the given closure with global functions available whose functionality routes into
/// externalities that draw from and populate `storage`. Forwards the value that the closure returns.
pub fn with_storage<R, F: FnOnce() -> R>(storage: &mut StorageOverlay, f: F) -> R {
let mut alt_storage = Default::default();
rstd::mem::swap(&mut alt_storage, storage);
let mut ext: BasicExternalities = alt_storage.into();
let r = ext::using(&mut ext, f);
*storage = ext.into();
r
}
/// Trait for things which can be printed.
pub trait Printable {
fn print(self);
@@ -248,7 +269,7 @@ mod std_tests {
#[test]
fn storage_works() {
let mut t = TestExternalities::<Blake2Hasher>::default();
let mut t = BasicExternalities::default();
assert!(with_externalities(&mut t, || {
assert_eq!(storage(b"hello"), None);
set_storage(b"hello", b"world");
@@ -258,7 +279,7 @@ mod std_tests {
true
}));
t = TestExternalities::new(map![b"foo".to_vec() => b"bar".to_vec()]);
t = BasicExternalities::new(map![b"foo".to_vec() => b"bar".to_vec()]);
assert!(!with_externalities(&mut t, || {
assert_eq!(storage(b"hello"), None);
@@ -269,7 +290,7 @@ mod std_tests {
#[test]
fn read_storage_works() {
let mut t = TestExternalities::<Blake2Hasher>::new(map![
let mut t = BasicExternalities::new(map![
b":test".to_vec() => b"\x0b\0\0\0Hello world".to_vec()
]);
@@ -285,7 +306,7 @@ mod std_tests {
#[test]
fn clear_prefix_works() {
let mut t = TestExternalities::<Blake2Hasher>::new(map![
let mut t = BasicExternalities::new(map![
b":a".to_vec() => b"\x0b\0\0\0Hello world".to_vec(),
b":abcd".to_vec() => b"\x0b\0\0\0Hello world".to_vec(),
b":abc".to_vec() => b"\x0b\0\0\0Hello world".to_vec(),
+1 -1
View File
@@ -13,7 +13,7 @@ parity-codec = { version = "3.1", default-features = false, features = ["derive"
substrate-primitives = { path = "../primitives", default-features = false }
rstd = { package = "sr-std", path = "../sr-std", default-features = false }
runtime_io = { package = "sr-io", path = "../sr-io", default-features = false }
log = {version = "0.4", optional = true }
log = { version = "0.4", optional = true }
[dev-dependencies]
serde_json = "1.0"
+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 {}
+2 -2
View File
@@ -6,7 +6,7 @@ edition = "2018"
[dependencies]
impl-serde = { version = "0.1", optional = true }
serde = { version = "1.0", default-features = false }
serde = { version = "1.0", optional = true }
serde_derive = { version = "1.0", optional = true }
parity-codec = { version = "3.1", default-features = false, features = ["derive"] }
rstd = { package = "sr-std", path = "../sr-std", default-features = false }
@@ -16,7 +16,7 @@ runtime_primitives = { package = "sr-primitives", path = "../sr-primitives", def
default = ["std"]
std = [
"impl-serde",
"serde/std",
"serde",
"serde_derive",
"parity-codec/std",
"rstd/std",
+183
View File
@@ -0,0 +1,183 @@
// Copyright 2017-2018 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Basic implementation for Externalities.
use std::collections::HashMap;
use std::iter::FromIterator;
use hash_db::Hasher;
use heapsize::HeapSizeOf;
use trie::trie_root;
use primitives::storage::well_known_keys::{CHANGES_TRIE_CONFIG, CODE, HEAP_PAGES};
use parity_codec::Encode;
use super::{Externalities, OverlayedChanges};
/// Simple HashMap-based Externalities impl.
pub struct BasicExternalities {
inner: HashMap<Vec<u8>, Vec<u8>>,
changes: OverlayedChanges,
code: Option<Vec<u8>>,
}
impl BasicExternalities {
/// Create a new instance of `BasicExternalities`
pub fn new(inner: HashMap<Vec<u8>, Vec<u8>>) -> Self {
Self::new_with_code(&[], inner)
}
/// Create a new instance of `BasicExternalities`
pub fn new_with_code(code: &[u8], mut inner: HashMap<Vec<u8>, Vec<u8>>) -> Self {
let mut overlay = OverlayedChanges::default();
super::set_changes_trie_config(
&mut overlay,
inner.get(&CHANGES_TRIE_CONFIG.to_vec()).cloned(),
false,
).expect("changes trie configuration is correct in test env; qed");
inner.insert(HEAP_PAGES.to_vec(), 8u64.encode());
BasicExternalities {
inner,
changes: overlay,
code: Some(code.to_vec()),
}
}
/// Insert key/value
pub fn insert(&mut self, k: Vec<u8>, v: Vec<u8>) -> Option<Vec<u8>> {
self.inner.insert(k, v)
}
}
impl ::std::fmt::Debug for BasicExternalities {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{:?}", self.inner)
}
}
impl PartialEq for BasicExternalities {
fn eq(&self, other: &BasicExternalities) -> bool {
self.inner.eq(&other.inner)
}
}
impl FromIterator<(Vec<u8>, Vec<u8>)> for BasicExternalities {
fn from_iter<I: IntoIterator<Item=(Vec<u8>, Vec<u8>)>>(iter: I) -> Self {
let mut t = Self::new(Default::default());
t.inner.extend(iter);
t
}
}
impl Default for BasicExternalities {
fn default() -> Self { Self::new(Default::default()) }
}
impl From<BasicExternalities> for HashMap<Vec<u8>, Vec<u8>> {
fn from(tex: BasicExternalities) -> Self {
tex.inner.into()
}
}
impl From< HashMap<Vec<u8>, Vec<u8>> > for BasicExternalities {
fn from(hashmap: HashMap<Vec<u8>, Vec<u8>>) -> Self {
BasicExternalities {
inner: hashmap,
changes: Default::default(),
code: None,
}
}
}
impl<H: Hasher> Externalities<H> for BasicExternalities where H::Out: Ord + HeapSizeOf {
fn storage(&self, key: &[u8]) -> Option<Vec<u8>> {
match key {
CODE => self.code.clone(),
_ => self.inner.get(key).cloned(),
}
}
fn child_storage(&self, _storage_key: &[u8], _key: &[u8]) -> Option<Vec<u8>> {
None
}
fn place_storage(&mut self, key: Vec<u8>, maybe_value: Option<Vec<u8>>) {
self.changes.set_storage(key.clone(), maybe_value.clone());
match key.as_ref() {
CODE => self.code = maybe_value,
_ => {
match maybe_value {
Some(value) => { self.inner.insert(key, value); }
None => { self.inner.remove(&key); }
}
}
}
}
fn place_child_storage(&mut self, _storage_key: Vec<u8>, _key: Vec<u8>, _value: Option<Vec<u8>>) -> bool {
false
}
fn kill_child_storage(&mut self, _storage_key: &[u8]) { }
fn clear_prefix(&mut self, prefix: &[u8]) {
self.changes.clear_prefix(prefix);
self.inner.retain(|key, _| !key.starts_with(prefix));
}
fn chain_id(&self) -> u64 { 42 }
fn storage_root(&mut self) -> H::Out {
trie_root::<H, _, _, _>(self.inner.clone())
}
fn child_storage_root(&mut self, _storage_key: &[u8]) -> Option<Vec<u8>> {
None
}
fn storage_changes_root(&mut self, _parent: H::Out, _parent_num: u64) -> Option<H::Out> {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
use primitives::{Blake2Hasher, H256};
use hex_literal::{hex, hex_impl};
#[test]
fn commit_should_work() {
let mut ext = BasicExternalities::default();
let ext = &mut ext as &mut Externalities<Blake2Hasher>;
ext.set_storage(b"doe".to_vec(), b"reindeer".to_vec());
ext.set_storage(b"dog".to_vec(), b"puppy".to_vec());
ext.set_storage(b"dogglesworth".to_vec(), b"cat".to_vec());
const ROOT: [u8; 32] = hex!("0b33ed94e74e0f8e92a55923bece1ed02d16cf424e124613ddebc53ac3eeeabe");
assert_eq!(ext.storage_root(), H256::from(ROOT));
}
#[test]
fn set_and_retrieve_code() {
let mut ext = BasicExternalities::default();
let ext = &mut ext as &mut Externalities<Blake2Hasher>;
let code = vec![1, 2, 3];
ext.set_storage(CODE.to_vec(), code.clone());
assert_eq!(&ext.storage(CODE).unwrap(), &code);
}
}
+3 -1
View File
@@ -29,6 +29,7 @@ pub mod backend;
mod changes_trie;
mod ext;
mod testing;
mod basic;
mod overlayed_changes;
mod proving_backend;
mod trie_backend;
@@ -37,6 +38,7 @@ mod trie_backend_essence;
use overlayed_changes::OverlayedChangeSet;
pub use trie::{TrieMut, TrieDBMut, DBValue, MemoryDB};
pub use testing::TestExternalities;
pub use basic::BasicExternalities;
pub use ext::Ext;
pub use backend::Backend;
pub use changes_trie::{
@@ -625,7 +627,7 @@ where
/// differs from previous OR config decode has failed.
pub(crate) fn set_changes_trie_config(overlay: &mut OverlayedChanges, config: Option<Vec<u8>>, final_check: bool) -> Result<(), Box<Error>> {
let config = match config {
Some(v) => Some(changes_trie::Configuration::decode(&mut &v[..])
Some(v) => Some(Decode::decode(&mut &v[..])
.ok_or_else(|| Box::new("Failed to decode changes trie configuration".to_owned()) as Box<Error>)?),
None => None,
};
+7
View File
@@ -44,6 +44,11 @@ name = "bitflags"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "bitmask"
version = "0.5.0"
source = "git+https://github.com/paritytech/bitmask#c2d8d196e30b018d1385be8357fdca61b978facf"
[[package]]
name = "blake2-rfc"
version = "0.2.18"
@@ -1221,6 +1226,7 @@ dependencies = [
name = "srml-support"
version = "0.1.0"
dependencies = [
"bitmask 0.5.0 (git+https://github.com/paritytech/bitmask)",
"hex-literal 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"once_cell 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1863,6 +1869,7 @@ dependencies = [
"checksum backtrace-sys 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)" = "c66d56ac8dabd07f6aacdaf633f4b8262f5b3601a810a0dcddffd5c22c69daa0"
"checksum base58 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5024ee8015f02155eee35c711107ddd9a9bf3cb689cf2a9089c97e79b6e1ae83"
"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12"
"checksum bitmask 0.5.0 (git+https://github.com/paritytech/bitmask)" = "<none>"
"checksum blake2-rfc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "5d6d530bdd2d52966a6d03b7a964add7ae1a288d25214066fd4b600f0f796400"
"checksum block-buffer 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1339a1042f5d9f295737ad4d9a6ab6bf81c84a933dba110b9200cd6d1448b814"
"checksum block-buffer 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "49665c62e0e700857531fa5d3763e91b539ff1abeebd56808d378b495870d60d"
-1
View File
@@ -72,7 +72,6 @@ impl Decode for NodeHeader {
BRANCH_NODE_NO_VALUE => NodeHeader::Branch(false), // 254
BRANCH_NODE_WITH_VALUE => NodeHeader::Branch(true), // 255
_ => unreachable!(),
})
}
}