Skip to content

Commit b98a979

Browse files
authored
fix(chain-1986): add MAX_BASE_SIGNER_COUNT to VerificationLib (#111)
* fix(chain-1986): add MAX_BASE_SIGNER_COUNT to VerificationLib * chore: impl review feedback
1 parent 669e409 commit b98a979

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

base/src/libraries/VerificationLib.sol

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ library VerificationLib {
3030
/// @notice The length of a signature in bytes.
3131
uint256 internal constant SIGNATURE_LENGTH_THRESHOLD = 65;
3232

33+
/// @notice The max allowed base signer count
34+
uint256 internal constant MAX_BASE_SIGNER_COUNT = 16;
35+
3336
/// @dev Slot for the `VerificationLibStorage` struct in storage.
3437
/// Computed from:
3538
/// keccak256(abi.encode(uint256(keccak256("coinbase.storage.VerificationLib")) - 1)) &
@@ -59,6 +62,9 @@ library VerificationLib {
5962
/// @notice Thrown when threshold is invalid (0 or exceeds validator count).
6063
error InvalidThreshold();
6164

65+
/// @notice Thrown when base signer count is too high.
66+
error BaseSignerCountTooHigh();
67+
6268
/// @notice Thrown when a validator address is 0.
6369
error InvalidValidatorAddress();
6470

@@ -92,6 +98,7 @@ library VerificationLib {
9298
VerificationLibStorage storage $ = getVerificationLibStorage();
9399

94100
require(threshold > 0 && threshold <= validators.length, InvalidThreshold());
101+
require(validators.length <= MAX_BASE_SIGNER_COUNT, BaseSignerCountTooHigh());
95102

96103
for (uint256 i; i < validators.length; i++) {
97104
require(validators[i] != address(0), InvalidValidatorAddress());
@@ -122,12 +129,15 @@ library VerificationLib {
122129
require(validator != address(0), InvalidValidatorAddress());
123130
require(!$.validators[validator], ValidatorAlreadyAdded());
124131

125-
$.validators[validator] = true;
126-
132+
uint128 newValidatorCount;
127133
unchecked {
128-
$.validatorCount++;
134+
newValidatorCount = $.validatorCount + 1;
129135
}
130136

137+
require(newValidatorCount <= MAX_BASE_SIGNER_COUNT, BaseSignerCountTooHigh());
138+
$.validators[validator] = true;
139+
$.validatorCount = newValidatorCount;
140+
131141
emit ValidatorAdded(validator);
132142
}
133143

base/test/BridgeValidator.t.sol

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,23 @@ contract BridgeValidatorTest is CommonTest {
366366
/// Guardian/VerificationLib Tests ///
367367
//////////////////////////////////////////////////////////////
368368

369+
function test_initialize_revertsWhenAboveMaxBaseSignerCount() public {
370+
// Unset the initializer slot.
371+
vm.store(
372+
address(bridgeValidator),
373+
bytes32(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffbf601132),
374+
bytes32(0)
375+
);
376+
377+
address[] memory validators = new address[](VerificationLib.MAX_BASE_SIGNER_COUNT + 1);
378+
for (uint256 i; i < VerificationLib.MAX_BASE_SIGNER_COUNT + 1; i++) {
379+
validators[i] = vm.addr(i + 2);
380+
}
381+
382+
vm.expectRevert(VerificationLib.BaseSignerCountTooHigh.selector);
383+
bridgeValidator.initialize(validators, 3);
384+
}
385+
369386
function test_setThreshold_onlyGuardian_revertsForNonGuardian() public {
370387
vm.expectRevert(BridgeValidator.CallerNotGuardian.selector);
371388
bridgeValidator.setThreshold(1);
@@ -437,6 +454,17 @@ contract BridgeValidatorTest is CommonTest {
437454
bridgeValidator.removeValidator(vm.addr(1));
438455
}
439456

457+
function test_addValidator_revertsWhenAboveMaxBaseSignerCount() public {
458+
vm.startPrank(cfg.guardians[0]);
459+
460+
for (uint256 i; i < VerificationLib.MAX_BASE_SIGNER_COUNT - 1; i++) {
461+
bridgeValidator.addValidator(vm.addr(i + 2));
462+
}
463+
464+
vm.expectRevert(VerificationLib.BaseSignerCountTooHigh.selector);
465+
bridgeValidator.addValidator(vm.addr(0x42));
466+
}
467+
440468
function test_removeValidator_asGuardian_emitsEvent_andKeepsRegistering() public {
441469
// Add a second validator, keep threshold at 1, then remove it
442470
address newValidator = vm.addr(2);

0 commit comments

Comments
 (0)