Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
geoff-vball committed Sep 6, 2024
1 parent d54c724 commit 885b8d9
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 58 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions contracts/staking/PoSValidatorManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ abstract contract PoSValidatorManager is IPoSValidatorManager, ValidatorManager

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

_checkAndUpdateChurnTracker(weight);
Expand Down Expand Up @@ -262,7 +262,7 @@ abstract contract PoSValidatorManager is IPoSValidatorManager, ValidatorManager
require(
delegator.owner == _msgSender(), "PoSValidatorManager: delegation not owned by sender"
);
uint64 nonce = _getAndIncrementNonce(validationID);
uint64 nonce = _incrementAndGetNonce(validationID);

// Set the delegator status to pending removed, so that it can be properly removed in
// the complete step, even if the delivered nonce is greater than the nonce used to
Expand Down Expand Up @@ -299,8 +299,9 @@ abstract contract PoSValidatorManager is IPoSValidatorManager, ValidatorManager
PoSValidatorManagerStorage storage $ = _getPoSValidatorManagerStorage();
Delegator memory delegator = $._delegatorStakes[delegationID];
require(
$._delegatorStakes[delegationID].status == DelegatorStatus.PendingAdded,
"PoSValidatorManager: delegation registration not pending"
delegator.status == DelegatorStatus.PendingAdded
|| delegator.status == DelegatorStatus.PendingRemoved,
"PoSValidatorManager: delegation status not pending"
);

Validator memory validator = _getValidator(delegator.validationID);
Expand Down
24 changes: 11 additions & 13 deletions contracts/staking/ValidatorManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,12 @@ abstract contract ValidatorManager is
nodeID: nodeID,
owner: _msgSender(),
startingWeight: weight,
messageNonce: 0,
messageNonce: 1,
weight: weight,
startedAt: 0, // The validation period only starts once the registration is acknowledged.
endedAt: 0
});
// Increment the nonce for the next usage.
_getAndIncrementNonce(validationID);

emit ValidationPeriodCreated(validationID, nodeID, messageID, weight, registrationExpiry);

return validationID;
Expand Down Expand Up @@ -235,7 +234,7 @@ abstract contract ValidatorManager is

// Submit the message to the Warp precompile.
bytes memory setValidatorWeightPayload = ValidatorMessages
.packSetSubnetValidatorWeightMessage(validationID, _getAndIncrementNonce(validationID), 0);
.packSetSubnetValidatorWeightMessage(validationID, _incrementAndGetNonce(validationID), 0);

bytes32 messageID = WARP_MESSENGER.sendWarpMessage(setValidatorWeightPayload);

