This repository has been archived by the owner on Aug 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.20; | ||
|
||
interface IKeyBroadcastContract { | ||
function broadcastEonKey(uint64 eon, bytes memory key) external; | ||
|
||
function getEonKey(uint64 eon) external view returns (bytes memory); | ||
|
||
event EonKeyBroadcast(uint64 eon, bytes key); | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.20; | ||
|
||
import "src/IKeyBroadcastContract.sol"; | ||
import "src/IKeyperSetManager.sol"; | ||
|
||
contract KeyBroadcastContract is IKeyBroadcastContract { | ||
mapping(uint64 => bytes) private keys; | ||
address private keyperSetManagerAddress; | ||
|
||
constructor(address _keyperSetManagerAddress) { | ||
keyperSetManagerAddress = _keyperSetManagerAddress; | ||
} | ||
|
||
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" | ||
); | ||
|
||
keys[eon] = key; | ||
emit EonKeyBroadcast(eon, key); | ||
} | ||
|
||
function getEonKey(uint64 eon) external view returns (bytes memory) { | ||
return keys[eon]; | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.20; | ||
|
||
import "forge-std/Test.sol"; | ||
import "../src/IKeyBroadcastContract.sol"; | ||
import "../src/KeyBroadcastContract.sol"; | ||
import "../src/KeyperSetManager.sol"; | ||
import "../src/KeyperSet.sol"; | ||
|
||
contract KeyBroadcastTest is Test { | ||
KeyBroadcastContract public keyBroadcastContract; | ||
KeyperSetManager public keyperSetManager; | ||
KeyperSet public keyperSet; | ||
|
||
event EonKeyBroadcast(uint64 eon, bytes key); | ||
|
||
function setUp() public { | ||
keyperSetManager = new KeyperSetManager(); | ||
keyBroadcastContract = new KeyBroadcastContract( | ||
address(keyperSetManager) | ||
); | ||
keyperSet = new KeyperSet(); | ||
keyperSet.setFinalized(); | ||
keyperSetManager.addKeyperSet(100, address(keyperSet)); | ||
|
||
keyperSet = new KeyperSet(); | ||
keyperSet.setFinalized(); | ||
keyperSetManager.addKeyperSet(200, address(keyperSet)); | ||
} | ||
|
||
function testBroadcastEonKeyEmpty() public { | ||
vm.expectRevert("key is empty"); | ||
bytes memory key = bytes(""); | ||
vm.prank(address(keyperSet)); | ||
keyBroadcastContract.broadcastEonKey(1, key); | ||
} | ||
|
||
function testBroadcastEonKeyNotAllowed() public { | ||
vm.expectRevert("not allowed to broadcast eon key"); | ||
bytes memory key = bytes("foo bar"); | ||
vm.prank(address(keyperSet)); | ||
keyBroadcastContract.broadcastEonKey(0, key); | ||
} | ||
|
||
function testBroadcastEonKeyDuplicate() public { | ||
bytes memory key = bytes("foo bar"); | ||
vm.prank(address(keyperSet)); | ||
keyBroadcastContract.broadcastEonKey(1, key); | ||
|
||
vm.expectRevert("key already broadcast for this eon"); | ||
vm.prank(address(keyperSet)); | ||
keyBroadcastContract.broadcastEonKey(1, key); | ||
} | ||
|
||
function testBroadcastEonKeyEmitsEvent() public { | ||
vm.expectEmit(address(keyBroadcastContract)); | ||
bytes memory key = bytes("foo bar"); | ||
emit EonKeyBroadcast(1, key); | ||
vm.prank(address(keyperSet)); | ||
keyBroadcastContract.broadcastEonKey(1, key); | ||
} | ||
|
||
function testGetEonKey() public { | ||
assertEq(keyBroadcastContract.getEonKey(1), bytes("")); | ||
vm.prank(address(keyperSet)); | ||
keyBroadcastContract.broadcastEonKey(1, bytes("foo bar")); | ||
assertEq(keyBroadcastContract.getEonKey(1), bytes("foo bar")); | ||
} | ||
} |