Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/staking-contract' into nonzero-d…
Browse files Browse the repository at this point in the history
…elegation-fee
  • Loading branch information
iansuvak committed Sep 6, 2024
2 parents 7bcdef6 + 1e2489d commit 5cb1c8c
Show file tree
Hide file tree
Showing 15 changed files with 662 additions and 600 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion contracts/staking/ERC20TokenStakingManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ contract ERC20TokenStakingManager is
function initializeDelegatorRegistration(
bytes32 validationID,
uint256 delegationAmount
) external {
) external returns (bytes32) {
return _initializeDelegatorRegistration(validationID, _msgSender(), delegationAmount);
}

Expand Down
6 changes: 5 additions & 1 deletion contracts/staking/NativeTokenStakingManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ contract NativeTokenStakingManager is
* @notice Begins the delegator registration process. Locks the provided native asset in the contract as the delegated stake.
* @param validationID The ID of the validation period being delegated to.
*/
function initializeDelegatorRegistration(bytes32 validationID) external payable {
function initializeDelegatorRegistration(bytes32 validationID)
external
payable
returns (bytes32)
{
return _initializeDelegatorRegistration(validationID, _msgSender(), msg.value);
}

Expand Down
191 changes: 106 additions & 85 deletions contracts/staking/PoSValidatorManager.sol

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions contracts/staking/ValidatorManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,8 @@ abstract contract ValidatorManager is
return $._validationPeriods[validationID];
}

function _setValidator(bytes32 validationID, Validator memory validator) internal {
function _setValidatorWeight(bytes32 validationID, uint64 weight) internal {
ValidatorManagerStorage storage $ = _getValidatorManagerStorage();
$._validationPeriods[validationID] = validator;
$._validationPeriods[validationID].weight = weight;
}
}
5 changes: 4 additions & 1 deletion contracts/staking/interfaces/IERC20TokenStakingManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ interface IERC20TokenStakingManager is IPoSValidatorManager {
bytes memory blsPublicKey
) external returns (bytes32 validationID);

function initializeDelegatorRegistration(bytes32 validationID, uint256 stakeAmount) external;
function initializeDelegatorRegistration(
bytes32 validationID,
uint256 stakeAmount
) external returns (bytes32);
}
5 changes: 4 additions & 1 deletion contracts/staking/interfaces/INativeTokenStakingManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ interface INativeTokenStakingManager is IPoSValidatorManager {
bytes memory blsPublicKey
) external payable returns (bytes32 validationID);

