Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
4def98e
TODOs
gavofyork Dec 18, 2019
338362a
Remove superfluous:
gavofyork Dec 18, 2019
0c22a21
Merge remote-tracking branch 'origin/master' into gav-lazy-rewards
gui1117 Jan 7, 2020
f92f0e8
partial implementation
gui1117 Jan 7, 2020
bb71353
full implementation
gui1117 Jan 8, 2020
3a4a9be
fix preferences
gui1117 Jan 9, 2020
7a80d10
Merge remote-tracking branch 'origin/master' into gav-lazy-rewards
gui1117 Jan 9, 2020
5345095
update comments
gui1117 Jan 9, 2020
dbe3a64
upgrade test WIP
gui1117 Jan 9, 2020
f41ee81
fix more tests
gui1117 Jan 10, 2020
fd48681
fix cutoff
gui1117 Jan 10, 2020
fa38f84
fix saturation
gui1117 Jan 10, 2020
c4b0f2f
comment
gui1117 Jan 10, 2020
4315218
upgrade mock
gui1117 Jan 10, 2020
a26e9da
upgrade test
gui1117 Jan 10, 2020
cc6dd63
Merge remote-tracking branch 'origin/master' into gav-lazy-rewards
gui1117 Jan 13, 2020
245ec98
WIP migration
gui1117 Jan 13, 2020
4206435
WIP migration
gui1117 Jan 14, 2020
f1586b2
Merge commit '857f7338b2066a03dc956acf004b101b4bdcf593' into gav-lazy…
gui1117 Jan 14, 2020
8e0bfa4
Merge commit 'd63ff7b9ac8d766b87a7a2cd99d5d73a521f97a9' into gav-lazy…
gui1117 Jan 14, 2020
70b9ff8
remove slot stake stuff
gui1117 Jan 14, 2020
61881d6
Merge commit '6688c3be1ff71803d5963935df7a0d646b2d50d2' into gav-lazy…
gui1117 Jan 14, 2020
fd5bafc
Merge remote-tracking branch 'origin/master' into gav-lazy-rewards
gui1117 Jan 14, 2020
f392d4a
fix merge
gui1117 Jan 14, 2020
2b9f806
migration of ledger
gui1117 Jan 14, 2020
d0431f4
remove equalize from test
gui1117 Jan 14, 2020
10232de
add test
gui1117 Jan 14, 2020
6367fda
fix
gui1117 Jan 14, 2020
30eb57c
update doc
gui1117 Jan 14, 2020
8eb8a58
fix compilation
gui1117 Jan 14, 2020
eb0d4e5
improve test readibility
gui1117 Jan 15, 2020
54b670f
Merge remote-tracking branch 'origin/master' into gav-lazy-rewards
gui1117 Jan 16, 2020
16a6fd0
improve doc
gui1117 Jan 20, 2020
ceffbfd
Merge commit 'c3af86cee45d98dd5f97fb2de419ba4e25ac14b7' into gav-lazy…
gui1117 Jan 20, 2020
c2cda09
Merge commit '561bd727489501cfb24d29020062b3a5d4fdcca9' into gav-lazy…
gui1117 Jan 20, 2020
a241d95
Merge remote-tracking branch 'origin/master' into gav-lazy-rewards
gui1117 Jan 20, 2020
3d426bb
fix most todo
gui1117 Jan 20, 2020
5d4c2e5
fix migration and test
gui1117 Jan 21, 2020
3590769
remove println
gui1117 Jan 21, 2020
df3a4de
WIP
gui1117 Jan 21, 2020
70979ac
add test and spec
gui1117 Jan 22, 2020
3c56ab3
weight
gui1117 Jan 22, 2020
c088756
update doc
gui1117 Jan 22, 2020
72ff737
safer end_era
gui1117 Jan 30, 2020
58a2c90
fix exposure of conversion
gui1117 Jan 30, 2020
02c05b2
Merge remote-tracking branch 'origin/master' into gav-lazy-rewards
gui1117 Jan 30, 2020
dd7590f
Revert "safer end_era"
gui1117 Jan 30, 2020
c99772b
fix useless put
gui1117 Jan 30, 2020
eb1a6ef
exposure clipped
gui1117 Jan 30, 2020
4e1d12d
doc
gui1117 Jan 30, 2020
33afe32
fix payout with clipped
gui1117 Jan 30, 2020
35f76ab
fix node runtime
gui1117 Jan 30, 2020
d35dafc
add doc
gui1117 Jan 31, 2020
381d5ef
pluggable and generalized staking module
gui1117 Jan 31, 2020
2519aef
remove print
gui1117 Jan 31, 2020
e881add
update doc
gui1117 Jan 31, 2020
1f7b353
refactor
gui1117 Jan 31, 2020
086eb8d
improve documentation and implementation
gui1117 Feb 1, 2020
5162c0b
fix test
gui1117 Feb 1, 2020
1d9f460
Merge remote-tracking branch 'origin/master' into gav-lazy-rewards
gui1117 Feb 1, 2020
9cd88e2
Fix test
gui1117 Feb 1, 2020
68105c8
fix test
gui1117 Feb 1, 2020
827847b
fix test
gui1117 Feb 1, 2020
e2af768
fix remove lowest stake from exposure, not biggest.
gui1117 Feb 3, 2020
ea4ef67
nomination index arguments in nominator_payout
gui1117 Feb 3, 2020
74a4cbe
add test
gui1117 Feb 3, 2020
0116e20
Merge remote-tracking branch 'origin/master' into gav-lazy-rewards
gui1117 Feb 3, 2020
faaddec
try to fix offence
gui1117 Feb 3, 2020
20b28b3
apply slashed and bond eras until active era
gui1117 Feb 3, 2020
7dd7975
doc
gui1117 Feb 3, 2020
a89202e
update spec version
gui1117 Feb 3, 2020
3f860bd
Merge branch 'master' into gav-lazy-rewards
gavofyork Feb 12, 2020
8e652b1
Merge branch 'master' into gav-lazy-rewards
gavofyork Feb 12, 2020
89974f8
Merge remote-tracking branch 'origin/master' into gav-lazy-rewards
gui1117 Feb 19, 2020
c87a3d6
add test upgrade from previous test environment
gui1117 Feb 19, 2020
961618d
Apply suggestions from code review
gui1117 Feb 21, 2020
751b993
nominators upgrade has been cleaned
gui1117 Feb 21, 2020
e56e70a
dynamic history depth implementation
gui1117 Feb 21, 2020
975a6eb
make current_era - history_depth included
gui1117 Feb 21, 2020
3d78f79
Change equality check to start era to less than or equal
shawntabrizi Feb 22, 2020
b834b4e
Use era specific validator prefs
shawntabrizi Feb 23, 2020
23a4b2b
Add print statement and comment about start era if <
shawntabrizi Feb 23, 2020
45bf05b
fix next_reward overflow
gui1117 Feb 24, 2020
3dce1b0
make more check for bad era claim for zero cost
gui1117 Feb 24, 2020
22c46b5
small refactor
gui1117 Feb 24, 2020
2f70fbe
Merge branch 'master' into gav-lazy-rewards
shawntabrizi Feb 24, 2020
24d334f
code refactor + fix use of deprecated storage
gui1117 Feb 26, 2020
7edfdae
fix wasm build
gui1117 Feb 26, 2020
5627b2b
Merge remote-tracking branch 'origin/master' into gav-lazy-rewards
gui1117 Feb 26, 2020
4caa747
add comment
gui1117 Feb 26, 2020
015c39a
Fix tests
shawntabrizi Feb 28, 2020
f7ba093
Merge remote-tracking branch 'origin/master' into gav-lazy-rewards
gui1117 Mar 2, 2020
c4d27b2
remove outdated comment
gui1117 Mar 2, 2020
129a6d9
Apply suggestions from code review
gui1117 Mar 3, 2020
d5b3080
gather active era information into one storage
gui1117 Mar 3, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
WIP migration
  • Loading branch information
