Compact format for block number, nonce, balance (#910)

* Try to make everything compact

* Make `Clone` work

* Fix

* Move contracts and balances over to compact encoding

* Session and timestamp are compact

* Sataking uses compact numbers

* Treasury now compact

* Compact Democracy

* Council is compact

* Fix
This commit is contained in:
Gav Wood
2018-10-16 22:47:30 +02:00
committed by Bastian Köcher
parent 54a0f5b204
commit 8bc5242c92
69 changed files with 728 additions and 661 deletions
+28 -20
View File
@@ -51,6 +51,7 @@ extern crate srml_timestamp as timestamp;
use rstd::prelude::*;
use rstd::cmp;
use codec::{HasCompact, Compact};
use runtime_support::{Parameter, StorageValue, StorageMap};
use runtime_support::dispatch::Result;
use session::OnSessionChange;
@@ -75,14 +76,16 @@ pub enum LockStatus<BlockNumber: Parameter> {
/// Preference of what happens on a slash event.
#[derive(PartialEq, Eq, Clone, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
pub struct ValidatorPrefs<Balance> {
pub struct ValidatorPrefs<Balance: HasCompact + Copy> { // TODO: @bkchr shouldn't need this Copy but derive(Encode) breaks otherwise
/// Validator should ensure this many more slashes than is necessary before being unstaked.
#[codec(compact)]
pub unstake_threshold: u32,
// Reward that validator takes up-front; only the rest is split between themselves and nominators.
#[codec(encoded_as = "<Balance as HasCompact>::Type")]
pub validator_payment: Balance,
}
impl<B: Default> Default for ValidatorPrefs<B> {
impl<B: Default + HasCompact + Copy> Default for ValidatorPrefs<B> {
fn default() -> Self {
ValidatorPrefs {
unstake_threshold: 3,
@@ -103,16 +106,16 @@ decl_module! {
#[cfg_attr(feature = "std", serde(bound(deserialize = "T::Balance: ::serde::de::DeserializeOwned")))]
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn stake(origin) -> Result;
fn unstake(origin, intentions_index: u32) -> Result;
fn unstake(origin, intentions_index: Compact<u32>) -> Result;
fn nominate(origin, target: Address<T::AccountId, T::AccountIndex>) -> Result;
fn unnominate(origin, target_index: u32) -> Result;
fn register_preferences(origin, intentions_index: u32, prefs: ValidatorPrefs<T::Balance>) -> Result;
fn unnominate(origin, target_index: Compact<u32>) -> Result;
fn register_preferences(origin, intentions_index: Compact<u32>, prefs: ValidatorPrefs<T::Balance>) -> Result;
fn set_sessions_per_era(new: T::BlockNumber) -> Result;
fn set_bonding_duration(new: T::BlockNumber) -> Result;
fn set_validator_count(new: u32) -> Result;
fn set_sessions_per_era(new: <T::BlockNumber as HasCompact>::Type) -> Result;
fn set_bonding_duration(new: <T::BlockNumber as HasCompact>::Type) -> Result;
fn set_validator_count(new: Compact<u32>) -> Result;
fn force_new_era(apply_rewards: bool) -> Result;
fn set_offline_slash_grace(new: u32) -> Result;
fn set_offline_slash_grace(new: Compact<u32>) -> Result;
}
}
@@ -243,8 +246,9 @@ impl<T: Trait> Module<T> {
/// Retract the desire to stake for the transactor.
///
/// Effects will be felt at the beginning of the next era.
fn unstake(origin: T::Origin, intentions_index: u32) -> Result {
fn unstake(origin: T::Origin, intentions_index: Compact<u32>) -> Result {
let who = ensure_signed(origin)?;
let intentions_index: u32 = intentions_index.into();
// unstake fails in degenerate case of having too few existing staked parties
if Self::intentions().len() <= Self::minimum_validator_count() as usize {
return Err("cannot unstake when there are too few staked participants")
@@ -275,8 +279,9 @@ impl<T: Trait> Module<T> {
/// Will panic if called when source isn't currently nominating target.
/// Updates Nominating, NominatorsFor and NominationBalance.
fn unnominate(origin: T::Origin, target_index: u32) -> Result {
fn unnominate(origin: T::Origin, target_index: Compact<u32>) -> Result {
let source = ensure_signed(origin)?;
let target_index: u32 = target_index.into();
let target_index = target_index as usize;
let target = <Nominating<T>>::get(&source).ok_or("Account must be nominating")?;
@@ -305,10 +310,11 @@ impl<T: Trait> Module<T> {
/// An error (no-op) if `Self::intentions()[intentions_index] != origin`.
fn register_preferences(
origin: T::Origin,
intentions_index: u32,
intentions_index: Compact<u32>,
prefs: ValidatorPrefs<T::Balance>
) -> Result {
let who = ensure_signed(origin)?;
let intentions_index: u32 = intentions_index.into();
if Self::intentions().get(intentions_index as usize) != Some(&who) {
return Err("Invalid index")
@@ -322,20 +328,21 @@ impl<T: Trait> Module<T> {
// PRIV DISPATCH
/// Set the number of sessions in an era.
fn set_sessions_per_era(new: T::BlockNumber) -> Result {
<NextSessionsPerEra<T>>::put(&new);
fn set_sessions_per_era(new: <T::BlockNumber as HasCompact>::Type) -> Result {
<NextSessionsPerEra<T>>::put(new.into());
Ok(())
}
/// The length of the bonding duration in eras.
fn set_bonding_duration(new: T::BlockNumber) -> Result {
<BondingDuration<T>>::put(&new);
fn set_bonding_duration(new: <T::BlockNumber as HasCompact>::Type) -> Result {
<BondingDuration<T>>::put(new.into());
Ok(())
}
/// The length of a staking era in sessions.
fn set_validator_count(new: u32) -> Result {
<ValidatorCount<T>>::put(&new);
fn set_validator_count(new: Compact<u32>) -> Result {
let new: u32 = new.into();
<ValidatorCount<T>>::put(new);
Ok(())
}
@@ -353,8 +360,9 @@ impl<T: Trait> Module<T> {
/// Set the offline slash grace period.
fn set_offline_slash_grace(new: u32) -> Result {
<OfflineSlashGrace<T>>::put(&new);
fn set_offline_slash_grace(new: Compact<u32>) -> Result {
let new: u32 = new.into();
<OfflineSlashGrace<T>>::put(new);
Ok(())
}
+15 -11
View File
@@ -75,7 +75,7 @@ fn note_offline_grace_should_work() {
with_externalities(&mut new_test_ext(0, 3, 3, 0, true, 10), || {
Balances::set_free_balance(&10, 70);
Balances::set_free_balance(&20, 70);
assert_ok!(Staking::set_offline_slash_grace(1));
assert_ok!(Staking::set_offline_slash_grace(1.into()));
assert_eq!(Staking::offline_slash_grace(), 1);
assert_eq!(Staking::slash_count(&10), 0);
@@ -130,7 +130,7 @@ fn note_offline_auto_unstake_session_change_should_work() {
with_externalities(&mut new_test_ext(0, 3, 3, 0, true, 10), || {
Balances::set_free_balance(&10, 7000);
Balances::set_free_balance(&20, 7000);
assert_ok!(Staking::register_preferences(Origin::signed(10), 0, ValidatorPrefs { unstake_threshold: 1, validator_payment: 0 }));
assert_ok!(Staking::register_preferences(Origin::signed(10), 0.into(), ValidatorPrefs { unstake_threshold: 1, validator_payment: 0 }));
assert_eq!(Staking::intentions(), vec![10, 20]);
@@ -236,7 +236,7 @@ fn staking_should_work() {
assert_eq!(Staking::validator_count(), 2);
assert_eq!(Session::validators(), vec![10, 20]);
assert_ok!(Staking::set_bonding_duration(2));
assert_ok!(Staking::set_bonding_duration(2.into()));
assert_eq!(Staking::bonding_duration(), 2);
// Block 1: Add three validators. No obvious change.
@@ -257,7 +257,7 @@ fn staking_should_work() {
// Block 3: Unstake highest, introduce another staker. No change yet.
System::set_block_number(3);
assert_ok!(Staking::stake(Origin::signed(3)));
assert_ok!(Staking::unstake(Origin::signed(4), Staking::intentions().iter().position(|&x| x == 4).unwrap() as u32));
assert_ok!(Staking::unstake(Origin::signed(4), (Staking::intentions().iter().position(|&x| x == 4).unwrap() as u32).into()));
assert_eq!(Staking::current_era(), 1);
Session::check_rotate_session(System::block_number());
@@ -269,7 +269,7 @@ fn staking_should_work() {
// Block 5: Transfer stake from highest to lowest. No change yet.
System::set_block_number(5);
assert_ok!(Balances::transfer(Origin::signed(4), 1.into(), 40));
assert_ok!(Balances::transfer(Origin::signed(4), 1.into(), 40.into()));
Session::check_rotate_session(System::block_number());
// Block 6: Lowest now validator.
@@ -279,7 +279,7 @@ fn staking_should_work() {
// Block 7: Unstake three. No change yet.
System::set_block_number(7);
assert_ok!(Staking::unstake(Origin::signed(3), Staking::intentions().iter().position(|&x| x == 3).unwrap() as u32));
assert_ok!(Staking::unstake(Origin::signed(3), (Staking::intentions().iter().position(|&x| x == 3).unwrap() as u32).into()));
Session::check_rotate_session(System::block_number());
assert_eq!(Session::validators(), vec![1, 3]);
@@ -312,7 +312,7 @@ fn nominating_and_rewards_should_work() {
assert_eq!(Balances::total_balance(&4), 40);
System::set_block_number(2);
assert_ok!(Staking::unnominate(Origin::signed(4), 0));
assert_ok!(Staking::unnominate(Origin::signed(4), 0.into()));
Session::check_rotate_session(System::block_number());
assert_eq!(Staking::current_era(), 2);
assert_eq!(Session::validators(), vec![3, 2]);
@@ -323,7 +323,7 @@ fn nominating_and_rewards_should_work() {
System::set_block_number(3);
assert_ok!(Staking::stake(Origin::signed(4)));
assert_ok!(Staking::unstake(Origin::signed(3), Staking::intentions().iter().position(|&x| x == 3).unwrap() as u32));
assert_ok!(Staking::unstake(Origin::signed(3), (Staking::intentions().iter().position(|&x| x == 3).unwrap() as u32).into()));
assert_ok!(Staking::nominate(Origin::signed(3), 1.into()));
Session::check_rotate_session(System::block_number());
assert_eq!(Session::validators(), vec![1, 4]);
@@ -355,7 +355,11 @@ fn rewards_with_off_the_table_should_work() {
assert_eq!(Balances::total_balance(&3), 30);
System::set_block_number(2);
assert_ok!(Staking::register_preferences(Origin::signed(1), Staking::intentions().into_iter().position(|i| i == 1).unwrap() as u32, ValidatorPrefs { unstake_threshold: 3, validator_payment: 4 }));
assert_ok!(Staking::register_preferences(
Origin::signed(1),
(Staking::intentions().into_iter().position(|i| i == 1).unwrap() as u32).into(),
ValidatorPrefs { unstake_threshold: 3, validator_payment: 4 }
));
Session::check_rotate_session(System::block_number());
assert_eq!(Balances::total_balance(&1), 22);
assert_eq!(Balances::total_balance(&2), 37);
@@ -441,7 +445,7 @@ fn staking_eras_work() {
// Block 3: Schedule an era length change; no visible changes.
System::set_block_number(3);
assert_ok!(Staking::set_sessions_per_era(3));
assert_ok!(Staking::set_sessions_per_era(3.into()));
Session::check_rotate_session(System::block_number());
assert_eq!(Session::current_index(), 3);
assert_eq!(Staking::sessions_per_era(), 2);
@@ -487,7 +491,7 @@ fn staking_balance_transfer_when_bonded_should_not_work() {
with_externalities(&mut new_test_ext(0, 1, 3, 1, false, 0), || {
Balances::set_free_balance(&1, 111);
assert_ok!(Staking::stake(Origin::signed(1)));
assert_noop!(Balances::transfer(Origin::signed(1), 2.into(), 69), "cannot transfer illiquid funds");
assert_noop!(Balances::transfer(Origin::signed(1), 2.into(), 69.into()), "cannot transfer illiquid funds");
});
}