Skip to content

Commit

Permalink
Merge pull request #2 from Layr-Labs/alex/reorder-functions
Browse files Browse the repository at this point in the history
Organize and clean up file and function layout
  • Loading branch information
wadealexc authored Oct 18, 2023
2 parents bc5b758 + 3da5e12 commit 5b1b729
Show file tree
Hide file tree
Showing 10 changed files with 867 additions and 801 deletions.
12 changes: 10 additions & 2 deletions src/BLSOperatorStateRetriever.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ contract BLSOperatorStateRetriever {
* 2) 2d array of Operator structs. For each quorum the provided operator
* was a part of at `blockNumber`, an ordered list of operators.
*/
function getOperatorState(IBLSRegistryCoordinatorWithIndices registryCoordinator, bytes32 operatorId, uint32 blockNumber) external view returns (uint256, Operator[][] memory) {
function getOperatorState(
IBLSRegistryCoordinatorWithIndices registryCoordinator,
bytes32 operatorId,
uint32 blockNumber
) external view returns (uint256, Operator[][] memory) {
bytes32[] memory operatorIds = new bytes32[](1);
operatorIds[0] = operatorId;
uint256 index = registryCoordinator.getQuorumBitmapIndicesByOperatorIdsAtBlockNumber(blockNumber, operatorIds)[0];
Expand All @@ -55,7 +59,11 @@ contract BLSOperatorStateRetriever {
* @param blockNumber is the block number to get the operator state for
* @return 2d array of operators. For each quorum, an ordered list of operators
*/
function getOperatorState(IBLSRegistryCoordinatorWithIndices registryCoordinator, bytes memory quorumNumbers, uint32 blockNumber) public view returns(Operator[][] memory) {
function getOperatorState(
IBLSRegistryCoordinatorWithIndices registryCoordinator,
bytes memory quorumNumbers,
uint32 blockNumber
) public view returns(Operator[][] memory) {
IStakeRegistry stakeRegistry = registryCoordinator.stakeRegistry();
IIndexRegistry indexRegistry = registryCoordinator.indexRegistry();

Expand Down
133 changes: 56 additions & 77 deletions src/BLSPubkeyRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ contract BLSPubkeyRegistry is BLSPubkeyRegistryStorage {
IBLSPublicKeyCompendium _pubkeyCompendium
) BLSPubkeyRegistryStorage(_registryCoordinator, _pubkeyCompendium) {}

/*******************************************************************************
EXTERNAL FUNCTIONS - REGISTRY COORDINATOR
*******************************************************************************/

/**
* @notice Registers the `operator`'s pubkey for the specified `quorumNumbers`.
* @param operator The address of the operator to register.
Expand All @@ -40,7 +44,6 @@ contract BLSPubkeyRegistry is BLSPubkeyRegistryStorage {
bytes memory quorumNumbers,
BN254.G1Point memory pubkey
) external onlyRegistryCoordinator returns (bytes32) {
_beforeRegisterOperator(operator, quorumNumbers);
//calculate hash of the operator's pubkey
bytes32 pubkeyHash = BN254.hashG1Point(pubkey);

Expand All @@ -53,7 +56,6 @@ contract BLSPubkeyRegistry is BLSPubkeyRegistryStorage {
// update each quorum's aggregate pubkey
_processQuorumApkUpdate(quorumNumbers, pubkey);

_afterRegisterOperator(operator, quorumNumbers);
// emit event so offchain actors can update their state
emit OperatorAddedToQuorums(operator, quorumNumbers);
return pubkeyHash;
Expand All @@ -78,7 +80,6 @@ contract BLSPubkeyRegistry is BLSPubkeyRegistryStorage {
bytes memory quorumNumbers,
BN254.G1Point memory pubkey
) external onlyRegistryCoordinator {
_beforeDeregisterOperator(operator, quorumNumbers);
bytes32 pubkeyHash = BN254.hashG1Point(pubkey);

require(
Expand All @@ -89,16 +90,65 @@ contract BLSPubkeyRegistry is BLSPubkeyRegistryStorage {
// update each quorum's aggregate pubkey
_processQuorumApkUpdate(quorumNumbers, pubkey.negate());

_afterDeregisterOperator(operator, quorumNumbers);

emit OperatorRemovedFromQuorums(operator, quorumNumbers);
}

/*******************************************************************************
INTERNAL FUNCTIONS
*******************************************************************************/

function _processQuorumApkUpdate(bytes memory quorumNumbers, BN254.G1Point memory point) internal {
BN254.G1Point memory apkAfterUpdate;

for (uint i = 0; i < quorumNumbers.length; ) {
uint8 quorumNumber = uint8(quorumNumbers[i]);

uint256 quorumApkUpdatesLength = quorumApkUpdates[quorumNumber].length;
if (quorumApkUpdatesLength > 0) {
// update nextUpdateBlockNumber of the current latest ApkUpdate
quorumApkUpdates[quorumNumber][quorumApkUpdatesLength - 1].nextUpdateBlockNumber = uint32(block.number);
}

apkAfterUpdate = quorumApk[quorumNumber].plus(point);

//update aggregate public key for this quorum
quorumApk[quorumNumber] = apkAfterUpdate;
//create new ApkUpdate to add to the mapping
ApkUpdate memory latestApkUpdate;
latestApkUpdate.apkHash = bytes24(BN254.hashG1Point(apkAfterUpdate));
latestApkUpdate.updateBlockNumber = uint32(block.number);
quorumApkUpdates[quorumNumber].push(latestApkUpdate);

unchecked {
++i;
}
}
}

function _validateApkHashForQuorumAtBlockNumber(ApkUpdate memory apkUpdate, uint32 blockNumber) internal pure {
require(
blockNumber >= apkUpdate.updateBlockNumber,
"BLSPubkeyRegistry._validateApkHashForQuorumAtBlockNumber: index too recent"
);
/**
* if there is a next update, check that the blockNumber is before the next update or if
* there is no next update, then apkUpdate.nextUpdateBlockNumber is 0.
*/
require(
apkUpdate.nextUpdateBlockNumber == 0 || blockNumber < apkUpdate.nextUpdateBlockNumber,
"BLSPubkeyRegistry._validateApkHashForQuorumAtBlockNumber: not latest apk update"
);
}

/*******************************************************************************
VIEW FUNCTIONS
*******************************************************************************/

/**
* @notice Returns the indices of the quorumApks index at `blockNumber` for the provided `quorumNumbers`
* @dev Returns the current indices if `blockNumber >= block.number`
*/
function getApkIndicesForQuorumsAtBlockNumber(
function getApkIndicesForQuorumsAtBlockNumber(
bytes calldata quorumNumbers,
uint256 blockNumber
) external view returns (uint32[] memory) {
Expand Down Expand Up @@ -159,75 +209,4 @@ contract BLSPubkeyRegistry is BLSPubkeyRegistryStorage {
function getOperatorFromPubkeyHash(bytes32 pubkeyHash) public view returns (address) {
return pubkeyCompendium.pubkeyHashToOperator(pubkeyHash);
}

function _processQuorumApkUpdate(bytes memory quorumNumbers, BN254.G1Point memory point) internal {
BN254.G1Point memory apkAfterUpdate;

for (uint i = 0; i < quorumNumbers.length; ) {
uint8 quorumNumber = uint8(quorumNumbers[i]);

uint256 quorumApkUpdatesLength = quorumApkUpdates[quorumNumber].length;
if (quorumApkUpdatesLength > 0) {
// update nextUpdateBlockNumber of the current latest ApkUpdate
quorumApkUpdates[quorumNumber][quorumApkUpdatesLength - 1].nextUpdateBlockNumber = uint32(block.number);
}

apkAfterUpdate = quorumApk[quorumNumber].plus(point);

//update aggregate public key for this quorum
quorumApk[quorumNumber] = apkAfterUpdate;
//create new ApkUpdate to add to the mapping
ApkUpdate memory latestApkUpdate;
latestApkUpdate.apkHash = bytes24(BN254.hashG1Point(apkAfterUpdate));
latestApkUpdate.updateBlockNumber = uint32(block.number);
quorumApkUpdates[quorumNumber].push(latestApkUpdate);

unchecked {
++i;
}
}
}

/**
* @dev Hook that is called before any operator registration to insert additional logic.
* @param operator The address of the operator to register.
* @param quorumNumbers The quorum numbers the operator is registering for, where each byte is an 8 bit integer quorumNumber.
*/
function _beforeRegisterOperator(address operator, bytes memory quorumNumbers) internal virtual {}

/**
* @dev Hook that is called after any operator registration to insert additional logic.
* @param operator The address of the operator to register.
* @param quorumNumbers The quorum numbers the operator is registering for, where each byte is an 8 bit integer quorumNumber.
*/
function _afterRegisterOperator(address operator, bytes memory quorumNumbers) internal virtual {}

/**
* @dev Hook that is called before any operator deregistration to insert additional logic.
* @param operator The address of the operator to deregister.
* @param quorumNumbers The quorum numbers the operator is registering for, where each byte is an 8 bit integer quorumNumber.
*/
function _beforeDeregisterOperator(address operator, bytes memory quorumNumbers) internal virtual {}

/**
* @dev Hook that is called after any operator deregistration to insert additional logic.
* @param operator The address of the operator to deregister.
* @param quorumNumbers The quorum numbers the operator is registering for, where each byte is an 8 bit integer quorumNumber.
*/
function _afterDeregisterOperator(address operator, bytes memory quorumNumbers) internal virtual {}

function _validateApkHashForQuorumAtBlockNumber(ApkUpdate memory apkUpdate, uint32 blockNumber) internal pure {
require(
blockNumber >= apkUpdate.updateBlockNumber,
"BLSPubkeyRegistry._validateApkHashForQuorumAtBlockNumber: index too recent"
);
/**
* if there is a next update, check that the blockNumber is before the next update or if
* there is no next update, then apkUpdate.nextUpdateBlockNumber is 0.
*/
require(
apkUpdate.nextUpdateBlockNumber == 0 || blockNumber < apkUpdate.nextUpdateBlockNumber,
"BLSPubkeyRegistry._validateApkHashForQuorumAtBlockNumber: not latest apk update"
);
}
}
14 changes: 13 additions & 1 deletion src/BLSPublicKeyCompendium.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@ contract BLSPublicKeyCompendium is IBLSPublicKeyCompendium {
/// @notice mapping from pubkey hash to operator address
mapping(bytes32 => address) public pubkeyHashToOperator;

/*******************************************************************************
EXTERNAL FUNCTIONS
*******************************************************************************/

/**
* @notice Called by an operator to register themselves as the owner of a BLS public key and reveal their G1 and G2 public key.
* @param signedMessageHash is the registration message hash signed by the private key of the operator
* @param pubkeyG1 is the corresponding G1 public key of the operator
* @param pubkeyG2 is the corresponding G2 public key of the operator
*/
function registerBLSPublicKey(BN254.G1Point memory signedMessageHash, BN254.G1Point memory pubkeyG1, BN254.G2Point memory pubkeyG2) external {
function registerBLSPublicKey(
BN254.G1Point memory signedMessageHash,
BN254.G1Point memory pubkeyG1,
BN254.G2Point memory pubkeyG2
) external {
bytes32 pubkeyHash = BN254.hashG1Point(pubkeyG1);
require(
operatorToPubkeyHash[msg.sender] == bytes32(0),
Expand Down Expand Up @@ -63,6 +71,10 @@ contract BLSPublicKeyCompendium is IBLSPublicKeyCompendium {
emit NewPubkeyRegistration(msg.sender, pubkeyG1, pubkeyG2);
}

/*******************************************************************************
VIEW FUNCTIONS
*******************************************************************************/

/**
* @notice Returns the message hash that an operator must sign to register their BLS public key.
* @param operator is the address of the operator registering their BLS public key
Expand Down
Loading

0 comments on commit 5b1b729

Please sign in to comment.