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 key broadcast contract
Browse files Browse the repository at this point in the history
  • Loading branch information
schmir committed Jul 4, 2023
1 parent 22b1fd4 commit ef70dcf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/IKeyBroadcastContract.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 InvalidKey();
error AlreadyHaveKey();
error NotAllowed();

interface IKeyBroadcastContract {
function broadcastEonKey(uint64 eon, bytes memory key) external;

Expand Down
21 changes: 12 additions & 9 deletions src/KeyBroadcastContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ contract KeyBroadcastContract is IKeyBroadcastContract {
}

function broadcastEonKey(uint64 eon, bytes memory key) external {
require(key.length > 0, "key is empty");
require(keys[eon].length == 0, "key already broadcast for this eon");
require(
msg.sender ==
IKeyperSetManager(keyperSetManagerAddress).getKeyperSetAddress(
eon
),
"not allowed to broadcast eon key"
);
if (key.length == 0) {
revert InvalidKey();
}
if (keys[eon].length > 0) {
revert AlreadyHaveKey();
}
if (
msg.sender !=
IKeyperSetManager(keyperSetManagerAddress).getKeyperSetAddress(eon)
) {
revert NotAllowed();
}

keys[eon] = key;
emit EonKeyBroadcast(eon, key);
Expand Down
6 changes: 3 additions & 3 deletions test/KeyBroadcastContract.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ contract KeyBroadcastTest is Test {
}

function testBroadcastEonKeyEmpty() public {
vm.expectRevert("key is empty");
vm.expectRevert(InvalidKey.selector);
bytes memory key = bytes("");
vm.prank(address(keyperSet));
keyBroadcastContract.broadcastEonKey(1, key);
}

function testBroadcastEonKeyNotAllowed() public {
vm.expectRevert("not allowed to broadcast eon key");
vm.expectRevert(NotAllowed.selector);
bytes memory key = bytes("foo bar");
vm.prank(address(keyperSet));
keyBroadcastContract.broadcastEonKey(0, key);
Expand All @@ -47,7 +47,7 @@ contract KeyBroadcastTest is Test {
vm.prank(address(keyperSet));
keyBroadcastContract.broadcastEonKey(1, key);

vm.expectRevert("key already broadcast for this eon");
vm.expectRevert(AlreadyHaveKey.selector);
vm.prank(address(keyperSet));
keyBroadcastContract.broadcastEonKey(1, key);
}
Expand Down

0 comments on commit ef70dcf

Please sign in to comment.