-
Notifications
You must be signed in to change notification settings - Fork 23
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
Delegation PoC #504
Delegation PoC #504
Changes from all commits
24c72c6
12f8664
717fd66
319af42
8ec0bbd
76e3277
4ea1ff0
abf330e
c290bca
040b9d0
49abda9
f93b3eb
a27e9db
5d8526f
de44d59
9c9db92
070be82
07a215f
89da916
305e475
f751b30
ea1e56b
b6706d7
54f3f7b
31113ad
94d4c1f
036bfa6
7d80231
221e588
66ba792
9571e51
38ba93a
b34d2e5
6b82cbf
bdef782
1f1238b
215f981
ba108a2
e0a8f70
68c9eaa
d63a131
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,12 @@ | |
|
||
pragma solidity 0.8.25; | ||
|
||
import {IPoSValidatorManager} from "./interfaces/IPoSValidatorManager.sol"; | ||
import { | ||
IPoSValidatorManager, Delegator, DelegatorStatus | ||
} from "./interfaces/IPoSValidatorManager.sol"; | ||
import {PoSValidatorManagerSettings} from "./interfaces/IPoSValidatorManager.sol"; | ||
import {ValidatorManager} from "./ValidatorManager.sol"; | ||
import {Validator, ValidatorStatus} from "./interfaces/IValidatorManager.sol"; | ||
import {WarpMessage} from | ||
"@avalabs/[email protected]/contracts/interfaces/IWarpMessenger.sol"; | ||
import {ValidatorMessages} from "./ValidatorMessages.sol"; | ||
|
@@ -17,10 +20,23 @@ | |
// solhint-disable private-vars-leading-underscore | ||
/// @custom:storage-location erc7201:avalanche-icm.storage.PoSValidatorManager | ||
struct PoSValidatorManagerStorage { | ||
/// @notice The minimum amount of stake required to be a validator. | ||
uint256 _minimumStakeAmount; | ||
/// @notice The maximum amount of stake allowed to be a validator. | ||
uint256 _maximumStakeAmount; | ||
/// @notice The minimum amount of time a validator must be staked for. | ||
uint64 _minimumStakeDuration; | ||
/// @notice The reward calculator for this validator manager. | ||
IRewardCalculator _rewardCalculator; | ||
/// @notice Maps the validationID to a mapping of delegator address to delegator information. | ||
mapping(bytes32 validationID => mapping(address delegator => Delegator)) _delegatorStakes; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it may make more sense here to create a notion of a Theoretically, an address should be able to delegate to the same validation ID multiple times. This would also have the benefit of having a single ID to refer to a delegation rather than a composite key, and hopefully reduce the number of state lookups since there won't be nested mappings. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This idea came up previously. I agree that using a unique delegation ID rather than the address is the way to go, but going to defer that to a followup. Using the nonce rather than a timestamp (as originally suggested in the linked conversation) seems like the way to go. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i like the (validationID, nonce) hash combo |
||
/// @notice Maps the validationID to a mapping of delegator address to pending register delegator messages. | ||
mapping(bytes32 validationID => mapping(address delegator => bytes)) | ||
_pendingRegisterDelegatorMessages; | ||
/// @notice Maps the validationID to a mapping of delegator address to pending end delegator messages. | ||
mapping(bytes32 validationID => mapping(address delegator => bytes)) | ||
_pendingEndDelegatorMessages; | ||
/// @notice Maps the validationID to the uptime of the validator. | ||
mapping(bytes32 validationID => uint64) _validatorUptimes; | ||
} | ||
// solhint-enable private-vars-leading-underscore | ||
|
@@ -108,11 +124,9 @@ | |
PoSValidatorManagerStorage storage $ = _getPoSValidatorManagerStorage(); | ||
// Lock the stake in the contract. | ||
uint256 lockedValue = _lock(stakeAmount); | ||
|
||
// Ensure the stake churn doesn't exceed the maximum churn rate. | ||
uint64 weight = valueToWeight(lockedValue); | ||
// Ensure the weight is within the valid range. | ||
|
||
// Ensure the weight is within the valid range. | ||
require( | ||
weight >= $._minimumStakeAmount && weight <= $._maximumStakeAmount, | ||
"PoSValidatorManager: invalid stake amount" | ||
|
@@ -130,4 +144,196 @@ | |
|
||
function _lock(uint256 value) internal virtual returns (uint256); | ||
function _unlock(uint256 value, address to) internal virtual; | ||
|
||
function _initializeDelegatorRegistration( | ||
bytes32 validationID, | ||
address delegator, | ||
uint256 delegationAmount | ||
) internal nonReentrant { | ||
uint64 weight = valueToWeight(_lock(delegationAmount)); | ||
PoSValidatorManagerStorage storage $ = _getPoSValidatorManagerStorage(); | ||
|
||
// Ensure the validation period is active | ||
Validator memory validator = _getValidator(validationID); | ||
require( | ||
validator.status == ValidatorStatus.Active, "PoSValidatorManager: validator not active" | ||
); | ||
|
||
// Ensure the delegator is not already registered | ||
require( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will delegators be able to update their delegations and add more stake to the same validationID? Seems like a use case we should support There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's defer this to a followup. I think the safest way to this would be to come up with a unique delegation ID that takes into account the validatorID, the delegator address, and maybe the starting timestamp for uniquenes. Then add a separate delegation owner field, rather than using the delegator address for that purpose. That would also be more general, since the address that adds the delegation would not need to be the same address that claims rewards. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if this would make a big UX difference for delegators. Couldn't they just start a separate delegation with the additional stake in most cases? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I agree with Cam here. Let's use the timestamp for uniqueness for the ID, that way one address can make multiple delegations, which prevents us from ever having to change the details of a delegation. We should have a way for any given address to access all of their delegations though There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In the current case they could but would have to use a separate address given this check. I do like the idea of the delegation owner field being tracked separately from the delegator address There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good with deferring |
||
$._delegatorStakes[validationID][delegator].weight == 0, | ||
"PoSValidatorManager: delegator already registered" | ||
); | ||
|
||
_checkAndUpdateChurnTracker(weight); | ||
|
||
// Update the validator weight | ||
validator.weight += weight; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we're going to need a new field to track the original weight of the validator. It'll be needed if the validator ends validation period to calculate their rewards, because right now we'd have to use the total weight with the delegations included. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sounds right to me, but we can defer to when handling delegation exit rewards There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went ahead and added that here, so that it doesn't fall through the cracks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would advocate to only add it in when needed because future changes might not need it or want to implement differently, for example, whether |
||
_setValidator(validationID, validator); | ||
|
||
// Submit the message to the Warp precompile. | ||
uint64 nonce = _getAndIncrementNonce(validationID); | ||
bytes memory setValidatorWeightPayload = ValidatorMessages | ||
.packSetSubnetValidatorWeightMessage(validationID, nonce, validator.weight); | ||
$._pendingRegisterDelegatorMessages[validationID][delegator] = setValidatorWeightPayload; | ||
bytes32 messageID = WARP_MESSENGER.sendWarpMessage(setValidatorWeightPayload); | ||
|
||
// Store the delegator information | ||
$._delegatorStakes[validationID][delegator] = Delegator({ | ||
status: DelegatorStatus.PendingAdded, | ||
weight: weight, | ||
startedAt: 0, | ||
endedAt: 0, | ||
startingNonce: nonce, | ||
endingNonce: 0 | ||
}); | ||
|
||
emit DelegatorAdded({ | ||
validationID: validationID, | ||
setWeightMessageID: messageID, | ||
delegator: delegator, | ||
delegatorWeight: weight, | ||
validatorWeight: validator.weight, | ||
nonce: nonce | ||
}); | ||
} | ||
|
||
function resendDelegatorRegistration(bytes32 validationID, address delegator) external { | ||
_checkPendingRegisterDelegatorMessages(validationID, delegator); | ||
PoSValidatorManagerStorage storage $ = _getPoSValidatorManagerStorage(); | ||
// Submit the message to the Warp precompile. | ||
WARP_MESSENGER.sendWarpMessage($._pendingRegisterDelegatorMessages[validationID][delegator]); | ||
} | ||
|
||
|
||
function completeDelegatorRegistration(uint32 messageIndex, address delegator) external { | ||
PoSValidatorManagerStorage storage $ = _getPoSValidatorManagerStorage(); | ||
|
||
// Unpack the Warp message | ||
WarpMessage memory warpMessage = _getPChainWarpMessage(messageIndex); | ||
(bytes32 validationID, uint64 nonce,) = | ||
ValidatorMessages.unpackSubnetValidatorWeightUpdateMessage(warpMessage.payload); | ||
Comment on lines
+213
to
+214
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we were to store the latest acked nonce for a given validationID, we could have a much cheaper function to complete a delegator registration if the nonce of the registration is less than or equal to the latest acked nonce, which would save on the warp message verification. Feel free to defer this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Going to defer this to a followup, since it is an optimization. Definitely think we should support this though. |
||
_checkPendingRegisterDelegatorMessages(validationID, delegator); | ||
delete $._pendingRegisterDelegatorMessages[validationID][delegator]; | ||
|
||
Validator memory validator = _getValidator(validationID); | ||
|
||
// The received nonce should be no greater than the highest sent nonce | ||
require(validator.messageNonce >= nonce, "PoSValidatorManager: invalid nonce"); | ||
// It should also be greater than or equal to the delegator's starting nonce | ||
require( | ||
cam-schultz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$._delegatorStakes[validationID][delegator].startingNonce <= nonce, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need start and ending nonce again, as opposed to saving latest nonce for delegator? Then the check here would instead check that Warp message received is latest nonce for delegator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nvm it's because P-chain is only willing to sign latest nonce weight update message. We keep a start nonce to check for registration completion, and ending nonce for ending completion. |
||
"PoSValidatorManager: nonce does not match" | ||
); | ||
|
||
require( | ||
$._delegatorStakes[validationID][delegator].status == DelegatorStatus.PendingAdded, | ||
"PoSValidatorManager: delegator not pending added" | ||
); | ||
// Update the delegator status | ||
$._delegatorStakes[validationID][delegator].status = DelegatorStatus.Active; | ||
$._delegatorStakes[validationID][delegator].startedAt = uint64(block.timestamp); | ||
|
||
emit DelegatorRegistered({ | ||
validationID: validationID, | ||
delegator: delegator, | ||
nonce: nonce, | ||
startTime: block.timestamp | ||
}); | ||
} | ||
|
||
function initializeEndDelegation(bytes32 validationID) external { | ||
PoSValidatorManagerStorage storage $ = _getPoSValidatorManagerStorage(); | ||
|
||
// Ensure the delegator is active | ||
Delegator memory delegator = $._delegatorStakes[validationID][_msgSender()]; | ||
require( | ||
delegator.status == DelegatorStatus.Active, "PoSValidatorManager: delegator not active" | ||
); | ||
uint64 nonce = _getAndIncrementNonce(validationID); | ||
delegator.status = DelegatorStatus.PendingRemoved; | ||
delegator.endedAt = uint64(block.timestamp); | ||
delegator.endingNonce = nonce; | ||
|
||
$._delegatorStakes[validationID][_msgSender()] = delegator; | ||
|
||
Validator memory validator = _getValidator(validationID); | ||
require(validator.weight > delegator.weight, "PoSValidatorManager: Invalid weight"); | ||
validator.weight -= delegator.weight; | ||
_setValidator(validationID, validator); | ||
|
||
// Submit the message to the Warp precompile. | ||
bytes memory setValidatorWeightPayload = ValidatorMessages | ||
.packSetSubnetValidatorWeightMessage(validationID, nonce, validator.weight); | ||
$._pendingEndDelegatorMessages[validationID][_msgSender()] = setValidatorWeightPayload; | ||
bytes32 messageID = WARP_MESSENGER.sendWarpMessage(setValidatorWeightPayload); | ||
|
||
emit DelegatorRemovalInitialized({ | ||
validationID: validationID, | ||
setWeightMessageID: messageID, | ||
delegator: _msgSender(), | ||
validatorWeight: validator.weight, | ||
nonce: nonce, | ||
endTime: block.timestamp | ||
}); | ||
} | ||
|
||
function resendEndDelegation(bytes32 validationID, address delegator) external { | ||
_checkPendingEndDelegatorMessage(validationID, delegator); | ||
PoSValidatorManagerStorage storage $ = _getPoSValidatorManagerStorage(); | ||
WARP_MESSENGER.sendWarpMessage($._pendingEndDelegatorMessages[validationID][delegator]); | ||
} | ||
|
||
|
||
function completeEndDelegation(uint32 messageIndex, address delegator) external { | ||
PoSValidatorManagerStorage storage $ = _getPoSValidatorManagerStorage(); | ||
|
||
// Unpack the Warp message | ||
WarpMessage memory warpMessage = _getPChainWarpMessage(messageIndex); | ||
(bytes32 validationID, uint64 nonce,) = | ||
ValidatorMessages.unpackSubnetValidatorWeightUpdateMessage(warpMessage.payload); | ||
_checkPendingEndDelegatorMessage(validationID, delegator); | ||
delete $._pendingEndDelegatorMessages[validationID][delegator]; | ||
|
||
Validator memory validator = _getValidator(validationID); | ||
// The received nonce should be no greater than the highest sent nonce | ||
require(validator.messageNonce >= nonce, "PoSValidatorManager: invalid nonce"); | ||
// It should also be greater than or equal to the delegator's ending nonce | ||
require( | ||
cam-schultz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$._delegatorStakes[validationID][delegator].endingNonce <= nonce, | ||
"PoSValidatorManager: nonce does not match" | ||
); | ||
|
||
require( | ||
$._delegatorStakes[validationID][delegator].status == DelegatorStatus.PendingRemoved, | ||
"PoSValidatorManager: delegator not pending added" | ||
); | ||
|
||
// Update the delegator status | ||
$._delegatorStakes[validationID][delegator].status = DelegatorStatus.Completed; | ||
|
||
emit DelegationEnded(validationID, delegator, nonce); | ||
} | ||
|
||
function _checkPendingEndDelegatorMessage( | ||
bytes32 validationID, | ||
address delegator | ||
) private view { | ||
PoSValidatorManagerStorage storage $ = _getPoSValidatorManagerStorage(); | ||
require( | ||
$._pendingEndDelegatorMessages[validationID][delegator].length > 0 | ||
&& $._delegatorStakes[validationID][delegator].status == DelegatorStatus.PendingRemoved, | ||
"PoSValidatorManager: delegator removal not pending" | ||
); | ||
} | ||
|
||
function _checkPendingRegisterDelegatorMessages( | ||
bytes32 validationID, | ||
address delegator | ||
) private view { | ||
PoSValidatorManagerStorage storage $ = _getPoSValidatorManagerStorage(); | ||
require( | ||
$._pendingRegisterDelegatorMessages[validationID][delegator].length > 0 | ||
&& $._delegatorStakes[validationID][delegator].status == DelegatorStatus.PendingAdded, | ||
"PoSValidatorManager: delegator registration not pending" | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did we want to offer delegation by default? Thought it was going to be an extension
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally speaking, I prefer to make features optional rather than add complexity to the inheritance graph by adding extensions to support those features. Inheritance is best used as an abstraction tool to reduce shared code or provide polymorphism.
Regardless, for now I'm going to keep this as part of the PoSValidatorManager for the PoC, and we can revisit inheritance later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think more so thinking to allow Subnets to choose validator manager contract's that don't include delegation by default, and they can simply deploy the contract without delegation logic, as opposed to for reducing shared code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that validators should have the option to not support delegation. There are many ways to do that, such as providing that toggle as a constructor argument, or leaving it up to the individual validator to specify when they register. I'll enumerate those options and defer to a followup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good I'm good with deferring. Likely would push back as a constructor argument for turn on/off, since that increases contract bytecode and deployment costs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need to worry about deployment costs, as these contracts will be deployed by subnet owners