-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
claim_staking_rewards
extrinsic
#2080
Merged
shannonwells
merged 6 commits into
feat/capacity-staking-rewards-impl
from
extrinsic-claim-rewards
Jul 18, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cb36af5
claim_staking_rewards extrinsic + tests plus UnclaimedRewardInfo smal…
shannonwells c299b37
claim_staking_rewards benchmark + updated weights.rs
shannonwells bf7f004
cleanup for PR
shannonwells 8c89ce5
weights file
shannonwells 57b22f1
correct benchmark
shannonwells c5d713b
address PR comments
shannonwells File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ use frame_system::{pallet_prelude::BlockNumberFor, RawOrigin}; | |
use parity_scale_codec::alloc::vec::Vec; | ||
|
||
const SEED: u32 = 0; | ||
const REWARD_POOL_TOTAL: u32 = 2_000_000; | ||
|
||
fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) { | ||
frame_system::Pallet::<T>::assert_last_event(generic_event.into()); | ||
|
@@ -87,19 +88,36 @@ fn fill_unlock_chunks<T: Config>(caller: &T::AccountId, count: u32) { | |
UnstakeUnlocks::<T>::set(caller, Some(unlocking)); | ||
} | ||
|
||
fn fill_reward_pool_chunks<T: Config>() { | ||
let chunk_len = T::RewardPoolChunkLength::get(); | ||
let chunks = T::ProviderBoostHistoryLimit::get() / (chunk_len); | ||
for i in 0..chunks { | ||
let mut new_chunk = RewardPoolHistoryChunk::<T>::new(); | ||
for j in 0..chunk_len { | ||
let era = (i + 1) * (j + 1); | ||
assert_ok!(new_chunk.try_insert(era.into(), (1000u32 * era).into())); | ||
} | ||
ProviderBoostRewardPools::<T>::set(i, Some(new_chunk)); | ||
fn fill_reward_pool_chunks<T: Config>(current_era: RewardEra) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The previous version of this function, while it worked for the provider_boost benchmark, did not correctly fill up the reward_pool_chunks, so it caused an EraOutOfRange error in the claim_staking_reward benchmark. |
||
let history_limit: RewardEra = <T as Config>::ProviderBoostHistoryLimit::get(); | ||
let starting_era: RewardEra = current_era - history_limit - 1u32; | ||
for era in starting_era..current_era { | ||
Capacity::<T>::update_provider_boost_reward_pool(era, REWARD_POOL_TOTAL.into()); | ||
} | ||
} | ||
|
||
fn fill_boost_history<T: Config>( | ||
caller: &T::AccountId, | ||
amount: BalanceOf<T>, | ||
current_era: RewardEra, | ||
) { | ||
let max_history: RewardEra = <T as Config>::ProviderBoostHistoryLimit::get().into(); | ||
let starting_era = current_era - max_history - 1u32; | ||
for i in starting_era..current_era { | ||
assert_ok!(Capacity::<T>::upsert_boost_history(caller.into(), i, amount, true)); | ||
} | ||
} | ||
|
||
fn unclaimed_rewards_total<T: Config>(caller: &T::AccountId) -> BalanceOf<T> { | ||
let zero_balance: BalanceOf<T> = 0u32.into(); | ||
let rewards: Vec<UnclaimedRewardInfo<BalanceOf<T>, BlockNumberFor<T>>> = | ||
Capacity::<T>::list_unclaimed_rewards(caller).unwrap_or_default().to_vec(); | ||
rewards | ||
.iter() | ||
.fold(zero_balance, |acc, reward_info| acc.saturating_add(reward_info.earned_amount)) | ||
.into() | ||
} | ||
|
||
benchmarks! { | ||
stake { | ||
let caller: T::AccountId = create_funded_account::<T>("account", SEED, 105u32); | ||
|
@@ -152,7 +170,7 @@ benchmarks! { | |
|
||
let current_era: RewardEra = (history_limit + 1u32).into(); | ||
CurrentEraInfo::<T>::set(RewardEraInfo{ era_index: current_era, started_at }); | ||
fill_reward_pool_chunks::<T>(); | ||
fill_reward_pool_chunks::<T>(current_era); | ||
}: { | ||
Capacity::<T>::start_new_reward_era_if_needed(current_block); | ||
} verify { | ||
|
@@ -232,12 +250,26 @@ benchmarks! { | |
|
||
}: _ (RawOrigin::Signed(caller.clone()), target, boost_amount) | ||
verify { | ||
assert!(StakingAccountLedger::<T>::contains_key(&caller)); | ||
assert!(StakingTargetLedger::<T>::contains_key(&caller, target)); | ||
assert!(CapacityLedger::<T>::contains_key(target)); | ||
assert_last_event::<T>(Event::<T>::ProviderBoosted {account: caller, amount: boost_amount, target, capacity}.into()); | ||
} | ||
|
||
// TODO: vary the boost_history to get better weight estimates. | ||
claim_staking_rewards { | ||
let caller: T::AccountId = create_funded_account::<T>("account", SEED, 5u32); | ||
let from_msa = 33; | ||
let boost_amount: BalanceOf<T> = T::MinimumStakingAmount::get(); | ||
setup_provider_stake::<T>(&caller, &from_msa, boost_amount, false); | ||
frame_system::Pallet::<T>::set_block_number(1002u32.into()); | ||
let current_era: RewardEra = 100; | ||
set_era_and_reward_pool_at_block::<T>(current_era, 1001u32.into(), REWARD_POOL_TOTAL.into()); | ||
fill_reward_pool_chunks::<T>(current_era); | ||
fill_boost_history::<T>(&caller, 100u32.into(), current_era); | ||
let unclaimed_rewards = unclaimed_rewards_total::<T>(&caller); | ||
}: _ (RawOrigin::Signed(caller.clone())) | ||
verify { | ||
assert_last_event::<T>(Event::<T>::ProviderBoostRewardClaimed {account: caller.clone(), reward_amount: unclaimed_rewards}.into()); | ||
} | ||
|
||
impl_benchmark_test_suite!(Capacity, | ||
tests::mock::new_test_ext(), | ||
tests::mock::Test); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes sense to have the amount they staked in each Reward Info.