Skip to content

Commit

Permalink
tests for requestValidatorDeregistrations
Browse files Browse the repository at this point in the history
  • Loading branch information
shaspitz committed Sep 14, 2024
1 parent 3568f7f commit 0fe8082
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 3 deletions.
2 changes: 2 additions & 0 deletions contracts/contracts/interfaces/IMevCommitMiddleware.sol
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ interface IMevCommitMiddleware {

error MissingValidatorRecord(bytes blsPubkey);

error ValidatorDeregRequestExists(bytes blsPubkey);

error ValidatorNotReadyToDeregister(bytes blsPubkey, uint256 currentTimestamp, uint256 deregRequestTimestamp);

error VaultAlreadyRegistered(address vault);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ contract MevCommitMiddleware is IMevCommitMiddleware, MevCommitMiddlewareStorage

function _requestValDeregistration(bytes calldata blsPubkey) internal {
require(validatorRecords[blsPubkey].exists, MissingValidatorRecord(blsPubkey));
require(!validatorRecords[blsPubkey].deregRequestOccurrence.exists, ValidatorDeregRequestExists(blsPubkey));
if (msg.sender != owner()) {
_checkCallingOperator(validatorRecords[blsPubkey].operator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,173 @@ contract MevCommitMiddlewareTestCont is MevCommitMiddlewareTest {
mevCommitMiddleware.registerValidators(blsPubkeys, vaults);
}

function test_requestValidatorDeregistrationsMissingValidatorRecord() public {
bytes[] memory blsPubkeys = getSixPubkeys();
vm.expectRevert(
abi.encodeWithSelector(IMevCommitMiddleware.MissingValidatorRecord.selector,
sampleValPubkey1)
);
mevCommitMiddleware.requestValDeregistrations(blsPubkeys);
}

function test_requestValidatorDeregistrationsOnlyOperator() public {
test_registerValidators();
address operator1 = vm.addr(0x1117);
bytes[] memory blsPubkeys = getSixPubkeys();
vm.prank(vm.addr(0x9999999));
vm.expectRevert(
abi.encodeWithSelector(IMevCommitMiddleware.OnlyOperator.selector, operator1)
);
mevCommitMiddleware.requestValDeregistrations(blsPubkeys);
}

function test_requestValidatorDeregistrationsOperatorNotEntity() public {
test_registerValidators();
address operator1 = vm.addr(0x1117);

vm.prank(operator1);
operatorRegistryMock.deregister();

bytes[] memory blsPubkeys = getSixPubkeys();
vm.prank(operator1);
vm.expectRevert(
abi.encodeWithSelector(IMevCommitMiddleware.OperatorNotEntity.selector, operator1)
);
mevCommitMiddleware.requestValDeregistrations(blsPubkeys);
}

function test_requestValidatorDeregistrationsOperatorNotRegistered() public {
test_registerValidators();

address operator1 = vm.addr(0x1117);
address[] memory operators = new address[](1);
operators[0] = operator1;
vm.prank(owner);
mevCommitMiddleware.requestOperatorDeregistrations(operators);
IMevCommitMiddleware.OperatorRecord memory operatorRecord = getOperatorRecord(operator1);
assertTrue(operatorRecord.deregRequestOccurrence.exists);

bytes[] memory blsPubkeys = getSixPubkeys();
vm.prank(operator1);
vm.expectRevert(
abi.encodeWithSelector(IMevCommitMiddleware.OperatorDeregRequestExists.selector, operator1)
);
mevCommitMiddleware.requestValDeregistrations(blsPubkeys);

vm.warp(block.timestamp + mevCommitMiddleware.slashPeriodSeconds() + 1);

vm.prank(owner);
mevCommitMiddleware.deregisterOperators(operators);
operatorRecord = getOperatorRecord(operator1);
assertFalse(operatorRecord.exists);

vm.prank(operator1);
vm.expectRevert(
abi.encodeWithSelector(IMevCommitMiddleware.OperatorNotRegistered.selector, operator1)
);
mevCommitMiddleware.requestValDeregistrations(blsPubkeys);
}

function test_requestValidatorDeregistrationsOperatorIsBlacklisted() public {
test_registerValidators();

address operator1 = vm.addr(0x1117);
address[] memory operators = new address[](1);
operators[0] = operator1;

vm.prank(owner);
mevCommitMiddleware.blacklistOperators(operators);

bytes[] memory blsPubkeys = getSixPubkeys();
vm.prank(operator1);
vm.expectRevert(
abi.encodeWithSelector(IMevCommitMiddleware.OperatorIsBlacklisted.selector, operator1)
);
mevCommitMiddleware.requestValDeregistrations(blsPubkeys);
}

function test_requestValidatorDeregistrationsFromValidOperator() public {
test_registerValidators();
bytes[] memory blsPubkeys = getSixPubkeys();

for (uint256 i = 0; i < blsPubkeys.length; i++) {
IMevCommitMiddleware.ValidatorRecord memory valRecord = getValidatorRecord(blsPubkeys[i]);
assertTrue(valRecord.exists);
assertFalse(valRecord.deregRequestOccurrence.exists);
assertEq(valRecord.deregRequestOccurrence.timestamp, 0);
}

vm.warp(91);

address operator1 = vm.addr(0x1117);
vm.prank(operator1);
vm.expectEmit(true, true, true, true);
emit ValidatorDeregistrationRequested(sampleValPubkey1, operator1, 1);
vm.expectEmit(true, true, true, true);
emit ValidatorDeregistrationRequested(sampleValPubkey2, operator1, 2);
vm.expectEmit(true, true, true, true);
emit ValidatorDeregistrationRequested(sampleValPubkey3, operator1, 3);
vm.expectEmit(true, true, true, true);
emit ValidatorDeregistrationRequested(sampleValPubkey4, operator1, 1);
vm.expectEmit(true, true, true, true);
emit ValidatorDeregistrationRequested(sampleValPubkey5, operator1, 2);
vm.expectEmit(true, true, true, true);
emit ValidatorDeregistrationRequested(sampleValPubkey6, operator1, 3);
mevCommitMiddleware.requestValDeregistrations(blsPubkeys);

for (uint256 i = 0; i < blsPubkeys.length; i++) {
IMevCommitMiddleware.ValidatorRecord memory valRecord = getValidatorRecord(blsPubkeys[i]);
assertTrue(valRecord.exists);
assertTrue(valRecord.deregRequestOccurrence.exists);
assertEq(valRecord.deregRequestOccurrence.timestamp, 91);
}

function test_requestValidatorDeregistrations() public {
// Test function is valid from contract owner or fully valid operator
vm.prank(operator1);
vm.expectRevert(
abi.encodeWithSelector(IMevCommitMiddleware.ValidatorDeregRequestExists.selector, sampleValPubkey1)
);
mevCommitMiddleware.requestValDeregistrations(blsPubkeys);
}

function test_requestValidatorDeregistrationsFromContractOwner() public {
test_registerValidators();
bytes[] memory blsPubkeys = getSixPubkeys();

vm.prank(owner);
vm.expectEmit(true, true, true, true);
emit ValidatorDeregistrationRequested(sampleValPubkey1, owner, 1);
vm.expectEmit(true, true, true, true);
emit ValidatorDeregistrationRequested(sampleValPubkey2, owner, 2);
vm.expectEmit(true, true, true, true);
emit ValidatorDeregistrationRequested(sampleValPubkey3, owner, 3);
vm.expectEmit(true, true, true, true);
emit ValidatorDeregistrationRequested(sampleValPubkey4, owner, 1);
vm.expectEmit(true, true, true, true);
emit ValidatorDeregistrationRequested(sampleValPubkey5, owner, 2);
vm.expectEmit(true, true, true, true);
emit ValidatorDeregistrationRequested(sampleValPubkey6, owner, 3);
mevCommitMiddleware.requestValDeregistrations(blsPubkeys);

vm.prank(owner);
vm.expectRevert(
abi.encodeWithSelector(IMevCommitMiddleware.ValidatorDeregRequestExists.selector, sampleValPubkey1)
);
mevCommitMiddleware.requestValDeregistrations(blsPubkeys);
}

// For repeated use in requestValidatorDeregistrations tests
function getSixPubkeys() internal view returns (bytes[] memory) {
bytes[] memory blsPubkeys = new bytes[](6);
blsPubkeys[0] = sampleValPubkey1;
blsPubkeys[1] = sampleValPubkey2;
blsPubkeys[2] = sampleValPubkey3;
blsPubkeys[3] = sampleValPubkey4;
blsPubkeys[4] = sampleValPubkey5;
blsPubkeys[5] = sampleValPubkey6;
return blsPubkeys;
}

// Test dereg functions are valid from contract owner or fully valid operator

// TODO: val reg cycle
}
10 changes: 9 additions & 1 deletion contracts/test/validator-registry/middleware/RegistryMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ contract RegistryMock is IRegistry {
return msg.sender;
}

function deregister() external {
_removeEntity(msg.sender);
}

function isEntity(
address entity_
) public view returns (bool) {
Expand All @@ -41,8 +45,12 @@ contract RegistryMock is IRegistry {
address entity_
) internal {
_entities.add(entity_);
}

emit AddEntity(entity_);
function _removeEntity(
address entity_
) internal {
_entities.remove(entity_);
}

function _checkEntity(
Expand Down

0 comments on commit 0fe8082

Please sign in to comment.