Retire storage_items! (#3950)

* Retire storage_items

* Add storage_items! tests to srml/support/tests

* Assimilate genesis config
This commit is contained in:
Ashley
2019-10-31 20:33:25 +00:00
committed by Bastian Köcher
parent 984163e86e
commit b0a58647eb
5 changed files with 43 additions and 292 deletions
@@ -18,7 +18,7 @@
use std::collections::HashMap;
use runtime_io::{blake2_256, twox_128};
use super::{AuthorityId, AccountId, WASM_BINARY};
use super::{AuthorityId, AccountId, WASM_BINARY, system};
use codec::{Encode, KeyedVec, Joiner};
use primitives::{ChangesTrieConfiguration, map, storage::well_known_keys};
use sr_primitives::traits::{Block as BlockT, Hash as HashT, Header as HeaderT};
@@ -77,10 +77,16 @@ impl GenesisConfig {
map.insert(well_known_keys::CHANGES_TRIE_CONFIG.to_vec(), changes_trie_config.encode());
}
map.insert(twox_128(&b"sys:auth"[..])[..].to_vec(), self.authorities.encode());
// Finally, add the extra storage entries.
// Add the extra storage entries.
map.extend(self.extra_storage.clone().into_iter());
(map, self.child_extra_storage.clone())
// Assimilate the system genesis config.
let mut storage = (map, self.child_extra_storage.clone());
let mut config = system::GenesisConfig::default();
config.authorities = self.authorities.clone();
config.assimilate_storage(&mut storage);
storage
}
}
+20 -9
View File
@@ -20,12 +20,13 @@
use rstd::prelude::*;
use runtime_io::{storage_root, storage_changes_root, blake2_256};
use runtime_support::storage::{self, StorageValue, StorageMap};
use runtime_support::storage_items;
use runtime_support::{decl_storage, decl_module};
use sr_primitives::{
traits::{Hash as HashT, BlakeTwo256, Header as _}, generic, ApplyError, ApplyResult,
transaction_validity::{TransactionValidity, ValidTransaction, InvalidTransaction},
};
use codec::{KeyedVec, Encode};
use srml_system::Trait;
use crate::{
AccountId, BlockNumber, Extrinsic, Transfer, H256 as Hash, Block, Header, Digest, AuthorityId
};
@@ -34,14 +35,20 @@ use primitives::storage::well_known_keys;
const NONCE_OF: &[u8] = b"nonce:";
const BALANCE_OF: &[u8] = b"balance:";
storage_items! {
ExtrinsicData: b"sys:xtd" => required map [ u32 => Vec<u8> ];
// The current block number being processed. Set by `execute_block`.
Number: b"sys:num" => BlockNumber;
ParentHash: b"sys:pha" => required Hash;
NewAuthorities: b"sys:new_auth" => Vec<AuthorityId>;
StorageDigest: b"sys:digest" => Digest;
Authorities get(authorities): b"sys:auth" => default Vec<AuthorityId>;
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {}
}
decl_storage! {
trait Store for Module<T: Trait> as TestRuntime {
ExtrinsicData: map u32 => Vec<u8>;
// The current block number being processed. Set by `execute_block`.
Number get(fn number): Option<BlockNumber>;
ParentHash get(fn parent_hash): Hash;
NewAuthorities get(fn new_authorities): Option<Vec<AuthorityId>>;
StorageDigest get(fn storage_digest): Option<Digest>;
Authorities get(fn authorities) config(): Vec<AuthorityId>;
}
}
pub fn balance_of_key(who: AccountId) -> Vec<u8> {
@@ -70,6 +77,10 @@ pub fn initialize_block(header: &Header) {
}
}
pub fn authorities() -> Vec<AuthorityId> {
Authorities::get()
}
pub fn get_block_number() -> Option<BlockNumber> {
Number::get()
}
-1
View File
@@ -47,7 +47,6 @@ pub use sr_primitives::RuntimeDebug;
pub mod debug;
#[macro_use]
pub mod dispatch;
#[macro_use]
pub mod storage;
mod hash;
#[macro_use]
@@ -20,8 +20,6 @@ use rstd::prelude::*;
use codec::{FullCodec, FullEncode, Encode, EncodeAppend, EncodeLike, Decode};
use crate::traits::Len;
#[macro_use]
pub mod storage_items;
pub mod unhashed;
pub mod hashed;
pub mod child;
@@ -1,4 +1,4 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
@@ -14,276 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Strongly typed wrappers around values in storage.
//!
//! This crate exports a macro `storage_items!` and traits describing behavior of generated
//! structs.
//!
//! Two kinds of data types are currently supported:
//! - values
//! - maps
//!
//! # Examples:
//!
//! ```rust
//! #[macro_use]
//! extern crate srml_support;
//!
//! type AuthorityId = [u8; 32];
//! type Balance = u64;
//! pub type SessionKey = [u8; 32];
//!
//! storage_items! {
//! // public value
//! pub Value: b"putd_key" => SessionKey;
//! // private map.
//! Balances: b"private_map:" => map [AuthorityId => Balance];
//! }
//!
//!# fn main() { }
//! ```
#[doc(hidden)]
pub use crate::rstd::borrow::Borrow;
#[doc(hidden)]
pub use crate::rstd::marker::PhantomData;
#[doc(hidden)]
pub use crate::rstd::boxed::Box;
#[doc(hidden)]
pub fn id<T>(t: T) -> T {
t
}
#[doc(hidden)]
pub use Some;
#[doc(hidden)]
pub fn unwrap_or_default<T: Default>(t: Option<T>) -> T {
t.unwrap_or_else(|| Default::default())
}
#[doc(hidden)]
pub fn require<T: Default>(t: Option<T>) -> T {
t.expect("Required values must be in storage")
}
// FIXME #1466 Remove this in favor of `decl_storage` macro.
/// Declares strongly-typed wrappers around codec-compatible types in storage.
#[macro_export]
macro_rules! storage_items {
// simple values
($name:ident : $key:expr => $ty:ty; $($t:tt)*) => {
$crate::__storage_items_internal!(() () (OPTION_TYPE Option<$ty>) (id) (id) $name: $key => $ty);
storage_items!($($t)*);
};
(pub $name:ident : $key:expr => $ty:ty; $($t:tt)*) => {
$crate::__storage_items_internal!((pub) () (OPTION_TYPE Option<$ty>) (id) (id) $name: $key => $ty);
storage_items!($($t)*);
};
($name:ident : $key:expr => default $ty:ty; $($t:tt)*) => {
$crate::__storage_items_internal!(() () (RAW_TYPE $ty) (unwrap_or_default) (Some) $name: $key => $ty);
storage_items!($($t)*);
};
(pub $name:ident : $key:expr => default $ty:ty; $($t:tt)*) => {
$crate::__storage_items_internal!((pub) () (RAW_TYPE $ty) (unwrap_or_default) (Some) $name: $key => $ty);
storage_items!($($t)*);
};
($name:ident : $key:expr => required $ty:ty; $($t:tt)*) => {
$crate::__storage_items_internal!(() () (RAW_TYPE $ty) (require) (Some) $name: $key => $ty);
storage_items!($($t)*);
};
(pub $name:ident : $key:expr => required $ty:ty; $($t:tt)*) => {
$crate::__storage_items_internal!((pub) () (RAW_TYPE $ty) (require) (Some) $name: $key => $ty);
storage_items!($($t)*);
};
($name:ident get($getfn:ident) : $key:expr => $ty:ty; $($t:tt)*) => {
$crate::__storage_items_internal!(() ($getfn) (OPTION_TYPE Option<$ty>) (id) (id) $name: $key => $ty);
storage_items!($($t)*);
};
(pub $name:ident get($getfn:ident) : $key:expr => $ty:ty; $($t:tt)*) => {
$crate::__storage_items_internal!((pub) ($getfn) (OPTION_TYPE Option<$ty>) (id) (id) $name: $key => $ty);
storage_items!($($t)*);
};
($name:ident get($getfn:ident) : $key:expr => default $ty:ty; $($t:tt)*) => {
$crate::__storage_items_internal!(() ($getfn) (RAW_TYPE $ty) (unwrap_or_default) (Some) $name: $key => $ty);
storage_items!($($t)*);
};
(pub $name:ident get($getfn:ident) : $key:expr => default $ty:ty; $($t:tt)*) => {
$crate::__storage_items_internal!((pub) ($getfn) (RAW_TYPE $ty) (unwrap_or_default) (Some) $name: $key => $ty);
storage_items!($($t)*);
};
($name:ident get($getfn:ident) : $key:expr => required $ty:ty; $($t:tt)*) => {
$crate::__storage_items_internal!(() ($getfn) (RAW_TYPE $ty) (require) (Some) $name: $key => $ty);
storage_items!($($t)*);
};
(pub $name:ident get($getfn:ident) : $key:expr => required $ty:ty; $($t:tt)*) => {
$crate::__storage_items_internal!((pub) ($getfn) (RAW_TYPE $ty) (require) (Some) $name: $key => $ty);
storage_items!($($t)*);
};
// maps
($name:ident : $prefix:expr => map [$kty:ty => $ty:ty]; $($t:tt)*) => {
$crate::__storage_items_internal!(() () (OPTION_TYPE Option<$ty>) (id) (id) $name: $prefix => map [$kty => $ty]);
storage_items!($($t)*);
};
(pub $name:ident : $prefix:expr => map [$kty:ty => $ty:ty]; $($t:tt)*) => {
$crate::__storage_items_internal!((pub) () (OPTION_TYPE Option<$ty>) (id) (id) $name: $prefix => map [$kty => $ty]);
storage_items!($($t)*);
};
($name:ident : $prefix:expr => default map [$kty:ty => $ty:ty]; $($t:tt)*) => {
$crate::__storage_items_internal!(() () (RAW_TYPE $ty) (unwrap_or_default) (Some) $name: $prefix => map [$kty => $ty]);
storage_items!($($t)*);
};
(pub $name:ident : $prefix:expr => default map [$kty:ty => $ty:ty]; $($t:tt)*) => {
$crate::__storage_items_internal!((pub) () (RAW_TYPE $ty) (unwrap_or_default) (Some) $name: $prefix => map [$kty => $ty]);
storage_items!($($t)*);
};
($name:ident : $prefix:expr => required map [$kty:ty => $ty:ty]; $($t:tt)*) => {
$crate::__storage_items_internal!(() () (RAW_TYPE $ty) (require) (Some) $name: $prefix => map [$kty => $ty]);
storage_items!($($t)*);
};
(pub $name:ident : $prefix:expr => required map [$kty:ty => $ty:ty]; $($t:tt)*) => {
$crate::__storage_items_internal!((pub) () (RAW_TYPE $ty) (require) (Some) $name: $prefix => map [$kty => $ty]);
storage_items!($($t)*);
};
($name:ident get($getfn:ident) : $prefix:expr => map [$kty:ty => $ty:ty]; $($t:tt)*) => {
$crate::__storage_items_internal!(() ($getfn) (OPTION_TYPE Option<$ty>) (id) (id) $name: $prefix => map [$kty => $ty]);
storage_items!($($t)*);
};
(pub $name:ident get($getfn:ident) : $prefix:expr => map [$kty:ty => $ty:ty]; $($t:tt)*) => {
$crate::__storage_items_internal!((pub) ($getfn) (OPTION_TYPE Option<$ty>) (id) (id) $name: $prefix => map [$kty => $ty]);
storage_items!($($t)*);
};
($name:ident get($getfn:ident) : $prefix:expr => default map [$kty:ty => $ty:ty]; $($t:tt)*) => {
$crate::__storage_items_internal!(() ($getfn) (RAW_TYPE $ty) (unwrap_or_default) (Some) $name: $prefix => map [$kty => $ty]);
storage_items!($($t)*);
};
(pub $name:ident get($getfn:ident) : $prefix:expr => default map [$kty:ty => $ty:ty]; $($t:tt)*) => {
$crate::__storage_items_internal!((pub) ($getfn) (RAW_TYPE $ty) (unwrap_or_default) (Some) $name: $prefix => map [$kty => $ty]);
storage_items!($($t)*);
};
($name:ident get($getfn:ident) : $prefix:expr => required map [$kty:ty => $ty:ty]; $($t:tt)*) => {
$crate::__storage_items_internal!(() ($getfn) (RAW_TYPE $ty) (require) (Some) $name: $prefix => map [$kty => $ty]);
storage_items!($($t)*);
};
(pub $name:ident get($getfn:ident) : $prefix:expr => required map [$kty:ty => $ty:ty]; $($t:tt)*) => {
$crate::__storage_items_internal!((pub) ($getfn) (RAW_TYPE $ty) (require) (Some) $name: $prefix => map [$kty => $ty]);
storage_items!($($t)*);
};
() => ()
}
#[macro_export]
#[doc(hidden)]
macro_rules! __storage_items_internal {
// generator for values.
(($($vis:tt)*) ($get_fn:ident) ($wraptype:ident $gettype:ty) ($into_query:ident) ($into_opt_val:ident) $name:ident : $key:expr => $ty:ty) => {
$crate::__storage_items_internal!{ ($($vis)*) () ($wraptype $gettype) ($into_query) ($into_opt_val) $name : $key => $ty }
pub fn $get_fn() -> $gettype { <$name as $crate::storage::StorageValue<$ty>> :: get() }
};
(($($vis:tt)*) () ($wraptype:ident $gettype:ty) ($into_query:ident) ($into_opt_val:ident) $name:ident : $key:expr => $ty:ty) => {
$($vis)* struct $name;
impl $crate::storage::generator::StorageValue<$ty> for $name {
type Query = $gettype;
fn unhashed_key() -> &'static [u8] {
$key
}
fn from_optional_value_to_query(v: Option<$ty>) -> Self::Query {
$crate::storage::storage_items::$into_query(v)
}
fn from_query_to_optional_value(v: Self::Query) -> Option<$ty> {
$crate::storage::storage_items::$into_opt_val(v)
}
}
};
// generator for maps.
(($($vis:tt)*) ($get_fn:ident) ($wraptype:ident $gettype:ty) ($into_query:ident) ($into_opt_val:ident) $name:ident : $prefix:expr => map [$kty:ty => $ty:ty]) => {
$crate::__storage_items_internal!{ ($($vis)*) () ($wraptype $gettype) ($into_query) ($into_opt_val) $name : $prefix => map [$kty => $ty] }
pub fn $get_fn<K: $crate::storage::storage_items::Borrow<$kty>>(key: K) -> $gettype {
<$name as $crate::storage::StorageMap<$kty, $ty>> :: get(key.borrow())
}
};
(($($vis:tt)*) () ($wraptype:ident $gettype:ty) ($into_query:ident) ($into_opt_val:ident) $name:ident : $prefix:expr => map [$kty:ty => $ty:ty]) => {
$($vis)* struct $name;
impl $crate::storage::generator::StorageMap<$kty, $ty> for $name {
type Query = $gettype;
type Hasher = $crate::Blake2_256;
fn prefix() -> &'static [u8] {
$prefix
}
fn from_optional_value_to_query(v: Option<$ty>) -> Self::Query {
$crate::storage::storage_items::$into_query(v)
}
fn from_query_to_optional_value(v: Self::Query) -> Option<$ty> {
$crate::storage::storage_items::$into_opt_val(v)
}
}
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! __handle_wrap_internal {
(RAW_TYPE { $($raw:tt)* } { $($option:tt)* }) => {
$($raw)*;
};
(OPTION_TYPE { $($raw:tt)* } { $($option:tt)* }) => {
$($option)*;
};
}
// FIXME: revisit this idiom once we get `type`s in `impl`s.
/*impl<T: Trait> Module<T> {
type Now = super::Now<T>;
}*/
#[cfg(test)]
// Do not complain about unused `dispatch` and `dispatch_aux`.
#[allow(dead_code)]
mod tests {
use crate::metadata::*;
use crate::metadata::StorageHasher;
use crate::rstd::marker::PhantomData;
use crate::codec::{Encode, Decode, EncodeLike};
use support::metadata::*;
use support::metadata::StorageHasher;
use support::rstd::marker::PhantomData;
use support::codec::{Encode, Decode, EncodeLike};
storage_items! {
Value: b"a" => u32;
Map: b"c:" => map [u32 => [u8; 32]];
}
#[test]
fn value() {
runtime_io::with_storage(&mut Default::default(), || {
assert!(Value::get().is_none());
Value::put(&100_000);
assert_eq!(Value::get(), Some(100_000));
Value::kill();
assert!(Value::get().is_none());
})
}
#[test]
fn map() {
runtime_io::with_storage(&mut Default::default(), || {
assert!(Map::get(&5).is_none());
Map::insert(&5, &[1; 32]);
assert_eq!(Map::get(&5), Some([1; 32]));
assert_eq!(Map::take(&5), Some([1; 32]));
assert!(Map::get(&5).is_none());
assert!(Map::get(&999).is_none());
})
support::decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {}
}
pub trait Trait {
@@ -291,11 +32,7 @@ mod tests {
type BlockNumber;
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {}
}
crate::decl_storage! {
support::decl_storage! {
trait Store for Module<T: Trait> as TestStorage {
// non-getters: pub / $default
@@ -701,13 +438,13 @@ mod test2 {
type BlockNumber;
}
decl_module! {
support::decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {}
}
type PairOf<T> = (T, T);
crate::decl_storage! {
support::decl_storage! {
trait Store for Module<T: Trait> as TestStorage {
SingleDef : u32;
PairDef : PairOf<u32>;
@@ -736,10 +473,10 @@ mod test3 {
type Origin;
type BlockNumber;
}
decl_module! {
support::decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {}
}
crate::decl_storage! {
support::decl_storage! {
trait Store for Module<T: Trait> as Test {
Foo get(fn foo) config(initial_foo): u32;
}
@@ -766,14 +503,14 @@ mod test_append_and_len {
type BlockNumber;
}
decl_module! {
support::decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {}
}
#[derive(PartialEq, Eq, Clone, Encode, Decode)]
struct NoDef(u32);
crate::decl_storage! {
support::decl_storage! {
trait Store for Module<T: Trait> as Test {
NoDefault: Option<NoDef>;