gui1117 committed Jan 14, 2020
commit 42064353c5cc7fb40dde354abce793c54dc0e795
47 changes: 32 additions & 15 deletions frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@
//! The **reward and slashing** procedure is the core of the Staking module, attempting to _embrace
//! valid behavior_ while _punishing any misbehavior or lack of availability_.
//!
//! Reward must be claimed by stakers for each era before it gets too old by $HISTORY_DEPTH.
//!
//! Slashing can occur at any point in time, once misbehavior is reported. Once slashing is
//! determined, a value is deducted from the balance of the validator and all the nominators who
//! voted for this validator (values are deducted from the _stash_ account of the slashed entity).
Expand Down Expand Up @@ -165,14 +167,6 @@
//!
//! ## Implementation Details
//!
//! ### Slot Stake
//!
//! The term [`SlotStake`](./struct.Module.html#method.slot_stake) will be used throughout this
//! section. It refers to a value calculated at the end of each era, containing the _minimum value
//! at stake among all validators._ Note that a validator's value at stake might be a combination
//! of the validator's own stake and the votes it received. See [`Exposure`](./struct.Exposure.html)
//! for more details.
//!
//! ### Reward Calculation
//!
//! Validators and nominators are rewarded at the end of each era. The total reward of an era is
Expand Down Expand Up @@ -314,7 +308,7 @@ pub struct EraRewardPoints<AccountId: Ord> {
individual: BTreeMap<AccountId, RewardPoint>,
}

