Skip to content

Commit

Permalink
feat(KeyRegistry): add settable IdRegistry (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
horsefacts authored Jul 30, 2023
1 parent 042fde2 commit 5d62f7a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BundleRegistryGasUsageTest:testGasRegisterWithSig() (gas: 826205)
BundleRegistryGasUsageTest:testGasTrustedBatchRegister() (gas: 6732822)
BundleRegistryGasUsageTest:testGasTrustedRegister() (gas: 849451)
BundleRegistryGasUsageTest:testGasTrustedBatchRegister() (gas: 6744222)
BundleRegistryGasUsageTest:testGasTrustedRegister() (gas: 852391)
IdRegistryGasUsageTest:testGasRegister() (gas: 734576)
IdRegistryGasUsageTest:testGasRegisterForAndRecover() (gas: 1702036)
IdRegistryGasUsageTest:testGasRegisterFromTrustedCaller() (gas: 808056)
Expand Down
40 changes: 33 additions & 7 deletions src/KeyRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import {IdRegistry} from "./IdRegistry.sol";
import {Signatures} from "./lib/Signatures.sol";
import {TrustedCaller} from "./lib/TrustedCaller.sol";

interface IdRegistryLike {
function idOf(address owner) external view returns (uint256);
}

contract KeyRegistry is TrustedCaller, Signatures, EIP712, Nonces {
/**
* @notice State enumeration for a key in the registry. During migration, an admin can change
Expand Down Expand Up @@ -137,6 +141,14 @@ contract KeyRegistry is TrustedCaller, Signatures, EIP712, Nonces {
*/
event Migrated(uint256 indexed keysMigratedAt);

/**
* @dev Emit an event when the admin sets a new IdRegistry contract address.
*
* @param oldIdRegistry The previous IdRegistry address.
* @param newIdRegistry The ne IdRegistry address.
*/
event SetIdRegistry(address oldIdRegistry, address newIdRegistry);

/*//////////////////////////////////////////////////////////////
CONSTANTS
//////////////////////////////////////////////////////////////*/
Expand All @@ -151,11 +163,6 @@ contract KeyRegistry is TrustedCaller, Signatures, EIP712, Nonces {
IMMUTABLES
//////////////////////////////////////////////////////////////*/

/**
* @dev The IdRegistry contract.
*/
IdRegistry public immutable idRegistry;

/**
* @dev Period in seconds after migration during which admin can bulk add/reset keys.
* Admins can make corrections to the migrated data during the grace period if necessary,
Expand All @@ -167,6 +174,11 @@ contract KeyRegistry is TrustedCaller, Signatures, EIP712, Nonces {
STORAGE
//////////////////////////////////////////////////////////////*/

/**
* @dev The IdRegistry contract.
*/
IdRegistryLike public idRegistry;

/**
* @dev Timestamp at which keys migrated. Hubs will cut over to use this key registry as their
* source of truth after this timestamp.
Expand All @@ -190,7 +202,7 @@ contract KeyRegistry is TrustedCaller, Signatures, EIP712, Nonces {
/**
* @notice Set the IdRegistry, migration grace period, and owner.
*
* @param _idRegistry IdRegistry contract address. Immutable.
* @param _idRegistry IdRegistry contract address.
* @param _gracePeriod Migration grace period in seconds. Immutable.
* @param _owner Contract owner address.
*/
Expand All @@ -200,7 +212,7 @@ contract KeyRegistry is TrustedCaller, Signatures, EIP712, Nonces {
address _owner
) TrustedCaller(_owner) EIP712("Farcaster KeyRegistry", "1") {
gracePeriod = _gracePeriod;
idRegistry = IdRegistry(_idRegistry);
idRegistry = IdRegistryLike(_idRegistry);
}

/*//////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -362,6 +374,20 @@ contract KeyRegistry is TrustedCaller, Signatures, EIP712, Nonces {
}
}

/*//////////////////////////////////////////////////////////////
ADMIN
//////////////////////////////////////////////////////////////*/

/**
* @notice Set the IdRegistry contract address. Only callable by owner.
*
* @param _idRegistry The new IdRegistry address.
*/
function setIdRegistry(address _idRegistry) external onlyOwner {
emit SetIdRegistry(address(idRegistry), _idRegistry);
idRegistry = IdRegistryLike(_idRegistry);
}

/*//////////////////////////////////////////////////////////////
HELPERS
//////////////////////////////////////////////////////////////*/
Expand Down
25 changes: 25 additions & 0 deletions test/KeyRegistry/KeyRegistry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ contract KeyRegistryTest is KeyRegistryTestSuite {
event Remove(uint256 indexed fid, bytes indexed key, bytes keyBytes);
event AdminReset(uint256 indexed fid, bytes indexed key, bytes keyBytes);
event Migrated(uint256 indexed keysMigratedAt);
event SetIdRegistry(address oldIdRegistry, address newIdRegistry);

function testInitialIdRegistry() public {
assertEq(address(keyRegistry.idRegistry()), address(idRegistry));
Expand Down Expand Up @@ -854,6 +855,30 @@ contract KeyRegistryTest is KeyRegistryTestSuite {
vm.stopPrank();
}

/*//////////////////////////////////////////////////////////////
SET IDREGISTRY
//////////////////////////////////////////////////////////////*/

function testFuzzOnlyAdminCanSetIdRegistry(address caller, address idRegistry) public {
vm.assume(caller != owner);

vm.prank(caller);
vm.expectRevert("Ownable: caller is not the owner");
keyRegistry.setIdRegistry(idRegistry);
}

function testFuzzSetIdRegistry(address idRegistry) public {
address currentIdRegistry = address(keyRegistry.idRegistry());

vm.expectEmit(false, false, false, true);
emit SetIdRegistry(currentIdRegistry, idRegistry);

vm.prank(owner);
keyRegistry.setIdRegistry(idRegistry);

assertEq(address(keyRegistry.idRegistry()), idRegistry);
}

/*//////////////////////////////////////////////////////////////
HELPERS
//////////////////////////////////////////////////////////////*/
Expand Down

0 comments on commit 5d62f7a

Please sign in to comment.