Skip to content
This repository has been archived by the owner on Aug 26, 2024. It is now read-only.

Commit

Permalink
Use custom errors for the keyper set manager contract
Browse files Browse the repository at this point in the history
  • Loading branch information
schmir committed Jul 1, 2023
1 parent e9ebd35 commit 49203bd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/IKeyperSetManager.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

error KeyperSetNotFinalized();
error AlreadyHaveKeyperSet();
error NoActiveKeyperSet();

interface IKeyperSetManager {
function addKeyperSet(
uint64 activationSlot,
Expand Down
20 changes: 10 additions & 10 deletions src/KeyperSetManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ contract KeyperSetManager is IKeyperSetManager, Ownable {
uint64 activationSlot,
address keyperSetContract
) external onlyOwner {
require(
contracts.length == 0 ||
activationSlot >= activationSlots[activationSlots.length - 1],
"keyper set already added for this activation slot"
);
require(
IKeyperSet(keyperSetContract).isFinalized(),
"keyper set contract not finalized"
);
if (
contracts.length > 0 &&
activationSlot < activationSlots[activationSlots.length - 1]
) {
revert AlreadyHaveKeyperSet();
}
if (!IKeyperSet(keyperSetContract).isFinalized()) {
revert KeyperSetNotFinalized();
}
activationSlots.push(activationSlot);
contracts.push(keyperSetContract);
emit KeyperSetAdded(activationSlot, keyperSetContract);
Expand All @@ -39,7 +39,7 @@ contract KeyperSetManager is IKeyperSetManager, Ownable {
return uint64(i - 1);
}
}
revert("no active keyper set found for given slot");
revert NoActiveKeyperSet();
}

function getKeyperSetAddress(uint64 index) external view returns (address) {
Expand Down
10 changes: 5 additions & 5 deletions test/KeyperSetManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,30 @@ contract KeyperSetManagerTest is Test {

function testAddKeyperSetRequiresFinalizedSet() public {
KeyperSet ks = new KeyperSet();
vm.expectRevert("keyper set contract not finalized");
vm.expectRevert(KeyperSetNotFinalized.selector);
keyperSetManager.addKeyperSet(0, address(ks));
}

function testAddKeyperSetRequiresIncreasingActivationBlock() public {
keyperSetManager.addKeyperSet(1000, address(members0));
vm.expectRevert("keyper set already added for this activation slot");
vm.expectRevert(AlreadyHaveKeyperSet.selector);
keyperSetManager.addKeyperSet(999, address(members1));
keyperSetManager.addKeyperSet(1000, address(members1));
}

function testGetKeyperSetIndexBySlotEmpty() public {
vm.expectRevert("no active keyper set found for given slot");
vm.expectRevert(NoActiveKeyperSet.selector);
keyperSetManager.getKeyperSetIndexBySlot(0);
}

function testGetKeyperSetIndexBySlot() public {
keyperSetManager.addKeyperSet(1000, address(members0));
keyperSetManager.addKeyperSet(1100, address(members1));

vm.expectRevert("no active keyper set found for given slot");
vm.expectRevert(NoActiveKeyperSet.selector);
keyperSetManager.getKeyperSetIndexBySlot(0);

vm.expectRevert("no active keyper set found for given slot");
vm.expectRevert(NoActiveKeyperSet.selector);
keyperSetManager.getKeyperSetIndexBySlot(999);

assertEq(keyperSetManager.getKeyperSetIndexBySlot(1000), 0);
Expand Down

0 comments on commit 49203bd

Please sign in to comment.