Skip to content
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

make distribute_rewards transactional #299

Merged
merged 2 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
14 changes: 13 additions & 1 deletion pallets/pooled-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,20 @@ impl frame_system::Config for Runtime {
type MaxConsumers = ConstU32<16>;
}

/// Allows to change ED mid-test.
pub struct MockExistentialDeposit;
impl MockExistentialDeposit {
pub fn get() -> Balance {
frame_support::storage::unhashed::get(b":mock_ed").unwrap_or(1)
}

pub fn set(amount: Balance) {
frame_support::storage::unhashed::put(b":mock_ed", &amount);
}
}

parameter_types! {
pub const ExistentialDeposit: u128 = 1;
pub ExistentialDeposit: u128 = MockExistentialDeposit::get();
}

impl pallet_balances::Config for Runtime {
Expand Down
1 change: 1 addition & 0 deletions pallets/pooled-staking/src/pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ impl<T: Config> ManualRewards<T> {
/// absorbed in the ManualRewards distribution, which simply consist of
/// transfering the funds to the candidate account.
#[allow(dead_code)]
#[frame_support::transactional]
pub fn distribute_rewards<T: Config>(
candidate: &Candidate<T>,
rewards: CreditOf<T>,
Expand Down
47 changes: 47 additions & 0 deletions pallets/pooled-staking/src/tests/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use {
Pallet, TargetPool,
},
tp_traits::DistributeRewards,
frame_support::assert_err,
sp_runtime::DispatchError,
};

struct Delegation {
Expand Down Expand Up @@ -582,3 +584,48 @@ fn delegator_only_candidate_no_stake_auto_compounding() {
)
});
}

#[test]
fn reward_distribution_is_transactional() {
ExtBuilder::default().build().execute_with(|| {
use crate::traits::Timer;
let request_time = <Runtime as crate::Config>::JoiningRequestTimer::now();

assert_ok!(Staking::request_delegate(
RuntimeOrigin::signed(ACCOUNT_CANDIDATE_1.into()),
ACCOUNT_CANDIDATE_1.into(),
TargetPool::AutoCompounding,
1_000_000_000,
));

// Wait for delegation to be executable
for _ in 0..BLOCKS_TO_WAIT {
roll_one_block();
}

assert_ok!(Staking::execute_pending_operations(
RuntimeOrigin::signed(ACCOUNT_CANDIDATE_1.into()),
vec![PendingOperationQuery {
delegator: ACCOUNT_CANDIDATE_1.into(),
operation: PendingOperationKey::JoiningAutoCompounding {
candidate: ACCOUNT_CANDIDATE_1.into(),
at: request_time
},
}]
));

let total_staked_before = pools::AutoCompounding::<Runtime>::total_staked(&ACCOUNT_CANDIDATE_1.into());

// Increase ED to make reward destribution fail when resolving
// credit to Staking account.
MockExistentialDeposit::set(u128::MAX);

let rewards = Balances::issue(1_000_000_000);
assert_err!(Staking::distribute_rewards(ACCOUNT_CANDIDATE_1.into(), rewards), DispatchError::NoProviders);

let total_staked_after = pools::AutoCompounding::<Runtime>::total_staked(&ACCOUNT_CANDIDATE_1.into());
assert_eq!(total_staked_before, total_staked_after, "distribution should be reverted");


})
}
Loading