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

Add translate API for storage values #3947

Merged
merged 9 commits into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
19 changes: 18 additions & 1 deletion srml/support/src/storage/generator/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#[cfg(not(feature = "std"))]
use rstd::prelude::*;
use codec::{FullCodec, Encode, EncodeAppend, EncodeLike};
use codec::{FullCodec, Encode, EncodeAppend, EncodeLike, Decode};
use crate::{storage::{self, unhashed}, hash::{Twox128, StorageHasher}, traits::Len};

/// Generator for `StorageValue` used by `decl_storage`.
Expand Down Expand Up @@ -60,6 +60,23 @@ impl<T: FullCodec, G: StorageValue<T>> storage::StorageValue<T> for G {
G::from_optional_value_to_query(value)
}

fn translate<O: Decode, F: FnOnce(Option<O>) -> Option<T>>(f: F) -> Result<Option<T>, ()> {
let key = Self::storage_value_final_key();

// attempt to get the length directly.
let maybe_old = match unhashed::get_raw(&key) {
Some(old_data) => Some(O::decode(&mut &old_data[..]).map_err(|_| ())?),
None => None,
};
let maybe_new = f(maybe_old);
if let Some(new) = maybe_new.as_ref() {
new.using_encoded(|d| unhashed::put_raw(&key, d));
} else {
unhashed::kill(&key);
}
Ok(maybe_new)
}

fn put<Arg: EncodeLike<T>>(val: Arg) {
unhashed::put(&Self::storage_value_final_key(), &val)
}
Expand Down
24 changes: 23 additions & 1 deletion srml/support/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! Stuff to do with the runtime's storage.

use rstd::prelude::*;
use codec::{FullCodec, FullEncode, Encode, EncodeAppend, EncodeLike};
use codec::{FullCodec, FullEncode, Encode, EncodeAppend, EncodeLike, Decode};
use crate::traits::Len;

#[macro_use]
Expand All @@ -41,6 +41,28 @@ pub trait StorageValue<T: FullCodec> {
/// Load the value from the provided storage instance.
fn get() -> Self::Query;

/// Translate a value from some previous type (`O`) to the current type.
///
/// `f: F` is the translation function.
///
/// Returns `Err` if the storage item could not be interpreted as the old type, and Ok, along
/// with the new value if it could.
///
/// NOTE: This operates from and to `Option<>` types; no effort is made to respect the default
/// value of the original type.
///
/// # Warning
///
/// This function must be used with care as every other call to the storage still contains the
/// old type.
///
/// # Usage
///
/// This would typically be called inside the module implementation of on_initialize, while
/// ensuring no usage of this storage are made before the call to on_initialize.
/// (More precisely prior initialized modules doesn't make use of this storage).
fn translate<O: Decode, F: FnOnce(Option<O>) -> Option<T>>(f: F) -> Result<Option<T>, ()>;

/// Store a value under this key into the provided storage instance.
fn put<Arg: EncodeLike<T>>(val: Arg);

Expand Down