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

Staking manager unit tests #476

Merged
merged 31 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6ecadb7
export StakingManagerSettings
cam-schultz Jul 30, 2024
36a8e0f
unit test skeleton
cam-schultz Jul 30, 2024
4eff319
format
cam-schultz Jul 30, 2024
8154fc5
call initialize from ctr
cam-schultz Jul 31, 2024
dbc031a
init vdr registration test
cam-schultz Jul 31, 2024
0a29c43
resend register vdr test
cam-schultz Jul 31, 2024
a6f2949
format
cam-schultz Jul 31, 2024
d84f8ee
wip
cam-schultz Jul 31, 2024
80080fb
Merge branch 'staking-contract' into staking-manager-unit-tests
cam-schultz Aug 1, 2024
304bec6
move files
cam-schultz Aug 1, 2024
1c1985f
full pack/unpack parity
cam-schultz Aug 1, 2024
4d4ed7d
staking messages tests
cam-schultz Aug 1, 2024
23089c0
restrict to view
cam-schultz Aug 1, 2024
a80ac9c
complete vdr registration test
cam-schultz Aug 1, 2024
f8fc4d5
format
cam-schultz Aug 1, 2024
1c0a769
Merge branch 'staking-contract' into staking-manager-unit-tests
cam-schultz Aug 1, 2024
cf1e444
init end vdr test
cam-schultz Aug 1, 2024
6ea330f
resent init vdr end test
cam-schultz Aug 1, 2024
e728e2f
emit validation period ended
cam-schultz Aug 1, 2024
5343d39
complete validation test
cam-schultz Aug 1, 2024
5df9ae3
format
cam-schultz Aug 1, 2024
3480ed9
add expected warp msgs
cam-schultz Aug 1, 2024
0087c07
lint
cam-schultz Aug 1, 2024
4a15a81
build fix
cam-schultz Aug 1, 2024
050bbb0
default constants
cam-schultz Aug 2, 2024
5b59a1f
warp precompile mock helpers
cam-schultz Aug 2, 2024
85fb6ab
format
cam-schultz Aug 2, 2024
de7363a
Merge branch 'staking-contract' into staking-manager-unit-tests
cam-schultz Aug 5, 2024
361ce60
icm initializable
cam-schultz Aug 5, 2024
9735935
format
cam-schultz Aug 5, 2024
7e0e67f
Merge branch 'staking-contract' into staking-manager-unit-tests
cam-schultz Aug 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion contracts/staking/NativeTokenStakingManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ pragma solidity 0.8.25;
import {INativeTokenStakingManager} from "./interfaces/INativeTokenStakingManager.sol";
import {Address} from "@openzeppelin/[email protected]/utils/Address.sol";
import {StakingManager} from "./StakingManager.sol";
import {StakingManagerSettings} from "./interfaces/IStakingManager.sol";

