-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidatorFactory.t.sol
More file actions
323 lines (274 loc) · 13.5 KB
/
ValidatorFactory.t.sol
File metadata and controls
323 lines (274 loc) · 13.5 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// SPDX-License-Identifier: MIT
// solhint-disable use-natspec
pragma solidity =0.8.30;
import {Test} from "forge-std/Test.sol";
import {UpgradeableBeacon} from "@openzeppelin/contracts/proxy/beacon/UpgradeableBeacon.sol";
import {BeaconProxy} from "@openzeppelin/contracts/proxy/beacon/BeaconProxy.sol";
import {Create2} from "@openzeppelin/contracts/utils/Create2.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import {CommonTypes} from "filecoin-solidity/v0.8/types/CommonTypes.sol";
import {ValidatorFactory} from "../src/ValidatorFactory.sol";
import {Validator} from "../src/Validator.sol";
import {IAccessControl} from "@openzeppelin/contracts/access/IAccessControl.sol";
import {PoRepMarketMock} from "./contracts/PoRepMarketMock.sol";
import {SLITypes} from "../src/types/SLITypes.sol";
import {PoRepTypes} from "../src/types/PoRepTypes.sol";
contract ValidatorFactoryTest is Test {
ValidatorFactory public factory;
address public validatorAddress;
address public admin;
address public poRepService;
address public filecoinPay;
address public sliScorer;
address public clientSmartContract;
address public poRepMarket;
address public spRegistry;
address public client;
uint256 public dealId;
CommonTypes.FilActorId public provider;
ValidatorFactory public factoryImpl;
bytes public initData;
PoRepMarketMock public poRepMarketMock;
function setUp() public {
admin = vm.addr(1);
poRepService = vm.addr(2);
filecoinPay = vm.addr(3);
sliScorer = vm.addr(4);
clientSmartContract = vm.addr(5);
spRegistry = vm.addr(6);
poRepMarketMock = new PoRepMarketMock();
poRepMarket = address(poRepMarketMock);
client = vm.addr(6);
dealId = 1;
provider = CommonTypes.FilActorId.wrap(1);
validatorAddress = address(new Validator());
factoryImpl = new ValidatorFactory();
poRepMarketMock.setDealProposal(
dealId,
PoRepTypes.DealProposal({
dealId: dealId,
client: client,
provider: provider,
requirements: SLITypes.SLIThresholds({
retrievabilityBps: 80, bandwidthMbps: 500, latencyMs: 200, indexingPct: 90
}),
terms: SLITypes.DealTerms({dealSizeBytes: 1_000_000, pricePerSector: 100, durationDays: 365}),
validator: vm.addr(10),
state: PoRepTypes.DealState.Accepted,
railId: 200,
manifestLocation: "https://example.com/manifest"
})
);
initData = abi.encodeCall(ValidatorFactory.initialize, (admin, validatorAddress));
factory = ValidatorFactory(address(new ERC1967Proxy(address(factoryImpl), initData)));
vm.prank(admin);
factory.initialize2(poRepService, filecoinPay, sliScorer, clientSmartContract, poRepMarket, spRegistry);
}
function testEmitsUpgradedInConstructor() public {
vm.expectEmit(true, true, true, true);
emit UpgradeableBeacon.Upgraded(validatorAddress);
new ERC1967Proxy(address(factoryImpl), initData);
}
function testDeployEmitsEvent() public {
vm.expectEmit(true, true, true, true);
address expectedProxy = computeProxyAddress(admin, dealId);
emit ValidatorFactory.ProxyCreated(expectedProxy, dealId);
vm.prank(client);
factory.create(dealId);
assertTrue(factory.isValidatorContract(expectedProxy));
}
function testDeployMarksProxyAsDeployed() public {
address expectedProxy = computeProxyAddress(admin, dealId);
vm.prank(client);
factory.create(dealId);
assertTrue(factory.getInstance(dealId) == expectedProxy);
}
function testDeployRevertsIfInstanceExists() public {
vm.prank(client);
factory.create(dealId);
vm.expectRevert(abi.encodeWithSelector(ValidatorFactory.InstanceAlreadyExists.selector));
vm.prank(client);
factory.create(dealId);
}
function computeProxyAddress(address admin_, uint256 dealId_) private view returns (address) {
bytes memory initCode = abi.encodePacked(
type(BeaconProxy).creationCode,
abi.encode(
address(factory.getBeacon()),
abi.encodeCall(
Validator.initialize,
(
admin_,
poRepService,
filecoinPay,
sliScorer,
clientSmartContract,
poRepMarket,
spRegistry,
dealId_
)
)
)
);
bytes32 salt = keccak256(abi.encode(admin_, dealId_));
bytes32 bytecodeHash = keccak256(initCode);
return Create2.computeAddress(salt, bytecodeHash, address(factory));
}
function testAuthorizeUpgradeRevert() public {
address newImpl = address(new ValidatorFactory());
address unauthorized = vm.addr(999);
bytes32 upgraderRole = factory.UPGRADER_ROLE();
vm.prank(unauthorized);
vm.expectRevert(
abi.encodeWithSelector(IAccessControl.AccessControlUnauthorizedAccount.selector, unauthorized, upgraderRole)
);
factory.upgradeToAndCall(newImpl, "");
}
function testShouldReturnFalseIfValidatorDoesNotExist() public view {
assertFalse(factory.isValidatorContract(address(0)));
}
function testShouldRevertWhenIncorrectClientAddress() public {
address incorrectClient = vm.addr(999);
vm.expectRevert(abi.encodeWithSelector(ValidatorFactory.InvalidClientAddress.selector));
vm.prank(incorrectClient);
factory.create(dealId);
}
function testInitialize2RevertsWhenPoRepServiceIsZero() public {
ValidatorFactory f = ValidatorFactory(address(new ERC1967Proxy(address(factoryImpl), initData)));
vm.expectRevert(abi.encodeWithSelector(ValidatorFactory.InvalidPoRepServiceAddress.selector));
vm.prank(admin);
f.initialize2(address(0), filecoinPay, sliScorer, clientSmartContract, poRepMarket, spRegistry);
}
function testInitialize2RevertsWhenFilecoinPayIsZero() public {
ValidatorFactory f = ValidatorFactory(address(new ERC1967Proxy(address(factoryImpl), initData)));
vm.expectRevert(abi.encodeWithSelector(ValidatorFactory.InvalidFilecoinPayAddress.selector));
vm.prank(admin);
f.initialize2(poRepService, address(0), sliScorer, clientSmartContract, poRepMarket, spRegistry);
}
function testInitialize2RevertsWhenSliScorerIsZero() public {
ValidatorFactory f = ValidatorFactory(address(new ERC1967Proxy(address(factoryImpl), initData)));
vm.expectRevert(abi.encodeWithSelector(ValidatorFactory.InvalidSliScorerAddress.selector));
vm.prank(admin);
f.initialize2(poRepService, filecoinPay, address(0), clientSmartContract, poRepMarket, spRegistry);
}
function testInitialize2RevertsWhenClientSmartContractIsZero() public {
ValidatorFactory f = ValidatorFactory(address(new ERC1967Proxy(address(factoryImpl), initData)));
vm.expectRevert(abi.encodeWithSelector(ValidatorFactory.InvalidClientSmartContractAddress.selector));
vm.prank(admin);
f.initialize2(poRepService, filecoinPay, sliScorer, address(0), poRepMarket, spRegistry);
}
function testInitialize2RevertsWhenPoRepMarketIsZero() public {
ValidatorFactory f = ValidatorFactory(address(new ERC1967Proxy(address(factoryImpl), initData)));
vm.expectRevert(abi.encodeWithSelector(ValidatorFactory.InvalidPoRepMarketAddress.selector));
vm.prank(admin);
f.initialize2(poRepService, filecoinPay, sliScorer, clientSmartContract, address(0), spRegistry);
}
function testInitialize2RevertsWhenSPRegistryIsZero() public {
ValidatorFactory f = ValidatorFactory(address(new ERC1967Proxy(address(factoryImpl), initData)));
vm.expectRevert(abi.encodeWithSelector(ValidatorFactory.InvalidSPRegistryAddress.selector));
vm.prank(admin);
f.initialize2(poRepService, filecoinPay, sliScorer, clientSmartContract, poRepMarket, address(0));
}
function testInitialize2RevertsWhenNotAdmin() public {
ValidatorFactory f = ValidatorFactory(address(new ERC1967Proxy(address(factoryImpl), initData)));
vm.expectRevert(
abi.encodeWithSelector(
IAccessControl.AccessControlUnauthorizedAccount.selector, client, f.DEFAULT_ADMIN_ROLE()
)
);
vm.prank(client);
f.initialize2(poRepService, filecoinPay, sliScorer, clientSmartContract, poRepMarket, spRegistry);
}
function testInitializeRevertsWhenAdminIsZero() public {
address zeroAddress = address(0);
address validImplementation = address(0x1234);
ValidatorFactory factoryImplementation = new ValidatorFactory();
vm.expectRevert(ValidatorFactory.InvalidAdminAddress.selector);
factoryImplementation.initialize(zeroAddress, validImplementation);
}
function testInitializeRevertsWhenImplementationIsZero() public {
address validAdmin = address(0x1234);
address zeroAddress = address(0);
ValidatorFactory factoryImplementation = new ValidatorFactory();
vm.expectRevert(ValidatorFactory.InvalidImplementationAddress.selector);
factoryImplementation.initialize(validAdmin, zeroAddress);
}
function testSetAdminRevertsWhenNewAdminIsZero() public {
Validator validatorImplementation = new Validator();
ValidatorFactory factoryImplementation = new ValidatorFactory();
address validAdmin = address(0xABCD);
factoryImplementation.initialize(validAdmin, address(validatorImplementation));
vm.prank(validAdmin);
vm.expectRevert(ValidatorFactory.InvalidNewAdminAddress.selector);
factoryImplementation.setAdmin(address(0));
}
function testSetsNewAdminCorrectly() public {
Validator validatorImplementation = new Validator();
ValidatorFactory factoryImplementation = new ValidatorFactory();
address validAdmin = address(0xABCD);
address newValidAdmin = address(0xDCBA);
factoryImplementation.initialize(validAdmin, address(validatorImplementation));
vm.prank(validAdmin);
factoryImplementation.setAdmin(newValidAdmin);
assertTrue(factoryImplementation.hasRole(factoryImplementation.DEFAULT_ADMIN_ROLE(), newValidAdmin));
}
function testGrantRoleReverts() public {
bytes32 adminRole = factory.DEFAULT_ADMIN_ROLE();
vm.prank(admin);
vm.expectRevert(abi.encodeWithSelector(ValidatorFactory.RoleManagementDisabled.selector));
factory.grantRole(adminRole, client);
}
function testRevokeRoleReverts() public {
bytes32 adminRole = factory.DEFAULT_ADMIN_ROLE();
vm.prank(admin);
vm.expectRevert(abi.encodeWithSelector(ValidatorFactory.RoleManagementDisabled.selector));
factory.revokeRole(adminRole, client);
}
function testRenounceRoleReverts() public {
bytes32 adminRole = factory.DEFAULT_ADMIN_ROLE();
vm.prank(admin);
vm.expectRevert(abi.encodeWithSelector(ValidatorFactory.RoleManagementDisabled.selector));
factory.renounceRole(adminRole, admin);
}
function testSetUpgraderRoleRevertsWhenNewUpgraderRoleIsZero() public {
Validator validatorImplementation = new Validator();
ValidatorFactory factoryImplementation = new ValidatorFactory();
address validAdmin = address(0xABCD);
factoryImplementation.initialize(validAdmin, address(validatorImplementation));
vm.prank(validAdmin);
vm.expectRevert(ValidatorFactory.InvalidNewUpgraderRoleAddress.selector);
factoryImplementation.setUpgraderRole(address(0));
}
function testSetsNewUpgraderRoleCorrectly() public {
Validator validatorImplementation = new Validator();
ValidatorFactory factoryImplementation = new ValidatorFactory();
address validAdmin = address(0xABCD);
address newUpgrader = address(0xDCBA);
factoryImplementation.initialize(validAdmin, address(validatorImplementation));
vm.prank(validAdmin);
factoryImplementation.setUpgraderRole(newUpgrader);
assertTrue(factoryImplementation.hasRole(factoryImplementation.UPGRADER_ROLE(), newUpgrader));
}
function testSetAdminEmitsAdminChangedEvent() public {
Validator validatorImplementation = new Validator();
ValidatorFactory factoryImplementation = new ValidatorFactory();
address validAdmin = address(0xABCD);
address newValidAdmin = address(0xDCBA);
factoryImplementation.initialize(validAdmin, address(validatorImplementation));
vm.expectEmit(true, false, false, false);
emit ValidatorFactory.AdminChanged(newValidAdmin);
vm.prank(validAdmin);
factoryImplementation.setAdmin(newValidAdmin);
}
function testSetUpgraderRoleEmitsUpgraderRoleChangedEvent() public {
Validator validatorImplementation = new Validator();
ValidatorFactory factoryImplementation = new ValidatorFactory();
address validAdmin = address(0xABCD);
address newUpgrader = address(0xDCBA);
factoryImplementation.initialize(validAdmin, address(validatorImplementation));
vm.expectEmit(true, false, false, false);
emit ValidatorFactory.UpgraderRoleChanged(newUpgrader);
vm.prank(validAdmin);
factoryImplementation.setUpgraderRole(newUpgrader);
}
}