Skip to content

Commit

Permalink
♻️ Add Nicks to TestPlus (#1218)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized authored Dec 9, 2024
1 parent 297c952 commit 9339f25
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 50 deletions.
13 changes: 1 addition & 12 deletions test/ERC1271.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,8 @@ contract ERC1271Test is SoladyTest {
_erc6551V2 = address(new MockERC6551V2());
}

function _etchNicksFactory() internal returns (address nicksFactory) {
nicksFactory = 0x4e59b44847b379578588920cA78FbF26c0B4956C;
if (nicksFactory.code.length != 0) {
vm.etch(
nicksFactory,
hex"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe03601600081602082378035828234f58015156039578182fd5b8082525050506014600cf3"
);
}
}

function _etchBasefeeContract(bytes32 salt, bytes memory initcode) internal {
(bool success,) = _etchNicksFactory().call(abi.encodePacked(salt, initcode));
assertTrue(success);
_nicksCreate2(0, salt, initcode);
}

function _etchBasefeeContract() internal {
Expand Down
17 changes: 3 additions & 14 deletions test/SignatureCheckerLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -358,16 +358,6 @@ contract SignatureCheckerLibTest is SoladyTest {
assertEq(SignatureCheckerLib.isValidSignatureNow(signer, hash, signature), false);
}

function _etchNicksFactory() internal returns (address nicksFactory) {
nicksFactory = 0x4e59b44847b379578588920cA78FbF26c0B4956C;
if (nicksFactory.code.length != 0) {
vm.etch(
nicksFactory,
hex"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe03601600081602082378035828234f58015156039578182fd5b8082525050506014600cf3"
);
}
}

bytes32 private constant _ERC6492_DETECTION_SUFFIX =
0x6492649264926492649264926492649264926492649264926492649264926492;

Expand All @@ -388,7 +378,7 @@ contract SignatureCheckerLibTest is SoladyTest {
}

function _erc6492TestTemps() internal returns (_ERC6492TestTemps memory t) {
t.factory = _etchNicksFactory();
t.factory = _NICKS_FACTORY;
assertGt(t.factory.code.length, 0);
(t.eoa, t.privateKey) = _randomSigner();
t.initcode = abi.encodePacked(type(MockERC1271Wallet).creationCode, uint256(uint160(t.eoa)));
Expand Down Expand Up @@ -536,12 +526,11 @@ contract SignatureCheckerLibTest is SoladyTest {
_ERC6492TestTemps memory t;
t.initcode =
hex"6040600b3d3960403df3fe36383d373d3d6020515160208051013d3d515af160203851516084018038385101606037303452813582523838523490601c34355afa34513060e01b141634fd";
t.factory = _etchNicksFactory();
t.factory = _NICKS_FACTORY;
t.salt = 0x000000000000000000000000000000000000000068f35e1510740001fd13984a;
(bool success,) = t.factory.call(abi.encodePacked(t.salt, t.initcode));
revertingVerifier =
LibClone.predictDeterministicAddress(keccak256(t.initcode), t.salt, t.factory);
assertTrue(success);
assertEq(_nicksCreate2(0, t.salt, t.initcode), revertingVerifier);
assertGt(revertingVerifier.code.length, 0);
emit LogBytes32(keccak256(t.initcode));
emit LogBytes(revertingVerifier.code);
Expand Down
87 changes: 63 additions & 24 deletions test/utils/TestPlus.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ contract TestPlus is Brutalizer {
event LogBoolArray(string name, bool[] value);
event LogBoolArray(bool[] value);

/// @dev Canonical address of Nick's factory.
address internal constant _NICKS_FACTORY = 0x4e59b44847b379578588920cA78FbF26c0B4956C;

/// @dev The bytecode of Nick's factory.
bytes internal constant _NICKS_FACTORY_BYTECODE =
hex"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe03601600081602082378035828234f58015156039578182fd5b8082525050506014600cf3";

/// @dev Canonical address of 0age's immutable create 2 factory.
address internal constant _IMMUTABLE_CREATE2_FACTORY =
0x0000000000FFe8B47B3e2130213B802212439497;

/// @dev The bytecode of 0age's immutable create 2 factory.
bytes internal constant _IMMUTABLE_CREATE2_FACTORY_BYTECODE =
hex"60806040526004361061003f5760003560e01c806308508b8f1461004457806364e030871461009857806385cf97ab14610138578063a49a7c90146101bc575b600080fd5b34801561005057600080fd5b506100846004803603602081101561006757600080fd5b503573ffffffffffffffffffffffffffffffffffffffff166101ec565b604080519115158252519081900360200190f35b61010f600480360360408110156100ae57600080fd5b813591908101906040810160208201356401000000008111156100d057600080fd5b8201836020820111156100e257600080fd5b8035906020019184600183028401116401000000008311171561010457600080fd5b509092509050610217565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b34801561014457600080fd5b5061010f6004803603604081101561015b57600080fd5b8135919081019060408101602082013564010000000081111561017d57600080fd5b82018360208201111561018f57600080fd5b803590602001918460018302840111640100000000831117156101b157600080fd5b509092509050610592565b3480156101c857600080fd5b5061010f600480360360408110156101df57600080fd5b508035906020013561069e565b73ffffffffffffffffffffffffffffffffffffffff1660009081526020819052604090205460ff1690565b600083606081901c33148061024c57507fffffffffffffffffffffffffffffffffffffffff0000000000000000000000008116155b6102a1576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260458152602001806107746045913960600191505060405180910390fd5b606084848080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920182905250604051855195965090943094508b93508692506020918201918291908401908083835b6020831061033557805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe090920191602091820191016102f8565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff018019909216911617905260408051929094018281037fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe00183528085528251928201929092207fff000000000000000000000000000000000000000000000000000000000000008383015260609890981b7fffffffffffffffffffffffffffffffffffffffff00000000000000000000000016602183015260358201969096526055808201979097528251808203909701875260750182525084519484019490942073ffffffffffffffffffffffffffffffffffffffff81166000908152938490529390922054929350505060ff16156104a7576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252603f815260200180610735603f913960400191505060405180910390fd5b81602001825188818334f5955050508073ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff161461053a576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260468152602001806107b96046913960600191505060405180910390fd5b50505073ffffffffffffffffffffffffffffffffffffffff8116600090815260208190526040902080547fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff001660011790559392505050565b6000308484846040516020018083838082843760408051919093018181037fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe001825280845281516020928301207fff000000000000000000000000000000000000000000000000000000000000008383015260609990991b7fffffffffffffffffffffffffffffffffffffffff000000000000000000000000166021820152603581019790975260558088019890985282518088039098018852607590960182525085519585019590952073ffffffffffffffffffffffffffffffffffffffff81166000908152948590529490932054939450505060ff909116159050610697575060005b9392505050565b604080517fff000000000000000000000000000000000000000000000000000000000000006020808301919091523060601b6021830152603582018590526055808301859052835180840390910181526075909201835281519181019190912073ffffffffffffffffffffffffffffffffffffffff81166000908152918290529190205460ff161561072e575060005b9291505056fe496e76616c696420636f6e7472616374206372656174696f6e202d20636f6e74726163742068617320616c7265616479206265656e206465706c6f7965642e496e76616c69642073616c74202d206669727374203230206279746573206f66207468652073616c74206d757374206d617463682063616c6c696e6720616464726573732e4661696c656420746f206465706c6f7920636f6e7472616374207573696e672070726f76696465642073616c7420616e6420696e697469616c697a6174696f6e20636f64652ea265627a7a723058202bdc55310d97c4088f18acf04253db593f0914059f0c781a9df3624dcef0d1cf64736f6c634300050a0032";

/// @dev `address(bytes20(uint160(uint256(keccak256("hevm cheat code")))))`.
address private constant _VM_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D;

Expand Down Expand Up @@ -652,35 +667,59 @@ contract TestPlus is Brutalizer {
}
}

/// @dev Deploys a contract via 0age's immutable create 2 factory for testing.
function _safeCreate2(uint256 payableAmount, bytes32 salt, bytes memory initializationCode)
/// @dev Etches bytecode onto the target.
function __etch(address target, bytes memory bytecode) private {
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40)
mstore(m, 0xb4d6c782) // `etch(address,bytes)`.
mstore(add(m, 0x20), target)
mstore(add(m, 0x40), 0x40)
let n := mload(bytecode)
mstore(add(m, 0x60), n)
// prettier-ignore
for { let i := 0 } lt(i, n) { i := add(0x20, i) } {
mstore(add(add(m, 0x80), i), mload(add(add(bytecode, 0x20), i)))
}
pop(call(gas(), _VM_ADDRESS, 0, add(m, 0x1c), add(n, 0x64), 0x00, 0x00))
}
}

/// @dev Returns if the `target` has code.
function __hasCode(address target) private view returns (bool result) {
/// @solidity memory-safe-assembly
assembly {
result := iszero(iszero(extcodesize(target)))
}
}

/// @dev Deploys a contract via Nick's factory for testing.
function _nicksCreate2(uint256 payableAmount, bytes32 salt, bytes memory initializationCode)
internal
returns (address deploymentAddress)
{
// Canonical address of 0age's immutable create 2 factory.
address c2f = 0x0000000000FFe8B47B3e2130213B802212439497;
uint256 c2fCodeLength;
address f = _NICKS_FACTORY;
if (!__hasCode(f)) __etch(f, _NICKS_FACTORY_BYTECODE);
/// @solidity memory-safe-assembly
assembly {
c2fCodeLength := extcodesize(c2f)
}
if (c2fCodeLength == 0) {
bytes memory ic2fBytecode =
hex"60806040526004361061003f5760003560e01c806308508b8f1461004457806364e030871461009857806385cf97ab14610138578063a49a7c90146101bc575b600080fd5b34801561005057600080fd5b506100846004803603602081101561006757600080fd5b503573ffffffffffffffffffffffffffffffffffffffff166101ec565b604080519115158252519081900360200190f35b61010f600480360360408110156100ae57600080fd5b813591908101906040810160208201356401000000008111156100d057600080fd5b8201836020820111156100e257600080fd5b8035906020019184600183028401116401000000008311171561010457600080fd5b509092509050610217565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b34801561014457600080fd5b5061010f6004803603604081101561015b57600080fd5b8135919081019060408101602082013564010000000081111561017d57600080fd5b82018360208201111561018f57600080fd5b803590602001918460018302840111640100000000831117156101b157600080fd5b509092509050610592565b3480156101c857600080fd5b5061010f600480360360408110156101df57600080fd5b508035906020013561069e565b73ffffffffffffffffffffffffffffffffffffffff1660009081526020819052604090205460ff1690565b600083606081901c33148061024c57507fffffffffffffffffffffffffffffffffffffffff0000000000000000000000008116155b6102a1576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260458152602001806107746045913960600191505060405180910390fd5b606084848080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920182905250604051855195965090943094508b93508692506020918201918291908401908083835b6020831061033557805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe090920191602091820191016102f8565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff018019909216911617905260408051929094018281037fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe00183528085528251928201929092207fff000000000000000000000000000000000000000000000000000000000000008383015260609890981b7fffffffffffffffffffffffffffffffffffffffff00000000000000000000000016602183015260358201969096526055808201979097528251808203909701875260750182525084519484019490942073ffffffffffffffffffffffffffffffffffffffff81166000908152938490529390922054929350505060ff16156104a7576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252603f815260200180610735603f913960400191505060405180910390fd5b81602001825188818334f5955050508073ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff161461053a576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260468152602001806107b96046913960600191505060405180910390fd5b50505073ffffffffffffffffffffffffffffffffffffffff8116600090815260208190526040902080547fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff001660011790559392505050565b6000308484846040516020018083838082843760408051919093018181037fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe001825280845281516020928301207fff000000000000000000000000000000000000000000000000000000000000008383015260609990991b7fffffffffffffffffffffffffffffffffffffffff000000000000000000000000166021820152603581019790975260558088019890985282518088039098018852607590960182525085519585019590952073ffffffffffffffffffffffffffffffffffffffff81166000908152948590529490932054939450505060ff909116159050610697575060005b9392505050565b604080517fff000000000000000000000000000000000000000000000000000000000000006020808301919091523060601b6021830152603582018590526055808301859052835180840390910181526075909201835281519181019190912073ffffffffffffffffffffffffffffffffffffffff81166000908152918290529190205460ff161561072e575060005b9291505056fe496e76616c696420636f6e7472616374206372656174696f6e202d20636f6e74726163742068617320616c7265616479206265656e206465706c6f7965642e496e76616c69642073616c74202d206669727374203230206279746573206f66207468652073616c74206d757374206d617463682063616c6c696e6720616464726573732e4661696c656420746f206465706c6f7920636f6e7472616374207573696e672070726f76696465642073616c7420616e6420696e697469616c697a6174696f6e20636f64652ea265627a7a723058202bdc55310d97c4088f18acf04253db593f0914059f0c781a9df3624dcef0d1cf64736f6c634300050a0032";
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40)
mstore(m, 0xb4d6c782) // `etch(address,bytes)`.
mstore(add(m, 0x20), c2f)
mstore(add(m, 0x40), 0x40)
let n := mload(ic2fBytecode)
mstore(add(m, 0x60), n)
for { let i := 0 } lt(i, n) { i := add(0x20, i) } {
mstore(add(add(m, 0x80), i), mload(add(add(ic2fBytecode, 0x20), i)))
}
pop(call(gas(), _VM_ADDRESS, 0, add(m, 0x1c), add(n, 0x64), 0x00, 0x00))
let n := mload(initializationCode)
mstore(initializationCode, salt)
if iszero(call(gas(), f, payableAmount, initializationCode, add(n, 0x20), 0x00, 0x20)) {
returndatacopy(initializationCode, 0x00, returndatasize())
revert(initializationCode, returndatasize())
}
mstore(initializationCode, n) // Restore the length.
deploymentAddress := shr(96, mload(0x00))
}
}

/// @dev Deploys a contract via 0age's immutable create 2 factory for testing.
function _safeCreate2(uint256 payableAmount, bytes32 salt, bytes memory initializationCode)
internal
returns (address deploymentAddress)
{
address f = _IMMUTABLE_CREATE2_FACTORY;
if (!__hasCode(f)) __etch(f, _IMMUTABLE_CREATE2_FACTORY_BYTECODE);
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40)
Expand All @@ -693,8 +732,8 @@ contract TestPlus is Brutalizer {
for { let i := 0 } lt(i, n) { i := add(i, 0x20) } {
mstore(add(add(m, 0x80), i), mload(add(add(initializationCode, 0x20), i)))
}
if iszero(call(gas(), c2f, payableAmount, add(m, 0x1c), add(n, 0x64), m, 0x20)) {
returndatacopy(m, m, returndatasize())
if iszero(call(gas(), f, payableAmount, add(m, 0x1c), add(n, 0x64), m, 0x20)) {
returndatacopy(m, 0x00, returndatasize())
revert(m, returndatasize())
}
deploymentAddress := mload(m)
Expand Down

0 comments on commit 9339f25

Please sign in to comment.