Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
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
Prev Previous commit
Next Next commit
move generator to runtime-support
  • Loading branch information
rphmeier committed Mar 6, 2018
commit 06c3d95fd9c702377fbe9f861f7c54a9178ecfae
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ members = [
"substrate/runtime-support",
"substrate/serializer",
"substrate/state-machine",
"substrate/storage-wrapper",
"substrate/test-runtime",
]
exclude = [
Expand Down
2 changes: 1 addition & 1 deletion polkadot/runtime/src/runtime/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! Conensus module for runtime; manages the authority set ready for the native code.

use rstd::prelude::*;
use runtime_support::storage::unhashed::StorageVec;
use runtime_support::storage::StorageVec;
use polkadot_primitives::SessionKey;

struct AuthorityStorageVec {}
Expand Down
5 changes: 4 additions & 1 deletion substrate/runtime-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@

extern crate substrate_runtime_std as rstd;
extern crate substrate_runtime_io as runtime_io;
extern crate substrate_codec as codec;
extern crate substrate_primitives as primitives;

#[doc(hidden)]
pub extern crate substrate_codec as codec;
pub use self::storage::generator::Storage as GenericStorage;

pub mod storage;
mod hashable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
//!
//! ```rust
//! #[macro_use]
//! extern crate substrate_storage_wrapper;
//! extern crate substrate_runtime_support;
//!
//! type AuthorityId = [u8; 32];
//! type Balance = u64;
Expand All @@ -46,8 +46,8 @@
//!# fn main() { }
//! ```

#[doc(hidden)]
pub extern crate substrate_codec as codec;
use codec;
use rstd::vec::Vec;

/// Abstraction around storage.
pub trait Storage {
Expand Down Expand Up @@ -148,44 +148,44 @@ macro_rules! __storage_items_internal {
}

/// Load the value from the provided storage instance.
$($vis)* fn load<S: $crate::Storage>(storage: &S) -> Option<$ty> {
$($vis)* fn load<S: $crate::GenericStorage>(storage: &S) -> Option<$ty> {
storage.load($key)
}

/// Store a value under this key into the provded storage instance.
$($vis)* fn store<S: $crate::Storage>(val: &$ty, storage: &S) {
$($vis)* fn store<S: $crate::GenericStorage>(val: &$ty, storage: &S) {
storage.store($key, val)
}

/// Kill the value.
$($vis)* fn kill<S: $crate::Storage>(storage: &S) {
$($vis)* fn kill<S: $crate::GenericStorage>(storage: &S) {
storage.kill($key)
}

/// Take and remove the value from the provided storage instance.
$($vis)* fn take<S: $crate::Storage>(storage: &S) -> Option<$ty> {
$($vis)* fn take<S: $crate::GenericStorage>(storage: &S) -> Option<$ty> {
storage.take($key)
}
}

impl $crate::StorageValue<$ty> for $name {
impl $crate::storage::generator::StorageValue<$ty> for $name {
fn key() -> &'static [u8] {
$key
}

fn load<S: $crate::Storage>(storage: &S) -> Option<$ty> {
fn load<S: $crate::GenericStorage>(storage: &S) -> Option<$ty> {
$name::load(storage)
}

fn store<S: $crate::Storage>(val: &$ty, storage: &S) {
fn store<S: $crate::GenericStorage>(val: &$ty, storage: &S) {
$name::store(val, storage)
}

fn kill<S: $crate::Storage>(storage: &S) {
fn kill<S: $crate::GenericStorage>(storage: &S) {
$name::kill(storage)
}

fn take<S: $crate::Storage>(storage: &S) -> Option<$ty> {
fn take<S: $crate::GenericStorage>(storage: &S) -> Option<$ty> {
$name::take(storage)
}
}
Expand All @@ -209,30 +209,30 @@ macro_rules! __storage_items_internal {
}

/// Load the value associated with the given key from the map.
$($vis)* fn get<S: $crate::Storage>(key: &$kty, storage: &S) -> Option<$ty> {
$($vis)* fn get<S: $crate::GenericStorage>(key: &$kty, storage: &S) -> Option<$ty> {
let key = $name::key_for(key);
storage.load(&key[..])
}

/// Store a value to be associated with the given key from the map.
$($vis)* fn insert<S: $crate::Storage>(key: &$kty, val: &$ty, storage: &S) {
$($vis)* fn insert<S: $crate::GenericStorage>(key: &$kty, val: &$ty, storage: &S) {
let key = $name::key_for(key);
storage.store(&key[..], val);
}

/// Remove the value from storage.
$($vis)* fn remove<S: $crate::Storage>(key: &$kty, storage: &S) {
$($vis)* fn remove<S: $crate::GenericStorage>(key: &$kty, storage: &S) {
storage.kill(&$name::key_for(key)[..]);
}

/// Take the value, reading and removing it.
$($vis)* fn take<S: $crate::Storage>(key: &$kty, storage: &S) -> Option<$ty> {
$($vis)* fn take<S: $crate::GenericStorage>(key: &$kty, storage: &S) -> Option<$ty> {
let key = $name::key_for(key);
storage.take(&key[..])
}
}

impl $crate::StorageMap<$kty, $ty> for $name {
impl $crate::storage::generator::StorageMap<$kty, $ty> for $name {
fn prefix() -> &'static [u8] {
$prefix
}
Expand All @@ -241,19 +241,19 @@ macro_rules! __storage_items_internal {
$name::key_for(x)
}

fn get<S: $crate::Storage>(key: &$kty, storage: &S) -> Option<$ty> {
fn get<S: $crate::GenericStorage>(key: &$kty, storage: &S) -> Option<$ty> {
$name::get(key, storage)
}

fn insert<S: $crate::Storage>(key: &$kty, val: &$ty, storage: &S) {
fn insert<S: $crate::GenericStorage>(key: &$kty, val: &$ty, storage: &S) {
$name::insert(key, val, storage)
}

fn remove<S: $crate::Storage>(key: &$kty, storage: &S) {
fn remove<S: $crate::GenericStorage>(key: &$kty, storage: &S) {
$name::remove(key, storage)
}

fn take<S: $crate::Storage>(key: &$kty, storage: &S) -> Option<$ty> {
fn take<S: $crate::GenericStorage>(key: &$kty, storage: &S) -> Option<$ty> {
$name::take(key, storage)
}
}
Expand Down Expand Up @@ -285,58 +285,58 @@ macro_rules! __storage_items_internal {
}

/// Read out all the items.
$($vis)* fn items<S: $crate::Storage>(storage: &S) -> Vec<$ty> {
$($vis)* fn items<S: $crate::GenericStorage>(storage: &S) -> Vec<$ty> {
(0..$name::len(storage))
.map(|i| $name::get(i, storage).expect("all items within length are set; qed"))
.collect()
}

/// Set the current set of items.
$($vis)* fn set_items<S: $crate::Storage>(items: &[$ty], storage: &S) {
$($vis)* fn set_items<S: $crate::GenericStorage>(items: &[$ty], storage: &S) {
$name::set_len(items.len() as u32, storage);
items.iter()
.enumerate()
.for_each(|(i, item)| $name::set_item(i as u32, item, storage));
}

$($vis)* fn set_item<S: $crate::Storage>(index: u32, item: &$ty, storage: &S) {
$($vis)* fn set_item<S: $crate::GenericStorage>(index: u32, item: &$ty, storage: &S) {
if index < $name::len(storage) {
storage.store(&$name::key_for(index)[..], item);
}
}

/// Load the value at given index. Returns `None` if the index is out-of-bounds.
$($vis)* fn get<S: $crate::Storage>(index: u32, storage: &S) -> Option<$ty> {
$($vis)* fn get<S: $crate::GenericStorage>(index: u32, storage: &S) -> Option<$ty> {
storage.load(&$name::key_for(index)[..])
}

/// Load the length of the list.
$($vis)* fn len<S: $crate::Storage>(storage: &S) -> u32 {
$($vis)* fn len<S: $crate::GenericStorage>(storage: &S) -> u32 {
storage.load(&$name::len_key()).unwrap_or_default()
}

/// Clear the list.
$($vis)* fn clear<S: $crate::Storage>(storage: &S) {
$($vis)* fn clear<S: $crate::GenericStorage>(storage: &S) {
for i in 0..$name::len(storage) {
$name::clear_item(i, storage);
}

storage.kill(&$name::len_key()[..])
}

fn clear_item<S: $crate::Storage>(index: u32, storage: &S) {
fn clear_item<S: $crate::GenericStorage>(index: u32, storage: &S) {
if index < $name::len(storage) {
storage.kill(&$name::key_for(index));
}
}

fn set_len<S: $crate::Storage>(count: u32, storage: &S) {
fn set_len<S: $crate::GenericStorage>(count: u32, storage: &S) {
(count..$name::len(storage)).for_each(|i| $name::clear_item(i, storage));
storage.store(&$name::len_key(), &count);
}
}

impl $crate::StorageList<$ty> for $name {
impl $crate::storage::generator::StorageList<$ty> for $name {
/// Get the prefix key in storage.
fn prefix() -> &'static [u8] {
$prefix
Expand All @@ -354,29 +354,29 @@ macro_rules! __storage_items_internal {
}

/// Read out all the items.
fn items<S: $crate::Storage>(storage: &S) -> Vec<$ty> {
fn items<S: $crate::GenericStorage>(storage: &S) -> Vec<$ty> {
$name::items(storage)
}

/// Set the current set of items.
fn set_items<S: $crate::Storage>(items: &[$ty], storage: &S) {
fn set_items<S: $crate::GenericStorage>(items: &[$ty], storage: &S) {
$name::set_items(items, storage)
}

fn set_item<S: $crate::Storage>(index: u32, item: &$ty, storage: &S) {
fn set_item<S: $crate::GenericStorage>(index: u32, item: &$ty, storage: &S) {
$name::set_item(index, item, storage)
}

/// Load the value at given index. Returns `None` if the index is out-of-bounds.
fn get<S: $crate::Storage>(index: u32, storage: &S) -> Option<$ty> {
fn get<S: $crate::GenericStorage>(index: u32, storage: &S) -> Option<$ty> {
$name::get(index, storage)
}

fn len<S: $crate::Storage>(storage: &S) -> u32 {
fn len<S: $crate::GenericStorage>(storage: &S) -> u32 {
$name::len(storage)
}

fn clear<S: $crate::Storage>(storage: &S) {
fn clear<S: $crate::GenericStorage>(storage: &S) {
$name::clear(storage)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use rstd::prelude::*;
use runtime_io::{self, twox_128};
use codec::{Slicable, KeyedVec, Input};

pub mod generator;

// TODO: consider using blake256 to avoid possible preimage attack.

struct IncrementalInput<'a> {
Expand Down Expand Up @@ -119,8 +121,12 @@ pub fn put_raw(key: &[u8], value: &[u8]) {
runtime_io::set_storage(&twox_128(key)[..], value)
}

/// A trait for working with `storage-wrapper` values under the substrate storage API.
pub trait StorageValue {

}

/// A trait to conveniently store a vector of storable data.
// TODO: add iterator support
pub trait StorageVec {
type Item: Default + Sized + Slicable;
const PREFIX: &'static [u8];
Expand Down Expand Up @@ -163,7 +169,7 @@ pub trait StorageVec {
}

pub mod unhashed {
use super::{runtime_io, Slicable, KeyedVec, Vec, IncrementalInput};
use super::{runtime_io, Slicable, Vec, IncrementalInput};

/// 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> {
Expand Down Expand Up @@ -245,49 +251,6 @@ pub mod unhashed {
pub fn put_raw(key: &[u8], value: &[u8]) {
runtime_io::set_storage(key, value)
}

/// A trait to conveniently store a vector of storable data.
// TODO: add iterator support
pub trait StorageVec {
type Item: Default + Sized + Slicable;
const PREFIX: &'static [u8];

/// Get the current set of items.
fn items() -> Vec<Self::Item> {
(0..Self::count()).into_iter().map(Self::item).collect()
}

/// Set the current set of items.
fn set_items(items: &[Self::Item]) {
Self::set_count(items.len() as u32);
items.iter().enumerate().for_each(|(v, ref i)| Self::set_item(v as u32, i));
}

fn set_item(index: u32, item: &Self::Item) {
if index < Self::count() {
put(&index.to_keyed_vec(Self::PREFIX), item);
}
}

fn clear_item(index: u32) {
if index < Self::count() {
kill(&index.to_keyed_vec(Self::PREFIX));
}
}

fn item(index: u32) -> Self::Item {
get_or_default(&index.to_keyed_vec(Self::PREFIX))
}

fn set_count(count: u32) {
(count..Self::count()).for_each(Self::clear_item);
put(&b"len".to_keyed_vec(Self::PREFIX), &count);
}

fn count() -> u32 {
get_or_default(&b"len".to_keyed_vec(Self::PREFIX))
}
}
}

#[cfg(test)]
Expand Down
7 changes: 0 additions & 7 deletions substrate/storage-wrapper/Cargo.toml

This file was deleted.