/// Deprecated
/// Deprecated. Used for migration only.
// Reward points of an era. Used to split era total payout between validators.
#[derive(Encode, Decode, Default)]
pub struct EraPoints {
Expand Down Expand Up @@ -382,6 +376,17 @@ pub struct UnlockChunk<Balance: HasCompact> {
era: EraIndex,
}

/// Deprecated. Used for migration only.
#[derive(Encode, Decode)]
struct StakingLedgerV1<AccountId, Balance: HasCompact> {
stash: AccountId,
#[codec(compact)]
total: Balance,
#[codec(compact)]
active: Balance,
unlocking: Vec<UnlockChunk<Balance>>,
}

/// The ledger of a (bonded) stash.
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
pub struct StakingLedger<AccountId, Balance: HasCompact> {
Expand All @@ -400,10 +405,8 @@ pub struct StakingLedger<AccountId, Balance: HasCompact> {
pub unlocking: Vec<UnlockChunk<Balance>>,
/// The next era at which the staker can claim reward.
pub next_reward: EraIndex,
// TODO: ^^^ this will need migration logic.
}


impl<
AccountId,
Balance: HasCompact + Copy + Saturating + SimpleArithmetic,
Expand Down Expand Up @@ -584,6 +587,8 @@ pub trait SessionInterface<AccountId>: frame_system::Trait {
fn validators() -> Vec<AccountId>;
/// Prune historical session tries up to but not including the given index.
fn prune_historical_up_to(up_to: SessionIndex);
/// The current session index.
fn current_index() -> SessionIndex;
}

impl<T: Trait> SessionInterface<<T as frame_system::Trait>::AccountId> for T where
Expand All @@ -608,6 +613,10 @@ impl<T: Trait> SessionInterface<<T as frame_system::Trait>::AccountId> for T whe
fn prune_historical_up_to(up_to: SessionIndex) {
<pallet_session::historical::Module<T>>::prune_up_to(up_to);
}

fn current_index() -> SessionIndex {
<pallet_session::Module<T>>::current_index()
}
}

pub trait Trait: frame_system::Trait {
Expand Down Expand Up @@ -804,10 +813,18 @@ decl_storage! {
/// Deprecated
// Rewards for the current era. Using indices of current elected set.
CurrentEraPointsEarned: EraPoints;
}
add_extra_genesis {
config(stakers):
Vec<(T::AccountId, T::AccountId, BalanceOf<T>, StakerStatus<T::AccountId>)>;

/// Deprecated
// Nominators for a particular account that is in action right now. You can't iterate
// through validators here, but you can find them in the Session module.
//
// This is keyed by the stash account.
Stakers: map T::AccountId => Exposure<T::AccountId, BalanceOf<T>>;

}
add_extra_genesis {
config(stakers):
Vec<(T::AccountId, T::AccountId, BalanceOf<T>, StakerStatus<T::AccountId>)>;
build(|config: &GenesisConfig<T>| {
for &(ref stash, ref controller, balance, ref status) in &config.stakers {
assert!(
Expand Down
61 changes: 39 additions & 22 deletions frame/staking/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ pub const CURRENT_VERSION: VersionNumber = 2;

#[cfg(any(test, feature = "migrate"))]
mod inner {
use crate::{Store, Module, Trait};
use frame_support::{StorageLinkedMap, StorageValue};
use crate::{
Store, Module, Trait, EraRewardPoints, SessionInterface, BalanceOf, StakingLedger,
StakingLedgerV1,
};
use frame_support::{StorageLinkedMap, StorageValue, StorageMap, StorageDoubleMap, StoragePrefixedMap};
use sp_std::vec::Vec;
use super::{CURRENT_VERSION, VersionNumber};
use sp_runtime::traits::Zero;

// the minimum supported version of the migration logic.
const MIN_SUPPORTED_VERSION: VersionNumber = 0;
Expand Down Expand Up @@ -60,52 +64,65 @@ mod inner {
frame_support::print("Finished migrating Staking storage to v1.");
}


pub fn from_v1_to_v2<T: Trait>(version: &mut VersionNumber) {
if *version != 1 { return }
*version += 1;

// Fill new storages.

// TODO TODO: ActiveEra.
// we can't just compare current elected and T::SessionInterface::validators() because we
// could be in a new era while having the same set, we need to compare
// CurrentEraStartSessionIndex with session::current_index
// Or: what if you put a wrong one ?
// Do ActiveEra
let current_era_start_index = <Module<T> as Store>::CurrentEraStartSessionIndex::get();
let current_era = <Module<T> as Store>::CurrentEra::get();
let active_era = if current_era_start_index > T::SessionInterface::current_index() {
current_era - 1
} else {
current_era
};
<Module<T> as Store>::ActiveEra::put(active_era);

// Do ErasStakers, ErasValidatorPrefs and ErasTotalStake
let current_era = <Module<T> as Store>::CurrentEra::get();
let active_era = <Module<T> as Store>::ActiveEra::get();
let old_current_elected = <Module<T>>::CurrentElected::get();
let mut current_total_stake = 0.into();
let old_current_elected = <Module<T> as Store>::CurrentElected::get();
let mut current_total_stake = <BalanceOf<T>>::zero();
for validator in &old_current_elected {
let exposure = <Module<T> as Store>::Stakers::get(elected);
let exposure = <Module<T> as Store>::Stakers::get(validator);
current_total_stake += exposure.total;
let pref = <Module<T> as Store>::Validator::get(elected);
let pref = <Module<T> as Store>::Validators::get(validator);
<Module<T> as Store>::ErasStakers::insert(current_era, validator, exposure);
<Module<T> as Store>::ErasValidatorPrefs::insert(current_era, validator, exposure);
<Module<T> as Store>::ErasValidatorPrefs::insert(current_era, validator, pref);
}
<Module<T> as Store>::ErasTotalStake::insert(current_era, current_total_stake);
let mut active_total_stake = 0.into();
for validator in T::SessionInterface::validators() {
let exposure = <Module<T> as Store>::Stakers::get(elected);
let mut active_total_stake = <BalanceOf<T>>::zero();
for validator in &T::SessionInterface::validators() {
let exposure = <Module<T> as Store>::Stakers::get(validator);
active_total_stake += exposure.total;
let pref = <Module<T> as Store>::Validator::get(elected);
let pref = <Module<T> as Store>::Validators::get(validator);
<Module<T> as Store>::ErasStakers::insert(active_era, validator, exposure);
<Module<T> as Store>::ErasValidatorPrefs::insert(active_era, validator, exposure);
<Module<T> as Store>::ErasValidatorPrefs::insert(active_era, validator, pref);
}
<Module<T> as Store>::ErasTotalStake::insert(active_era, active_total_stake);

// Do ErasRewardPoints
let points = <Module<T> as Store>::CurrentEraPointsEarned::get();
<Module<T> as Store>::ErasRewardPoints::insert(active_era, EraRewardPoints {
total: points.total,
individual: current_elected.zip(points.individual).collect(),
// TODO TODO: this or zip with active_validators?
})
individual: old_current_elected.iter().cloned().zip(points.individual.iter().cloned()).collect(),
});

// Do ActiveEraStart
<Module<T> as Store>::ActiveEraStart::put(<Module<T> as Store>::CurrentEraStart::get());

// TODO TODO: StakingLedger
// let res = <Module<T> as Store>::Ledger::translate_value(
// |old: StakingLedgerV1<T::AccountId, BalanceOf<T>>| StakingLedger {
// stash: old.stash,
// total: old.total,
// active: old.active,
// unlocking: old.unlocking,
// next_reward: 0,
// }
// );

// Kill old storages
<Module<T> as Store>::SlotStake::kill();
<Module<T> as Store>::CurrentElected::kill();
Expand Down