Skip to content

Commit

Permalink
Add upgradability to IndexRegistry and PubkeyRegistry (#216)
Browse files Browse the repository at this point in the history
* init

* fix storage gap
  • Loading branch information
0x0aa0 authored Oct 4, 2023
1 parent 0d3f335 commit 6310f6e
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 49 deletions.
29 changes: 8 additions & 21 deletions src/contracts/middleware/BLSPubkeyRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.12;

import "../interfaces/IBLSPubkeyRegistry.sol";
import "../interfaces/IRegistryCoordinator.sol";
import "../interfaces/IBLSPublicKeyCompendium.sol";

import "./BLSPubkeyRegistryStorage.sol";
import "../libraries/BN254.sol";

contract BLSPubkeyRegistry is IBLSPubkeyRegistry {
contract BLSPubkeyRegistry is BLSPubkeyRegistryStorage {
using BN254 for BN254.G1Point;

/// @notice the hash of the zero pubkey aka BN254.G1Point(0,0)
bytes32 internal constant ZERO_PK_HASH = hex"ad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5";
/// @notice the registry coordinator contract
IRegistryCoordinator public immutable registryCoordinator;
/// @notice the BLSPublicKeyCompendium contract against which pubkey ownership is checked
IBLSPublicKeyCompendium public immutable pubkeyCompendium;

// mapping of quorumNumber => ApkUpdate[], tracking the aggregate pubkey updates of every quorum
mapping(uint8 => ApkUpdate[]) public quorumApkUpdates;
// mapping of quorumNumber => current aggregate pubkey of quorum
mapping(uint8 => BN254.G1Point) private quorumApk;

/// @notice when applied to a function, only allows the RegistryCoordinator to call it
modifier onlyRegistryCoordinator() {
require(
msg.sender == address(registryCoordinator),
Expand All @@ -30,10 +16,11 @@ contract BLSPubkeyRegistry is IBLSPubkeyRegistry {
_;
}

constructor(IRegistryCoordinator _registryCoordinator, IBLSPublicKeyCompendium _pubkeyCompendium) {
registryCoordinator = _registryCoordinator;
pubkeyCompendium = _pubkeyCompendium;
}
/// @notice Sets the (immutable) `registryCoordinator` and `pubkeyCompendium` addresses
constructor(
IRegistryCoordinator _registryCoordinator,
IBLSPublicKeyCompendium _pubkeyCompendium
) BLSPubkeyRegistryStorage(_registryCoordinator, _pubkeyCompendium) {}

/**
* @notice Registers the `operator`'s pubkey for the specified `quorumNumbers`.
Expand Down
32 changes: 32 additions & 0 deletions src/contracts/middleware/BLSPubkeyRegistryStorage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.12;

import "../interfaces/IBLSPubkeyRegistry.sol";
import "../interfaces/IRegistryCoordinator.sol";
import "../interfaces/IBLSPublicKeyCompendium.sol";

import "@openzeppelin-upgrades/contracts/proxy/utils/Initializable.sol";

abstract contract BLSPubkeyRegistryStorage is Initializable, IBLSPubkeyRegistry {
/// @notice the hash of the zero pubkey aka BN254.G1Point(0,0)
bytes32 internal constant ZERO_PK_HASH = hex"ad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5";
/// @notice the registry coordinator contract
IRegistryCoordinator public immutable registryCoordinator;
/// @notice the BLSPublicKeyCompendium contract against which pubkey ownership is checked
IBLSPublicKeyCompendium public immutable pubkeyCompendium;

/// @notice mapping of quorumNumber => ApkUpdate[], tracking the aggregate pubkey updates of every quorum
mapping(uint8 => ApkUpdate[]) public quorumApkUpdates;
/// @notice mapping of quorumNumber => current aggregate pubkey of quorum
mapping(uint8 => BN254.G1Point) public quorumApk;

constructor(IRegistryCoordinator _registryCoordinator, IBLSPublicKeyCompendium _pubkeyCompendium) {
registryCoordinator = _registryCoordinator;
pubkeyCompendium = _pubkeyCompendium;
// disable initializers so that the implementation contract cannot be initialized
_disableInitializers();
}

// storage gap for upgradeability
uint256[48] private __GAP;
}
25 changes: 5 additions & 20 deletions src/contracts/middleware/IndexRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,36 +1,21 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.12;


import "../interfaces/IIndexRegistry.sol";
import "../interfaces/IRegistryCoordinator.sol";
import "./IndexRegistryStorage.sol";
import "../libraries/BN254.sol";

contract IndexRegistry is IIndexRegistry {

/// @notice The value that indices of deregistered operators are set to
uint32 public constant OPERATOR_DEREGISTERED_INDEX = type(uint32).max;

IRegistryCoordinator public immutable registryCoordinator;

// list of all operators ever registered, may include duplicates. used to avoid running an indexer on nodes
bytes32[] public globalOperatorList;

// mapping of operatorId => quorumNumber => index history of that operator
mapping(bytes32 => mapping(uint8 => OperatorIndexUpdate[])) internal _operatorIdToIndexHistory;
// mapping of quorumNumber => history of numbers of unique registered operators
mapping(uint8 => OperatorIndexUpdate[]) internal _totalOperatorsHistory;
contract IndexRegistry is IndexRegistryStorage {

/// @notice when applied to a function, only allows the RegistryCoordinator to call it
modifier onlyRegistryCoordinator() {
require(msg.sender == address(registryCoordinator), "IndexRegistry.onlyRegistryCoordinator: caller is not the registry coordinator");
_;
}

/// @notice sets the (immutable) `registryCoordinator` address
constructor(
IRegistryCoordinator _registryCoordinator
){
registryCoordinator = _registryCoordinator;
}
) IndexRegistryStorage(_registryCoordinator) {}

/**
* @notice Registers the operator with the specified `operatorId` for the quorums specified by `quorumNumbers`.
Expand Down
35 changes: 35 additions & 0 deletions src/contracts/middleware/IndexRegistryStorage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.12;

import "../interfaces/IIndexRegistry.sol";
import "../interfaces/IRegistryCoordinator.sol";

import "@openzeppelin-upgrades/contracts/proxy/utils/Initializable.sol";

abstract contract IndexRegistryStorage is Initializable, IIndexRegistry {

/// @notice The value that indices of deregistered operators are set to
uint32 public constant OPERATOR_DEREGISTERED_INDEX = type(uint32).max;

/// @notice The RegistryCoordinator contract for this middleware
IRegistryCoordinator public immutable registryCoordinator;

/// @notice list of all operators ever registered, may include duplicates. used to avoid running an indexer on nodes
bytes32[] public globalOperatorList;

/// @notice mapping of operatorId => quorumNumber => index history of that operator
mapping(bytes32 => mapping(uint8 => OperatorIndexUpdate[])) internal _operatorIdToIndexHistory;
/// @notice mapping of quorumNumber => history of numbers of unique registered operators
mapping(uint8 => OperatorIndexUpdate[]) internal _totalOperatorsHistory;

constructor(
IRegistryCoordinator _registryCoordinator
){
registryCoordinator = _registryCoordinator;
// disable initializers so that the implementation contract cannot be initialized
_disableInitializers();
}

// storage gap for upgradeability
uint256[47] private __GAP;
}
2 changes: 1 addition & 1 deletion src/contracts/middleware/StakeRegistryStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ abstract contract StakeRegistryStorage is VoteWeigherBase, IStakeRegistry {
}

// storage gap for upgradeability
uint256[63] private __GAP;
uint256[65] private __GAP;

Check failure

Code scanning / Slither

State variable shadowing High

}
6 changes: 1 addition & 5 deletions src/contracts/middleware/VoteWeigherBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ contract VoteWeigherBase is VoteWeigherBaseStorage {
constructor(
IStrategyManager _strategyManager,
IServiceManager _serviceManager
) VoteWeigherBaseStorage(_strategyManager, _serviceManager)
// solhint-disable-next-line no-empty-blocks
{

}
) VoteWeigherBaseStorage(_strategyManager, _serviceManager) {}

/// @notice Returns the strategy and weight multiplier for the `index`'th strategy in the quorum `quorumNumber`
function strategyAndWeightingMultiplierForQuorumByIndex(uint8 quorumNumber, uint256 index)
Expand Down
3 changes: 1 addition & 2 deletions src/contracts/middleware/VoteWeigherBaseStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ abstract contract VoteWeigherBaseStorage is Initializable, IVoteWeigher {
IStrategyManager _strategyManager,
IServiceManager _serviceManager
) {
// sanity check that the VoteWeigher is being initialized with at least 1 quorum
strategyManager = _strategyManager;
delegation = _strategyManager.delegation();
slasher = _strategyManager.slasher();
Expand All @@ -63,5 +62,5 @@ abstract contract VoteWeigherBaseStorage is Initializable, IVoteWeigher {
}

// storage gap for upgradeability
uint256[47] private __GAP;
uint256[48] private __GAP;
}

0 comments on commit 6310f6e

Please sign in to comment.