Skip to content

Commit 1a71505

Browse files
authored
Replace Error Strings (#270)
Reviewer @rvagg This reduces codesize 24553 -> 24108 (-445) by replacing revert strings with solidity errors. #### Changes * remove error strings from initialize and setViewContract
1 parent b47546d commit 1a71505

File tree

4 files changed

+63
-13
lines changed

4 files changed

+63
-13
lines changed

service_contracts/abi/FilecoinWarmStorageService.abi.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,17 @@
14741474
],
14751475
"anonymous": false
14761476
},
1477+
{
1478+
"type": "error",
1479+
"name": "AddressAlreadySet",
1480+
"inputs": [
1481+
{
1482+
"name": "field",
1483+
"type": "uint8",
1484+
"internalType": "enum Errors.AddressField"
1485+
}
1486+
]
1487+
},
14771488
{
14781489
"type": "error",
14791490
"name": "AddressEmptyCode",
@@ -1819,6 +1830,28 @@
18191830
"name": "InvalidInitialization",
18201831
"inputs": []
18211832
},
1833+
{
1834+
"type": "error",
1835+
"name": "InvalidServiceDescriptionLength",
1836+
"inputs": [
1837+
{
1838+
"name": "length",
1839+
"type": "uint256",
1840+
"internalType": "uint256"
1841+
}
1842+
]
1843+
},
1844+
{
1845+
"type": "error",
1846+
"name": "InvalidServiceNameLength",
1847+
"inputs": [
1848+
{
1849+
"name": "length",
1850+
"type": "uint256",
1851+
"internalType": "uint256"
1852+
}
1853+
]
1854+
},
18221855
{
18231856
"type": "error",
18241857
"name": "InvalidSignature",

service_contracts/src/Errors.sol

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ library Errors {
2424
/// ServiceProviderRegistry contract address
2525
ServiceProviderRegistry,
2626
/// FilBeam beneficiary address
27-
FilBeamBeneficiary
27+
FilBeamBeneficiary,
28+
/// View contract address
29+
View
2830
}
2931

3032
/// @notice Enumerates the types of commission rates used in the protocol
@@ -39,6 +41,11 @@ library Errors {
3941
/// @param field The specific address field that was zero (see enum {AddressField})
4042
error ZeroAddress(AddressField field);
4143

44+
/// @notice Tried to set an address that can only be set once
45+
/// @dev Used for parameter validation when a non-zero address is required
46+
/// @param field The specific address field already set (see enum {AddressField})
47+
error AddressAlreadySet(AddressField field);
48+
4249
/// @notice Only the PDPVerifier contract can call this function
4350
/// @param expected The expected PDPVerifier address
4451
/// @param actual The caller address
@@ -58,6 +65,14 @@ library Errors {
5865
/// @param challengeWindowSize The provided challenge window size
5966
error InvalidChallengeWindowSize(uint256 maxProvingPeriod, uint256 challengeWindowSize);
6067

68+
/// @notice The service name length must be >0 and <= 256
69+
/// @param length the attempted length
70+
error InvalidServiceNameLength(uint256 length);
71+
72+
/// @notice The service description length must be >0 and <= 256
73+
/// @param length the attempted length
74+
error InvalidServiceDescriptionLength(uint256 length);
75+
6176
/// @notice This function can only be called by the contract itself during upgrade
6277
/// @param expected The expected caller (the contract address)
6378
/// @param actual The actual caller address

service_contracts/src/FilecoinWarmStorageService.sol

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,13 @@ contract FilecoinWarmStorageService is
370370
require(_filBeamControllerAddress != address(0), Errors.ZeroAddress(Errors.AddressField.FilBeamController));
371371
filBeamControllerAddress = _filBeamControllerAddress;
372372

373-
// Validate name and description
374-
require(bytes(_name).length > 0, "Service name cannot be empty");
375-
require(bytes(_name).length <= 256, "Service name exceeds 256 characters");
376-
require(bytes(_description).length > 0, "Service description cannot be empty");
377-
require(bytes(_description).length <= 256, "Service description exceeds 256 characters");
373+
uint256 serviceNameLength = bytes(_name).length;
374+
require(serviceNameLength > 0, Errors.InvalidServiceNameLength(serviceNameLength));
375+
require(serviceNameLength <= 256, Errors.InvalidServiceNameLength(serviceNameLength));
376+
377+
uint256 serviceDescriptionLength = bytes(_description).length;
378+
require(serviceDescriptionLength > 0, Errors.InvalidServiceDescriptionLength(serviceDescriptionLength));
379+
require(serviceDescriptionLength <= 256, Errors.InvalidServiceDescriptionLength(serviceDescriptionLength));
378380

379381
// Emit the FilecoinServiceDeployed event
380382
emit FilecoinServiceDeployed(_name, _description);
@@ -441,8 +443,8 @@ contract FilecoinWarmStorageService is
441443
* @param _viewContract Address of the view contract
442444
*/
443445
function setViewContract(address _viewContract) external onlyOwner {
444-
require(_viewContract != address(0), "Invalid view contract address");
445-
require(viewContractAddress == address(0), "View contract already set");
446+
require(_viewContract != address(0), Errors.ZeroAddress(Errors.AddressField.View));
447+
require(viewContractAddress == address(0), Errors.AddressAlreadySet(Errors.AddressField.View));
446448
viewContractAddress = _viewContract;
447449
emit ViewContractSet(_viewContract);
448450
}

service_contracts/test/FilecoinWarmStorageService.t.sol

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ contract FilecoinWarmStorageServiceTest is Test {
372372
"Valid description"
373373
);
374374

375-
vm.expectRevert("Service name cannot be empty");
375+
vm.expectRevert(abi.encodeWithSelector(Errors.InvalidServiceNameLength.selector, 0));
376376
new MyERC1967Proxy(address(serviceImpl1), initDataEmptyName);
377377

378378
// Test empty description validation
@@ -394,7 +394,7 @@ contract FilecoinWarmStorageServiceTest is Test {
394394
"" // empty description
395395
);
396396

397-
vm.expectRevert("Service description cannot be empty");
397+
vm.expectRevert(abi.encodeWithSelector(Errors.InvalidServiceDescriptionLength.selector, 0));
398398
new MyERC1967Proxy(address(serviceImpl2), initDataEmptyDesc);
399399

400400
// Test name exceeding 256 characters
@@ -426,7 +426,7 @@ contract FilecoinWarmStorageServiceTest is Test {
426426
"Valid description"
427427
);
428428

429-
vm.expectRevert("Service name exceeds 256 characters");
429+
vm.expectRevert(abi.encodeWithSelector(Errors.InvalidServiceNameLength.selector, bytes(longName).length));
430430
new MyERC1967Proxy(address(serviceImpl3), initDataLongName);
431431

432432
// Test description exceeding 256 characters
@@ -458,7 +458,7 @@ contract FilecoinWarmStorageServiceTest is Test {
458458
longDesc
459459
);
460460

461-
vm.expectRevert("Service description exceeds 256 characters");
461+
vm.expectRevert(abi.encodeWithSelector(Errors.InvalidServiceDescriptionLength.selector, bytes(longDesc).length));
462462
new MyERC1967Proxy(address(serviceImpl4), initDataLongDesc);
463463
}
464464

@@ -3779,7 +3779,7 @@ contract FilecoinWarmStorageServiceUpgradeTest is Test {
37793779
// Test that it cannot be set again (one-time only)
37803780
FilecoinWarmStorageServiceStateView newViewContract =
37813781
new FilecoinWarmStorageServiceStateView(warmStorageService);
3782-
vm.expectRevert("View contract already set");
3782+
vm.expectRevert(abi.encodeWithSelector(Errors.AddressAlreadySet.selector, Errors.AddressField.View));
37833783
warmStorageService.setViewContract(address(newViewContract));
37843784

37853785
// Test that zero address is rejected (would need a new contract to test this properly)

0 commit comments

Comments
 (0)