mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-09 19:01:08 +00:00
Let StorageDoubleMap use borrowed key types (#1804)
* Let StorageDoubleMap use borrowed key types * Bump impl version
This commit is contained in:
committed by
Gav Wood
parent
4bf4b37185
commit
f5ab24804f
BIN
Binary file not shown.
@@ -61,7 +61,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
|
||||
impl_name: create_runtime_str!("substrate-node"),
|
||||
authoring_version: 10,
|
||||
spec_version: 30,
|
||||
impl_version: 32,
|
||||
impl_version: 30,
|
||||
apis: RUNTIME_API_VERSIONS,
|
||||
};
|
||||
|
||||
|
||||
BIN
Binary file not shown.
@@ -54,7 +54,7 @@ pub trait AccountDb<T: Trait> {
|
||||
pub struct DirectAccountDb;
|
||||
impl<T: Trait> AccountDb<T> for DirectAccountDb {
|
||||
fn get_storage(&self, account: &T::AccountId, location: &[u8]) -> Option<Vec<u8>> {
|
||||
<StorageOf<T>>::get(account.clone(), location.to_vec())
|
||||
<StorageOf<T>>::get(account, &location.to_vec())
|
||||
}
|
||||
fn get_code(&self, account: &T::AccountId) -> Option<CodeHash<T>> {
|
||||
<CodeHashOf<T>>::get(account)
|
||||
@@ -83,9 +83,9 @@ impl<T: Trait> AccountDb<T> for DirectAccountDb {
|
||||
}
|
||||
for (k, v) in changed.storage.into_iter() {
|
||||
if let Some(value) = v {
|
||||
<StorageOf<T>>::insert(address.clone(), k, value);
|
||||
<StorageOf<T>>::insert(&address, &k, value);
|
||||
} else {
|
||||
<StorageOf<T>>::remove(address.clone(), k);
|
||||
<StorageOf<T>>::remove(&address, &k);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -369,7 +369,7 @@ impl<T: Trait> StorageDoubleMap for StorageOf<T> {
|
||||
impl<T: Trait> OnFreeBalanceZero<T::AccountId> for Module<T> {
|
||||
fn on_free_balance_zero(who: &T::AccountId) {
|
||||
<CodeHashOf<T>>::remove(who);
|
||||
<StorageOf<T>>::remove_prefix(who.clone());
|
||||
<StorageOf<T>>::remove_prefix(who);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -224,13 +224,13 @@ fn account_removal_removes_storage() {
|
||||
{
|
||||
Balances::set_free_balance(&1, 110);
|
||||
Balances::increase_total_stake_by(110);
|
||||
<StorageOf<Test>>::insert(1, b"foo".to_vec(), b"1".to_vec());
|
||||
<StorageOf<Test>>::insert(1, b"bar".to_vec(), b"2".to_vec());
|
||||
<StorageOf<Test>>::insert(&1, &b"foo".to_vec(), b"1".to_vec());
|
||||
<StorageOf<Test>>::insert(&1, &b"bar".to_vec(), b"2".to_vec());
|
||||
|
||||
Balances::set_free_balance(&2, 110);
|
||||
Balances::increase_total_stake_by(110);
|
||||
<StorageOf<Test>>::insert(2, b"hello".to_vec(), b"3".to_vec());
|
||||
<StorageOf<Test>>::insert(2, b"world".to_vec(), b"4".to_vec());
|
||||
<StorageOf<Test>>::insert(&2, &b"hello".to_vec(), b"3".to_vec());
|
||||
<StorageOf<Test>>::insert(&2, &b"world".to_vec(), b"4".to_vec());
|
||||
}
|
||||
|
||||
// Transfer funds from account 1 of such amount that after this transfer
|
||||
@@ -242,15 +242,15 @@ fn account_removal_removes_storage() {
|
||||
// Verify that all entries from account 1 is removed, while
|
||||
// entries from account 2 is in place.
|
||||
{
|
||||
assert_eq!(<StorageOf<Test>>::get(1, b"foo".to_vec()), None);
|
||||
assert_eq!(<StorageOf<Test>>::get(1, b"bar".to_vec()), None);
|
||||
assert_eq!(<StorageOf<Test>>::get(&1, &b"foo".to_vec()), None);
|
||||
assert_eq!(<StorageOf<Test>>::get(&1, &b"bar".to_vec()), None);
|
||||
|
||||
assert_eq!(
|
||||
<StorageOf<Test>>::get(2, b"hello".to_vec()),
|
||||
<StorageOf<Test>>::get(&2, &b"hello".to_vec()),
|
||||
Some(b"3".to_vec())
|
||||
);
|
||||
assert_eq!(
|
||||
<StorageOf<Test>>::get(2, b"world".to_vec()),
|
||||
<StorageOf<Test>>::get(&2, &b"world".to_vec()),
|
||||
Some(b"4".to_vec())
|
||||
);
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
use crate::rstd::prelude::*;
|
||||
use crate::codec::{Codec, Encode};
|
||||
use crate::storage::unhashed;
|
||||
use sr_std::borrow::Borrow;
|
||||
|
||||
/// An implementation of a map with a two keys.
|
||||
///
|
||||
@@ -38,34 +39,66 @@ pub trait StorageDoubleMap {
|
||||
const PREFIX: &'static [u8];
|
||||
|
||||
/// Insert an entry into this map.
|
||||
fn insert(k1: Self::Key1, k2: Self::Key2, val: Self::Value) {
|
||||
fn insert<Q, R>(k1: &Q, k2: &R, val: Self::Value)
|
||||
where
|
||||
Self::Key1: Borrow<Q>,
|
||||
Self::Key2: Borrow<R>,
|
||||
Q: Codec,
|
||||
R: Codec
|
||||
{
|
||||
unhashed::put(&Self::full_key(k1, k2)[..], &val);
|
||||
}
|
||||
|
||||
/// Remove an entry from this map.
|
||||
fn remove(k1: Self::Key1, k2: Self::Key2) {
|
||||
fn remove<Q, R>(k1: &Q, k2: &R)
|
||||
where
|
||||
Self::Key1: Borrow<Q>,
|
||||
Self::Key2: Borrow<R>,
|
||||
Q: Codec,
|
||||
R: Codec
|
||||
{
|
||||
unhashed::kill(&Self::full_key(k1, k2)[..]);
|
||||
}
|
||||
|
||||
/// Get an entry from this map.
|
||||
///
|
||||
/// If there is entry stored under the given keys, returns `None`.
|
||||
fn get(k1: Self::Key1, k2: Self::Key2) -> Option<Self::Value> {
|
||||
fn get<Q, R>(k1: &Q, k2: &R) -> Option<Self::Value>
|
||||
where
|
||||
Self::Key1: Borrow<Q>,
|
||||
Self::Key2: Borrow<R>,
|
||||
Q: Codec,
|
||||
R: Codec
|
||||
{
|
||||
unhashed::get(&Self::full_key(k1, k2)[..])
|
||||
}
|
||||
|
||||
/// Returns `true` if value under the specified keys exists.
|
||||
fn exists(k1: Self::Key1, k2: Self::Key2) -> bool {
|
||||
fn exists<Q, R>(k1: &Q, k2: &R) -> bool
|
||||
where
|
||||
Self::Key1: Borrow<Q>,
|
||||
Self::Key2: Borrow<R>,
|
||||
Q: Codec,
|
||||
R: Codec
|
||||
{
|
||||
unhashed::exists(&Self::full_key(k1, k2)[..])
|
||||
}
|
||||
|
||||
/// Removes all entries that shares the `k1` as the first key.
|
||||
fn remove_prefix(k1: Self::Key1) {
|
||||
fn remove_prefix<Q>(k1: &Q)
|
||||
where
|
||||
Self::Key1: Borrow<Q>,
|
||||
Q: Codec
|
||||
{
|
||||
unhashed::kill_prefix(&Self::derive_key1(Self::encode_key1(k1)))
|
||||
}
|
||||
|
||||
/// Encode key1 into Vec<u8> and prepend a prefix
|
||||
fn encode_key1(key: Self::Key1) -> Vec<u8> {
|
||||
fn encode_key1<Q>(key: &Q) -> Vec<u8>
|
||||
where
|
||||
Self::Key1: Borrow<Q>,
|
||||
Q: Codec
|
||||
{
|
||||
let mut raw_prefix = Vec::new();
|
||||
raw_prefix.extend(Self::PREFIX);
|
||||
raw_prefix.extend(Encode::encode(&key));
|
||||
@@ -73,7 +106,11 @@ pub trait StorageDoubleMap {
|
||||
}
|
||||
|
||||
/// Encode key2 into Vec<u8>
|
||||
fn encode_key2(key: Self::Key2) -> Vec<u8> {
|
||||
fn encode_key2<R>(key: &R) -> Vec<u8>
|
||||
where
|
||||
Self::Key2: Borrow<R>,
|
||||
R: Codec
|
||||
{
|
||||
Encode::encode(&key)
|
||||
}
|
||||
|
||||
@@ -85,7 +122,13 @@ pub trait StorageDoubleMap {
|
||||
|
||||
/// Returns a compound key that consist of the two parts: (prefix, `k1`) and `k2`.
|
||||
/// The first part is hased and then concatenated with a hash of `k2`.
|
||||
fn full_key(k1: Self::Key1, k2: Self::Key2) -> Vec<u8> {
|
||||
fn full_key<Q, R>(k1: &Q, k2: &R) -> Vec<u8>
|
||||
where
|
||||
Self::Key1: Borrow<Q>,
|
||||
Self::Key2: Borrow<R>,
|
||||
Q: Codec,
|
||||
R: Codec
|
||||
{
|
||||
let key1_data = Self::encode_key1(k1);
|
||||
let key2_data = Self::encode_key2(k2);
|
||||
let mut key = Self::derive_key1(key1_data);
|
||||
|
||||
Reference in New Issue
Block a user