Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Fix decl_storage map and linked_map for insert_ref (#3316)
Browse files Browse the repository at this point in the history
* test

* fix

* bumpd version

* Update srml/support/procedural/src/storage/impls.rs

Co-Authored-By: Bastian Köcher <[email protected]>
  • Loading branch information
gui1117 and bkchr authored Aug 7, 2019
1 parent 3198388 commit e560b2a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
21 changes: 21 additions & 0 deletions srml/support/procedural/src/storage/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,27 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
storage.put(key_for, &(val, linkage))
}

/// Store a value under this key into the provided storage instance; this can take any reference
/// type that derefs to `T` (and has `Encode` implemented).
/// Store a value under this key into the provided storage instance.
fn insert_ref<Arg, S>(key: &#kty, val: &Arg, storage: &mut S)
where
#typ: AsRef<Arg>,
Arg: ?Sized + #scrate::codec::Encode,
S: #scrate::HashedStorage<#scrate::#hasher>
{
use self::#inner_module::Utils;

let key_for = &*#as_map::key_for(key);
let linkage = match Self::read_with_linkage(storage, key_for) {
// overwrite but reuse existing linkage
Some((_data, linkage)) => linkage,
// create new linkage
None => Self::new_head_linkage(storage, key),
};
storage.put(key_for, &(val, linkage))
}

/// Mutate the value under a key
fn mutate<R, F, S>(key: &#kty, f: F, storage: &mut S) -> R
where
Expand Down
2 changes: 1 addition & 1 deletion srml/support/src/storage/hashed/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ pub trait StorageMap<K: codec::Codec, V: codec::Codec> {
/// Store a value under this key into the provided storage instance; this can take any reference
/// type that derefs to `T` (and has `Encode` implemented).
/// Store a value under this key into the provided storage instance.
fn insert_ref<Arg: ?Sized + Encode, S: HashedStorage<Twox128>>(
fn insert_ref<Arg: ?Sized + Encode, S: HashedStorage<Self::Hasher>>(
key: &K,
val: &Arg,
storage: &mut S
Expand Down
32 changes: 20 additions & 12 deletions srml/support/test/tests/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use srml_support::{
use inherents::{
ProvideInherent, InherentData, InherentIdentifier, RuntimeString, MakeFatalError
};
use srml_support::{StorageValue, StorageMap, StorageDoubleMap};
use srml_support::{StorageValue, StorageMap, StorageDoubleMap, EnumerableStorageMap};
use primitives::{H256, sr25519};

mod system;
Expand Down Expand Up @@ -140,7 +140,7 @@ mod module2 {
trait Store for Module<T: Trait<I>, I: Instance=DefaultInstance> as Module2 {
pub Value config(value): T::Amount;
pub Map config(map): map u64 => u64;
pub LinkedMap config(linked_map): linked_map u64 => u64;
pub LinkedMap config(linked_map): linked_map u64 => Vec<u8>;
pub DoubleMap config(double_map): double_map u64, blake2_256(u64) => u64;
}
}
Expand Down Expand Up @@ -288,13 +288,13 @@ fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
module2: Some(module2::GenesisConfig {
value: 4,
map: vec![(0, 0)],
linked_map: vec![(0, 0)],
linked_map: vec![(0, vec![0])],
double_map: vec![(0, 0, 0)],
}),
module2_Instance1: Some(module2::GenesisConfig {
value: 4,
map: vec![(0, 0)],
linked_map: vec![(0, 0)],
linked_map: vec![(0, vec![0])],
double_map: vec![(0, 0, 0)],
}),
module2_Instance2: None,
Expand Down Expand Up @@ -388,15 +388,23 @@ fn storage_with_instance_basic_operation() {

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);
assert_eq!(LinkedMap::get(key), 0);
LinkedMap::mutate(key, |a| *a=2);
assert_eq!(LinkedMap::get(key), 2);
LinkedMap::insert(key, vec![1]);
assert_eq!(LinkedMap::enumerate().count(), 2);
assert_eq!(LinkedMap::get(key), vec![1]);
assert_eq!(LinkedMap::take(key), vec![1]);
assert_eq!(LinkedMap::enumerate().count(), 1);
assert_eq!(LinkedMap::get(key), vec![]);
LinkedMap::mutate(key, |a| *a=vec![2]);
assert_eq!(LinkedMap::enumerate().count(), 2);
assert_eq!(LinkedMap::get(key), vec![2]);
LinkedMap::remove(key);
assert_eq!(LinkedMap::enumerate().count(), 1);
assert_eq!(LinkedMap::exists(key), false);
assert_eq!(LinkedMap::get(key), 0);
assert_eq!(LinkedMap::get(key), vec![]);
assert_eq!(LinkedMap::exists(key), false);
assert_eq!(LinkedMap::enumerate().count(), 1);
LinkedMap::insert_ref(key, &vec![1]);
assert_eq!(LinkedMap::enumerate().count(), 2);

let key1 = 1;
let key2 = 1;
Expand Down Expand Up @@ -454,7 +462,7 @@ const EXPECTED_METADATA: StorageMetadata = StorageMetadata {
ty: StorageEntryType::Map {
hasher: StorageHasher::Blake2_256,
key: DecodeDifferent::Encode("u64"),
value: DecodeDifferent::Encode("u64"),
value: DecodeDifferent::Encode("Vec<u8>"),
is_linked: true,
},
default: DecodeDifferent::Encode(
Expand Down

0 comments on commit e560b2a

Please sign in to comment.