contract NativeTokenStakingManager is StakingManager, INativeTokenStakingManager {
using Address for address payable;

constructor(StakingManagerSettings memory settings) {

Choose a reason for hiding this comment

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

we should avoid pasing through constructor since the contract is meant to be upgradeable

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'll wait to merge this PR until after #477, and update all the constructor/initialize calls then.

StakingManager.initialize(settings);
}

/**
* @notice Begins the validator registration process. Locks the provided native asset in the contract as the stake.
* @param nodeID The node ID of the validator being registered.
Expand All @@ -22,7 +28,6 @@ contract NativeTokenStakingManager is StakingManager, INativeTokenStakingManager
* The signature field will be validated by the P-Chain. Implementations may choose to validate that the signature
* field is well-formed but it is not required.
*/

function initializeValidatorRegistration(
bytes32 nodeID,
uint64 registrationExpiry,
Expand Down
33 changes: 13 additions & 20 deletions contracts/staking/StakingManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

pragma solidity 0.8.25;

import {IStakingManager} from "./interfaces/IStakingManager.sol";
import {IStakingManager, StakingManagerSettings} from "./interfaces/IStakingManager.sol";
import {
WarpMessage,
IWarpMessenger
Expand Down Expand Up @@ -78,62 +78,50 @@
}
}

struct InitialStakerInfo {
StakingMessages.ValidationInfo validationInfo;
address owner;
}

struct StakingManagerSettings {
bytes32 pChainBlockchainID;
bytes32 subnetID;
uint256 minimumStakeAmount;
uint256 maximumStakeAmount;
uint64 minimumStakeDuration;
uint8 maximumHourlyChurn;
InitialStakerInfo[] initialStakers;
IRewardCalculator rewardCalculator;
}

function initialize(StakingManagerSettings calldata settings) public {
function initialize(StakingManagerSettings memory settings) public {
StakingManagerStorage storage $ = _getStakingManagerStorage();
$._warpMessenger = IWarpMessenger(0x0200000000000000000000000000000000000005);
$._pChainBlockchainID = settings.pChainBlockchainID;
$._subnetID = settings.subnetID;
$._minimumStakeAmount = settings.minimumStakeAmount;
$._maximumStakeAmount = settings.maximumStakeAmount;
$._minimumStakeDuration = settings.minimumStakeDuration;
$._maximumHourlyChurn = settings.maximumHourlyChurn;
// Add each of the initial stakers as validators
uint64 initialStake;
for (uint256 i; i < settings.initialStakers.length; ++i) {
(bytes32 validationID,) =
StakingMessages.packValidationInfo(settings.initialStakers[i].validationInfo);
$._validationPeriods[validationID] = Validator({
status: ValidatorStatus.Active,
nodeID: settings.initialStakers[i].validationInfo.nodeID,
weight: settings.initialStakers[i].validationInfo.weight,
startedAt: uint64(block.timestamp),
endedAt: 0,
uptimeSeconds: 0,
owner: settings.initialStakers[i].owner,
rewarded: false,
messageNonce: 0
});
initialStake += settings.initialStakers[i].validationInfo.weight;
}
$._remainingInitialStake = initialStake;
$._rewardCalculator = settings.rewardCalculator;
}

/**
* @notice Modifier to ensure that the initial stake has been provided.
*/
modifier onlyWhenInitialStakeProvided() {
StakingManagerStorage storage $ = _getStakingManagerStorage();
require($._remainingInitialStake > 0, "StakingManager: Initial stake not provided");
require(initialStakeProvided(), "StakingManager: Initial stake not provided");
_;
}

function initialStakeProvided() public view returns (bool) {
StakingManagerStorage storage $ = _getStakingManagerStorage();
return $._remainingInitialStake == 0;
}

/**
* @notice Called to provide initial stake amount for original validators added prior to the contract's initialization.
*/
Expand Down Expand Up @@ -425,6 +413,7 @@
// Calculate the reward for the validator.

// Emit event.
emit ValidationPeriodEnded(validationID);
}

/**
Expand All @@ -434,6 +423,10 @@
*/
function _checkAndUpdateChurnTracker(uint64 amount) private {
StakingManagerStorage storage $ = _getStakingManagerStorage();
if ($._maximumHourlyChurn == 0) {
return;
}

ValidatorChurnPeriod storage churnTracker = $._churnTracker;
uint256 currentTime = block.timestamp;
if (currentTime - churnTracker.startedAt >= 1 hours) {
Expand Down
194 changes: 192 additions & 2 deletions contracts/staking/StakingMessages.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ library StakingMessages {
* +-----------+----------+-----------+
* | 148 bytes |
* +-----------+
*
* @param valiationInfo The information to pack into the message.
* @return The validationID and the packed message.
*/
function packRegisterSubnetValidatorMessage(ValidationInfo memory valiationInfo)
internal
cam-schultz marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -70,7 +73,70 @@ library StakingMessages {
}

/**
* @notice Unpacks a byte array as a RegisterSubnetValidator message.
* @notice Unpacks a byte array as a RegisterSubnetValidatorMessage message.
* The message format specification is the same as the one used in above for packing.
*
* @param input The byte array to unpack.
* @return the unpacked ValidationInfo.
*/
function unpackRegisterSubnetValidatorMessage(bytes memory input)
internal
pure
returns (ValidationInfo memory)
{
require(input.length == 148, "StakingMessages: Invalid message length");

// Unpack the type ID
uint32 typeID;
for (uint256 i; i < 4; ++i) {
typeID |= uint32(uint8(input[i])) << uint32((8 * (3 - i)));
}
require(
typeID == SUBNET_VALIDATOR_REGISTRATION_MESSAGE_TYPE_ID,
"StakingMessages: Invalid message type"
);

// Unpack the subnet ID
bytes32 subnetID;
for (uint256 i; i < 32; ++i) {
subnetID |= bytes32(uint256(uint8(input[i + 4])) << (8 * (31 - i)));
}

// Unpack the node ID
bytes32 nodeID;
for (uint256 i; i < 32; ++i) {
nodeID |= bytes32(uint256(uint8(input[i + 36])) << (8 * (31 - i)));
}

// Unpack the weight
uint64 weight;
for (uint256 i; i < 8; ++i) {
weight |= uint64(uint8(input[i + 68])) << uint64((8 * (7 - i)));
}

// Unpack the expiry
uint64 expiry;
for (uint256 i; i < 8; ++i) {
expiry |= uint64(uint8(input[i + 76])) << uint64((8 * (7 - i)));
}

// Unpack the signature
bytes memory signature = new bytes(64);
for (uint256 i; i < 64; ++i) {
signature[i] = input[i + 84];
}

return ValidationInfo({
subnetID: subnetID,
nodeID: nodeID,
weight: weight,
registrationExpiry: expiry,
signature: signature
});
}

/**
* @notice Packs a SubnetValidatorRegistrationMessage into a byte array.
* The message format specification is:
* +--------------+----------+----------+
* | typeID : uint32 | 4 bytes |
Expand All @@ -81,6 +147,35 @@ library StakingMessages {
* +--------------+----------+----------+
* | 37 bytes |
* +----------+
*
* @param validationID The ID of the validation period.
* @param valid true if the validation period was registered, false if it was not and never will be.
* @return The packed message.
*
*/
function packSubnetValidatorRegistrationMessage(
bytes32 validationID,
bool valid
) internal pure returns (bytes memory) {
bytes memory res = new bytes(37);
// Pack the type ID.
for (uint256 i; i < 4; ++i) {
res[i] = bytes1(uint8(SUBNET_VALIDATOR_REGISTRATION_MESSAGE_TYPE_ID >> (8 * (3 - i))));
}
// Pack the validation ID.
for (uint256 i; i < 32; ++i) {
res[i + 4] = bytes1(uint8(uint256(validationID >> (8 * (31 - i)))));
}
// Pack the validity.
res[36] = bytes1(valid ? 1 : 0);
return res;
}

/**
* @notice Unpacks a byte array as a SubnetValidatorRegistrationMessage message.
* The message format specification is the same as the one used in above for packing.
*
* @param input The byte array to unpack.
* @return The validationID and whether or the validation period was registered
* or is not a validator and never will be a validator to do the expiry time passing.
*/
Expand Down Expand Up @@ -127,6 +222,11 @@ library StakingMessages {
* +--------------+----------+----------+
* | 52 bytes |
* +----------+
*
* @param validationID The ID of the validation period.
* @param nonce The nonce of the validation ID.
* @param weight The new weight of the validator.
* @return The packed message.
*/
function packSetSubnetValidatorWeightMessage(
bytes32 validationID,
Expand Down Expand Up @@ -156,6 +256,9 @@ library StakingMessages {
/**
* @notice Unpacks a byte array as a SetSubnetValidatorWeight message.
* The message format specification is the same as the one used in above for packing.
*
* @param input The byte array to unpack.
* @return The validationID, nonce, and weight.
*/
function unpackSetSubnetValidatorWeightMessage(bytes memory input)
internal
Expand Down Expand Up @@ -196,7 +299,7 @@ library StakingMessages {
}

/**
* @notice Unpacks a byte array as a ValidationUptimeMessage.
* @notice Packs a ValidationUptimeMessage into a byte array.
* The message format specification is:
* +--------------+----------+----------+
* | typeID : uint32 | 4 bytes |
Expand All @@ -207,6 +310,37 @@ library StakingMessages {
* +--------------+----------+----------+
* | 44 bytes |
* +----------+
*
* @param validationID The ID of the validation period.
* @param uptime The uptime of the validator.
* @return The packed message.
*/
function packValidationUptimeMessage(

Choose a reason for hiding this comment

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

does abi.encodePacked work for these packs? ref: #482

bytes32 validationID,
uint64 uptime
) internal pure returns (bytes memory) {
bytes memory res = new bytes(44);
// Pack the type ID.
for (uint256 i; i < 4; ++i) {
res[i] = bytes1(uint8(VALIDATION_UPTIME_MESSAGE_TYPE_ID >> (8 * (3 - i))));
}
// Pack the validation ID.
for (uint256 i; i < 32; ++i) {
res[i + 4] = bytes1(uint8(uint256(validationID >> (8 * (31 - i)))));
}
// Pack the uptime.
for (uint256 i; i < 8; ++i) {
res[i + 36] = bytes1(uint8(uptime >> (8 * (7 - i))));
}
return res;
}

/**
* @notice Unpacks a byte array as a ValidationUptimeMessage.
* The message format specification is the same as the one used in above for packing.
*
* @param input The byte array to unpack.
* @return The validationID and uptime.
*/
function unpackValidationUptimeMessage(bytes memory input)
internal
Expand Down Expand Up @@ -256,6 +390,9 @@ library StakingMessages {
* +-----------+----------+-----------+
* | 144 bytes |
* +-----------+
*
* @param validationInfo The information to pack.
* @return The validationID and the packed data.
*/
function packValidationInfo(ValidationInfo memory validationInfo)
internal
Expand Down Expand Up @@ -286,4 +423,57 @@ library StakingMessages {
}
return (sha256(res), res);
}

/**
* @notice Unpacks a byte array as a ValidationInfo.
* The message format specification is the same as the one used in above for packing.
*
* @param input The byte array to unpack.
* @return The unpacked ValidationInfo.
*/
function unpackValidationInfo(bytes memory input)
internal
pure
returns (ValidationInfo memory)
{
require(input.length == 144, "StakingMessages: Invalid message length");

// Unpack the subnetID
bytes32 subnetID;
for (uint256 i; i < 32; ++i) {
subnetID |= bytes32(uint256(uint8(input[i])) << (8 * (31 - i)));
}

// Unpack the nodeID
bytes32 nodeID;
for (uint256 i; i < 32; ++i) {
nodeID |= bytes32(uint256(uint8(input[i + 32])) << (8 * (31 - i)));
}

// Unpack the weight
uint64 weight;
for (uint256 i; i < 8; ++i) {
weight |= uint64(uint8(input[i + 64])) << uint64((8 * (7 - i)));
}

// Unpack the registration expiry
uint64 expiry;
for (uint256 i; i < 8; ++i) {
expiry |= uint64(uint8(input[i + 72])) << uint64((8 * (7 - i)));
}

// Unpack the signature
bytes memory signature = new bytes(64);
for (uint256 i; i < 64; ++i) {
signature[i] = input[i + 80];
}

return ValidationInfo({
subnetID: subnetID,
nodeID: nodeID,
weight: weight,
registrationExpiry: expiry,
signature: signature
});
}
}
19 changes: 19 additions & 0 deletions contracts/staking/interfaces/IStakingManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@

pragma solidity 0.8.25;

import {StakingMessages} from "../StakingMessages.sol";
import {IRewardCalculator} from "./IRewardCalculator.sol";

struct InitialStakerInfo {
StakingMessages.ValidationInfo validationInfo;
address owner;
}

struct StakingManagerSettings {
bytes32 pChainBlockchainID;
bytes32 subnetID;
uint256 minimumStakeAmount;
uint256 maximumStakeAmount;
uint64 minimumStakeDuration;
uint8 maximumHourlyChurn;
InitialStakerInfo[] initialStakers;
IRewardCalculator rewardCalculator;
}

interface IStakingManager {
/**
* @notice Emitted when a new validation period is created by stake being locked in the manager contract.
Expand Down
Loading