function initializeDelegatorRegistration(bytes32 validationID) external payable;
function initializeDelegatorRegistration(bytes32 validationID)
external
payable
returns (bytes32);
}
81 changes: 44 additions & 37 deletions contracts/staking/interfaces/IPoSValidatorManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ struct PoSValidatorManagerSettings {

struct Delegator {
DelegatorStatus status;
address owner;
bytes32 validationID;
uint64 weight;
uint64 startedAt;
uint64 endedAt;
Expand All @@ -44,62 +46,61 @@ interface IPoSValidatorManager is IValidatorManager {

/**
* @notice Event emitted when a delegator registration is initiated
* @param delegationID The ID of the delegation
* @param validationID The ID of the validation period
* @param setWeightMessageID The ID of the Warp message that updates the validator's weight on the P-Chain
* @param delegator The address of the delegator
* @param delegatorWeight The weight of the delegator
* @param validatorWeight The updated validator weight that is sent to the P-Chain
* @param delegatorAddress The address of the delegator
* @param nonce The message nonce used to update the validator weight
* @param validatorWeight The updated validator weight that is sent to the P-Chain
* @param delegatorWeight The weight of the delegator
* @param setWeightMessageID The ID of the Warp message that updates the validator's weight on the P-Chain
*/
event DelegatorAdded(
bytes32 indexed delegationID,
bytes32 indexed validationID,
bytes32 indexed setWeightMessageID,
address indexed delegator,
uint64 delegatorWeight,
address indexed delegatorAddress,
uint64 nonce,
uint64 validatorWeight,
uint64 nonce
uint64 delegatorWeight,
bytes32 setWeightMessageID
);

/**
* @notice Event emitted when a delegator registration is completed
* @param validationID The ID of the validation period
* @param delegator The address of the delegator
* @param delegationID The ID of the delegation
* @param nonce The message nonce used to update the validator weight, as returned by the P-Chain
* @param startTime The time at which the registration was completed
*/
event DelegatorRegistered(
bytes32 indexed delegationID,
bytes32 indexed validationID,
address indexed delegator,
uint64 indexed nonce,
uint256 startTime
);

/**
* @notice Event emitted when delegator removal is initiated
* @param validationID The ID of the validation period
* @param setWeightMessageID The ID of the Warp message that updates the validator's weight on the P-Chain
* @param delegator The address of the delegator
* @param validatorWeight The updated validator weight that is sent to the P-Chain
* @param delegationID The ID of the delegation
* @param nonce The message nonce used to update the validator weight
* @param validatorWeight The updated validator weight that is sent to the P-Chain
* @param endTime The time at which the removal was initiated
* @param setWeightMessageID The ID of the Warp message that updates the validator's weight on the P-Chain
*/
event DelegatorRemovalInitialized(
bytes32 indexed delegationID,
bytes32 indexed validationID,
bytes32 indexed setWeightMessageID,
address indexed delegator,
uint64 indexed nonce,
uint64 validatorWeight,
uint64 nonce,
uint256 endTime
uint256 endTime,
bytes32 setWeightMessageID
);

/**
* @notice Event emitted when delegator removal is completed
* @param validationID The ID of the validation period
* @param delegator The address of the delegator
* @param delegationID The ID of the delegation
* @param nonce The message nonce used to update the validator weight, as returned by the P-Chain
*/
event DelegationEnded(
bytes32 indexed validationID, address indexed delegator, uint64 indexed nonce
bytes32 indexed delegationID, bytes32 indexed validationID, uint64 indexed nonce
);

/**
Expand All @@ -121,51 +122,57 @@ interface IPoSValidatorManager is IValidatorManager {
/**
* @notice Resubmits a delegator registration message to be sent to the P-Chain.
* Only necessary if the original message can't be delivered due to validator churn.
* @param validationID The ID of the validation period being registered.
* @param delegator The address of the delegator being registered.
* @param delegationID The ID of the delegation being registered.
*/
function resendDelegatorRegistration(bytes32 validationID, address delegator) external;
function resendDelegatorRegistration(bytes32 delegationID) external;

/**
* @notice Completes the delegator registration process by returning an acknowledgement of the registration of a
* validationID from the P-Chain. After this function is called, the validator's weight is updated in the contract state.
* Any P-Chain acknowledgement with a nonce greater than or equal to the nonce used to initialize registration of the
* delegator is valid, as long as that nonce has been sent by the contract.
* delegator is valid, as long as that nonce has been sent by the contract. For the purposes of computing delegation rewards,
* the delegation is considered active after this function is called.
* Note: only the specified delegation will be marked as registered, even if the validator weight update
* message implicitly includes multiple weight changes.
* @param messageIndex The index of the Warp message to be received providing the acknowledgement.
* @param delegator The address of the delegator being registered.
* @param delegationID The ID of the delegation being registered.
*/
function completeDelegatorRegistration(uint32 messageIndex, address delegator) external;
function completeDelegatorRegistration(uint32 messageIndex, bytes32 delegationID) external;

/**
* @notice Begins the process of removing a delegator from a validation period. The delegator must have been previously
* registered with the given validationID.
* @param validationID The ID of the validation period being removed.
* registered with the given validationID. For the purposes of computing delegation rewards, the delegation period is
* considered ended when this function is called. In order to be eligible for rewards, an uptime proof must be provided.
* Note that this function can only be called by the address that registered the delegation.
* @param delegationID The ID of the delegation being removed.
* @param includeUptimeProof Whether or not an uptime proof is provided for the validation period.
* If no uptime proof is provided, the validation uptime for the delegation period will be assumed to be 0.
* @param messageIndex If {includeUptimeProof} is true, the index of the Warp message to be received providing the
* uptime proof.
*/
function initializeEndDelegation(
bytes32 validationID,
bytes32 delegationID,
bool includeUptimeProof,
uint32 messageIndex
) external;

/**
* @notice Resubmits a delegator end message to be sent to the P-Chain.
* Only necessary if the original message can't be delivered due to validator churn.
* @param validationID The ID of the validation period being ended.
* @param delegator The address of the delegator being removed.
* @param delegationID The ID of the delegation being removed.
*/
function resendEndDelegation(bytes32 validationID, address delegator) external;
function resendEndDelegation(bytes32 delegationID) external;

/**
* @notice Completes the process of ending a delegation by receiving an acknowledgement from the P-Chain.
* After this function is called, the validator's weight is updated in the contract state.
* Any P-Chain acknowledgement with a nonce greater than or equal to the nonce used to initialize the end of the
* delegator's delegation is valid, as long as that nonce has been sent by the contract.
* delegator's delegation is valid, as long as that nonce has been sent by the contract. This is because the validator
* weight change pertaining to the delegation ending is included in any subsequent validator weight update messages.
* Note: only the specified delegation will be marked as completed, even if the validator weight update
* message implicitly includes multiple weight changes.
* @param messageIndex The index of the Warp message to be received providing the acknowledgement.
* @param delegator The address of the delegator being removed.
* @param delegationID The ID of the delegation being removed.
*/
function completeEndDelegation(uint32 messageIndex, address delegator) external;
function completeEndDelegation(uint32 messageIndex, bytes32 delegationID) external;
}
9 changes: 5 additions & 4 deletions contracts/staking/tests/ERC20TokenStakingManagerTests.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {

function _initializeDelegatorRegistration(
bytes32 validationID,
address delegator,
address delegatorAddress,
uint64 weight
) internal virtual override {
) internal virtual override returns (bytes32) {
uint256 value = app.weightToValue(weight);
vm.startPrank(delegator);
app.initializeDelegatorRegistration(validationID, value);
vm.startPrank(delegatorAddress);
bytes32 delegationID = app.initializeDelegatorRegistration(validationID, value);
vm.stopPrank();
return delegationID;
}

function _beforeSend(uint64 weight, address spender) internal override {
Expand Down
10 changes: 5 additions & 5 deletions contracts/staking/tests/NativeTokenStakingManagerTests.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ contract NativeTokenStakingManagerTest is PoSValidatorManagerTest {

function _initializeDelegatorRegistration(
bytes32 validationID,
address delegator,
address delegatorAddress,
uint64 weight
) internal virtual override {
) internal virtual override returns (bytes32) {
uint256 value = app.weightToValue(weight);
vm.prank(delegator);
vm.deal(delegator, value);
app.initializeDelegatorRegistration{value: value}(validationID);
vm.prank(delegatorAddress);
vm.deal(delegatorAddress, value);
return app.initializeDelegatorRegistration{value: value}(validationID);
}

function _beforeSend(uint64 weight, address spender) internal override {
Expand Down
Loading

0 comments on commit 5cb1c8c

Please sign in to comment.