Revise storage API.

This commit is contained in:
Gav
2018-01-28 17:08:45 +01:00
parent 59469995b2
commit bfd599e5de
11 changed files with 175 additions and 134 deletions
@@ -17,7 +17,7 @@
//! Support code for the runtime.
mod environment;
mod storable;
pub mod storage;
mod hashable;
#[cfg(feature = "with-std")]
mod statichex;
@@ -25,8 +25,8 @@ mod statichex;
#[cfg(feature = "with-std")]
mod testing;
pub use self::environment::{Environment, with_env};
pub use self::storable::{StorageVec, Storable, kill};
pub use self::environment::with_env;
pub use self::storage::StorageVec;
pub use self::hashable::Hashable;
#[cfg(feature = "with-std")]
pub use self::statichex::{StaticHexConversion, StaticHexInto};
@@ -20,60 +20,95 @@ use runtime_std::prelude::*;
use runtime_std::{self, twox_128};
use codec::{Slicable, KeyedVec};
/// Trait for a value which may be stored in the storage DB.
pub trait Storable {
/// Lookup the value in storage and deserialise, giving a default value if not found.
fn lookup_default(key: &[u8]) -> Self where Self: Sized + Default {
Self::lookup(key).unwrap_or_else(Default::default)
}
/// Lookup `Some` value in storage and deserialise; `None` if it's not there.
fn lookup(_key: &[u8]) -> Option<Self> where Self: Sized {
unimplemented!()
}
/// Retrives and returns the serialised value of a key from storage, removing it immediately.
fn take(key: &[u8]) -> Option<Self> where Self: Sized {
if let Some(value) = Self::lookup(key) {
kill(key);
Some(value)
} else {
None
}
}
/// Place the value in storage under `key`.
fn store(&self, key: &[u8]);
}
// TODO: consider using blake256 to avoid possible preimage attack.
/// Remove `key` from storage.
/// Return the value of the item in storage under `key`, or `None` if there is no explicit entry.
pub fn get<T: Slicable + Sized>(key: &[u8]) -> Option<T> {
Slicable::set_as_slice(&|out, offset|
runtime_std::read_storage(&twox_128(key)[..], out, offset) >= out.len()
)
}
/// Return the value of the item in storage under `key`, or the type's default if there is no
/// explicit entry.
pub fn get_default<T: Slicable + Sized + Default>(key: &[u8]) -> T {
get(key).unwrap_or_else(Default::default)
}
/// Return the value of the item in storage under `key`, or `default_value` if there is no
/// explicit entry.
pub fn get_or<T: Slicable + Sized>(key: &[u8], default_value: T) -> T {
get(key).unwrap_or(default_value)
}
/// Return the value of the item in storage under `key`, or `default_value()` if there is no
/// explicit entry.
pub fn get_or_else<T: Slicable + Sized, F: FnOnce() -> T>(key: &[u8], default_value: F) -> T {
get(key).unwrap_or_else(default_value)
}
/// Please `value` in storage under `key`.
pub fn put<T: Slicable>(key: &[u8], value: &T) {
value.as_slice_then(|slice| runtime_std::set_storage(&twox_128(key)[..], slice));
}
/// Please `value` in storage under `key`.
pub fn place<T: Slicable>(key: &[u8], value: T) {
value.as_slice_then(|slice| runtime_std::set_storage(&twox_128(key)[..], slice));
}
/// Remove `key` from storage, returning its value if it had an explicit entry or `None` otherwise.
pub fn take<T: Slicable + Sized>(key: &[u8]) -> Option<T> {
let r = get(key);
if r.is_some() {
kill(key);
}
r
}
/// Remove `key` from storage, returning its value, or, if there was no explicit entry in storage,
/// the default for its type.
pub fn take_default<T: Slicable + Sized + Default>(key: &[u8]) -> T {
take(key).unwrap_or_else(Default::default)
}
/// Return the value of the item in storage under `key`, or `default_value` if there is no
/// explicit entry. Ensure there is no explicit entry on return.
pub fn take_or<T: Slicable + Sized>(key: &[u8], default_value: T) -> T {
take(key).unwrap_or(default_value)
}
/// Return the value of the item in storage under `key`, or `default_value()` if there is no
/// explicit entry. Ensure there is no explicit entry on return.
pub fn take_or_else<T: Slicable + Sized, F: FnOnce() -> T>(key: &[u8], default_value: F) -> T {
take(key).unwrap_or_else(default_value)
}
/// Check to see if `key` has an explicit entry in storage.
pub fn exists(key: &[u8]) -> bool {
let mut x = [0u8; 1];
runtime_std::read_storage(&twox_128(key)[..], &mut x[..], 0) == 1
}
/// Ensure `key` has no explicit entry in storage.
pub fn kill(key: &[u8]) {
runtime_std::set_storage(&twox_128(key)[..], b"");
}
impl<T: Sized + Slicable> Storable for T {
fn lookup(key: &[u8]) -> Option<Self> {
Slicable::set_as_slice(&|out, offset|
runtime_std::read_storage(&twox_128(key)[..], out, offset) >= out.len()
)
}
fn store(&self, key: &[u8]) {
self.as_slice_then(|slice| runtime_std::set_storage(&twox_128(key)[..], slice));
}
/// Get a Vec of bytes from storage.
pub fn get_raw(key: &[u8]) -> Vec<u8> {
runtime_std::storage(&twox_128(key)[..])
}
impl Storable for [u8] {
fn store(&self, key: &[u8]) {
runtime_std::set_storage(&twox_128(key)[..], self)
}
/// Put a raw byte slice into storage.
pub fn put_raw(key: &[u8], value: &[u8]) {
runtime_std::set_storage(&twox_128(key)[..], value)
}
/// A trait to conveniently store a vector of storable data.
// TODO: add iterator support
pub trait StorageVec {
type Item: Default + Sized + Storable;
type Item: Default + Sized + Slicable;
const PREFIX: &'static [u8];
/// Get the current set of items.
@@ -88,20 +123,20 @@ pub trait StorageVec {
}
fn set_item(index: u32, item: &Self::Item) {
item.store(&index.to_keyed_vec(Self::PREFIX));
put(&index.to_keyed_vec(Self::PREFIX), item);
}
fn item(index: u32) -> Self::Item {
Storable::lookup_default(&index.to_keyed_vec(Self::PREFIX))
get_default(&index.to_keyed_vec(Self::PREFIX))
}
fn set_count(count: u32) {
(count..Self::count()).for_each(|i| Self::set_item(i, &Self::Item::default()));
count.store(&b"len".to_keyed_vec(Self::PREFIX));
put(&b"len".to_keyed_vec(Self::PREFIX), &count);
}
fn count() -> u32 {
Storable::lookup_default(&b"len".to_keyed_vec(Self::PREFIX))
get_default(&b"len".to_keyed_vec(Self::PREFIX))
}
}
@@ -118,14 +153,14 @@ mod tests {
let mut t = TestExternalities { storage: HashMap::new(), };
with_externalities(&mut t, || {
let x = 69u32;
x.store(b":test");
let y = u32::lookup(b":test").unwrap();
put(b":test", &x);
let y: u32 = get(b":test").unwrap();
assert_eq!(x, y);
});
with_externalities(&mut t, || {
let x = 69426942i64;
x.store(b":test");
let y = i64::lookup(b":test").unwrap();
put(b":test", &x);
let y: i64 = get(b":test").unwrap();
assert_eq!(x, y);
});
}
@@ -135,15 +170,15 @@ mod tests {
let mut t = TestExternalities { storage: HashMap::new(), };
with_externalities(&mut t, || {
let x = true;
x.store(b":test");
let y = bool::lookup(b":test").unwrap();
put(b":test", &x);
let y: bool = get(b":test").unwrap();
assert_eq!(x, y);
});
with_externalities(&mut t, || {
let x = false;
x.store(b":test");
let y = bool::lookup(b":test").unwrap();
put(b":test", &x);
let y: bool = get(b":test").unwrap();
assert_eq!(x, y);
});
}
@@ -155,7 +190,7 @@ mod tests {
runtime_std::set_storage(&twox_128(b":test"), b"\x0b\0\0\0Hello world");
let x = b"Hello world".to_vec();
println!("Hex: {}", HexDisplay::from(&storage(&twox_128(b":test"))));
let y = <Vec<u8>>::lookup(b":test").unwrap();
let y = get::<Vec<u8>>(b":test").unwrap();
assert_eq!(x, y);
});
@@ -167,13 +202,13 @@ mod tests {
let x = b"Hello world".to_vec();
with_externalities(&mut t, || {
x.store(b":test");
put(b":test", &x);
});
println!("Ext is {:?}", t);
with_externalities(&mut t, || {
println!("Hex: {}", HexDisplay::from(&storage(&twox_128(b":test"))));
let y = <Vec<u8>>::lookup(b":test").unwrap();
let y: Vec<u8> = get(b":test").unwrap();
assert_eq!(x, y);
});
}
@@ -184,8 +219,8 @@ mod tests {
let mut t = TestExternalities { storage: HashMap::new(), };
with_externalities(&mut t, || {
let x = Proposal { function: InternalFunction::StakingSetSessionsPerEra, input_data: b"Hello world".to_vec() };
x.store(b":test");
let y = Proposal::lookup(b":test").unwrap();
put(b":test", &x);
let y: Proposal = get(b":test").unwrap();
assert_eq!(x, y);
});
}