Submitted by kenzo, also found by itsmeSTYJ
debtWriteOff updates totalFrozen before withdrawing unionToken rewards.
As the borrower is overdue, this means the staker calling debtWriteOff will lose his rewards if for example totalStaked == totalFrozen.
(Note: If the borrower would to first call withdrawRewards/stake/unstake before calling debtWriteOff, he would get the rewards.)
Staker loses rewards.
(Or at the very least, inconsistency at rewards calculation between debtWriteOff and stake/unstake/withdrawRewards.)
debtWriteOff first calls updateTotalFrozen, and then comptroller.withdrawRewards: L710: L712
updateTotalFrozen can update totalFrozen to be same as totalStaked.
comptroller.withdrawRewards calls calculateRewardsByBlocks: Comptroller.sol L98
calculateRewardsByBlocks is calculating userManagerContract.totalStaked() - userManagerData.totalFrozen, which can be 0 in certain cases,
Comptroller.sol L140
and passing it as the third parameter to calculateRewards: Comptroller.sol L167
In calculateRewards, if the third parameter is 0, the user wont get any rewards.
Comptroller.sol L253
So in this scenario the user wont get any rewards after calling debtWriteOff.
If we were to call updateTotalFrozen after the withdrawing of the rewards, or if the staker would call withdrawRewards before calling debtWriteOff, the totalFrozen would not have been updated, and the user would get his rewards by calling debtWriteOff.
As I mentioned earlier, if theres a reason Im not seeing as to why updateTotalFrozen is updated in debtWriteOff before withdrawRewards is called, then it is not consistent with stake/unstake/withdrawRewards functions.
I have a created an (admittedly hacky) script to show the bug.
It will run two scenarios which are almost the same (based on integration/testUserManager.js).
In the first the staker will call debtWriteOff at some point,
and at the second the staker will call withdrawRewards at the same point,
and the test will print the difference in unionToken balance after each call.
File password: union.
https://pastebin.com/xkS0PXtq
Manual analysis, hardhat.
If I am not missing anything, in debtWriteOff I would move the withdrawRewards to before updateTotalFrozen.
UserManager.sol L710:L712
kingjacob (Union) acknowledged
GalloDaSballo (judge) commented:
