Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
d723499
wip: poc
wischli Jul 20, 2022
82da1d9
refactor: extrinsics and inherents
wischli Jul 28, 2022
03cd34d
refactor: remove storage redundancy
wischli Jul 29, 2022
766c777
tests: fix existing ones for staking
wischli Jul 29, 2022
791ef9c
tests: 80%
wischli Aug 1, 2022
38e92dd
feat: rm delegate_another_candidate and more
wischli Aug 1, 2022
d8bb955
docs: remove deprecated weight
wischli Aug 1, 2022
24dc4bf
Apply suggestions from code review
wischli Aug 2, 2022
7789cda
tests: 100%
wischli Aug 2, 2022
870b534
fix: benchmarks
wischli Aug 3, 2022
8926e4c
feat: remove multiple delegations (#391)
wischli Aug 8, 2022
3dd9c6d
Merge remote-tracking branch 'origin/wf/1930-staking-refactor' into w…
wischli Aug 8, 2022
05f520e
Merge branch 'develop' into wf/1930-staking-refactor
wischli Aug 8, 2022
f8c1419
Apply suggestions from code review
wischli Aug 9, 2022
ad13650
fix: suggestions from @ntn-x2 review
wischli Aug 9, 2022
adbcd46
fmt
wischli Aug 9, 2022
ddebe3a
fix: typos
wischli Aug 9, 2022
28e47ef
Merge remote-tracking branch 'origin/develop' into wf/1930-staking-re…
wischli Aug 10, 2022
7b9fda3
fix: docker file
wischli Aug 11, 2022
3e21d0c
Merge remote-tracking branch 'origin/develop' into wf/1930-staking-re…
wischli Sep 7, 2022
e70c044
feat: add staking rewards runtime api
wischli Sep 8, 2022
b1de42a
docs: add new extrinsics to staking header
wischli Sep 8, 2022
6f73296
feat: add staking rates api
wischli Sep 8, 2022
20fd523
tests: add api
wischli Sep 8, 2022
0f8160b
fix: unify staking runtime api
wischli Sep 9, 2022
420170d
refactor: stricter claiming
wischli Sep 15, 2022
25f1878
Merge remote-tracking branch 'origin/develop' into wf/1930-staking-re…
wischli Sep 15, 2022
a3b2631
fix: easy suggestions from code review
wischli Sep 16, 2022
15424e0
refactor: separate runtime api from custom rpc
wischli Sep 16, 2022
e703493
tests: comp api claim with claiming
wischli Sep 19, 2022
a00364b
refactor: move runtime api calls to mod
wischli Sep 19, 2022
02eb407
stlye: fmt
wischli Sep 19, 2022
379c736
refactor: split api into separate crates
wischli Sep 19, 2022
dfbd9e3
Merge remote-tracking branch 'origin/develop' into wf/1930-staking-re…
wischli Oct 27, 2022
bc0890f
Merge remote-tracking branch 'origin/develop' into wf/1930-staking-re…
wischli Oct 28, 2022
c5548db
Merge remote-tracking branch 'origin/wf/1930-staking-refactor' into w…
wischli Oct 28, 2022
a9f58bc
refactor: simplify staking api name
wischli Oct 28, 2022
01d09bb
refactor: normalize public creds runtime api
wischli Oct 28, 2022
4f30a8a
refactor: machete cleanup
wischli Oct 28, 2022
36e4abd
tests: add safety check to API rewards
wischli Oct 31, 2022
56a5539
style: fmt
wischli Oct 31, 2022
98eab08
Merge branch 'develop' into wf/1930-staking-refactor
wischli Oct 31, 2022
30044dd
feat: write delegator state migration
wischli Sep 16, 2022
55620d2
refactor: remove unneeded no_std flags
wischli Oct 31, 2022
30be731
refactor: Delegator interface functions
wischli Nov 1, 2022
55c6ac3
refactor: rm Option for Delegator.owner
wischli Nov 1, 2022
cd9684e
style: apply turbofish to staking
wischli Nov 1, 2022
f459050
fix: replace RewardCount with two counters
wischli Nov 2, 2022
39e3716
refactor: rm col input for del stake adjustment
wischli Nov 2, 2022
bf304e5
Merge remote-tracking branch 'origin/wf/1930-staking-refactor' into w…
wischli Nov 2, 2022
f87f01e
style: fmt
wischli Nov 3, 2022
f6f73aa
Merge remote-tracking branch 'origin/develop' into wf-1930-staking-re…
wischli Nov 3, 2022
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
fix: suggestions from @ntn-x2 review
  • Loading branch information
wischli committed Aug 9, 2022
commit ad13650e92a6c87335b1ac3374bfc03531c6025f
2 changes: 1 addition & 1 deletion pallets/parachain-staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ benchmarks! {
assert_eq!(pallet_balances::Pallet::<T>::usable_balance(&beneficiary), (amount + amount).into());
}

execute_pending_reward_change {
exectue_scheduled_reward_change {
// we need at least 1 collators
let n in 0 .. T::MaxTopCandidates::get();
// we need at least 1 delegator
Expand Down
6 changes: 3 additions & 3 deletions pallets/parachain-staking/src/default_weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub trait WeightInfo {
fn increment_delegator_rewards() -> Weight;
fn increment_collator_rewards(_m: u32, ) -> Weight;
fn claim_rewards_for() -> Weight;
fn execute_pending_reward_change(n: u32, m: u32, ) -> Weight;
fn exectue_scheduled_reward_change(n: u32, m: u32, ) -> Weight;
}

/// Weights for parachain_staking using the Substrate node and recommended hardware.
Expand Down Expand Up @@ -421,7 +421,7 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Storage: ParachainStaking CounterForCandidatePool (r:1 w:0)
/// The range of component `n` is `[0, 75]`.
/// The range of component `m` is `[0, 35]`.
fn execute_pending_reward_change(n: u32, m: u32, ) -> Weight {
fn exectue_scheduled_reward_change(n: u32, m: u32, ) -> Weight {
(0 as Weight)
// Standard Error: 5_730_000
.saturating_add((202_623_000 as Weight).saturating_mul(n as Weight))
Expand Down Expand Up @@ -782,7 +782,7 @@ impl WeightInfo for () {
// Storage: ParachainStaking CounterForCandidatePool (r:1 w:0)
/// The range of component `n` is `[0, 75]`.
/// The range of component `m` is `[0, 35]`.
fn execute_pending_reward_change(n: u32, m: u32, ) -> Weight {
fn exectue_scheduled_reward_change(n: u32, m: u32, ) -> Weight {
(0 as Weight)
// Standard Error: 5_730_000
.saturating_add((202_623_000 as Weight).saturating_mul(n as Weight))
Expand Down
21 changes: 15 additions & 6 deletions pallets/parachain-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,10 +647,17 @@ pub mod pallet {
#[pallet::getter(fn last_reward_reduction)]
pub(crate) type LastRewardReduction<T: Config> = StorageValue<_, T::BlockNumber, ValueQuery>;

/// The counter of accumulated rewards for a collator candidate.
/// The counter of accumulated rewards for an account.
///
/// It maps from collator accounts to the number of block authored by them
/// since the last payout.
/// For collators, it reflects the number of authored blocks since the last
/// reward raise. Thus, everytime a collator authors a block, the
/// counter is increased. It is reset, when the collator increments their
/// rewards.
///
/// For delegators, it is used to determine the difference between the
/// delegator and corresponding collator when incrementing the delegator's
/// rewards. In this case, the counter is never incremented but reset to the
/// collator one when the delegator reward increment happens.
// TODO: Maybe rather use u64. Assuming 30 validators, u32 would suffice for 27 years of constant 12s blocktime.
#[pallet::storage]
#[pallet::getter(fn reward_count)]
Expand Down Expand Up @@ -1778,8 +1785,8 @@ pub mod pallet {
/// before adjusting the inflation.
///
/// Emits `RoundInflationSet`.
#[pallet::weight(<T as Config>::WeightInfo::execute_pending_reward_change(T::MaxTopCandidates::get(), T::MaxDelegatorsPerCollator::get()))]
pub fn execute_pending_reward_change(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
#[pallet::weight(<T as Config>::WeightInfo::exectue_scheduled_reward_change(T::MaxTopCandidates::get(), T::MaxDelegatorsPerCollator::get()))]
pub fn exectue_scheduled_reward_change(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
ensure_signed(origin)?;

let now = frame_system::Pallet::<T>::block_number();
Expand Down Expand Up @@ -1827,7 +1834,7 @@ pub mod pallet {
new_inflation.delegator.reward_rate.per_block,
));

Ok(Some(<T as Config>::WeightInfo::execute_pending_reward_change(
Ok(Some(<T as Config>::WeightInfo::exectue_scheduled_reward_change(
CandidatePool::<T>::count(),
max_num_delegators.saturated_into(),
))
Expand Down Expand Up @@ -2469,6 +2476,8 @@ pub mod pallet {
Rewards::<T>::mutate(acc, |reward| {
*reward = reward.saturating_add(Self::calc_block_rewards_delegator(stake, diff.into()));
});
// align with collator counter such that incrementing would
// lead to 0 rewards until the collator counter increases
RewardCount::<T>::insert(acc, col_reward_count);

// 4 reads from reward calc
Expand Down
17 changes: 17 additions & 0 deletions pallets/parachain-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,15 @@ pub(crate) fn almost_equal(left: Balance, right: Balance, precision: Perbill) ->
left.max(right) - left.min(right) <= err
}

/// Incrementelly traverses from the current block to the provided one and
/// potentially sets a block authors.
///
/// If for a block `i` the corresponding index of the authors input is set, this
/// account is regarded to be the block author and thus gets noted.
///
/// NOTE: At most, this updates the RewardCount of the block author but does not
/// increment rewards or claim them. Please use `roll_to_claim_rewards` in that
/// case.
pub(crate) fn roll_to(n: BlockNumber, authors: Vec<Option<AccountId>>) {
while System::block_number() < n {
if let Some(Some(author)) = authors.get((System::block_number()) as usize) {
Expand All @@ -367,6 +376,14 @@ pub(crate) fn roll_to(n: BlockNumber, authors: Vec<Option<AccountId>>) {
}

#[allow(unused_must_use)]
/// Incrementelly traverses from the current block to the provided one and
/// potentially sets a block authors.
///
/// If existent, rewards of the block author and their delegators are
/// incremented and claimed.
///
/// If for a block `i` the corresponding index of the authors input is set, this
/// account is regarded to be the block author and thus gets noted.
pub(crate) fn roll_to_claim_rewards(n: BlockNumber, authors: Vec<Option<AccountId>>) {
while System::block_number() < n {
if let Some(Some(author)) = authors.get((System::block_number()) as usize) {
Expand Down
6 changes: 3 additions & 3 deletions pallets/parachain-staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2757,7 +2757,7 @@ fn adjust_reward_rates() {
roll_to_claim_rewards(<Test as Config>::BLOCKS_PER_YEAR + 1, vec![]);
// reward reduction should not happen automatically anymore
assert_eq!(StakePallet::last_reward_reduction(), 0u64);
assert_ok!(StakePallet::execute_pending_reward_change(Origin::signed(1)));
assert_ok!(StakePallet::exectue_scheduled_reward_change(Origin::signed(1)));
assert_eq!(StakePallet::last_reward_reduction(), 1u64);
let inflation_1 = InflationInfo::new(
<Test as Config>::BLOCKS_PER_YEAR,
Expand Down Expand Up @@ -2788,7 +2788,7 @@ fn adjust_reward_rates() {
roll_to_claim_rewards(2 * <Test as Config>::BLOCKS_PER_YEAR + 1, vec![]);
// reward reduction should not happen automatically anymore
assert_eq!(StakePallet::last_reward_reduction(), 1u64);
assert_ok!(StakePallet::execute_pending_reward_change(Origin::signed(1)));
assert_ok!(StakePallet::exectue_scheduled_reward_change(Origin::signed(1)));
assert_eq!(StakePallet::last_reward_reduction(), 2u64);
let inflation_2 = InflationInfo::new(
<Test as Config>::BLOCKS_PER_YEAR,
Expand Down Expand Up @@ -4175,7 +4175,7 @@ fn rewards_yearly_inflation_adjustment() {
});

// execute to trigger reward increment
assert_ok!(StakePallet::execute_pending_reward_change(Origin::signed(1)));
assert_ok!(StakePallet::exectue_scheduled_reward_change(Origin::signed(1)));
(1..=5).for_each(|id| {
assert!(StakePallet::reward_count(id).is_zero(), "acc_id {:?}", id);
assert!(!StakePallet::rewards(id).is_zero(), "acc_id {:?}", id);
Expand Down
14 changes: 6 additions & 8 deletions pallets/parachain-staking/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use frame_support::traits::{Currency, Get};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_runtime::{
traits::{AtLeast32BitUnsigned, Saturating, Zero},
traits::{AtLeast32BitUnsigned, CheckedSub, Saturating, Zero},
RuntimeDebug,
};
use sp_staking::SessionIndex;
Expand Down Expand Up @@ -213,7 +213,7 @@ pub type Delegator<AccountId, Balance> = Stake<Option<AccountId>, Balance>;
impl<AccountId, Balance> Delegator<AccountId, Balance>
where
AccountId: Eq + Ord + Clone + Debug,
Balance: Copy + Add<Output = Balance> + Saturating + PartialOrd + Eq + Ord + Debug + Zero + Default,
Balance: Copy + Add<Output = Balance> + Saturating + PartialOrd + Eq + Ord + Debug + Zero + Default + CheckedSub,
{
/// Adds a new delegation.
///
Expand Down Expand Up @@ -255,12 +255,10 @@ where
/// was not found and Ok(None) if delegated stake would underflow.
pub fn dec_delegation(&mut self, collator: AccountId, less: Balance) -> Result<Option<Balance>, ()> {
if self.owner == Some(collator) {
if self.amount > less {
self.amount = self.amount.saturating_sub(less);
Ok(Some(self.amount))
} else {
Ok(None)
}
Ok(self.amount.checked_sub(&less).map(|new| {
self.amount = new;
self.amount
}))
} else {
Err(())
}
Expand Down
2 changes: 1 addition & 1 deletion runtimes/peregrine/src/weights/parachain_staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ impl<T: frame_system::Config> parachain_staking::WeightInfo for WeightInfo<T> {
// Storage: ParachainStaking CounterForCandidatePool (r:1 w:0)
/// The range of component `n` is `[0, 75]`.
/// The range of component `m` is `[0, 35]`.
fn execute_pending_reward_change(n: u32, m: u32, ) -> Weight {
fn exectue_scheduled_reward_change(n: u32, m: u32, ) -> Weight {
(0 as Weight)
// Standard Error: 5_730_000
.saturating_add((202_623_000 as Weight).saturating_mul(n as Weight))
Expand Down
2 changes: 1 addition & 1 deletion runtimes/spiritnet/src/weights/parachain_staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ impl<T: frame_system::Config> parachain_staking::WeightInfo for WeightInfo<T> {
// Storage: ParachainStaking CounterForCandidatePool (r:1 w:0)
/// The range of component `n` is `[0, 75]`.
/// The range of component `m` is `[0, 35]`.
fn execute_pending_reward_change(n: u32, m: u32, ) -> Weight {
fn exectue_scheduled_reward_change(n: u32, m: u32, ) -> Weight {
(0 as Weight)
// Standard Error: 5_730_000
.saturating_add((202_623_000 as Weight).saturating_mul(n as Weight))
Expand Down