Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to equal spec_version. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 170,
impl_version: 170,
spec_version: 171,
impl_version: 171,
apis: RUNTIME_API_VERSIONS,
};

Expand Down
40 changes: 39 additions & 1 deletion srml/support/src/storage/generator/double_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use rstd::prelude::*;
use rstd::borrow::Borrow;
use codec::{Ref, FullCodec, FullEncode, Encode, EncodeLike, EncodeAppend};
use crate::{storage::{self, unhashed}, hash::StorageHasher};
use crate::{storage::{self, unhashed}, hash::StorageHasher, traits::Len};

/// Generator for `StorageDoubleMap` used by `decl_storage`.
///
Expand Down Expand Up @@ -116,6 +116,27 @@ where
G::from_optional_value_to_query(value)
}

fn swap<KArg1, KArg2>(key11: KArg1, key12: KArg2, key21: KArg1, key22: KArg2)
where
KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>,
{
let final_k1 = Self::storage_double_map_final_key(key11, key12);
let final_k2 = Self::storage_double_map_final_key(key21, key22);

let v1 = unhashed::get_raw(&final_k1);
if let Some(val) = unhashed::get_raw(&final_k2) {
unhashed::put_raw(&final_k1, &val);
} else {
unhashed::kill(&final_k1)
}
if let Some(val) = v1 {
unhashed::put_raw(&final_k2, &val);
} else {
unhashed::kill(&final_k2)
}
}

fn insert<KArg1, KArg2, VArg>(k1: KArg1, k2: KArg2, val: VArg)
where
KArg1: EncodeLike<K1>,
Expand Down Expand Up @@ -204,4 +225,21 @@ where
Self::append(Ref::from(&k1), Ref::from(&k2), items.clone())
.unwrap_or_else(|_| Self::insert(k1, k2, items));
}

fn decode_len<KArg1, KArg2>(key1: KArg1, key2: KArg2) -> Result<usize, &'static str>
where KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>,
V: codec::DecodeLength + Len,
{
let final_key = Self::storage_double_map_final_key(key1, key2);
if let Some(v) = unhashed::get_raw(&final_key) {
<V as codec::DecodeLength>::len(&v).map_err(|e| e.what())
} else {
let len = G::from_query_to_optional_value(G::from_optional_value_to_query(None))
.map(|v| v.len())
.unwrap_or(0);

Ok(len)
}
}
}
19 changes: 19 additions & 0 deletions srml/support/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ pub trait StorageDoubleMap<K1: FullEncode, K2: FullEncode, V: FullCodec> {
KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>;

/// Swap the values of two key-pairs.
fn swap<KArg1, KArg2>(key11: KArg1, key12: KArg2, key21: KArg1, key22: KArg2)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to see 4 generic parameters here. Otherwise we force the user to have 2 times the same type. That is not something we wanted to achieve with EncodeLike.

where
KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>;

fn insert<KArg1, KArg2, VArg>(k1: KArg1, k2: KArg2, val: VArg)
where
KArg1: EncodeLike<K1>,
Expand Down Expand Up @@ -272,4 +278,17 @@ pub trait StorageDoubleMap<K1: FullEncode, K2: FullEncode, V: FullCodec> {
V: EncodeAppend<Item=Item>,
Items: IntoIterator<Item=EncodeLikeItem> + Clone + EncodeLike<V>,
Items::IntoIter: ExactSizeIterator;

/// Read the length of the value in a fast way, without decoding the entire value.
///
/// `T` is required to implement `Codec::DecodeLength`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// `T` is required to implement `Codec::DecodeLength`.
/// `V` is required to implement `Codec::DecodeLength`.

///
/// Note that `0` is returned as the default value if no encoded value exists at the given key.
/// Therefore, this function cannot be used as a sign of _existence_. use the `::exists()`
/// function for this purpose.
fn decode_len<KArg1, KArg2>(key1: KArg1, key2: KArg2) -> Result<usize, &'static str>
where
KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>,
V: codec::DecodeLength + Len;
}