diff --git a/substrate/state_machine/src/lib.rs b/substrate/state_machine/src/lib.rs index e94b2b6078..91622c9bc4 100644 --- a/substrate/state_machine/src/lib.rs +++ b/substrate/state_machine/src/lib.rs @@ -244,7 +244,7 @@ mod tests { fn set_storage(&mut self, key: Vec, value: Vec) { self.storage.insert(key, value); } - + fn chain_id(&self) -> u64 { 42 } } diff --git a/substrate/wasm-runtime/polkadot/src/lib.rs b/substrate/wasm-runtime/polkadot/src/lib.rs index 4a1302a756..7e8cc2233e 100644 --- a/substrate/wasm-runtime/polkadot/src/lib.rs +++ b/substrate/wasm-runtime/polkadot/src/lib.rs @@ -12,7 +12,7 @@ mod codec; mod support; mod runtime; pub use codec::{endiansensitive, streamreader, joiner, slicable, keyedvec}; -pub use support::{primitives, function, environment, storage, storagevec}; +pub use support::{primitives, function, environment, storable}; #[cfg(test)] pub use support::{testing, statichex}; diff --git a/substrate/wasm-runtime/polkadot/src/runtime/consensus.rs b/substrate/wasm-runtime/polkadot/src/runtime/consensus.rs index b773e7e1e6..02bf288493 100644 --- a/substrate/wasm-runtime/polkadot/src/runtime/consensus.rs +++ b/substrate/wasm-runtime/polkadot/src/runtime/consensus.rs @@ -1,5 +1,5 @@ use runtime_support::Vec; -use storagevec::StorageVec; +use storable::StorageVec; use primitives::SessionKey; struct AuthorityStorageVec {} diff --git a/substrate/wasm-runtime/polkadot/src/runtime/session.rs b/substrate/wasm-runtime/polkadot/src/runtime/session.rs index 8e133ad563..66309becaf 100644 --- a/substrate/wasm-runtime/polkadot/src/runtime/session.rs +++ b/substrate/wasm-runtime/polkadot/src/runtime/session.rs @@ -1,7 +1,6 @@ use runtime_support::Vec; use keyedvec::KeyedVec; -use storage::Storage; -use storagevec::StorageVec; +use storable::{kill, Storable, StorageVec}; use primitives::{AccountID, SessionKey, BlockNumber}; use runtime::{system, staking, consensus}; @@ -38,7 +37,7 @@ pub fn set_validators(new: &[AccountID]) { /// The number of blocks in each session. pub fn length() -> BlockNumber { - Storage::into(b"ses\0bps") + Storable::lookup_default(b"ses\0bps") } /// Hook to be called prior to transaction processing. @@ -62,11 +61,13 @@ pub fn post_transactions() { /// Move onto next session: register the new authority set. fn next_session() { - // TODO: Call set_authorities() with any new authorities. validators().iter().enumerate().for_each(|(i, v)| { let k = v.to_keyed_vec(b"ses\0nxt\0"); - if let Some(n) = Storage::try_into(&k) { + if let Some(n) = Storable::lookup(&k) { consensus::set_authority(i as u32, &n); + kill(&k); } - }) + }); } + +// TODO: tests diff --git a/substrate/wasm-runtime/polkadot/src/runtime/staking.rs b/substrate/wasm-runtime/polkadot/src/runtime/staking.rs index ea2116b035..29d6ac0272 100644 --- a/substrate/wasm-runtime/polkadot/src/runtime/staking.rs +++ b/substrate/wasm-runtime/polkadot/src/runtime/staking.rs @@ -1,5 +1,5 @@ use keyedvec::KeyedVec; -use storage::Storage; +use storable::Storable; use primitives::{BlockNumber, Balance, AccountID}; use runtime::{system, session}; @@ -10,7 +10,7 @@ use runtime::{system, session}; /// The length of a staking era in sessions. pub fn lockup_eras() -> BlockNumber { - Storage::into(b"sta\0lpe") + Storable::lookup_default(b"sta\0lpe") } /// The length of a staking era in blocks. @@ -20,12 +20,12 @@ pub fn era_length() -> BlockNumber { /// The length of a staking era in sessions. pub fn sessions_per_era() -> BlockNumber { - Storage::into(b"sta\0spe") + Storable::lookup_default(b"sta\0spe") } /// The current era index. pub fn current_era() -> BlockNumber { - Storage::into(b"sta\0era") + Storable::lookup_default(b"sta\0era") } /// The current era index. @@ -35,7 +35,7 @@ pub fn set_current_era(new: BlockNumber) { /// The block number at which the era length last changed. pub fn last_era_length_change() -> BlockNumber { - Storage::into(b"sta\0lec") + Storable::lookup_default(b"sta\0lec") } /// Set a new era length. Won't kick in until the next era change (at current length). @@ -51,7 +51,7 @@ fn next_era() { set_current_era(current_era() + 1); // Enact era length change. - let next_spe: u64 = Storage::into(b"sta\0nse"); + let next_spe: u64 = Storable::lookup_default(b"sta\0nse"); if next_spe > 0 && next_spe != sessions_per_era() { next_spe.store(b"sta\0spe"); system::block_number().store(b"sta\0lec"); @@ -63,16 +63,16 @@ fn next_era() { /// The balance of a given account. pub fn balance_inactive(who: &AccountID) -> Balance { - Storage::into(&who.to_keyed_vec(b"sta\0bal\0")) + Storable::lookup_default(&who.to_keyed_vec(b"sta\0bal\0")) } /// Transfer some unlocked staking balance to another staker. pub fn transfer_inactive(transactor: &AccountID, dest: &AccountID, value: Balance) { let from_key = transactor.to_keyed_vec(b"sta\0bal\0"); - let from_balance: Balance = Storage::into(&from_key); + let from_balance: Balance = Storable::lookup_default(&from_key); assert!(from_balance >= value); let to_key = dest.to_keyed_vec(b"sta\0bal\0"); - let to_balance: Balance = Storage::into(&to_key); + let to_balance: Balance = Storable::lookup_default(&to_key); assert!(to_balance + value > to_balance); // no overflow (from_balance - value).store(&from_key); (to_balance + value).store(&to_key); diff --git a/substrate/wasm-runtime/polkadot/src/runtime/system.rs b/substrate/wasm-runtime/polkadot/src/runtime/system.rs index d7abd1835a..f6be199625 100644 --- a/substrate/wasm-runtime/polkadot/src/runtime/system.rs +++ b/substrate/wasm-runtime/polkadot/src/runtime/system.rs @@ -1,6 +1,6 @@ use primitives::{Block, BlockNumber, Hash, UncheckedTransaction, TxOrder, Hashable}; use runtime_support::{Vec, swap}; -use storage::Storage; +use storable::Storable; use keyedvec::KeyedVec; use environment::with_env; use runtime::session; @@ -12,7 +12,7 @@ pub fn block_number() -> BlockNumber { /// Get the block hash of a given block (uses storage). pub fn block_hash(number: BlockNumber) -> Hash { - Storage::into(&number.to_keyed_vec(b"sys\0old\0")) + Storable::lookup_default(&number.to_keyed_vec(b"sys\0old\0")) } /// Deposits a log and ensures it matches the blocks log data. @@ -69,7 +69,7 @@ pub fn execute_transaction(utx: &UncheckedTransaction) { // check nonce let nonce_key = tx.signed.to_keyed_vec(b"sys\0non\0"); - let expected_nonce: TxOrder = Storage::into(&nonce_key); + let expected_nonce: TxOrder = Storable::lookup_default(&nonce_key); assert!(tx.nonce == expected_nonce, "All transactions should have the correct nonce"); // increment nonce in storage diff --git a/substrate/wasm-runtime/polkadot/src/runtime/timestamp.rs b/substrate/wasm-runtime/polkadot/src/runtime/timestamp.rs index e0f3d2c04e..2232843b07 100644 --- a/substrate/wasm-runtime/polkadot/src/runtime/timestamp.rs +++ b/substrate/wasm-runtime/polkadot/src/runtime/timestamp.rs @@ -1,8 +1,8 @@ use primitives::Timestamp; -use storage::Storage; +use storable::Storable; pub fn get() -> Timestamp { - Storage::into(b"tim\0val") + Storable::lookup_default(b"tim\0val") } pub fn set(now: Timestamp) { diff --git a/substrate/wasm-runtime/polkadot/src/support/mod.rs b/substrate/wasm-runtime/polkadot/src/support/mod.rs index e4dae46c40..802476cc1b 100644 --- a/substrate/wasm-runtime/polkadot/src/support/mod.rs +++ b/substrate/wasm-runtime/polkadot/src/support/mod.rs @@ -1,8 +1,7 @@ pub mod primitives; pub mod function; pub mod environment; -pub mod storage; -pub mod storagevec; +pub mod storable; #[cfg(test)] pub mod statichex; diff --git a/substrate/wasm-runtime/polkadot/src/support/storable.rs b/substrate/wasm-runtime/polkadot/src/support/storable.rs new file mode 100644 index 0000000000..7d45587338 --- /dev/null +++ b/substrate/wasm-runtime/polkadot/src/support/storable.rs @@ -0,0 +1,62 @@ +use slicable::Slicable; +use endiansensitive::EndianSensitive; +use keyedvec::KeyedVec; +use runtime_support; + +pub trait Storable { + fn lookup_default(key: &[u8]) -> Self where Self: Sized + Default { Self::lookup(key).unwrap_or_else(Default::default) } + fn lookup(_key: &[u8]) -> Option where Self: Sized { unimplemented!() } + fn store(&self, key: &[u8]); +} + +pub fn kill(key: &[u8]) { runtime_support::set_storage(key, b""); } + +impl Storable for T { + fn lookup(key: &[u8]) -> Option { + Slicable::set_as_slice(|out| runtime_support::read_storage(key, out) == out.len()) + } + fn store(&self, key: &[u8]) { + self.as_slice_then(|slice| runtime_support::set_storage(key, slice)); + } +} + +impl Storable for [u8] { + fn store(&self, key: &[u8]) { + runtime_support::set_storage(key, self) + } +} + +/// A trait to conveniently store a vector of storable data. +// TODO: add iterator support +pub trait StorageVec { + type Item: Default + Sized + Storable; + const PREFIX: &'static [u8]; + + /// Get the current set of items. + fn items() -> Vec { + (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) { + item.store(&index.to_keyed_vec(Self::PREFIX)); + } + + fn item(index: u32) -> Self::Item { + Storable::lookup_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)); + } + + fn count() -> u32 { + Storable::lookup_default(&b"len".to_keyed_vec(Self::PREFIX)) + } +} diff --git a/substrate/wasm-runtime/polkadot/src/support/storage.rs b/substrate/wasm-runtime/polkadot/src/support/storage.rs deleted file mode 100644 index 7c9696bc11..0000000000 --- a/substrate/wasm-runtime/polkadot/src/support/storage.rs +++ /dev/null @@ -1,24 +0,0 @@ -use slicable::Slicable; -use endiansensitive::EndianSensitive; -use runtime_support; - -pub trait Storage { - fn into(key: &[u8]) -> Self where Self: Sized + Default { Self::try_into(key).unwrap_or_else(Default::default) } - fn try_into(_key: &[u8]) -> Option where Self: Sized { unimplemented!() } - fn store(&self, key: &[u8]); -} - -impl Storage for T { - fn try_into(key: &[u8]) -> Option { - Slicable::set_as_slice(|out| runtime_support::read_storage(key, out) == out.len()) - } - fn store(&self, key: &[u8]) { - self.as_slice_then(|slice| runtime_support::set_storage(key, slice)); - } -} - -impl Storage for [u8] { - fn store(&self, key: &[u8]) { - runtime_support::set_storage(key, self) - } -} diff --git a/substrate/wasm-runtime/polkadot/src/support/storagevec.rs b/substrate/wasm-runtime/polkadot/src/support/storagevec.rs deleted file mode 100644 index ac0ded3a56..0000000000 --- a/substrate/wasm-runtime/polkadot/src/support/storagevec.rs +++ /dev/null @@ -1,38 +0,0 @@ -use runtime_support::Vec; -use keyedvec::KeyedVec; -use storage::Storage; - -/// A trait to conveniently store a vector of storable data. -// TODO: add iterator support -pub trait StorageVec { - type Item: Default + Sized + Storage; - const PREFIX: &'static [u8]; - - /// Get the current set of items. - fn items() -> Vec { - (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) { - item.store(&index.to_keyed_vec(Self::PREFIX)); - } - - fn item(index: u32) -> Self::Item { - Storage::into(&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)); - } - - fn count() -> u32 { - Storage::into(&b"len".to_keyed_vec(Self::PREFIX)) - } -}