-
Notifications
You must be signed in to change notification settings - Fork 6
Simple ImmutableBeaconProxy implementation #19
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
Open
kyzia551
wants to merge
5
commits into
main
Choose a base branch
from
feat/immutabe-beacon-proxy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
88a48e3
simple ImmutableBeaconProxy implementation
kyzia551 e5c6339
Update src/contracts/transparent-proxy/ImmutableBeaconProxy.sol
kyzia551 895d4f1
update error messages
kyzia551 e343ae3
event added to ImmutableBeaconProfy, small refactoring
kyzia551 b1e1402
typo in IBeacon import
kyzia551 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
36 changes: 36 additions & 0 deletions
36
src/contracts/immutable-beacon-proxy/ImmutableBeaconProxy.sol
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,36 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.0; | ||
|
|
||
| import {IBeacon} from './interfaces/IBeacon.sol'; | ||
| import {Proxy} from '../oz-common/Proxy.sol'; | ||
| import {Address} from '../oz-common/Address.sol'; | ||
|
|
||
| /** | ||
| * @dev This contract implements a proxy that gets the implementation address for each call from an {UpgradeableBeacon}. | ||
| * The beacon address is immutable. The purpose of it, is to be able to access this proxy via delegatecall | ||
|
|
||
| * !!! IMPORTANT CONSIDERATION !!! | ||
| * We expect that the implementation will not have any storage associated, | ||
| * because it when accessed via delegatecall, will not work as expected creating dangerous side-effects. Preferable, the implementation should be declared as a library | ||
| */ | ||
| contract ImmutableBeaconProxy is Proxy { | ||
| address internal immutable _beacon; | ||
|
|
||
| event ImmutableBeaconSet(address indexed beacon); | ||
|
|
||
| constructor(address beacon) { | ||
| require(Address.isContract(beacon), 'INVALID_BEACON'); | ||
| require(Address.isContract(IBeacon(beacon).implementation()), 'INVALID_IMPLEMENTATION'); | ||
|
|
||
| // there is no initialization call, because we expect that implementation should have no storage | ||
| _beacon = beacon; | ||
| emit ImmutableBeaconSet(beacon); | ||
| } | ||
|
|
||
| /** | ||
| * @dev Returns the current implementation address of the associated beacon. | ||
| */ | ||
| function _implementation() internal view virtual override returns (address) { | ||
| return IBeacon(_beacon).implementation(); | ||
| } | ||
| } |
68 changes: 68 additions & 0 deletions
68
src/contracts/immutable-beacon-proxy/UpgradeableBeacon.sol
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,68 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| // OpenZeppelin Contracts v4.4.1 (proxy/beacon/UpgradeableBeacon.sol) | ||
|
|
||
| pragma solidity ^0.8.0; | ||
|
|
||
| import './interfaces/IBeacon.sol'; | ||
| import {Ownable} from '../oz-common/Ownable.sol'; | ||
| import {Address} from '../oz-common/Address.sol'; | ||
|
|
||
| /** | ||
| * @dev This contract is used in conjunction with one or more instances of {BeaconProxy} to determine their | ||
| * implementation contract, which is where they will delegate all function calls. | ||
| * | ||
| * An owner is able to change the implementation the beacon points to, thus upgrading the proxies that use this beacon. | ||
| */ | ||
| contract UpgradeableBeacon is IBeacon, Ownable { | ||
| address private _implementation; | ||
|
|
||
| /** | ||
| * @dev Emitted when the implementation returned by the beacon is changed. | ||
| */ | ||
| event Upgraded(address indexed implementation); | ||
|
|
||
| /** | ||
| * @dev Sets the address of the initial implementation, and the deployer account as the owner who can upgrade the | ||
| * beacon. | ||
| */ | ||
| constructor(address implementation_) { | ||
| _setImplementation(implementation_); | ||
| } | ||
|
|
||
| /** | ||
| * @dev Returns the current implementation address. | ||
| */ | ||
| function implementation() public view virtual override returns (address) { | ||
| return _implementation; | ||
| } | ||
|
|
||
| /** | ||
| * @dev Upgrades the beacon to a new implementation. | ||
| * | ||
| * Emits an {Upgraded} event. | ||
| * | ||
| * Requirements: | ||
| * | ||
| * - msg.sender must be the owner of the contract. | ||
| * - `newImplementation` must be a contract. | ||
| */ | ||
| function upgradeTo(address newImplementation) public virtual onlyOwner { | ||
| _setImplementation(newImplementation); | ||
| emit Upgraded(newImplementation); | ||
| } | ||
|
|
||
| /** | ||
| * @dev Sets the implementation contract address for this beacon | ||
| * | ||
| * Requirements: | ||
| * | ||
| * - `newImplementation` must be a contract. | ||
| */ | ||
| function _setImplementation(address newImplementation) private { | ||
| require( | ||
| Address.isContract(newImplementation), | ||
| 'UpgradeableBeacon: implementation is not a contract' | ||
| ); | ||
| _implementation = newImplementation; | ||
| } | ||
| } | ||
16 changes: 16 additions & 0 deletions
16
src/contracts/immutable-beacon-proxy/interfaces/IBeacon.sol
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,16 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| // OpenZeppelin Contracts v4.4.1 (proxy/beacon/IBeacon.sol) | ||
|
|
||
| pragma solidity ^0.8.0; | ||
|
|
||
| /** | ||
| * @dev This is the interface that {BeaconProxy} expects of its beacon. | ||
| */ | ||
| interface IBeacon { | ||
| /** | ||
| * @dev Must return an address that can be used as a delegate call target. | ||
| * | ||
| * {BeaconProxy} will check that this address is a contract. | ||
| */ | ||
| function implementation() external view returns (address); | ||
| } |
File renamed without changes.
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,47 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.0; | ||
| import 'forge-std/Test.sol'; | ||
| import {ImmutableBeaconProxy, IBeacon} from '../src/contracts/immutable-beacon-proxy/ImmutableBeaconProxy.sol'; | ||
|
|
||
| contract ImplementationMock {} | ||
|
|
||
| contract BeaconMock is IBeacon { | ||
| address public implementation; | ||
|
|
||
| constructor(address newImplementation) { | ||
| implementation = newImplementation; | ||
| } | ||
| } | ||
|
|
||
| contract ImmutableBeaconProxyMock is ImmutableBeaconProxy { | ||
| constructor(address beacon) ImmutableBeaconProxy(beacon) {} | ||
|
|
||
| function implementation() public view returns (address) { | ||
| return _implementation(); | ||
| } | ||
| } | ||
|
|
||
| contract ImmutableBeaconProxyTest is Test { | ||
| event ImmutableBeaconSet(address indexed beacon); | ||
|
|
||
| function testResolvesImplementationCorrectly() public { | ||
| address implementation = address(new ImplementationMock()); | ||
| address beacon = address(new BeaconMock(implementation)); | ||
|
|
||
| vm.expectEmit(true, false, false, true); | ||
| emit ImmutableBeaconSet(beacon); | ||
| assertEq(implementation, (new ImmutableBeaconProxyMock(beacon)).implementation()); | ||
| } | ||
|
|
||
| function testBeaconNotAContract() public { | ||
| vm.expectRevert(bytes('INVALID_BEACON')); | ||
| new ImmutableBeaconProxy(address(1)); | ||
| } | ||
|
|
||
| function testImplementationNotAContract() public { | ||
| address beacon = address(new BeaconMock(address(1))); | ||
|
|
||
| vm.expectRevert(bytes('INVALID_IMPLEMENTATION')); | ||
| new ImmutableBeaconProxy(beacon); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this import this way?