Skip to content

Commit

Permalink
feat(store): improve FieldLayout errors [N-03] (#2114)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Jan 11, 2024
1 parent 73f8dca commit 103f635
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
12 changes: 12 additions & 0 deletions .changeset/young-poets-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"@latticexyz/store": minor
---

Improved error messages for invalid `FieldLayout`s

```diff
-error FieldLayoutLib_InvalidLength(uint256 length);
+error FieldLayoutLib_TooManyFields(uint256 numFields, uint256 maxFields);
+error FieldLayoutLib_TooManyDynamicFields(uint256 numFields, uint256 maxFields);
+error FieldLayoutLib_Empty();
```
17 changes: 11 additions & 6 deletions packages/store/src/FieldLayout.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ using FieldLayoutInstance for FieldLayout global;
* various constraints regarding the length and size of the fields.
*/
library FieldLayoutLib {
error FieldLayoutLib_InvalidLength(uint256 length);
error FieldLayoutLib_TooManyFields(uint256 numFields, uint256 maxFields);
error FieldLayoutLib_TooManyDynamicFields(uint256 numFields, uint256 maxFields);
error FieldLayoutLib_Empty();
error FieldLayoutLib_StaticLengthIsZero();
error FieldLayoutLib_StaticLengthDoesNotFitInAWord();

Expand All @@ -41,8 +43,9 @@ library FieldLayoutLib {
uint256 fieldLayout;
uint256 totalLength;
uint256 totalFields = _staticFields.length + numDynamicFields;
if (totalFields > MAX_TOTAL_FIELDS) revert FieldLayoutLib_InvalidLength(totalFields);
if (numDynamicFields > MAX_DYNAMIC_FIELDS) revert FieldLayoutLib_InvalidLength(numDynamicFields);
if (totalFields > MAX_TOTAL_FIELDS) revert FieldLayoutLib_TooManyFields(totalFields, MAX_TOTAL_FIELDS);
if (numDynamicFields > MAX_DYNAMIC_FIELDS)
revert FieldLayoutLib_TooManyDynamicFields(numDynamicFields, MAX_DYNAMIC_FIELDS);

// Compute the total static length and store the field lengths in the encoded fieldLayout
for (uint256 i = 0; i < _staticFields.length; ) {
Expand Down Expand Up @@ -149,16 +152,18 @@ library FieldLayoutInstance {
*/
function validate(FieldLayout fieldLayout, bool allowEmpty) internal pure {
// FieldLayout must not be empty
if (!allowEmpty && fieldLayout.isEmpty()) revert FieldLayoutLib.FieldLayoutLib_InvalidLength(0);
if (!allowEmpty && fieldLayout.isEmpty()) revert FieldLayoutLib.FieldLayoutLib_Empty();

// FieldLayout must have no more than MAX_DYNAMIC_FIELDS
uint256 _numDynamicFields = fieldLayout.numDynamicFields();
if (_numDynamicFields > MAX_DYNAMIC_FIELDS) revert FieldLayoutLib.FieldLayoutLib_InvalidLength(_numDynamicFields);
if (_numDynamicFields > MAX_DYNAMIC_FIELDS)
revert FieldLayoutLib.FieldLayoutLib_TooManyDynamicFields(_numDynamicFields, MAX_DYNAMIC_FIELDS);

uint256 _numStaticFields = fieldLayout.numStaticFields();
// FieldLayout must not have more than MAX_TOTAL_FIELDS in total
uint256 _numTotalFields = _numStaticFields + _numDynamicFields;
if (_numTotalFields > MAX_TOTAL_FIELDS) revert FieldLayoutLib.FieldLayoutLib_InvalidLength(_numTotalFields);
if (_numTotalFields > MAX_TOTAL_FIELDS)
revert FieldLayoutLib.FieldLayoutLib_TooManyFields(_numTotalFields, MAX_TOTAL_FIELDS);

// Static lengths must be valid
for (uint256 i; i < _numStaticFields; ) {
Expand Down
5 changes: 3 additions & 2 deletions packages/store/test/StoreCore.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ contract StoreCoreTest is Test, StoreMock {

vm.expectRevert(
abi.encodeWithSelector(
FieldLayoutLib.FieldLayoutLib_InvalidLength.selector,
invalidFieldLayout.numDynamicFields()
FieldLayoutLib.FieldLayoutLib_TooManyDynamicFields.selector,
invalidFieldLayout.numDynamicFields(),
5
)
);
IStore(this).registerTable(
Expand Down
4 changes: 2 additions & 2 deletions packages/store/ts/codegen/renderFieldLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export function encodeFieldLayout(fields: RenderType[]) {
let totalLength = 0;
const totalFields = fields.length;

if (totalFields > MAX_TOTAL_FIELDS) throw new Error(`FieldLayout: invalid length ${totalFields}`);
if (numDynamicFields > MAX_DYNAMIC_FIELDS) throw new Error(`FieldLayout: invalid length ${numDynamicFields}`);
if (totalFields > MAX_TOTAL_FIELDS) throw new Error("FieldLayout: too many fields");
if (numDynamicFields > MAX_DYNAMIC_FIELDS) throw new Error("FieldLayout: too many dynamic fields");

for (let i = 0; i < staticFields.length; i++) {
const { isDynamic, staticByteLength } = fields[i];
Expand Down
2 changes: 1 addition & 1 deletion packages/world/gas-report.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
"file": "test/World.t.sol",
"test": "testRegisterTable",
"name": "Register a new table in the namespace",
"gasUsed": 528361
"gasUsed": 528393
},
{
"file": "test/World.t.sol",
Expand Down

0 comments on commit 103f635

Please sign in to comment.