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

Centralize reward management in PoSValidatorManager #643

Merged
merged 1 commit into from
Nov 15, 2024

Conversation

richardpringle
Copy link
Contributor

@richardpringle richardpringle commented Nov 13, 2024

Why this should be merged

If any of the reward logic changes, the change happens in one place. For the delegator, the logic only existed in one place before, but this keeps the pattern consistent and improves the readability of _completeEndDelegation function.

How this works

Same as before, I just moved code

How this was tested

Existing tests

How is this documented

inline docs on new functions

@richardpringle richardpringle force-pushed the consolidate-reward-updates branch from 005ec21 to 7cb0f4a Compare November 13, 2024 17:02
@richardpringle richardpringle marked this pull request as ready for review November 13, 2024 17:02
@richardpringle richardpringle requested a review from a team as a code owner November 13, 2024 17:02
@richardpringle richardpringle force-pushed the consolidate-reward-updates branch from 7cb0f4a to 7e3c8ff Compare November 13, 2024 17:32
@@ -760,4 +744,41 @@ abstract contract PoSValidatorManager is
PoSValidatorManagerStorage storage $ = _getPoSValidatorManagerStorage();
return $._posValidatorInfo[validationID].owner != address(0);
}

function _withdrawValidationRewards(address rewardRecipient, bytes32 validationID) internal {
PoSValidatorManagerStorage storage $ = _getPoSValidatorManagerStorage();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make more sense to pass this in?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We currently retrieve the contract storage in every function that needs it, mainly under the assumption that the Solidity compiler is smart enough to cache subsequent reads of the same value. However, I'm not seeing anything in the optimizer steps that would directly translate. We should do a gas cost analysis of this pattern versus passing the storage object around (not necessary for this PR - let's stick to the existing pattern here)

Copy link
Contributor

@cam-schultz cam-schultz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small nit, otherwise LGTM

@@ -760,4 +744,41 @@ abstract contract PoSValidatorManager is
PoSValidatorManagerStorage storage $ = _getPoSValidatorManagerStorage();
return $._posValidatorInfo[validationID].owner != address(0);
}

function _withdrawValidationRewards(address rewardRecipient, bytes32 validationID) internal {
PoSValidatorManagerStorage storage $ = _getPoSValidatorManagerStorage();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We currently retrieve the contract storage in every function that needs it, mainly under the assumption that the Solidity compiler is smart enough to cache subsequent reads of the same value. However, I'm not seeing anything in the optimizer steps that would directly translate. We should do a gas cost analysis of this pattern versus passing the storage object around (not necessary for this PR - let's stick to the existing pattern here)

Comment on lines 764 to 765
uint256 delegationRewards = 0;
uint256 validatorFees = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OpenZeppelin called out in their recent audit that not initializing variables with their default value is more gas efficient.

Suggested change
uint256 delegationRewards = 0;
uint256 validatorFees = 0;
uint256 delegationRewards;
uint256 validatorFees;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's insane that this doesn't compile down to the same bytecode... will fix!

Copy link
Contributor

@bernard-avalabs bernard-avalabs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM other than the small issue that Cam brought up.

@richardpringle richardpringle force-pushed the consolidate-reward-updates branch 2 times, most recently from 45a5f32 to 498650f Compare November 15, 2024 17:05
contracts/validator-manager/PoSValidatorManager.sol Dismissed Show dismissed Hide dismissed
contracts/validator-manager/PoSValidatorManager.sol Dismissed Show dismissed Hide dismissed
@richardpringle richardpringle force-pushed the consolidate-reward-updates branch from 498650f to dee4e3d Compare November 15, 2024 17:13
Copy link
Contributor

@bernard-avalabs bernard-avalabs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Interesting that we are now getting Slither warnings.

@richardpringle richardpringle merged commit 877bb36 into main Nov 15, 2024
17 checks passed
@richardpringle richardpringle deleted the consolidate-reward-updates branch November 15, 2024 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants