Fix average reward calculation after IP removal in EvenSplitGroupPool #470
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.
Description
This PR addresses an issue in the EvenSplitGroupPool contract's group reward share calculation. The changes prevent situations where users are unable to add a new IP to a group due to an incorrect average reward share calculation by implementing an
averageRewardSharestale detection and update mechanism.When removing a IP, the
removeIpfunction checks if the removed IP'sexpectedGroupRewardShareis equal to the group'saverageRewardShare. TheaverageRewardSharemay be outdated in this case, since it should be equal to the largestexpectedGroupRewardShareof the group's remaining IPs. Therefore, it is marked as stale.Then, when adding an IP, if the group
averageRewardSharehas been previously marked as stale and if thetotalGroupRewardSharecalculated using the potentially outdatedaverageRewardShareexceeds 100%, the_updateGroupAverageRewardSharefunction is called. This function iterates over all the IPs of the group to find the up to dateaverageRewardShare.Since high gas costs may occur when calling
_updateGroupAverageRewardSharein a group containing a high number of IPs, those checks ensure that this function is called if and only ifaverageRewardSharewould make it impossible to add this new IPRelated Issue
#469
Testing
Added
test_EvenSplitGroupPool_addIp_after_removeIp_updateGroupRewardShareto the EvenSplitGroupPool tests. This test ensures that when removing the IP with the largest expectedGroupRewardShare from a group and then adding new IPs to the group until not updating theaverageRewardSharewould make thetotalGroupRewardShareexceed 100%, theaverageRewardShareis updated and thetotalGroupRewardShareis now below 100%.