From d1b2978bffe3677dd1b3fc4bc122067bc0e2f31f Mon Sep 17 00:00:00 2001 From: Vectorized Date: Mon, 19 Aug 2024 05:07:28 +0000 Subject: [PATCH 01/18] T --- ERCS/erc-9999.md | 119 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 ERCS/erc-9999.md diff --git a/ERCS/erc-9999.md b/ERCS/erc-9999.md new file mode 100644 index 0000000000..4ba4750cdd --- /dev/null +++ b/ERCS/erc-9999.md @@ -0,0 +1,119 @@ +--- +eip: 9999 +title: Minimal Upgradeable Proxies +author: Atarpara (@Atarpara), JT Riley (@jtriley-eth), Vectorized (@Vectorized) +discussions-to: https://ethereum-magicians.org/t/minimal-upgrad +status: Draft +type: Standards Track +category: ERC +created: 2024-08-19 +requires: 1967 +--- + +## Abstract + +This standard defines minimal [ERC-1967](./eip-1967.md) proxies for three patterns: (1) transparent, (2) UUPS, (3) beacon. The proxies support optional immutable arguments which are appended to the end of their runtime bytecode. Additional variants which support onchain implementation querying are provided. + +## Motivation + +Having standardized minimal bytecode for upgradeable proxies enables the following: + +1. Automatic verification on block explorers. +2. Ability for immutable arguments to be queried onchain, as these arguments are stored in the same bytecode offset, +3. Ability for the implementation to be queried and verified onchain. + +The minimal nature of the proxies enables cheaper deployment and runtime costs. + +## Specification + +The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119 and RFC 8174. + +### Minimal ERC-1967 transparent upgradeable proxy + + + + + +#### Minimal ERC-1967 transparent upgradeable proxy (I-variant) + + + + + +### Minimal ERC-1967 UUPS proxy + + + +#### Minimal ERC-1967 UUPS proxy (I-variant) + + + +### Minimal ERC-1967 beacon proxy + + + +#### Minimal ERC-1967 beacon proxy (I-variant) + + + +## Rationale + +### Optimization priorities + + + + +### Immutable admin in transparent upgradeable proxy + + + + +### I-variants + + + +### Omission of events in bytecode + + + +### Immutable arguments are not appended to forwarded calldata + + + +## Backwards Compatibility + +No backward compatibility issues found. + +## Reference Implementation + +### Minimal ERC-1967 transparent upgradeable proxy implementation + + + +#### Minimal ERC-1967 transparent upgradeable proxy implementation (I-variant) + + + +### Minimal ERC-1967 UUPS proxy implementation + + + +#### Minimal ERC-1967 UUPS proxy implementation (I-variant) + + + +### Minimal ERC-1967 beacon proxy implementation + + + +#### Minimal ERC-1967 beacon proxy implementation (I-variant) + + + +## Security Considerations + + + +## Copyright + +Copyright and related rights waived via [CC0](../LICENSE.md). From 9816395389c234cb02157867209ac0e5bb1d5384 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Mon, 19 Aug 2024 05:08:39 +0000 Subject: [PATCH 02/18] T --- ERCS/erc-9999.md | 1 + 1 file changed, 1 insertion(+) diff --git a/ERCS/erc-9999.md b/ERCS/erc-9999.md index 4ba4750cdd..939778d3b2 100644 --- a/ERCS/erc-9999.md +++ b/ERCS/erc-9999.md @@ -79,6 +79,7 @@ The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "S ### Immutable arguments are not appended to forwarded calldata + ## Backwards Compatibility From e4d6588538609337644257ea295fcf4e3796e1d5 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 23 Aug 2024 06:26:20 +0000 Subject: [PATCH 03/18] Edit ERC --- ERCS/erc-9999.md | 338 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 307 insertions(+), 31 deletions(-) diff --git a/ERCS/erc-9999.md b/ERCS/erc-9999.md index 939778d3b2..5b5794663d 100644 --- a/ERCS/erc-9999.md +++ b/ERCS/erc-9999.md @@ -1,8 +1,8 @@ --- eip: 9999 title: Minimal Upgradeable Proxies -author: Atarpara (@Atarpara), JT Riley (@jtriley-eth), Vectorized (@Vectorized) -discussions-to: https://ethereum-magicians.org/t/minimal-upgrad +author: Atarpara (@Atarpara), JT Riley (@jtriley-eth), xiaobaiskill (@xiaobaiskill), Vectorized (@Vectorized) +discussions-to: https://ethereum-magicians.org/t/minimal-upgradeable-proxies status: Draft type: Standards Track category: ERC @@ -12,14 +12,14 @@ requires: 1967 ## Abstract -This standard defines minimal [ERC-1967](./eip-1967.md) proxies for three patterns: (1) transparent, (2) UUPS, (3) beacon. The proxies support optional immutable arguments which are appended to the end of their runtime bytecode. Additional variants which support onchain implementation querying are provided. +This standard defines minimal [ERC-1967](./eip-1967.md) proxies for three patterns: (1) transparent upgradeable proxy, (2) UUPS proxy, (3) beacon proxy. The proxies support optional immutable arguments which are appended to the end of their runtime bytecode. Additional variants which support onchain implementation querying are provided. ## Motivation Having standardized minimal bytecode for upgradeable proxies enables the following: 1. Automatic verification on block explorers. -2. Ability for immutable arguments to be queried onchain, as these arguments are stored in the same bytecode offset, +2. Ability for immutable arguments to be queried onchain, as these arguments are stored at the same bytecode offset, 3. Ability for the implementation to be queried and verified onchain. The minimal nature of the proxies enables cheaper deployment and runtime costs. @@ -28,58 +28,155 @@ The minimal nature of the proxies enables cheaper deployment and runtime costs. The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119 and RFC 8174. +### Overview + +All of the following proxies MAY have optional data bytecode appended to the end of their runtime bytecode. + ### Minimal ERC-1967 transparent upgradeable proxy - +This is the runtime bytecode: + +``` +3d3d336d________________________________________14605157363d3d37363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e604c573d6000fd5b3d6000f35b3d3560203555604080361115604c5736038060403d373d3d355af43d6000803e604c573d6000fd +``` + +where `________________________________________` is the 20-byte factory address. + +The transparent upgradeable proxy MUST be deployed by a factory that is responsible for authenticating upgrades. + +As the proxy's runtime bytecode contains logic to allow the factory to set any storage slot with any value, the initialization code MAY skip storing the implementation slot. + +The upgrading logic does not emit the ERC-1967 event. Indexers MUST NOT expect the upgrading logic to emit the ERC-1967 events. + +During upgrades, the factory MUST call the upgradeable proxy with following calldata: + +```solidity +abi.encodePacked( + // The new implementation address, converted to a 32-byte word. + uint256(uint160(implementation)), + // ERC-1967 implementation slot. + bytes32(0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc), + // Optional calldata to be forwarded to the implementation + // via delegatecall after setting the implementation slot. + "" +) +``` + +Emitting the ERC-1967 events during initialization is OPTIONAL. Indexers MUST NOT expect the initialization code to emit the ERC-1967 events. + +#### Minimal ERC-1967 transparent upgradeable proxy (14-byte factory address variant) - +We provide a variant for a 14-byte factory address. -#### Minimal ERC-1967 transparent upgradeable proxy (I-variant) +It is beneficial to install the factory at a vanity address with leading zero bytes so that the proxy's bytecode can be optimized to be shorter. - +This is the runtime bytecode: - +``` +3d3d3373____________________________14605757363d3d37363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6052573d6000fd5b3d6000f35b3d356020355560408036111560525736038060403d373d3d355af43d6000803e6052573d6000fd +``` + +where `____________________________` is the 14-byte factory address. + +Emitting the ERC-1967 events during initialization is OPTIONAL. Indexers MUST NOT expect the initialization code to emit the ERC-1967 events. ### Minimal ERC-1967 UUPS proxy - +This is the runtime bytecode: + +``` +363d3d373d3d363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6038573d6000fd5b3d6000f3 +``` + +During deployment, the initialization code MUST store the implementation at the ERC-1967 implementation storage slot `0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc`. + +Emitting the ERC-1967 events during initialization is OPTIONAL. Indexers MUST NOT expect the initialization code to emit the ERC-1967 events. #### Minimal ERC-1967 UUPS proxy (I-variant) - +This is the runtime bytecode: + +``` +365814604357363d3d373d3d363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e603e573d6000fd5b3d6000f35b6020600f3d393d51543d52593df3 +``` + +When called with any 1-byte calldata, the I-variant returns the address of the implementation, and will not forward the calldata to the implementation. + +During deployment, the initialization code MUST store the implementation at the ERC-1967 implementation storage slot `0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc`. + +Emitting the ERC-1967 events during initialization is OPTIONAL. Indexers MUST NOT expect the initialization code to emit the ERC-1967 events. ### Minimal ERC-1967 beacon proxy - +This is the runtime bytecode: + +``` +363d3d373d3d363d602036600436635c60da1b60e01b36527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50545afa5036515af43d6000803e604d573d6000fd5b3d6000f3 +``` + +During deployment, the initialization code MUST store the beacon at the ERC-1967 beacon storage slot `0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50`. + +Emitting the ERC-1967 events during initialization is OPTIONAL. Indexers MUST NOT expect the initialization code to emit the ERC-1967 events. #### Minimal ERC-1967 beacon proxy (I-variant) - +This is the runtime bytecode: + +``` +363d3d373d3d363d602036600436635c60da1b60e01b36527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50545afa361460525736515af43d600060013e6052573d6001fd5b3d6001f3 +``` + +When called with any 1-byte calldata, the I-variant returns the address of the implementation, and will not forward the calldata to the implementation. + +During deployment, the initialization code MUST store the beacon at the ERC-1967 beacon storage slot `0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50`. + +Emitting the ERC-1967 events during initialization is OPTIONAL. Indexers MUST NOT expect the initialization code to emit the ERC-1967 events. ## Rationale +### No usage of `PUSH0` opcode + +For more widespread EVM compatibility, the proxies deliberately do not use the `PUSH0` opcode proposed in [EIP-3855](./eip-3855.md). + +Converting the proxies to `PUSH0` variants may be done in a separate future ERC. + ### Optimization priorities - - +The proxies are first optimized for minimal runtime gas before minimal bytecode size. + +### Transparent upgradeable proxy + +The factory address in the transparent upgradeable proxy is baked into the immutable bytecode of the minimal transparent upgradeable proxy. + +This is to save a `SLOAD` for every proxy call. -### Immutable admin in transparent upgradeable proxy +As the factory can contain custom authorization logic that allows for admin rotation, we do not lose any flexibility. - - +The upgrade logic takes in any 32 byte value and 32 byte storage slot. This is for flexibility and bytecode conciseness. + +We do not lose any security as the implementation can still modify any storage slot. ### I-variants - +The so-called "I-variants" contain logic that returns the implementation address baked into the proxy bytecode. + +This allows contracts to retrieve the implementation of the proxy onchain in a verifiable way. + +As long as the proxy's runtime bytecode starts with the bytecode in this standard, we can be sure that the implementation address is not spoofed. + +The choice of reserving 1-byte calldata to denote an implementation query request is for efficiency and to prevent calldata collision. Regular ETH transfers use 0-byte calldata, and regular Solidity function calls use calldata that is 4 bytes or longer. ### Omission of events in bytecode - +This is for minimal bytecode size and deployment costs. + +Most block explorers and indexers are able to deduce the latest implementation without the use of events simply by reading the slots. ### Immutable arguments are not appended to forwarded calldata - - +This is to avoid compatibility and safety issues with other ERC standards that append extra data to the calldata. + +The `EXTCODECOPY` opcode can be used to retrieve the immutable arguments. ## Backwards Compatibility @@ -89,31 +186,210 @@ No backward compatibility issues found. ### Minimal ERC-1967 transparent upgradeable proxy implementation - - -#### Minimal ERC-1967 transparent upgradeable proxy implementation (I-variant) - +```solidity +pragma solidity ^0.8.0; + +library ERC1967MinimalTransparentUpgradeableProxyLib { + function initCodeFor20ByteFactoryAddress() internal view returns (bytes memory) { + return abi.encodePacked( + bytes13(0x607f3d8160093d39f33d3d3373), + address(this), + bytes32(0x14605757363d3d37363d7f360894a13ba1a3210667c828492db98dca3e2076cc), + bytes32(0x3735a920a3ca505d382bbc545af43d6000803e6052573d6000fd5b3d6000f35b), + bytes32(0x3d356020355560408036111560525736038060403d373d3d355af43d6000803e), + bytes7(0x6052573d6000fd) + ); + } + + function initCodeFor14ByteFactoryAddress() internal view returns (bytes memory) { + return abi.encodePacked( + bytes13(0x60793d8160093d39f33d3d336d), + uint112(uint160(address(this))), + bytes32(0x14605157363d3d37363d7f360894a13ba1a3210667c828492db98dca3e2076cc), + bytes32(0x3735a920a3ca505d382bbc545af43d6000803e604c573d6000fd5b3d6000f35b), + bytes32(0x3d3560203555604080361115604c5736038060403d373d3d355af43d6000803e), + bytes7(0x604c573d6000fd) + ); + } + + function initCode() internal view returns (bytes memory) { + if (uint160(address(this)) >> 112 != 0) { + return initCodeFor20ByteFactoryAddress(); + } else { + return initCodeFor14ByteFactoryAddress(); + } + } + + function deploy(address implementation) internal returns (address instance) { + bytes memory m = initCode(); + assembly { + instance := create(0, add(m, 0x20), mload(m)) + } + require(instance != address(0), "Deployment failed."); + (bool success,) = instance.call( + abi.encodePacked( + // The new implementation address, converted to a 32-byte word. + uint256(uint160(implementation)), + // ERC-1967 implementation slot. + bytes32(0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc), + // Optional calldata to be forwarded to the implementation + // via delegatecall after setting the implementation slot. + "" + ) + ); + require(success, "Initialization failed."); + } +} +``` ### Minimal ERC-1967 UUPS proxy implementation - +```solidity +pragma solidity ^0.8.0; + +library ERC1967MinimalUUPSProxyLib { + function initCode(address implementation, bytes memory args) + internal + pure + returns (bytes memory) + { + uint256 n = 0x003d + args.length; + require(n <= 0xffff, "Immutable args too long."); + return abi.encodePacked( + bytes1(0x61), + uint16(n), + bytes7(0x3d8160233d3973), + implementation, + bytes2(0x6009), + bytes32(0x5155f3363d3d373d3d363d7f360894a13ba1a3210667c828492db98dca3e2076), + bytes32(0xcc3735a920a3ca505d382bbc545af43d6000803e6038573d6000fd5b3d6000f3), + args + ); + } + + function deploy(address implementation, bytes memory args) + internal + returns (address instance) + { + bytes memory m = initCode(implementation, args); + assembly { + instance := create(0, add(m, 0x20), mload(m)) + } + require(instance != address(0), "Deployment failed."); + } +} +``` #### Minimal ERC-1967 UUPS proxy implementation (I-variant) - +```solidity +pragma solidity ^0.8.0; + +library ERC1967IMinimalUUPSProxyLib { + function initCode(address implementation, bytes memory args) + internal + pure + returns (bytes memory) + { + uint256 n = 0x0052 + args.length; + require(n <= 0xffff, "Immutable args too long."); + return abi.encodePacked( + bytes1(0x61), + uint16(n), + bytes7(0x3d8160233d3973), + implementation, + bytes23(0x600f5155f3365814604357363d3d373d3d363d7f360894), + bytes32(0xa13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af4), + bytes32(0x3d6000803e603e573d6000fd5b3d6000f35b6020600f3d393d51543d52593df3), + args + ); + } + + function deploy(address implementation, bytes memory args) + internal + returns (address instance) + { + bytes memory m = initCode(implementation, args); + assembly { + instance := create(0, add(m, 0x20), mload(m)) + } + require(instance != address(0), "Deployment failed."); + } +} +``` ### Minimal ERC-1967 beacon proxy implementation - +```solidity +pragma solidity ^0.8.0; + +library ERC1967MinimalBeaconProxyLib { + function initCode(address beacon, bytes memory args) internal pure returns (bytes memory) { + uint256 n = 0x0052 + args.length; + require(n <= 0xffff, "Immutable args too long."); + return abi.encodePacked( + bytes1(0x61), + uint16(n), + bytes7(0x3d8160233d3973), + beacon, + bytes23(0x60195155f3363d3d373d3d363d602036600436635c60da), + bytes32(0x1b60e01b36527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6c), + bytes32(0xb3582b35133d50545afa5036515af43d6000803e604d573d6000fd5b3d6000f3), + args + ); + } + + function deploy(address beacon, bytes memory args) internal returns (address instance) { + bytes memory m = initCode(beacon, args); + assembly { + instance := create(0, add(m, 0x20), mload(m)) + } + require(instance != address(0), "Deployment failed."); + } +} +``` #### Minimal ERC-1967 beacon proxy implementation (I-variant) - +```solidity +pragma solidity ^0.8.0; + +library ERC1967IMinimalBeaconProxyLib { + function initCode(address beacon, bytes memory args) internal pure returns (bytes memory) { + uint256 n = 0x0057 + args.length; + require(n <= 0xffff, "Immutable args too long."); + return abi.encodePacked( + bytes1(0x61), + uint16(n), + bytes7(0x3d8160233d3973), + beacon, + bytes28(0x60195155f3363d3d373d3d363d602036600436635c60da1b60e01b36), + bytes32(0x527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b3513), + bytes32(0x3d50545afa361460525736515af43d600060013e6052573d6001fd5b3d6001f3), + args + ); + } + + function deploy(address beacon, bytes memory args) internal returns (address instance) { + bytes memory m = initCode(beacon, args); + assembly { + instance := create(0, add(m, 0x20), mload(m)) + } + require(instance != address(0), "Deployment failed."); + } +} +``` ## Security Considerations - +### Transparent upgradeable proxy factory security considerations + +The transparent upgradeable proxy factory must implement proper access control to allow only authorized accounts to upgrade proxies. + +### Calldata length collision for I-variants + +The I-variants reserve all calldata of length 1 to denote a request to return the implementation. This may pose compatibility issues if the underlying implementation actually uses 1-byte calldata for special purposes. ## Copyright From 8dea3c5989f122fc20ffdae699ade528a237c4c8 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 23 Aug 2024 06:26:56 +0000 Subject: [PATCH 04/18] Edit ERC --- ERCS/erc-9999.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ERCS/erc-9999.md b/ERCS/erc-9999.md index 5b5794663d..6acf076914 100644 --- a/ERCS/erc-9999.md +++ b/ERCS/erc-9999.md @@ -2,7 +2,7 @@ eip: 9999 title: Minimal Upgradeable Proxies author: Atarpara (@Atarpara), JT Riley (@jtriley-eth), xiaobaiskill (@xiaobaiskill), Vectorized (@Vectorized) -discussions-to: https://ethereum-magicians.org/t/minimal-upgradeable-proxies +discussions-to: https://ethereum-magicians.org/t/minimal-upgradeable-proxies/20868 status: Draft type: Standards Track category: ERC From eda11190f31e8964b4305f7aae2c20853b7ea81f Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 23 Aug 2024 06:30:39 +0000 Subject: [PATCH 05/18] Add description --- ERCS/erc-9999.md | 1 + 1 file changed, 1 insertion(+) diff --git a/ERCS/erc-9999.md b/ERCS/erc-9999.md index 6acf076914..0bd9dced4c 100644 --- a/ERCS/erc-9999.md +++ b/ERCS/erc-9999.md @@ -1,6 +1,7 @@ --- eip: 9999 title: Minimal Upgradeable Proxies +description: Minimal bytecode ERC-1967 upgradeable proxies (transparent, UUPS, beacon proxy) with optional immutable arguments author: Atarpara (@Atarpara), JT Riley (@jtriley-eth), xiaobaiskill (@xiaobaiskill), Vectorized (@Vectorized) discussions-to: https://ethereum-magicians.org/t/minimal-upgradeable-proxies/20868 status: Draft From e8bac73408a019e120fe7585150236c4d6e6246a Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 23 Aug 2024 06:31:24 +0000 Subject: [PATCH 06/18] Edit --- ERCS/erc-9999.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ERCS/erc-9999.md b/ERCS/erc-9999.md index 0bd9dced4c..b28e8d2ce2 100644 --- a/ERCS/erc-9999.md +++ b/ERCS/erc-9999.md @@ -1,7 +1,7 @@ --- eip: 9999 title: Minimal Upgradeable Proxies -description: Minimal bytecode ERC-1967 upgradeable proxies (transparent, UUPS, beacon proxy) with optional immutable arguments +description: Minimal bytecode ERC-1967 upgradeable proxies (transparent, UUPS, beacon) with optional immutable arguments author: Atarpara (@Atarpara), JT Riley (@jtriley-eth), xiaobaiskill (@xiaobaiskill), Vectorized (@Vectorized) discussions-to: https://ethereum-magicians.org/t/minimal-upgradeable-proxies/20868 status: Draft @@ -13,7 +13,7 @@ requires: 1967 ## Abstract -This standard defines minimal [ERC-1967](./eip-1967.md) proxies for three patterns: (1) transparent upgradeable proxy, (2) UUPS proxy, (3) beacon proxy. The proxies support optional immutable arguments which are appended to the end of their runtime bytecode. Additional variants which support onchain implementation querying are provided. +This standard defines minimal [ERC-1967](./eip-1967.md) proxies for three patterns: (1) transparent, (2) UUPS, (3) beacon. The proxies support optional immutable arguments which are appended to the end of their runtime bytecode. Additional variants which support onchain implementation querying are provided. ## Motivation From e00c8577c75ac471e54db876b47cc104d6ebc202 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 23 Aug 2024 06:32:35 +0000 Subject: [PATCH 07/18] Edit --- ERCS/erc-9999.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ERCS/erc-9999.md b/ERCS/erc-9999.md index b28e8d2ce2..49672c34d0 100644 --- a/ERCS/erc-9999.md +++ b/ERCS/erc-9999.md @@ -1,7 +1,7 @@ --- eip: 9999 title: Minimal Upgradeable Proxies -description: Minimal bytecode ERC-1967 upgradeable proxies (transparent, UUPS, beacon) with optional immutable arguments +description: Minimal bytecode ERC-1967 upgradeable proxies (transparent, UUPS, beacon) with immutable arguments and verifiable onchain implementation query support author: Atarpara (@Atarpara), JT Riley (@jtriley-eth), xiaobaiskill (@xiaobaiskill), Vectorized (@Vectorized) discussions-to: https://ethereum-magicians.org/t/minimal-upgradeable-proxies/20868 status: Draft From 6651f258791835b9eab7704170ce7a2bfb8c8c70 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 23 Aug 2024 06:34:54 +0000 Subject: [PATCH 08/18] Edit --- ERCS/erc-9999.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ERCS/erc-9999.md b/ERCS/erc-9999.md index 49672c34d0..604895eba3 100644 --- a/ERCS/erc-9999.md +++ b/ERCS/erc-9999.md @@ -1,7 +1,7 @@ --- eip: 9999 title: Minimal Upgradeable Proxies -description: Minimal bytecode ERC-1967 upgradeable proxies (transparent, UUPS, beacon) with immutable arguments and verifiable onchain implementation query support +description: Minimal upgradeable proxies with immutable arguments and support for onchain implementation queries author: Atarpara (@Atarpara), JT Riley (@jtriley-eth), xiaobaiskill (@xiaobaiskill), Vectorized (@Vectorized) discussions-to: https://ethereum-magicians.org/t/minimal-upgradeable-proxies/20868 status: Draft From 9b570d69af186f319767a6b8865fe555d42b5c64 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Sun, 25 Aug 2024 00:10:23 +0000 Subject: [PATCH 09/18] Update ERC draft --- ERCS/{erc-9999.md => erc-7760.md} | 158 +++++++++++++++++++++++++----- 1 file changed, 131 insertions(+), 27 deletions(-) rename ERCS/{erc-9999.md => erc-7760.md} (72%) diff --git a/ERCS/erc-9999.md b/ERCS/erc-7760.md similarity index 72% rename from ERCS/erc-9999.md rename to ERCS/erc-7760.md index 604895eba3..023198a8a5 100644 --- a/ERCS/erc-9999.md +++ b/ERCS/erc-7760.md @@ -1,9 +1,9 @@ --- -eip: 9999 +eip: 7760 title: Minimal Upgradeable Proxies description: Minimal upgradeable proxies with immutable arguments and support for onchain implementation queries author: Atarpara (@Atarpara), JT Riley (@jtriley-eth), xiaobaiskill (@xiaobaiskill), Vectorized (@Vectorized) -discussions-to: https://ethereum-magicians.org/t/minimal-upgradeable-proxies/20868 +discussions-to: https://ethereum-magicians.org/t/erc-7760-minimal-upgradeable-proxies/20868 status: Draft type: Standards Track category: ERC @@ -33,15 +33,9 @@ The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "S All of the following proxies MAY have optional data bytecode appended to the end of their runtime bytecode. -### Minimal ERC-1967 transparent upgradeable proxy - -This is the runtime bytecode: - -``` -3d3d336d________________________________________14605157363d3d37363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e604c573d6000fd5b3d6000f35b3d3560203555604080361115604c5736038060403d373d3d355af43d6000803e604c573d6000fd -``` +Emitting the ERC-1967 events during initialization is OPTIONAL. Indexers MUST NOT expect the initialization code to emit the ERC-1967 events. -where `________________________________________` is the 20-byte factory address. +### Minimal ERC-1967 transparent upgradeable proxy The transparent upgradeable proxy MUST be deployed by a factory that is responsible for authenticating upgrades. @@ -63,13 +57,20 @@ abi.encodePacked( ) ``` -Emitting the ERC-1967 events during initialization is OPTIONAL. Indexers MUST NOT expect the initialization code to emit the ERC-1967 events. +As it is beneficial to install the factory at a vanity address with leading zero bytes so that the proxy's bytecode can be optimized to be shorter, variants for 14-byte factory address are provided. -#### Minimal ERC-1967 transparent upgradeable proxy (14-byte factory address variant) +#### Minimal ERC-1967 transparent upgradeable proxy (20-byte factory address regular variant) -We provide a variant for a 14-byte factory address. +This is the runtime bytecode: + +``` +3d3d336d________________________________________14605157363d3d37363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e604c573d6000fd5b3d6000f35b3d3560203555604080361115604c5736038060403d373d3d355af43d6000803e604c573d6000fd +``` + +where `________________________________________` is the 20-byte factory address. -It is beneficial to install the factory at a vanity address with leading zero bytes so that the proxy's bytecode can be optimized to be shorter. + +#### Minimal ERC-1967 transparent upgradeable proxy (14-byte factory address regular variant) This is the runtime bytecode: @@ -79,10 +80,32 @@ This is the runtime bytecode: where `____________________________` is the 14-byte factory address. -Emitting the ERC-1967 events during initialization is OPTIONAL. Indexers MUST NOT expect the initialization code to emit the ERC-1967 events. +#### Minimal ERC-1967 transparent upgradeable proxy (20-byte factory address I-variant) + +``` +3658146083573d3d3373________________________________________14605D57363d3d37363D7f360894a13ba1A3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6058573d6000fd5b3d6000f35b3d35602035556040360380156058578060403d373d3d355af43d6000803e6058573d6000fd5b602060293d393d51543d52593df3 +``` + +where `________________________________________` is the 20-byte factory address. + +When called with any 1-byte calldata, the I-variant returns the address of the implementation, and will not forward the calldata to the implementation. + +#### Minimal ERC-1967 transparent upgradeable proxy (14-byte factory address I-variant) + +This is the runtime bytecode: + +``` +365814607d573d3d336d____________________________14605757363d3D37363d7F360894A13Ba1A3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6052573d6000fd5b3d6000f35b3d35602035556040360380156052578060403d373d3d355af43d6000803e6052573d6000fd5b602060233d393d51543d52593df3 +``` + +where `____________________________` is the 14-byte factory address. + +When called with any 1-byte calldata, the I-variant returns the address of the implementation, and will not forward the calldata to the implementation. ### Minimal ERC-1967 UUPS proxy +#### Minimal ERC-1967 UUPS proxy (regular variant) + This is the runtime bytecode: ``` @@ -91,8 +114,6 @@ This is the runtime bytecode: During deployment, the initialization code MUST store the implementation at the ERC-1967 implementation storage slot `0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc`. -Emitting the ERC-1967 events during initialization is OPTIONAL. Indexers MUST NOT expect the initialization code to emit the ERC-1967 events. - #### Minimal ERC-1967 UUPS proxy (I-variant) This is the runtime bytecode: @@ -105,10 +126,10 @@ When called with any 1-byte calldata, the I-variant returns the address of the i During deployment, the initialization code MUST store the implementation at the ERC-1967 implementation storage slot `0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc`. -Emitting the ERC-1967 events during initialization is OPTIONAL. Indexers MUST NOT expect the initialization code to emit the ERC-1967 events. - ### Minimal ERC-1967 beacon proxy +#### Minimal ERC-1967 beacon proxy (regular variant) + This is the runtime bytecode: ``` @@ -117,8 +138,6 @@ This is the runtime bytecode: During deployment, the initialization code MUST store the beacon at the ERC-1967 beacon storage slot `0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50`. -Emitting the ERC-1967 events during initialization is OPTIONAL. Indexers MUST NOT expect the initialization code to emit the ERC-1967 events. - #### Minimal ERC-1967 beacon proxy (I-variant) This is the runtime bytecode: @@ -131,8 +150,6 @@ When called with any 1-byte calldata, the I-variant returns the address of the i During deployment, the initialization code MUST store the beacon at the ERC-1967 beacon storage slot `0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50`. -Emitting the ERC-1967 events during initialization is OPTIONAL. Indexers MUST NOT expect the initialization code to emit the ERC-1967 events. - ## Rationale ### No usage of `PUSH0` opcode @@ -187,6 +204,7 @@ No backward compatibility issues found. ### Minimal ERC-1967 transparent upgradeable proxy implementation +#### Minimal ERC-1967 transparent upgradeable proxy implementation (regular variant) ```solidity pragma solidity ^0.8.0; @@ -222,12 +240,84 @@ library ERC1967MinimalTransparentUpgradeableProxyLib { } } - function deploy(address implementation) internal returns (address instance) { + function deploy(address implementation, bytes memory initializationData) + internal + returns (address instance) + { + bytes memory m = initCode(); + assembly { + instance := create(0, add(m, 0x20), mload(m)) + } + require(instance != address(0), "Deployment failed."); + upgrade(instance, implementation, initializationData); + } + + function upgrade(address instance, address implementation, bytes memory upgradeData) internal { + (bool success,) = instance.call( + abi.encodePacked( + // The new implementation address, converted to a 32-byte word. + uint256(uint160(implementation)), + // ERC-1967 implementation slot. + bytes32(0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc), + // Optional calldata to be forwarded to the implementation + // via delegatecall after setting the implementation slot. + upgradeData + ) + ); + require(success, "Upgrade failed."); + } +} +``` + +#### Minimal ERC-1967 transparent upgradeable proxy implementation (I-variant) + +```solidity +pragma solidity ^0.8.0; + +library ERC1967IMinimalTransparentUpgradeableProxyLib { + function initCodeFor20ByteFactoryAddress() internal view returns (bytes memory) { + return abi.encodePacked( + bytes19(0x60923d8160093d39f33658146083573d3d3373), + address(this), + bytes20(0x14605D57363d3d37363D7f360894a13ba1A32106), + bytes32(0x67c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e60), + bytes32(0x58573d6000fd5b3d6000f35b3d35602035556040360380156058578060403d37), + bytes32(0x3d3d355af43d6000803e6058573d6000fd5b602060293d393d51543d52593df3) + ); + } + + function initCodeFor14ByteFactoryAddress() internal view returns (bytes memory) { + return abi.encodePacked( + bytes19(0x608c3d8160093d39f3365814607d573d3d336d), + uint112(uint160(address(this))), + bytes20(0x14605757363d3D37363d7F360894A13Ba1A32106), + bytes32(0x67c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e60), + bytes32(0x52573d6000fd5b3d6000f35b3d35602035556040360380156052578060403d37), + bytes32(0x3d3d355af43d6000803e6052573d6000fd5b602060233d393d51543d52593df3) + ); + } + + function initCode() internal view returns (bytes memory) { + if (uint160(address(this)) >> 112 != 0) { + return initCodeFor20ByteFactoryAddress(); + } else { + return initCodeFor14ByteFactoryAddress(); + } + } + + function deploy(address implementation, bytes memory initializationData) + internal + returns (address instance) + { bytes memory m = initCode(); assembly { instance := create(0, add(m, 0x20), mload(m)) } require(instance != address(0), "Deployment failed."); + upgrade(instance, implementation, initializationData); + } + + function upgrade(address instance, address implementation, bytes memory upgradeData) internal { (bool success,) = instance.call( abi.encodePacked( // The new implementation address, converted to a 32-byte word. @@ -236,16 +326,18 @@ library ERC1967MinimalTransparentUpgradeableProxyLib { bytes32(0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc), // Optional calldata to be forwarded to the implementation // via delegatecall after setting the implementation slot. - "" + upgradeData ) ); - require(success, "Initialization failed."); + require(success, "Upgrade failed."); } } ``` ### Minimal ERC-1967 UUPS proxy implementation +#### Minimal ERC-1967 UUPS proxy implementation (regular variant) + ```solidity pragma solidity ^0.8.0; @@ -322,6 +414,8 @@ library ERC1967IMinimalUUPSProxyLib { ### Minimal ERC-1967 beacon proxy implementation +#### Minimal ERC-1967 beacon proxy implementation (regular variant) + ```solidity pragma solidity ^0.8.0; @@ -386,12 +480,22 @@ library ERC1967IMinimalBeaconProxyLib { ### Transparent upgradeable proxy factory security considerations -The transparent upgradeable proxy factory must implement proper access control to allow only authorized accounts to upgrade proxies. +The transparent upgradeable proxy factory must implement proper access control to allow proxies to be upgraded by only authorized accounts. ### Calldata length collision for I-variants The I-variants reserve all calldata of length 1 to denote a request to return the implementation. This may pose compatibility issues if the underlying implementation actually uses 1-byte calldata for special purposes. +### Onchain querying of implementation for I-variants + +The bytecode of the proxies before any optional immutable arguments must be verified with the following steps: + +1. Fetch the bytecode before any immutable arguments with `EXTCODECOPY`. +2. Zeroize any baked-in factory address in the fetched bytecode. +3. Ensure that the hash of the final fetched bytecode matches the expected hash of the bytecode. + +If the hash does not match, the implementation address returned must not be trusted. + ## Copyright Copyright and related rights waived via [CC0](../LICENSE.md). From d56ef9aacc2df2f3cd3e0358e21b6aef6be734b6 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Sun, 25 Aug 2024 00:20:24 +0000 Subject: [PATCH 10/18] Edit ERC --- ERCS/erc-7760.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/ERCS/erc-7760.md b/ERCS/erc-7760.md index 023198a8a5..f6943058b2 100644 --- a/ERCS/erc-7760.md +++ b/ERCS/erc-7760.md @@ -196,6 +196,16 @@ This is to avoid compatibility and safety issues with other ERC standards that a The `EXTCODECOPY` opcode can be used to retrieve the immutable arguments. +### Out of scope topics + +The following topics are intentionally out of scope of this standard, as they can contain custom logic: + +- Factories for proxy deployment. +- Logic for reading and verifying the implementation from the I-variants onchain. +- Beacon for the beacon proxies. + +Nevertheless, they require careful implementation to ensure security and correctness. + ## Backwards Compatibility No backward compatibility issues found. @@ -480,7 +490,7 @@ library ERC1967IMinimalBeaconProxyLib { ### Transparent upgradeable proxy factory security considerations -The transparent upgradeable proxy factory must implement proper access control to allow proxies to be upgraded by only authorized accounts. +The transparent upgradeable proxy factory MUST implement proper access control to allow proxies to be upgraded by only authorized accounts. ### Calldata length collision for I-variants @@ -488,13 +498,13 @@ The I-variants reserve all calldata of length 1 to denote a request to return th ### Onchain querying of implementation for I-variants -The bytecode of the proxies before any optional immutable arguments must be verified with the following steps: +The bytecode of the proxies before any optional immutable arguments MUST be verified with the following steps: 1. Fetch the bytecode before any immutable arguments with `EXTCODECOPY`. 2. Zeroize any baked-in factory address in the fetched bytecode. 3. Ensure that the hash of the final fetched bytecode matches the expected hash of the bytecode. -If the hash does not match, the implementation address returned must not be trusted. +If the hash does not match, the implementation address returned MUST NOT be trusted. ## Copyright From 1729e17381aa6dcfca9cc6bdb6a844260e6583c3 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Mon, 26 Aug 2024 01:03:44 +0000 Subject: [PATCH 11/18] Edit ERC --- ERCS/erc-7760.md | 54 ++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/ERCS/erc-7760.md b/ERCS/erc-7760.md index f6943058b2..624a4396a2 100644 --- a/ERCS/erc-7760.md +++ b/ERCS/erc-7760.md @@ -29,15 +29,29 @@ The minimal nature of the proxies enables cheaper deployment and runtime costs. The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119 and RFC 8174. -### Overview +### General specifications All of the following proxies MAY have optional data bytecode appended to the end of their runtime bytecode. Emitting the ERC-1967 events during initialization is OPTIONAL. Indexers MUST NOT expect the initialization code to emit the ERC-1967 events. +### Onchain querying of implementation for I-variants + +The I-variants have logic that returns the implementation baked into their bytecode. + +When called with any 1-byte calldata, these I-variants will return the address (left-zero-padded to 32 bytes). + +The bytecode of the proxies before any optional immutable arguments MUST be verified with the following steps: + +1. Fetch the bytecode before any immutable arguments with `EXTCODECOPY`. +2. Zeroize any baked-in factory address in the fetched bytecode. +3. Ensure that the hash of the final fetched bytecode matches the expected hash of the bytecode. + +If the hash does not match, the implementation address returned MUST NOT be trusted. + ### Minimal ERC-1967 transparent upgradeable proxy -The transparent upgradeable proxy MUST be deployed by a factory that is responsible for authenticating upgrades. +The transparent upgradeable proxy is RECOMMENDED to be deployed by a factory that doubles as the account that is authenticated to perform upgrades. An externally owned account may perform the deployment on behalf of the factory. For convention, we will refer to the factory as the immutable account authorized to invoke the upgrade logic on the proxy. As the proxy's runtime bytecode contains logic to allow the factory to set any storage slot with any value, the initialization code MAY skip storing the implementation slot. @@ -59,7 +73,7 @@ abi.encodePacked( As it is beneficial to install the factory at a vanity address with leading zero bytes so that the proxy's bytecode can be optimized to be shorter, variants for 14-byte factory address are provided. -#### Minimal ERC-1967 transparent upgradeable proxy (20-byte factory address regular variant) +#### Minimal ERC-1967 transparent upgradeable proxy for 20-byte factory address (basic variant) This is the runtime bytecode: @@ -70,7 +84,7 @@ This is the runtime bytecode: where `________________________________________` is the 20-byte factory address. -#### Minimal ERC-1967 transparent upgradeable proxy (14-byte factory address regular variant) +#### Minimal ERC-1967 transparent upgradeable proxy for 14-byte factory address (I-variant) This is the runtime bytecode: @@ -80,7 +94,7 @@ This is the runtime bytecode: where `____________________________` is the 14-byte factory address. -#### Minimal ERC-1967 transparent upgradeable proxy (20-byte factory address I-variant) +#### Minimal ERC-1967 transparent upgradeable proxy for 20-byte factory address (basic variant) ``` 3658146083573d3d3373________________________________________14605D57363d3d37363D7f360894a13ba1A3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6058573d6000fd5b3d6000f35b3d35602035556040360380156058578060403d373d3d355af43d6000803e6058573d6000fd5b602060293d393d51543d52593df3 @@ -90,7 +104,7 @@ where `________________________________________` is the 20-byte factory address. When called with any 1-byte calldata, the I-variant returns the address of the implementation, and will not forward the calldata to the implementation. -#### Minimal ERC-1967 transparent upgradeable proxy (14-byte factory address I-variant) +#### Minimal ERC-1967 transparent upgradeable proxy for 14-byte factory address (I-variant) This is the runtime bytecode: @@ -104,7 +118,7 @@ When called with any 1-byte calldata, the I-variant returns the address of the i ### Minimal ERC-1967 UUPS proxy -#### Minimal ERC-1967 UUPS proxy (regular variant) +#### Minimal ERC-1967 UUPS proxy (basic variant) This is the runtime bytecode: @@ -128,7 +142,7 @@ During deployment, the initialization code MUST store the implementation at the ### Minimal ERC-1967 beacon proxy -#### Minimal ERC-1967 beacon proxy (regular variant) +#### Minimal ERC-1967 beacon proxy (basic variant) This is the runtime bytecode: @@ -162,6 +176,12 @@ Converting the proxies to `PUSH0` variants may be done in a separate future ERC. The proxies are first optimized for minimal runtime gas before minimal bytecode size. +### Minimal nature + +These proxies made from handcrafted EVM bytecode. While utmost efforts have been made to ensure that they are as minimal as possible at the time of development, it is possible that they can be further optimized. If a variant has already been used in the wild, it is preferable to keep their existing layout in this standard, as the benefits of automatic block explorer verification will outweigh the few gas saved during runtime or deployment. + +For historical reference, the [ERC-1167](./eip-1167.md) minimal proxy was not the theoretical minimal at the time of writing. The 0age minimal proxy has lower runtime gas costs and smaller bytecode size. + ### Transparent upgradeable proxy The factory address in the transparent upgradeable proxy is baked into the immutable bytecode of the minimal transparent upgradeable proxy. @@ -214,7 +234,7 @@ No backward compatibility issues found. ### Minimal ERC-1967 transparent upgradeable proxy implementation -#### Minimal ERC-1967 transparent upgradeable proxy implementation (regular variant) +#### Minimal ERC-1967 transparent upgradeable proxy implementation (basic variant) ```solidity pragma solidity ^0.8.0; @@ -346,7 +366,7 @@ library ERC1967IMinimalTransparentUpgradeableProxyLib { ### Minimal ERC-1967 UUPS proxy implementation -#### Minimal ERC-1967 UUPS proxy implementation (regular variant) +#### Minimal ERC-1967 UUPS proxy implementation (basic variant) ```solidity pragma solidity ^0.8.0; @@ -424,7 +444,7 @@ library ERC1967IMinimalUUPSProxyLib { ### Minimal ERC-1967 beacon proxy implementation -#### Minimal ERC-1967 beacon proxy implementation (regular variant) +#### Minimal ERC-1967 beacon proxy implementation (basic variant) ```solidity pragma solidity ^0.8.0; @@ -490,22 +510,12 @@ library ERC1967IMinimalBeaconProxyLib { ### Transparent upgradeable proxy factory security considerations -The transparent upgradeable proxy factory MUST implement proper access control to allow proxies to be upgraded by only authorized accounts. +To ensure security, the transparent upgradeable proxy factory must implement proper access control to allow proxies to be upgraded by only authorized accounts. ### Calldata length collision for I-variants The I-variants reserve all calldata of length 1 to denote a request to return the implementation. This may pose compatibility issues if the underlying implementation actually uses 1-byte calldata for special purposes. -### Onchain querying of implementation for I-variants - -The bytecode of the proxies before any optional immutable arguments MUST be verified with the following steps: - -1. Fetch the bytecode before any immutable arguments with `EXTCODECOPY`. -2. Zeroize any baked-in factory address in the fetched bytecode. -3. Ensure that the hash of the final fetched bytecode matches the expected hash of the bytecode. - -If the hash does not match, the implementation address returned MUST NOT be trusted. - ## Copyright Copyright and related rights waived via [CC0](../LICENSE.md). From 5fdbccaaf7241e27f205fd3db4199c101e95782c Mon Sep 17 00:00:00 2001 From: Vectorized Date: Mon, 26 Aug 2024 12:29:55 +0000 Subject: [PATCH 12/18] Fix bytecode, add author --- ERCS/erc-7760.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ERCS/erc-7760.md b/ERCS/erc-7760.md index 624a4396a2..b063806332 100644 --- a/ERCS/erc-7760.md +++ b/ERCS/erc-7760.md @@ -2,7 +2,7 @@ eip: 7760 title: Minimal Upgradeable Proxies description: Minimal upgradeable proxies with immutable arguments and support for onchain implementation queries -author: Atarpara (@Atarpara), JT Riley (@jtriley-eth), xiaobaiskill (@xiaobaiskill), Vectorized (@Vectorized) +author: Atarpara (@Atarpara), JT Riley (@jtriley-eth), Thomas (@0xth0mas), xiaobaiskill (@xiaobaiskill), Vectorized (@Vectorized) discussions-to: https://ethereum-magicians.org/t/erc-7760-minimal-upgradeable-proxies/20868 status: Draft type: Standards Track @@ -78,23 +78,23 @@ As it is beneficial to install the factory at a vanity address with leading zero This is the runtime bytecode: ``` -3d3d336d________________________________________14605157363d3d37363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e604c573d6000fd5b3d6000f35b3d3560203555604080361115604c5736038060403d373d3d355af43d6000803e604c573d6000fd +3d3d3373________________________________________14605757363d3d37363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6052573d6000fd5b3d6000f35b3d356020355560408036111560525736038060403d373d3d355af43d6000803e6052573d6000fd ``` where `________________________________________` is the 20-byte factory address. -#### Minimal ERC-1967 transparent upgradeable proxy for 14-byte factory address (I-variant) +#### Minimal ERC-1967 transparent upgradeable proxy for 14-byte factory address (basic variant) This is the runtime bytecode: ``` -3d3d3373____________________________14605757363d3d37363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6052573d6000fd5b3d6000f35b3d356020355560408036111560525736038060403d373d3d355af43d6000803e6052573d6000fd +3d3d336d____________________________14605157363d3d37363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e604c573d6000fd5b3d6000f35b3d3560203555604080361115604c5736038060403d373d3d355af43d6000803e604c573d6000fd ``` where `____________________________` is the 14-byte factory address. -#### Minimal ERC-1967 transparent upgradeable proxy for 20-byte factory address (basic variant) +#### Minimal ERC-1967 transparent upgradeable proxy for 20-byte factory address (I-variant) ``` 3658146083573d3d3373________________________________________14605D57363d3d37363D7f360894a13ba1A3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6058573d6000fd5b3d6000f35b3d35602035556040360380156058578060403d373d3d355af43d6000803e6058573d6000fd5b602060293d393d51543d52593df3 From 9f403b267a271188885b2a2b980d75762e84343f Mon Sep 17 00:00:00 2001 From: Vectorized Date: Mon, 26 Aug 2024 17:04:14 +0000 Subject: [PATCH 13/18] Tidy --- ERCS/erc-7760.md | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/ERCS/erc-7760.md b/ERCS/erc-7760.md index b063806332..30c6328aa2 100644 --- a/ERCS/erc-7760.md +++ b/ERCS/erc-7760.md @@ -39,7 +39,7 @@ Emitting the ERC-1967 events during initialization is OPTIONAL. Indexers MUST NO The I-variants have logic that returns the implementation baked into their bytecode. -When called with any 1-byte calldata, these I-variants will return the address (left-zero-padded to 32 bytes). +When called with any 1-byte calldata, these I-variants will return the address (left-zero-padded to 32 bytes) and will not forward the calldata to the target. The bytecode of the proxies before any optional immutable arguments MUST be verified with the following steps: @@ -73,9 +73,9 @@ abi.encodePacked( As it is beneficial to install the factory at a vanity address with leading zero bytes so that the proxy's bytecode can be optimized to be shorter, variants for 14-byte factory address are provided. -#### Minimal ERC-1967 transparent upgradeable proxy for 20-byte factory address (basic variant) +#### Minimal ERC-1967 transparent upgradeable proxy for (basic variant) -This is the runtime bytecode: +Runtime bytecode (20-byte factory address subvariant): ``` 3d3d3373________________________________________14605757363d3d37363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6052573d6000fd5b3d6000f35b3d356020355560408036111560525736038060403d373d3d355af43d6000803e6052573d6000fd @@ -84,9 +84,7 @@ This is the runtime bytecode: where `________________________________________` is the 20-byte factory address. -#### Minimal ERC-1967 transparent upgradeable proxy for 14-byte factory address (basic variant) - -This is the runtime bytecode: +Runtime bytecode (14-byte factory address subvariant): ``` 3d3d336d____________________________14605157363d3d37363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e604c573d6000fd5b3d6000f35b3d3560203555604080361115604c5736038060403d373d3d355af43d6000803e604c573d6000fd @@ -94,7 +92,9 @@ This is the runtime bytecode: where `____________________________` is the 14-byte factory address. -#### Minimal ERC-1967 transparent upgradeable proxy for 20-byte factory address (I-variant) +#### Minimal ERC-1967 transparent upgradeable proxy (I-variant) + +Runtime bytecode (20-byte factory address subvariant): ``` 3658146083573d3d3373________________________________________14605D57363d3d37363D7f360894a13ba1A3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6058573d6000fd5b3d6000f35b3d35602035556040360380156058578060403d373d3d355af43d6000803e6058573d6000fd5b602060293d393d51543d52593df3 @@ -102,11 +102,7 @@ where `____________________________` is the 14-byte factory address. where `________________________________________` is the 20-byte factory address. -When called with any 1-byte calldata, the I-variant returns the address of the implementation, and will not forward the calldata to the implementation. - -#### Minimal ERC-1967 transparent upgradeable proxy for 14-byte factory address (I-variant) - -This is the runtime bytecode: +Runtime bytecode (14-byte factory address subvariant): ``` 365814607d573d3d336d____________________________14605757363d3D37363d7F360894A13Ba1A3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6052573d6000fd5b3d6000f35b3d35602035556040360380156052578060403d373d3d355af43d6000803e6052573d6000fd5b602060233d393d51543d52593df3 @@ -114,13 +110,11 @@ This is the runtime bytecode: where `____________________________` is the 14-byte factory address. -When called with any 1-byte calldata, the I-variant returns the address of the implementation, and will not forward the calldata to the implementation. - ### Minimal ERC-1967 UUPS proxy #### Minimal ERC-1967 UUPS proxy (basic variant) -This is the runtime bytecode: +Runtime bytecode: ``` 363d3d373d3d363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6038573d6000fd5b3d6000f3 @@ -130,21 +124,19 @@ During deployment, the initialization code MUST store the implementation at the #### Minimal ERC-1967 UUPS proxy (I-variant) -This is the runtime bytecode: +Runtime bytecode: ``` 365814604357363d3d373d3d363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e603e573d6000fd5b3d6000f35b6020600f3d393d51543d52593df3 ``` -When called with any 1-byte calldata, the I-variant returns the address of the implementation, and will not forward the calldata to the implementation. - During deployment, the initialization code MUST store the implementation at the ERC-1967 implementation storage slot `0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc`. ### Minimal ERC-1967 beacon proxy #### Minimal ERC-1967 beacon proxy (basic variant) -This is the runtime bytecode: +Runtime bytecode: ``` 363d3d373d3d363d602036600436635c60da1b60e01b36527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50545afa5036515af43d6000803e604d573d6000fd5b3d6000f3 @@ -154,14 +146,12 @@ During deployment, the initialization code MUST store the beacon at the ERC-1967 #### Minimal ERC-1967 beacon proxy (I-variant) -This is the runtime bytecode: +Runtime bytecode: ``` 363d3d373d3d363d602036600436635c60da1b60e01b36527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50545afa361460525736515af43d600060013e6052573d6001fd5b3d6001f3 ``` -When called with any 1-byte calldata, the I-variant returns the address of the implementation, and will not forward the calldata to the implementation. - During deployment, the initialization code MUST store the beacon at the ERC-1967 beacon storage slot `0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50`. ## Rationale From a7b2a64eccb89e5e8c045f43746e893b5f78b384 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Mon, 26 Aug 2024 17:14:17 +0000 Subject: [PATCH 14/18] Tidy --- ERCS/erc-7760.md | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/ERCS/erc-7760.md b/ERCS/erc-7760.md index 30c6328aa2..1038684b0a 100644 --- a/ERCS/erc-7760.md +++ b/ERCS/erc-7760.md @@ -83,7 +83,6 @@ Runtime bytecode (20-byte factory address subvariant): where `________________________________________` is the 20-byte factory address. - Runtime bytecode (14-byte factory address subvariant): ``` @@ -112,6 +111,8 @@ where `____________________________` is the 14-byte factory address. ### Minimal ERC-1967 UUPS proxy +As this proxy does not contain upgrading logic, the initialization code MUST store the implementation at the ERC-1967 implementation storage slot `0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc`. + #### Minimal ERC-1967 UUPS proxy (basic variant) Runtime bytecode: @@ -120,8 +121,6 @@ Runtime bytecode: 363d3d373d3d363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6038573d6000fd5b3d6000f3 ``` -During deployment, the initialization code MUST store the implementation at the ERC-1967 implementation storage slot `0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc`. - #### Minimal ERC-1967 UUPS proxy (I-variant) Runtime bytecode: @@ -130,10 +129,10 @@ Runtime bytecode: 365814604357363d3d373d3d363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e603e573d6000fd5b3d6000f35b6020600f3d393d51543d52593df3 ``` -During deployment, the initialization code MUST store the implementation at the ERC-1967 implementation storage slot `0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc`. - ### Minimal ERC-1967 beacon proxy +As this proxy does not contain upgrading logic, the initialization code MUST store the implementation at the ERC-1967 implementation storage slot `0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc`. + #### Minimal ERC-1967 beacon proxy (basic variant) Runtime bytecode: @@ -142,8 +141,6 @@ Runtime bytecode: 363d3d373d3d363d602036600436635c60da1b60e01b36527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50545afa5036515af43d6000803e604d573d6000fd5b3d6000f3 ``` -During deployment, the initialization code MUST store the beacon at the ERC-1967 beacon storage slot `0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50`. - #### Minimal ERC-1967 beacon proxy (I-variant) Runtime bytecode: @@ -152,8 +149,6 @@ Runtime bytecode: 363d3d373d3d363d602036600436635c60da1b60e01b36527fa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50545afa361460525736515af43d600060013e6052573d6001fd5b3d6001f3 ``` -During deployment, the initialization code MUST store the beacon at the ERC-1967 beacon storage slot `0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50`. - ## Rationale ### No usage of `PUSH0` opcode From ec4a412515d216adc584f59606d7a6e2295648f4 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Mon, 26 Aug 2024 17:24:33 +0000 Subject: [PATCH 15/18] Tidy --- ERCS/erc-7760.md | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/ERCS/erc-7760.md b/ERCS/erc-7760.md index 1038684b0a..c7be2dd36a 100644 --- a/ERCS/erc-7760.md +++ b/ERCS/erc-7760.md @@ -78,37 +78,29 @@ As it is beneficial to install the factory at a vanity address with leading zero Runtime bytecode (20-byte factory address subvariant): ``` -3d3d3373________________________________________14605757363d3d37363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6052573d6000fd5b3d6000f35b3d356020355560408036111560525736038060403d373d3d355af43d6000803e6052573d6000fd +3d3d3373{ 20-BYTE FACTORY ADDRESS }14605757363d3d37363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6052573d6000fd5b3d6000f35b3d356020355560408036111560525736038060403d373d3d355af43d6000803e6052573d6000fd ``` -where `________________________________________` is the 20-byte factory address. - Runtime bytecode (14-byte factory address subvariant): ``` -3d3d336d____________________________14605157363d3d37363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e604c573d6000fd5b3d6000f35b3d3560203555604080361115604c5736038060403d373d3d355af43d6000803e604c573d6000fd +3d3d336d{ 14-BYTE FACTORY ADDRESS }14605157363d3d37363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e604c573d6000fd5b3d6000f35b3d3560203555604080361115604c5736038060403d373d3d355af43d6000803e604c573d6000fd ``` -where `____________________________` is the 14-byte factory address. - #### Minimal ERC-1967 transparent upgradeable proxy (I-variant) Runtime bytecode (20-byte factory address subvariant): ``` -3658146083573d3d3373________________________________________14605D57363d3d37363D7f360894a13ba1A3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6058573d6000fd5b3d6000f35b3d35602035556040360380156058578060403d373d3d355af43d6000803e6058573d6000fd5b602060293d393d51543d52593df3 +3658146083573d3d3373{ 20-BYTE FACTORY ADDRESS }14605D57363d3d37363D7f360894a13ba1A3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6058573d6000fd5b3d6000f35b3d35602035556040360380156058578060403d373d3d355af43d6000803e6058573d6000fd5b602060293d393d51543d52593df3 ``` -where `________________________________________` is the 20-byte factory address. - Runtime bytecode (14-byte factory address subvariant): ``` -365814607d573d3d336d____________________________14605757363d3D37363d7F360894A13Ba1A3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6052573d6000fd5b3d6000f35b3d35602035556040360380156052578060403d373d3d355af43d6000803e6052573d6000fd5b602060233d393d51543d52593df3 +365814607d573d3d336d{ 14-BYTE FACTORY ADDRESS }14605757363d3D37363d7F360894A13Ba1A3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6052573d6000fd5b3d6000f35b3d35602035556040360380156052578060403d373d3d355af43d6000803e6052573d6000fd5b602060233d393d51543d52593df3 ``` -where `____________________________` is the 14-byte factory address. - ### Minimal ERC-1967 UUPS proxy As this proxy does not contain upgrading logic, the initialization code MUST store the implementation at the ERC-1967 implementation storage slot `0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc`. From f284a24e8d25d0088179c2ed9a1f7abccd885ab2 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Mon, 26 Aug 2024 17:28:49 +0000 Subject: [PATCH 16/18] Tidy --- ERCS/erc-7760.md | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/ERCS/erc-7760.md b/ERCS/erc-7760.md index c7be2dd36a..1038684b0a 100644 --- a/ERCS/erc-7760.md +++ b/ERCS/erc-7760.md @@ -78,29 +78,37 @@ As it is beneficial to install the factory at a vanity address with leading zero Runtime bytecode (20-byte factory address subvariant): ``` -3d3d3373{ 20-BYTE FACTORY ADDRESS }14605757363d3d37363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6052573d6000fd5b3d6000f35b3d356020355560408036111560525736038060403d373d3d355af43d6000803e6052573d6000fd +3d3d3373________________________________________14605757363d3d37363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6052573d6000fd5b3d6000f35b3d356020355560408036111560525736038060403d373d3d355af43d6000803e6052573d6000fd ``` +where `________________________________________` is the 20-byte factory address. + Runtime bytecode (14-byte factory address subvariant): ``` -3d3d336d{ 14-BYTE FACTORY ADDRESS }14605157363d3d37363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e604c573d6000fd5b3d6000f35b3d3560203555604080361115604c5736038060403d373d3d355af43d6000803e604c573d6000fd +3d3d336d____________________________14605157363d3d37363d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e604c573d6000fd5b3d6000f35b3d3560203555604080361115604c5736038060403d373d3d355af43d6000803e604c573d6000fd ``` +where `____________________________` is the 14-byte factory address. + #### Minimal ERC-1967 transparent upgradeable proxy (I-variant) Runtime bytecode (20-byte factory address subvariant): ``` -3658146083573d3d3373{ 20-BYTE FACTORY ADDRESS }14605D57363d3d37363D7f360894a13ba1A3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6058573d6000fd5b3d6000f35b3d35602035556040360380156058578060403d373d3d355af43d6000803e6058573d6000fd5b602060293d393d51543d52593df3 +3658146083573d3d3373________________________________________14605D57363d3d37363D7f360894a13ba1A3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6058573d6000fd5b3d6000f35b3d35602035556040360380156058578060403d373d3d355af43d6000803e6058573d6000fd5b602060293d393d51543d52593df3 ``` +where `________________________________________` is the 20-byte factory address. + Runtime bytecode (14-byte factory address subvariant): ``` -365814607d573d3d336d{ 14-BYTE FACTORY ADDRESS }14605757363d3D37363d7F360894A13Ba1A3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6052573d6000fd5b3d6000f35b3d35602035556040360380156052578060403d373d3d355af43d6000803e6052573d6000fd5b602060233d393d51543d52593df3 +365814607d573d3d336d____________________________14605757363d3D37363d7F360894A13Ba1A3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc545af43d6000803e6052573d6000fd5b3d6000f35b3d35602035556040360380156052578060403d373d3d355af43d6000803e6052573d6000fd5b602060233d393d51543d52593df3 ``` +where `____________________________` is the 14-byte factory address. + ### Minimal ERC-1967 UUPS proxy As this proxy does not contain upgrading logic, the initialization code MUST store the implementation at the ERC-1967 implementation storage slot `0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc`. From 331b42ca9708907d83cb5a547a079e05d20f80eb Mon Sep 17 00:00:00 2001 From: Vectorized Date: Mon, 26 Aug 2024 17:33:17 +0000 Subject: [PATCH 17/18] Edit ERC --- ERCS/erc-7760.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ERCS/erc-7760.md b/ERCS/erc-7760.md index 1038684b0a..90c32a7e23 100644 --- a/ERCS/erc-7760.md +++ b/ERCS/erc-7760.md @@ -71,8 +71,6 @@ abi.encodePacked( ) ``` -As it is beneficial to install the factory at a vanity address with leading zero bytes so that the proxy's bytecode can be optimized to be shorter, variants for 14-byte factory address are provided. - #### Minimal ERC-1967 transparent upgradeable proxy for (basic variant) Runtime bytecode (20-byte factory address subvariant): @@ -179,6 +177,12 @@ The upgrade logic takes in any 32 byte value and 32 byte storage slot. This is f We do not lose any security as the implementation can still modify any storage slot. +### 14-byte factory address subvariants + +It is beneficial to install the transparent upgradeable proxy factory at a vanity address with leading zero bytes so that the proxy's bytecode can be optimized to be shorter. + +A 14-byte factory address (i.e. 6 leading zero bytes) is chosen because it strikes a balance between mining costs and bytecode size. + ### I-variants The so-called "I-variants" contain logic that returns the implementation address baked into the proxy bytecode. From 0c1e818b0e2eb325e44192026ed7de60baf91d4b Mon Sep 17 00:00:00 2001 From: Vectorized Date: Mon, 26 Aug 2024 17:38:52 +0000 Subject: [PATCH 18/18] Add initialization code rationale --- ERCS/erc-7760.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ERCS/erc-7760.md b/ERCS/erc-7760.md index 90c32a7e23..50fb32b43a 100644 --- a/ERCS/erc-7760.md +++ b/ERCS/erc-7760.md @@ -205,6 +205,10 @@ This is to avoid compatibility and safety issues with other ERC standards that a The `EXTCODECOPY` opcode can be used to retrieve the immutable arguments. +### No fixed initialization code + +As long as the initialization code is able to initialize the relevant ERC-1967 implementation slot where needed (i.e. for the UUPS proxy and Beacon proxy), there is no need for additional requirements on the initialization code. + ### Out of scope topics The following topics are intentionally out of scope of this standard, as they can contain custom logic: