forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create protected storage on the CREATE2 opcode
Create the protected storage contract on the CREATE2 opcode. Add the create2.sol example that shows CREATE2 contract creation. Also, show that recreating a contract via CREATE2(including its associated protected storage) works and the address is the same deterministic one. For more information, please see: https://docs.soliditylang.org/en/v0.8.17/control-structures.html?highlight=create2#salted-contract-creations-create2
- Loading branch information
1 parent
cebcc47
commit daea23d
Showing
3 changed files
with
66 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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,50 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause-Clear | ||
|
||
pragma solidity >=0.7.0 <0.9.0; | ||
|
||
contract Contract { | ||
uint256 public value; | ||
|
||
constructor(uint256 v) { | ||
value = v; | ||
} | ||
|
||
function get() public view returns (uint256) { | ||
return value; | ||
} | ||
|
||
function destruct() public { | ||
selfdestruct(payable(tx.origin)); | ||
} | ||
} | ||
|
||
// Salt: 0x0102abcdef0102abcdef0102abcdef0102abcdef0102abcdef0102abcdefaaaa | ||
|
||
// Creates a member contract via the CREATE2 opcode. | ||
contract Factory { | ||
Contract public c; | ||
bytes32 public original_salt; | ||
uint256 public original_value; | ||
address public original_address; | ||
|
||
constructor(bytes32 salt, uint256 value) { | ||
c = new Contract{salt: salt}(value); | ||
original_salt = salt; | ||
original_value = value; | ||
original_address = address(c); | ||
} | ||
|
||
function get() public view returns (uint256) { | ||
return c.get(); | ||
} | ||
|
||
function destruct() public { | ||
c.destruct(); | ||
} | ||
|
||
// After `destruct()` is called, `recreate()` must succeed and `c` must have the same address as before. | ||
function recreate() public { | ||
c = new Contract{salt: original_salt}(original_value); | ||
require(original_address == address(c)); | ||
} | ||
} |
This file contains 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