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

[MOON-2552] fix: only add PoV weight of Self::mint_and_compound() the first time it is called #2459

Closed
wants to merge 5 commits into from
Closed
Changes from 4 commits
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
31 changes: 25 additions & 6 deletions pallets/parachain-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,7 @@ pub mod pallet {
delegator.clone(),
candidate.clone(),
more.clone(),
false,
)?;
Pallet::<T>::deposit_event(Event::DelegationIncreased {
delegator,
Expand Down Expand Up @@ -1819,11 +1820,14 @@ pub mod pallet {
));

// pay delegators due portion
for BondWithAutoCompound {
owner,
amount,
auto_compound,
} in state.delegations
for (
i,
BondWithAutoCompound {
owner,
amount,
auto_compound,
},
) in state.delegations.into_iter().enumerate()
{
let percent = Perbill::from_rational(amount, state.total);
let due = percent * amt_due;
Expand All @@ -1833,6 +1837,7 @@ pub mod pallet {
auto_compound.clone(),
collator.clone(),
owner.clone(),
!i.is_zero(),
));
}
}
Expand Down Expand Up @@ -2035,6 +2040,7 @@ pub mod pallet {
delegator: T::AccountId,
candidate: T::AccountId,
more: BalanceOf<T>,
already_called_for_candidate: bool,
) -> Result<
(bool, Weight),
DispatchErrorWithPostInfo<frame_support::dispatch::PostDispatchInfo>,
Expand All @@ -2045,9 +2051,17 @@ pub mod pallet {
Error::<T>::PendingDelegationRevoke
);

let actual_weight = T::WeightInfo::delegator_bond_more(
let mut actual_weight = T::WeightInfo::delegator_bond_more(
<DelegationScheduledRequests<T>>::get(&candidate).len() as u32,
grw-ms marked this conversation as resolved.
Show resolved Hide resolved
);

// If we have called delegator_bond_more for this candidate before, the weight
// returned will be an over-estimate since the read was already performed on the first
// call and subsequent calls do not increase PoV size further.
if already_called_for_candidate {
actual_weight = actual_weight.set_proof_size(0);
}

let in_top = state
.increase_delegation::<T>(candidate.clone(), more)
.map_err(|err| DispatchErrorWithPostInfo {
Expand Down Expand Up @@ -2087,11 +2101,15 @@ pub mod pallet {
/// delegator and tries to compound a specified percent of it back towards the delegation.
/// If a scheduled delegation revoke exists, then the amount is only minted, and nothing is
/// compounded. Emits the [Compounded] event.
/// already_called_for_candidate: bool is used to indicate if this function has already been
/// called for a given delegator by the callee. This is used to avoid over-estimating
/// weight, since subsequent calls to this function will not increase the pov size.
pub fn mint_and_compound(
amt: BalanceOf<T>,
compound_percent: Percent,
candidate: T::AccountId,
delegator: T::AccountId,
already_called_for_candidate: bool,
) -> Weight {
let mut weight = T::WeightInfo::mint_collator_reward();
if let Ok(amount_transferred) =
Expand All @@ -2111,6 +2129,7 @@ pub mod pallet {
delegator.clone(),
candidate.clone(),
compound_amount.clone(),
already_called_for_candidate,
) {
Err(err) => {
log::debug!(
Expand Down
Loading