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

Delegation PoC #504

Merged
merged 41 commits into from
Sep 5, 2024
Merged

Delegation PoC #504

merged 41 commits into from
Sep 5, 2024

Conversation

cam-schultz
Copy link
Contributor

Why this should be merged

Partially fixes #441

How this works

  • Adds initialize/resend/complete methods for adding and removing delegators
  • Adds pack/unpack methods for SubnetValidatorWeightUpdate Warp messages
  • Unit tests for the above
  • Various cleanup items in the existing implementations (especially regarding nonce + weight management)

How this was tested

CI, new unit tests

How is this documented

N/A

* @param validationID The ID of the validation period being delegated to.
* @param delegationAmount The amount to be delegated.
*/
function initializeDelegatorRegistration(

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

Copy link
Contributor Author

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.

Copy link

@minghinmatthewlam minghinmatthewlam Sep 4, 2024

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.

Copy link
Contributor Author

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.

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.

Copy link
Contributor

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

contracts/staking/PoSValidatorManager.sol Outdated Show resolved Hide resolved
contracts/staking/PoSValidatorManager.sol Outdated Show resolved Hide resolved
);

// Ensure the delegator is not already registered
require(

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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?

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

Choose a reason for hiding this comment

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

good with deferring

contracts/staking/PoSValidatorManager.sol Outdated Show resolved Hide resolved
contracts/staking/PoSValidatorManager.sol Show resolved Hide resolved
contracts/staking/PoSValidatorManager.sol Outdated Show resolved Hide resolved
contracts/staking/PoSValidatorManager.sol Outdated Show resolved Hide resolved
contracts/staking/PoSValidatorManager.sol Show resolved Hide resolved
contracts/staking/PoSValidatorManager.sol Outdated Show resolved Hide resolved
Copy link
Contributor

@geoff-vball geoff-vball left a comment

Choose a reason for hiding this comment

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

So far I've just gone over the implementations. I'll look over the tests next week :)

contracts/staking/ERC20TokenStakingManager.sol Outdated Show resolved Hide resolved
contracts/staking/PoSValidatorManager.sol Outdated Show resolved Hide resolved
);

// Ensure the delegator is not already registered
require(
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Comment on lines +213 to +214
(bytes32 validationID, uint64 nonce,) =
ValidatorMessages.unpackSubnetValidatorWeightUpdateMessage(warpMessage.payload);
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Comment on lines 284 to 289
// Update the validator weight and delegator status
Validator memory validator = _getValidator(validationID);
require(validator.messageNonce >= nonce, "PoSValidatorManager: invalid nonce");
$._delegatorStakes[validationID][delegator].status = DelegatorStatus.Completed;

emit DelegationEnded(validationID, delegator, nonce);
Copy link
Contributor

Choose a reason for hiding this comment

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

We should be checking uptime, giving out rewards, and returning stake in here I think?

For uptime, I think anyone should be able to provide an uptime proof for a validator. Since we track the seconds of uptime, a proof can be provided basically at any point, and if the highest reported uptimeSeconds is at least 80% of the time from the validation period starting and the MIN(validation end, current time), then we can disburse rewards.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm going to defer uptime/rewards handling since I think there are still some open design questions here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I implemented uptime handling here: #520

Happy to merge that into this branch and include it in this PR if you prefer.

contracts/staking/ValidatorManager.sol Outdated Show resolved Hide resolved
* @param validationID The ID of the validation period being ended.
* @param delegator The address of the delegator being removed.
*/
function resendEndDelegation(bytes32 validationID, address delegator) external;
Copy link
Contributor

Choose a reason for hiding this comment

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

Technically our resends for starting or ending a delegation would only need to resend the current validator total weight with the highest nonce we've used so far. We don't need to store any of these messages, we just need to send our current state.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm going to leave this PR storing the pending messages in the contract state, both for consistency, and because there's a potential gas tradeoff to reconstructing the Warp message when calling the resend methods. I'll create an issue to quantify that and proceed accordingly.

Copy link
Contributor

Choose a reason for hiding this comment

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

I imagine the storage will cost more than the computation, and we should probably be optimizing for the happy path instead of the resend case. Happy to defer this to another ticket though

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, don't want to make any assumptions about gas cost, so will defer that analysis and any implementation changes to a followup item.

IRewardCalculator _rewardCalculator;
/// @notice Maps the validationID to a mapping of delegator address to delegator information.
mapping(bytes32 validationID => mapping(address delegator => Delegator)) _delegatorStakes;
Copy link
Collaborator

@michaelkaplan13 michaelkaplan13 Sep 3, 2024

Choose a reason for hiding this comment

The 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 delegationID (similar to validationID) to key these maps on.

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 delegationID could be a hash of the (validationID,nonce), where the nonce is the nonce used in the SetSubnetValidatorWeight message that added the delegator.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Choose a reason for hiding this comment

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

i like the (validationID, nonce) hash combo

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.

Generally makes sense to me. I assume there are no side effects from moving the weight update from completeEndDelegation to initializeEndDelegation.

_checkAndUpdateChurnTracker(weight);

// Update the validator weight
validator.weight += weight;
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Choose a reason for hiding this comment

The 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 startTime is only needed for PoS, or using a separate mechanism as a whole. Not a big deal though now that's already added.

contracts/staking/NativeTokenStakingManager.sol Outdated Show resolved Hide resolved
require(validator.messageNonce >= nonce, "PoSValidatorManager: invalid nonce");
// It should also be greater than or equal to the delegator's starting nonce
require(
$._delegatorStakes[validationID][delegator].startingNonce <= nonce,

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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.

Copy link

@minghinmatthewlam minghinmatthewlam left a comment

Choose a reason for hiding this comment

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

LGTM

@cam-schultz cam-schultz merged commit 24b9c61 into staking-contract Sep 5, 2024
12 checks passed
@cam-schultz cam-schultz deleted the delegation-poc branch September 5, 2024 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

7 participants