Skip to content

Commit

Permalink
feat: store ciphernode in an incremental merkle tree
Browse files Browse the repository at this point in the history
  • Loading branch information
auryn-macmillan committed Sep 5, 2024
1 parent 2104514 commit 6305380
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 17 deletions.
22 changes: 20 additions & 2 deletions packages/evm/contracts/interfaces/ICiphernodeRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
53 changes: 38 additions & 15 deletions packages/evm/contracts/registry/CiphernodeRegistryOwnable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@ 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 //
// //
////////////////////////////////////////////////////////////

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 => bytes publicKey) public publicKeys;
Expand Down Expand Up @@ -91,13 +98,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);
}
Expand All @@ -114,17 +114,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);
}

////////////////////////////////////////////////////////////
Expand All @@ -139,4 +158,8 @@ 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))));
}
}

0 comments on commit 6305380

Please sign in to comment.