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
Show all changes
33 commits
Select commit Hold shift + click to select a range
a83059a
Completely rework dispatch mechanism into something modular.
gavofyork Mar 13, 2018
82a178d
Council vote tests.
gavofyork Mar 13, 2018
1d28d9a
Fix tests.
gavofyork Mar 13, 2018
69c88c3
whitespace.
gavofyork Mar 13, 2018
b12a708
Fix demo runtime tests.
gavofyork Mar 14, 2018
e48e86d
Merge branch 'gav-demo' into gav-dispatch
gavofyork Mar 14, 2018
8e3cc51
Fix up tests.
gavofyork Mar 14, 2018
53e2fdf
Merge branch 'gav-demo' into gav-dispatch
gavofyork Mar 14, 2018
27ecd6f
Merge branch 'master' into gav-dispatch
gavofyork Mar 14, 2018
5eca74a
Remove dead code.
gavofyork Mar 14, 2018
7cece3b
Merge branch 'master' into gav-dispatch
gavofyork Mar 14, 2018
8c2396d
Timestamp uses new storage API.
gavofyork Mar 14, 2018
6b6c240
Move over system module to new API.
gavofyork Mar 14, 2018
a79dab2
Much nicer storage API, moved over staking module.
gavofyork Mar 15, 2018
1fd6b3e
More refactoring.
gavofyork Mar 15, 2018
51b4a8c
Democracy uses new storage API.
gavofyork Mar 15, 2018
8ada9f7
Council uses new RPC.
gavofyork Mar 16, 2018
c4f5f42
Fix more tests.
gavofyork Mar 16, 2018
d11f5ca
Use match for Id
gavofyork Mar 16, 2018
faa0a44
Use match for Id
gavofyork Mar 16, 2018
7912de1
Make PrivPass better protected.
gavofyork Mar 19, 2018
aac3899
Address other grumbles.
gavofyork Mar 19, 2018
26519c6
Give PrivPass a private member.
gavofyork Mar 19, 2018
9f32ea7
Testing PrivPass.
gavofyork Mar 19, 2018
8966951
Add docs.
gavofyork Mar 19, 2018
91d5e75
Merge branch 'gav-dispatch' into gav-storage-revamp
gavofyork Mar 19, 2018
7ee81e6
Recompile binaries after merge.
gavofyork Mar 19, 2018
33215e4
Merge branch 'master' into gav-storage-revamp
gavofyork Mar 19, 2018
cf9659e
Remove duplicated code.
gavofyork Mar 19, 2018
b8c0832
New binaries.
gavofyork Mar 19, 2018
57a9a79
Docs
gavofyork Mar 19, 2018
59c7a1a
Docs
gavofyork Mar 19, 2018
6f8f456
avoid use of (arguably) confusing terminology.
gavofyork Mar 19, 2018
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 over system module to new API.
  • Loading branch information
