-
Notifications
You must be signed in to change notification settings - Fork 42
Staking manager unit tests #476
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
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
6ecadb7
export StakingManagerSettings
36a8e0f
unit test skeleton
4eff319
format
8154fc5
call initialize from ctr
dbc031a
init vdr registration test
0a29c43
resend register vdr test
a6f2949
format
d84f8ee
wip
80080fb
Merge branch 'staking-contract' into staking-manager-unit-tests
304bec6
move files
1c1985f
full pack/unpack parity
4d4ed7d
staking messages tests
23089c0
restrict to view
a80ac9c
complete vdr registration test
f8fc4d5
format
1c0a769
Merge branch 'staking-contract' into staking-manager-unit-tests
cf1e444
init end vdr test
6ea330f
resent init vdr end test
e728e2f
emit validation period ended
5343d39
complete validation test
5df9ae3
format
3480ed9
add expected warp msgs
0087c07
lint
4a15a81
build fix
050bbb0
default constants
5b59a1f
warp precompile mock helpers
85fb6ab
format
de7363a
Merge branch 'staking-contract' into staking-manager-unit-tests
361ce60
icm initializable
9735935
format
7e0e67f
Merge branch 'staking-contract' into staking-manager-unit-tests
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 hidden or 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 |
|---|---|---|
|
|
@@ -10,12 +10,15 @@ import {Address} from "@openzeppelin/[email protected]/utils/Address.sol"; | |
| import {StakingManager} from "./StakingManager.sol"; | ||
| import {Initializable} from | ||
| "@openzeppelin/[email protected]/proxy/utils/Initializable.sol"; | ||
| import {ICMInitializable} from "../utilities/ICMInitializable.sol"; | ||
|
|
||
| contract NativeTokenStakingManager is Initializable, StakingManager, INativeTokenStakingManager { | ||
| using Address for address payable; | ||
|
|
||
| constructor() { | ||
| _disableInitializers(); | ||
| constructor(ICMInitializable init) { | ||
| if (init == ICMInitializable.Disallowed) { | ||
| _disableInitializers(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or 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 hidden or 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 | ||
|
|
@@ -70,7 +73,70 @@ library StakingMessages { | |
| } | ||
|
|
||
| /** | ||
| * @notice Unpacks a byte array as a SubnetValidatorRegistrationMessage 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.