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
@@ -27,14 +27,14 @@
use runtime_std::prelude::*;
use codec::KeyedVec;
use support::{Storable, StorageVec, kill};
use support::storage;
use primitives::{AccountID, Hash, BlockNumber, Proposal};
use runtime::{staking, system, session};
/// The proportion of validators required for a propsal to be approved measured as the number out
/// of 1000.
pub fn approval_ppm_required() -> u32 {
Storable::lookup(b"gov:apr").unwrap_or(1000)
storage::get_or(b"gov:apr", 1000)
}
/// The number of concrete validator approvals required for a proposal to pass.
@@ -49,10 +49,10 @@ pub mod public {
/// Proposal is by the `transactor` and will automatically count as an approval. Transactor must
/// be a current validator. It is illegal to propose when there is already a proposal in effect.
pub fn propose(validator: &AccountID, proposal: &Proposal) {
if Proposal::lookup(b"gov:pro").is_some() {
if storage::exists(b"gov:pro") {
panic!("there may only be one proposal per era.");
}
proposal.store(b"gov:pro");
storage::put(b"gov:pro", proposal);
approve(validator, staking::current_era());
}
@@ -62,17 +62,17 @@ pub mod public {
if era_index != staking::current_era() {
panic!("approval vote applied on non-current era.")
}
if Proposal::lookup(b"gov:pro").is_none() {
if !storage::exists(b"gov:pro") {
panic!("there must be a proposal in order to approve.");
}
if session::validators().into_iter().position(|v| &v == validator).is_none() {
panic!("transactor must be a validator to approve.");
}
let key = validator.to_keyed_vec(b"gov:app:");
if bool::lookup(&key).is_some() {
if storage::exists(&key) {
panic!("transactor may not approve a proposal twice in one era.");
}
true.store(&key);
storage::put(&key, &true);
}
}
@@ -84,7 +84,7 @@ pub mod privileged {
/// validator. `1000` would require the approval of all validators; `667` would require two-thirds
/// (or there abouts) of validators.
pub fn set_approval_ppm_required(ppm: u32) {
ppm.store(b"gov:apr");
storage::put(b"gov:apr", &ppm);
}
}
@@ -94,11 +94,10 @@ pub mod internal {
/// Current era is ending; we should finish up any proposals.
pub fn end_of_an_era() {
// tally up votes for the current proposal, if any. enact if there are sufficient approvals.
if let Some(proposal) = Proposal::lookup(b"gov:pro") {
kill(b"gov:pro");
if let Some(proposal) = storage::take::<Proposal>(b"gov:pro") {
let approvals_required = approvals_required();
let approved = session::validators().into_iter()
.filter_map(|v| bool::take(&v.to_keyed_vec(b"gov:app:")))
.filter_map(|v| storage::take::<bool>(&v.to_keyed_vec(b"gov:app:")))
.take(approvals_required as usize)
.count() as u32;
if approved == approvals_required {
@@ -19,7 +19,7 @@
use runtime_std::prelude::*;
use codec::KeyedVec;
use support::{kill, Storable, StorageVec};
use support::{storage, StorageVec};
use primitives::{AccountID, SessionKey, BlockNumber};
use runtime::{system, staking, consensus};
@@ -30,7 +30,7 @@ pub fn validators() -> Vec<AccountID> {
/// The number of blocks in each session.
pub fn length() -> BlockNumber {
Storable::lookup_default(b"ses:len")
storage::get_or(b"ses:len", 0)
}
/// The number of validators currently.
@@ -40,12 +40,12 @@ pub fn validator_count() -> usize {
/// The current era index.
pub fn current_index() -> BlockNumber {
Storable::lookup_default(b"ses:ind")
storage::get_or(b"ses:ind", 0)
}
/// The block number at which the era length last changed.
pub fn last_length_change() -> BlockNumber {
Storable::lookup_default(b"ses:llc")
storage::get_or(b"ses:llc", 0)
}
pub mod public {
@@ -55,7 +55,7 @@ pub mod public {
/// session.
pub fn set_key(validator: &AccountID, key: &SessionKey) {
// set new value for next session
key.store(&validator.to_keyed_vec(b"ses:nxt:"));
storage::put(&validator.to_keyed_vec(b"ses:nxt:"), key);
}
}
@@ -64,7 +64,7 @@ pub mod privileged {
/// Set a new era length. Won't kick in until the next era change (at current length).
pub fn set_length(new: BlockNumber) {
new.store(b"ses:nln");
storage::put(b"ses:nln", &new);
}
}
@@ -102,21 +102,20 @@ impl StorageVec for ValidatorStorageVec {
/// Move onto next session: register the new authority set.
fn rotate_session() {
// Increment current session index.
(current_index() + 1).store(b"ses:ind");
storage::put(b"ses:ind", &(current_index() + 1));
// Enact era length change.
if let Some(next_len) = u64::lookup(b"ses:nln") {
next_len.store(b"ses:len");
system::block_number().store(b"ses:llc");
kill(b"ses:nln");
if let Some(next_len) = storage::get::<u64>(b"ses:nln") {
storage::put(b"ses:len", &next_len);
storage::put(b"ses:llc", &system::block_number());
storage::kill(b"ses:nln");
}
// Update any changes in session keys.
validators().iter().enumerate().for_each(|(i, v)| {
let k = v.to_keyed_vec(b"ses:nxt:");
if let Some(n) = Storable::lookup(&k) {
if let Some(n) = storage::take(&k) {
consensus::internal::set_authority(i as u32, &n);
kill(&k);
}
});
}
@@ -19,7 +19,7 @@
use runtime_std::prelude::*;
use runtime_std::cell::RefCell;
use codec::KeyedVec;
use support::{Storable, StorageVec};
use support::{storage, StorageVec};
use primitives::{BlockNumber, AccountID};
use runtime::{system, session, governance};
@@ -31,12 +31,12 @@ pub type Bondage = u64;
/// The length of the bonding duration in eras.
pub fn bonding_duration() -> BlockNumber {
Storable::lookup_default(b"sta:loc")
storage::get_default(b"sta:loc")
}
/// The length of a staking era in sessions.
pub fn validator_count() -> usize {
u32::lookup_default(b"sta:vac") as usize
storage::get_default::<u32>(b"sta:vac") as usize
}
/// The length of a staking era in blocks.
@@ -46,27 +46,27 @@ pub fn era_length() -> BlockNumber {
/// The length of a staking era in sessions.
pub fn sessions_per_era() -> BlockNumber {
Storable::lookup_default(b"sta:spe")
storage::get_default(b"sta:spe")
}
/// The current era index.
pub fn current_era() -> BlockNumber {
Storable::lookup_default(b"sta:era")
storage::get_default(b"sta:era")
}
/// The block number at which the era length last changed.
pub fn last_era_length_change() -> BlockNumber {
Storable::lookup_default(b"sta:lec")
storage::get_default(b"sta:lec")
}
/// The balance of a given account.
pub fn balance(who: &AccountID) -> Balance {
Storable::lookup_default(&who.to_keyed_vec(b"sta:bal:"))
storage::get_default(&who.to_keyed_vec(b"sta:bal:"))
}
/// The liquidity-state of a given account.
pub fn bondage(who: &AccountID) -> Bondage {
Storable::lookup_default(&who.to_keyed_vec(b"sta:bon:"))
storage::get_default(&who.to_keyed_vec(b"sta:bon:"))
}
// Each identity's stake may be in one of three bondage states, given by an integer:
@@ -81,14 +81,14 @@ pub mod public {
/// Transfer some unlocked staking balance to another staker.
pub fn transfer(transactor: &AccountID, dest: &AccountID, value: Balance) {
let from_key = transactor.to_keyed_vec(b"sta:bal:");
let from_balance = Balance::lookup_default(&from_key);
let from_balance = storage::get_default::<Balance>(&from_key);
assert!(from_balance >= value);
let to_key = dest.to_keyed_vec(b"sta:bal:");
let to_balance: Balance = Storable::lookup_default(&to_key);
let to_balance: Balance = storage::get_default(&to_key);
assert!(bondage(transactor) <= bondage(dest));
assert!(to_balance + value > to_balance); // no overflow
(from_balance - value).store(&from_key);
(to_balance + value).store(&to_key);
storage::put(&from_key, &(from_balance - value));
storage::put(&to_key, &(to_balance + value));
}
/// Declare the desire to stake for the transactor.
@@ -100,7 +100,7 @@ pub mod public {
assert!(intentions.iter().find(|t| *t == transactor).is_none(), "Cannot stake if already staked.");
intentions.push(transactor.clone());
IntentionStorageVec::set_items(&intentions);
u64::max_value().store(&transactor.to_keyed_vec(b"sta:bon:"));
storage::put(&transactor.to_keyed_vec(b"sta:bon:"), &u64::max_value());
}
/// Retract the desire to stake for the transactor.
@@ -114,7 +114,7 @@ pub mod public {
panic!("Cannot unstake if not already staked.");
}
IntentionStorageVec::set_items(&intentions);
(current_era() + bonding_duration()).store(&transactor.to_keyed_vec(b"sta:bon:"));
storage::put(&transactor.to_keyed_vec(b"sta:bon:"), &(current_era() + bonding_duration()));
}
}
@@ -123,17 +123,17 @@ pub mod privileged {
/// Set the number of sessions in an era.
pub fn set_sessions_per_era(new: BlockNumber) {
new.store(b"sta:nse");
storage::put(b"sta:nse", &new);
}
/// The length of the bonding duration in eras.
pub fn set_bonding_duration(new: BlockNumber) {
new.store(b"sta:loc");
storage::put(b"sta:loc", &new);
}
/// The length of a staking era in sessions.
pub fn set_validator_count(new: usize) {
(new as u32).store(b"sta:vac");
storage::put(b"sta:vac", &(new as u32));
}
}
@@ -164,13 +164,13 @@ fn new_era() {
governance::internal::end_of_an_era();
// Increment current era.
(current_era() + 1).store(b"sta:era");
storage::put(b"sta:era", &(current_era() + 1));
// Enact era length change.
let next_spe: u64 = Storable::lookup_default(b"sta:nse");
let next_spe: u64 = storage::get_default(b"sta:nse");
if next_spe > 0 && next_spe != sessions_per_era() {
next_spe.store(b"sta:spe");
system::block_number().store(b"sta:lec");
storage::put(b"sta:spe", &next_spe);
storage::put(b"sta:lec", &system::block_number());
}
// evaluate desired staking amounts and nominations and optimise to find the best
@@ -196,7 +196,7 @@ mod tests {
use super::internal::*;
use super::public::*;
use super::privileged::*;
use runtime_std::{with_externalities, twox_128};
use codec::{KeyedVec, Joiner};
use support::{one, two, TestExternalities, with_env};
@@ -20,7 +20,7 @@
use runtime_std::prelude::*;
use runtime_std::{mem, print};
use codec::KeyedVec;
use support::{Hashable, Storable, with_env};
use support::{Hashable, storage, with_env};
use primitives::{Block, BlockNumber, Hash, UncheckedTransaction, TxOrder};
use runtime::{staking, session};
@@ -31,7 +31,7 @@ pub fn block_number() -> BlockNumber {
/// Get the block hash of a given block (uses storage).
pub fn block_hash(number: BlockNumber) -> Hash {
Storable::lookup_default(&number.to_keyed_vec(b"sys:old:"))
storage::get_default(&number.to_keyed_vec(b"sys:old:"))
}
pub mod privileged {
@@ -39,7 +39,7 @@ pub mod privileged {
/// Set the new code.
pub fn set_code(new: &[u8]) {
new.store(b":code");
storage::put_raw(b":code", new);
}
}
@@ -77,7 +77,7 @@ pub mod internal {
// store the header hash in storage.
let header_hash_key = header.number.to_keyed_vec(b"sys:old:");
header.blake2_256().store(&header_hash_key);
storage::put(&header_hash_key, &header.blake2_256());
// execute transactions
block.transactions.iter().for_each(execute_transaction);
@@ -102,11 +102,11 @@ pub mod internal {
// check nonce
let nonce_key = tx.signed.to_keyed_vec(b"sys:non:");
let expected_nonce: TxOrder = Storable::lookup_default(&nonce_key);
let expected_nonce: TxOrder = storage::get_default(&nonce_key);
assert!(tx.nonce == expected_nonce, "All transactions should have the correct nonce");
// increment nonce in storage
(expected_nonce + 1).store(&nonce_key);
storage::put(&nonce_key, &(expected_nonce + 1));
// decode parameters and dispatch
tx.function.dispatch(&tx.signed, &tx.input_data);
@@ -16,14 +16,14 @@
//! Timestamp manager: just handles the current timestamp.
use support::Storable;
use support::storage;
/// Representation of a time.
pub type Timestamp = u64;
/// Get the current time.
pub fn get() -> Timestamp {
Storable::lookup_default(b"tim:val")
storage::get_default(b"tim:val")
}
pub mod public {
@@ -31,7 +31,7 @@ pub mod public {
/// Set the current time.
pub fn set(now: Timestamp) {
now.store(b"tim:val")
storage::put(b"tim:val", &now);
}
}