-
Notifications
You must be signed in to change notification settings - Fork 27
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
+699
−15
Merged
Changes from 24 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
6ecadb7
export StakingManagerSettings
cam-schultz 36a8e0f
unit test skeleton
cam-schultz 4eff319
format
cam-schultz 8154fc5
call initialize from ctr
cam-schultz dbc031a
init vdr registration test
cam-schultz 0a29c43
resend register vdr test
cam-schultz a6f2949
format
cam-schultz d84f8ee
wip
cam-schultz 80080fb
Merge branch 'staking-contract' into staking-manager-unit-tests
cam-schultz 304bec6
move files
cam-schultz 1c1985f
full pack/unpack parity
cam-schultz 4d4ed7d
staking messages tests
cam-schultz 23089c0
restrict to view
cam-schultz a80ac9c
complete vdr registration test
cam-schultz f8fc4d5
format
cam-schultz 1c0a769
Merge branch 'staking-contract' into staking-manager-unit-tests
cam-schultz cf1e444
init end vdr test
cam-schultz 6ea330f
resent init vdr end test
cam-schultz e728e2f
emit validation period ended
cam-schultz 5343d39
complete validation test
cam-schultz 5df9ae3
format
cam-schultz 3480ed9
add expected warp msgs
cam-schultz 0087c07
lint
cam-schultz 4a15a81
build fix
cam-schultz 050bbb0
default constants
cam-schultz 5b59a1f
warp precompile mock helpers
cam-schultz 85fb6ab
format
cam-schultz de7363a
Merge branch 'staking-contract' into staking-manager-unit-tests
cam-schultz 361ce60
icm initializable
cam-schultz 9735935
format
cam-schultz 7e0e67f
Merge branch 'staking-contract' into staking-manager-unit-tests
cam-schultz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
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. | ||
|
@@ -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, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
|
||
|
@@ -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 | | ||
|
@@ -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. | ||
*/ | ||
|
@@ -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, | ||
|
@@ -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 | ||
|
@@ -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 | | ||
|
@@ -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( | ||
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. does |
||
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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back 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.
we should avoid pasing through constructor since the contract is meant to be upgradeable
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'll wait to merge this PR until after #477, and update all the constructor/initialize calls then.