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

Commit

Permalink
Add KeyBroadcastContract
Browse files Browse the repository at this point in the history
  • Loading branch information
schmir committed Jul 1, 2023
1 parent a3eff31 commit 44ebf31
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/IKeyBroadcastContract.sol
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);
}
33 changes: 33 additions & 0 deletions src/KeyBroadcastContract.sol
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];
}
}
69 changes: 69 additions & 0 deletions test/KeyBroadcastContract.t.sol
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"));
}
}

0 comments on commit 44ebf31

Please sign in to comment.