Staking rate targeting and specific rewards. (#2882)

* PNPoS implementation

* wip: change staking api

* code readibility

* fix overflow

* comment

* license

* doc

* reorganize a bit

* rename to proper english + doc

* address comments

* refactor unused mock

* fix add_point

* update tests

* add not equalize to ci

* Revert "add not equalize to ci"

This reverts commit 1c61c25874b04b1cdfadd7d353b75a189a59ad35.

* bring test back

* update locks

* fix genesis config

* add authorship event handler + test

* uncouple timestamp from staking

* use on finalize instead

* remove todo

* simplify mock

* address comment

* doc

* merge test

* fmt

* remove todo todo

* move add_reward_point to regular function

* doc

* doc

* increase version

* doc and fmt

* Update srml/staking/src/inflation.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Fix some doc typos
This commit is contained in:
thiolliere
2019-07-24 19:25:43 +02:00
committed by Gavin Wood
parent 1c346d2190
commit 2b6f664caf
11 changed files with 711 additions and 206 deletions
+43 -10
View File
@@ -23,9 +23,10 @@ use primitives::testing::{Header, UintAuthorityId};
use substrate_primitives::{H256, Blake2Hasher};
use runtime_io;
use srml_support::{assert_ok, impl_outer_origin, parameter_types, EnumerableStorageMap};
use srml_support::traits::{Currency, Get};
use crate::{EraIndex, GenesisConfig, Module, Trait, StakerStatus,
ValidatorPrefs, RewardDestination, Nominators
use srml_support::traits::{Currency, Get, FindAuthor};
use crate::{
EraIndex, GenesisConfig, Module, Trait, StakerStatus, ValidatorPrefs, RewardDestination,
Nominators, inflation
};
/// The AccountId alias in this test module.
@@ -86,6 +87,16 @@ impl_outer_origin!{
pub enum Origin for Test {}
}
/// Author of block is always 11
pub struct Author11;
impl FindAuthor<u64> for Author11 {
fn find_author<'a, I>(_digests: I) -> Option<u64>
where I: 'a + IntoIterator<Item=(srml_support::ConsensusEngineId, &'a [u8])>
{
Some(11)
}
}
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Test;
@@ -97,7 +108,7 @@ parameter_types! {
impl system::Trait for Test {
type Origin = Origin;
type Index = u64;
type BlockNumber = u64;
type BlockNumber = BlockNumber;
type Hash = H256;
type Hashing = ::primitives::traits::BlakeTwo256;
type AccountId = AccountId;
@@ -132,6 +143,7 @@ impl balances::Trait for Test {
parameter_types! {
pub const Period: BlockNumber = 1;
pub const Offset: BlockNumber = 0;
pub const UncleGenerations: u64 = 0;
}
impl session::Trait for Test {
type OnSessionEnding = session::historical::NoteHistoricalRoot<Test, Staking>;
@@ -148,7 +160,12 @@ impl session::historical::Trait for Test {
type FullIdentification = crate::Exposure<AccountId, Balance>;
type FullIdentificationOf = crate::ExposureOf<Test>;
}
impl authorship::Trait for Test {
type FindAuthor = Author11;
type UncleGenerations = UncleGenerations;
type FilterUncle = ();
type EventHandler = Module<Test>;
}
parameter_types! {
pub const MinimumPeriod: u64 = 5;
}
@@ -163,6 +180,7 @@ parameter_types! {
}
impl Trait for Test {
type Currency = balances::Module<Self>;
type Time = timestamp::Module<Self>;
type CurrencyToVote = CurrencyToVoteHandler;
type OnRewardMinted = ();
type Event = ();
@@ -175,7 +193,6 @@ impl Trait for Test {
pub struct ExtBuilder {
existential_deposit: u64,
reward: u64,
validator_pool: bool,
nominate: bool,
validator_count: u32,
@@ -188,7 +205,6 @@ impl Default for ExtBuilder {
fn default() -> Self {
Self {
existential_deposit: 0,
reward: 10,
validator_pool: false,
nominate: true,
validator_count: 2,
@@ -261,6 +277,8 @@ impl ExtBuilder {
(41, balance_factor * 2000),
(100, 2000 * balance_factor),
(101, 2000 * balance_factor),
// This allow us to have a total_payout different from 0.
(999, 1_000_000_000_000),
],
vesting: vec![],
}.assimilate_storage(&mut t, &mut c);
@@ -285,9 +303,7 @@ impl ExtBuilder {
],
validator_count: self.validator_count,
minimum_validator_count: self.minimum_validator_count,
session_reward: Perbill::from_millionths((1000000 * self.reward / balance_factor) as u32),
offline_slash: Perbill::from_percent(5),
current_session_reward: self.reward,
offline_slash_grace: 0,
invulnerables: vec![],
}.assimilate_storage(&mut t, &mut c);
@@ -378,8 +394,9 @@ pub fn bond_nominator(acc: u64, val: u64, target: Vec<u64>) {
pub fn start_session(session_index: session::SessionIndex) {
// Compensate for session delay
let session_index = session_index + 1;
for i in 0..(session_index - Session::current_index()) {
for i in Session::current_index()..session_index {
System::set_block_number((i + 1).into());
Timestamp::set_timestamp(System::block_number());
Session::on_initialize(System::block_number());
}
@@ -391,6 +408,22 @@ pub fn start_era(era_index: EraIndex) {
assert_eq!(Staking::current_era(), era_index);
}
pub fn current_total_payout_for_duration(duration: u64) -> u64 {
let res = inflation::compute_total_payout(
<Module<Test>>::slot_stake()*2,
Balances::total_issuance(),
duration,
);
res
}
pub fn add_reward_points_to_all_elected() {
for v in <Module<Test>>::current_elected() {
<Module<Test>>::add_reward_points_to_validator(v, 1);
}
}
pub fn validator_controllers() -> Vec<AccountId> {
Session::validators().into_iter().map(|s| Staking::bonded(&s).expect("no controller for validator")).collect()
}