storage doublemap in decl_storage (#1918)

* factorization

* introduce GenericUnhashedStorage

* implement generator and storage

* impl double map in storage macro

* improve StorageDoubleMapXX methods

* remove storage from example and impl test

* remove old comments

* wasm compatible

* improve imports

* rename storages

* update runtime impl version

* make code less verbose

* impl hash config for second key in double map

hash available are all of Hashable trait

* use double map in decl_storage for contract

* fix double map config issue

* add hasher into metadata

* update impl version and build wasm

* doc

* add attrs

* update metadata version

* update runtime version

* fix unused storage
This commit is contained in:
thiolliere
2019-03-28 17:40:50 +01:00
committed by Gav Wood
parent e3516d2bf4
commit f9d0da0a18
18 changed files with 843 additions and 207 deletions
+50 -9
View File
@@ -28,7 +28,7 @@ use srml_support::Parameter;
use inherents::{
ProvideInherent, InherentData, InherentIdentifier, RuntimeString, MakeFatalError
};
use srml_support::{StorageValue, StorageMap};
use srml_support::{StorageValue, StorageMap, StorageDoubleMap};
use primitives::{H256, sr25519};
pub trait Currency {
@@ -218,6 +218,7 @@ mod module2 {
pub Value config(value): T::Amount;
pub Map config(map): map u64 => u64;
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;
}
@@ -368,10 +369,16 @@ fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
}),
module2: Some(module2::GenesisConfig {
value: 4,
map: vec![],
linked_map: vec![],
map: vec![(0, 0)],
linked_map: vec![(0, 0)],
double_map: vec![(0, 0, 0)],
}),
module2_Instance1: Some(module2::GenesisConfig {
value: 4,
map: vec![(0, 0)],
linked_map: vec![(0, 0)],
double_map: vec![(0, 0, 0)],
}),
module2_Instance1: None,
module2_Instance2: None,
module2_Instance3: None,
}.build_storage().unwrap().0.into()
@@ -381,7 +388,7 @@ fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
fn storage_instance_independance() {
with_externalities(&mut new_test_ext(), || {
let mut map = rstd::collections::btree_map::BTreeMap::new();
for key in &[
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(),
@@ -394,6 +401,10 @@ fn storage_instance_independance() {
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(),
@@ -410,20 +421,32 @@ fn storage_instance_independance() {
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(),
].iter() {
assert!(map.insert(key, ()).is_none())
}
});
}
// TODO TODO: check configuration doublemapstorage in instances
#[test]
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>;
assert_eq!(Value::exists(), false);
assert_eq!(Value::exists(), true);
assert_eq!(Value::get(), 4);
Value::put(1);
assert_eq!(Value::get(), 1);
assert_eq!(Value::take(), 1);
@@ -431,10 +454,12 @@ fn storage_with_instance_basic_operation() {
Value::mutate(|a| *a=2);
assert_eq!(Value::get(), 2);
Value::kill();
assert_eq!(Value::exists(), false);
assert_eq!(Value::get(), 0);
let key = 1;
assert_eq!(Map::exists(1), false);
assert_eq!(Map::exists(0), true);
assert_eq!(Map::exists(key), false);
Map::insert(key, 1);
assert_eq!(Map::get(key), 1);
assert_eq!(Map::take(key), 1);
@@ -442,9 +467,11 @@ fn storage_with_instance_basic_operation() {
Map::mutate(key, |a| *a=2);
assert_eq!(Map::get(key), 2);
Map::remove(key);
assert_eq!(Map::exists(key), false);
assert_eq!(Map::get(key), 0);
assert_eq!(LinkedMap::exists(1), false);
assert_eq!(LinkedMap::exists(0), true);
assert_eq!(LinkedMap::exists(key), false);
LinkedMap::insert(key, 1);
assert_eq!(LinkedMap::get(key), 1);
assert_eq!(LinkedMap::take(key), 1);
@@ -452,6 +479,20 @@ fn storage_with_instance_basic_operation() {
LinkedMap::mutate(key, |a| *a=2);
assert_eq!(LinkedMap::get(key), 2);
LinkedMap::remove(key);
assert_eq!(LinkedMap::exists(key), false);
assert_eq!(LinkedMap::get(key), 0);
let key1 = 1;
let key2 = 1;
assert_eq!(DoubleMap::exists(0, 0), true);
assert_eq!(DoubleMap::exists(key1, key2), false);
DoubleMap::insert(key1, key2, 1);
assert_eq!(DoubleMap::get(key1, key2), 1);
assert_eq!(DoubleMap::take(key1, key2), 1);
assert_eq!(DoubleMap::get(key1, key2), 0);
DoubleMap::mutate(key1, key2, |a| *a=2);
assert_eq!(DoubleMap::get(key1, key2), 2);
DoubleMap::remove(key1, key2);
assert_eq!(DoubleMap::get(key1, key2), 0);
});
}