Skip to content

Commit

Permalink
Move weight update to validatormanager
Browse files Browse the repository at this point in the history
  • Loading branch information
geoff-vball committed Sep 18, 2024
1 parent 15d9fbd commit 9abff90
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 46 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

34 changes: 5 additions & 29 deletions contracts/staking/PoSValidatorManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -238,19 +238,10 @@ abstract contract PoSValidatorManager is IPoSValidatorManager, ValidatorManager
newValidatorWeight <= validator.startingWeight * $._maximumStakeMultiplier,
"PoSValidatorManager: maximum validator weight reached"
);
_setValidatorWeight(validationID, newValidatorWeight);

// Construct the delegation ID. This is guaranteed to be unique since it is
// constructed using a new nonce.
uint64 nonce = _incrementAndGetNonce(validationID);
bytes32 delegationID = keccak256(abi.encodePacked(validationID, nonce));
(uint64 nonce, bytes32 messageID) = _setValidatorWeight(validationID, newValidatorWeight);

// Submit the message to the Warp precompile.
bytes32 messageID = WARP_MESSENGER.sendWarpMessage(
ValidatorMessages.packSetSubnetValidatorWeightMessage(
validationID, nonce, newValidatorWeight
)
);
bytes32 delegationID = keccak256(abi.encodePacked(validationID, nonce));

// Store the delegation information. Set the delegator status to pending added,
// so that it can be properly started in the complete step, even if the delivered
Expand Down Expand Up @@ -358,25 +349,10 @@ abstract contract PoSValidatorManager is IPoSValidatorManager, ValidatorManager
// a delegation whose validator has ended validating has no impact on the stake weight of the chain.
_checkAndUpdateChurnTrackerRemoval(delegator.weight);

delegator.endingNonce = _incrementAndGetNonce(validationID);
delegator.endedAt = uint64(block.timestamp);

uint64 newValidatorWeight = validator.weight - delegator.weight;
_setValidatorWeight(validationID, newValidatorWeight);

// Submit the message to the Warp precompile.
bytes32 messageID = WARP_MESSENGER.sendWarpMessage(
ValidatorMessages.packSetSubnetValidatorWeightMessage(
validationID, delegator.endingNonce, newValidatorWeight
)
);

emit ValidatorWeightUpdate({
validationID: validationID,
nonce: delegator.endingNonce,
validatorWeight: newValidatorWeight,
setWeightMessageID: messageID
});
(delegator.endingNonce,) = _setValidatorWeight(validationID, newValidatorWeight);

delegator.endedAt = uint64(block.timestamp);
} else {
delegator.endingNonce = validator.messageNonce;
delegator.endedAt = validator.endedAt;
Expand Down
22 changes: 21 additions & 1 deletion contracts/staking/ValidatorManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ValidatorStatus,
Validator,
ValidatorChurnPeriod,
ValidatorWeightUpdate,
ValidatorRegistrationInput
} from "./interfaces/IValidatorManager.sol";
import {
Expand Down Expand Up @@ -375,9 +376,28 @@ abstract contract ValidatorManager is
return warpMessage;
}

function _setValidatorWeight(bytes32 validationID, uint64 weight) internal {
function _setValidatorWeight(
bytes32 validationID,
uint64 weight
) internal returns (uint64, bytes32) {
uint64 nonce = _incrementAndGetNonce(validationID);

ValidatorManagerStorage storage $ = _getValidatorManagerStorage();
$._validationPeriods[validationID].weight = weight;

// Submit the message to the Warp precompile.
bytes32 messageID = WARP_MESSENGER.sendWarpMessage(
ValidatorMessages.packSetSubnetValidatorWeightMessage(validationID, nonce, weight)
);

emit ValidatorWeightUpdate({
validationID: validationID,
nonce: nonce,
validatorWeight: weight,
setWeightMessageID: messageID
});

return (nonce, messageID);
}

/**
Expand Down
14 changes: 0 additions & 14 deletions contracts/staking/interfaces/IPoSValidatorManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,6 @@ interface IPoSValidatorManager is IValidatorManager {
bytes32 indexed delegationID, bytes32 indexed validationID, uint256 endTime
);

/**
* @notice Event emitted when delegator removal is initiated
* @param validationID The ID of the validation period
* @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 setWeightMessageID The ID of the Warp message that updates the validator's weight on the P-Chain
*/
event ValidatorWeightUpdate(
bytes32 indexed validationID,
uint64 indexed nonce,
uint64 validatorWeight,
bytes32 setWeightMessageID
);

/**
* @notice Event emitted when delegator removal is completed
* @param delegationID The ID of the delegation
Expand Down
14 changes: 14 additions & 0 deletions contracts/staking/interfaces/IValidatorManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ struct ValidatorChurnPeriod {
uint64 churnAmount;
}

/**
* @notice Event emitted when delegator removal is initiated
* @param validationID The ID of the validation period
* @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 setWeightMessageID The ID of the Warp message that updates the validator's weight on the P-Chain
*/
event ValidatorWeightUpdate(
bytes32 indexed validationID,
uint64 indexed nonce,
uint64 validatorWeight,
bytes32 setWeightMessageID
);

struct ValidatorManagerSettings {
bytes32 pChainBlockchainID;
bytes32 subnetID;
Expand Down

0 comments on commit 9abff90

Please sign in to comment.