Skip to content

Commit 787acdc

Browse files
authored
setter for upgrader role (#31)
1 parent 040917c commit 787acdc

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

abis/ValidatorFactory.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,19 @@
281281
"outputs": [],
282282
"stateMutability": "nonpayable"
283283
},
284+
{
285+
"type": "function",
286+
"name": "setUpgraderRole",
287+
"inputs": [
288+
{
289+
"name": "newUpgraderRole",
290+
"type": "address",
291+
"internalType": "address"
292+
}
293+
],
294+
"outputs": [],
295+
"stateMutability": "nonpayable"
296+
},
284297
{
285298
"type": "function",
286299
"name": "supportsInterface",
@@ -451,6 +464,19 @@
451464
],
452465
"anonymous": false
453466
},
467+
{
468+
"type": "event",
469+
"name": "UpgraderRoleChanged",
470+
"inputs": [
471+
{
472+
"name": "newUpgraderRole",
473+
"type": "address",
474+
"indexed": true,
475+
"internalType": "address"
476+
}
477+
],
478+
"anonymous": false
479+
},
454480
{
455481
"type": "error",
456482
"name": "AccessControlBadConfirmation",
@@ -570,6 +596,11 @@
570596
"name": "InvalidNewAdminAddress",
571597
"inputs": []
572598
},
599+
{
600+
"type": "error",
601+
"name": "InvalidNewUpgraderRoleAddress",
602+
"inputs": []
603+
},
573604
{
574605
"type": "error",
575606
"name": "InvalidPoRepMarketAddress",

src/ValidatorFactory.sol

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ contract ValidatorFactory is UUPSUpgradeable, AccessControlUpgradeable {
3434
address _SPRegistry;
3535
address _beacon;
3636
address _admin;
37+
address _upgraderRole;
3738
}
3839

3940
// keccak256(abi.encode(uint256(keccak256("porepmarket.storage.ValidatorFactoryStorage")) - 1)) & ~bytes32(uint256(0xff))
@@ -68,6 +69,7 @@ contract ValidatorFactory is UUPSUpgradeable, AccessControlUpgradeable {
6869
error InvalidSPRegistryAddress();
6970
error InvalidImplementationAddress();
7071
error InvalidNewAdminAddress();
72+
error InvalidNewUpgraderRoleAddress();
7173
error RoleManagementDisabled();
7274

7375
/**
@@ -83,6 +85,12 @@ contract ValidatorFactory is UUPSUpgradeable, AccessControlUpgradeable {
8385
*/
8486
event AdminChanged(address indexed newAdmin);
8587

88+
/**
89+
* @notice Emitted when the upgrader role is changed
90+
* @param newUpgraderRole The address of the new upgrader role
91+
*/
92+
event UpgraderRoleChanged(address indexed newUpgraderRole);
93+
8694
/**
8795
* @notice Initializes the contract
8896
* @dev Initializes the contract by setting a default admin role and a UUPS upgradeable role
@@ -104,6 +112,7 @@ contract ValidatorFactory is UUPSUpgradeable, AccessControlUpgradeable {
104112
ValidatorFactoryStorage storage $ = s();
105113
$._beacon = address(new UpgradeableBeacon(implementation, admin));
106114
$._admin = admin;
115+
$._upgraderRole = admin;
107116
}
108117

109118
/**
@@ -200,6 +209,25 @@ contract ValidatorFactory is UUPSUpgradeable, AccessControlUpgradeable {
200209
emit AdminChanged(newAdmin);
201210
}
202211

212+
/**
213+
* @notice Sets a new upgrader role for the contract
214+
* @dev Only callable by the current admin. Reverts if the new upgrader role address is the zero address.
215+
* @param newUpgraderRole The new upgrader role address
216+
*/
217+
function setUpgraderRole(address newUpgraderRole) external onlyRole(DEFAULT_ADMIN_ROLE) {
218+
if (newUpgraderRole == address(0)) {
219+
revert InvalidNewUpgraderRoleAddress();
220+
}
221+
ValidatorFactoryStorage storage $ = s();
222+
223+
_revokeRole(UPGRADER_ROLE, $._upgraderRole);
224+
_grantRole(UPGRADER_ROLE, newUpgraderRole);
225+
226+
$._upgraderRole = newUpgraderRole;
227+
228+
emit UpgraderRoleChanged(newUpgraderRole);
229+
}
230+
203231
// solhint-disable use-natspec
204232
/**
205233
* @notice Disabled role management functions

test/ValidatorFactory.t.sol

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,58 @@ contract ValidatorFactoryTest is Test {
266266
vm.expectRevert(abi.encodeWithSelector(ValidatorFactory.RoleManagementDisabled.selector));
267267
factory.renounceRole(adminRole, admin);
268268
}
269+
270+
function testSetUpgraderRoleRevertsWhenNewUpgraderRoleIsZero() public {
271+
Validator validatorImplementation = new Validator();
272+
ValidatorFactory factoryImplementation = new ValidatorFactory();
273+
address validAdmin = address(0xABCD);
274+
275+
factoryImplementation.initialize(validAdmin, address(validatorImplementation));
276+
277+
vm.prank(validAdmin);
278+
vm.expectRevert(ValidatorFactory.InvalidNewUpgraderRoleAddress.selector);
279+
factoryImplementation.setUpgraderRole(address(0));
280+
}
281+
282+
function testSetsNewUpgraderRoleCorrectly() public {
283+
Validator validatorImplementation = new Validator();
284+
ValidatorFactory factoryImplementation = new ValidatorFactory();
285+
address validAdmin = address(0xABCD);
286+
address newUpgrader = address(0xDCBA);
287+
288+
factoryImplementation.initialize(validAdmin, address(validatorImplementation));
289+
290+
vm.prank(validAdmin);
291+
factoryImplementation.setUpgraderRole(newUpgrader);
292+
293+
assertTrue(factoryImplementation.hasRole(factoryImplementation.UPGRADER_ROLE(), newUpgrader));
294+
}
295+
296+
function testSetAdminEmitsAdminChangedEvent() public {
297+
Validator validatorImplementation = new Validator();
298+
ValidatorFactory factoryImplementation = new ValidatorFactory();
299+
address validAdmin = address(0xABCD);
300+
address newValidAdmin = address(0xDCBA);
301+
302+
factoryImplementation.initialize(validAdmin, address(validatorImplementation));
303+
304+
vm.expectEmit(true, false, false, false);
305+
emit ValidatorFactory.AdminChanged(newValidAdmin);
306+
vm.prank(validAdmin);
307+
factoryImplementation.setAdmin(newValidAdmin);
308+
}
309+
310+
function testSetUpgraderRoleEmitsUpgraderRoleChangedEvent() public {
311+
Validator validatorImplementation = new Validator();
312+
ValidatorFactory factoryImplementation = new ValidatorFactory();
313+
address validAdmin = address(0xABCD);
314+
address newUpgrader = address(0xDCBA);
315+
316+
factoryImplementation.initialize(validAdmin, address(validatorImplementation));
317+
318+
vm.expectEmit(true, false, false, false);
319+
emit ValidatorFactory.UpgraderRoleChanged(newUpgrader);
320+
vm.prank(validAdmin);
321+
factoryImplementation.setUpgraderRole(newUpgrader);
322+
}
269323
}

0 commit comments

Comments
 (0)