-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidatorFactory.sol
More file actions
291 lines (256 loc) · 11.1 KB
/
ValidatorFactory.sol
File metadata and controls
291 lines (256 loc) · 11.1 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
// SPDX-License-Identifier: MIT
// solhint-disable var-name-mixedcase
pragma solidity =0.8.30;
import {AccessControlUpgradeable} from "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
import {UpgradeableBeacon} from "@openzeppelin/contracts/proxy/beacon/UpgradeableBeacon.sol";
import {BeaconProxy} from "@openzeppelin/contracts/proxy/beacon/BeaconProxy.sol";
import {Validator} from "./Validator.sol";
import {Create2} from "@openzeppelin/contracts/utils/Create2.sol";
import {UUPSUpgradeable} from "@openzeppelin/contracts/proxy/utils/UUPSUpgradeable.sol";
import {PoRepMarket} from "./PoRepMarket.sol";
import {PoRepTypes} from "./types/PoRepTypes.sol";
/**
* @title ValidatorFactory
* @notice Beacon factory contract for creating Validator instances
*/
contract ValidatorFactory is UUPSUpgradeable, AccessControlUpgradeable {
/**
* @notice Upgradable role which allows for contract upgrades
*/
bytes32 public constant UPGRADER_ROLE = keccak256("UPGRADER_ROLE");
// @custom:storage-location erc7201:porepmarket.storage.ValidatorFactoryStorage
struct ValidatorFactoryStorage {
mapping(uint256 dealId => address contractAddress) _instances;
mapping(address => bool) _isValidatorContract;
address _clientSmartContract;
address _poRepService;
address _filecoinPay;
address _sliScorer;
address _poRepMarket;
address _SPRegistry;
address _beacon;
address _admin;
address _upgraderRole;
}
// keccak256(abi.encode(uint256(keccak256("porepmarket.storage.ValidatorFactoryStorage")) - 1)) & ~bytes32(uint256(0xff))
bytes32 private constant VALIDATOR_FACTORY_STORAGE_LOCATION =
0x4535768406d1af0f5a262f9968680cf180c0f29a04172a8e056d8f1b4b87ed00;
// solhint-disable-next-line use-natspec
function _getValidatorFactoryStorage() private pure returns (ValidatorFactoryStorage storage $) {
// solhint-disable-next-line no-inline-assembly
assembly {
$.slot := VALIDATOR_FACTORY_STORAGE_LOCATION
}
}
/**
* @dev Returns the storage struct for the ValidatorFactory contract.
* @notice function to allow acess to storage for inheriting contracts
* @return ValidatorFactoryStorage storage struct
*/
function s() internal pure returns (ValidatorFactoryStorage storage) {
return _getValidatorFactoryStorage();
}
error InstanceAlreadyExists();
error InvalidAdminAddress();
error InvalidClientAddress();
error InvalidPoRepMarketAddress();
error InvalidClientSmartContractAddress();
error InvalidFilecoinPayAddress();
error InvalidPoRepServiceAddress();
error InvalidSliScorerAddress();
error InvalidSPRegistryAddress();
error InvalidImplementationAddress();
error InvalidNewAdminAddress();
error InvalidNewUpgraderRoleAddress();
error RoleManagementDisabled();
/**
* @notice Emitted when a new proxy is successfully created
* @param proxy The address of the newly deployed proxy
* @param dealId The dealId for which the proxy was created
*/
event ProxyCreated(address indexed proxy, uint256 indexed dealId);
/**
* @notice Emitted when the admin is changed
* @param newAdmin The address of the new admin
*/
event AdminChanged(address indexed newAdmin);
/**
* @notice Emitted when the upgrader role is changed
* @param newUpgraderRole The address of the new upgrader role
*/
event UpgraderRoleChanged(address indexed newUpgraderRole);
/**
* @notice Initializes the contract
* @dev Initializes the contract by setting a default admin role and a UUPS upgradeable role
* @param admin The address of the admin responsible for the contract
* @param implementation The address of the implementation contract
*/
function initialize(address admin, address implementation) public initializer {
if (admin == address(0)) {
revert InvalidAdminAddress();
}
if (implementation == address(0)) {
revert InvalidImplementationAddress();
}
__AccessControl_init();
_grantRole(DEFAULT_ADMIN_ROLE, admin);
_grantRole(UPGRADER_ROLE, admin);
ValidatorFactoryStorage storage $ = s();
$._beacon = address(new UpgradeableBeacon(implementation, admin));
$._admin = admin;
$._upgraderRole = admin;
}
/**
* @notice Initializes the contract with the PoRepMarket, ClientSmartContract, and FilecoinPay addresses
* @dev This function is called after the contract is initialized with the admin and implementation addresses
* @param _poRepService The address of the PoRepService contract
* @param _filecoinPay The address of the FilecoinPay contract
* @param _sliScorer The address of the SLIScorer contract
* @param _clientSmartContract The address of the ClientSmartContract contract
* @param _poRepMarket The address of the PoRepMarket contract
* @param _SPRegistry The address of the SPRegistry contract
*/
function initialize2(
address _poRepService,
address _filecoinPay,
address _sliScorer,
address _clientSmartContract,
address _poRepMarket,
address _SPRegistry
) external reinitializer(2) onlyRole(DEFAULT_ADMIN_ROLE) {
if (_poRepService == address(0)) revert InvalidPoRepServiceAddress();
if (_poRepMarket == address(0)) revert InvalidPoRepMarketAddress();
if (_clientSmartContract == address(0)) revert InvalidClientSmartContractAddress();
if (_filecoinPay == address(0)) revert InvalidFilecoinPayAddress();
if (_sliScorer == address(0)) revert InvalidSliScorerAddress();
if (_SPRegistry == address(0)) revert InvalidSPRegistryAddress();
ValidatorFactoryStorage storage $ = s();
$._poRepMarket = _poRepMarket;
$._clientSmartContract = _clientSmartContract;
$._poRepService = _poRepService;
$._filecoinPay = _filecoinPay;
$._sliScorer = _sliScorer;
$._SPRegistry = _SPRegistry;
}
/**
* @notice Creates a new instance of an upgradeable contract.
* @dev Uses BeaconProxy to create a new proxy instance, pointing to the Beacon for the logic contract.
* @dev Reverts if an instance for the given dealId already exists.
* @param dealId The dealId for which the proxy was created.
*/
function create(uint256 dealId) external {
ValidatorFactoryStorage storage $ = s();
if ($._instances[dealId] != address(0)) revert InstanceAlreadyExists();
PoRepTypes.DealProposal memory dp = PoRepMarket($._poRepMarket).getDealProposal(dealId);
if (msg.sender != dp.client) revert InvalidClientAddress();
bytes memory initCode = abi.encodePacked(
type(BeaconProxy).creationCode,
abi.encode(
$._beacon,
abi.encodeCall(
Validator.initialize,
(
$._admin,
$._poRepService,
$._filecoinPay,
$._sliScorer,
$._clientSmartContract,
$._poRepMarket,
$._SPRegistry,
dealId
)
)
)
);
// forge-lint: disable-next-line(asm-keccak256)
bytes32 salt = keccak256(abi.encode($._admin, dealId));
address proxy = Create2.computeAddress(salt, keccak256(initCode), address(this));
$._instances[dealId] = proxy;
$._isValidatorContract[proxy] = true;
Create2.deploy(0, salt, initCode);
emit ProxyCreated(proxy, dealId);
}
/**
* @notice Sets a new admin for the contract and revoke the role from the old admin
* @dev Only callable by the current admin. Reverts if the new admin address is the zero address.
* @param newAdmin The new admin address
*/
function setAdmin(address newAdmin) external onlyRole(DEFAULT_ADMIN_ROLE) {
if (newAdmin == address(0)) {
revert InvalidNewAdminAddress();
}
ValidatorFactoryStorage storage $ = s();
_revokeRole(DEFAULT_ADMIN_ROLE, $._admin);
_grantRole(DEFAULT_ADMIN_ROLE, newAdmin);
$._admin = newAdmin;
emit AdminChanged(newAdmin);
}
/**
* @notice Sets a new upgrader role for the contract
* @dev Only callable by the current admin. Reverts if the new upgrader role address is the zero address.
* @param newUpgraderRole The new upgrader role address
*/
function setUpgraderRole(address newUpgraderRole) external onlyRole(DEFAULT_ADMIN_ROLE) {
if (newUpgraderRole == address(0)) {
revert InvalidNewUpgraderRoleAddress();
}
ValidatorFactoryStorage storage $ = s();
_revokeRole(UPGRADER_ROLE, $._upgraderRole);
_grantRole(UPGRADER_ROLE, newUpgraderRole);
$._upgraderRole = newUpgraderRole;
emit UpgraderRoleChanged(newUpgraderRole);
}
// solhint-disable use-natspec
/**
* @notice Disabled role management functions
* @dev This contract has a fixed admin and does not allow for dynamic role management
*/
function grantRole(bytes32, address) public pure override {
revert RoleManagementDisabled();
}
/**
* @notice Disabled role management functions
* @dev This contract has a fixed admin and does not allow for dynamic role management
*/
function revokeRole(bytes32, address) public pure override {
revert RoleManagementDisabled();
}
/**
* @notice Disabled role management functions
* @dev This contract has a fixed admin and does not allow for dynamic role management
*/
function renounceRole(bytes32, address) public pure override {
revert RoleManagementDisabled();
}
// solhint-enable use-natspec
/**
* @notice Checks if an address is a validator contract
* @param contractAddress The address to check
* @return True if the address is a validator contract, false otherwise
*/
function isValidatorContract(address contractAddress) external view returns (bool) {
return s()._isValidatorContract[contractAddress];
}
/**
* @notice Gets the instance for a given deal
* @param dealId The ID of the deal
* @return The instance for the given deal
*/
function getInstance(uint256 dealId) external view returns (address) {
return s()._instances[dealId];
}
/**
* @notice Gets the beacon for the factory
* @return The beacon for the factory
*/
function getBeacon() external view returns (address) {
return s()._beacon;
}
// solhint-disable no-empty-blocks
/**
* @notice Internal function used to implement new logic and check if upgrade is authorized
* @dev Will revert (reject upgrade) if upgrade isn't called by UPGRADER_ROLE
* @param newImplementation Address of new implementation
*/
function _authorizeUpgrade(address newImplementation) internal override onlyRole(UPGRADER_ROLE) {}
}