Skip to content

Commit

Permalink
fix: Don't double-count child staking rewards in parent finalization (#…
Browse files Browse the repository at this point in the history
…12905)

Signed-off-by: Matt Hess <matt.hess@swirldslabs.com>
  • Loading branch information
mhess-swl authored Apr 19, 2024
1 parent d0733c6 commit a98c580
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -638,12 +638,24 @@ private void handleUserTransaction(
}

throttleServiceManager.saveThrottleSnapshotsAndCongestionLevelStartsTo(stack);
transactionFinalizer.finalizeParentRecord(
payer,
tokenServiceContext,
functionality,
extraRewardReceivers(txBody, functionality, recordBuilder),
prePaidRewards);
try {
transactionFinalizer.finalizeParentRecord(
payer,
tokenServiceContext,
functionality,
extraRewardReceivers(txBody, functionality, recordBuilder),
prePaidRewards);
} catch (final Exception e) {
logger.error(
"Possibly CATASTROPHIC error: failed to finalize parent record for transaction {}",
transactionInfo.transactionID(),
e);

// Undo any changes made to the state
final var userTransactionRecordBuilder = recordListBuilder.userTransactionRecordBuilder();
userTransactionRecordBuilder.nullOutSideEffectFields();
rollback(true, ResponseCodeEnum.FAIL_INVALID, stack, recordListBuilder);
}

// Commit all state changes
stack.commitFullStack();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public Map<AccountID, Long> applyStakingRewards(
final var rewardsPaid = rewardsPayer.payRewardsIfPending(
rewardReceivers, writableStore, stakingRewardsStore, stakingInfoStore, consensusNow, recordBuilder);

// Decrease staking reward account balance by rewardPaid amount
decreaseStakeRewardAccountBalance(rewardsPaid, stakingRewardAccountId, writableStore);

if (!context.isScheduleDispatch()) {
// We only manage stake metadata once, at the end of a transaction; but to do
// this correctly, we need to include information about any rewards paid during
Expand All @@ -107,10 +110,15 @@ public Map<AccountID, Long> applyStakingRewards(
// Adjust stakes for nodes and account's staking metadata
adjustStakeMetadata(
writableStore, stakingInfoStore, stakingRewardsStore, consensusNow, rewardsPaid, rewardReceivers);

// Don't double-report prepaid rewards in the parent record
if (!prePaidRewards.isEmpty()) {
for (AccountID accountID : prePaidRewards.keySet()) {
rewardsPaid.remove(accountID);
}
}
}

// Decrease staking reward account balance by rewardPaid amount
decreaseStakeRewardAccountBalance(rewardsPaid, stakingRewardAccountId, writableStore);
return rewardsPaid;
}

Expand Down

0 comments on commit a98c580

Please sign in to comment.