Skip to content

Commit

Permalink
♻️ Flatten the trees
Browse files Browse the repository at this point in the history
Signed-off-by: Pascal Marco Caversaccio <[email protected]>
  • Loading branch information
pcaversaccio committed Oct 7, 2023
1 parent caad200 commit ca8429d
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 125 deletions.
36 changes: 18 additions & 18 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ CreateX_ComputeCreateAddress_1Arg_Public_Test:testFuzz_WhenTheNonceValueDoesNotE
CreateX_ComputeCreateAddress_1Arg_Public_Test:testFuzz_WhenTheNonceValueExceeds18446744073709551614(uint256) (runs: 256, μ: 37664, ~: 37659)
CreateX_ComputeCreateAddress_2Args_Public_Test:testFuzz_WhenTheNonceValueDoesNotExceed18446744073709551614(address,uint64) (runs: 256, μ: 2251522, ~: 2251527)
CreateX_ComputeCreateAddress_2Args_Public_Test:testFuzz_WhenTheNonceValueExceeds18446744073709551614(address,uint256) (runs: 256, μ: 37823, ~: 37801)
CreateX_DeployCreateAndInit_3Args_Public_Test:testFuzz_WhenTheContractCreationFails(uint64,(uint256,uint256)) (runs: 256, μ: 63117, ~: 63204)
CreateX_DeployCreateAndInit_3Args_Public_Test:testFuzz_WhenTheCreateXContractHasANonZeroBalanceAndWhenTheRefundTransactionIsSuccessful(uint64,(uint256,uint256),uint256) (runs: 256, μ: 1084185, ~: 1084411)
CreateX_DeployCreateAndInit_3Args_Public_Test:testFuzz_WhenTheCreatedRuntimeBytecodeHasANonZeroLengthAndWhenTheInitialisationCallIsSuccessful(uint64,(uint256,uint256)) (runs: 256, μ: 1027469, ~: 1027923)
CreateX_DeployCreateAndInit_3Args_Public_Test:testFuzz_WhenTheCreatedRuntimeBytecodeHasAZeroLength(uint64,(uint256,uint256)) (runs: 256, μ: 65251, ~: 65338)
CreateX_DeployCreateAndInit_3Args_Public_Test:testFuzz_WhenTheInitialisationCallIsUnsuccessful(uint64,(uint256,uint256)) (runs: 256, μ: 1005515, ~: 1005965)
CreateX_DeployCreateAndInit_3Args_Public_Test:testFuzz_WhenTheRefundTransactionIsUnsuccessful(uint64,(uint256,uint256),uint256) (runs: 256, μ: 1042067, ~: 1042248)
CreateX_DeployCreateAndInit_4Args_Public_Test:testFuzz_WhenTheContractCreationFails(uint64,(uint256,uint256)) (runs: 256, μ: 61068, ~: 61164)
CreateX_DeployCreateAndInit_4Args_Public_Test:testFuzz_WhenTheCreateXContractHasANonZeroBalanceAndWhenTheRefundTransactionIsSuccessful(uint64,(uint256,uint256),uint256) (runs: 256, μ: 1085468, ~: 1085742)
CreateX_DeployCreateAndInit_4Args_Public_Test:testFuzz_WhenTheCreatedRuntimeBytecodeHasANonZeroLengthAndWhenTheInitialisationCallIsSuccessful(uint64,(uint256,uint256)) (runs: 256, μ: 1026384, ~: 1026755)
CreateX_DeployCreateAndInit_4Args_Public_Test:testFuzz_WhenTheCreatedRuntimeBytecodeHasAZeroLength(uint64,(uint256,uint256)) (runs: 256, μ: 63107, ~: 63203)
CreateX_DeployCreateAndInit_4Args_Public_Test:testFuzz_WhenTheInitialisationCallIsUnsuccessful(uint64,(uint256,uint256)) (runs: 256, μ: 1003984, ~: 1004393)
CreateX_DeployCreateAndInit_4Args_Public_Test:testFuzz_WhenTheRefundTransactionIsUnsuccessful(uint64,(uint256,uint256),uint256) (runs: 256, μ: 1040646, ~: 1040782)
CreateX_DeployCreateAndInit_3Args_Public_Test:testFuzz_WhenTheCreateXContractHasANonZeroBalanceAndWhenTheRefundTransactionIsSuccessful(uint64,(uint256,uint256),uint256) (runs: 256, μ: 1083929, ~: 1084209)
CreateX_DeployCreateAndInit_3Args_Public_Test:testFuzz_WhenTheInitCodeFailsToDeployARuntimeBytecode(uint64,(uint256,uint256)) (runs: 256, μ: 63058, ~: 63182)
CreateX_DeployCreateAndInit_3Args_Public_Test:testFuzz_WhenTheInitCodeSuccessfullyCreatesARuntimeBytecodeWithANonZeroLengthAndWhenTheInitialisationCallIsSuccessful(uint64,(uint256,uint256)) (runs: 256, μ: 1027406, ~: 1027697)
CreateX_DeployCreateAndInit_3Args_Public_Test:testFuzz_WhenTheInitCodeSuccessfullyCreatesARuntimeBytecodeWithAZeroLength(uint64,(uint256,uint256)) (runs: 256, μ: 62957, ~: 63081)
CreateX_DeployCreateAndInit_3Args_Public_Test:testFuzz_WhenTheInitialisationCallIsUnsuccessful(uint64,(uint256,uint256)) (runs: 256, μ: 1005219, ~: 1005691)
CreateX_DeployCreateAndInit_3Args_Public_Test:testFuzz_WhenTheRefundTransactionIsUnsuccessful(uint64,(uint256,uint256),uint256) (runs: 256, μ: 1041915, ~: 1042057)
CreateX_DeployCreateAndInit_4Args_Public_Test:testFuzz_WhenTheCreateXContractHasANonZeroBalanceAndWhenTheRefundTransactionIsSuccessful(uint64,(uint256,uint256),uint256) (runs: 256, μ: 1085374, ~: 1085540)
CreateX_DeployCreateAndInit_4Args_Public_Test:testFuzz_WhenTheInitCodeFailsToDeployARuntimeBytecode(uint64,(uint256,uint256)) (runs: 256, μ: 61059, ~: 61142)
CreateX_DeployCreateAndInit_4Args_Public_Test:testFuzz_WhenTheInitCodeSuccessfullyCreatesARuntimeBytecodeWithANonZeroLengthAndWhenTheInitialisationCallIsSuccessful(uint64,(uint256,uint256)) (runs: 256, μ: 1026072, ~: 1026498)
CreateX_DeployCreateAndInit_4Args_Public_Test:testFuzz_WhenTheInitCodeSuccessfullyCreatesARuntimeBytecodeWithAZeroLength(uint64,(uint256,uint256)) (runs: 256, μ: 60863, ~: 60946)
CreateX_DeployCreateAndInit_4Args_Public_Test:testFuzz_WhenTheInitialisationCallIsUnsuccessful(uint64,(uint256,uint256)) (runs: 256, μ: 1003783, ~: 1004181)
CreateX_DeployCreateAndInit_4Args_Public_Test:testFuzz_WhenTheRefundTransactionIsUnsuccessful(uint64,(uint256,uint256),uint256) (runs: 256, μ: 1040454, ~: 1040591)
CreateX_DeployCreateClone_Public_Test:testFuzz_WhenTheEIP1167MinimalProxyContractCreationFails(uint64,uint256) (runs: 256, μ: 8937393460516730081, ~: 8937393460516730161)
CreateX_DeployCreateClone_Public_Test:testFuzz_WhenTheEIP1167MinimalProxyContractIsSuccessfullyCreatedAndWhenTheEIP1167MinimalProxyInitialisationCallIsSuccessful(uint64,uint256) (runs: 256, μ: 106429, ~: 106652)
CreateX_DeployCreateClone_Public_Test:testFuzz_WhenTheEIP1167MinimalProxyInitialisationCallIsUnsuccessful(uint64,uint256) (runs: 256, μ: 75252, ~: 75408)
CreateX_DeployCreate_Public_Test:testFuzz_WhenTheContractCreationFails(uint64,uint256) (runs: 256, μ: 53050, ~: 53028)
CreateX_DeployCreate_Public_Test:testFuzz_WhenTheCreatedRuntimeBytecodeHasANonZeroLength(uint64,uint256) (runs: 256, μ: 1012164, ~: 1012165)
CreateX_DeployCreate_Public_Test:testFuzz_WhenTheCreatedRuntimeBytecodeHasAZeroLength(uint64,uint256) (runs: 256, μ: 55194, ~: 55172)
CreateX_EfficientHash_Internal_Test:test_Requirements(bytes32,bytes32) (runs: 256, μ: 5968, ~: 5968)
CreateX_GenerateSalt_Internal_Test:testFuzz_NeverReverts(uint256) (runs: 256, μ: 20623, ~: 20623)
CreateX_GenerateSalt_Internal_Test:test_ShouldBeAFunctionOfMultipleBlockPropertiesAndTheCaller() (gas: 51805)
CreateX_DeployCreate_Public_Test:testFuzz_WhenTheInitCodeFailsToDeployARuntimeBytecode(uint64,uint256) (runs: 256, μ: 53015, ~: 53006)
CreateX_DeployCreate_Public_Test:testFuzz_WhenTheInitCodeSuccessfullyCreatesARuntimeBytecodeWithANonZeroLength(uint64,uint256) (runs: 256, μ: 1011818, ~: 1011908)
CreateX_DeployCreate_Public_Test:testFuzz_WhenTheInitCodeSuccessfullyCreatesARuntimeBytecodeWithAZeroLength(uint64,uint256) (runs: 256, μ: 52903, ~: 52894)
CreateX_EfficientHash_Internal_Test:testFuzz_MatchesTheOutputOfAHighLevelHashAndShouldNeverRevert(bytes32,bytes32) (runs: 256, μ: 5969, ~: 5969)
CreateX_GenerateSalt_Internal_Test:testFuzz_NeverReverts(uint256) (runs: 256, μ: 20650, ~: 20650)
CreateX_GenerateSalt_Internal_Test:testFuzz_ShouldBeAFunctionOfMultipleBlockPropertiesAndTheCaller(uint256,address,string,uint64,address) (runs: 256, μ: 54865, ~: 54620)
CreateX_Guard_Internal_Test:testFuzz_WhenTheFirst20BytesOfTheSaltDoNotEqualTheCallerOrTheZeroAddress(address,bytes32) (runs: 256, μ: 32472, ~: 32473)
CreateX_Guard_Internal_Test:testFuzz_WhenTheFirst20BytesOfTheSaltEqualsTheCallerAndWhenThe21stByteOfTheSaltEquals0x00(address,bytes32) (runs: 256, μ: 32539, ~: 32539)
CreateX_Guard_Internal_Test:testFuzz_WhenTheFirst20BytesOfTheSaltEqualsTheCallerAndWhenThe21stByteOfTheSaltEquals0x01(address,bytes32) (runs: 256, μ: 32479, ~: 32479)
Expand Down
2 changes: 1 addition & 1 deletion test/internal/CreateX._efficientHash.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ contract CreateX_EfficientHash_Internal_Test is BaseTest {
/* TESTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

function test_Requirements(bytes32 a, bytes32 b) external {
function testFuzz_MatchesTheOutputOfAHighLevelHashAndShouldNeverRevert(bytes32 a, bytes32 b) external {
// It should match the output of a high-level hash.
// It should never revert.
bytes32 expected = keccak256(abi.encodePacked(a, b));
Expand Down
23 changes: 16 additions & 7 deletions test/internal/CreateX._generateSalt.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ contract CreateX_GenerateSalt_Internal_Test is BaseTest {
/* TESTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

function test_ShouldBeAFunctionOfMultipleBlockPropertiesAndTheCaller() external {
function testFuzz_ShouldBeAFunctionOfMultipleBlockPropertiesAndTheCaller(
uint256 increment,
address coinbase,
string calldata prevrandao,
uint64 chainId,
address msgSender
) external {
// It should be a function of multiple block properties and the caller.
// The full set of dependencies is:
// - blockhash(block.number - 32),
Expand All @@ -34,46 +40,49 @@ contract CreateX_GenerateSalt_Internal_Test is BaseTest {
// - msg.sender.
// We test their dependencies by determining the current salt, changing any of those
// values, and verifying that the salt changes.
increment = bound(increment, 1, type(uint128).max);
uint256 snapshotId = vm.snapshot();
bytes32 originalSalt = createXHarness.exposed_generateSalt();

// Change block. Block number and hash are coupled, so we can't isolate this.
vm.roll(block.number + 1);
vm.roll(block.number + increment);
assertNotEq(originalSalt, createXHarness.exposed_generateSalt());

// Change coinbase.
vm.revertTo(snapshotId);
assertEq(createXHarness.exposed_generateSalt(), originalSalt);

vm.coinbase(makeAddr("new coinbase"));
vm.assume(coinbase != zeroAddress);
vm.coinbase(coinbase);
assertNotEq(originalSalt, createXHarness.exposed_generateSalt());

// Change timestamp.
vm.revertTo(snapshotId);
assertEq(createXHarness.exposed_generateSalt(), originalSalt);

vm.warp(block.timestamp + 1);
vm.warp(block.timestamp + increment);
assertNotEq(originalSalt, createXHarness.exposed_generateSalt());

// Change prevrandao.
vm.revertTo(snapshotId);
assertEq(createXHarness.exposed_generateSalt(), originalSalt);

vm.prevrandao("new prevrandao");
vm.prevrandao(keccak256(abi.encode(prevrandao)));
assertNotEq(originalSalt, createXHarness.exposed_generateSalt());

// Change chain ID.
vm.revertTo(snapshotId);
assertEq(createXHarness.exposed_generateSalt(), originalSalt);

vm.chainId(111222333);
vm.chainId(chainId);
assertNotEq(originalSalt, createXHarness.exposed_generateSalt());

// Change sender.
vm.revertTo(snapshotId);
assertEq(createXHarness.exposed_generateSalt(), originalSalt);

vm.startPrank(makeAddr("new sender"));
assumeAddressIsNot(msgSender, AddressType.ForgeAddress);
vm.startPrank(msgSender);
assertNotEq(originalSalt, createXHarness.exposed_generateSalt());
vm.stopPrank();
}
Expand Down
25 changes: 9 additions & 16 deletions test/public/CreateX.deployCreate.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,17 @@ contract CreateX_DeployCreate_Public_Test is BaseTest {
cachedInitCode = abi.encodePacked(type(ERC20MockPayable).creationCode, args);
}

modifier whenTheInitCodeCreatesAValidRuntimeBytecode() {
modifier whenTheInitCodeSuccessfullyCreatesARuntimeBytecodeWithANonZeroLength() {
if (cachedInitCode.length == 0) {
revert ZeroByteInitCode(SELF);
}
_;
}

modifier whenTheCreatedRuntimeBytecodeHasANonZeroLength() {
if (cachedInitCode.length == 0) {
revert ZeroByteInitCode(SELF);
}
_;
}

function testFuzz_WhenTheCreatedRuntimeBytecodeHasANonZeroLength(
function testFuzz_WhenTheInitCodeSuccessfullyCreatesARuntimeBytecodeWithANonZeroLength(
uint64 nonce,
uint256 msgValue
) external whenTheInitCodeCreatesAValidRuntimeBytecode whenTheCreatedRuntimeBytecodeHasANonZeroLength {
) external whenTheInitCodeSuccessfullyCreatesARuntimeBytecodeWithANonZeroLength {
vm.assume(nonce != 0 && nonce < type(uint64).max);
vm.setNonce(createXAddr, nonce);
msgValue = bound(msgValue, 0, type(uint64).max);
Expand All @@ -108,14 +101,14 @@ contract CreateX_DeployCreate_Public_Test is BaseTest {
assertEq(ERC20MockPayable(computedAddress).balanceOf(arg3), arg4);
}

modifier whenTheCreatedRuntimeBytecodeHasAZeroLength() {
modifier whenTheInitCodeSuccessfullyCreatesARuntimeBytecodeWithAZeroLength() {
_;
}

function testFuzz_WhenTheCreatedRuntimeBytecodeHasAZeroLength(
function testFuzz_WhenTheInitCodeSuccessfullyCreatesARuntimeBytecodeWithAZeroLength(
uint64 nonce,
uint256 msgValue
) external whenTheInitCodeCreatesAValidRuntimeBytecode whenTheCreatedRuntimeBytecodeHasAZeroLength {
) external whenTheInitCodeSuccessfullyCreatesARuntimeBytecodeWithAZeroLength {
vm.assume(nonce != 0 && nonce < type(uint64).max);
vm.setNonce(createXAddr, nonce);
msgValue = bound(msgValue, 0, type(uint64).max);
Expand All @@ -125,14 +118,14 @@ contract CreateX_DeployCreate_Public_Test is BaseTest {
createX.deployCreate{value: msgValue}(new bytes(0));
}

modifier whenTheContractCreationFails() {
modifier whenTheInitCodeFailsToDeployARuntimeBytecode() {
_;
}

function testFuzz_WhenTheContractCreationFails(
function testFuzz_WhenTheInitCodeFailsToDeployARuntimeBytecode(
uint64 nonce,
uint256 msgValue
) external whenTheContractCreationFails {
) external whenTheInitCodeFailsToDeployARuntimeBytecode {
vm.assume(nonce != 0 && nonce < type(uint64).max);
vm.setNonce(createXAddr, nonce);
msgValue = bound(msgValue, 0, type(uint64).max);
Expand Down
13 changes: 6 additions & 7 deletions test/public/CreateX.deployCreate.tree
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
CreateX_DeployCreate_Public_Test
├── When the initCode creates a valid runtime bytecode
│ ├── When the created runtime bytecode has a non-zero length
│ │ ├── It returns a contract address with a non-zero bytecode length and a potential non-zero ether balance.
│ │ └── It emits the event ContractCreation with the contract address as indexed argument.
│ └── When the created runtime bytecode has a zero length
│ └── It should revert.
└── When the contract creation fails
├── When the initCode successfully creates a runtime bytecode with a non-zero length
│ ├── It returns a contract address with a non-zero bytecode length and a potential non-zero ether balance.
│ └── It emits the event ContractCreation with the contract address as indexed argument.
├── When the initCode successfully creates a runtime bytecode with a zero length
│ └── It should revert.
└── When the initCode fails to deploy a runtime bytecode
└── It should revert.
35 changes: 12 additions & 23 deletions test/public/CreateX.deployCreateAndInit_3Args.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,7 @@ contract CreateX_DeployCreateAndInit_3Args_Public_Test is BaseTest {
cachedInitCode = abi.encodePacked(type(ERC20MockPayable).creationCode, args);
}

modifier whenTheInitCodeCreatesAValidRuntimeBytecode() {
if (cachedInitCode.length == 0) {
revert ZeroByteInitCode(SELF);
}
_;
}

modifier whenTheCreatedRuntimeBytecodeHasANonZeroLength() {
modifier whenTheInitCodeSuccessfullyCreatesARuntimeBytecodeWithANonZeroLength() {
if (cachedInitCode.length == 0) {
revert ZeroByteInitCode(SELF);
}
Expand All @@ -84,13 +77,12 @@ contract CreateX_DeployCreateAndInit_3Args_Public_Test is BaseTest {
_;
}

function testFuzz_WhenTheCreatedRuntimeBytecodeHasANonZeroLengthAndWhenTheInitialisationCallIsSuccessful(
function testFuzz_WhenTheInitCodeSuccessfullyCreatesARuntimeBytecodeWithANonZeroLengthAndWhenTheInitialisationCallIsSuccessful(
uint64 nonce,
CreateX.Values memory values
)
external
whenTheInitCodeCreatesAValidRuntimeBytecode
whenTheCreatedRuntimeBytecodeHasANonZeroLength
whenTheInitCodeSuccessfullyCreatesARuntimeBytecodeWithANonZeroLength
whenTheInitialisationCallIsSuccessful
{
vm.assume(nonce != 0 && nonce < type(uint64).max);
Expand Down Expand Up @@ -142,8 +134,7 @@ contract CreateX_DeployCreateAndInit_3Args_Public_Test is BaseTest {
uint256 amount
)
external
whenTheInitCodeCreatesAValidRuntimeBytecode
whenTheCreatedRuntimeBytecodeHasANonZeroLength
whenTheInitCodeSuccessfullyCreatesARuntimeBytecodeWithANonZeroLength
whenTheInitialisationCallIsSuccessful
whenTheCreateXContractHasANonZeroBalance(amount)
whenTheRefundTransactionIsSuccessful
Expand Down Expand Up @@ -193,8 +184,7 @@ contract CreateX_DeployCreateAndInit_3Args_Public_Test is BaseTest {
uint256 amount
)
external
whenTheInitCodeCreatesAValidRuntimeBytecode
whenTheCreatedRuntimeBytecodeHasANonZeroLength
whenTheInitCodeSuccessfullyCreatesARuntimeBytecodeWithANonZeroLength
whenTheInitialisationCallIsSuccessful
whenTheCreateXContractHasANonZeroBalance(amount)
whenTheRefundTransactionIsUnsuccessful
Expand Down Expand Up @@ -229,8 +219,7 @@ contract CreateX_DeployCreateAndInit_3Args_Public_Test is BaseTest {
CreateX.Values memory values
)
external
whenTheInitCodeCreatesAValidRuntimeBytecode
whenTheCreatedRuntimeBytecodeHasANonZeroLength
whenTheInitCodeSuccessfullyCreatesARuntimeBytecodeWithANonZeroLength
whenTheInitialisationCallIsUnsuccessful
{
vm.assume(nonce != 0 && nonce < type(uint64).max);
Expand All @@ -254,14 +243,14 @@ contract CreateX_DeployCreateAndInit_3Args_Public_Test is BaseTest {
vm.stopPrank();
}

modifier whenTheCreatedRuntimeBytecodeHasAZeroLength() {
modifier whenTheInitCodeSuccessfullyCreatesARuntimeBytecodeWithAZeroLength() {
_;
}

function testFuzz_WhenTheCreatedRuntimeBytecodeHasAZeroLength(
function testFuzz_WhenTheInitCodeSuccessfullyCreatesARuntimeBytecodeWithAZeroLength(
uint64 nonce,
CreateX.Values memory values
) external whenTheInitCodeCreatesAValidRuntimeBytecode whenTheCreatedRuntimeBytecodeHasAZeroLength {
) external whenTheInitCodeSuccessfullyCreatesARuntimeBytecodeWithAZeroLength {
vm.assume(nonce != 0 && nonce < type(uint64).max);
vm.setNonce(createXAddr, nonce);
values.constructorAmount = bound(values.constructorAmount, 0, type(uint64).max);
Expand All @@ -279,14 +268,14 @@ contract CreateX_DeployCreateAndInit_3Args_Public_Test is BaseTest {
vm.stopPrank();
}

modifier whenTheContractCreationFails() {
modifier whenTheInitCodeFailsToDeployARuntimeBytecode() {
_;
}

function testFuzz_WhenTheContractCreationFails(
function testFuzz_WhenTheInitCodeFailsToDeployARuntimeBytecode(
uint64 nonce,
CreateX.Values memory values
) external whenTheContractCreationFails {
) external whenTheInitCodeFailsToDeployARuntimeBytecode {
vm.assume(nonce != 0 && nonce < type(uint64).max);
vm.setNonce(createXAddr, nonce);
values.constructorAmount = bound(values.constructorAmount, 0, type(uint64).max);
Expand Down
Loading

0 comments on commit ca8429d

Please sign in to comment.