forked from dragonfly-xyz/useful-solidity-patterns
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FactoryProofs.t.sol
133 lines (118 loc) · 3.99 KB
/
FactoryProofs.t.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.17;
import "forge-std/Test.sol";
import "../patterns/factory-proofs/FactoryProofs.sol";
import "./TestUtils.sol";
contract FactoryProofsTest is Test, TestUtils {
Deployer deployer = new Deployer();
FactoryProofs proofs = new FactoryProofs();
function test_verifyCreate1() external {
address deployed = deployer.deploy1();
assertTrue(
proofs.verifyDeployedBy(deployed, address(deployer), uint32(deployer.lastNonce()))
);
}
function test_verifyCreate1_multiple() external {
for (uint256 i = 0; i < 10; ++i) {
address deployed = deployer.deploy1();
assertTrue(
proofs.verifyDeployedBy(deployed, address(deployer), uint32(deployer.lastNonce()))
);
}
}
function test_verifyCreate1_wrongNonceFails() external {
address deployed = deployer.deploy1();
assertFalse(
proofs.verifyDeployedBy(deployed, address(deployer), uint32(_randomUint256()))
);
}
function test_verifyCreate2() external {
bytes32 salt = _randomBytes32();
address deployed = deployer.deploy2(salt);
assertTrue(
proofs.verifySaltedDeployedBy(
deployed,
address(deployer),
keccak256(type(NewContract).creationCode),
salt
)
);
}
function test_verifyCreate2_multiple() external {
for (uint256 i = 0; i < 10; ++i) {
bytes32 salt = _randomBytes32();
address deployed = deployer.deploy2(salt);
assertTrue(
proofs.verifySaltedDeployedBy(
deployed,
address(deployer),
keccak256(type(NewContract).creationCode),
salt
)
);
}
}
function test_verifyCreate2_wrongSaltFails() external {
bytes32 salt = _randomBytes32();
address deployed = deployer.deploy2(salt);
assertFalse(
proofs.verifySaltedDeployedBy(
deployed,
address(deployer),
keccak256(type(NewContract).creationCode),
_randomBytes32()
)
);
}
function test_verifyCreate2_wrongInitCodeHashFails() external {
bytes32 salt = _randomBytes32();
address deployed = deployer.deploy2(salt);
assertFalse(
proofs.verifySaltedDeployedBy(
deployed,
address(deployer),
_randomBytes32(),
salt
)
);
}
function test_verifyCreate1AndCreate2() external {
address deployed = deployer.deploy1();
assertTrue(
proofs.verifyDeployedBy(deployed, address(deployer), uint32(deployer.lastNonce()))
);
bytes32 salt = _randomBytes32();
deployed = deployer.deploy2(salt);
assertTrue(
proofs.verifySaltedDeployedBy(
deployed,
address(deployer),
keccak256(type(NewContract).creationCode),
salt
)
);
}
function test_verifyCreate1_EOA() external {
address eoaDeployer = _randomAddress();
vm.prank(eoaDeployer, eoaDeployer);
address deployed = address(new NewContract());
assertTrue(
proofs.verifyDeployedBy(deployed, address(eoaDeployer), 0)
);
}
}
// The contract to deploy.
contract NewContract {}
// The contract doing the deploying.
contract Deployer {
// All contracts start with a nonce of 1, which goes up
// each time it deploys a new contract via create().
uint256 public lastNonce = 0;
function deploy1() external returns (address deployed) {
++lastNonce;
return address(new NewContract());
}
function deploy2(bytes32 salt) external returns (address deployed) {
return address(new NewContract{salt: salt}());
}
}