diff --git a/packages/evm/contracts/interfaces/ICiphernodeRegistry.sol b/packages/evm/contracts/interfaces/ICiphernodeRegistry.sol index 2a01199f..6a48f70a 100644 --- a/packages/evm/contracts/interfaces/ICiphernodeRegistry.sol +++ b/packages/evm/contracts/interfaces/ICiphernodeRegistry.sol @@ -22,10 +22,28 @@ interface ICiphernodeRegistry { event EnclaveSet(address indexed enclave); /// @notice This event MUST be emitted when a ciphernode is added to the registry. - event CiphernodeAdded(address indexed node); + /// @param node Address of the ciphernode. + /// @param index Index of the ciphernode in the registry. + /// @param numNodes Number of ciphernodes in the registry. + /// @param size Size of the registry. + event CiphernodeAdded( + address indexed node, + uint256 index, + uint256 numNodes, + uint256 size + ); /// @notice This event MUST be emitted when a ciphernode is removed from the registry. - event CiphernodeRemoved(address indexed node); + /// @param node Address of the ciphernode. + /// @param index Index of the ciphernode in the registry. + /// @param numNodes Number of ciphernodes in the registry. + /// @param size Size of the registry. + event CiphernodeRemoved( + address indexed node, + uint256 index, + uint256 numNodes, + uint256 size + ); function isCiphernodeEligible(address ciphernode) external returns (bool); diff --git a/packages/evm/contracts/registry/CiphernodeRegistryOwnable.sol b/packages/evm/contracts/registry/CiphernodeRegistryOwnable.sol index 6793fdf2..96c4c54b 100644 --- a/packages/evm/contracts/registry/CiphernodeRegistryOwnable.sol +++ b/packages/evm/contracts/registry/CiphernodeRegistryOwnable.sol @@ -6,8 +6,15 @@ import { IRegistryFilter } from "../interfaces/IRegistryFilter.sol"; import { OwnableUpgradeable } from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; +import { + InternalLeanIMT, + LeanIMTData, + PoseidonT3 +} from "@zk-kit/lean-imt.sol/InternalLeanIMT.sol"; contract CiphernodeRegistryOwnable is ICiphernodeRegistry, OwnableUpgradeable { + using InternalLeanIMT for LeanIMTData; + //////////////////////////////////////////////////////////// // // // Storage Variables // @@ -15,10 +22,11 @@ contract CiphernodeRegistryOwnable is ICiphernodeRegistry, OwnableUpgradeable { //////////////////////////////////////////////////////////// address public enclave; - - mapping(address ciphernode => bool isEnabled) public isEnabled; + uint256 public numCiphernodes; + LeanIMTData public ciphernodes; mapping(uint256 e3Id => IRegistryFilter filter) public requests; + mapping(uint256 e3Id => uint256 root) public roots; mapping(uint256 e3Id => bytes publicKey) public publicKeys; //////////////////////////////////////////////////////////// @@ -77,6 +85,7 @@ contract CiphernodeRegistryOwnable is ICiphernodeRegistry, OwnableUpgradeable { CommitteeAlreadyRequested() ); requests[e3Id] = IRegistryFilter(filter); + roots[e3Id] = root(); IRegistryFilter(filter).requestCommittee(e3Id, threshold); emit CommitteeRequested(e3Id, filter, threshold); @@ -91,13 +100,6 @@ contract CiphernodeRegistryOwnable is ICiphernodeRegistry, OwnableUpgradeable { // only to be published by the filter require(address(requests[e3Id]) == msg.sender, CommitteeDoesNotExist()); - // for (uint256 i = 0; i < ciphernodes.length; i++) { - // require( - // isEnabled[ciphernodes[i]] == true, - // CiphernodeNotEnabled(ciphernodes[i]) - // ); - // } - publicKeys[e3Id] = publicKey; emit CommitteePublished(e3Id, publicKey); } @@ -114,17 +116,36 @@ contract CiphernodeRegistryOwnable is ICiphernodeRegistry, OwnableUpgradeable { } function addCiphernode(address node) external onlyOwner { - isEnabled[node] = true; - emit CiphernodeAdded(node); + uint256 ciphernode = uint256(bytes32(bytes20(node))); + ciphernodes._insert(ciphernode); + numCiphernodes++; + emit CiphernodeAdded( + node, + ciphernodes._indexOf(ciphernode), + numCiphernodes, + ciphernodes.size + ); } - function removeCiphernode(address node) external onlyOwner { - isEnabled[node] = false; - emit CiphernodeRemoved(node); + function removeCiphernode( + address node, + uint256[] calldata siblingNodes + ) external onlyOwner { + uint256 ciphernode = uint256(bytes32(bytes20(node))); + ciphernodes._remove(ciphernode, siblingNodes); + uint256 index = ciphernodes._indexOf(ciphernode); + numCiphernodes--; + emit CiphernodeAdded( + node, + ciphernodes._indexOf(ciphernode), + numCiphernodes, + ciphernodes.size + ); + emit CiphernodeRemoved(node, index, numCiphernodes, ciphernodes.size); } function isCiphernodeEligible(address node) external view returns (bool) { - return isEnabled[node]; + return isEnabled(node); } //////////////////////////////////////////////////////////// @@ -139,4 +160,16 @@ contract CiphernodeRegistryOwnable is ICiphernodeRegistry, OwnableUpgradeable { publicKey = publicKeys[e3Id]; require(publicKey.length > 0, CommitteeNotPublished()); } + + function isEnabled(address node) public view returns (bool) { + return ciphernodes._has(uint256(bytes32(bytes20(node)))); + } + + function root() public view returns (uint256) { + return (ciphernodes._root()); + } + + function rootAt(uint256 e3Id) public view returns (uint256) { + return roots[e3Id]; + } }