gavofyork committed Mar 14, 2018
commit 6b6c240d6d6fb906f6c574e2785b90da5a9b7b21
6 changes: 3 additions & 3 deletions demo/runtime/src/genesismap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use codec::{KeyedVec, Joiner};
use std::collections::HashMap;
use runtime_io::twox_128;
use runtime_support::Hashable;
use runtime_support::{Hashable, StorageMap};
use primitives::Block;
use demo_primitives::{BlockNumber, AccountId};
use runtime::staking::Balance;
Expand Down Expand Up @@ -113,7 +113,7 @@ impl GenesisConfig {
)
.map(|(k, v)| (twox_128(&k[..])[..].to_vec(), v.to_vec()))
.chain(vec![
(system::CODE[..].into(), wasm_runtime),
(b":code".to_vec(), wasm_runtime),
(consensus::AUTHORITY_COUNT[..].into(), vec![].and(&(self.authorities.len() as u32))),
].into_iter())
.chain(self.authorities.iter()
Expand All @@ -127,6 +127,6 @@ impl GenesisConfig {
pub fn additional_storage_with_genesis(genesis_block: &Block) -> HashMap<Vec<u8>, Vec<u8>> {
use codec::Slicable;
map![
twox_128(&0u64.to_keyed_vec(system::BLOCK_HASH_AT)).to_vec() => genesis_block.header.blake2_256().encode()
system::BlockHash::key_for(&0) => genesis_block.header.blake2_256().encode()
]
}
27 changes: 13 additions & 14 deletions demo/runtime/src/runtime/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ use rstd::prelude::*;
use rstd::mem;
use runtime_io::{print, storage_root, enumerated_trie_root};
use codec::{KeyedVec, Slicable};
use runtime_support::{Hashable, storage};
use runtime_support::{Hashable, storage, StorageValue, StorageMap};
use environment::with_env;
use demo_primitives::{AccountId, Hash, TxOrder, BlockNumber, Header, Log};
use block::Block;
use transaction::UncheckedTransaction;
use runtime::{staking, session};
use dispatch;

pub const NONCE_OF: &[u8] = b"sys:non:";
pub const BLOCK_HASH_AT: &[u8] = b"sys:old:";
pub const CODE: &[u8] = b"sys:cod";

storage_items! {
pub Nonce: b"sys:non" => map [ AccountId => TxOrder ];
pub BlockHash: b"sys:old" => map [ BlockNumber => [u8; 32] ];
}

/// The current block number being processed. Set by `execute_block`.
pub fn block_number() -> BlockNumber {
with_env(|e| e.block_number)
}

/// Get the block hash of a given block (uses storage).
pub fn block_hash(number: BlockNumber) -> Hash {
storage::get_or_default(&number.to_keyed_vec(BLOCK_HASH_AT))
pub fn block_hash(number: BlockNumber) -> Option<Hash> {
BlockHash::get(&number).map(Into::into)
}

pub struct PrivPass;
Expand Down Expand Up @@ -146,12 +146,11 @@ fn execute_transaction(utx: UncheckedTransaction) {

{
// check nonce
let nonce_key = tx.signed.to_keyed_vec(NONCE_OF);
let expected_nonce: TxOrder = storage::get_or(&nonce_key, 0);
let expected_nonce: TxOrder = Nonce::get_or(&tx.signed, 0);
assert!(tx.nonce == expected_nonce, "All transactions should have the correct nonce");

// increment nonce in storage
storage::put(&nonce_key, &(expected_nonce + 1));
Nonce::insert(&tx.signed, &(expected_nonce + 1));
}

// decode parameters and dispatch
Expand All @@ -164,7 +163,7 @@ fn initial_checks(block: &Block) {

// check parent_hash is correct.
assert!(
header.number > 0 && block_hash(header.number - 1) == header.parent_hash,
header.number > 0 && block_hash(header.number - 1).map(|h| h == header.parent_hash).unwrap_or(false),
"Parent hash should be valid."
);

Expand Down Expand Up @@ -193,7 +192,7 @@ fn final_checks(block: &Block) {
fn post_finalise(header: &Header) {
// store the header hash in storage; we can't do it before otherwise there would be a
// cyclic dependency.
storage::put(&header.number.to_keyed_vec(BLOCK_HASH_AT), &header.blake2_256());
BlockHash::insert(&header.number, &header.blake2_256());
}

#[cfg(feature = "std")]
Expand Down Expand Up @@ -221,7 +220,7 @@ pub mod testing {

pub fn externalities() -> TestExternalities {
map![
twox_128(&0u64.to_keyed_vec(BLOCK_HASH_AT)).to_vec() => [69u8; 32].encode()
twox_128(&BlockHash::key_for(&0)).to_vec() => [69u8; 32].encode()
]
}
}
Expand Down Expand Up @@ -278,7 +277,7 @@ mod tests {
let h = Header {
parent_hash: [69u8; 32].into(),
number: 1,
state_root: hex!("584e0c1f4d4b96153591e3906d756762493dffeb5fa7159e7107014aec8d9c3d").into(),
state_root: hex!("c3cd675eefaa269502ee6dc1af7b9941c0be462eb31a5273c00cec40a8ace837").into(),
transaction_root: hex!("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421").into(),
digest: Digest { logs: vec![], },
};
Expand Down
14 changes: 6 additions & 8 deletions demo/runtime/src/runtime/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,21 @@ use runtime::staking::PublicPass;
pub type Timestamp = u64;

storage_items! {
pub now: b"tim:val" => Timestamp;
pub Now: b"tim:val" => Timestamp;
}

/// Get the current time.
pub fn get() -> Timestamp {
now::get_or_default()
}
pub fn get() -> Timestamp { Now::require() }

impl_dispatch! {
pub mod public;
fn set(new_now: Timestamp) = 0;
fn set(now: Timestamp) = 0;
}

impl<'a> public::Dispatch for PublicPass<'a> {
/// Set the current time.
fn set(self, new_now: Timestamp) {
now::put(&new_now);
fn set(self, now: Timestamp) {
Now::put(&now);
}
}

Expand All @@ -57,7 +55,7 @@ mod tests {
#[test]
fn timestamp_works() {
let mut t: TestExternalities = map![
twox_128(now::key()).to_vec() => vec![].and(&42u64)
twox_128(Now::key()).to_vec() => vec![].and(&42u64)
];

with_externalities(&mut t, || {
Expand Down
2 changes: 1 addition & 1 deletion substrate/runtime-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ pub use self::storage::generator::Storage as GenericStorage;
pub mod storage;
mod hashable;

pub use self::storage::StorageVec;
pub use self::storage::{StorageVec, StorageValue, StorageMap};
pub use self::hashable::Hashable;
34 changes: 17 additions & 17 deletions substrate/runtime-support/src/storage/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,24 +186,24 @@ macro_rules! __storage_items_internal {

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

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

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

/// Take the value, reading and removing it.
fn take<S: $crate::GenericStorage>(key: &$kty, storage: &S) -> Option<$ty> {
let key = $name::key_for(key);
let key = <$name as $crate::storage::generator::StorageMap<$kty, $ty>>::key_for(key);
storage.take(&key[..])
}
}
Expand All @@ -214,14 +214,14 @@ macro_rules! __storage_items_internal {

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

fn set_len<S: $crate::GenericStorage>(count: u32, storage: &S) {
(count..$name::len(storage)).for_each(|i| $name::clear_item(i, storage));
storage.put(&$name::len_key(), &count);
(count..<$name as $crate::storage::generator::StorageList<$ty>>::len(storage)).for_each(|i| $name::clear_item(i, storage));
storage.put(&<$name as $crate::storage::generator::StorageList<$ty>>::len_key(), &count);
}
}

Expand All @@ -248,8 +248,8 @@ macro_rules! __storage_items_internal {

/// Read out all the items.
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"))
(0..<$name as $crate::storage::generator::StorageList<$ty>>::len(storage))
.map(|i| <$name as $crate::storage::generator::StorageList<$ty>>::get(i, storage).expect("all items within length are set; qed"))
.collect()
}

Expand All @@ -258,32 +258,32 @@ macro_rules! __storage_items_internal {
$name::set_len(items.len() as u32, storage);
items.iter()
.enumerate()
.for_each(|(i, item)| $name::set_item(i as u32, item, storage));
.for_each(|(i, item)| <$name as $crate::storage::generator::StorageList<$ty>>::set_item(i as u32, item, storage));
}

fn set_item<S: $crate::GenericStorage>(index: u32, item: &$ty, storage: &S) {
if index < $name::len(storage) {
storage.put(&$name::key_for(index)[..], item);
if index < <$name as $crate::storage::generator::StorageList<$ty>>::len(storage) {
storage.put(&<$name as $crate::storage::generator::StorageList<$ty>>::key_for(index)[..], item);
}
}

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

/// Load the length of the list.
fn len<S: $crate::GenericStorage>(storage: &S) -> u32 {
storage.get(&$name::len_key()).unwrap_or_default()
storage.get(&<$name as $crate::storage::generator::StorageList<$ty>>::len_key()).unwrap_or_default()
}

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

storage.kill(&$name::len_key()[..])
storage.kill(&<$name as $crate::storage::generator::StorageList<$ty>>::len_key()[..])
}
}
};
Expand Down
6 changes: 6 additions & 0 deletions substrate/runtime-support/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ pub trait StorageValue<T: Slicable> {
/// Load the value from the provided storage instance.
fn get() -> Option<T>;

/// Load the value from the provided storage instance.
fn require() -> T { Self::get().expect("Required values must be present") }

/// Load the value from the provided storage instance.
fn get_or(alt: T) -> T { Self::get().unwrap_or(alt) }

Expand Down Expand Up @@ -279,6 +282,9 @@ pub trait StorageMap<K: Slicable, V: Slicable> {
/// Load the value associated with the given key from the map.
fn get(key: &K) -> Option<V>;

/// Load the value from the provided storage instance.
fn require(key: &K) -> V { Self::get(key).expect("Required values must be present") }

/// Load the value from the provided storage instance.
fn get_or(key: &K, alt: V) -> V { Self::get(key).unwrap_or(alt) }

Expand Down