-
Notifications
You must be signed in to change notification settings - Fork 0
AddressRegistry: the write-once IAddressRegistryV1 concrete #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // SPDX-License-Identifier: LicenseRef-DCL-1.0 | ||
| // SPDX-FileCopyrightText: Copyright (c) 2020 Rain Open Source Software Ltd | ||
| pragma solidity =0.8.25; | ||
|
|
||
| import {IAddressRegistryV1} from "rain-deploy-0.1.6/src/interface/IAddressRegistryV1.sol"; | ||
|
|
||
| /// @dev PLACEHOLDER ROOT AUTHORITY. THIS IS NOT A REAL ROOT. | ||
| /// | ||
| /// The only account that may bind a name. It is a compile-time constant, not | ||
| /// storage, so it can never be rotated, and it is part of the creation code, so | ||
| /// changing it changes the deterministic deploy address and code hash of | ||
| /// `AddressRegistry` on every network. | ||
| /// | ||
| /// A human MUST replace this value with the intended root before any deploy-pin | ||
| /// snapshot is generated for this contract, and the pins in `rain-deploy`'s | ||
| /// `LibAddressRegistry` MUST be regenerated from the resulting creation code. | ||
| address constant ADDRESS_REGISTRY_ROOT = address(0xdeaDDeADDEaDdeaDdEAddEADDEAdDeadDEADDEaD); | ||
|
|
||
| /// @title AddressRegistry | ||
| /// @notice The whole of `IAddressRegistryV1`: an immutable root authority binds | ||
| /// a `bytes32` name that is unbound, anyone reads a name that is bound, and a | ||
| /// read of an unbound name reverts. | ||
| /// | ||
| /// There is deliberately nothing else. No rotation, no removal, no upgrade, no | ||
| /// pause, no admin surface, and no reader that returns the zero address for an | ||
| /// unbound name. Every one of those would turn a binding from a constant back | ||
| /// into a value that can move, which is the single property the registry | ||
| /// exists to provide. | ||
| /// | ||
| /// The storage mapping is `internal` rather than `public` for that reason: a | ||
| /// public mapping's generated getter answers an unbound name with the zero | ||
| /// address, which is exactly the silent failure `get` reverts to prevent. | ||
| contract AddressRegistry is IAddressRegistryV1 { | ||
| /// The bindings. Not `public`: the only reader is `get`, which reverts on an | ||
| /// unbound name. A name maps to the zero address if and only if it is | ||
| /// unbound, which is why `register` rejects the zero address. | ||
| mapping(bytes32 name => address account) internal sAddresses; | ||
|
|
||
| /// @inheritdoc IAddressRegistryV1 | ||
| function register(bytes32 name, address account) external { | ||
| if (msg.sender != ADDRESS_REGISTRY_ROOT) { | ||
| revert NotRoot(msg.sender); | ||
| } | ||
| if (account == address(0)) { | ||
| revert ZeroAccount(name); | ||
| } | ||
| address registered = sAddresses[name]; | ||
| // Write-once. Root has no more authority here than anyone else: an | ||
| // existing binding is never overwritten, not even with the same | ||
| // address. | ||
| if (registered != address(0)) { | ||
| revert NameAlreadyRegistered(name, registered); | ||
| } | ||
| sAddresses[name] = account; | ||
| emit Register(name, account); | ||
| } | ||
|
|
||
| /// @inheritdoc IAddressRegistryV1 | ||
| function get(bytes32 name) external view returns (address account) { | ||
| account = sAddresses[name]; | ||
| if (account == address(0)) { | ||
| revert NameNotRegistered(name); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // SPDX-License-Identifier: LicenseRef-DCL-1.0 | ||
| // SPDX-FileCopyrightText: Copyright (c) 2020 Rain Open Source Software Ltd | ||
| pragma solidity =0.8.25; | ||
|
|
||
| import {Test} from "forge-std-1.16.1/src/Test.sol"; | ||
|
|
||
| import {LibRainDeploy} from "rain-deploy-0.1.6/src/lib/LibRainDeploy.sol"; | ||
| import {LibAddressRegistry} from "rain-deploy-0.1.6/src/lib/LibAddressRegistry.sol"; | ||
| import {AddressRegistry} from "../../../src/concrete/AddressRegistry.sol"; | ||
|
|
||
| /// @title AddressRegistryDeployPinsTest | ||
| /// @notice `rain-deploy`'s `LibAddressRegistry` pins the deterministic address | ||
| /// and code hash of the `AddressRegistry` in this repo, and consumers resolve | ||
| /// names through those pins. The root authority is a constant in this contract's | ||
| /// creation code, so changing it moves both pins; this suite is what makes that | ||
| /// loud instead of silent. | ||
| contract AddressRegistryDeployPinsTest is Test { | ||
| /// The pins MUST be derivable from this source without deploying anything: | ||
| /// the Zoltu factory is `CREATE2` over its calldata with a zero salt, so the | ||
| /// address is a pure function of the creation code, and the code hash is | ||
| /// `keccak256` of the runtime code that creation code leaves behind. | ||
| function testAddressRegistryPinsDeriveFromThisSource() external pure { | ||
| assertEq(LibRainDeploy.zoltuAddress(type(AddressRegistry).creationCode), LibAddressRegistry.ADDRESS_REGISTRY); | ||
| assertEq(keccak256(type(AddressRegistry).runtimeCode), LibAddressRegistry.ADDRESS_REGISTRY_CODEHASH); | ||
| } | ||
|
|
||
| /// Actually deploying this contract's creation code through the Zoltu | ||
| /// factory MUST land at the pinned address with the pinned code hash, so the | ||
| /// derivation is checked against the factory rather than only against | ||
| /// itself. | ||
| function testAddressRegistryDeploysToPinnedAddress() external { | ||
| LibRainDeploy.etchZoltuFactory(vm); | ||
|
|
||
| address deployed = LibRainDeploy.deployZoltu(type(AddressRegistry).creationCode); | ||
|
|
||
| assertEq(deployed, LibAddressRegistry.ADDRESS_REGISTRY); | ||
| assertEq(deployed.codehash, LibAddressRegistry.ADDRESS_REGISTRY_CODEHASH); | ||
| assertEq(keccak256(deployed.code), LibAddressRegistry.ADDRESS_REGISTRY_CODEHASH); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // SPDX-License-Identifier: LicenseRef-DCL-1.0 | ||
| // SPDX-FileCopyrightText: Copyright (c) 2020 Rain Open Source Software Ltd | ||
| pragma solidity =0.8.25; | ||
|
|
||
| import {Test} from "forge-std-1.16.1/src/Test.sol"; | ||
|
|
||
| import {IAddressRegistryV1} from "rain-deploy-0.1.6/src/interface/IAddressRegistryV1.sol"; | ||
| import {AddressRegistry, ADDRESS_REGISTRY_ROOT} from "../../../src/concrete/AddressRegistry.sol"; | ||
|
|
||
| /// @title AddressRegistryGetTest | ||
| /// @notice A test suite for `AddressRegistry.get`: it answers a bound name with | ||
| /// its address and an unbound name with a revert, and it is the only reader. | ||
| contract AddressRegistryGetTest is Test { | ||
| /// The registry under test. Stateful, so a fresh one per test. | ||
| AddressRegistry internal sRegistry; | ||
|
|
||
| function setUp() external { | ||
| sRegistry = new AddressRegistry(); | ||
| } | ||
|
|
||
| /// A read of an unbound name reverts rather than returning the zero | ||
| /// address, so a caller cannot proceed on a name nobody bound by forgetting | ||
| /// to check. | ||
| function testGetUnsetReverts(bytes32 name) external { | ||
| vm.expectRevert(abi.encodeWithSelector(IAddressRegistryV1.NameNotRegistered.selector, name)); | ||
| sRegistry.get(name); | ||
| } | ||
|
|
||
| /// A read of a bound name returns exactly what was bound, and reading does | ||
| /// not consume or alter the binding. | ||
| function testGetReturnsRegistered(bytes32 name, address account) external { | ||
| vm.assume(account != address(0)); | ||
|
|
||
| vm.prank(ADDRESS_REGISTRY_ROOT); | ||
| sRegistry.register(name, account); | ||
|
|
||
| assertEq(sRegistry.get(name), account); | ||
| assertEq(sRegistry.get(name), account); | ||
| } | ||
|
|
||
| /// Names are opaque: nothing about a name's bytes changes how it is stored | ||
| /// or read, including names a string-hashing convention would never | ||
| /// produce. | ||
| function testGetOpaqueNames(address account) external { | ||
| vm.assume(account != address(0)); | ||
|
|
||
| bytes32[3] memory names = [bytes32(0), bytes32(uint256(1)), bytes32(type(uint256).max)]; | ||
| for (uint256 i = 0; i < names.length; i++) { | ||
| AddressRegistry registry = new AddressRegistry(); | ||
| vm.prank(ADDRESS_REGISTRY_ROOT); | ||
| registry.register(names[i], account); | ||
| assertEq(registry.get(names[i]), account); | ||
| } | ||
| } | ||
|
|
||
| /// `get` is the only reader. The bindings mapping is not `public`, so the | ||
| /// getter a `public` mapping would generate — which answers an unbound name | ||
| /// with the zero address, the exact silent failure `get` reverts to prevent | ||
| /// — does not exist. | ||
| function testGetNoGeneratedMappingGetter(bytes32 name) external { | ||
| (bool success,) = address(sRegistry).call(abi.encodeWithSignature("sAddresses(bytes32)", name)); | ||
| assertFalse(success); | ||
| } | ||
|
|
||
| /// There is no other entry point at all: no fallback, no receive, and | ||
| /// nothing beyond the two `IAddressRegistryV1` functions, so an unknown | ||
| /// selector reverts instead of being silently absorbed. | ||
| function testGetNoOtherEntryPoint(bytes4 selector, bytes32 name) external { | ||
| vm.assume(selector != IAddressRegistryV1.get.selector); | ||
| vm.assume(selector != IAddressRegistryV1.register.selector); | ||
|
|
||
| (bool success,) = address(sRegistry).call(abi.encodeWithSelector(selector, name, address(this))); | ||
| assertFalse(success); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.