Expand All @@ -250,18 +249,18 @@ abstract contract ValidatorManager is
// solhint-disable-next-line
function resendEndValidatorMessage(bytes32 validationID) external {
ValidatorManagerStorage storage $ = _getValidatorManagerStorage();
Validator memory validator = $._validationPeriods[validationID];

require(
$._validationPeriods[validationID].status == ValidatorStatus.PendingRemoved,
validator.status == ValidatorStatus.PendingRemoved,
"ValidatorManager: Validator not pending removal"
);

bytes memory setValidatorWeightPayload = ValidatorMessages
.packSetSubnetValidatorWeightMessage(
validationID, $._validationPeriods[validationID].messageNonce, 0
WARP_MESSENGER.sendWarpMessage(
ValidatorMessages.packSetSubnetValidatorWeightMessage(
validationID, validator.messageNonce, 0
)
);

WARP_MESSENGER.sendWarpMessage(setValidatorWeightPayload);
}

/**
Expand Down Expand Up @@ -347,11 +346,10 @@ abstract contract ValidatorManager is
$._churnTracker = churnTracker;
}

function _getAndIncrementNonce(bytes32 validationID) internal returns (uint64) {
function _incrementAndGetNonce(bytes32 validationID) internal returns (uint64) {
ValidatorManagerStorage storage $ = _getValidatorManagerStorage();
uint64 currentNonce = $._validationPeriods[validationID].messageNonce;
$._validationPeriods[validationID].messageNonce++;
return currentNonce;
return $._validationPeriods[validationID].messageNonce;
}

function _getPChainWarpMessage(uint32 messageIndex)
Expand Down
72 changes: 36 additions & 36 deletions contracts/staking/tests/PoSValidatorManagerTests.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
weight: DEFAULT_DELEGATOR_WEIGHT,
registrationTimestamp: DEFAULT_DELEGATOR_INIT_REGISTRATION_TIMESTAMP,
expectedValidatorWeight: DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT,
expectedNonce: 1
expectedNonce: 2
});
}

Expand All @@ -181,11 +181,11 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
weight: DEFAULT_DELEGATOR_WEIGHT,
registrationTimestamp: DEFAULT_DELEGATOR_INIT_REGISTRATION_TIMESTAMP,
expectedValidatorWeight: DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT,
expectedNonce: 1
expectedNonce: 2
});
bytes memory setValidatorWeightPayload = ValidatorMessages
.packSetSubnetValidatorWeightMessage(
validationID, 1, DEFAULT_WEIGHT + DEFAULT_DELEGATOR_WEIGHT
validationID, 2, DEFAULT_WEIGHT + DEFAULT_DELEGATOR_WEIGHT
);
_mockSendWarpMessage(setValidatorWeightPayload, bytes32(0));
posValidatorManager.resendUpdateDelegation(delegationID);
Expand All @@ -206,14 +206,14 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
weight: DEFAULT_DELEGATOR_WEIGHT,
registrationTimestamp: DEFAULT_DELEGATOR_INIT_REGISTRATION_TIMESTAMP,
expectedValidatorWeight: DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT,
expectedNonce: 1
expectedNonce: 2
});
_setUpCompleteDelegatorRegistration(
validationID,
delegationID,
DEFAULT_DELEGATOR_COMPLETE_REGISTRATION_TIMESTAMP,
DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT,
1
2
);
}

Expand All @@ -234,7 +234,7 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
weight: DEFAULT_DELEGATOR_WEIGHT,
registrationTimestamp: DEFAULT_DELEGATOR_INIT_REGISTRATION_TIMESTAMP,
expectedValidatorWeight: DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT,
expectedNonce: 1
expectedNonce: 2
});
address delegator2 = address(0x5678567856785678567856785678567856785678);
bytes32 delegationID2 = _setUpInitializeDelegatorRegistration({
Expand All @@ -244,14 +244,14 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
registrationTimestamp: DEFAULT_DELEGATOR_INIT_REGISTRATION_TIMESTAMP + 1,
expectedValidatorWeight: DEFAULT_DELEGATOR_WEIGHT + DEFAULT_DELEGATOR_WEIGHT
+ DEFAULT_WEIGHT,
expectedNonce: 2
expectedNonce: 3
});

// Complete registration of delegator2 with delegator1's nonce
// Note that registering delegator1 with delegator2's nonce is valid
bytes memory setValidatorWeightPayload = ValidatorMessages
.packSubnetValidatorWeightUpdateMessage(
validationID, 1, DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT
validationID, 2, DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT
);
_mockGetVerifiedWarpMessage(setValidatorWeightPayload, true);

Expand All @@ -277,7 +277,7 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
weight: DEFAULT_DELEGATOR_WEIGHT,
registrationTimestamp: DEFAULT_DELEGATOR_INIT_REGISTRATION_TIMESTAMP,
expectedValidatorWeight: DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT,
expectedNonce: 1
expectedNonce: 2
});
address delegator2 = address(0x5678567856785678567856785678567856785678);
_setUpInitializeDelegatorRegistration({
Expand All @@ -287,15 +287,15 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
registrationTimestamp: DEFAULT_DELEGATOR_INIT_REGISTRATION_TIMESTAMP + 1,
expectedValidatorWeight: DEFAULT_DELEGATOR_WEIGHT + DEFAULT_DELEGATOR_WEIGHT
+ DEFAULT_WEIGHT,
expectedNonce: 2
expectedNonce: 3
});
// Mark delegator1 as registered by delivering the weight update from nonce 2 (delegator 2's nonce)
_setUpCompleteDelegatorRegistration(
validationID,
delegationID1,
DEFAULT_DELEGATOR_COMPLETE_REGISTRATION_TIMESTAMP,
DEFAULT_DELEGATOR_WEIGHT + DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT,
2
3
);
}

Expand All @@ -314,22 +314,22 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
weight: DEFAULT_DELEGATOR_WEIGHT,
registrationTimestamp: DEFAULT_DELEGATOR_INIT_REGISTRATION_TIMESTAMP,
expectedValidatorWeight: DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT,
expectedNonce: 1
expectedNonce: 2
});
_setUpCompleteDelegatorRegistration(
validationID,
delegationID,
DEFAULT_DELEGATOR_COMPLETE_REGISTRATION_TIMESTAMP,
DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT,
1
2
);
_setUpInitializeEndDelegation({
validationID: validationID,
delegatorAddress: DEFAULT_DELEGATOR_ADDRESS,
delegationID: delegationID,
endDelegationTimestamp: DEFAULT_DELEGATOR_END_DELEGATION_TIMESTAMP,
expectedValidatorWeight: DEFAULT_WEIGHT,
expectedNonce: 2
expectedNonce: 3
});
}

Expand All @@ -348,25 +348,25 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
weight: DEFAULT_DELEGATOR_WEIGHT,
registrationTimestamp: DEFAULT_DELEGATOR_INIT_REGISTRATION_TIMESTAMP,
expectedValidatorWeight: DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT,
expectedNonce: 1
expectedNonce: 2
});
_setUpCompleteDelegatorRegistration(
validationID,
delegationID,
DEFAULT_DELEGATOR_COMPLETE_REGISTRATION_TIMESTAMP,
DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT,
1
2
);
_setUpInitializeEndDelegation({
validationID: validationID,
delegatorAddress: DEFAULT_DELEGATOR_ADDRESS,
delegationID: delegationID,
endDelegationTimestamp: DEFAULT_DELEGATOR_END_DELEGATION_TIMESTAMP,
expectedValidatorWeight: DEFAULT_WEIGHT,
expectedNonce: 2
expectedNonce: 3
});
bytes memory setValidatorWeightPayload =
ValidatorMessages.packSetSubnetValidatorWeightMessage(validationID, 2, DEFAULT_WEIGHT);
ValidatorMessages.packSetSubnetValidatorWeightMessage(validationID, 3, DEFAULT_WEIGHT);
_mockSendWarpMessage(setValidatorWeightPayload, bytes32(0));
posValidatorManager.resendUpdateDelegation(delegationID);
}
Expand All @@ -386,24 +386,24 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
weight: DEFAULT_DELEGATOR_WEIGHT,
registrationTimestamp: DEFAULT_DELEGATOR_INIT_REGISTRATION_TIMESTAMP,
expectedValidatorWeight: DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT,
expectedNonce: 1
expectedNonce: 2
});
_setUpCompleteDelegatorRegistration(
validationID,
delegationID,
DEFAULT_DELEGATOR_COMPLETE_REGISTRATION_TIMESTAMP,
DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT,
1
2
);
_setUpInitializeEndDelegation({
validationID: validationID,
delegatorAddress: DEFAULT_DELEGATOR_ADDRESS,
delegationID: delegationID,
endDelegationTimestamp: DEFAULT_DELEGATOR_END_DELEGATION_TIMESTAMP,
expectedValidatorWeight: DEFAULT_WEIGHT,
expectedNonce: 2
expectedNonce: 3
});
_setUpCompleteEndDelegation(validationID, delegationID, DEFAULT_WEIGHT, DEFAULT_WEIGHT, 2);
_setUpCompleteEndDelegation(validationID, delegationID, DEFAULT_WEIGHT, DEFAULT_WEIGHT, 3);
}

function testCompleteEndDelegationWrongNonce() public {
Expand All @@ -423,14 +423,14 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
weight: DEFAULT_DELEGATOR_WEIGHT,
registrationTimestamp: DEFAULT_DELEGATOR_INIT_REGISTRATION_TIMESTAMP,
expectedValidatorWeight: DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT,
expectedNonce: 1
expectedNonce: 2
});
_setUpCompleteDelegatorRegistration(
validationID,
delegationID1,
DEFAULT_DELEGATOR_COMPLETE_REGISTRATION_TIMESTAMP,
DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT,
1
2
);
address delegator2 = address(0x5678567856785678567856785678567856785678);
bytes32 delegationID2 = _setUpInitializeDelegatorRegistration({
Expand All @@ -440,14 +440,14 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
registrationTimestamp: DEFAULT_DELEGATOR_INIT_REGISTRATION_TIMESTAMP + 1,
expectedValidatorWeight: DEFAULT_DELEGATOR_WEIGHT + DEFAULT_DELEGATOR_WEIGHT
+ DEFAULT_WEIGHT,
expectedNonce: 2
expectedNonce: 3
});
_setUpCompleteDelegatorRegistration(
validationID,
delegationID2,
DEFAULT_DELEGATOR_COMPLETE_REGISTRATION_TIMESTAMP,
DEFAULT_DELEGATOR_WEIGHT + DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT,
2
3
);

// Initialize end delegation for both delegators
Expand All @@ -457,22 +457,22 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
delegationID: delegationID1,
endDelegationTimestamp: DEFAULT_DELEGATOR_END_DELEGATION_TIMESTAMP,
expectedValidatorWeight: DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT,
expectedNonce: 3
expectedNonce: 4
});
_setUpInitializeEndDelegation({
validationID: validationID,
delegatorAddress: delegator2,
delegationID: delegationID2,
endDelegationTimestamp: DEFAULT_DELEGATOR_END_DELEGATION_TIMESTAMP + 1,
expectedValidatorWeight: DEFAULT_WEIGHT,
expectedNonce: 4
expectedNonce: 5
});

// Complete ending delegator2 with delegator1's nonce
// Note that ending delegator1 with delegator2's nonce is valid
bytes memory setValidatorWeightPayload = ValidatorMessages
.packSubnetValidatorWeightUpdateMessage(
validationID, 3, DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT
validationID, 4, DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT
);
_mockGetVerifiedWarpMessage(setValidatorWeightPayload, true);

Expand All @@ -497,14 +497,14 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
weight: DEFAULT_DELEGATOR_WEIGHT,
registrationTimestamp: DEFAULT_DELEGATOR_INIT_REGISTRATION_TIMESTAMP,
expectedValidatorWeight: DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT,
expectedNonce: 1
expectedNonce: 2
});
_setUpCompleteDelegatorRegistration(
validationID,
delegationID1,
DEFAULT_DELEGATOR_COMPLETE_REGISTRATION_TIMESTAMP,
DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT,
1
2
);
address delegator2 = address(0x5678567856785678567856785678567856785678);
bytes32 delegationID2 = _setUpInitializeDelegatorRegistration({
Expand All @@ -514,14 +514,14 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
registrationTimestamp: DEFAULT_DELEGATOR_INIT_REGISTRATION_TIMESTAMP + 1,
expectedValidatorWeight: DEFAULT_DELEGATOR_WEIGHT + DEFAULT_DELEGATOR_WEIGHT
+ DEFAULT_WEIGHT,
expectedNonce: 2
expectedNonce: 3
});
_setUpCompleteDelegatorRegistration(
validationID,
delegationID2,
DEFAULT_DELEGATOR_COMPLETE_REGISTRATION_TIMESTAMP,
DEFAULT_DELEGATOR_WEIGHT + DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT,
2
3
);

// Initialize end delegation for both delegators
Expand All @@ -531,19 +531,19 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
delegationID: delegationID1,
endDelegationTimestamp: DEFAULT_DELEGATOR_END_DELEGATION_TIMESTAMP,
expectedValidatorWeight: DEFAULT_DELEGATOR_WEIGHT + DEFAULT_WEIGHT,
expectedNonce: 3
expectedNonce: 4
});
_setUpInitializeEndDelegation({
validationID: validationID,
delegatorAddress: delegator2,
delegationID: delegationID2,
endDelegationTimestamp: DEFAULT_DELEGATOR_END_DELEGATION_TIMESTAMP + 1,
expectedValidatorWeight: DEFAULT_WEIGHT,
expectedNonce: 4
expectedNonce: 5
});

// Complete delegation1 by delivering the weight update from nonce 4 (delegator2's nonce)
_setUpCompleteEndDelegation(validationID, delegationID1, DEFAULT_WEIGHT, DEFAULT_WEIGHT, 4);
_setUpCompleteEndDelegation(validationID, delegationID1, DEFAULT_WEIGHT, DEFAULT_WEIGHT, 5);
}

function testValueToWeight() public view {
Expand Down
Loading

0 comments on commit 885b8d9

Please sign in to comment.