Refactor and timestamp test

This commit is contained in:
Gav
2018-01-19 08:49:38 +01:00
parent f179adbc4e
commit ba0d71a551
9 changed files with 110 additions and 72 deletions
@@ -3,6 +3,11 @@ use storage::Storage;
use primitives::{BlockNumber, Balance, AccountID};
use runtime::{system, session};
// Each validator's stake has one amount in each of three states:
// - inactive: free to be transferred
// - active: currently representing a validator
// - deactivating: recently representing a validator and not yet ready for transfer
/// The length of a staking era in blocks.
pub fn era_length() -> BlockNumber {
sessions_per_era() * session::length()
@@ -13,21 +18,26 @@ pub fn sessions_per_era() -> BlockNumber {
Storage::into(b"sta\0spe")
}
/// The length of a staking era in sessions.
pub fn lockup_eras() -> BlockNumber {
Storage::into(b"sta\0lpe")
}
/// The era has changed - enact new staking set.
///
/// NOTE: This always happens on a session change.
pub fn next_era() {
fn next_era() {
// TODO: evaluate desired staking amounts and nominations and optimise to find the best
// combination of validators, then use session::set_validators().
}
/// The balance of a given account.
pub fn balance(who: &AccountID) -> Balance {
pub fn balance_inactive(who: &AccountID) -> Balance {
Storage::into(&who.to_keyed_vec(b"sta\0bal\0"))
}
/// Transfer some unlocked staking balance to another staker.
pub fn transfer_stake(transactor: &AccountID, dest: &AccountID, value: Balance) {
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);
assert!(from_balance >= value);
@@ -42,14 +52,14 @@ pub fn transfer_stake(transactor: &AccountID, dest: &AccountID, value: Balance)
///
/// Effects will be felt at the beginning of the next era.
pub fn stake(_transactor: &AccountID) {
unimplemented!()
// TODO: record the desire for `_transactor` to activate their stake.
}
/// Retract the desire to stake for the transactor.
///
/// Effects will be felt at the beginning of the next era.
pub fn unstake(_transactor: &AccountID) {
unimplemented!()
// TODO: record the desire for `_transactor` to deactivate their stake.
}
/// Hook to be called prior to transaction processing.
@@ -88,8 +98,8 @@ mod tests {
], };
with_externalities(&mut t, || {
assert_eq!(staking::balance(&one), 42);
assert_eq!(staking::balance(&two), 0);
assert_eq!(staking::balance_inactive(&one), 42);
assert_eq!(staking::balance_inactive(&two), 0);
});
}
@@ -103,9 +113,9 @@ mod tests {
], };
with_externalities(&mut t, || {
staking::transfer_stake(&one, &two, 69);
assert_eq!(staking::balance(&one), 42);
assert_eq!(staking::balance(&two), 69);
staking::transfer_inactive(&one, &two, 69);
assert_eq!(staking::balance_inactive(&one), 42);
assert_eq!(staking::balance_inactive(&two), 69);
});
}
}