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 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make keyper set threshold publish eon key
This adds a new contract and contract role to the Keyper contracts: The contract `EonKeyPublish` is the entrypoint for keypers to announce a new Eon key. The contract tracks that the configured threshold of keypers agreed on the same eon key, and only then forwards it to the `KeyBroadcastContract`. The existing logic was changed, so that only a contract, that implements the interface `EonKeyPublisher`, is allowed to call the broadcasting contract. A publisher must be set for each `KeyperSet` before it gets finalized.
- Loading branch information
1 parent
7d7803c
commit dff2b70
Showing
6 changed files
with
227 additions
and
12 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,59 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.22; | ||
|
||
import "src/KeyBroadcastContract.sol"; | ||
|
||
contract EonKeyPublish is EonKeyPublisher { | ||
mapping(uint64 => bytes) private keys; | ||
uint64[] publishers; | ||
KeyperSet keyperSet; | ||
KeyBroadcastContract broadcaster; | ||
uint64 eon; | ||
|
||
constructor( | ||
KeyperSet _keyperSet, | ||
KeyBroadcastContract _broadcaster, | ||
uint64 _eon | ||
) { | ||
keyperSet = KeyperSet(_keyperSet); | ||
broadcaster = KeyBroadcastContract(_broadcaster); | ||
eon = _eon; | ||
} | ||
|
||
function eonKeyConfirmed(bytes memory eonKey) public view returns (bool) { | ||
uint required = keyperSet.getThreshold(); | ||
if (publishers.length >= keyperSet.getThreshold()) { | ||
for (uint i = 0; i < publishers.length; i++) { | ||
if (keccak256(keys[publishers[i]]) == keccak256(eonKey)) { | ||
required--; | ||
if (required == 0) { | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
return required <= 0; | ||
} | ||
|
||
function publishEonKey(bytes memory eonKey) public { | ||
if (eonKey.length == 0) { | ||
revert InvalidKey(); | ||
} | ||
if (!keyperSet.isFinalized()) { | ||
revert KeyperSetNotFinalized(); | ||
} | ||
uint64 keyperId = uint64(keyperSet.keyperIndex(msg.sender)); | ||
if (keyperId < 0) { | ||
revert NotAllowed(); | ||
} | ||
publishers.push(keyperId); | ||
if (eonKeyConfirmed(eonKey)) { | ||
// broadcast | ||
broadcaster.broadcastEonKey(eon, eonKey); | ||
//exit | ||
return; | ||
} | ||
// only store key, if not yet broadcast | ||
keys[keyperId] = eonKey; | ||
} | ||
} |
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
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
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,96 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.22; | ||
|
||
import "forge-std/Test.sol"; | ||
import "../src/EonKeyPublish.sol"; | ||
|
||
contract EonKeyPublishTest is Test { | ||
KeyperSetManager public manager; | ||
EonKeyPublish public eonKeyPublish; | ||
KeyperSet public keyperSet0; | ||
KeyperSet public keyperSet; | ||
KeyBroadcastContract public broadcastContract; | ||
address public initializer; | ||
address public dao; | ||
|
||
function setUp() public { | ||
initializer = address(19); | ||
dao = address(42); | ||
manager = new KeyperSetManager(initializer); | ||
vm.prank(initializer); | ||
manager.initialize(dao, address(420)); | ||
broadcastContract = new KeyBroadcastContract(address(manager)); | ||
keyperSet = new KeyperSet(); | ||
keyperSet0 = new KeyperSet(); | ||
keyperSet0.setFinalized(); | ||
vm.prank(dao); | ||
manager.addKeyperSet(uint64(10), address(keyperSet0)); | ||
eonKeyPublish = new EonKeyPublish(keyperSet, broadcastContract, 1); | ||
keyperSet.setKeyBroadcaster(address(broadcastContract)); | ||
} | ||
|
||
function testPublishEonKeyNotFinalized() public { | ||
vm.expectRevert(KeyperSetNotFinalized.selector); | ||
eonKeyPublish.publishEonKey("deadbeef"); | ||
} | ||
|
||
function testAllowedToBroadcast() public { | ||
KeyperSet ks; | ||
EonKeyPublish publisher; | ||
uint64 eon = 2; | ||
address[] memory members = new address[](3); | ||
members[0] = address(81); | ||
members[1] = address(82); | ||
members[2] = address(83); | ||
ks = new KeyperSet(); | ||
publisher = new EonKeyPublish(ks, broadcastContract, eon); | ||
ks.setKeyBroadcaster(address(broadcastContract)); | ||
ks.setPublisher(address(publisher)); | ||
ks.addMembers(members); | ||
ks.setFinalized(); | ||
vm.prank(dao); | ||
manager.addKeyperSet(uint64(10), address(ks)); | ||
vm.prank(address(publisher)); | ||
assertEq(ks.isAllowedToBroadcastEonKey(address(publisher)), true); | ||
} | ||
|
||
function testPublishEonKey() public { | ||
uint64 eon = 1; | ||
address[] memory members = new address[](5); | ||
members[0] = address(91); | ||
members[1] = address(92); | ||
members[2] = address(93); | ||
members[3] = address(94); | ||
members[4] = address(95); | ||
keyperSet.addMembers(members); | ||
keyperSet.setThreshold(3); | ||
keyperSet.setPublisher(address(eonKeyPublish)); | ||
keyperSet.setFinalized(); | ||
assertEq(keyperSet.getThreshold(), 3); | ||
vm.startPrank(dao); | ||
manager.addKeyperSet(uint64(block.number + 10), address(keyperSet)); | ||
vm.stopPrank(); | ||
for (uint i = 0; i < keyperSet.getThreshold(); i++) { | ||
vm.prank(members[i]); | ||
eonKeyPublish.publishEonKey(bytes("deadbeef")); | ||
} | ||
assertEq(keyperSet.getPublisher(), address(eonKeyPublish)); | ||
assertEq( | ||
keyperSet.isAllowedToBroadcastEonKey(address(eonKeyPublish)), | ||
true | ||
); | ||
uint threshold = keyperSet.getThreshold(); | ||
vm.startPrank(members[threshold]); | ||
assertEq(broadcastContract.getEonKey(eon), bytes("")); | ||
// calling broadcast directly should not be allowed: | ||
assertEq( | ||
keyperSet.isAllowedToBroadcastEonKey(members[threshold]), | ||
false | ||
); | ||
vm.expectRevert(NotAllowed.selector); | ||
broadcastContract.broadcastEonKey(eon, bytes("deadbeef")); | ||
eonKeyPublish.publishEonKey(bytes("deadbeef")); | ||
assertEq(broadcastContract.getEonKey(eon), bytes("deadbeef")); | ||
vm.stopPrank(); | ||
} | ||
} |
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
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