-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13533 from ethereum/evmhost-create
EVMHost: Calculate address for CREATE properly
- Loading branch information
Showing
15 changed files
with
114 additions
and
21 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
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
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
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
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
29 changes: 29 additions & 0 deletions
29
test/libsolidity/semanticTests/salted_create/prediction_example.sol
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,29 @@ | ||
contract D { | ||
uint public x; | ||
constructor(uint a) { | ||
x = a; | ||
} | ||
} | ||
|
||
contract C { | ||
function createDSalted(bytes32 salt, uint arg) public { | ||
address predictedAddress = address(uint160(uint(keccak256(abi.encodePacked( | ||
bytes1(0xff), | ||
address(this), | ||
salt, | ||
keccak256(abi.encodePacked( | ||
type(D).creationCode, | ||
arg | ||
)) | ||
))))); | ||
|
||
D d = new D{salt: salt}(arg); | ||
require(address(d) == predictedAddress, "Address mismatch."); | ||
} | ||
} | ||
// ==== | ||
// EVMVersion: >=constantinople | ||
// compileViaYul: also | ||
// ---- | ||
// createDSalted(bytes32,uint256): 42, 64 -> | ||
// gas legacy: 104365 |
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
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,39 @@ | ||
contract C { | ||
function addr() external returns (address) { | ||
return address(this); | ||
} | ||
|
||
function testRunner() external returns (address a1, address a2) { | ||
assembly { | ||
// This is `return(0, 1)`. We are using a simplified/fixed initcode to avoid | ||
// instability due to metadata changes. | ||
let initcode := hex"60016000f3" | ||
mstore(0, initcode) | ||
|
||
a1 := create(0, 0, 5) | ||
a2 := create2(0, 0, 5, address()) | ||
} | ||
} | ||
|
||
function testCalc() external returns (address a1, address a2) { | ||
a1 = calculateCreate(address(this), 1); | ||
a2 = calculateCreate2(address(this), keccak256(hex"60016000f3"), bytes32(uint256(uint160(address(this))))); | ||
} | ||
|
||
function calculateCreate(address from, uint256 nonce) private pure returns (address) { | ||
assert(nonce <= 127); | ||
bytes memory data = | ||
bytes.concat(hex"d694", bytes20(uint160(from)), nonce == 0 ? bytes1(hex"80") : bytes1(uint8(nonce))); | ||
return address(uint160(uint256(keccak256(data)))); // Take the lower 160-bits | ||
} | ||
|
||
function calculateCreate2(address creator, bytes32 codehash, bytes32 salt) private pure returns (address) { | ||
return address(uint160(uint256(keccak256(abi.encodePacked(bytes1(0xff), creator, salt, codehash))))); | ||
} | ||
} | ||
// ==== | ||
// EVMVersion: >=constantinople | ||
// ---- | ||
// addr() -> 0xc06afe3a8444fc0004668591e8306bfb9968e79e | ||
// testRunner() -> 0x137aa4dfc0911524504fcd4d98501f179bc13b4a, 0x2c1c30623ddd93e0b765a6caaca0c859eeb0644d | ||
// testCalc() -> 0x137aa4dfc0911524504fcd4d98501f179bc13b4a, 0x2c1c30623ddd93e0b765a6caaca0c859eeb0644d |
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