Skip to content

Commit

Permalink
Merge branch 'master' into feat/add-back-avs-deploy-scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
stevennevins authored Oct 18, 2023
2 parents d571dd8 + cf71ce1 commit a32545c
Show file tree
Hide file tree
Showing 11 changed files with 965 additions and 801 deletions.
98 changes: 98 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
Business Source License 1.1

License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved.
"Business Source License" is a trademark of MariaDB Corporation Ab.

-----------------------------------------------------------------------------

Parameters

Licensor: Layr Labs, Inc.

Licensed Work: EigenLayer Middleware Contracts
The Licensed Work is (c) 2023 Layr Labs, Inc.

Additional Use Grant: None.

Change Date: 2025-05-01 (May 1st, 2025)

Change License: MIT

-----------------------------------------------------------------------------

Terms

The Licensor hereby grants you the right to copy, modify, create derivative
works, redistribute, and make non-production use of the Licensed Work. The
Licensor may make an Additional Use Grant, above, permitting limited
production use.

Effective on the Change Date, or the fourth anniversary of the first publicly
available distribution of a specific version of the Licensed Work under this
License, whichever comes first, the Licensor hereby grants you rights under
the terms of the Change License, and the rights granted in the paragraph
above terminate.

If your use of the Licensed Work does not comply with the requirements
currently in effect as described in this License, you must purchase a
commercial license from the Licensor, its affiliated entities, or authorized
resellers, or you must refrain from using the Licensed Work.

All copies of the original and modified Licensed Work, and derivative works
of the Licensed Work, are subject to this License. This License applies
separately for each version of the Licensed Work and the Change Date may vary
for each version of the Licensed Work released by Licensor.

You must conspicuously display this License on each original or modified copy
of the Licensed Work. If you receive the Licensed Work in original or
modified form from a third party, the terms and conditions set forth in this
License apply to your use of that work.

Any use of the Licensed Work in violation of this License will automatically
terminate your rights under this License for the current and all other
versions of the Licensed Work.

This License does not grant you any right in any trademark or logo of
Licensor or its affiliates (provided that you may use a trademark or logo of
Licensor as expressly required by this License).

TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
TITLE.

MariaDB hereby grants you permission to use this License’s text to license
your works, and to refer to it using the trademark "Business Source License",
as long as you comply with the Covenants of Licensor below.

-----------------------------------------------------------------------------

Covenants of Licensor

In consideration of the right to use this License’s text and the "Business
Source License" name and trademark, Licensor covenants to MariaDB, and to all
other recipients of the licensed work to be provided by Licensor:

1. To specify as the Change License the GPL Version 2.0 or any later version,
or a license that is compatible with GPL Version 2.0 or a later version,
where "compatible" means that software provided under the Change License can
be included in a program with software provided under GPL Version 2.0 or a
later version. Licensor may specify additional Change Licenses without
limitation.

2. To either: (a) specify an additional grant of rights to use that does not
impose any additional restriction on the right granted in this License, as
the Additional Use Grant; or (b) insert the text "None".

3. To specify a Change Date.

4. Not to modify this License in any other way.

-----------------------------------------------------------------------------

Notice

The Business Source License (this document, or the "License") is not an Open
Source license. However, the Licensed Work will eventually be made available
under an Open Source License, as stated in this License.
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 a32545c

Please sign in to comment.