mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 23:57:56 +00:00
Implement a proper generic resolution in decl_storage! (#2913)
* Add failing test case * move storage maps to blake2_128 (#2268) * remove default hash, introduce twox_128 and blake2 * use blake2_128 & create ext_blake2_128 * refactor code * add benchmark * factorize generator * fix * parameterizable hasher * some fix * fix * fix * fix * metadata * fix * remove debug print * map -> blake2_256 * fix test * fix test * Apply suggestions from code review Co-Authored-By: thiolliere <gui.thiolliere@gmail.com> * impl twox 128 concat (#2353) * impl twox_128_concat * comment addressed * fix * impl twox_128->64_concat * fix test * Fix compilation and cleanup some docs * Lol * Remove traits from storage types that are not generic * Get instance test almost working as wanted * Make `srml-support-test` compile again :) * Fixes test of srml-support * Fix compilation * Break some lines * Remove incorrect macro match arm * Integrates review feedback * Update documentation * Fix compilation
This commit is contained in:
@@ -17,7 +17,6 @@
|
||||
use runtime_io::{with_externalities, Blake2Hasher};
|
||||
use srml_support::{StorageValue, StorageMap, StorageDoubleMap};
|
||||
use srml_support::storage::unhashed;
|
||||
use srml_support::runtime_primitives::BuildStorage;
|
||||
use parity_codec::{Encode, Decode};
|
||||
|
||||
pub trait Trait {
|
||||
@@ -60,37 +59,37 @@ fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
#[test]
|
||||
fn final_keys() {
|
||||
with_externalities(&mut new_test_ext(), || {
|
||||
<Value<Test>>::put(1);
|
||||
Value::put(1);
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::twox_128(b"Module Value")), Some(1u32));
|
||||
|
||||
<Map<Test>>::insert(1, 2);
|
||||
Map::insert(1, 2);
|
||||
let mut k = b"Module Map".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::blake2_256(&k)), Some(2u32));
|
||||
|
||||
<Map2<Test>>::insert(1, 2);
|
||||
Map2::insert(1, 2);
|
||||
let mut k = b"Module Map2".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::twox_128(&k)), Some(2u32));
|
||||
|
||||
<LinkedMap<Test>>::insert(1, 2);
|
||||
LinkedMap::insert(1, 2);
|
||||
let mut k = b"Module LinkedMap".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::blake2_256(&k)), Some(2u32));
|
||||
|
||||
<LinkedMap2<Test>>::insert(1, 2);
|
||||
LinkedMap2::insert(1, 2);
|
||||
let mut k = b"Module LinkedMap2".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::twox_128(&k)), Some(2u32));
|
||||
|
||||
<DoubleMap<Test>>::insert(1, 2, 3);
|
||||
DoubleMap::insert(1, 2, 3);
|
||||
let mut k = b"Module DoubleMap".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
let mut k = runtime_io::blake2_256(&k).to_vec();
|
||||
k.extend(&runtime_io::blake2_256(&2u32.encode()));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(3u32));
|
||||
|
||||
<DoubleMap2<Test>>::insert(1, 2, 3);
|
||||
DoubleMap2::insert(1, 2, 3);
|
||||
let mut k = b"Module DoubleMap2".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
let mut k = runtime_io::twox_128(&k).to_vec();
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
// Copyright 2019 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/>.
|
||||
|
||||
pub trait Trait {
|
||||
type BlockNumber: parity_codec::Codec + Default;
|
||||
type Origin;
|
||||
}
|
||||
|
||||
srml_support::decl_module! {
|
||||
pub struct Module<T: Trait> for enum Call where origin: T::Origin {}
|
||||
}
|
||||
|
||||
srml_support::decl_storage! {
|
||||
trait Store for Module<T: Trait> as Example {
|
||||
pub AppendableDM config(t): double_map u32, blake2_256(T::BlockNumber) => Vec<u32>;
|
||||
}
|
||||
}
|
||||
|
||||
struct Test;
|
||||
|
||||
impl Trait for Test {
|
||||
type BlockNumber = u32;
|
||||
type Origin = ();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn init_genesis_config() {
|
||||
GenesisConfig::<Test> {
|
||||
t: Default::default(),
|
||||
};
|
||||
}
|
||||
@@ -13,84 +13,22 @@
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#![recursion_limit="128"]
|
||||
|
||||
use runtime_io::{with_externalities, Blake2Hasher};
|
||||
use srml_support::rstd::prelude::*;
|
||||
use srml_support::rstd as rstd;
|
||||
use srml_support::runtime_primitives::{generic, BuildStorage};
|
||||
use srml_support::runtime_primitives::traits::{BlakeTwo256, Block as _, Verify};
|
||||
use srml_support::Parameter;
|
||||
use srml_support::{
|
||||
Parameter,
|
||||
runtime_primitives::{generic, BuildStorage, traits::{BlakeTwo256, Block as _, Verify}},
|
||||
};
|
||||
use inherents::{
|
||||
ProvideInherent, InherentData, InherentIdentifier, RuntimeString, MakeFatalError
|
||||
};
|
||||
use srml_support::{StorageValue, StorageMap, StorageDoubleMap};
|
||||
use primitives::{H256, sr25519};
|
||||
|
||||
pub trait Currency {
|
||||
}
|
||||
mod system;
|
||||
|
||||
// Mock
|
||||
mod system {
|
||||
use super::*;
|
||||
|
||||
pub trait Trait: 'static + Eq + Clone {
|
||||
type Origin: Into<Result<RawOrigin<Self::AccountId>, Self::Origin>>
|
||||
+ From<RawOrigin<Self::AccountId>>;
|
||||
type BlockNumber;
|
||||
type Hash;
|
||||
type AccountId;
|
||||
type Event: From<Event>;
|
||||
}
|
||||
|
||||
pub type DigestItemOf<T> = generic::DigestItem<<T as Trait>::Hash>;
|
||||
|
||||
srml_support::decl_module! {
|
||||
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
|
||||
pub fn deposit_event(_event: T::Event) {
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T: Trait> Module<T> {
|
||||
pub fn deposit_log(_item: DigestItemOf<T>) {
|
||||
unimplemented!();
|
||||
}
|
||||
}
|
||||
|
||||
srml_support::decl_event!(
|
||||
pub enum Event {
|
||||
ExtrinsicSuccess,
|
||||
ExtrinsicFailed,
|
||||
}
|
||||
);
|
||||
|
||||
/// Origin for the system module.
|
||||
#[derive(PartialEq, Eq, Clone)]
|
||||
#[cfg_attr(feature = "std", derive(Debug))]
|
||||
pub enum RawOrigin<AccountId> {
|
||||
Root,
|
||||
Signed(AccountId),
|
||||
None,
|
||||
}
|
||||
|
||||
impl<AccountId> From<Option<AccountId>> for RawOrigin<AccountId> {
|
||||
fn from(s: Option<AccountId>) -> RawOrigin<AccountId> {
|
||||
match s {
|
||||
Some(who) => RawOrigin::Signed(who),
|
||||
None => RawOrigin::None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type Origin<T> = RawOrigin<<T as Trait>::AccountId>;
|
||||
|
||||
pub fn ensure_root<OuterOrigin, AccountId>(o: OuterOrigin) -> Result<(), &'static str>
|
||||
where OuterOrigin: Into<Result<RawOrigin<AccountId>, OuterOrigin>>
|
||||
{
|
||||
o.into().map(|_| ()).map_err(|_| "bad origin: expected to be a root origin")
|
||||
}
|
||||
}
|
||||
pub trait Currency {}
|
||||
|
||||
// Test for:
|
||||
// * No default instance
|
||||
@@ -123,7 +61,7 @@ mod module1 {
|
||||
}
|
||||
|
||||
srml_support::decl_event! {
|
||||
pub enum Event<T, I> where Phantom = rstd::marker::PhantomData<T> {
|
||||
pub enum Event<T, I> where Phantom = std::marker::PhantomData<T> {
|
||||
_Phantom(Phantom),
|
||||
AnotherVariant(u32),
|
||||
}
|
||||
@@ -133,7 +71,7 @@ mod module1 {
|
||||
#[cfg_attr(feature = "std", derive(Debug))]
|
||||
pub enum Origin<T: Trait<I>, I> {
|
||||
Members(u32),
|
||||
_Phantom(rstd::marker::PhantomData<(T, I)>),
|
||||
_Phantom(std::marker::PhantomData<(T, I)>),
|
||||
}
|
||||
|
||||
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"12345678";
|
||||
@@ -147,7 +85,7 @@ mod module1 {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn check_inherent(_call: &Self::Call, _data: &InherentData) -> rstd::result::Result<(), Self::Error> {
|
||||
fn check_inherent(_call: &Self::Call, _data: &InherentData) -> std::result::Result<(), Self::Error> {
|
||||
unimplemented!();
|
||||
}
|
||||
}
|
||||
@@ -180,7 +118,6 @@ mod module2 {
|
||||
pub LinkedMap config(linked_map): linked_map u64 => u64;
|
||||
pub DoubleMap config(double_map): double_map u64, blake2_256(u64) => u64;
|
||||
}
|
||||
extra_genesis_skip_phantom_data_field;
|
||||
}
|
||||
|
||||
srml_support::decl_event! {
|
||||
@@ -193,7 +130,7 @@ mod module2 {
|
||||
#[cfg_attr(feature = "std", derive(Debug))]
|
||||
pub enum Origin<T: Trait<I>, I=DefaultInstance> {
|
||||
Members(u32),
|
||||
_Phantom(rstd::marker::PhantomData<(T, I)>),
|
||||
_Phantom(std::marker::PhantomData<(T, I)>),
|
||||
}
|
||||
|
||||
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"12345678";
|
||||
@@ -207,7 +144,7 @@ mod module2 {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn check_inherent(_call: &Self::Call, _data: &InherentData) -> rstd::result::Result<(), Self::Error> {
|
||||
fn check_inherent(_call: &Self::Call, _data: &InherentData) -> std::result::Result<(), Self::Error> {
|
||||
unimplemented!();
|
||||
}
|
||||
}
|
||||
@@ -224,8 +161,7 @@ mod module3 {
|
||||
}
|
||||
|
||||
srml_support::decl_module! {
|
||||
pub struct Module<T: Trait> for enum Call where origin: <T as system::Trait>::Origin {
|
||||
}
|
||||
pub struct Module<T: Trait> for enum Call where origin: <T as system::Trait>::Origin {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -282,12 +218,22 @@ srml_support::construct_runtime!(
|
||||
UncheckedExtrinsic = UncheckedExtrinsic
|
||||
{
|
||||
System: system::{Module, Call, Event},
|
||||
Module1_1: module1::<Instance1>::{Module, Call, Storage, Event<T, I>, Config<T, I>, Origin<T, I>, Inherent},
|
||||
Module1_2: module1::<Instance2>::{Module, Call, Storage, Event<T, I>, Config<T, I>, Origin<T, I>, Inherent},
|
||||
Module1_1: module1::<Instance1>::{
|
||||
Module, Call, Storage, Event<T>, Config, Origin<T>, Inherent
|
||||
},
|
||||
Module1_2: module1::<Instance2>::{
|
||||
Module, Call, Storage, Event<T>, Config, Origin<T>, Inherent
|
||||
},
|
||||
Module2: module2::{Module, Call, Storage, Event<T>, Config<T>, Origin<T>, Inherent},
|
||||
Module2_1: module2::<Instance1>::{Module, Call, Storage, Event<T, I>, Config<T, I>, Origin<T, I>, Inherent},
|
||||
Module2_2: module2::<Instance2>::{Module, Call, Storage, Event<T, I>, Config<T, I>, Origin<T, I>, Inherent},
|
||||
Module2_3: module2::<Instance3>::{Module, Call, Storage, Event<T, I>, Config<T, I>, Origin<T, I>, Inherent},
|
||||
Module2_1: module2::<Instance1>::{
|
||||
Module, Call, Storage, Event<T>, Config<T>, Origin<T>, Inherent
|
||||
},
|
||||
Module2_2: module2::<Instance2>::{
|
||||
Module, Call, Storage, Event<T>, Config<T>, Origin<T>, Inherent
|
||||
},
|
||||
Module2_3: module2::<Instance3>::{
|
||||
Module, Call, Storage, Event<T>, Config<T>, Origin<T>, Inherent
|
||||
},
|
||||
Module3: module3::{Module, Call},
|
||||
}
|
||||
);
|
||||
@@ -300,11 +246,9 @@ fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
GenesisConfig{
|
||||
module1_Instance1: Some(module1::GenesisConfig {
|
||||
value: 3,
|
||||
.. Default::default()
|
||||
}),
|
||||
module1_Instance2: Some(module1::GenesisConfig {
|
||||
value: 4,
|
||||
_genesis_phantom_data: Default::default(),
|
||||
}),
|
||||
module2: Some(module2::GenesisConfig {
|
||||
value: 4,
|
||||
@@ -326,48 +270,48 @@ fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
#[test]
|
||||
fn storage_instance_independance() {
|
||||
with_externalities(&mut new_test_ext(), || {
|
||||
let mut map = rstd::collections::btree_map::BTreeMap::new();
|
||||
let mut map = std::collections::btree_map::BTreeMap::new();
|
||||
for key in [
|
||||
module2::Value::<Runtime>::key().to_vec(),
|
||||
module2::Value::<Runtime, module2::Instance1>::key().to_vec(),
|
||||
module2::Value::<Runtime, module2::Instance2>::key().to_vec(),
|
||||
module2::Value::<Runtime, module2::Instance3>::key().to_vec(),
|
||||
module2::Map::<Runtime>::prefix().to_vec(),
|
||||
module2::Map::<Runtime, module2::Instance1>::prefix().to_vec(),
|
||||
module2::Map::<Runtime, module2::Instance2>::prefix().to_vec(),
|
||||
module2::Map::<Runtime, module2::Instance3>::prefix().to_vec(),
|
||||
module2::LinkedMap::<Runtime>::prefix().to_vec(),
|
||||
module2::LinkedMap::<Runtime, module2::Instance1>::prefix().to_vec(),
|
||||
module2::LinkedMap::<Runtime, module2::Instance2>::prefix().to_vec(),
|
||||
module2::LinkedMap::<Runtime, module2::Instance3>::prefix().to_vec(),
|
||||
module2::DoubleMap::<Runtime>::prefix().to_vec(),
|
||||
module2::DoubleMap::<Runtime, module2::Instance1>::prefix().to_vec(),
|
||||
module2::DoubleMap::<Runtime, module2::Instance2>::prefix().to_vec(),
|
||||
module2::DoubleMap::<Runtime, module2::Instance3>::prefix().to_vec(),
|
||||
module2::Map::<Runtime>::key_for(0),
|
||||
module2::Map::<Runtime, module2::Instance1>::key_for(0).to_vec(),
|
||||
module2::Map::<Runtime, module2::Instance2>::key_for(0).to_vec(),
|
||||
module2::Map::<Runtime, module2::Instance3>::key_for(0).to_vec(),
|
||||
module2::LinkedMap::<Runtime>::key_for(0),
|
||||
module2::LinkedMap::<Runtime, module2::Instance1>::key_for(0).to_vec(),
|
||||
module2::LinkedMap::<Runtime, module2::Instance2>::key_for(0).to_vec(),
|
||||
module2::LinkedMap::<Runtime, module2::Instance3>::key_for(0).to_vec(),
|
||||
module2::Map::<Runtime>::key_for(1),
|
||||
module2::Map::<Runtime, module2::Instance1>::key_for(1).to_vec(),
|
||||
module2::Map::<Runtime, module2::Instance2>::key_for(1).to_vec(),
|
||||
module2::Map::<Runtime, module2::Instance3>::key_for(1).to_vec(),
|
||||
module2::LinkedMap::<Runtime>::key_for(1),
|
||||
module2::LinkedMap::<Runtime, module2::Instance1>::key_for(1).to_vec(),
|
||||
module2::LinkedMap::<Runtime, module2::Instance2>::key_for(1).to_vec(),
|
||||
module2::LinkedMap::<Runtime, module2::Instance3>::key_for(1).to_vec(),
|
||||
module2::DoubleMap::<Runtime>::prefix_for(1),
|
||||
module2::DoubleMap::<Runtime, module2::Instance1>::prefix_for(1).to_vec(),
|
||||
module2::DoubleMap::<Runtime, module2::Instance2>::prefix_for(1).to_vec(),
|
||||
module2::DoubleMap::<Runtime, module2::Instance3>::prefix_for(1).to_vec(),
|
||||
module2::DoubleMap::<Runtime>::key_for(1, 1),
|
||||
module2::DoubleMap::<Runtime, module2::Instance1>::key_for(1, 1).to_vec(),
|
||||
module2::DoubleMap::<Runtime, module2::Instance2>::key_for(1, 1).to_vec(),
|
||||
module2::DoubleMap::<Runtime, module2::Instance3>::key_for(1, 1).to_vec(),
|
||||
module2::Map::<module2::DefaultInstance>::prefix().to_vec(),
|
||||
module2::Map::<module2::Instance1>::prefix().to_vec(),
|
||||
module2::Map::<module2::Instance2>::prefix().to_vec(),
|
||||
module2::Map::<module2::Instance3>::prefix().to_vec(),
|
||||
module2::LinkedMap::<module2::DefaultInstance>::prefix().to_vec(),
|
||||
module2::LinkedMap::<module2::Instance1>::prefix().to_vec(),
|
||||
module2::LinkedMap::<module2::Instance2>::prefix().to_vec(),
|
||||
module2::LinkedMap::<module2::Instance3>::prefix().to_vec(),
|
||||
module2::DoubleMap::<module2::DefaultInstance>::prefix().to_vec(),
|
||||
module2::DoubleMap::<module2::Instance1>::prefix().to_vec(),
|
||||
module2::DoubleMap::<module2::Instance2>::prefix().to_vec(),
|
||||
module2::DoubleMap::<module2::Instance3>::prefix().to_vec(),
|
||||
module2::Map::<module2::DefaultInstance>::key_for(0),
|
||||
module2::Map::<module2::Instance1>::key_for(0).to_vec(),
|
||||
module2::Map::<module2::Instance2>::key_for(0).to_vec(),
|
||||
module2::Map::<module2::Instance3>::key_for(0).to_vec(),
|
||||
module2::LinkedMap::<module2::DefaultInstance>::key_for(0),
|
||||
module2::LinkedMap::<module2::Instance1>::key_for(0).to_vec(),
|
||||
module2::LinkedMap::<module2::Instance2>::key_for(0).to_vec(),
|
||||
module2::LinkedMap::<module2::Instance3>::key_for(0).to_vec(),
|
||||
module2::Map::<module2::DefaultInstance>::key_for(1),
|
||||
module2::Map::<module2::Instance1>::key_for(1).to_vec(),
|
||||
module2::Map::<module2::Instance2>::key_for(1).to_vec(),
|
||||
module2::Map::<module2::Instance3>::key_for(1).to_vec(),
|
||||
module2::LinkedMap::<module2::DefaultInstance>::key_for(1),
|
||||
module2::LinkedMap::<module2::Instance1>::key_for(1).to_vec(),
|
||||
module2::LinkedMap::<module2::Instance2>::key_for(1).to_vec(),
|
||||
module2::LinkedMap::<module2::Instance3>::key_for(1).to_vec(),
|
||||
module2::DoubleMap::<module2::DefaultInstance>::prefix_for(1),
|
||||
module2::DoubleMap::<module2::Instance1>::prefix_for(1).to_vec(),
|
||||
module2::DoubleMap::<module2::Instance2>::prefix_for(1).to_vec(),
|
||||
module2::DoubleMap::<module2::Instance3>::prefix_for(1).to_vec(),
|
||||
module2::DoubleMap::<module2::DefaultInstance>::key_for(1, 1),
|
||||
module2::DoubleMap::<module2::Instance1>::key_for(1, 1).to_vec(),
|
||||
module2::DoubleMap::<module2::Instance2>::key_for(1, 1).to_vec(),
|
||||
module2::DoubleMap::<module2::Instance3>::key_for(1, 1).to_vec(),
|
||||
].iter() {
|
||||
assert!(map.insert(key, ()).is_none())
|
||||
}
|
||||
@@ -378,9 +322,9 @@ fn storage_instance_independance() {
|
||||
fn storage_with_instance_basic_operation() {
|
||||
with_externalities(&mut new_test_ext(), || {
|
||||
type Value = module2::Value<Runtime, module2::Instance1>;
|
||||
type Map = module2::Map<Runtime, module2::Instance1>;
|
||||
type LinkedMap = module2::LinkedMap<Runtime, module2::Instance1>;
|
||||
type DoubleMap = module2::DoubleMap<Runtime, module2::Instance1>;
|
||||
type Map = module2::Map<module2::Instance1>;
|
||||
type LinkedMap = module2::LinkedMap<module2::Instance1>;
|
||||
type DoubleMap = module2::DoubleMap<module2::Instance1>;
|
||||
|
||||
assert_eq!(Value::exists(), true);
|
||||
assert_eq!(Value::get(), 4);
|
||||
@@ -432,4 +376,4 @@ fn storage_with_instance_basic_operation() {
|
||||
DoubleMap::remove(key1, key2);
|
||||
assert_eq!(DoubleMap::get(key1, key2), 0);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
// Copyright 2019 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/>.
|
||||
|
||||
use srml_support::runtime_primitives::generic;
|
||||
use srml_support::runtime_primitives::traits::{BlakeTwo256, Block as _, Verify};
|
||||
use srml_support::codec::{Encode, Decode};
|
||||
use primitives::{H256, sr25519};
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
mod system;
|
||||
|
||||
mod module {
|
||||
use super::*;
|
||||
|
||||
pub type Request<T> = (
|
||||
<T as system::Trait>::AccountId,
|
||||
Role,
|
||||
<T as system::Trait>::BlockNumber,
|
||||
);
|
||||
pub type Requests<T> = Vec<Request<T>>;
|
||||
|
||||
#[derive(Encode, Decode, Copy, Clone, Eq, PartialEq, Debug)]
|
||||
pub enum Role {
|
||||
Storage,
|
||||
}
|
||||
|
||||
#[derive(Encode, Decode, Copy, Clone, Eq, PartialEq, Debug)]
|
||||
pub struct RoleParameters<T: Trait> {
|
||||
// minimum actors to maintain - if role is unstaking
|
||||
// and remaining actors would be less that this value - prevent or punish for unstaking
|
||||
pub min_actors: u32,
|
||||
|
||||
// the maximum number of spots available to fill for a role
|
||||
pub max_actors: u32,
|
||||
|
||||
// payouts are made at this block interval
|
||||
pub reward_period: T::BlockNumber,
|
||||
|
||||
// minimum amount of time before being able to unstake
|
||||
pub bonding_period: T::BlockNumber,
|
||||
|
||||
// how long tokens remain locked for after unstaking
|
||||
pub unbonding_period: T::BlockNumber,
|
||||
|
||||
// minimum period required to be in service. unbonding before this time is highly penalized
|
||||
pub min_service_period: T::BlockNumber,
|
||||
|
||||
// "startup" time allowed for roles that need to sync their infrastructure
|
||||
// with other providers before they are considered in service and punishable for
|
||||
// not delivering required level of service.
|
||||
pub startup_grace_period: T::BlockNumber,
|
||||
}
|
||||
|
||||
impl<T: Trait> Default for RoleParameters<T> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
max_actors: 10,
|
||||
reward_period: T::BlockNumber::default(),
|
||||
unbonding_period: T::BlockNumber::default(),
|
||||
|
||||
// not currently used
|
||||
min_actors: 5,
|
||||
bonding_period: T::BlockNumber::default(),
|
||||
min_service_period: T::BlockNumber::default(),
|
||||
startup_grace_period: T::BlockNumber::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Trait: system::Trait {}
|
||||
|
||||
srml_support::decl_module! {
|
||||
pub struct Module<T: Trait> for enum Call where origin: T::Origin {}
|
||||
}
|
||||
|
||||
#[derive(Encode, Decode, Copy, Clone, Serialize, Deserialize)]
|
||||
pub struct Data<T: Trait> {
|
||||
pub data: T::BlockNumber,
|
||||
}
|
||||
|
||||
impl<T: Trait> Default for Data<T> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
data: T::BlockNumber::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
srml_support::decl_storage! {
|
||||
trait Store for Module<T: Trait> as Actors {
|
||||
/// requirements to enter and maintain status in roles
|
||||
pub Parameters get(parameters) build(|config: &GenesisConfig| {
|
||||
if config.enable_storage_role {
|
||||
let storage_params: RoleParameters<T> = Default::default();
|
||||
vec![(Role::Storage, storage_params)]
|
||||
} else {
|
||||
vec![]
|
||||
}
|
||||
}): map Role => Option<RoleParameters<T>>;
|
||||
|
||||
/// the roles members can enter into
|
||||
pub AvailableRoles get(available_roles) build(|config: &GenesisConfig| {
|
||||
if config.enable_storage_role {
|
||||
vec![(Role::Storage)]
|
||||
} else {
|
||||
vec![]
|
||||
}
|
||||
}): Vec<Role>;
|
||||
|
||||
/// Actors list
|
||||
pub ActorAccountIds get(actor_account_ids) : Vec<T::AccountId>;
|
||||
|
||||
/// actor accounts associated with a role
|
||||
pub AccountIdsByRole get(account_ids_by_role) : map Role => Vec<T::AccountId>;
|
||||
|
||||
/// tokens locked until given block number
|
||||
pub Bondage get(bondage) : map T::AccountId => T::BlockNumber;
|
||||
|
||||
/// First step before enter a role is registering intent with a new account/key.
|
||||
/// This is done by sending a role_entry_request() from the new account.
|
||||
/// The member must then send a stake() transaction to approve the request and enter the desired role.
|
||||
/// The account making the request will be bonded and must have
|
||||
/// sufficient balance to cover the minimum stake for the role.
|
||||
/// Bonding only occurs after successful entry into a role.
|
||||
pub RoleEntryRequests get(role_entry_requests) : Requests<T>;
|
||||
|
||||
/// Entry request expires after this number of blocks
|
||||
pub RequestLifeTime get(request_life_time) config(request_life_time) : u64 = 0;
|
||||
}
|
||||
add_extra_genesis {
|
||||
config(enable_storage_role): bool;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type Signature = sr25519::Signature;
|
||||
pub type AccountId = <Signature as Verify>::Signer;
|
||||
pub type BlockNumber = u64;
|
||||
pub type Index = u64;
|
||||
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
|
||||
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
|
||||
pub type UncheckedExtrinsic = generic::UncheckedMortalCompactExtrinsic<u32, Index, Call, Signature>;
|
||||
|
||||
impl system::Trait for Runtime {
|
||||
type Hash = H256;
|
||||
type Origin = Origin;
|
||||
type BlockNumber = BlockNumber;
|
||||
type AccountId = AccountId;
|
||||
type Event = Event;
|
||||
}
|
||||
|
||||
impl module::Trait for Runtime {}
|
||||
|
||||
srml_support::construct_runtime!(
|
||||
pub enum Runtime where
|
||||
Block = Block,
|
||||
NodeBlock = Block,
|
||||
UncheckedExtrinsic = UncheckedExtrinsic
|
||||
{
|
||||
System: system::{Module, Call, Event},
|
||||
Module: module::{Module, Call, Storage, Config},
|
||||
}
|
||||
);
|
||||
|
||||
#[test]
|
||||
fn create_genesis_config() {
|
||||
GenesisConfig {
|
||||
module: Some(module::GenesisConfig {
|
||||
request_life_time: 0,
|
||||
enable_storage_role: true,
|
||||
})
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
use srml_support::codec::{Encode, Decode};
|
||||
|
||||
pub trait Trait: 'static + Eq + Clone {
|
||||
type Origin: Into<Result<RawOrigin<Self::AccountId>, Self::Origin>>
|
||||
+ From<RawOrigin<Self::AccountId>>;
|
||||
|
||||
type BlockNumber: Decode + Encode + Clone + Default;
|
||||
type Hash;
|
||||
type AccountId: Encode + Decode;
|
||||
type Event: From<Event>;
|
||||
}
|
||||
|
||||
srml_support::decl_module! {
|
||||
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
|
||||
pub fn deposit_event(_event: T::Event) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
srml_support::decl_event!(
|
||||
pub enum Event {
|
||||
ExtrinsicSuccess,
|
||||
ExtrinsicFailed,
|
||||
}
|
||||
);
|
||||
|
||||
/// Origin for the system module.
|
||||
#[derive(PartialEq, Eq, Clone)]
|
||||
#[cfg_attr(feature = "std", derive(Debug))]
|
||||
pub enum RawOrigin<AccountId> {
|
||||
Root,
|
||||
Signed(AccountId),
|
||||
None,
|
||||
}
|
||||
|
||||
impl<AccountId> From<Option<AccountId>> for RawOrigin<AccountId> {
|
||||
fn from(s: Option<AccountId>) -> RawOrigin<AccountId> {
|
||||
match s {
|
||||
Some(who) => RawOrigin::Signed(who),
|
||||
None => RawOrigin::None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type Origin<T> = RawOrigin<<T as Trait>::AccountId>;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn ensure_root<OuterOrigin, AccountId>(o: OuterOrigin) -> Result<(), &'static str>
|
||||
where OuterOrigin: Into<Result<RawOrigin<AccountId>, OuterOrigin>>
|
||||
{
|
||||
o.into().map(|_| ()).map_err(|_| "bad origin: expected to be a root origin")
|
||||
}
|
||||
Reference in New Issue
Block a user