diff --git a/.changeset/hot-mice-play.md b/.changeset/hot-mice-play.md new file mode 100644 index 0000000000..defaa99a25 --- /dev/null +++ b/.changeset/hot-mice-play.md @@ -0,0 +1,8 @@ +--- +"@latticexyz/cli": patch +"@latticexyz/store": patch +"@latticexyz/world": patch +--- + +The `FieldLayout` in table libraries is now generated at compile time instead of dynamically in a table library function. +This significantly reduces gas cost in all table library functions. diff --git a/e2e/packages/contracts/src/codegen/tables/Multi.sol b/e2e/packages/contracts/src/codegen/tables/Multi.sol index a66e9b0d72..2f25c111dd 100644 --- a/e2e/packages/contracts/src/codegen/tables/Multi.sol +++ b/e2e/packages/contracts/src/codegen/tables/Multi.sol @@ -21,6 +21,10 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Multi"))); bytes32 constant MultiTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0021020020010000000000000000000000000000000000000000000000000000 +); + struct MultiData { int256 num; bool value; @@ -29,11 +33,7 @@ struct MultiData { library Multi { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](2); - _fieldLayout[0] = 32; - _fieldLayout[1] = 1; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -74,19 +74,12 @@ library Multi { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get num */ @@ -97,7 +90,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (int256(uint256(Bytes.slice32(_blob, 0)))); } @@ -109,7 +102,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (int256(uint256(Bytes.slice32(_blob, 0)))); } @@ -121,7 +114,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((num)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((num)), _fieldLayout); } /** Set num (using the specified store) */ @@ -132,7 +125,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((num)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((num)), _fieldLayout); } /** Get value */ @@ -143,7 +136,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, _fieldLayout); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -155,7 +148,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, _fieldLayout); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -167,7 +160,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((value)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((value)), _fieldLayout); } /** Set value (using the specified store) */ @@ -178,7 +171,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((value)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((value)), _fieldLayout); } /** Get the full data */ @@ -189,7 +182,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -201,7 +194,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -218,7 +211,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using individual values (using the specified store) */ @@ -234,7 +227,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using the data struct */ @@ -288,7 +281,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -299,7 +292,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/e2e/packages/contracts/src/codegen/tables/Number.sol b/e2e/packages/contracts/src/codegen/tables/Number.sol index 68d463bacd..07eeb5619e 100644 --- a/e2e/packages/contracts/src/codegen/tables/Number.sol +++ b/e2e/packages/contracts/src/codegen/tables/Number.sol @@ -21,13 +21,14 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Number"))); bytes32 constant NumberTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0004010004000000000000000000000000000000000000000000000000000000 +); + library Number { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](1); - _fieldLayout[0] = 4; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -60,19 +61,12 @@ library Number { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ @@ -80,7 +74,7 @@ library Number { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint32(Bytes.slice4(_blob, 0))); } @@ -89,7 +83,7 @@ library Number { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint32(Bytes.slice4(_blob, 0))); } @@ -98,7 +92,7 @@ library Number { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } /** Set value (using the specified store) */ @@ -106,7 +100,7 @@ library Number { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } /** Tightly pack static data using this table's schema */ @@ -137,7 +131,7 @@ library Number { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -145,6 +139,6 @@ library Number { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/e2e/packages/contracts/src/codegen/tables/NumberList.sol b/e2e/packages/contracts/src/codegen/tables/NumberList.sol index 76ab24b8e5..0f26dba1a2 100644 --- a/e2e/packages/contracts/src/codegen/tables/NumberList.sol +++ b/e2e/packages/contracts/src/codegen/tables/NumberList.sol @@ -21,12 +21,14 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("NumberList"))); bytes32 constant NumberListTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0000000100000000000000000000000000000000000000000000000000000000 +); + library NumberList { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](0); - - return FieldLayoutLib.encode(_fieldLayout, 1); + return _fieldLayout; } /** Get the table's key schema */ @@ -57,26 +59,19 @@ library NumberList { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ function get() internal view returns (uint32[] memory value) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } @@ -84,7 +79,7 @@ library NumberList { function get(IStore _store) internal view returns (uint32[] memory value) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } @@ -92,21 +87,21 @@ library NumberList { function set(uint32[] memory value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); } /** Set value (using the specified store) */ function set(IStore _store, uint32[] memory value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); } /** Get the length of value */ function length() internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](0); - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); unchecked { return _byteLength / 4; } @@ -116,7 +111,7 @@ library NumberList { function length(IStore _store) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](0); - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); unchecked { return _byteLength / 4; } @@ -134,7 +129,7 @@ library NumberList { _tableId, _keyTuple, 0, - getFieldLayout(), + _fieldLayout, _index * 4, (_index + 1) * 4 ); @@ -150,7 +145,7 @@ library NumberList { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getFieldLayout(), _index * 4, (_index + 1) * 4); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, _fieldLayout, _index * 4, (_index + 1) * 4); return (uint32(Bytes.slice4(_blob, 0))); } } @@ -159,28 +154,28 @@ library NumberList { function push(uint32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } /** Push an element to value (using the specified store) */ function push(IStore _store, uint32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } /** Pop an element from value */ function pop() internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 4, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 0, 4, _fieldLayout); } /** Pop an element from value (using the specified store) */ function pop(IStore _store) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.popFromField(_tableId, _keyTuple, 0, 4, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 0, 4, _fieldLayout); } /** @@ -191,7 +186,7 @@ library NumberList { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 4, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 4, abi.encodePacked((_element)), _fieldLayout); } } @@ -203,7 +198,7 @@ library NumberList { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - _store.updateInField(_tableId, _keyTuple, 0, _index * 4, abi.encodePacked((_element)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 0, _index * 4, abi.encodePacked((_element)), _fieldLayout); } } @@ -240,13 +235,13 @@ library NumberList { function deleteRecord() internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/e2e/packages/contracts/src/codegen/tables/Vector.sol b/e2e/packages/contracts/src/codegen/tables/Vector.sol index c64c1d9086..a7870c1a6c 100644 --- a/e2e/packages/contracts/src/codegen/tables/Vector.sol +++ b/e2e/packages/contracts/src/codegen/tables/Vector.sol @@ -21,6 +21,10 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Vector"))); bytes32 constant VectorTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0008020004040000000000000000000000000000000000000000000000000000 +); + struct VectorData { int32 x; int32 y; @@ -29,11 +33,7 @@ struct VectorData { library Vector { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](2); - _fieldLayout[0] = 4; - _fieldLayout[1] = 4; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -68,19 +68,12 @@ library Vector { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get x */ @@ -88,7 +81,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -97,7 +90,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -106,7 +99,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), _fieldLayout); } /** Set x (using the specified store) */ @@ -114,7 +107,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), _fieldLayout); } /** Get y */ @@ -122,7 +115,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, _fieldLayout); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -131,7 +124,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, _fieldLayout); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -140,7 +133,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), _fieldLayout); } /** Set y (using the specified store) */ @@ -148,7 +141,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), _fieldLayout); } /** Get the full data */ @@ -156,7 +149,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -165,7 +158,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -179,7 +172,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using individual values (using the specified store) */ @@ -192,7 +185,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using the data struct */ @@ -240,7 +233,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -248,6 +241,6 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/examples/minimal/packages/contracts/src/codegen/tables/CounterTable.sol b/examples/minimal/packages/contracts/src/codegen/tables/CounterTable.sol index 0de74a8c72..04bf83b923 100644 --- a/examples/minimal/packages/contracts/src/codegen/tables/CounterTable.sol +++ b/examples/minimal/packages/contracts/src/codegen/tables/CounterTable.sol @@ -21,13 +21,14 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("CounterTable"))); bytes32 constant CounterTableTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0004010004000000000000000000000000000000000000000000000000000000 +); + library CounterTable { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](1); - _fieldLayout[0] = 4; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -58,26 +59,19 @@ library CounterTable { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ function get() internal view returns (uint32 value) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint32(Bytes.slice4(_blob, 0))); } @@ -85,7 +79,7 @@ library CounterTable { function get(IStore _store) internal view returns (uint32 value) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint32(Bytes.slice4(_blob, 0))); } @@ -93,14 +87,14 @@ library CounterTable { function set(uint32 value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } /** Set value (using the specified store) */ function set(IStore _store, uint32 value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } /** Tightly pack static data using this table's schema */ @@ -129,13 +123,13 @@ library CounterTable { function deleteRecord() internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/examples/minimal/packages/contracts/src/codegen/tables/Inventory.sol b/examples/minimal/packages/contracts/src/codegen/tables/Inventory.sol index 15c35b4678..2fe128efd0 100644 --- a/examples/minimal/packages/contracts/src/codegen/tables/Inventory.sol +++ b/examples/minimal/packages/contracts/src/codegen/tables/Inventory.sol @@ -21,13 +21,14 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Inventory"))); bytes32 constant InventoryTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0004010004000000000000000000000000000000000000000000000000000000 +); + library Inventory { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](1); - _fieldLayout[0] = 4; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -64,19 +65,12 @@ library Inventory { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get amount */ @@ -86,7 +80,7 @@ library Inventory { _keyTuple[1] = bytes32(uint256(item)); _keyTuple[2] = bytes32(uint256(itemVariant)); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint32(Bytes.slice4(_blob, 0))); } @@ -97,7 +91,7 @@ library Inventory { _keyTuple[1] = bytes32(uint256(item)); _keyTuple[2] = bytes32(uint256(itemVariant)); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint32(Bytes.slice4(_blob, 0))); } @@ -108,7 +102,7 @@ library Inventory { _keyTuple[1] = bytes32(uint256(item)); _keyTuple[2] = bytes32(uint256(itemVariant)); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((amount)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((amount)), _fieldLayout); } /** Set amount (using the specified store) */ @@ -118,7 +112,7 @@ library Inventory { _keyTuple[1] = bytes32(uint256(item)); _keyTuple[2] = bytes32(uint256(itemVariant)); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((amount)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((amount)), _fieldLayout); } /** Tightly pack static data using this table's schema */ @@ -153,7 +147,7 @@ library Inventory { _keyTuple[1] = bytes32(uint256(item)); _keyTuple[2] = bytes32(uint256(itemVariant)); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -163,6 +157,6 @@ library Inventory { _keyTuple[1] = bytes32(uint256(item)); _keyTuple[2] = bytes32(uint256(itemVariant)); - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/examples/minimal/packages/contracts/src/codegen/tables/MessageTable.sol b/examples/minimal/packages/contracts/src/codegen/tables/MessageTable.sol index 142691da0a..6e58cfc3df 100644 --- a/examples/minimal/packages/contracts/src/codegen/tables/MessageTable.sol +++ b/examples/minimal/packages/contracts/src/codegen/tables/MessageTable.sol @@ -21,12 +21,14 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("MessageTable"))); bytes32 constant MessageTableTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0000000100000000000000000000000000000000000000000000000000000000 +); + library MessageTable { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](0); - - return FieldLayoutLib.encode(_fieldLayout, 1); + return _fieldLayout; } /** Get the table's key schema */ @@ -57,19 +59,12 @@ library MessageTable { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Emit the ephemeral event using individual values */ @@ -80,7 +75,7 @@ library MessageTable { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.emitEphemeralRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + StoreSwitch.emitEphemeralRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Emit the ephemeral event using individual values (using the specified store) */ @@ -91,7 +86,7 @@ library MessageTable { bytes32[] memory _keyTuple = new bytes32[](0); - _store.emitEphemeralRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + _store.emitEphemeralRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Tightly pack dynamic data using this table's schema */ diff --git a/packages/cli/contracts/src/codegen/tables/Dynamics1.sol b/packages/cli/contracts/src/codegen/tables/Dynamics1.sol index f32451cf30..9d96284463 100644 --- a/packages/cli/contracts/src/codegen/tables/Dynamics1.sol +++ b/packages/cli/contracts/src/codegen/tables/Dynamics1.sol @@ -21,6 +21,10 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Dynamics1"))); bytes32 constant Dynamics1TableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0000000500000000000000000000000000000000000000000000000000000000 +); + struct Dynamics1Data { bytes32[1] staticB32; int32[2] staticI32; @@ -32,9 +36,7 @@ struct Dynamics1Data { library Dynamics1 { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](0); - - return FieldLayoutLib.encode(_fieldLayout, 5); + return _fieldLayout; } /** Get the table's key schema */ @@ -75,19 +77,12 @@ library Dynamics1 { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get staticB32 */ @@ -95,7 +90,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return toStaticArray_bytes32_1(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -104,7 +99,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return toStaticArray_bytes32_1(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -118,7 +113,7 @@ library Dynamics1 { _keyTuple, 0, EncodeArray.encode(fromStaticArray_bytes32_1(staticB32)), - getFieldLayout() + _fieldLayout ); } @@ -127,7 +122,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode(fromStaticArray_bytes32_1(staticB32)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode(fromStaticArray_bytes32_1(staticB32)), _fieldLayout); } /** Get the length of staticB32 */ @@ -135,7 +130,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); unchecked { return _byteLength / 32; } @@ -146,7 +141,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); unchecked { return _byteLength / 32; } @@ -165,7 +160,7 @@ library Dynamics1 { _tableId, _keyTuple, 0, - getFieldLayout(), + _fieldLayout, _index * 32, (_index + 1) * 32 ); @@ -182,14 +177,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice( - _tableId, - _keyTuple, - 0, - getFieldLayout(), - _index * 32, - (_index + 1) * 32 - ); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, _fieldLayout, _index * 32, (_index + 1) * 32); return (Bytes.slice32(_blob, 0)); } } @@ -199,7 +187,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } /** Push an element to staticB32 (using the specified store) */ @@ -207,7 +195,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } /** Pop an element from staticB32 */ @@ -215,7 +203,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 32, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 0, 32, _fieldLayout); } /** Pop an element from staticB32 (using the specified store) */ @@ -223,7 +211,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 0, 32, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 0, 32, _fieldLayout); } /** @@ -235,7 +223,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), _fieldLayout); } } @@ -248,7 +236,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), _fieldLayout); } } @@ -257,7 +245,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, _fieldLayout); return toStaticArray_int32_2(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_int32()); } @@ -266,7 +254,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, _fieldLayout); return toStaticArray_int32_2(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_int32()); } @@ -275,13 +263,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField( - _tableId, - _keyTuple, - 1, - EncodeArray.encode(fromStaticArray_int32_2(staticI32)), - getFieldLayout() - ); + StoreSwitch.setField(_tableId, _keyTuple, 1, EncodeArray.encode(fromStaticArray_int32_2(staticI32)), _fieldLayout); } /** Set staticI32 (using the specified store) */ @@ -289,7 +271,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 1, EncodeArray.encode(fromStaticArray_int32_2(staticI32)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 1, EncodeArray.encode(fromStaticArray_int32_2(staticI32)), _fieldLayout); } /** Get the length of staticI32 */ @@ -297,7 +279,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 1, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 1, _fieldLayout); unchecked { return _byteLength / 4; } @@ -308,7 +290,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 1, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 1, _fieldLayout); unchecked { return _byteLength / 4; } @@ -327,7 +309,7 @@ library Dynamics1 { _tableId, _keyTuple, 1, - getFieldLayout(), + _fieldLayout, _index * 4, (_index + 1) * 4 ); @@ -344,7 +326,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 1, getFieldLayout(), _index * 4, (_index + 1) * 4); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 1, _fieldLayout, _index * 4, (_index + 1) * 4); return (int32(uint32(Bytes.slice4(_blob, 0)))); } } @@ -354,7 +336,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), _fieldLayout); } /** Push an element to staticI32 (using the specified store) */ @@ -362,7 +344,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), _fieldLayout); } /** Pop an element from staticI32 */ @@ -370,7 +352,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 1, 4, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 1, 4, _fieldLayout); } /** Pop an element from staticI32 (using the specified store) */ @@ -378,7 +360,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 1, 4, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 1, 4, _fieldLayout); } /** @@ -390,7 +372,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 1, _index * 4, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 1, _index * 4, abi.encodePacked((_element)), _fieldLayout); } } @@ -403,7 +385,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 1, _index * 4, abi.encodePacked((_element)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 1, _index * 4, abi.encodePacked((_element)), _fieldLayout); } } @@ -412,7 +394,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, _fieldLayout); return toStaticArray_uint128_3(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint128()); } @@ -421,7 +403,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, _fieldLayout); return toStaticArray_uint128_3(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint128()); } @@ -435,7 +417,7 @@ library Dynamics1 { _keyTuple, 2, EncodeArray.encode(fromStaticArray_uint128_3(staticU128)), - getFieldLayout() + _fieldLayout ); } @@ -444,13 +426,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField( - _tableId, - _keyTuple, - 2, - EncodeArray.encode(fromStaticArray_uint128_3(staticU128)), - getFieldLayout() - ); + _store.setField(_tableId, _keyTuple, 2, EncodeArray.encode(fromStaticArray_uint128_3(staticU128)), _fieldLayout); } /** Get the length of staticU128 */ @@ -458,7 +434,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 2, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 2, _fieldLayout); unchecked { return _byteLength / 16; } @@ -469,7 +445,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 2, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 2, _fieldLayout); unchecked { return _byteLength / 16; } @@ -488,7 +464,7 @@ library Dynamics1 { _tableId, _keyTuple, 2, - getFieldLayout(), + _fieldLayout, _index * 16, (_index + 1) * 16 ); @@ -505,14 +481,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice( - _tableId, - _keyTuple, - 2, - getFieldLayout(), - _index * 16, - (_index + 1) * 16 - ); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 2, _fieldLayout, _index * 16, (_index + 1) * 16); return (uint128(Bytes.slice16(_blob, 0))); } } @@ -522,7 +491,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), _fieldLayout); } /** Push an element to staticU128 (using the specified store) */ @@ -530,7 +499,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), _fieldLayout); } /** Pop an element from staticU128 */ @@ -538,7 +507,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 2, 16, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 2, 16, _fieldLayout); } /** Pop an element from staticU128 (using the specified store) */ @@ -546,7 +515,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 2, 16, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 2, 16, _fieldLayout); } /** @@ -558,7 +527,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 2, _index * 16, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 2, _index * 16, abi.encodePacked((_element)), _fieldLayout); } } @@ -571,7 +540,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 2, _index * 16, abi.encodePacked((_element)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 2, _index * 16, abi.encodePacked((_element)), _fieldLayout); } } @@ -580,7 +549,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, _fieldLayout); return toStaticArray_address_4(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_address()); } @@ -589,7 +558,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, _fieldLayout); return toStaticArray_address_4(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_address()); } @@ -603,7 +572,7 @@ library Dynamics1 { _keyTuple, 3, EncodeArray.encode(fromStaticArray_address_4(staticAddrs)), - getFieldLayout() + _fieldLayout ); } @@ -612,13 +581,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField( - _tableId, - _keyTuple, - 3, - EncodeArray.encode(fromStaticArray_address_4(staticAddrs)), - getFieldLayout() - ); + _store.setField(_tableId, _keyTuple, 3, EncodeArray.encode(fromStaticArray_address_4(staticAddrs)), _fieldLayout); } /** Get the length of staticAddrs */ @@ -626,7 +589,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 3, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 3, _fieldLayout); unchecked { return _byteLength / 20; } @@ -637,7 +600,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 3, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 3, _fieldLayout); unchecked { return _byteLength / 20; } @@ -656,7 +619,7 @@ library Dynamics1 { _tableId, _keyTuple, 3, - getFieldLayout(), + _fieldLayout, _index * 20, (_index + 1) * 20 ); @@ -673,14 +636,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice( - _tableId, - _keyTuple, - 3, - getFieldLayout(), - _index * 20, - (_index + 1) * 20 - ); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 3, _fieldLayout, _index * 20, (_index + 1) * 20); return (address(Bytes.slice20(_blob, 0))); } } @@ -690,7 +646,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), _fieldLayout); } /** Push an element to staticAddrs (using the specified store) */ @@ -698,7 +654,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), _fieldLayout); } /** Pop an element from staticAddrs */ @@ -706,7 +662,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 3, 20, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 3, 20, _fieldLayout); } /** Pop an element from staticAddrs (using the specified store) */ @@ -714,7 +670,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 3, 20, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 3, 20, _fieldLayout); } /** @@ -726,7 +682,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 3, _index * 20, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 3, _index * 20, abi.encodePacked((_element)), _fieldLayout); } } @@ -739,7 +695,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 3, _index * 20, abi.encodePacked((_element)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 3, _index * 20, abi.encodePacked((_element)), _fieldLayout); } } @@ -748,7 +704,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 4, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 4, _fieldLayout); return toStaticArray_bool_5(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bool()); } @@ -757,7 +713,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 4, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 4, _fieldLayout); return toStaticArray_bool_5(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bool()); } @@ -766,13 +722,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField( - _tableId, - _keyTuple, - 4, - EncodeArray.encode(fromStaticArray_bool_5(staticBools)), - getFieldLayout() - ); + StoreSwitch.setField(_tableId, _keyTuple, 4, EncodeArray.encode(fromStaticArray_bool_5(staticBools)), _fieldLayout); } /** Set staticBools (using the specified store) */ @@ -780,7 +730,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 4, EncodeArray.encode(fromStaticArray_bool_5(staticBools)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 4, EncodeArray.encode(fromStaticArray_bool_5(staticBools)), _fieldLayout); } /** Get the length of staticBools */ @@ -788,7 +738,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 4, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 4, _fieldLayout); unchecked { return _byteLength / 1; } @@ -799,7 +749,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 4, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 4, _fieldLayout); unchecked { return _byteLength / 1; } @@ -818,7 +768,7 @@ library Dynamics1 { _tableId, _keyTuple, 4, - getFieldLayout(), + _fieldLayout, _index * 1, (_index + 1) * 1 ); @@ -835,7 +785,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 4, getFieldLayout(), _index * 1, (_index + 1) * 1); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 4, _fieldLayout, _index * 1, (_index + 1) * 1); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } } @@ -845,7 +795,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), _fieldLayout); } /** Push an element to staticBools (using the specified store) */ @@ -853,7 +803,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), _fieldLayout); } /** Pop an element from staticBools */ @@ -861,7 +811,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 4, 1, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 4, 1, _fieldLayout); } /** Pop an element from staticBools (using the specified store) */ @@ -869,7 +819,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 4, 1, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 4, 1, _fieldLayout); } /** @@ -881,7 +831,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 4, _index * 1, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 4, _index * 1, abi.encodePacked((_element)), _fieldLayout); } } @@ -894,7 +844,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 4, _index * 1, abi.encodePacked((_element)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 4, _index * 1, abi.encodePacked((_element)), _fieldLayout); } } @@ -903,7 +853,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -912,7 +862,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -932,7 +882,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using individual values (using the specified store) */ @@ -952,7 +902,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using the data struct */ @@ -1075,7 +1025,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -1083,7 +1033,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/cli/contracts/src/codegen/tables/Dynamics2.sol b/packages/cli/contracts/src/codegen/tables/Dynamics2.sol index 9ced950df1..2317c58515 100644 --- a/packages/cli/contracts/src/codegen/tables/Dynamics2.sol +++ b/packages/cli/contracts/src/codegen/tables/Dynamics2.sol @@ -21,6 +21,10 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Dynamics2"))); bytes32 constant Dynamics2TableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0000000300000000000000000000000000000000000000000000000000000000 +); + struct Dynamics2Data { uint64[] u64; string str; @@ -30,9 +34,7 @@ struct Dynamics2Data { library Dynamics2 { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](0); - - return FieldLayoutLib.encode(_fieldLayout, 3); + return _fieldLayout; } /** Get the table's key schema */ @@ -69,19 +71,12 @@ library Dynamics2 { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get u64 */ @@ -89,7 +84,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint64()); } @@ -98,7 +93,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint64()); } @@ -107,7 +102,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((u64)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((u64)), _fieldLayout); } /** Set u64 (using the specified store) */ @@ -115,7 +110,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((u64)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((u64)), _fieldLayout); } /** Get the length of u64 */ @@ -123,7 +118,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); unchecked { return _byteLength / 8; } @@ -134,7 +129,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); unchecked { return _byteLength / 8; } @@ -153,7 +148,7 @@ library Dynamics2 { _tableId, _keyTuple, 0, - getFieldLayout(), + _fieldLayout, _index * 8, (_index + 1) * 8 ); @@ -170,7 +165,7 @@ library Dynamics2 { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getFieldLayout(), _index * 8, (_index + 1) * 8); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, _fieldLayout, _index * 8, (_index + 1) * 8); return (uint64(Bytes.slice8(_blob, 0))); } } @@ -180,7 +175,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } /** Push an element to u64 (using the specified store) */ @@ -188,7 +183,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } /** Pop an element from u64 */ @@ -196,7 +191,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 8, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 0, 8, _fieldLayout); } /** Pop an element from u64 (using the specified store) */ @@ -204,7 +199,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 0, 8, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 0, 8, _fieldLayout); } /** @@ -216,7 +211,7 @@ library Dynamics2 { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 8, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 8, abi.encodePacked((_element)), _fieldLayout); } } @@ -229,7 +224,7 @@ library Dynamics2 { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 0, _index * 8, abi.encodePacked((_element)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 0, _index * 8, abi.encodePacked((_element)), _fieldLayout); } } @@ -238,7 +233,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, _fieldLayout); return (string(_blob)); } @@ -247,7 +242,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, _fieldLayout); return (string(_blob)); } @@ -256,7 +251,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 1, bytes((str)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 1, bytes((str)), _fieldLayout); } /** Set str (using the specified store) */ @@ -264,7 +259,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 1, bytes((str)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 1, bytes((str)), _fieldLayout); } /** Get the length of str */ @@ -272,7 +267,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 1, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 1, _fieldLayout); unchecked { return _byteLength / 1; } @@ -283,7 +278,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 1, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 1, _fieldLayout); unchecked { return _byteLength / 1; } @@ -302,7 +297,7 @@ library Dynamics2 { _tableId, _keyTuple, 1, - getFieldLayout(), + _fieldLayout, _index * 1, (_index + 1) * 1 ); @@ -319,7 +314,7 @@ library Dynamics2 { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 1, getFieldLayout(), _index * 1, (_index + 1) * 1); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 1, _fieldLayout, _index * 1, (_index + 1) * 1); return (string(_blob)); } } @@ -329,7 +324,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 1, bytes((_slice)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 1, bytes((_slice)), _fieldLayout); } /** Push a slice to str (using the specified store) */ @@ -337,7 +332,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 1, bytes((_slice)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 1, bytes((_slice)), _fieldLayout); } /** Pop a slice from str */ @@ -345,7 +340,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 1, 1, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 1, 1, _fieldLayout); } /** Pop a slice from str (using the specified store) */ @@ -353,7 +348,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 1, 1, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 1, 1, _fieldLayout); } /** @@ -365,7 +360,7 @@ library Dynamics2 { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 1, _index * 1, bytes((_slice)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 1, _index * 1, bytes((_slice)), _fieldLayout); } } @@ -378,7 +373,7 @@ library Dynamics2 { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 1, _index * 1, bytes((_slice)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 1, _index * 1, bytes((_slice)), _fieldLayout); } } @@ -387,7 +382,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, _fieldLayout); return (bytes(_blob)); } @@ -396,7 +391,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, _fieldLayout); return (bytes(_blob)); } @@ -405,7 +400,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 2, bytes((b)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 2, bytes((b)), _fieldLayout); } /** Set b (using the specified store) */ @@ -413,7 +408,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 2, bytes((b)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 2, bytes((b)), _fieldLayout); } /** Get the length of b */ @@ -421,7 +416,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 2, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 2, _fieldLayout); unchecked { return _byteLength / 1; } @@ -432,7 +427,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 2, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 2, _fieldLayout); unchecked { return _byteLength / 1; } @@ -451,7 +446,7 @@ library Dynamics2 { _tableId, _keyTuple, 2, - getFieldLayout(), + _fieldLayout, _index * 1, (_index + 1) * 1 ); @@ -468,7 +463,7 @@ library Dynamics2 { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 2, getFieldLayout(), _index * 1, (_index + 1) * 1); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 2, _fieldLayout, _index * 1, (_index + 1) * 1); return (bytes(_blob)); } } @@ -478,7 +473,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 2, bytes((_slice)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 2, bytes((_slice)), _fieldLayout); } /** Push a slice to b (using the specified store) */ @@ -486,7 +481,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 2, bytes((_slice)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 2, bytes((_slice)), _fieldLayout); } /** Pop a slice from b */ @@ -494,7 +489,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 2, 1, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 2, 1, _fieldLayout); } /** Pop a slice from b (using the specified store) */ @@ -502,7 +497,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 2, 1, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 2, 1, _fieldLayout); } /** @@ -514,7 +509,7 @@ library Dynamics2 { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 2, _index * 1, bytes((_slice)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 2, _index * 1, bytes((_slice)), _fieldLayout); } } @@ -527,7 +522,7 @@ library Dynamics2 { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 2, _index * 1, bytes((_slice)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 2, _index * 1, bytes((_slice)), _fieldLayout); } } @@ -536,7 +531,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -545,7 +540,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -558,7 +553,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using individual values (using the specified store) */ @@ -570,7 +565,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using the data struct */ @@ -654,7 +649,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -662,6 +657,6 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/cli/contracts/src/codegen/tables/Ephemeral.sol b/packages/cli/contracts/src/codegen/tables/Ephemeral.sol index 0742a42eb8..31b8000e6b 100644 --- a/packages/cli/contracts/src/codegen/tables/Ephemeral.sol +++ b/packages/cli/contracts/src/codegen/tables/Ephemeral.sol @@ -21,13 +21,14 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Ephemeral"))); bytes32 constant EphemeralTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0020010020000000000000000000000000000000000000000000000000000000 +); + library Ephemeral { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](1); - _fieldLayout[0] = 32; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -60,19 +61,12 @@ library Ephemeral { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Emit the ephemeral event using individual values */ @@ -85,7 +79,7 @@ library Ephemeral { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.emitEphemeralRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + StoreSwitch.emitEphemeralRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Emit the ephemeral event using individual values (using the specified store) */ @@ -98,7 +92,7 @@ library Ephemeral { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.emitEphemeralRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + _store.emitEphemeralRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Tightly pack static data using this table's schema */ diff --git a/packages/cli/contracts/src/codegen/tables/Singleton.sol b/packages/cli/contracts/src/codegen/tables/Singleton.sol index 29ef27a1d8..d331c6eda3 100644 --- a/packages/cli/contracts/src/codegen/tables/Singleton.sol +++ b/packages/cli/contracts/src/codegen/tables/Singleton.sol @@ -21,13 +21,14 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Singleton"))); bytes32 constant SingletonTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0020010320000000000000000000000000000000000000000000000000000000 +); + library Singleton { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](1); - _fieldLayout[0] = 32; - - return FieldLayoutLib.encode(_fieldLayout, 3); + return _fieldLayout; } /** Get the table's key schema */ @@ -64,26 +65,19 @@ library Singleton { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get v1 */ function getV1() internal view returns (int256 v1) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (int256(uint256(Bytes.slice32(_blob, 0)))); } @@ -91,7 +85,7 @@ library Singleton { function getV1(IStore _store) internal view returns (int256 v1) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (int256(uint256(Bytes.slice32(_blob, 0)))); } @@ -99,21 +93,21 @@ library Singleton { function setV1(int256 v1) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((v1)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((v1)), _fieldLayout); } /** Set v1 (using the specified store) */ function setV1(IStore _store, int256 v1) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((v1)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((v1)), _fieldLayout); } /** Get v2 */ function getV2() internal view returns (uint32[2] memory v2) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, _fieldLayout); return toStaticArray_uint32_2(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } @@ -121,7 +115,7 @@ library Singleton { function getV2(IStore _store) internal view returns (uint32[2] memory v2) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, _fieldLayout); return toStaticArray_uint32_2(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } @@ -129,21 +123,21 @@ library Singleton { function setV2(uint32[2] memory v2) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setField(_tableId, _keyTuple, 1, EncodeArray.encode(fromStaticArray_uint32_2(v2)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 1, EncodeArray.encode(fromStaticArray_uint32_2(v2)), _fieldLayout); } /** Set v2 (using the specified store) */ function setV2(IStore _store, uint32[2] memory v2) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.setField(_tableId, _keyTuple, 1, EncodeArray.encode(fromStaticArray_uint32_2(v2)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 1, EncodeArray.encode(fromStaticArray_uint32_2(v2)), _fieldLayout); } /** Get the length of v2 */ function lengthV2() internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](0); - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 1, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 1, _fieldLayout); unchecked { return _byteLength / 4; } @@ -153,7 +147,7 @@ library Singleton { function lengthV2(IStore _store) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](0); - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 1, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 1, _fieldLayout); unchecked { return _byteLength / 4; } @@ -171,7 +165,7 @@ library Singleton { _tableId, _keyTuple, 1, - getFieldLayout(), + _fieldLayout, _index * 4, (_index + 1) * 4 ); @@ -187,7 +181,7 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 1, getFieldLayout(), _index * 4, (_index + 1) * 4); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 1, _fieldLayout, _index * 4, (_index + 1) * 4); return (uint32(Bytes.slice4(_blob, 0))); } } @@ -196,28 +190,28 @@ library Singleton { function pushV2(uint32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), _fieldLayout); } /** Push an element to v2 (using the specified store) */ function pushV2(IStore _store, uint32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), _fieldLayout); } /** Pop an element from v2 */ function popV2() internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.popFromField(_tableId, _keyTuple, 1, 4, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 1, 4, _fieldLayout); } /** Pop an element from v2 (using the specified store) */ function popV2(IStore _store) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.popFromField(_tableId, _keyTuple, 1, 4, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 1, 4, _fieldLayout); } /** @@ -228,7 +222,7 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 1, _index * 4, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 1, _index * 4, abi.encodePacked((_element)), _fieldLayout); } } @@ -240,7 +234,7 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - _store.updateInField(_tableId, _keyTuple, 1, _index * 4, abi.encodePacked((_element)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 1, _index * 4, abi.encodePacked((_element)), _fieldLayout); } } @@ -248,7 +242,7 @@ library Singleton { function getV3() internal view returns (uint32[2] memory v3) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, _fieldLayout); return toStaticArray_uint32_2(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } @@ -256,7 +250,7 @@ library Singleton { function getV3(IStore _store) internal view returns (uint32[2] memory v3) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, _fieldLayout); return toStaticArray_uint32_2(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } @@ -264,21 +258,21 @@ library Singleton { function setV3(uint32[2] memory v3) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setField(_tableId, _keyTuple, 2, EncodeArray.encode(fromStaticArray_uint32_2(v3)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 2, EncodeArray.encode(fromStaticArray_uint32_2(v3)), _fieldLayout); } /** Set v3 (using the specified store) */ function setV3(IStore _store, uint32[2] memory v3) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.setField(_tableId, _keyTuple, 2, EncodeArray.encode(fromStaticArray_uint32_2(v3)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 2, EncodeArray.encode(fromStaticArray_uint32_2(v3)), _fieldLayout); } /** Get the length of v3 */ function lengthV3() internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](0); - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 2, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 2, _fieldLayout); unchecked { return _byteLength / 4; } @@ -288,7 +282,7 @@ library Singleton { function lengthV3(IStore _store) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](0); - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 2, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 2, _fieldLayout); unchecked { return _byteLength / 4; } @@ -306,7 +300,7 @@ library Singleton { _tableId, _keyTuple, 2, - getFieldLayout(), + _fieldLayout, _index * 4, (_index + 1) * 4 ); @@ -322,7 +316,7 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 2, getFieldLayout(), _index * 4, (_index + 1) * 4); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 2, _fieldLayout, _index * 4, (_index + 1) * 4); return (uint32(Bytes.slice4(_blob, 0))); } } @@ -331,28 +325,28 @@ library Singleton { function pushV3(uint32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), _fieldLayout); } /** Push an element to v3 (using the specified store) */ function pushV3(IStore _store, uint32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), _fieldLayout); } /** Pop an element from v3 */ function popV3() internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.popFromField(_tableId, _keyTuple, 2, 4, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 2, 4, _fieldLayout); } /** Pop an element from v3 (using the specified store) */ function popV3(IStore _store) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.popFromField(_tableId, _keyTuple, 2, 4, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 2, 4, _fieldLayout); } /** @@ -363,7 +357,7 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 2, _index * 4, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 2, _index * 4, abi.encodePacked((_element)), _fieldLayout); } } @@ -375,7 +369,7 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - _store.updateInField(_tableId, _keyTuple, 2, _index * 4, abi.encodePacked((_element)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 2, _index * 4, abi.encodePacked((_element)), _fieldLayout); } } @@ -383,7 +377,7 @@ library Singleton { function getV4() internal view returns (uint32[1] memory v4) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, _fieldLayout); return toStaticArray_uint32_1(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } @@ -391,7 +385,7 @@ library Singleton { function getV4(IStore _store) internal view returns (uint32[1] memory v4) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, _fieldLayout); return toStaticArray_uint32_1(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } @@ -399,21 +393,21 @@ library Singleton { function setV4(uint32[1] memory v4) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setField(_tableId, _keyTuple, 3, EncodeArray.encode(fromStaticArray_uint32_1(v4)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 3, EncodeArray.encode(fromStaticArray_uint32_1(v4)), _fieldLayout); } /** Set v4 (using the specified store) */ function setV4(IStore _store, uint32[1] memory v4) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.setField(_tableId, _keyTuple, 3, EncodeArray.encode(fromStaticArray_uint32_1(v4)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 3, EncodeArray.encode(fromStaticArray_uint32_1(v4)), _fieldLayout); } /** Get the length of v4 */ function lengthV4() internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](0); - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 3, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 3, _fieldLayout); unchecked { return _byteLength / 4; } @@ -423,7 +417,7 @@ library Singleton { function lengthV4(IStore _store) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](0); - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 3, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 3, _fieldLayout); unchecked { return _byteLength / 4; } @@ -441,7 +435,7 @@ library Singleton { _tableId, _keyTuple, 3, - getFieldLayout(), + _fieldLayout, _index * 4, (_index + 1) * 4 ); @@ -457,7 +451,7 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 3, getFieldLayout(), _index * 4, (_index + 1) * 4); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 3, _fieldLayout, _index * 4, (_index + 1) * 4); return (uint32(Bytes.slice4(_blob, 0))); } } @@ -466,28 +460,28 @@ library Singleton { function pushV4(uint32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), _fieldLayout); } /** Push an element to v4 (using the specified store) */ function pushV4(IStore _store, uint32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), _fieldLayout); } /** Pop an element from v4 */ function popV4() internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.popFromField(_tableId, _keyTuple, 3, 4, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 3, 4, _fieldLayout); } /** Pop an element from v4 (using the specified store) */ function popV4(IStore _store) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.popFromField(_tableId, _keyTuple, 3, 4, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 3, 4, _fieldLayout); } /** @@ -498,7 +492,7 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 3, _index * 4, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 3, _index * 4, abi.encodePacked((_element)), _fieldLayout); } } @@ -510,7 +504,7 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - _store.updateInField(_tableId, _keyTuple, 3, _index * 4, abi.encodePacked((_element)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 3, _index * 4, abi.encodePacked((_element)), _fieldLayout); } } @@ -518,7 +512,7 @@ library Singleton { function get() internal view returns (int256 v1, uint32[2] memory v2, uint32[2] memory v3, uint32[1] memory v4) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -528,7 +522,7 @@ library Singleton { ) internal view returns (int256 v1, uint32[2] memory v2, uint32[2] memory v3, uint32[1] memory v4) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -541,7 +535,7 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using individual values (using the specified store) */ @@ -553,7 +547,7 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); - _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** @@ -649,14 +643,14 @@ library Singleton { function deleteRecord() internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/cli/contracts/src/codegen/tables/Statics.sol b/packages/cli/contracts/src/codegen/tables/Statics.sol index 9d6fcab330..14e42c5370 100644 --- a/packages/cli/contracts/src/codegen/tables/Statics.sol +++ b/packages/cli/contracts/src/codegen/tables/Statics.sol @@ -24,6 +24,10 @@ import { Enum2, Enum1 } from "./../Types.sol"; bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Statics"))); bytes32 constant StaticsTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x004a060020041014010100000000000000000000000000000000000000000000 +); + struct StaticsData { uint256 v1; int32 v2; @@ -36,15 +40,7 @@ struct StaticsData { library Statics { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](6); - _fieldLayout[0] = 32; - _fieldLayout[1] = 4; - _fieldLayout[2] = 16; - _fieldLayout[3] = 20; - _fieldLayout[4] = 1; - _fieldLayout[5] = 1; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -97,19 +93,12 @@ library Statics { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get v1 */ @@ -122,7 +111,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint256(Bytes.slice32(_blob, 0))); } @@ -144,7 +133,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint256(Bytes.slice32(_blob, 0))); } @@ -158,7 +147,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((v1)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((v1)), _fieldLayout); } /** Set v1 (using the specified store) */ @@ -171,7 +160,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((v1)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((v1)), _fieldLayout); } /** Get v2 */ @@ -184,7 +173,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, _fieldLayout); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -206,7 +195,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, _fieldLayout); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -220,7 +209,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((v2)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((v2)), _fieldLayout); } /** Set v2 (using the specified store) */ @@ -233,7 +222,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((v2)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((v2)), _fieldLayout); } /** Get v3 */ @@ -246,7 +235,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, _fieldLayout); return (Bytes.slice16(_blob, 0)); } @@ -268,7 +257,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, _fieldLayout); return (Bytes.slice16(_blob, 0)); } @@ -282,7 +271,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - StoreSwitch.setField(_tableId, _keyTuple, 2, abi.encodePacked((v3)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 2, abi.encodePacked((v3)), _fieldLayout); } /** Set v3 (using the specified store) */ @@ -295,7 +284,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - _store.setField(_tableId, _keyTuple, 2, abi.encodePacked((v3)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 2, abi.encodePacked((v3)), _fieldLayout); } /** Get v4 */ @@ -308,7 +297,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, _fieldLayout); return (address(Bytes.slice20(_blob, 0))); } @@ -330,7 +319,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, _fieldLayout); return (address(Bytes.slice20(_blob, 0))); } @@ -344,7 +333,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - StoreSwitch.setField(_tableId, _keyTuple, 3, abi.encodePacked((v4)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 3, abi.encodePacked((v4)), _fieldLayout); } /** Set v4 (using the specified store) */ @@ -357,7 +346,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - _store.setField(_tableId, _keyTuple, 3, abi.encodePacked((v4)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 3, abi.encodePacked((v4)), _fieldLayout); } /** Get v5 */ @@ -370,7 +359,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 4, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 4, _fieldLayout); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -392,7 +381,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 4, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 4, _fieldLayout); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -406,7 +395,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - StoreSwitch.setField(_tableId, _keyTuple, 4, abi.encodePacked((v5)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 4, abi.encodePacked((v5)), _fieldLayout); } /** Set v5 (using the specified store) */ @@ -419,7 +408,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - _store.setField(_tableId, _keyTuple, 4, abi.encodePacked((v5)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 4, abi.encodePacked((v5)), _fieldLayout); } /** Get v6 */ @@ -432,7 +421,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 5, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 5, _fieldLayout); return Enum1(uint8(Bytes.slice1(_blob, 0))); } @@ -454,7 +443,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 5, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 5, _fieldLayout); return Enum1(uint8(Bytes.slice1(_blob, 0))); } @@ -468,7 +457,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - StoreSwitch.setField(_tableId, _keyTuple, 5, abi.encodePacked(uint8(v6)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 5, abi.encodePacked(uint8(v6)), _fieldLayout); } /** Set v6 (using the specified store) */ @@ -481,7 +470,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - _store.setField(_tableId, _keyTuple, 5, abi.encodePacked(uint8(v6)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 5, abi.encodePacked(uint8(v6)), _fieldLayout); } /** Get the full data */ @@ -501,7 +490,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -523,7 +512,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -555,7 +544,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using individual values (using the specified store) */ @@ -587,7 +576,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using the data struct */ @@ -683,7 +672,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -696,7 +685,7 @@ library Statics { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/store/gas-report.json b/packages/store/gas-report.json index 68427b3528..536bc29c03 100644 --- a/packages/store/gas-report.json +++ b/packages/store/gas-report.json @@ -339,7 +339,7 @@ "file": "test/KeyEncoding.t.sol", "test": "testRegisterAndGetFieldLayout", "name": "register KeyEncoding table", - "gasUsed": 697932 + "gasUsed": 691073 }, { "file": "test/Mixed.t.sol", @@ -351,19 +351,19 @@ "file": "test/Mixed.t.sol", "test": "testRegisterAndGetFieldLayout", "name": "register Mixed table", - "gasUsed": 560113 + "gasUsed": 552911 }, { "file": "test/Mixed.t.sol", "test": "testSetAndGet", "name": "set record in Mixed", - "gasUsed": 108795 + "gasUsed": 105912 }, { "file": "test/Mixed.t.sol", "test": "testSetAndGet", "name": "get record from Mixed", - "gasUsed": 9639 + "gasUsed": 7821 }, { "file": "test/PackedCounter.t.sol", @@ -549,13 +549,13 @@ "file": "test/StoreCoreDynamic.t.sol", "test": "testGetFieldSlice", "name": "get field slice (cold, 1 slot)", - "gasUsed": 8425 + "gasUsed": 8424 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testGetFieldSlice", "name": "get field slice (warm, 1 slot)", - "gasUsed": 2493 + "gasUsed": 2492 }, { "file": "test/StoreCoreDynamic.t.sol", @@ -567,13 +567,13 @@ "file": "test/StoreCoreDynamic.t.sol", "test": "testGetFieldSlice", "name": "get field slice (warm, 2 slots)", - "gasUsed": 4725 + "gasUsed": 4724 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testGetSecondFieldLength", "name": "get field length (cold, 1 slot)", - "gasUsed": 7966 + "gasUsed": 7965 }, { "file": "test/StoreCoreDynamic.t.sol", @@ -597,25 +597,25 @@ "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromSecondField", "name": "pop from field (cold, 1 slot, 1 uint32 item)", - "gasUsed": 23281 + "gasUsed": 22215 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromSecondField", "name": "pop from field (warm, 1 slot, 1 uint32 item)", - "gasUsed": 17317 + "gasUsed": 16250 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromThirdField", "name": "pop from field (cold, 2 slots, 10 uint32 items)", - "gasUsed": 25478 + "gasUsed": 24412 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromThirdField", "name": "pop from field (warm, 2 slots, 10 uint32 items)", - "gasUsed": 17514 + "gasUsed": 16447 }, { "file": "test/StoreCoreGas.t.sol", @@ -651,115 +651,115 @@ "file": "test/StoreCoreGas.t.sol", "test": "testDeleteData", "name": "delete record (complex data, 3 slots)", - "gasUsed": 9470 + "gasUsed": 8403 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHasFieldLayout", "name": "Check for existence of table (existent)", - "gasUsed": 4465 + "gasUsed": 2303 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHasFieldLayout", "name": "check for existence of table (non-existent)", - "gasUsed": 6468 + "gasUsed": 4305 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooks", "name": "register subscriber", - "gasUsed": 62552 + "gasUsed": 60434 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooks", "name": "set record on table with subscriber", - "gasUsed": 76175 + "gasUsed": 74048 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooks", "name": "set static field on table with subscriber", - "gasUsed": 25942 + "gasUsed": 23815 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooks", "name": "delete record on table with subscriber", - "gasUsed": 21493 + "gasUsed": 19366 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooksDynamicData", "name": "register subscriber", - "gasUsed": 62552 + "gasUsed": 60434 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooksDynamicData", "name": "set (dynamic) record on table with subscriber", - "gasUsed": 169944 + "gasUsed": 167817 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooksDynamicData", "name": "set (dynamic) field on table with subscriber", - "gasUsed": 29579 + "gasUsed": 27452 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooksDynamicData", "name": "delete (dynamic) record on table with subscriber", - "gasUsed": 22878 + "gasUsed": 20750 }, { "file": "test/StoreCoreGas.t.sol", "test": "testPushToField", "name": "push to field (1 slot, 1 uint32 item)", - "gasUsed": 15223 + "gasUsed": 14154 }, { "file": "test/StoreCoreGas.t.sol", "test": "testPushToField", "name": "push to field (2 slots, 10 uint32 items)", - "gasUsed": 37997 + "gasUsed": 36926 }, { "file": "test/StoreCoreGas.t.sol", "test": "testRegisterAndGetFieldLayout", "name": "StoreCore: register table", - "gasUsed": 620426 + "gasUsed": 615037 }, { "file": "test/StoreCoreGas.t.sol", "test": "testRegisterAndGetFieldLayout", "name": "StoreCore: get field layout (warm)", - "gasUsed": 4479 + "gasUsed": 2317 }, { "file": "test/StoreCoreGas.t.sol", "test": "testRegisterAndGetFieldLayout", "name": "StoreCore: get value schema (warm)", - "gasUsed": 5033 + "gasUsed": 2870 }, { "file": "test/StoreCoreGas.t.sol", "test": "testRegisterAndGetFieldLayout", "name": "StoreCore: get key schema (warm)", - "gasUsed": 9281 + "gasUsed": 4952 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetDynamicData", "name": "set complex record with dynamic data (4 slots)", - "gasUsed": 104895 + "gasUsed": 103828 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetDynamicData", "name": "get complex record with dynamic data (4 slots)", - "gasUsed": 5105 + "gasUsed": 5104 }, { "file": "test/StoreCoreGas.t.sol", @@ -777,7 +777,7 @@ "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetDynamicDataLength", "name": "set dynamic length of dynamic index 0", - "gasUsed": 23082 + "gasUsed": 23081 }, { "file": "test/StoreCoreGas.t.sol", @@ -795,19 +795,19 @@ "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetField", "name": "set static field (1 slot)", - "gasUsed": 34114 + "gasUsed": 33047 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetField", "name": "get static field (1 slot)", - "gasUsed": 1482 + "gasUsed": 1483 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetField", "name": "set static field (overlap 2 slot)", - "gasUsed": 32761 + "gasUsed": 31694 }, { "file": "test/StoreCoreGas.t.sol", @@ -819,7 +819,7 @@ "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetField", "name": "set dynamic field (1 slot, first dynamic field)", - "gasUsed": 55894 + "gasUsed": 54826 }, { "file": "test/StoreCoreGas.t.sol", @@ -831,31 +831,31 @@ "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetField", "name": "set dynamic field (1 slot, second dynamic field)", - "gasUsed": 34134 + "gasUsed": 33064 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetField", "name": "get dynamic field (1 slot, second dynamic field)", - "gasUsed": 2635 + "gasUsed": 2633 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetStaticData", "name": "set static record (1 slot)", - "gasUsed": 34695 + "gasUsed": 33629 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetStaticData", "name": "get static record (1 slot)", - "gasUsed": 1734 + "gasUsed": 1733 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetStaticDataSpanningWords", "name": "set static record (2 slots)", - "gasUsed": 57199 + "gasUsed": 56133 }, { "file": "test/StoreCoreGas.t.sol", @@ -867,13 +867,13 @@ "file": "test/StoreCoreGas.t.sol", "test": "testUpdateInField", "name": "update in field (1 slot, 1 uint32 item)", - "gasUsed": 16261 + "gasUsed": 15192 }, { "file": "test/StoreCoreGas.t.sol", "test": "testUpdateInField", "name": "push to field (2 slots, 6 uint64 items)", - "gasUsed": 17082 + "gasUsed": 16012 }, { "file": "test/StoreHook.t.sol", @@ -921,121 +921,121 @@ "file": "test/tables/Callbacks.t.sol", "test": "testSetAndGet", "name": "Callbacks: set field", - "gasUsed": 60015 + "gasUsed": 57881 }, { "file": "test/tables/Callbacks.t.sol", "test": "testSetAndGet", "name": "Callbacks: get field (warm)", - "gasUsed": 5160 + "gasUsed": 4092 }, { "file": "test/tables/Callbacks.t.sol", "test": "testSetAndGet", "name": "Callbacks: push 1 element", - "gasUsed": 39345 + "gasUsed": 37215 }, { "file": "test/tables/StoreHooks.t.sol", "test": "testOneSlot", "name": "StoreHooks: set field with one elements (cold)", - "gasUsed": 61997 + "gasUsed": 59867 }, { "file": "test/tables/StoreHooks.t.sol", "test": "testTable", "name": "StoreHooks: set field (cold)", - "gasUsed": 61997 + "gasUsed": 59866 }, { "file": "test/tables/StoreHooks.t.sol", "test": "testTable", "name": "StoreHooks: get field (warm)", - "gasUsed": 5136 + "gasUsed": 4071 }, { "file": "test/tables/StoreHooks.t.sol", "test": "testTable", "name": "StoreHooks: push 1 element (cold)", - "gasUsed": 19410 + "gasUsed": 17284 }, { "file": "test/tables/StoreHooks.t.sol", "test": "testTable", "name": "StoreHooks: pop 1 element (warm)", - "gasUsed": 15844 + "gasUsed": 13710 }, { "file": "test/tables/StoreHooks.t.sol", "test": "testTable", "name": "StoreHooks: push 1 element (warm)", - "gasUsed": 17479 + "gasUsed": 15349 }, { "file": "test/tables/StoreHooks.t.sol", "test": "testTable", "name": "StoreHooks: update 1 element (warm)", - "gasUsed": 38603 + "gasUsed": 36470 }, { "file": "test/tables/StoreHooks.t.sol", "test": "testTable", "name": "StoreHooks: delete record (warm)", - "gasUsed": 10994 + "gasUsed": 8856 }, { "file": "test/tables/StoreHooks.t.sol", "test": "testTable", "name": "StoreHooks: set field (warm)", - "gasUsed": 34250 + "gasUsed": 32110 }, { "file": "test/tables/StoreHooks.t.sol", "test": "testThreeSlots", "name": "StoreHooks: set field with three elements (cold)", - "gasUsed": 84688 + "gasUsed": 82558 }, { "file": "test/tables/StoreHooks.t.sol", "test": "testTwoSlots", "name": "StoreHooks: set field with two elements (cold)", - "gasUsed": 84600 + "gasUsed": 82469 }, { "file": "test/tables/StoreHooksColdLoad.t.sol", "test": "testDelete", "name": "StoreHooks: delete record (cold)", - "gasUsed": 19792 + "gasUsed": 17662 }, { "file": "test/tables/StoreHooksColdLoad.t.sol", "test": "testGet", "name": "StoreHooks: get field (cold)", - "gasUsed": 11129 + "gasUsed": 10064 }, { "file": "test/tables/StoreHooksColdLoad.t.sol", "test": "testGetItem", "name": "StoreHooks: get 1 element (cold)", - "gasUsed": 7644 + "gasUsed": 6582 }, { "file": "test/tables/StoreHooksColdLoad.t.sol", "test": "testLength", "name": "StoreHooks: get length (cold)", - "gasUsed": 7127 + "gasUsed": 6062 }, { "file": "test/tables/StoreHooksColdLoad.t.sol", "test": "testPop", "name": "StoreHooks: pop 1 element (cold)", - "gasUsed": 26291 + "gasUsed": 24160 }, { "file": "test/tables/StoreHooksColdLoad.t.sol", "test": "testUpdate", "name": "StoreHooks: update 1 element (cold)", - "gasUsed": 28565 + "gasUsed": 26441 }, { "file": "test/tightcoder/DecodeSlice.t.sol", @@ -1089,18 +1089,18 @@ "file": "test/Vector2.t.sol", "test": "testRegisterAndGetFieldLayout", "name": "register Vector2 field layout", - "gasUsed": 421554 + "gasUsed": 414353 }, { "file": "test/Vector2.t.sol", "test": "testSetAndGet", "name": "set Vector2 record", - "gasUsed": 37413 + "gasUsed": 34533 }, { "file": "test/Vector2.t.sol", "test": "testSetAndGet", "name": "get Vector2 record", - "gasUsed": 4472 + "gasUsed": 2656 } ] diff --git a/packages/store/src/codegen/tables/Callbacks.sol b/packages/store/src/codegen/tables/Callbacks.sol index 5a370f8a20..8d9b02a251 100644 --- a/packages/store/src/codegen/tables/Callbacks.sol +++ b/packages/store/src/codegen/tables/Callbacks.sol @@ -21,12 +21,14 @@ import { PackedCounter, PackedCounterLib } from "../../PackedCounter.sol"; bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16("mudstore"), bytes16("Callbacks"))); bytes32 constant CallbacksTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0000000100000000000000000000000000000000000000000000000000000000 +); + library Callbacks { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](0); - - return FieldLayoutLib.encode(_fieldLayout, 1); + return _fieldLayout; } /** Get the table's key schema */ @@ -59,19 +61,12 @@ library Callbacks { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ @@ -79,7 +74,7 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes24()); } @@ -88,7 +83,7 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes24()); } @@ -97,7 +92,7 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); } /** Set value (using the specified store) */ @@ -105,7 +100,7 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); } /** Get the length of value */ @@ -113,7 +108,7 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); unchecked { return _byteLength / 24; } @@ -124,7 +119,7 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); unchecked { return _byteLength / 24; } @@ -143,7 +138,7 @@ library Callbacks { _tableId, _keyTuple, 0, - getFieldLayout(), + _fieldLayout, _index * 24, (_index + 1) * 24 ); @@ -160,14 +155,7 @@ library Callbacks { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice( - _tableId, - _keyTuple, - 0, - getFieldLayout(), - _index * 24, - (_index + 1) * 24 - ); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, _fieldLayout, _index * 24, (_index + 1) * 24); return (Bytes.slice24(_blob, 0)); } } @@ -177,7 +165,7 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } /** Push an element to value (using the specified store) */ @@ -185,7 +173,7 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } /** Pop an element from value */ @@ -193,7 +181,7 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 24, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 0, 24, _fieldLayout); } /** Pop an element from value (using the specified store) */ @@ -201,7 +189,7 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 0, 24, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 0, 24, _fieldLayout); } /** @@ -213,7 +201,7 @@ library Callbacks { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 24, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 24, abi.encodePacked((_element)), _fieldLayout); } } @@ -226,7 +214,7 @@ library Callbacks { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 0, _index * 24, abi.encodePacked((_element)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 0, _index * 24, abi.encodePacked((_element)), _fieldLayout); } } @@ -265,7 +253,7 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -273,6 +261,6 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/store/src/codegen/tables/Hooks.sol b/packages/store/src/codegen/tables/Hooks.sol index 4d12e0babb..3332c321e4 100644 --- a/packages/store/src/codegen/tables/Hooks.sol +++ b/packages/store/src/codegen/tables/Hooks.sol @@ -18,12 +18,14 @@ import { FieldLayout, FieldLayoutLib } from "../../FieldLayout.sol"; import { Schema, SchemaLib } from "../../Schema.sol"; import { PackedCounter, PackedCounterLib } from "../../PackedCounter.sol"; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0000000100000000000000000000000000000000000000000000000000000000 +); + library Hooks { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](0); - - return FieldLayoutLib.encode(_fieldLayout, 1); + return _fieldLayout; } /** Get the table's key schema */ @@ -56,19 +58,12 @@ library Hooks { /** Register the table with its config */ function register(bytes32 _tableId) internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store, bytes32 _tableId) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ @@ -76,7 +71,7 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } @@ -85,7 +80,7 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } @@ -94,7 +89,7 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); } /** Set value (using the specified store) */ @@ -102,7 +97,7 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); } /** Get the length of value */ @@ -110,7 +105,7 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); unchecked { return _byteLength / 21; } @@ -121,7 +116,7 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); unchecked { return _byteLength / 21; } @@ -140,7 +135,7 @@ library Hooks { _tableId, _keyTuple, 0, - getFieldLayout(), + _fieldLayout, _index * 21, (_index + 1) * 21 ); @@ -157,14 +152,7 @@ library Hooks { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice( - _tableId, - _keyTuple, - 0, - getFieldLayout(), - _index * 21, - (_index + 1) * 21 - ); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, _fieldLayout, _index * 21, (_index + 1) * 21); return (Bytes.slice21(_blob, 0)); } } @@ -174,7 +162,7 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } /** Push an element to value (using the specified store) */ @@ -182,7 +170,7 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } /** Pop an element from value */ @@ -190,7 +178,7 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 21, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 0, 21, _fieldLayout); } /** Pop an element from value (using the specified store) */ @@ -198,7 +186,7 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 0, 21, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 0, 21, _fieldLayout); } /** @@ -210,7 +198,7 @@ library Hooks { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 21, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 21, abi.encodePacked((_element)), _fieldLayout); } } @@ -223,7 +211,7 @@ library Hooks { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 0, _index * 21, abi.encodePacked((_element)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 0, _index * 21, abi.encodePacked((_element)), _fieldLayout); } } @@ -262,7 +250,7 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -270,6 +258,6 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/store/src/codegen/tables/KeyEncoding.sol b/packages/store/src/codegen/tables/KeyEncoding.sol index f8db386345..b23620d632 100644 --- a/packages/store/src/codegen/tables/KeyEncoding.sol +++ b/packages/store/src/codegen/tables/KeyEncoding.sol @@ -24,13 +24,14 @@ import { ExampleEnum } from "./../Types.sol"; bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16("mudstore"), bytes16("KeyEncoding"))); bytes32 constant KeyEncodingTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0001010001000000000000000000000000000000000000000000000000000000 +); + library KeyEncoding { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](1); - _fieldLayout[0] = 1; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -73,19 +74,12 @@ library KeyEncoding { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ @@ -105,7 +99,7 @@ library KeyEncoding { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -127,7 +121,7 @@ library KeyEncoding { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -141,7 +135,7 @@ library KeyEncoding { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } /** Set value (using the specified store) */ @@ -163,7 +157,7 @@ library KeyEncoding { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } /** Tightly pack static data using this table's schema */ @@ -211,7 +205,7 @@ library KeyEncoding { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -224,7 +218,7 @@ library KeyEncoding { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/store/src/codegen/tables/Mixed.sol b/packages/store/src/codegen/tables/Mixed.sol index 9be293952b..d6060d0c95 100644 --- a/packages/store/src/codegen/tables/Mixed.sol +++ b/packages/store/src/codegen/tables/Mixed.sol @@ -21,6 +21,10 @@ import { PackedCounter, PackedCounterLib } from "../../PackedCounter.sol"; bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16("mudstore"), bytes16("Mixed"))); bytes32 constant MixedTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0014020204100000000000000000000000000000000000000000000000000000 +); + struct MixedData { uint32 u32; uint128 u128; @@ -31,11 +35,7 @@ struct MixedData { library Mixed { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](2); - _fieldLayout[0] = 4; - _fieldLayout[1] = 16; - - return FieldLayoutLib.encode(_fieldLayout, 2); + return _fieldLayout; } /** Get the table's key schema */ @@ -74,19 +74,12 @@ library Mixed { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get u32 */ @@ -94,7 +87,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint32(Bytes.slice4(_blob, 0))); } @@ -103,7 +96,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint32(Bytes.slice4(_blob, 0))); } @@ -112,7 +105,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((u32)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((u32)), _fieldLayout); } /** Set u32 (using the specified store) */ @@ -120,7 +113,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((u32)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((u32)), _fieldLayout); } /** Get u128 */ @@ -128,7 +121,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, _fieldLayout); return (uint128(Bytes.slice16(_blob, 0))); } @@ -137,7 +130,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, _fieldLayout); return (uint128(Bytes.slice16(_blob, 0))); } @@ -146,7 +139,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((u128)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((u128)), _fieldLayout); } /** Set u128 (using the specified store) */ @@ -154,7 +147,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((u128)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((u128)), _fieldLayout); } /** Get a32 */ @@ -162,7 +155,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } @@ -171,7 +164,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } @@ -180,7 +173,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 2, EncodeArray.encode((a32)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 2, EncodeArray.encode((a32)), _fieldLayout); } /** Set a32 (using the specified store) */ @@ -188,7 +181,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 2, EncodeArray.encode((a32)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 2, EncodeArray.encode((a32)), _fieldLayout); } /** Get the length of a32 */ @@ -196,7 +189,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 2, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 2, _fieldLayout); unchecked { return _byteLength / 4; } @@ -207,7 +200,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 2, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 2, _fieldLayout); unchecked { return _byteLength / 4; } @@ -226,7 +219,7 @@ library Mixed { _tableId, _keyTuple, 2, - getFieldLayout(), + _fieldLayout, _index * 4, (_index + 1) * 4 ); @@ -243,7 +236,7 @@ library Mixed { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 2, getFieldLayout(), _index * 4, (_index + 1) * 4); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 2, _fieldLayout, _index * 4, (_index + 1) * 4); return (uint32(Bytes.slice4(_blob, 0))); } } @@ -253,7 +246,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), _fieldLayout); } /** Push an element to a32 (using the specified store) */ @@ -261,7 +254,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), _fieldLayout); } /** Pop an element from a32 */ @@ -269,7 +262,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 2, 4, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 2, 4, _fieldLayout); } /** Pop an element from a32 (using the specified store) */ @@ -277,7 +270,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 2, 4, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 2, 4, _fieldLayout); } /** @@ -289,7 +282,7 @@ library Mixed { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 2, _index * 4, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 2, _index * 4, abi.encodePacked((_element)), _fieldLayout); } } @@ -302,7 +295,7 @@ library Mixed { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 2, _index * 4, abi.encodePacked((_element)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 2, _index * 4, abi.encodePacked((_element)), _fieldLayout); } } @@ -311,7 +304,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, _fieldLayout); return (string(_blob)); } @@ -320,7 +313,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, _fieldLayout); return (string(_blob)); } @@ -329,7 +322,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 3, bytes((s)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 3, bytes((s)), _fieldLayout); } /** Set s (using the specified store) */ @@ -337,7 +330,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 3, bytes((s)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 3, bytes((s)), _fieldLayout); } /** Get the length of s */ @@ -345,7 +338,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 3, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 3, _fieldLayout); unchecked { return _byteLength / 1; } @@ -356,7 +349,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 3, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 3, _fieldLayout); unchecked { return _byteLength / 1; } @@ -375,7 +368,7 @@ library Mixed { _tableId, _keyTuple, 3, - getFieldLayout(), + _fieldLayout, _index * 1, (_index + 1) * 1 ); @@ -392,7 +385,7 @@ library Mixed { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 3, getFieldLayout(), _index * 1, (_index + 1) * 1); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 3, _fieldLayout, _index * 1, (_index + 1) * 1); return (string(_blob)); } } @@ -402,7 +395,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 3, bytes((_slice)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 3, bytes((_slice)), _fieldLayout); } /** Push a slice to s (using the specified store) */ @@ -410,7 +403,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 3, bytes((_slice)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 3, bytes((_slice)), _fieldLayout); } /** Pop a slice from s */ @@ -418,7 +411,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 3, 1, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 3, 1, _fieldLayout); } /** Pop a slice from s (using the specified store) */ @@ -426,7 +419,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 3, 1, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 3, 1, _fieldLayout); } /** @@ -438,7 +431,7 @@ library Mixed { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 3, _index * 1, bytes((_slice)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 3, _index * 1, bytes((_slice)), _fieldLayout); } } @@ -451,7 +444,7 @@ library Mixed { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 3, _index * 1, bytes((_slice)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 3, _index * 1, bytes((_slice)), _fieldLayout); } } @@ -460,7 +453,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -469,7 +462,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -483,7 +476,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using individual values (using the specified store) */ @@ -496,7 +489,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using the data struct */ @@ -580,7 +573,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -588,6 +581,6 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/store/src/codegen/tables/StoreHooks.sol b/packages/store/src/codegen/tables/StoreHooks.sol index fe82a2a292..e997c1a4f9 100644 --- a/packages/store/src/codegen/tables/StoreHooks.sol +++ b/packages/store/src/codegen/tables/StoreHooks.sol @@ -21,12 +21,14 @@ import { PackedCounter, PackedCounterLib } from "../../PackedCounter.sol"; bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16("mudstore"), bytes16("StoreHooks"))); bytes32 constant StoreHooksTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0000000100000000000000000000000000000000000000000000000000000000 +); + library StoreHooks { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](0); - - return FieldLayoutLib.encode(_fieldLayout, 1); + return _fieldLayout; } /** Get the table's key schema */ @@ -59,19 +61,12 @@ library StoreHooks { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ @@ -79,7 +74,7 @@ library StoreHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } @@ -88,7 +83,7 @@ library StoreHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } @@ -97,7 +92,7 @@ library StoreHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); } /** Set value (using the specified store) */ @@ -105,7 +100,7 @@ library StoreHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); } /** Get the length of value */ @@ -113,7 +108,7 @@ library StoreHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); unchecked { return _byteLength / 21; } @@ -124,7 +119,7 @@ library StoreHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); unchecked { return _byteLength / 21; } @@ -143,7 +138,7 @@ library StoreHooks { _tableId, _keyTuple, 0, - getFieldLayout(), + _fieldLayout, _index * 21, (_index + 1) * 21 ); @@ -160,14 +155,7 @@ library StoreHooks { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice( - _tableId, - _keyTuple, - 0, - getFieldLayout(), - _index * 21, - (_index + 1) * 21 - ); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, _fieldLayout, _index * 21, (_index + 1) * 21); return (Bytes.slice21(_blob, 0)); } } @@ -177,7 +165,7 @@ library StoreHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } /** Push an element to value (using the specified store) */ @@ -185,7 +173,7 @@ library StoreHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } /** Pop an element from value */ @@ -193,7 +181,7 @@ library StoreHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 21, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 0, 21, _fieldLayout); } /** Pop an element from value (using the specified store) */ @@ -201,7 +189,7 @@ library StoreHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 0, 21, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 0, 21, _fieldLayout); } /** @@ -213,7 +201,7 @@ library StoreHooks { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 21, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 21, abi.encodePacked((_element)), _fieldLayout); } } @@ -226,7 +214,7 @@ library StoreHooks { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 0, _index * 21, abi.encodePacked((_element)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 0, _index * 21, abi.encodePacked((_element)), _fieldLayout); } } @@ -265,7 +253,7 @@ library StoreHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -273,6 +261,6 @@ library StoreHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/store/src/codegen/tables/Tables.sol b/packages/store/src/codegen/tables/Tables.sol index 520bbd4497..b3e553b585 100644 --- a/packages/store/src/codegen/tables/Tables.sol +++ b/packages/store/src/codegen/tables/Tables.sol @@ -21,6 +21,10 @@ import { PackedCounter, PackedCounterLib } from "../../PackedCounter.sol"; bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16("mudstore"), bytes16("Tables"))); bytes32 constant TablesTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0060030220202000000000000000000000000000000000000000000000000000 +); + struct TablesData { bytes32 fieldLayout; bytes32 keySchema; @@ -32,12 +36,7 @@ struct TablesData { library Tables { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](3); - _fieldLayout[0] = 32; - _fieldLayout[1] = 32; - _fieldLayout[2] = 32; - - return FieldLayoutLib.encode(_fieldLayout, 2); + return _fieldLayout; } /** Get the table's key schema */ @@ -78,19 +77,12 @@ library Tables { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get fieldLayout */ @@ -98,7 +90,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (Bytes.slice32(_blob, 0)); } @@ -107,7 +99,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (Bytes.slice32(_blob, 0)); } @@ -116,7 +108,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((fieldLayout)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((fieldLayout)), _fieldLayout); } /** Set fieldLayout (using the specified store) */ @@ -124,7 +116,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((fieldLayout)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((fieldLayout)), _fieldLayout); } /** Get keySchema */ @@ -132,7 +124,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, _fieldLayout); return (Bytes.slice32(_blob, 0)); } @@ -141,7 +133,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, _fieldLayout); return (Bytes.slice32(_blob, 0)); } @@ -150,7 +142,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((keySchema)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((keySchema)), _fieldLayout); } /** Set keySchema (using the specified store) */ @@ -158,7 +150,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((keySchema)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((keySchema)), _fieldLayout); } /** Get valueSchema */ @@ -166,7 +158,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, _fieldLayout); return (Bytes.slice32(_blob, 0)); } @@ -175,7 +167,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, _fieldLayout); return (Bytes.slice32(_blob, 0)); } @@ -184,7 +176,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - StoreSwitch.setField(_tableId, _keyTuple, 2, abi.encodePacked((valueSchema)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 2, abi.encodePacked((valueSchema)), _fieldLayout); } /** Set valueSchema (using the specified store) */ @@ -192,7 +184,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - _store.setField(_tableId, _keyTuple, 2, abi.encodePacked((valueSchema)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 2, abi.encodePacked((valueSchema)), _fieldLayout); } /** Get abiEncodedKeyNames */ @@ -200,7 +192,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, _fieldLayout); return (bytes(_blob)); } @@ -212,7 +204,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, _fieldLayout); return (bytes(_blob)); } @@ -221,7 +213,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - StoreSwitch.setField(_tableId, _keyTuple, 3, bytes((abiEncodedKeyNames)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 3, bytes((abiEncodedKeyNames)), _fieldLayout); } /** Set abiEncodedKeyNames (using the specified store) */ @@ -229,7 +221,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - _store.setField(_tableId, _keyTuple, 3, bytes((abiEncodedKeyNames)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 3, bytes((abiEncodedKeyNames)), _fieldLayout); } /** Get the length of abiEncodedKeyNames */ @@ -237,7 +229,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 3, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 3, _fieldLayout); unchecked { return _byteLength / 1; } @@ -248,7 +240,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 3, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 3, _fieldLayout); unchecked { return _byteLength / 1; } @@ -267,7 +259,7 @@ library Tables { _tableId, _keyTuple, 3, - getFieldLayout(), + _fieldLayout, _index * 1, (_index + 1) * 1 ); @@ -288,7 +280,7 @@ library Tables { _keyTuple[0] = tableId; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 3, getFieldLayout(), _index * 1, (_index + 1) * 1); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 3, _fieldLayout, _index * 1, (_index + 1) * 1); return (bytes(_blob)); } } @@ -298,7 +290,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - StoreSwitch.pushToField(_tableId, _keyTuple, 3, bytes((_slice)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 3, bytes((_slice)), _fieldLayout); } /** Push a slice to abiEncodedKeyNames (using the specified store) */ @@ -306,7 +298,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - _store.pushToField(_tableId, _keyTuple, 3, bytes((_slice)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 3, bytes((_slice)), _fieldLayout); } /** Pop a slice from abiEncodedKeyNames */ @@ -314,7 +306,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - StoreSwitch.popFromField(_tableId, _keyTuple, 3, 1, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 3, 1, _fieldLayout); } /** Pop a slice from abiEncodedKeyNames (using the specified store) */ @@ -322,7 +314,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - _store.popFromField(_tableId, _keyTuple, 3, 1, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 3, 1, _fieldLayout); } /** @@ -334,7 +326,7 @@ library Tables { _keyTuple[0] = tableId; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 3, _index * 1, bytes((_slice)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 3, _index * 1, bytes((_slice)), _fieldLayout); } } @@ -347,7 +339,7 @@ library Tables { _keyTuple[0] = tableId; unchecked { - _store.updateInField(_tableId, _keyTuple, 3, _index * 1, bytes((_slice)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 3, _index * 1, bytes((_slice)), _fieldLayout); } } @@ -356,7 +348,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 4, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 4, _fieldLayout); return (bytes(_blob)); } @@ -368,7 +360,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 4, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 4, _fieldLayout); return (bytes(_blob)); } @@ -377,7 +369,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - StoreSwitch.setField(_tableId, _keyTuple, 4, bytes((abiEncodedFieldNames)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 4, bytes((abiEncodedFieldNames)), _fieldLayout); } /** Set abiEncodedFieldNames (using the specified store) */ @@ -385,7 +377,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - _store.setField(_tableId, _keyTuple, 4, bytes((abiEncodedFieldNames)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 4, bytes((abiEncodedFieldNames)), _fieldLayout); } /** Get the length of abiEncodedFieldNames */ @@ -393,7 +385,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 4, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 4, _fieldLayout); unchecked { return _byteLength / 1; } @@ -404,7 +396,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 4, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 4, _fieldLayout); unchecked { return _byteLength / 1; } @@ -423,7 +415,7 @@ library Tables { _tableId, _keyTuple, 4, - getFieldLayout(), + _fieldLayout, _index * 1, (_index + 1) * 1 ); @@ -444,7 +436,7 @@ library Tables { _keyTuple[0] = tableId; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 4, getFieldLayout(), _index * 1, (_index + 1) * 1); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 4, _fieldLayout, _index * 1, (_index + 1) * 1); return (bytes(_blob)); } } @@ -454,7 +446,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - StoreSwitch.pushToField(_tableId, _keyTuple, 4, bytes((_slice)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 4, bytes((_slice)), _fieldLayout); } /** Push a slice to abiEncodedFieldNames (using the specified store) */ @@ -462,7 +454,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - _store.pushToField(_tableId, _keyTuple, 4, bytes((_slice)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 4, bytes((_slice)), _fieldLayout); } /** Pop a slice from abiEncodedFieldNames */ @@ -470,7 +462,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - StoreSwitch.popFromField(_tableId, _keyTuple, 4, 1, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 4, 1, _fieldLayout); } /** Pop a slice from abiEncodedFieldNames (using the specified store) */ @@ -478,7 +470,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - _store.popFromField(_tableId, _keyTuple, 4, 1, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 4, 1, _fieldLayout); } /** @@ -490,7 +482,7 @@ library Tables { _keyTuple[0] = tableId; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 4, _index * 1, bytes((_slice)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 4, _index * 1, bytes((_slice)), _fieldLayout); } } @@ -503,7 +495,7 @@ library Tables { _keyTuple[0] = tableId; unchecked { - _store.updateInField(_tableId, _keyTuple, 4, _index * 1, bytes((_slice)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 4, _index * 1, bytes((_slice)), _fieldLayout); } } @@ -512,7 +504,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -521,7 +513,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -542,7 +534,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using individual values (using the specified store) */ @@ -563,7 +555,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using the data struct */ @@ -680,7 +672,7 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -688,6 +680,6 @@ library Tables { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = tableId; - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/store/src/codegen/tables/Vector2.sol b/packages/store/src/codegen/tables/Vector2.sol index f71080845a..9c1f7cbea1 100644 --- a/packages/store/src/codegen/tables/Vector2.sol +++ b/packages/store/src/codegen/tables/Vector2.sol @@ -21,6 +21,10 @@ import { PackedCounter, PackedCounterLib } from "../../PackedCounter.sol"; bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16("mudstore"), bytes16("Vector2"))); bytes32 constant Vector2TableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0008020004040000000000000000000000000000000000000000000000000000 +); + struct Vector2Data { uint32 x; uint32 y; @@ -29,11 +33,7 @@ struct Vector2Data { library Vector2 { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](2); - _fieldLayout[0] = 4; - _fieldLayout[1] = 4; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -68,19 +68,12 @@ library Vector2 { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get x */ @@ -88,7 +81,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint32(Bytes.slice4(_blob, 0))); } @@ -97,7 +90,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint32(Bytes.slice4(_blob, 0))); } @@ -106,7 +99,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), _fieldLayout); } /** Set x (using the specified store) */ @@ -114,7 +107,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), _fieldLayout); } /** Get y */ @@ -122,7 +115,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, _fieldLayout); return (uint32(Bytes.slice4(_blob, 0))); } @@ -131,7 +124,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, _fieldLayout); return (uint32(Bytes.slice4(_blob, 0))); } @@ -140,7 +133,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), _fieldLayout); } /** Set y (using the specified store) */ @@ -148,7 +141,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), _fieldLayout); } /** Get the full data */ @@ -156,7 +149,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -165,7 +158,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -179,7 +172,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using individual values (using the specified store) */ @@ -192,7 +185,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using the data struct */ @@ -240,7 +233,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -248,6 +241,6 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/store/test/FieldLayout.t.sol b/packages/store/test/FieldLayout.t.sol index c8555fb18d..c8f302e1fa 100644 --- a/packages/store/test/FieldLayout.t.sol +++ b/packages/store/test/FieldLayout.t.sol @@ -204,4 +204,35 @@ contract FieldLayoutTest is Test, GasReporter { assertFalse(empty); } + + function testHex() public { + uint256[] memory _fieldLayout = new uint256[](24); + _fieldLayout[0] = 1; + _fieldLayout[1] = 2; + _fieldLayout[2] = 3; + _fieldLayout[3] = 4; + _fieldLayout[4] = 5; + _fieldLayout[5] = 6; + _fieldLayout[6] = 7; + _fieldLayout[7] = 8; + _fieldLayout[8] = 9; + _fieldLayout[9] = 10; + _fieldLayout[10] = 11; + _fieldLayout[11] = 12; + _fieldLayout[12] = 13; + _fieldLayout[13] = 14; + _fieldLayout[14] = 15; + _fieldLayout[15] = 16; + _fieldLayout[16] = 17; + _fieldLayout[17] = 18; + _fieldLayout[18] = 19; + _fieldLayout[19] = 20; + _fieldLayout[20] = 21; + _fieldLayout[21] = 22; + _fieldLayout[22] = 23; + _fieldLayout[23] = 32; + + FieldLayout encodedFieldLayout = FieldLayoutLib.encode(_fieldLayout, 4); + assertEq(encodedFieldLayout.unwrap(), hex"013418040102030405060708090a0b0c0d0e0f10111213141516172000000000"); + } } diff --git a/packages/store/ts/codegen/ephemeral.ts b/packages/store/ts/codegen/ephemeral.ts index 96182f65fb..21b850a782 100644 --- a/packages/store/ts/codegen/ephemeral.ts +++ b/packages/store/ts/codegen/ephemeral.ts @@ -20,7 +20,7 @@ export function renderEphemeralMethods(options: RenderTableOptions) { ${_keyTupleDefinition} - ${_store}.emitEphemeralRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + ${_store}.emitEphemeralRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } ` ); diff --git a/packages/store/ts/codegen/field.ts b/packages/store/ts/codegen/field.ts index 29f74789f0..5d1c3bdf81 100644 --- a/packages/store/ts/codegen/field.ts +++ b/packages/store/ts/codegen/field.ts @@ -25,7 +25,7 @@ export function renderFieldMethods(options: RenderTableOptions) { _typedKeyArgs, ])}) internal view returns (${_typedFieldName}) { ${_keyTupleDefinition} - bytes memory _blob = ${_store}.getField(_tableId, _keyTuple, ${schemaIndex}, getFieldLayout()); + bytes memory _blob = ${_store}.getField(_tableId, _keyTuple, ${schemaIndex}, _fieldLayout); return ${renderDecodeFieldSingle(field)}; } ` @@ -42,7 +42,7 @@ export function renderFieldMethods(options: RenderTableOptions) { _typedFieldName, ])}) internal { ${_keyTupleDefinition} - ${_store}.setField(_tableId, _keyTuple, ${schemaIndex}, ${renderEncodeFieldSingle(field)}, getFieldLayout()); + ${_store}.setField(_tableId, _keyTuple, ${schemaIndex}, ${renderEncodeFieldSingle(field)}, _fieldLayout); } ` ); @@ -60,7 +60,7 @@ export function renderFieldMethods(options: RenderTableOptions) { _typedKeyArgs, ])}) internal view returns (uint256) { ${_keyTupleDefinition} - uint256 _byteLength = ${_store}.getFieldLength(_tableId, _keyTuple, ${schemaIndex}, getFieldLayout()); + uint256 _byteLength = ${_store}.getFieldLength(_tableId, _keyTuple, ${schemaIndex}, _fieldLayout); unchecked { return _byteLength / ${portionData.elementLength}; } @@ -87,7 +87,7 @@ export function renderFieldMethods(options: RenderTableOptions) { _tableId, _keyTuple, ${schemaIndex}, - getFieldLayout(), + _fieldLayout, _index * ${portionData.elementLength}, (_index + 1) * ${portionData.elementLength} ); @@ -108,7 +108,7 @@ export function renderFieldMethods(options: RenderTableOptions) { `${portionData.typeWithLocation} ${portionData.name}`, ])}) internal { ${_keyTupleDefinition} - ${_store}.pushToField(_tableId, _keyTuple, ${schemaIndex}, ${portionData.encoded}, getFieldLayout()); + ${_store}.pushToField(_tableId, _keyTuple, ${schemaIndex}, ${portionData.encoded}, _fieldLayout); } ` ); @@ -123,7 +123,7 @@ export function renderFieldMethods(options: RenderTableOptions) { _typedKeyArgs, ])}) internal { ${_keyTupleDefinition} - ${_store}.popFromField(_tableId, _keyTuple, ${schemaIndex}, ${portionData.elementLength}, getFieldLayout()); + ${_store}.popFromField(_tableId, _keyTuple, ${schemaIndex}, ${portionData.elementLength}, _fieldLayout); } ` ); @@ -150,7 +150,7 @@ export function renderFieldMethods(options: RenderTableOptions) { ${schemaIndex}, _index * ${portionData.elementLength}, ${portionData.encoded}, - getFieldLayout() + _fieldLayout ); } } diff --git a/packages/store/ts/codegen/record.ts b/packages/store/ts/codegen/record.ts index b13dfd25d7..75277e463e 100644 --- a/packages/store/ts/codegen/record.ts +++ b/packages/store/ts/codegen/record.ts @@ -1,9 +1,9 @@ import { - renderList, + RenderDynamicField, renderArguments, renderCommonData, + renderList, renderWithStore, - RenderDynamicField, } from "@latticexyz/common/codegen"; import { renderDecodeValueType } from "./field"; import { RenderTableOptions } from "./types"; @@ -22,7 +22,7 @@ export function renderRecordMethods(options: RenderTableOptions) { _typedKeyArgs, ])}) internal view returns (${renderDecodedRecord(options)}) { ${_keyTupleDefinition} - bytes memory _blob = ${_store}.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = ${_store}.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } ` @@ -42,7 +42,7 @@ export function renderRecordMethods(options: RenderTableOptions) { ${_keyTupleDefinition} - ${_store}.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + ${_store}.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } ` ); diff --git a/packages/store/ts/codegen/renderFieldLayout.test.ts b/packages/store/ts/codegen/renderFieldLayout.test.ts new file mode 100644 index 0000000000..a2a170ef8d --- /dev/null +++ b/packages/store/ts/codegen/renderFieldLayout.test.ts @@ -0,0 +1,42 @@ +import { describe, expect, it } from "vitest"; +import { renderFieldLayout } from "./renderFieldLayout"; +import { RenderType } from "@latticexyz/common/codegen"; + +describe("renderFieldLayout", () => { + it("should match the FieldLayout.sol encoding", () => { + const fields = [ + { isDynamic: false, staticByteLength: 1 }, + { isDynamic: false, staticByteLength: 2 }, + { isDynamic: false, staticByteLength: 3 }, + { isDynamic: false, staticByteLength: 4 }, + { isDynamic: false, staticByteLength: 5 }, + { isDynamic: false, staticByteLength: 6 }, + { isDynamic: false, staticByteLength: 7 }, + { isDynamic: false, staticByteLength: 8 }, + { isDynamic: false, staticByteLength: 9 }, + { isDynamic: false, staticByteLength: 10 }, + { isDynamic: false, staticByteLength: 11 }, + { isDynamic: false, staticByteLength: 12 }, + { isDynamic: false, staticByteLength: 13 }, + { isDynamic: false, staticByteLength: 14 }, + { isDynamic: false, staticByteLength: 15 }, + { isDynamic: false, staticByteLength: 16 }, + { isDynamic: false, staticByteLength: 17 }, + { isDynamic: false, staticByteLength: 18 }, + { isDynamic: false, staticByteLength: 19 }, + { isDynamic: false, staticByteLength: 20 }, + { isDynamic: false, staticByteLength: 21 }, + { isDynamic: false, staticByteLength: 22 }, + { isDynamic: false, staticByteLength: 23 }, + { isDynamic: false, staticByteLength: 32 }, + { isDynamic: true, staticByteLength: 0 }, + { isDynamic: true, staticByteLength: 0 }, + { isDynamic: true, staticByteLength: 0 }, + { isDynamic: true, staticByteLength: 0 }, + ] as RenderType[]; + + expect(renderFieldLayout(fields)).toEqual( + `FieldLayout constant _fieldLayout = FieldLayout.wrap(0x013418040102030405060708090a0b0c0d0e0f10111213141516172000000000);` + ); + }); +}); diff --git a/packages/store/ts/codegen/renderFieldLayout.ts b/packages/store/ts/codegen/renderFieldLayout.ts new file mode 100644 index 0000000000..cb112a875c --- /dev/null +++ b/packages/store/ts/codegen/renderFieldLayout.ts @@ -0,0 +1,33 @@ +import { RenderType } from "@latticexyz/common/codegen"; +import { BYTE_TO_BITS, LayoutOffsets, MAX_DYNAMIC_FIELDS, MAX_TOTAL_FIELDS, WORD_LAST_INDEX } from "../constants"; + +export function renderFieldLayout(fields: RenderType[]) { + return `FieldLayout constant _fieldLayout = FieldLayout.wrap(${encodeFieldLayout(fields)});`; +} + +// Make sure this logic stays aligned with @latticexyz/store/src/FieldLayout.sol +export function encodeFieldLayout(fields: RenderType[]) { + const staticFields = fields.filter(({ isDynamic }) => !isDynamic); + const numDynamicFields = fields.length - staticFields.length; + + let fieldLayout = 0n; + 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}`); + + for (let i = 0; i < staticFields.length; i++) { + const { isDynamic, staticByteLength } = fields[i]; + if (isDynamic) throw new Error(`FieldLayout: static type after dynamic type`); + + totalLength += staticByteLength; + fieldLayout |= BigInt(staticByteLength) << BigInt((WORD_LAST_INDEX - 4 - i) * BYTE_TO_BITS); + } + + fieldLayout |= BigInt(totalLength) << BigInt(LayoutOffsets.TOTAL_LENGTH); + fieldLayout |= BigInt(staticFields.length) << BigInt(LayoutOffsets.NUM_STATIC_FIELDS); + fieldLayout |= BigInt(numDynamicFields) << BigInt(LayoutOffsets.NUM_DYNAMIC_FIELDS); + + return `0x${fieldLayout.toString(16).padStart(64, "0")}`; +} diff --git a/packages/store/ts/codegen/renderTable.ts b/packages/store/ts/codegen/renderTable.ts index 72816bf28f..e918822585 100644 --- a/packages/store/ts/codegen/renderTable.ts +++ b/packages/store/ts/codegen/renderTable.ts @@ -2,17 +2,18 @@ import { renderArguments, renderCommonData, renderList, - renderedSolidityHeader, renderRelativeImports, renderTableId, - renderWithStore, renderTypeHelpers, + renderWithStore, + renderedSolidityHeader, RenderDynamicField, RenderStaticField, } from "@latticexyz/common/codegen"; import { renderEphemeralMethods } from "./ephemeral"; import { renderEncodeFieldSingle, renderFieldMethods } from "./field"; import { renderRecordData, renderRecordMethods } from "./record"; +import { renderFieldLayout } from "./renderFieldLayout"; import { RenderTableOptions } from "./types"; export function renderTable(options: RenderTableOptions) { @@ -64,6 +65,8 @@ export function renderTable(options: RenderTableOptions) { ${staticResourceData ? renderTableId(staticResourceData).tableIdDefinition : ""} + ${renderFieldLayout(fields)} + ${ !structName ? "" @@ -77,10 +80,7 @@ export function renderTable(options: RenderTableOptions) { library ${libraryName} { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](${staticFields.length}); - ${renderList(staticFields, ({ staticByteLength }, index) => `_fieldLayout[${index}] = ${staticByteLength};`)} - - return FieldLayoutLib.encode(_fieldLayout, ${dynamicFields.length}); + return _fieldLayout; } /** Get the table's key schema */ @@ -116,7 +116,7 @@ export function renderTable(options: RenderTableOptions) { (_typedStore, _store, _commentSuffix) => ` /** Register the table with its config${_commentSuffix} */ function register(${renderArguments([_typedStore, _typedTableId])}) internal { - ${_store}.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + ${_store}.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } ` )} @@ -156,7 +156,7 @@ export function renderTable(options: RenderTableOptions) { /* Delete all data for given keys${_commentSuffix} */ function deleteRecord(${renderArguments([_typedStore, _typedTableId, _typedKeyArgs])}) internal { ${_keyTupleDefinition} - ${_store}.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + ${_store}.deleteRecord(_tableId, _keyTuple, _fieldLayout); } ` ) diff --git a/packages/store/ts/constants.ts b/packages/store/ts/constants.ts new file mode 100644 index 0000000000..9c93949bf3 --- /dev/null +++ b/packages/store/ts/constants.ts @@ -0,0 +1,23 @@ +/* Shared constants */ +// Make sure these stay aligned with @latticexyz/store/src/constants.sol + +// Total byte length of an EVM word +export const WORD_SIZE = 32; +// Index of the last byte in an EVM word +export const WORD_LAST_INDEX = 31; +// Conversion for bit shifting +export const BYTE_TO_BITS = 8; + +// Schema's capacity +export const MAX_TOTAL_FIELDS = 28; +// FieldLayout's capacity +export const MAX_STATIC_FIELDS = 28; +// PackedCounter's capacity +export const MAX_DYNAMIC_FIELDS = 5; + +// FieldLayout and Schema have the same offsets for metadata +export const LayoutOffsets = { + TOTAL_LENGTH: (WORD_SIZE - 2) * BYTE_TO_BITS, + NUM_STATIC_FIELDS: (WORD_SIZE - 2 - 1) * BYTE_TO_BITS, + NUM_DYNAMIC_FIELDS: (WORD_SIZE - 2 - 1 - 1) * BYTE_TO_BITS, +}; diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json index 64b6bfac8c..0757bfb9b3 100644 --- a/packages/world/gas-report.json +++ b/packages/world/gas-report.json @@ -3,31 +3,31 @@ "file": "test/AccessControl.t.sol", "test": "testAccessControl", "name": "AccessControl: hasAccess (cold)", - "gasUsed": 13860 + "gasUsed": 10912 }, { "file": "test/AccessControl.t.sol", "test": "testAccessControl", "name": "AccessControl: hasAccess (warm, namespace only)", - "gasUsed": 4007 + "gasUsed": 2532 }, { "file": "test/AccessControl.t.sol", "test": "testAccessControl", "name": "AccessControl: hasAccess (warm)", - "gasUsed": 7892 + "gasUsed": 4940 }, { "file": "test/AccessControl.t.sol", "test": "testRequireAccess", "name": "AccessControl: requireAccess (cold)", - "gasUsed": 13903 + "gasUsed": 10954 }, { "file": "test/AccessControl.t.sol", "test": "testRequireAccess", "name": "AccessControl: requireAccess (warm)", - "gasUsed": 7909 + "gasUsed": 4959 }, { "file": "test/AccessControl.t.sol", @@ -39,73 +39,73 @@ "file": "test/KeysInTableModule.t.sol", "test": "testInstallComposite", "name": "install keys in table module", - "gasUsed": 1521715 + "gasUsed": 1453384 }, { "file": "test/KeysInTableModule.t.sol", "test": "testInstallGas", "name": "install keys in table module", - "gasUsed": 1521715 + "gasUsed": 1453384 }, { "file": "test/KeysInTableModule.t.sol", "test": "testInstallGas", "name": "set a record on a table with keysInTableModule installed", - "gasUsed": 187187 + "gasUsed": 170849 }, { "file": "test/KeysInTableModule.t.sol", "test": "testInstallSingleton", "name": "install keys in table module", - "gasUsed": 1521715 + "gasUsed": 1453384 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookCompositeGas", "name": "install keys in table module", - "gasUsed": 1521715 + "gasUsed": 1453384 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookCompositeGas", "name": "change a composite record on a table with keysInTableModule installed", - "gasUsed": 30396 + "gasUsed": 26036 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookCompositeGas", "name": "delete a composite record on a table with keysInTableModule installed", - "gasUsed": 256692 + "gasUsed": 205921 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookGas", "name": "install keys in table module", - "gasUsed": 1521715 + "gasUsed": 1453384 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookGas", "name": "change a record on a table with keysInTableModule installed", - "gasUsed": 29117 + "gasUsed": 24758 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookGas", "name": "delete a record on a table with keysInTableModule installed", - "gasUsed": 134551 + "gasUsed": 106239 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testGetKeysWithValueGas", "name": "install keys with value module", - "gasUsed": 730803 + "gasUsed": 682161 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testGetKeysWithValueGas", "name": "Get list of keys with a given value", - "gasUsed": 7584 + "gasUsed": 6521 }, { "file": "test/KeysWithValueModule.t.sol", @@ -117,264 +117,264 @@ "file": "test/KeysWithValueModule.t.sol", "test": "testInstall", "name": "install keys with value module", - "gasUsed": 730803 + "gasUsed": 682161 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testInstall", "name": "set a record on a table with KeysWithValueModule installed", - "gasUsed": 159933 + "gasUsed": 146160 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetAndDeleteRecordHook", "name": "install keys with value module", - "gasUsed": 730803 + "gasUsed": 682161 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetAndDeleteRecordHook", "name": "change a record on a table with KeysWithValueModule installed", - "gasUsed": 129535 + "gasUsed": 115759 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetAndDeleteRecordHook", "name": "delete a record on a table with KeysWithValueModule installed", - "gasUsed": 49133 + "gasUsed": 40440 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetField", "name": "install keys with value module", - "gasUsed": 730803 + "gasUsed": 682161 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetField", "name": "set a field on a table with KeysWithValueModule installed", - "gasUsed": 164678 + "gasUsed": 150906 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetField", "name": "change a field on a table with KeysWithValueModule installed", - "gasUsed": 129437 + "gasUsed": 115665 }, { "file": "test/query.t.sol", "test": "testCombinedHasHasValueNotQuery", "name": "CombinedHasHasValueNotQuery", - "gasUsed": 141735 + "gasUsed": 112790 }, { "file": "test/query.t.sol", "test": "testCombinedHasHasValueQuery", "name": "CombinedHasHasValueQuery", - "gasUsed": 69540 + "gasUsed": 57749 }, { "file": "test/query.t.sol", "test": "testCombinedHasNotQuery", "name": "CombinedHasNotQuery", - "gasUsed": 184268 + "gasUsed": 140333 }, { "file": "test/query.t.sol", "test": "testCombinedHasQuery", "name": "CombinedHasQuery", - "gasUsed": 122085 + "gasUsed": 90971 }, { "file": "test/query.t.sol", "test": "testCombinedHasValueNotQuery", "name": "CombinedHasValueNotQuery", - "gasUsed": 117986 + "gasUsed": 91191 }, { "file": "test/query.t.sol", "test": "testCombinedHasValueQuery", "name": "CombinedHasValueQuery", - "gasUsed": 19461 + "gasUsed": 17316 }, { "file": "test/query.t.sol", "test": "testHasQuery", "name": "HasQuery", - "gasUsed": 27973 + "gasUsed": 20461 }, { "file": "test/query.t.sol", "test": "testHasQuery1000Keys", "name": "HasQuery with 1000 keys", - "gasUsed": 7118904 + "gasUsed": 5985870 }, { "file": "test/query.t.sol", "test": "testHasQuery100Keys", "name": "HasQuery with 100 keys", - "gasUsed": 672769 + "gasUsed": 560764 }, { "file": "test/query.t.sol", "test": "testHasValueQuery", "name": "HasValueQuery", - "gasUsed": 9401 + "gasUsed": 8329 }, { "file": "test/query.t.sol", "test": "testNotValueQuery", "name": "NotValueQuery", - "gasUsed": 63136 + "gasUsed": 51345 }, { "file": "test/StandardDelegationsModule.t.sol", "test": "testCallFromCallboundDelegation", "name": "register a callbound delegation", - "gasUsed": 133856 + "gasUsed": 120433 }, { "file": "test/StandardDelegationsModule.t.sol", "test": "testCallFromCallboundDelegation", "name": "call a system via a callbound delegation", - "gasUsed": 49405 + "gasUsed": 39201 }, { "file": "test/StandardDelegationsModule.t.sol", "test": "testCallFromTimeboundDelegation", "name": "register a timebound delegation", - "gasUsed": 128205 + "gasUsed": 114784 }, { "file": "test/StandardDelegationsModule.t.sol", "test": "testCallFromTimeboundDelegation", "name": "call a system via a timebound delegation", - "gasUsed": 38496 + "gasUsed": 30837 }, { "file": "test/UniqueEntityModule.t.sol", "test": "testInstall", "name": "install unique entity module", - "gasUsed": 790963 + "gasUsed": 721941 }, { "file": "test/UniqueEntityModule.t.sol", "test": "testInstall", "name": "get a unique entity nonce (non-root module)", - "gasUsed": 67727 + "gasUsed": 57530 }, { "file": "test/UniqueEntityModule.t.sol", "test": "testInstallRoot", "name": "installRoot unique entity module", - "gasUsed": 774507 + "gasUsed": 708712 }, { "file": "test/UniqueEntityModule.t.sol", "test": "testInstallRoot", "name": "get a unique entity nonce (root module)", - "gasUsed": 67727 + "gasUsed": 57530 }, { "file": "test/World.t.sol", "test": "testCall", "name": "call a system via the World", - "gasUsed": 19564 + "gasUsed": 15199 }, { "file": "test/World.t.sol", "test": "testCallFromUnlimitedDelegation", "name": "register an unlimited delegation", - "gasUsed": 59312 + "gasUsed": 52070 }, { "file": "test/World.t.sol", "test": "testCallFromUnlimitedDelegation", "name": "call a system via an unlimited delegation", - "gasUsed": 19972 + "gasUsed": 15608 }, { "file": "test/World.t.sol", "test": "testDeleteRecord", "name": "Delete record", - "gasUsed": 13885 + "gasUsed": 11340 }, { "file": "test/World.t.sol", "test": "testPushToField", "name": "Push data to the table", - "gasUsed": 93919 + "gasUsed": 91372 }, { "file": "test/World.t.sol", "test": "testRegisterFallbackSystem", "name": "Register a fallback system", - "gasUsed": 75955 + "gasUsed": 65058 }, { "file": "test/World.t.sol", "test": "testRegisterFallbackSystem", "name": "Register a root fallback system", - "gasUsed": 69204 + "gasUsed": 58308 }, { "file": "test/World.t.sol", "test": "testRegisterFunctionSelector", "name": "Register a function selector", - "gasUsed": 96549 + "gasUsed": 85651 }, { "file": "test/World.t.sol", "test": "testRegisterNamespace", "name": "Register a new namespace", - "gasUsed": 147155 + "gasUsed": 133367 }, { "file": "test/World.t.sol", "test": "testRegisterRootFunctionSelector", "name": "Register a root function selector", - "gasUsed": 91122 + "gasUsed": 80226 }, { "file": "test/World.t.sol", "test": "testRegisterTable", "name": "Register a new table in the namespace", - "gasUsed": 686517 + "gasUsed": 660018 }, { "file": "test/World.t.sol", "test": "testSetField", "name": "Write data to a table field", - "gasUsed": 42174 + "gasUsed": 39628 }, { "file": "test/World.t.sol", "test": "testSetRecord", "name": "Write data to the table", - "gasUsed": 41619 + "gasUsed": 37622 }, { "file": "test/WorldDynamicUpdate.t.sol", "test": "testPopFromField", "name": "pop 1 address (cold)", - "gasUsed": 32933 + "gasUsed": 30387 }, { "file": "test/WorldDynamicUpdate.t.sol", "test": "testPopFromField", "name": "pop 1 address (warm)", - "gasUsed": 20046 + "gasUsed": 17500 }, { "file": "test/WorldDynamicUpdate.t.sol", "test": "testUpdateInField", "name": "updateInField 1 item (cold)", - "gasUsed": 36456 + "gasUsed": 33910 }, { "file": "test/WorldDynamicUpdate.t.sol", "test": "testUpdateInField", "name": "updateInField 1 item (warm)", - "gasUsed": 23661 + "gasUsed": 21115 } ] diff --git a/packages/world/src/modules/core/tables/Balances.sol b/packages/world/src/modules/core/tables/Balances.sol index 417d4b2751..ac0cada199 100644 --- a/packages/world/src/modules/core/tables/Balances.sol +++ b/packages/world/src/modules/core/tables/Balances.sol @@ -21,13 +21,14 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Balances"))); bytes32 constant BalancesTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0020010020000000000000000000000000000000000000000000000000000000 +); + library Balances { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](1); - _fieldLayout[0] = 32; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -60,19 +61,12 @@ library Balances { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get balance */ @@ -80,7 +74,7 @@ library Balances { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint256(Bytes.slice32(_blob, 0))); } @@ -89,7 +83,7 @@ library Balances { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint256(Bytes.slice32(_blob, 0))); } @@ -98,7 +92,7 @@ library Balances { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((balance)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((balance)), _fieldLayout); } /** Set balance (using the specified store) */ @@ -106,7 +100,7 @@ library Balances { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((balance)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((balance)), _fieldLayout); } /** Tightly pack static data using this table's schema */ @@ -137,7 +131,7 @@ library Balances { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -145,6 +139,6 @@ library Balances { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/world/src/modules/core/tables/FunctionSelectors.sol b/packages/world/src/modules/core/tables/FunctionSelectors.sol index 152672637c..f679ed4e18 100644 --- a/packages/world/src/modules/core/tables/FunctionSelectors.sol +++ b/packages/world/src/modules/core/tables/FunctionSelectors.sol @@ -21,14 +21,14 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("FunctionSelector"))); bytes32 constant FunctionSelectorsTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0024020020040000000000000000000000000000000000000000000000000000 +); + library FunctionSelectors { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](2); - _fieldLayout[0] = 32; - _fieldLayout[1] = 4; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -63,19 +63,12 @@ library FunctionSelectors { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get resourceSelector */ @@ -83,7 +76,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (Bytes.slice32(_blob, 0)); } @@ -95,7 +88,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (Bytes.slice32(_blob, 0)); } @@ -104,7 +97,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((resourceSelector)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((resourceSelector)), _fieldLayout); } /** Set resourceSelector (using the specified store) */ @@ -112,7 +105,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((resourceSelector)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((resourceSelector)), _fieldLayout); } /** Get systemFunctionSelector */ @@ -120,7 +113,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, _fieldLayout); return (Bytes.slice4(_blob, 0)); } @@ -132,7 +125,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, _fieldLayout); return (Bytes.slice4(_blob, 0)); } @@ -141,7 +134,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((systemFunctionSelector)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((systemFunctionSelector)), _fieldLayout); } /** Set systemFunctionSelector (using the specified store) */ @@ -149,7 +142,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((systemFunctionSelector)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((systemFunctionSelector)), _fieldLayout); } /** Get the full data */ @@ -159,7 +152,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -171,7 +164,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -185,7 +178,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using individual values (using the specified store) */ @@ -203,7 +196,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Decode the tightly packed blob using this table's field layout */ @@ -241,7 +234,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -249,6 +242,6 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/world/src/modules/core/tables/ResourceType.sol b/packages/world/src/modules/core/tables/ResourceType.sol index ef1ce9ce3d..1d56109aa6 100644 --- a/packages/world/src/modules/core/tables/ResourceType.sol +++ b/packages/world/src/modules/core/tables/ResourceType.sol @@ -24,13 +24,14 @@ import { Resource } from "./../../../Types.sol"; bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("ResourceType"))); bytes32 constant ResourceTypeTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0001010001000000000000000000000000000000000000000000000000000000 +); + library ResourceType { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](1); - _fieldLayout[0] = 1; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -63,19 +64,12 @@ library ResourceType { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get resourceType */ @@ -83,7 +77,7 @@ library ResourceType { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return Resource(uint8(Bytes.slice1(_blob, 0))); } @@ -92,7 +86,7 @@ library ResourceType { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return Resource(uint8(Bytes.slice1(_blob, 0))); } @@ -101,7 +95,7 @@ library ResourceType { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked(uint8(resourceType)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked(uint8(resourceType)), _fieldLayout); } /** Set resourceType (using the specified store) */ @@ -109,7 +103,7 @@ library ResourceType { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked(uint8(resourceType)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked(uint8(resourceType)), _fieldLayout); } /** Tightly pack static data using this table's schema */ @@ -140,7 +134,7 @@ library ResourceType { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -148,6 +142,6 @@ library ResourceType { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/world/src/modules/core/tables/SystemHooks.sol b/packages/world/src/modules/core/tables/SystemHooks.sol index d0503ff496..8ea6e2b31b 100644 --- a/packages/world/src/modules/core/tables/SystemHooks.sol +++ b/packages/world/src/modules/core/tables/SystemHooks.sol @@ -21,12 +21,14 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("SystemHooks"))); bytes32 constant SystemHooksTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0000000100000000000000000000000000000000000000000000000000000000 +); + library SystemHooks { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](0); - - return FieldLayoutLib.encode(_fieldLayout, 1); + return _fieldLayout; } /** Get the table's key schema */ @@ -59,19 +61,12 @@ library SystemHooks { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ @@ -79,7 +74,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } @@ -88,7 +83,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } @@ -97,7 +92,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); } /** Set value (using the specified store) */ @@ -105,7 +100,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); } /** Get the length of value */ @@ -113,7 +108,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); unchecked { return _byteLength / 21; } @@ -124,7 +119,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); unchecked { return _byteLength / 21; } @@ -143,7 +138,7 @@ library SystemHooks { _tableId, _keyTuple, 0, - getFieldLayout(), + _fieldLayout, _index * 21, (_index + 1) * 21 ); @@ -160,14 +155,7 @@ library SystemHooks { _keyTuple[0] = resourceSelector; unchecked { - bytes memory _blob = _store.getFieldSlice( - _tableId, - _keyTuple, - 0, - getFieldLayout(), - _index * 21, - (_index + 1) * 21 - ); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, _fieldLayout, _index * 21, (_index + 1) * 21); return (Bytes.slice21(_blob, 0)); } } @@ -177,7 +165,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } /** Push an element to value (using the specified store) */ @@ -185,7 +173,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } /** Pop an element from value */ @@ -193,7 +181,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 21, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 0, 21, _fieldLayout); } /** Pop an element from value (using the specified store) */ @@ -201,7 +189,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.popFromField(_tableId, _keyTuple, 0, 21, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 0, 21, _fieldLayout); } /** @@ -213,7 +201,7 @@ library SystemHooks { _keyTuple[0] = resourceSelector; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 21, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 21, abi.encodePacked((_element)), _fieldLayout); } } @@ -226,7 +214,7 @@ library SystemHooks { _keyTuple[0] = resourceSelector; unchecked { - _store.updateInField(_tableId, _keyTuple, 0, _index * 21, abi.encodePacked((_element)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 0, _index * 21, abi.encodePacked((_element)), _fieldLayout); } } @@ -265,7 +253,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -273,6 +261,6 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/world/src/modules/core/tables/SystemRegistry.sol b/packages/world/src/modules/core/tables/SystemRegistry.sol index fa5e247567..4a83e64bec 100644 --- a/packages/world/src/modules/core/tables/SystemRegistry.sol +++ b/packages/world/src/modules/core/tables/SystemRegistry.sol @@ -21,13 +21,14 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("SystemRegistry"))); bytes32 constant SystemRegistryTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0020010020000000000000000000000000000000000000000000000000000000 +); + library SystemRegistry { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](1); - _fieldLayout[0] = 32; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -60,19 +61,12 @@ library SystemRegistry { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get resourceSelector */ @@ -80,7 +74,7 @@ library SystemRegistry { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (Bytes.slice32(_blob, 0)); } @@ -89,7 +83,7 @@ library SystemRegistry { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (Bytes.slice32(_blob, 0)); } @@ -98,7 +92,7 @@ library SystemRegistry { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((resourceSelector)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((resourceSelector)), _fieldLayout); } /** Set resourceSelector (using the specified store) */ @@ -106,7 +100,7 @@ library SystemRegistry { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((resourceSelector)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((resourceSelector)), _fieldLayout); } /** Tightly pack static data using this table's schema */ @@ -137,7 +131,7 @@ library SystemRegistry { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -145,6 +139,6 @@ library SystemRegistry { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/world/src/modules/core/tables/Systems.sol b/packages/world/src/modules/core/tables/Systems.sol index 52a9c0ad04..1316f4331a 100644 --- a/packages/world/src/modules/core/tables/Systems.sol +++ b/packages/world/src/modules/core/tables/Systems.sol @@ -21,14 +21,14 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Systems"))); bytes32 constant SystemsTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0015020014010000000000000000000000000000000000000000000000000000 +); + library Systems { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](2); - _fieldLayout[0] = 20; - _fieldLayout[1] = 1; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -63,19 +63,12 @@ library Systems { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get system */ @@ -83,7 +76,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (address(Bytes.slice20(_blob, 0))); } @@ -92,7 +85,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (address(Bytes.slice20(_blob, 0))); } @@ -101,7 +94,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((system)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((system)), _fieldLayout); } /** Set system (using the specified store) */ @@ -109,7 +102,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((system)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((system)), _fieldLayout); } /** Get publicAccess */ @@ -117,7 +110,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, _fieldLayout); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -126,7 +119,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, _fieldLayout); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -135,7 +128,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((publicAccess)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((publicAccess)), _fieldLayout); } /** Set publicAccess (using the specified store) */ @@ -143,7 +136,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((publicAccess)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((publicAccess)), _fieldLayout); } /** Get the full data */ @@ -151,7 +144,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -160,7 +153,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -174,7 +167,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using individual values (using the specified store) */ @@ -187,7 +180,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Decode the tightly packed blob using this table's field layout */ @@ -225,7 +218,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -233,7 +226,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/world/src/modules/keysintable/tables/KeysInTable.sol b/packages/world/src/modules/keysintable/tables/KeysInTable.sol index ba91e6d7ae..589642c66c 100644 --- a/packages/world/src/modules/keysintable/tables/KeysInTable.sol +++ b/packages/world/src/modules/keysintable/tables/KeysInTable.sol @@ -21,6 +21,10 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("KeysInTable"))); bytes32 constant KeysInTableTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0000000500000000000000000000000000000000000000000000000000000000 +); + struct KeysInTableData { bytes32[] keys0; bytes32[] keys1; @@ -32,9 +36,7 @@ struct KeysInTableData { library KeysInTable { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](0); - - return FieldLayoutLib.encode(_fieldLayout, 5); + return _fieldLayout; } /** Get the table's key schema */ @@ -75,19 +77,12 @@ library KeysInTable { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get keys0 */ @@ -95,7 +90,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -104,7 +99,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -113,7 +108,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keys0)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keys0)), _fieldLayout); } /** Set keys0 (using the specified store) */ @@ -121,7 +116,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keys0)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keys0)), _fieldLayout); } /** Get the length of keys0 */ @@ -129,7 +124,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); unchecked { return _byteLength / 32; } @@ -140,7 +135,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); unchecked { return _byteLength / 32; } @@ -159,7 +154,7 @@ library KeysInTable { _tableId, _keyTuple, 0, - getFieldLayout(), + _fieldLayout, _index * 32, (_index + 1) * 32 ); @@ -176,14 +171,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - bytes memory _blob = _store.getFieldSlice( - _tableId, - _keyTuple, - 0, - getFieldLayout(), - _index * 32, - (_index + 1) * 32 - ); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, _fieldLayout, _index * 32, (_index + 1) * 32); return (Bytes.slice32(_blob, 0)); } } @@ -193,7 +181,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } /** Push an element to keys0 (using the specified store) */ @@ -201,7 +189,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } /** Pop an element from keys0 */ @@ -209,7 +197,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 32, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 0, 32, _fieldLayout); } /** Pop an element from keys0 (using the specified store) */ @@ -217,7 +205,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.popFromField(_tableId, _keyTuple, 0, 32, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 0, 32, _fieldLayout); } /** @@ -229,7 +217,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), _fieldLayout); } } @@ -242,7 +230,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - _store.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), _fieldLayout); } } @@ -251,7 +239,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -260,7 +248,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -269,7 +257,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.setField(_tableId, _keyTuple, 1, EncodeArray.encode((keys1)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 1, EncodeArray.encode((keys1)), _fieldLayout); } /** Set keys1 (using the specified store) */ @@ -277,7 +265,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.setField(_tableId, _keyTuple, 1, EncodeArray.encode((keys1)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 1, EncodeArray.encode((keys1)), _fieldLayout); } /** Get the length of keys1 */ @@ -285,7 +273,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 1, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 1, _fieldLayout); unchecked { return _byteLength / 32; } @@ -296,7 +284,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 1, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 1, _fieldLayout); unchecked { return _byteLength / 32; } @@ -315,7 +303,7 @@ library KeysInTable { _tableId, _keyTuple, 1, - getFieldLayout(), + _fieldLayout, _index * 32, (_index + 1) * 32 ); @@ -332,14 +320,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - bytes memory _blob = _store.getFieldSlice( - _tableId, - _keyTuple, - 1, - getFieldLayout(), - _index * 32, - (_index + 1) * 32 - ); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 1, _fieldLayout, _index * 32, (_index + 1) * 32); return (Bytes.slice32(_blob, 0)); } } @@ -349,7 +330,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), _fieldLayout); } /** Push an element to keys1 (using the specified store) */ @@ -357,7 +338,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), _fieldLayout); } /** Pop an element from keys1 */ @@ -365,7 +346,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.popFromField(_tableId, _keyTuple, 1, 32, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 1, 32, _fieldLayout); } /** Pop an element from keys1 (using the specified store) */ @@ -373,7 +354,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.popFromField(_tableId, _keyTuple, 1, 32, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 1, 32, _fieldLayout); } /** @@ -385,7 +366,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 1, _index * 32, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 1, _index * 32, abi.encodePacked((_element)), _fieldLayout); } } @@ -398,7 +379,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - _store.updateInField(_tableId, _keyTuple, 1, _index * 32, abi.encodePacked((_element)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 1, _index * 32, abi.encodePacked((_element)), _fieldLayout); } } @@ -407,7 +388,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -416,7 +397,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -425,7 +406,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.setField(_tableId, _keyTuple, 2, EncodeArray.encode((keys2)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 2, EncodeArray.encode((keys2)), _fieldLayout); } /** Set keys2 (using the specified store) */ @@ -433,7 +414,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.setField(_tableId, _keyTuple, 2, EncodeArray.encode((keys2)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 2, EncodeArray.encode((keys2)), _fieldLayout); } /** Get the length of keys2 */ @@ -441,7 +422,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 2, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 2, _fieldLayout); unchecked { return _byteLength / 32; } @@ -452,7 +433,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 2, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 2, _fieldLayout); unchecked { return _byteLength / 32; } @@ -471,7 +452,7 @@ library KeysInTable { _tableId, _keyTuple, 2, - getFieldLayout(), + _fieldLayout, _index * 32, (_index + 1) * 32 ); @@ -488,14 +469,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - bytes memory _blob = _store.getFieldSlice( - _tableId, - _keyTuple, - 2, - getFieldLayout(), - _index * 32, - (_index + 1) * 32 - ); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 2, _fieldLayout, _index * 32, (_index + 1) * 32); return (Bytes.slice32(_blob, 0)); } } @@ -505,7 +479,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), _fieldLayout); } /** Push an element to keys2 (using the specified store) */ @@ -513,7 +487,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), _fieldLayout); } /** Pop an element from keys2 */ @@ -521,7 +495,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.popFromField(_tableId, _keyTuple, 2, 32, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 2, 32, _fieldLayout); } /** Pop an element from keys2 (using the specified store) */ @@ -529,7 +503,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.popFromField(_tableId, _keyTuple, 2, 32, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 2, 32, _fieldLayout); } /** @@ -541,7 +515,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 2, _index * 32, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 2, _index * 32, abi.encodePacked((_element)), _fieldLayout); } } @@ -554,7 +528,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - _store.updateInField(_tableId, _keyTuple, 2, _index * 32, abi.encodePacked((_element)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 2, _index * 32, abi.encodePacked((_element)), _fieldLayout); } } @@ -563,7 +537,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -572,7 +546,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -581,7 +555,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.setField(_tableId, _keyTuple, 3, EncodeArray.encode((keys3)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 3, EncodeArray.encode((keys3)), _fieldLayout); } /** Set keys3 (using the specified store) */ @@ -589,7 +563,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.setField(_tableId, _keyTuple, 3, EncodeArray.encode((keys3)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 3, EncodeArray.encode((keys3)), _fieldLayout); } /** Get the length of keys3 */ @@ -597,7 +571,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 3, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 3, _fieldLayout); unchecked { return _byteLength / 32; } @@ -608,7 +582,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 3, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 3, _fieldLayout); unchecked { return _byteLength / 32; } @@ -627,7 +601,7 @@ library KeysInTable { _tableId, _keyTuple, 3, - getFieldLayout(), + _fieldLayout, _index * 32, (_index + 1) * 32 ); @@ -644,14 +618,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - bytes memory _blob = _store.getFieldSlice( - _tableId, - _keyTuple, - 3, - getFieldLayout(), - _index * 32, - (_index + 1) * 32 - ); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 3, _fieldLayout, _index * 32, (_index + 1) * 32); return (Bytes.slice32(_blob, 0)); } } @@ -661,7 +628,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), _fieldLayout); } /** Push an element to keys3 (using the specified store) */ @@ -669,7 +636,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), _fieldLayout); } /** Pop an element from keys3 */ @@ -677,7 +644,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.popFromField(_tableId, _keyTuple, 3, 32, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 3, 32, _fieldLayout); } /** Pop an element from keys3 (using the specified store) */ @@ -685,7 +652,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.popFromField(_tableId, _keyTuple, 3, 32, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 3, 32, _fieldLayout); } /** @@ -697,7 +664,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 3, _index * 32, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 3, _index * 32, abi.encodePacked((_element)), _fieldLayout); } } @@ -710,7 +677,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - _store.updateInField(_tableId, _keyTuple, 3, _index * 32, abi.encodePacked((_element)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 3, _index * 32, abi.encodePacked((_element)), _fieldLayout); } } @@ -719,7 +686,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 4, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 4, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -728,7 +695,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 4, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 4, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -737,7 +704,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.setField(_tableId, _keyTuple, 4, EncodeArray.encode((keys4)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 4, EncodeArray.encode((keys4)), _fieldLayout); } /** Set keys4 (using the specified store) */ @@ -745,7 +712,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.setField(_tableId, _keyTuple, 4, EncodeArray.encode((keys4)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 4, EncodeArray.encode((keys4)), _fieldLayout); } /** Get the length of keys4 */ @@ -753,7 +720,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 4, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 4, _fieldLayout); unchecked { return _byteLength / 32; } @@ -764,7 +731,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 4, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 4, _fieldLayout); unchecked { return _byteLength / 32; } @@ -783,7 +750,7 @@ library KeysInTable { _tableId, _keyTuple, 4, - getFieldLayout(), + _fieldLayout, _index * 32, (_index + 1) * 32 ); @@ -800,14 +767,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - bytes memory _blob = _store.getFieldSlice( - _tableId, - _keyTuple, - 4, - getFieldLayout(), - _index * 32, - (_index + 1) * 32 - ); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 4, _fieldLayout, _index * 32, (_index + 1) * 32); return (Bytes.slice32(_blob, 0)); } } @@ -817,7 +777,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), _fieldLayout); } /** Push an element to keys4 (using the specified store) */ @@ -825,7 +785,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), _fieldLayout); } /** Pop an element from keys4 */ @@ -833,7 +793,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.popFromField(_tableId, _keyTuple, 4, 32, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 4, 32, _fieldLayout); } /** Pop an element from keys4 (using the specified store) */ @@ -841,7 +801,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.popFromField(_tableId, _keyTuple, 4, 32, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 4, 32, _fieldLayout); } /** @@ -853,7 +813,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 4, _index * 32, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 4, _index * 32, abi.encodePacked((_element)), _fieldLayout); } } @@ -866,7 +826,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - _store.updateInField(_tableId, _keyTuple, 4, _index * 32, abi.encodePacked((_element)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 4, _index * 32, abi.encodePacked((_element)), _fieldLayout); } } @@ -875,7 +835,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -884,7 +844,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -904,7 +864,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using individual values (using the specified store) */ @@ -924,7 +884,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using the data struct */ @@ -1047,7 +1007,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -1055,6 +1015,6 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol b/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol index ac68814aa7..a0ebf6dea2 100644 --- a/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol +++ b/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol @@ -21,14 +21,14 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("UsedKeysIndex"))); bytes32 constant UsedKeysIndexTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0006020001050000000000000000000000000000000000000000000000000000 +); + library UsedKeysIndex { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](2); - _fieldLayout[0] = 1; - _fieldLayout[1] = 5; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -65,19 +65,12 @@ library UsedKeysIndex { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get has */ @@ -86,7 +79,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -96,7 +89,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -106,7 +99,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((has)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((has)), _fieldLayout); } /** Set has (using the specified store) */ @@ -115,7 +108,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((has)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((has)), _fieldLayout); } /** Get index */ @@ -124,7 +117,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, _fieldLayout); return (uint40(Bytes.slice5(_blob, 0))); } @@ -134,7 +127,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, _fieldLayout); return (uint40(Bytes.slice5(_blob, 0))); } @@ -144,7 +137,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((index)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((index)), _fieldLayout); } /** Set index (using the specified store) */ @@ -153,7 +146,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((index)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((index)), _fieldLayout); } /** Get the full data */ @@ -162,7 +155,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -172,7 +165,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -187,7 +180,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using individual values (using the specified store) */ @@ -201,7 +194,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Decode the tightly packed blob using this table's field layout */ @@ -241,7 +234,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -250,7 +243,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/world/src/modules/keyswithvalue/tables/KeysWithValue.sol b/packages/world/src/modules/keyswithvalue/tables/KeysWithValue.sol index a13236775a..47b0af5076 100644 --- a/packages/world/src/modules/keyswithvalue/tables/KeysWithValue.sol +++ b/packages/world/src/modules/keyswithvalue/tables/KeysWithValue.sol @@ -18,12 +18,14 @@ import { FieldLayout, FieldLayoutLib } from "@latticexyz/store/src/FieldLayout.s import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCounter.sol"; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0000000100000000000000000000000000000000000000000000000000000000 +); + library KeysWithValue { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](0); - - return FieldLayoutLib.encode(_fieldLayout, 1); + return _fieldLayout; } /** Get the table's key schema */ @@ -56,19 +58,12 @@ library KeysWithValue { /** Register the table with its config */ function register(bytes32 _tableId) internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store, bytes32 _tableId) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get keysWithValue */ @@ -76,7 +71,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -89,7 +84,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -98,7 +93,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keysWithValue)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keysWithValue)), _fieldLayout); } /** Set keysWithValue (using the specified store) */ @@ -106,7 +101,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keysWithValue)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keysWithValue)), _fieldLayout); } /** Get the length of keysWithValue */ @@ -114,7 +109,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); unchecked { return _byteLength / 32; } @@ -125,7 +120,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); unchecked { return _byteLength / 32; } @@ -144,7 +139,7 @@ library KeysWithValue { _tableId, _keyTuple, 0, - getFieldLayout(), + _fieldLayout, _index * 32, (_index + 1) * 32 ); @@ -161,14 +156,7 @@ library KeysWithValue { _keyTuple[0] = valueHash; unchecked { - bytes memory _blob = _store.getFieldSlice( - _tableId, - _keyTuple, - 0, - getFieldLayout(), - _index * 32, - (_index + 1) * 32 - ); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, _fieldLayout, _index * 32, (_index + 1) * 32); return (Bytes.slice32(_blob, 0)); } } @@ -178,7 +166,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } /** Push an element to keysWithValue (using the specified store) */ @@ -186,7 +174,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } /** Pop an element from keysWithValue */ @@ -194,7 +182,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 32, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 0, 32, _fieldLayout); } /** Pop an element from keysWithValue (using the specified store) */ @@ -202,7 +190,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - _store.popFromField(_tableId, _keyTuple, 0, 32, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 0, 32, _fieldLayout); } /** @@ -214,7 +202,7 @@ library KeysWithValue { _keyTuple[0] = valueHash; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), _fieldLayout); } } @@ -227,7 +215,7 @@ library KeysWithValue { _keyTuple[0] = valueHash; unchecked { - _store.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), _fieldLayout); } } @@ -266,7 +254,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -274,6 +262,6 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/world/src/modules/std-delegations/tables/CallboundDelegations.sol b/packages/world/src/modules/std-delegations/tables/CallboundDelegations.sol index 070d868fe0..0cb37a05be 100644 --- a/packages/world/src/modules/std-delegations/tables/CallboundDelegations.sol +++ b/packages/world/src/modules/std-delegations/tables/CallboundDelegations.sol @@ -21,13 +21,14 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("CallboundDelegat"))); bytes32 constant CallboundDelegationsTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0020010020000000000000000000000000000000000000000000000000000000 +); + library CallboundDelegations { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](1); - _fieldLayout[0] = 32; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -66,19 +67,12 @@ library CallboundDelegations { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get availableCalls */ @@ -94,7 +88,7 @@ library CallboundDelegations { _keyTuple[2] = resourceSelector; _keyTuple[3] = funcSelectorAndArgsHash; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint256(Bytes.slice32(_blob, 0))); } @@ -112,7 +106,7 @@ library CallboundDelegations { _keyTuple[2] = resourceSelector; _keyTuple[3] = funcSelectorAndArgsHash; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint256(Bytes.slice32(_blob, 0))); } @@ -130,7 +124,7 @@ library CallboundDelegations { _keyTuple[2] = resourceSelector; _keyTuple[3] = funcSelectorAndArgsHash; - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((availableCalls)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((availableCalls)), _fieldLayout); } /** Set availableCalls (using the specified store) */ @@ -148,7 +142,7 @@ library CallboundDelegations { _keyTuple[2] = resourceSelector; _keyTuple[3] = funcSelectorAndArgsHash; - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((availableCalls)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((availableCalls)), _fieldLayout); } /** Tightly pack static data using this table's schema */ @@ -195,7 +189,7 @@ library CallboundDelegations { _keyTuple[2] = resourceSelector; _keyTuple[3] = funcSelectorAndArgsHash; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -212,6 +206,6 @@ library CallboundDelegations { _keyTuple[2] = resourceSelector; _keyTuple[3] = funcSelectorAndArgsHash; - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/world/src/modules/std-delegations/tables/TimeboundDelegations.sol b/packages/world/src/modules/std-delegations/tables/TimeboundDelegations.sol index 3e7a015869..4fc2762cee 100644 --- a/packages/world/src/modules/std-delegations/tables/TimeboundDelegations.sol +++ b/packages/world/src/modules/std-delegations/tables/TimeboundDelegations.sol @@ -21,13 +21,14 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("TimeboundDelegat"))); bytes32 constant TimeboundDelegationsTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0020010020000000000000000000000000000000000000000000000000000000 +); + library TimeboundDelegations { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](1); - _fieldLayout[0] = 32; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -62,19 +63,12 @@ library TimeboundDelegations { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get maxTimestamp */ @@ -83,7 +77,7 @@ library TimeboundDelegations { _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint256(Bytes.slice32(_blob, 0))); } @@ -93,7 +87,7 @@ library TimeboundDelegations { _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint256(Bytes.slice32(_blob, 0))); } @@ -103,7 +97,7 @@ library TimeboundDelegations { _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((maxTimestamp)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((maxTimestamp)), _fieldLayout); } /** Set maxTimestamp (using the specified store) */ @@ -112,7 +106,7 @@ library TimeboundDelegations { _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((maxTimestamp)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((maxTimestamp)), _fieldLayout); } /** Tightly pack static data using this table's schema */ @@ -145,7 +139,7 @@ library TimeboundDelegations { _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -154,6 +148,6 @@ library TimeboundDelegations { _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/world/src/modules/uniqueentity/tables/UniqueEntity.sol b/packages/world/src/modules/uniqueentity/tables/UniqueEntity.sol index 1b2238a638..a3e4568034 100644 --- a/packages/world/src/modules/uniqueentity/tables/UniqueEntity.sol +++ b/packages/world/src/modules/uniqueentity/tables/UniqueEntity.sol @@ -18,13 +18,14 @@ import { FieldLayout, FieldLayoutLib } from "@latticexyz/store/src/FieldLayout.s import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCounter.sol"; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0020010020000000000000000000000000000000000000000000000000000000 +); + library UniqueEntity { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](1); - _fieldLayout[0] = 32; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -55,26 +56,19 @@ library UniqueEntity { /** Register the table with its config */ function register(bytes32 _tableId) internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store, bytes32 _tableId) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ function get(bytes32 _tableId) internal view returns (uint256 value) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint256(Bytes.slice32(_blob, 0))); } @@ -82,7 +76,7 @@ library UniqueEntity { function get(IStore _store, bytes32 _tableId) internal view returns (uint256 value) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint256(Bytes.slice32(_blob, 0))); } @@ -90,14 +84,14 @@ library UniqueEntity { function set(bytes32 _tableId, uint256 value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } /** Set value (using the specified store) */ function set(IStore _store, bytes32 _tableId, uint256 value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } /** Tightly pack static data using this table's schema */ @@ -126,13 +120,13 @@ library UniqueEntity { function deleteRecord(bytes32 _tableId) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes32 _tableId) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/world/src/tables/Delegations.sol b/packages/world/src/tables/Delegations.sol index 22420ee76b..5bdb511112 100644 --- a/packages/world/src/tables/Delegations.sol +++ b/packages/world/src/tables/Delegations.sol @@ -21,13 +21,14 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Delegations"))); bytes32 constant DelegationsTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0020010020000000000000000000000000000000000000000000000000000000 +); + library Delegations { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](1); - _fieldLayout[0] = 32; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -62,19 +63,12 @@ library Delegations { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get delegationControlId */ @@ -83,7 +77,7 @@ library Delegations { _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (Bytes.slice32(_blob, 0)); } @@ -97,7 +91,7 @@ library Delegations { _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (Bytes.slice32(_blob, 0)); } @@ -107,7 +101,7 @@ library Delegations { _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((delegationControlId)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((delegationControlId)), _fieldLayout); } /** Set delegationControlId (using the specified store) */ @@ -116,7 +110,7 @@ library Delegations { _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((delegationControlId)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((delegationControlId)), _fieldLayout); } /** Tightly pack static data using this table's schema */ @@ -149,7 +143,7 @@ library Delegations { _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -158,6 +152,6 @@ library Delegations { _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/world/src/tables/InstalledModules.sol b/packages/world/src/tables/InstalledModules.sol index 9f7b45859c..637770c6b9 100644 --- a/packages/world/src/tables/InstalledModules.sol +++ b/packages/world/src/tables/InstalledModules.sol @@ -21,13 +21,14 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("InstalledModules"))); bytes32 constant InstalledModulesTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0014010014000000000000000000000000000000000000000000000000000000 +); + library InstalledModules { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](1); - _fieldLayout[0] = 20; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -62,19 +63,12 @@ library InstalledModules { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get moduleAddress */ @@ -83,7 +77,7 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (address(Bytes.slice20(_blob, 0))); } @@ -93,7 +87,7 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (address(Bytes.slice20(_blob, 0))); } @@ -103,7 +97,7 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((moduleAddress)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((moduleAddress)), _fieldLayout); } /** Set moduleAddress (using the specified store) */ @@ -112,7 +106,7 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((moduleAddress)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((moduleAddress)), _fieldLayout); } /** Tightly pack static data using this table's schema */ @@ -145,7 +139,7 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -154,6 +148,6 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/world/src/tables/NamespaceOwner.sol b/packages/world/src/tables/NamespaceOwner.sol index 85d58a4921..d290e3fcab 100644 --- a/packages/world/src/tables/NamespaceOwner.sol +++ b/packages/world/src/tables/NamespaceOwner.sol @@ -21,13 +21,14 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("NamespaceOwner"))); bytes32 constant NamespaceOwnerTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0014010014000000000000000000000000000000000000000000000000000000 +); + library NamespaceOwner { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](1); - _fieldLayout[0] = 20; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -60,19 +61,12 @@ library NamespaceOwner { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get owner */ @@ -80,7 +74,7 @@ library NamespaceOwner { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (address(Bytes.slice20(_blob, 0))); } @@ -89,7 +83,7 @@ library NamespaceOwner { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (address(Bytes.slice20(_blob, 0))); } @@ -98,7 +92,7 @@ library NamespaceOwner { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((owner)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((owner)), _fieldLayout); } /** Set owner (using the specified store) */ @@ -106,7 +100,7 @@ library NamespaceOwner { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((owner)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((owner)), _fieldLayout); } /** Tightly pack static data using this table's schema */ @@ -137,7 +131,7 @@ library NamespaceOwner { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -145,6 +139,6 @@ library NamespaceOwner { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/world/src/tables/ResourceAccess.sol b/packages/world/src/tables/ResourceAccess.sol index 51a3dba36d..1e6b75690b 100644 --- a/packages/world/src/tables/ResourceAccess.sol +++ b/packages/world/src/tables/ResourceAccess.sol @@ -21,13 +21,14 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("ResourceAccess"))); bytes32 constant ResourceAccessTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0001010001000000000000000000000000000000000000000000000000000000 +); + library ResourceAccess { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](1); - _fieldLayout[0] = 1; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -62,19 +63,12 @@ library ResourceAccess { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get access */ @@ -83,7 +77,7 @@ library ResourceAccess { _keyTuple[0] = resourceSelector; _keyTuple[1] = bytes32(uint256(uint160(caller))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -93,7 +87,7 @@ library ResourceAccess { _keyTuple[0] = resourceSelector; _keyTuple[1] = bytes32(uint256(uint160(caller))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -103,7 +97,7 @@ library ResourceAccess { _keyTuple[0] = resourceSelector; _keyTuple[1] = bytes32(uint256(uint160(caller))); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((access)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((access)), _fieldLayout); } /** Set access (using the specified store) */ @@ -112,7 +106,7 @@ library ResourceAccess { _keyTuple[0] = resourceSelector; _keyTuple[1] = bytes32(uint256(uint160(caller))); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((access)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((access)), _fieldLayout); } /** Tightly pack static data using this table's schema */ @@ -145,7 +139,7 @@ library ResourceAccess { _keyTuple[0] = resourceSelector; _keyTuple[1] = bytes32(uint256(uint160(caller))); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -154,7 +148,7 @@ library ResourceAccess { _keyTuple[0] = resourceSelector; _keyTuple[1] = bytes32(uint256(uint160(caller))); - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/world/test/tables/AddressArray.sol b/packages/world/test/tables/AddressArray.sol index ecd9e930ac..8064fad34e 100644 --- a/packages/world/test/tables/AddressArray.sol +++ b/packages/world/test/tables/AddressArray.sol @@ -18,12 +18,14 @@ import { FieldLayout, FieldLayoutLib } from "@latticexyz/store/src/FieldLayout.s import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCounter.sol"; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0000000100000000000000000000000000000000000000000000000000000000 +); + library AddressArray { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](0); - - return FieldLayoutLib.encode(_fieldLayout, 1); + return _fieldLayout; } /** Get the table's key schema */ @@ -56,19 +58,12 @@ library AddressArray { /** Register the table with its config */ function register(bytes32 _tableId) internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store, bytes32 _tableId) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ @@ -76,7 +71,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_address()); } @@ -85,7 +80,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_address()); } @@ -94,7 +89,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); } /** Set value (using the specified store) */ @@ -102,7 +97,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); } /** Get the length of value */ @@ -110,7 +105,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getFieldLayout()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); unchecked { return _byteLength / 20; } @@ -121,7 +116,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getFieldLayout()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); unchecked { return _byteLength / 20; } @@ -140,7 +135,7 @@ library AddressArray { _tableId, _keyTuple, 0, - getFieldLayout(), + _fieldLayout, _index * 20, (_index + 1) * 20 ); @@ -157,14 +152,7 @@ library AddressArray { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice( - _tableId, - _keyTuple, - 0, - getFieldLayout(), - _index * 20, - (_index + 1) * 20 - ); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, _fieldLayout, _index * 20, (_index + 1) * 20); return (address(Bytes.slice20(_blob, 0))); } } @@ -174,7 +162,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } /** Push an element to value (using the specified store) */ @@ -182,7 +170,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getFieldLayout()); + _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } /** Pop an element from value */ @@ -190,7 +178,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 20, getFieldLayout()); + StoreSwitch.popFromField(_tableId, _keyTuple, 0, 20, _fieldLayout); } /** Pop an element from value (using the specified store) */ @@ -198,7 +186,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 0, 20, getFieldLayout()); + _store.popFromField(_tableId, _keyTuple, 0, 20, _fieldLayout); } /** @@ -210,7 +198,7 @@ library AddressArray { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), getFieldLayout()); + StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), _fieldLayout); } } @@ -223,7 +211,7 @@ library AddressArray { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), getFieldLayout()); + _store.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), _fieldLayout); } } @@ -262,7 +250,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -270,6 +258,6 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/packages/world/test/tables/Bool.sol b/packages/world/test/tables/Bool.sol index 9d471471ac..001f076745 100644 --- a/packages/world/test/tables/Bool.sol +++ b/packages/world/test/tables/Bool.sol @@ -18,13 +18,14 @@ import { FieldLayout, FieldLayoutLib } from "@latticexyz/store/src/FieldLayout.s import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCounter.sol"; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0001010001000000000000000000000000000000000000000000000000000000 +); + library Bool { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](1); - _fieldLayout[0] = 1; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -55,26 +56,19 @@ library Bool { /** Register the table with its config */ function register(bytes32 _tableId) internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store, bytes32 _tableId) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ function get(bytes32 _tableId) internal view returns (bool value) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -82,7 +76,7 @@ library Bool { function get(IStore _store, bytes32 _tableId) internal view returns (bool value) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -90,14 +84,14 @@ library Bool { function set(bytes32 _tableId, bool value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } /** Set value (using the specified store) */ function set(IStore _store, bytes32 _tableId, bool value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } /** Tightly pack static data using this table's schema */ @@ -126,14 +120,14 @@ library Bool { function deleteRecord(bytes32 _tableId) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes32 _tableId) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/templates/phaser/packages/contracts/src/codegen/tables/Counter.sol b/templates/phaser/packages/contracts/src/codegen/tables/Counter.sol index 6b2a3f319b..5422d16dad 100644 --- a/templates/phaser/packages/contracts/src/codegen/tables/Counter.sol +++ b/templates/phaser/packages/contracts/src/codegen/tables/Counter.sol @@ -21,13 +21,14 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Counter"))); bytes32 constant CounterTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0004010004000000000000000000000000000000000000000000000000000000 +); + library Counter { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](1); - _fieldLayout[0] = 4; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -58,26 +59,19 @@ library Counter { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ function get() internal view returns (uint32 value) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint32(Bytes.slice4(_blob, 0))); } @@ -85,7 +79,7 @@ library Counter { function get(IStore _store) internal view returns (uint32 value) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint32(Bytes.slice4(_blob, 0))); } @@ -93,14 +87,14 @@ library Counter { function set(uint32 value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } /** Set value (using the specified store) */ function set(IStore _store, uint32 value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } /** Tightly pack static data using this table's schema */ @@ -129,13 +123,13 @@ library Counter { function deleteRecord() internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/templates/react/packages/contracts/src/codegen/tables/Counter.sol b/templates/react/packages/contracts/src/codegen/tables/Counter.sol index 6b2a3f319b..5422d16dad 100644 --- a/templates/react/packages/contracts/src/codegen/tables/Counter.sol +++ b/templates/react/packages/contracts/src/codegen/tables/Counter.sol @@ -21,13 +21,14 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Counter"))); bytes32 constant CounterTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0004010004000000000000000000000000000000000000000000000000000000 +); + library Counter { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](1); - _fieldLayout[0] = 4; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -58,26 +59,19 @@ library Counter { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ function get() internal view returns (uint32 value) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint32(Bytes.slice4(_blob, 0))); } @@ -85,7 +79,7 @@ library Counter { function get(IStore _store) internal view returns (uint32 value) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint32(Bytes.slice4(_blob, 0))); } @@ -93,14 +87,14 @@ library Counter { function set(uint32 value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } /** Set value (using the specified store) */ function set(IStore _store, uint32 value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } /** Tightly pack static data using this table's schema */ @@ -129,13 +123,13 @@ library Counter { function deleteRecord() internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/templates/threejs/packages/contracts/src/codegen/tables/Position.sol b/templates/threejs/packages/contracts/src/codegen/tables/Position.sol index b93c3a63c3..624887fbb2 100644 --- a/templates/threejs/packages/contracts/src/codegen/tables/Position.sol +++ b/templates/threejs/packages/contracts/src/codegen/tables/Position.sol @@ -21,6 +21,10 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Position"))); bytes32 constant PositionTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x000c030004040400000000000000000000000000000000000000000000000000 +); + struct PositionData { int32 x; int32 y; @@ -30,12 +34,7 @@ struct PositionData { library Position { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](3); - _fieldLayout[0] = 4; - _fieldLayout[1] = 4; - _fieldLayout[2] = 4; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -72,19 +71,12 @@ library Position { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get x */ @@ -92,7 +84,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -101,7 +93,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -110,7 +102,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), _fieldLayout); } /** Set x (using the specified store) */ @@ -118,7 +110,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), _fieldLayout); } /** Get y */ @@ -126,7 +118,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, _fieldLayout); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -135,7 +127,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, _fieldLayout); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -144,7 +136,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), _fieldLayout); } /** Set y (using the specified store) */ @@ -152,7 +144,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), _fieldLayout); } /** Get z */ @@ -160,7 +152,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, _fieldLayout); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -169,7 +161,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, _fieldLayout); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -178,7 +170,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 2, abi.encodePacked((z)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 2, abi.encodePacked((z)), _fieldLayout); } /** Set z (using the specified store) */ @@ -186,7 +178,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 2, abi.encodePacked((z)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 2, abi.encodePacked((z)), _fieldLayout); } /** Get the full data */ @@ -194,7 +186,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -203,7 +195,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getFieldLayout()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); } @@ -217,7 +209,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using individual values (using the specified store) */ @@ -230,7 +222,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, getFieldLayout()); + _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using the data struct */ @@ -280,7 +272,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ @@ -288,6 +280,6 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } } diff --git a/templates/vanilla/packages/contracts/src/codegen/tables/Counter.sol b/templates/vanilla/packages/contracts/src/codegen/tables/Counter.sol index 6b2a3f319b..5422d16dad 100644 --- a/templates/vanilla/packages/contracts/src/codegen/tables/Counter.sol +++ b/templates/vanilla/packages/contracts/src/codegen/tables/Counter.sol @@ -21,13 +21,14 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Counter"))); bytes32 constant CounterTableId = _tableId; +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0004010004000000000000000000000000000000000000000000000000000000 +); + library Counter { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { - uint256[] memory _fieldLayout = new uint256[](1); - _fieldLayout[0] = 4; - - return FieldLayoutLib.encode(_fieldLayout, 0); + return _fieldLayout; } /** Get the table's key schema */ @@ -58,26 +59,19 @@ library Counter { /** Register the table with its config */ function register() internal { - StoreSwitch.registerTable( - _tableId, - getFieldLayout(), - getKeySchema(), - getValueSchema(), - getKeyNames(), - getFieldNames() - ); + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { - _store.registerTable(_tableId, getFieldLayout(), getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ function get() internal view returns (uint32 value) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint32(Bytes.slice4(_blob, 0))); } @@ -85,7 +79,7 @@ library Counter { function get(IStore _store) internal view returns (uint32 value) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getFieldLayout()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, _fieldLayout); return (uint32(Bytes.slice4(_blob, 0))); } @@ -93,14 +87,14 @@ library Counter { function set(uint32 value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getFieldLayout()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } /** Set value (using the specified store) */ function set(IStore _store, uint32 value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getFieldLayout()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } /** Tightly pack static data using this table's schema */ @@ -129,13 +123,13 @@ library Counter { function deleteRecord() internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.deleteRecord(_tableId, _keyTuple, getFieldLayout()); + _store.deleteRecord(_tableId, _keyTuple, _fieldLayout); } }