From afaf2f5ffb36fe389a3aba8da2f6d8c84bdb26ab Mon Sep 17 00:00:00 2001 From: alvarius Date: Wed, 16 Aug 2023 23:46:46 +0200 Subject: [PATCH] feat(store,world): combine schema and metadata registration, rename getSchema to getValueSchema, change Schema table id (#1182) Co-authored-by: Kevin Ingersoll --- .changeset/modern-hornets-jam.md | 32 + docs/pages/store/internals.mdx | 8 +- .../contracts/src/codegen/tables/Multi.sol | 83 ++- .../contracts/src/codegen/tables/Number.sol | 54 +- .../src/codegen/tables/NumberList.sol | 86 +-- .../contracts/src/codegen/tables/Vector.sol | 80 ++- .../src/systems/NumberListSystem.sol | 2 +- .../types/ethers-contracts/IWorld.ts | 269 +++----- .../factories/IWorld__factory.ts | 139 ++-- e2e/packages/contracts/worlds.json | 2 +- .../src/codegen/tables/CounterTable.sol | 59 +- .../src/codegen/tables/Inventory.sol | 62 +- .../src/codegen/tables/MessageTable.sol | 51 +- .../contracts/src/systems/IncrementSystem.sol | 2 +- .../types/ethers-contracts/IWorld.ts | 269 +++----- .../factories/IWorld__factory.ts | 139 ++-- .../src/codegen/tables/Dynamics1.sol | 279 +++++---- .../src/codegen/tables/Dynamics2.sol | 173 ++--- .../src/codegen/tables/Ephemeral.sol | 50 +- .../src/codegen/tables/Singleton.sol | 182 +++--- .../contracts/src/codegen/tables/Statics.sol | 128 ++-- packages/cli/contracts/test/Tablegen.t.sol | 12 +- packages/cli/src/commands/trace.ts | 2 +- packages/cli/src/utils/deploy.ts | 19 +- .../store-sync/src/blockLogsToStorage.test.ts | 4 +- packages/store-sync/src/blockLogsToStorage.ts | 193 ++---- .../src/recs/defineInternalComponents.ts | 4 +- packages/store-sync/src/recs/recsStorage.ts | 6 +- .../src/sqlite/sqliteStorage.test.ts | 79 +-- packages/store/abi/IStore.sol/IStore.abi.json | 77 +-- .../store/abi/IStore.sol/IStoreData.abi.json | 2 +- .../store/abi/IStore.sol/IStoreRead.abi.json | 2 +- .../IStore.sol/IStoreRegistration.abi.json | 41 +- .../IStoreErrors.sol/IStoreErrors.abi.json | 16 + .../MirrorSubscriber.abi.json | 14 +- .../abi/StoreMock.sol/StoreMock.abi.json | 77 +-- .../abi/StoreRead.sol/StoreRead.abi.json | 18 +- .../StoreReadWithStubs.abi.json | 73 +-- .../Tables.abi.json} | 0 packages/store/gas-report.json | 132 ++-- packages/store/mud.config.ts | 6 +- packages/store/src/IStore.sol | 12 +- packages/store/src/IStoreErrors.sol | 1 + packages/store/src/StoreCore.sol | 121 +--- packages/store/src/StoreRead.sol | 4 +- packages/store/src/StoreReadWithStubs.sol | 9 +- packages/store/src/StoreSwitch.sol | 27 +- packages/store/src/codegen/Tables.sol | 2 +- .../store/src/codegen/tables/Callbacks.sol | 85 +-- packages/store/src/codegen/tables/Hooks.sol | 85 +-- .../store/src/codegen/tables/KeyEncoding.sol | 65 +- packages/store/src/codegen/tables/Mixed.sol | 158 ++--- .../src/codegen/tables/StoreMetadata.sol | 473 -------------- packages/store/src/codegen/tables/Tables.sol | 591 ++++++++++++++++++ packages/store/src/codegen/tables/Vector2.sol | 80 ++- packages/store/test/KeyEncoding.t.sol | 10 +- packages/store/test/MirrorSubscriber.sol | 10 +- packages/store/test/Mixed.t.sol | 8 +- packages/store/test/StoreCore.t.sol | 343 +++++----- packages/store/test/StoreCoreDynamic.t.sol | 68 +- packages/store/test/StoreCoreGas.t.sol | 208 +++--- packages/store/test/StoreMetadata.t.sol | 30 - packages/store/test/StoreMock.sol | 14 +- packages/store/test/Vector2.t.sol | 10 +- packages/store/test/tables/Callbacks.t.sol | 2 +- packages/store/ts/codegen/ephemeral.ts | 2 +- packages/store/ts/codegen/field.ts | 14 +- packages/store/ts/codegen/record.ts | 4 +- packages/store/ts/codegen/renderTable.ts | 48 +- .../abi/CoreModule.sol/CoreModule.abi.json | 16 +- .../abi/CoreSystem.sol/CoreSystem.abi.json | 135 ++-- .../abi/IBaseWorld.sol/IBaseWorld.abi.json | 139 ++-- packages/world/abi/IStore.sol/IStore.abi.json | 77 +-- .../world/abi/IStore.sol/IStoreData.abi.json | 2 +- .../world/abi/IStore.sol/IStoreRead.abi.json | 2 +- .../IStore.sol/IStoreRegistration.abi.json | 41 +- .../IStoreErrors.sol/IStoreErrors.abi.json | 16 + .../IWorldRegistrationSystem.abi.json | 42 +- .../abi/StoreRead.sol/StoreRead.abi.json | 18 +- .../StoreRegistrationSystem.abi.json | 41 +- .../Tables.abi.json} | 0 packages/world/abi/World.sol/World.abi.json | 18 +- .../WorldRegistrationSystem.abi.json | 68 +- .../world/abi/src/IStore.sol/IStore.abi.json | 77 +-- .../abi/src/IStore.sol/IStoreData.abi.json | 2 +- .../abi/src/IStore.sol/IStoreRead.abi.json | 2 +- .../IStore.sol/IStoreRegistration.abi.json | 41 +- packages/world/gas-report.json | 104 ++- packages/world/src/World.sol | 2 +- .../interfaces/IWorldRegistrationSystem.sol | 11 +- .../world/src/modules/core/CoreModule.sol | 34 +- .../StoreRegistrationSystem.sol | 39 +- .../WorldRegistrationSystem.sol | 23 +- .../modules/core/tables/FunctionSelectors.sol | 92 ++- .../src/modules/core/tables/ResourceType.sol | 58 +- .../src/modules/core/tables/SystemHooks.sol | 85 +-- .../modules/core/tables/SystemRegistry.sol | 58 +- .../world/src/modules/core/tables/Systems.sol | 80 ++- .../modules/keysintable/KeysInTableModule.sol | 20 +- .../keysintable/tables/KeysInTable.sol | 249 ++++---- .../keysintable/tables/UsedKeysIndex.sol | 81 ++- .../keyswithvalue/KeysWithValueModule.sol | 16 +- .../keyswithvalue/tables/KeysWithValue.sol | 85 +-- .../uniqueentity/UniqueEntityModule.sol | 7 +- .../uniqueentity/tables/UniqueEntity.sol | 59 +- .../world/src/tables/InstalledModules.sol | 69 +- packages/world/src/tables/NamespaceOwner.sol | 58 +- packages/world/src/tables/ResourceAccess.sol | 61 +- packages/world/test/AccessControl.t.sol | 4 +- packages/world/test/KeysInTableModule.t.sol | 77 ++- packages/world/test/KeysWithValueModule.t.sol | 9 +- packages/world/test/World.t.sol | 241 ++++--- packages/world/test/WorldDynamicUpdate.t.sol | 36 +- packages/world/test/query.t.sol | 122 ++-- packages/world/test/tables/AddressArray.sol | 85 +-- packages/world/test/tables/Bool.sol | 59 +- .../contracts/src/codegen/tables/Counter.sol | 59 +- .../contracts/src/codegen/tables/Counter.sol | 59 +- .../contracts/src/codegen/tables/Position.sol | 92 ++- .../contracts/src/codegen/tables/Counter.sol | 59 +- 120 files changed, 4069 insertions(+), 4271 deletions(-) create mode 100644 .changeset/modern-hornets-jam.md rename packages/store/abi/{StoreMetadata.sol/StoreMetadata.abi.json => Tables.sol/Tables.abi.json} (100%) delete mode 100644 packages/store/src/codegen/tables/StoreMetadata.sol create mode 100644 packages/store/src/codegen/tables/Tables.sol delete mode 100644 packages/store/test/StoreMetadata.t.sol rename packages/world/abi/{StoreMetadata.sol/StoreMetadata.abi.json => Tables.sol/Tables.abi.json} (100%) diff --git a/.changeset/modern-hornets-jam.md b/.changeset/modern-hornets-jam.md new file mode 100644 index 0000000000..3b4440ce83 --- /dev/null +++ b/.changeset/modern-hornets-jam.md @@ -0,0 +1,32 @@ +--- +"@latticexyz/cli": major +"@latticexyz/store": major +"@latticexyz/world": major +"@latticexyz/store-sync": major +"create-mud": patch +--- + +- `Store`'s internal schema table is now a normal table instead of using special code paths. It is renamed to Tables, and the table ID changed from `mudstore:schema` to `mudstore:Tables` +- `Store`'s `registerSchema` and `setMetadata` are combined into a single `registerTable` method. This means metadata (key names, field names) is immutable and indexers can create tables with this metadata when a new table is registered on-chain. + + ```diff + - function registerSchema(bytes32 table, Schema schema, Schema keySchema) external; + - + - function setMetadata(bytes32 table, string calldata tableName, string[] calldata fieldNames) external; + + + function registerTable( + + bytes32 table, + + Schema keySchema, + + Schema valueSchema, + + string[] calldata keyNames, + + string[] calldata fieldNames + + ) external; + ``` + +- `World`'s `registerTable` method is updated to match the `Store` interface, `setMetadata` is removed +- The `getSchema` method is renamed to `getValueSchema` on all interfaces + ```diff + - function getSchema(bytes32 table) external view returns (Schema schema); + + function getValueSchema(bytes32 table) external view returns (Schema valueSchema); + ``` +- The `store-sync` and `cli` packages are updated to integrate the breaking protocol changes. Downstream projects only need to manually integrate these changes if they access low level `Store` or `World` functions. Otherwise, a fresh deploy with the latest MUD will get you these changes. diff --git a/docs/pages/store/internals.mdx b/docs/pages/store/internals.mdx index 6097de46a1..c0b24d5580 100644 --- a/docs/pages/store/internals.mdx +++ b/docs/pages/store/internals.mdx @@ -5,9 +5,5 @@ It is called within `StoreRead`'s constructor (which is implemented by `World`). ### Tables -- `SCHEMA_TABLE` - a very low-level table with no library for it. - All table schemas are stored here. - Directly using it is not recommended, table libraries generated via `tablegen` have schema methods like `registerSchema`. - Alternatively, `StoreCore` exposes `getSchema`, `getKeySchema`, `hasTable`, `registerSchema`, `setMetadata`, most of which are used by table libraries internally. -- `Hooks` - maps tableId to an array of table hooks -- `StoreMetadata` - maps tableId to optional metadata, like table and field names +- `Tables` - maps table ID to the table's key schema, value schema, key names, and field names. +- `Hooks` - maps table ID to an array of table hooks diff --git a/e2e/packages/contracts/src/codegen/tables/Multi.sol b/e2e/packages/contracts/src/codegen/tables/Multi.sol index 80ed45e627..4a8dda1a71 100644 --- a/e2e/packages/contracts/src/codegen/tables/Multi.sol +++ b/e2e/packages/contracts/src/codegen/tables/Multi.sol @@ -26,15 +26,7 @@ struct MultiData { } library Multi { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](2); - _schema[0] = SchemaType.INT256; - _schema[1] = SchemaType.BOOL; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](4); _schema[0] = SchemaType.UINT32; @@ -45,34 +37,39 @@ library Multi { return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](2); - _fieldNames[0] = "num"; - _fieldNames[1] = "value"; - return ("Multi", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](2); + _schema[0] = SchemaType.INT256; + _schema[1] = SchemaType.BOOL; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](4); + keyNames[0] = "a"; + keyNames[1] = "b"; + keyNames[2] = "c"; + keyNames[3] = "d"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](2); + fieldNames[0] = "num"; + fieldNames[1] = "value"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get num */ @@ -83,7 +80,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (int256(uint256(Bytes.slice32(_blob, 0)))); } @@ -95,7 +92,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (int256(uint256(Bytes.slice32(_blob, 0)))); } @@ -107,7 +104,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((num)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((num)), getValueSchema()); } /** Set num (using the specified store) */ @@ -118,7 +115,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((num)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((num)), getValueSchema()); } /** Get value */ @@ -129,7 +126,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getValueSchema()); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -141,7 +138,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getValueSchema()); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -153,7 +150,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((value)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((value)), getValueSchema()); } /** Set value (using the specified store) */ @@ -164,7 +161,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((value)), getSchema()); + _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((value)), getValueSchema()); } /** Get the full data */ @@ -175,7 +172,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -187,7 +184,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -201,7 +198,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - StoreSwitch.setRecord(_tableId, _keyTuple, _data, getSchema()); + StoreSwitch.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using individual values (using the specified store) */ @@ -214,7 +211,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - _store.setRecord(_tableId, _keyTuple, _data, getSchema()); + _store.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using the data struct */ @@ -258,7 +255,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -269,7 +266,7 @@ library Multi { _keyTuple[2] = bytes32(uint256(c)); _keyTuple[3] = bytes32(uint256(int256(d))); - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/e2e/packages/contracts/src/codegen/tables/Number.sol b/e2e/packages/contracts/src/codegen/tables/Number.sol index d7dac5bac4..a626749183 100644 --- a/e2e/packages/contracts/src/codegen/tables/Number.sol +++ b/e2e/packages/contracts/src/codegen/tables/Number.sol @@ -21,48 +21,42 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Numbe bytes32 constant NumberTableId = _tableId; library Number { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { + /** Get the table's key schema */ + function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); _schema[0] = SchemaType.UINT32; return SchemaLib.encode(_schema); } - function getKeySchema() internal pure returns (Schema) { + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); _schema[0] = SchemaType.UINT32; return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "value"; - return ("Number", _fieldNames); - } - - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "key"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "value"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ @@ -70,7 +64,7 @@ library Number { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (uint32(Bytes.slice4(_blob, 0))); } @@ -79,7 +73,7 @@ library Number { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (uint32(Bytes.slice4(_blob, 0))); } @@ -88,7 +82,7 @@ library Number { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getValueSchema()); } /** Set value (using the specified store) */ @@ -96,7 +90,7 @@ library Number { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getValueSchema()); } /** Tightly pack full data using this table's schema */ @@ -117,7 +111,7 @@ library Number { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -125,6 +119,6 @@ library Number { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/e2e/packages/contracts/src/codegen/tables/NumberList.sol b/e2e/packages/contracts/src/codegen/tables/NumberList.sol index 16725ce200..049c84f70a 100644 --- a/e2e/packages/contracts/src/codegen/tables/NumberList.sol +++ b/e2e/packages/contracts/src/codegen/tables/NumberList.sol @@ -21,54 +21,47 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Numbe bytes32 constant NumberListTableId = _tableId; library NumberList { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.UINT32_ARRAY; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](0); return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "value"; - return ("NumberList", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](1); + _schema[0] = SchemaType.UINT32_ARRAY; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](0); } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "value"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, 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, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } @@ -76,7 +69,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, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } @@ -84,21 +77,21 @@ library NumberList { function set(uint32[] memory value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getValueSchema()); } /** 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)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getValueSchema()); } /** Get the length of value */ function length() internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](0); - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); unchecked { return _byteLength / 4; } @@ -108,7 +101,7 @@ library NumberList { function length(IStore _store) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](0); - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); unchecked { return _byteLength / 4; } @@ -122,7 +115,14 @@ library NumberList { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 4, (_index + 1) * 4); + bytes memory _blob = StoreSwitch.getFieldSlice( + _tableId, + _keyTuple, + 0, + getValueSchema(), + _index * 4, + (_index + 1) * 4 + ); return (uint32(Bytes.slice4(_blob, 0))); } } @@ -135,7 +135,7 @@ library NumberList { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 4, (_index + 1) * 4); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getValueSchema(), _index * 4, (_index + 1) * 4); return (uint32(Bytes.slice4(_blob, 0))); } } @@ -144,28 +144,28 @@ library NumberList { function push(uint32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** 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)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from value */ function pop() internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 4, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 0, 4, getValueSchema()); } /** 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, getSchema()); + _store.popFromField(_tableId, _keyTuple, 0, 4, getValueSchema()); } /** @@ -176,7 +176,7 @@ library NumberList { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 4, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 4, abi.encodePacked((_element)), getValueSchema()); } } @@ -188,7 +188,7 @@ library NumberList { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - _store.updateInField(_tableId, _keyTuple, 0, _index * 4, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 0, _index * 4, abi.encodePacked((_element)), getValueSchema()); } } @@ -212,13 +212,13 @@ library NumberList { function deleteRecord() internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* 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, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/e2e/packages/contracts/src/codegen/tables/Vector.sol b/e2e/packages/contracts/src/codegen/tables/Vector.sol index ab38e25720..edd2318914 100644 --- a/e2e/packages/contracts/src/codegen/tables/Vector.sol +++ b/e2e/packages/contracts/src/codegen/tables/Vector.sol @@ -26,15 +26,7 @@ struct VectorData { } library Vector { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](2); - _schema[0] = SchemaType.INT32; - _schema[1] = SchemaType.INT32; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); _schema[0] = SchemaType.UINT32; @@ -42,34 +34,36 @@ library Vector { return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](2); - _fieldNames[0] = "x"; - _fieldNames[1] = "y"; - return ("Vector", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](2); + _schema[0] = SchemaType.INT32; + _schema[1] = SchemaType.INT32; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "key"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](2); + fieldNames[0] = "x"; + fieldNames[1] = "y"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get x */ @@ -77,7 +71,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -86,7 +80,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -95,7 +89,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), getValueSchema()); } /** Set x (using the specified store) */ @@ -103,7 +97,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), getValueSchema()); } /** Get y */ @@ -111,7 +105,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getValueSchema()); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -120,7 +114,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getValueSchema()); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -129,7 +123,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), getValueSchema()); } /** Set y (using the specified store) */ @@ -137,7 +131,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), getSchema()); + _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), getValueSchema()); } /** Get the full data */ @@ -145,7 +139,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -154,7 +148,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -165,7 +159,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - StoreSwitch.setRecord(_tableId, _keyTuple, _data, getSchema()); + StoreSwitch.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using individual values (using the specified store) */ @@ -175,7 +169,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - _store.setRecord(_tableId, _keyTuple, _data, getSchema()); + _store.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using the data struct */ @@ -213,7 +207,7 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -221,6 +215,6 @@ library Vector { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(key)); - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/e2e/packages/contracts/src/systems/NumberListSystem.sol b/e2e/packages/contracts/src/systems/NumberListSystem.sol index 4587ea3879..83446e12fe 100644 --- a/e2e/packages/contracts/src/systems/NumberListSystem.sol +++ b/e2e/packages/contracts/src/systems/NumberListSystem.sol @@ -22,7 +22,7 @@ contract NumberListSystem is System { } bytes32[] memory emptyKey; - StoreSwitch.pushToField(NumberListTableId, emptyKey, 0, EncodeArray.encode(list), NumberList.getSchema()); + StoreSwitch.pushToField(NumberListTableId, emptyKey, 0, EncodeArray.encode(list), NumberList.getValueSchema()); } function pop() public { diff --git a/e2e/packages/contracts/types/ethers-contracts/IWorld.ts b/e2e/packages/contracts/types/ethers-contracts/IWorld.ts index 69bddd92f8..d71872c609 100644 --- a/e2e/packages/contracts/types/ethers-contracts/IWorld.ts +++ b/e2e/packages/contracts/types/ethers-contracts/IWorld.ts @@ -40,7 +40,7 @@ export interface IWorldInterface extends utils.Interface { "getFieldSlice(bytes32,bytes32[],uint8,bytes32,uint256,uint256)": FunctionFragment; "getKeySchema(bytes32)": FunctionFragment; "getRecord(bytes32,bytes32[],bytes32)": FunctionFragment; - "getSchema(bytes32)": FunctionFragment; + "getValueSchema(bytes32)": FunctionFragment; "grantAccess(bytes16,bytes16,address)": FunctionFragment; "installModule(address,bytes)": FunctionFragment; "installRootModule(address,bytes)": FunctionFragment; @@ -55,18 +55,16 @@ export interface IWorldInterface extends utils.Interface { "registerHook(bytes16,bytes16,address)": FunctionFragment; "registerNamespace(bytes16)": FunctionFragment; "registerRootFunctionSelector(bytes16,bytes16,bytes4,bytes4)": FunctionFragment; - "registerSchema(bytes32,bytes32,bytes32)": FunctionFragment; "registerStoreHook(bytes32,address)": FunctionFragment; "registerSystem(bytes16,bytes16,address,bool)": FunctionFragment; "registerSystemHook(bytes16,bytes16,address)": FunctionFragment; - "registerTable(bytes16,bytes16,bytes32,bytes32)": FunctionFragment; + "registerTable(bytes16,bytes16,bytes32,bytes32,string[],string[])": FunctionFragment; + "registerTable(bytes32,bytes32,bytes32,string[],string[])": FunctionFragment; "registerTableHook(bytes16,bytes16,address)": FunctionFragment; "revokeAccess(bytes16,bytes16,address)": FunctionFragment; "set(uint32[])": FunctionFragment; "setField(bytes16,bytes16,bytes32[],uint8,bytes,bytes32)": FunctionFragment; "setField(bytes32,bytes32[],uint8,bytes,bytes32)": FunctionFragment; - "setMetadata(bytes16,bytes16,string,string[])": FunctionFragment; - "setMetadata(bytes32,string,string[])": FunctionFragment; "setRecord(bytes32,bytes32[],bytes,bytes32)": FunctionFragment; "setRecord(bytes16,bytes16,bytes32[],bytes,bytes32)": FunctionFragment; "stub(uint256)": FunctionFragment; @@ -86,7 +84,7 @@ export interface IWorldInterface extends utils.Interface { | "getFieldSlice" | "getKeySchema" | "getRecord" - | "getSchema" + | "getValueSchema" | "grantAccess" | "installModule" | "installRootModule" @@ -101,18 +99,16 @@ export interface IWorldInterface extends utils.Interface { | "registerHook" | "registerNamespace" | "registerRootFunctionSelector" - | "registerSchema" | "registerStoreHook" | "registerSystem" | "registerSystemHook" - | "registerTable" + | "registerTable(bytes16,bytes16,bytes32,bytes32,string[],string[])" + | "registerTable(bytes32,bytes32,bytes32,string[],string[])" | "registerTableHook" | "revokeAccess" | "set" | "setField(bytes16,bytes16,bytes32[],uint8,bytes,bytes32)" | "setField(bytes32,bytes32[],uint8,bytes,bytes32)" - | "setMetadata(bytes16,bytes16,string,string[])" - | "setMetadata(bytes32,string,string[])" | "setRecord(bytes32,bytes32[],bytes,bytes32)" | "setRecord(bytes16,bytes16,bytes32[],bytes,bytes32)" | "stub" @@ -206,7 +202,7 @@ export interface IWorldInterface extends utils.Interface { ] ): string; encodeFunctionData( - functionFragment: "getSchema", + functionFragment: "getValueSchema", values: [PromiseOrValue] ): string; encodeFunctionData( @@ -306,14 +302,6 @@ export interface IWorldInterface extends utils.Interface { PromiseOrValue ] ): string; - encodeFunctionData( - functionFragment: "registerSchema", - values: [ - PromiseOrValue, - PromiseOrValue, - PromiseOrValue - ] - ): string; encodeFunctionData( functionFragment: "registerStoreHook", values: [PromiseOrValue, PromiseOrValue] @@ -336,12 +324,24 @@ export interface IWorldInterface extends utils.Interface { ] ): string; encodeFunctionData( - functionFragment: "registerTable", + functionFragment: "registerTable(bytes16,bytes16,bytes32,bytes32,string[],string[])", values: [ PromiseOrValue, PromiseOrValue, PromiseOrValue, - PromiseOrValue + PromiseOrValue, + PromiseOrValue[], + PromiseOrValue[] + ] + ): string; + encodeFunctionData( + functionFragment: "registerTable(bytes32,bytes32,bytes32,string[],string[])", + values: [ + PromiseOrValue, + PromiseOrValue, + PromiseOrValue, + PromiseOrValue[], + PromiseOrValue[] ] ): string; encodeFunctionData( @@ -385,23 +385,6 @@ export interface IWorldInterface extends utils.Interface { PromiseOrValue ] ): string; - encodeFunctionData( - functionFragment: "setMetadata(bytes16,bytes16,string,string[])", - values: [ - PromiseOrValue, - PromiseOrValue, - PromiseOrValue, - PromiseOrValue[] - ] - ): string; - encodeFunctionData( - functionFragment: "setMetadata(bytes32,string,string[])", - values: [ - PromiseOrValue, - PromiseOrValue, - PromiseOrValue[] - ] - ): string; encodeFunctionData( functionFragment: "setRecord(bytes32,bytes32[],bytes,bytes32)", values: [ @@ -480,7 +463,10 @@ export interface IWorldInterface extends utils.Interface { data: BytesLike ): Result; decodeFunctionResult(functionFragment: "getRecord", data: BytesLike): Result; - decodeFunctionResult(functionFragment: "getSchema", data: BytesLike): Result; + decodeFunctionResult( + functionFragment: "getValueSchema", + data: BytesLike + ): Result; decodeFunctionResult( functionFragment: "grantAccess", data: BytesLike @@ -528,10 +514,6 @@ export interface IWorldInterface extends utils.Interface { functionFragment: "registerRootFunctionSelector", data: BytesLike ): Result; - decodeFunctionResult( - functionFragment: "registerSchema", - data: BytesLike - ): Result; decodeFunctionResult( functionFragment: "registerStoreHook", data: BytesLike @@ -545,7 +527,11 @@ export interface IWorldInterface extends utils.Interface { data: BytesLike ): Result; decodeFunctionResult( - functionFragment: "registerTable", + functionFragment: "registerTable(bytes16,bytes16,bytes32,bytes32,string[],string[])", + data: BytesLike + ): Result; + decodeFunctionResult( + functionFragment: "registerTable(bytes32,bytes32,bytes32,string[],string[])", data: BytesLike ): Result; decodeFunctionResult( @@ -565,14 +551,6 @@ export interface IWorldInterface extends utils.Interface { functionFragment: "setField(bytes32,bytes32[],uint8,bytes,bytes32)", data: BytesLike ): Result; - decodeFunctionResult( - functionFragment: "setMetadata(bytes16,bytes16,string,string[])", - data: BytesLike - ): Result; - decodeFunctionResult( - functionFragment: "setMetadata(bytes32,string,string[])", - data: BytesLike - ): Result; decodeFunctionResult( functionFragment: "setRecord(bytes32,bytes32[],bytes,bytes32)", data: BytesLike @@ -765,7 +743,7 @@ export interface IWorld extends BaseContract { overrides?: CallOverrides ): Promise<[string] & { data: string }>; - getSchema( + getValueSchema( table: PromiseOrValue, overrides?: CallOverrides ): Promise<[string] & { schema: string }>; @@ -870,13 +848,6 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - registerSchema( - table: PromiseOrValue, - schema: PromiseOrValue, - keySchema: PromiseOrValue, - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - registerStoreHook( table: PromiseOrValue, hook: PromiseOrValue, @@ -898,11 +869,22 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - registerTable( + "registerTable(bytes16,bytes16,bytes32,bytes32,string[],string[])"( namespace: PromiseOrValue, name: PromiseOrValue, + keySchema: PromiseOrValue, valueSchema: PromiseOrValue, + keyNames: PromiseOrValue[], + fieldNames: PromiseOrValue[], + overrides?: Overrides & { from?: PromiseOrValue } + ): Promise; + + "registerTable(bytes32,bytes32,bytes32,string[],string[])"( + table: PromiseOrValue, keySchema: PromiseOrValue, + valueSchema: PromiseOrValue, + keyNames: PromiseOrValue[], + fieldNames: PromiseOrValue[], overrides?: Overrides & { from?: PromiseOrValue } ): Promise; @@ -944,21 +926,6 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - "setMetadata(bytes16,bytes16,string,string[])"( - namespace: PromiseOrValue, - name: PromiseOrValue, - tableName: PromiseOrValue, - fieldNames: PromiseOrValue[], - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - - "setMetadata(bytes32,string,string[])"( - table: PromiseOrValue, - tableName: PromiseOrValue, - fieldNames: PromiseOrValue[], - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - "setRecord(bytes32,bytes32[],bytes,bytes32)"( table: PromiseOrValue, key: PromiseOrValue[], @@ -1080,7 +1047,7 @@ export interface IWorld extends BaseContract { overrides?: CallOverrides ): Promise; - getSchema( + getValueSchema( table: PromiseOrValue, overrides?: CallOverrides ): Promise; @@ -1185,13 +1152,6 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - registerSchema( - table: PromiseOrValue, - schema: PromiseOrValue, - keySchema: PromiseOrValue, - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - registerStoreHook( table: PromiseOrValue, hook: PromiseOrValue, @@ -1213,11 +1173,22 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - registerTable( + "registerTable(bytes16,bytes16,bytes32,bytes32,string[],string[])"( namespace: PromiseOrValue, name: PromiseOrValue, + keySchema: PromiseOrValue, valueSchema: PromiseOrValue, + keyNames: PromiseOrValue[], + fieldNames: PromiseOrValue[], + overrides?: Overrides & { from?: PromiseOrValue } + ): Promise; + + "registerTable(bytes32,bytes32,bytes32,string[],string[])"( + table: PromiseOrValue, keySchema: PromiseOrValue, + valueSchema: PromiseOrValue, + keyNames: PromiseOrValue[], + fieldNames: PromiseOrValue[], overrides?: Overrides & { from?: PromiseOrValue } ): Promise; @@ -1259,21 +1230,6 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - "setMetadata(bytes16,bytes16,string,string[])"( - namespace: PromiseOrValue, - name: PromiseOrValue, - tableName: PromiseOrValue, - fieldNames: PromiseOrValue[], - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - - "setMetadata(bytes32,string,string[])"( - table: PromiseOrValue, - tableName: PromiseOrValue, - fieldNames: PromiseOrValue[], - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - "setRecord(bytes32,bytes32[],bytes,bytes32)"( table: PromiseOrValue, key: PromiseOrValue[], @@ -1395,7 +1351,7 @@ export interface IWorld extends BaseContract { overrides?: CallOverrides ): Promise; - getSchema( + getValueSchema( table: PromiseOrValue, overrides?: CallOverrides ): Promise; @@ -1498,13 +1454,6 @@ export interface IWorld extends BaseContract { overrides?: CallOverrides ): Promise; - registerSchema( - table: PromiseOrValue, - schema: PromiseOrValue, - keySchema: PromiseOrValue, - overrides?: CallOverrides - ): Promise; - registerStoreHook( table: PromiseOrValue, hook: PromiseOrValue, @@ -1526,14 +1475,25 @@ export interface IWorld extends BaseContract { overrides?: CallOverrides ): Promise; - registerTable( + "registerTable(bytes16,bytes16,bytes32,bytes32,string[],string[])"( namespace: PromiseOrValue, name: PromiseOrValue, - valueSchema: PromiseOrValue, keySchema: PromiseOrValue, + valueSchema: PromiseOrValue, + keyNames: PromiseOrValue[], + fieldNames: PromiseOrValue[], overrides?: CallOverrides ): Promise; + "registerTable(bytes32,bytes32,bytes32,string[],string[])"( + table: PromiseOrValue, + keySchema: PromiseOrValue, + valueSchema: PromiseOrValue, + keyNames: PromiseOrValue[], + fieldNames: PromiseOrValue[], + overrides?: CallOverrides + ): Promise; + registerTableHook( namespace: PromiseOrValue, name: PromiseOrValue, @@ -1572,21 +1532,6 @@ export interface IWorld extends BaseContract { overrides?: CallOverrides ): Promise; - "setMetadata(bytes16,bytes16,string,string[])"( - namespace: PromiseOrValue, - name: PromiseOrValue, - tableName: PromiseOrValue, - fieldNames: PromiseOrValue[], - overrides?: CallOverrides - ): Promise; - - "setMetadata(bytes32,string,string[])"( - table: PromiseOrValue, - tableName: PromiseOrValue, - fieldNames: PromiseOrValue[], - overrides?: CallOverrides - ): Promise; - "setRecord(bytes32,bytes32[],bytes,bytes32)"( table: PromiseOrValue, key: PromiseOrValue[], @@ -1755,7 +1700,7 @@ export interface IWorld extends BaseContract { overrides?: CallOverrides ): Promise; - getSchema( + getValueSchema( table: PromiseOrValue, overrides?: CallOverrides ): Promise; @@ -1860,13 +1805,6 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - registerSchema( - table: PromiseOrValue, - schema: PromiseOrValue, - keySchema: PromiseOrValue, - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - registerStoreHook( table: PromiseOrValue, hook: PromiseOrValue, @@ -1888,11 +1826,22 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - registerTable( + "registerTable(bytes16,bytes16,bytes32,bytes32,string[],string[])"( namespace: PromiseOrValue, name: PromiseOrValue, + keySchema: PromiseOrValue, valueSchema: PromiseOrValue, + keyNames: PromiseOrValue[], + fieldNames: PromiseOrValue[], + overrides?: Overrides & { from?: PromiseOrValue } + ): Promise; + + "registerTable(bytes32,bytes32,bytes32,string[],string[])"( + table: PromiseOrValue, keySchema: PromiseOrValue, + valueSchema: PromiseOrValue, + keyNames: PromiseOrValue[], + fieldNames: PromiseOrValue[], overrides?: Overrides & { from?: PromiseOrValue } ): Promise; @@ -1934,21 +1883,6 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - "setMetadata(bytes16,bytes16,string,string[])"( - namespace: PromiseOrValue, - name: PromiseOrValue, - tableName: PromiseOrValue, - fieldNames: PromiseOrValue[], - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - - "setMetadata(bytes32,string,string[])"( - table: PromiseOrValue, - tableName: PromiseOrValue, - fieldNames: PromiseOrValue[], - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - "setRecord(bytes32,bytes32[],bytes,bytes32)"( table: PromiseOrValue, key: PromiseOrValue[], @@ -2071,7 +2005,7 @@ export interface IWorld extends BaseContract { overrides?: CallOverrides ): Promise; - getSchema( + getValueSchema( table: PromiseOrValue, overrides?: CallOverrides ): Promise; @@ -2176,13 +2110,6 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - registerSchema( - table: PromiseOrValue, - schema: PromiseOrValue, - keySchema: PromiseOrValue, - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - registerStoreHook( table: PromiseOrValue, hook: PromiseOrValue, @@ -2204,11 +2131,22 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - registerTable( + "registerTable(bytes16,bytes16,bytes32,bytes32,string[],string[])"( namespace: PromiseOrValue, name: PromiseOrValue, + keySchema: PromiseOrValue, valueSchema: PromiseOrValue, + keyNames: PromiseOrValue[], + fieldNames: PromiseOrValue[], + overrides?: Overrides & { from?: PromiseOrValue } + ): Promise; + + "registerTable(bytes32,bytes32,bytes32,string[],string[])"( + table: PromiseOrValue, keySchema: PromiseOrValue, + valueSchema: PromiseOrValue, + keyNames: PromiseOrValue[], + fieldNames: PromiseOrValue[], overrides?: Overrides & { from?: PromiseOrValue } ): Promise; @@ -2250,21 +2188,6 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - "setMetadata(bytes16,bytes16,string,string[])"( - namespace: PromiseOrValue, - name: PromiseOrValue, - tableName: PromiseOrValue, - fieldNames: PromiseOrValue[], - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - - "setMetadata(bytes32,string,string[])"( - table: PromiseOrValue, - tableName: PromiseOrValue, - fieldNames: PromiseOrValue[], - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - "setRecord(bytes32,bytes32[],bytes,bytes32)"( table: PromiseOrValue, key: PromiseOrValue[], diff --git a/e2e/packages/contracts/types/ethers-contracts/factories/IWorld__factory.ts b/e2e/packages/contracts/types/ethers-contracts/factories/IWorld__factory.ts index 8909650ca8..238458566a 100644 --- a/e2e/packages/contracts/types/ethers-contracts/factories/IWorld__factory.ts +++ b/e2e/packages/contracts/types/ethers-contracts/factories/IWorld__factory.ts @@ -137,6 +137,22 @@ const _abi = [ name: "StoreCore_InvalidFieldNamesLength", type: "error", }, + { + inputs: [ + { + internalType: "uint256", + name: "expected", + type: "uint256", + }, + { + internalType: "uint256", + name: "received", + type: "uint256", + }, + ], + name: "StoreCore_InvalidKeyNamesLength", + type: "error", + }, { inputs: [], name: "StoreCore_NotDynamicField", @@ -648,7 +664,7 @@ const _abi = [ type: "bytes32", }, ], - name: "getSchema", + name: "getValueSchema", outputs: [ { internalType: "Schema", @@ -1002,29 +1018,6 @@ const _abi = [ stateMutability: "nonpayable", type: "function", }, - { - inputs: [ - { - internalType: "bytes32", - name: "table", - type: "bytes32", - }, - { - internalType: "Schema", - name: "schema", - type: "bytes32", - }, - { - internalType: "Schema", - name: "keySchema", - type: "bytes32", - }, - ], - name: "registerSchema", - outputs: [], - stateMutability: "nonpayable", - type: "function", - }, { inputs: [ { @@ -1114,14 +1107,24 @@ const _abi = [ }, { internalType: "Schema", - name: "valueSchema", + name: "keySchema", type: "bytes32", }, { internalType: "Schema", - name: "keySchema", + name: "valueSchema", type: "bytes32", }, + { + internalType: "string[]", + name: "keyNames", + type: "string[]", + }, + { + internalType: "string[]", + name: "fieldNames", + type: "string[]", + }, ], name: "registerTable", outputs: [ @@ -1134,6 +1137,39 @@ const _abi = [ stateMutability: "nonpayable", type: "function", }, + { + inputs: [ + { + internalType: "bytes32", + name: "table", + type: "bytes32", + }, + { + internalType: "Schema", + name: "keySchema", + type: "bytes32", + }, + { + internalType: "Schema", + name: "valueSchema", + type: "bytes32", + }, + { + internalType: "string[]", + name: "keyNames", + type: "string[]", + }, + { + internalType: "string[]", + name: "fieldNames", + type: "string[]", + }, + ], + name: "registerTable", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, { inputs: [ { @@ -1264,57 +1300,6 @@ const _abi = [ stateMutability: "nonpayable", type: "function", }, - { - inputs: [ - { - internalType: "bytes16", - name: "namespace", - type: "bytes16", - }, - { - internalType: "bytes16", - name: "name", - type: "bytes16", - }, - { - internalType: "string", - name: "tableName", - type: "string", - }, - { - internalType: "string[]", - name: "fieldNames", - type: "string[]", - }, - ], - name: "setMetadata", - outputs: [], - stateMutability: "nonpayable", - type: "function", - }, - { - inputs: [ - { - internalType: "bytes32", - name: "table", - type: "bytes32", - }, - { - internalType: "string", - name: "tableName", - type: "string", - }, - { - internalType: "string[]", - name: "fieldNames", - type: "string[]", - }, - ], - name: "setMetadata", - outputs: [], - stateMutability: "nonpayable", - type: "function", - }, { inputs: [ { diff --git a/e2e/packages/contracts/worlds.json b/e2e/packages/contracts/worlds.json index a9f5c67245..7f54955b0d 100644 --- a/e2e/packages/contracts/worlds.json +++ b/e2e/packages/contracts/worlds.json @@ -1,5 +1,5 @@ { "31337": { - "address": "0xA7c59f010700930003b33aB25a7a0679C860f29c" + "address": "0xD84379CEae14AA33C123Af12424A37803F885889" } } \ No newline at end of file diff --git a/examples/minimal/packages/contracts/src/codegen/tables/CounterTable.sol b/examples/minimal/packages/contracts/src/codegen/tables/CounterTable.sol index e769476b8d..647bfc052f 100644 --- a/examples/minimal/packages/contracts/src/codegen/tables/CounterTable.sol +++ b/examples/minimal/packages/contracts/src/codegen/tables/CounterTable.sol @@ -21,54 +21,47 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Count bytes32 constant CounterTableTableId = _tableId; library CounterTable { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.UINT32; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](0); return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "value"; - return ("CounterTable", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](1); + _schema[0] = SchemaType.UINT32; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](0); } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "value"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, 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, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (uint32(Bytes.slice4(_blob, 0))); } @@ -76,7 +69,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, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (uint32(Bytes.slice4(_blob, 0))); } @@ -84,14 +77,14 @@ library CounterTable { function set(uint32 value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getValueSchema()); } /** 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)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getValueSchema()); } /** Tightly pack full data using this table's schema */ @@ -110,13 +103,13 @@ library CounterTable { function deleteRecord() internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* 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, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/examples/minimal/packages/contracts/src/codegen/tables/Inventory.sol b/examples/minimal/packages/contracts/src/codegen/tables/Inventory.sol index bf0398ea04..f3406f9e3c 100644 --- a/examples/minimal/packages/contracts/src/codegen/tables/Inventory.sol +++ b/examples/minimal/packages/contracts/src/codegen/tables/Inventory.sol @@ -21,14 +21,7 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Inven bytes32 constant InventoryTableId = _tableId; library Inventory { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.UINT32; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](3); _schema[0] = SchemaType.ADDRESS; @@ -38,33 +31,36 @@ library Inventory { return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "amount"; - return ("Inventory", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](1); + _schema[0] = SchemaType.UINT32; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](3); + keyNames[0] = "owner"; + keyNames[1] = "item"; + keyNames[2] = "itemVariant"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "amount"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get amount */ @@ -74,7 +70,7 @@ library Inventory { _keyTuple[1] = bytes32(uint256(item)); _keyTuple[2] = bytes32(uint256(itemVariant)); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (uint32(Bytes.slice4(_blob, 0))); } @@ -85,7 +81,7 @@ library Inventory { _keyTuple[1] = bytes32(uint256(item)); _keyTuple[2] = bytes32(uint256(itemVariant)); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (uint32(Bytes.slice4(_blob, 0))); } @@ -96,7 +92,7 @@ library Inventory { _keyTuple[1] = bytes32(uint256(item)); _keyTuple[2] = bytes32(uint256(itemVariant)); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((amount)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((amount)), getValueSchema()); } /** Set amount (using the specified store) */ @@ -106,7 +102,7 @@ library Inventory { _keyTuple[1] = bytes32(uint256(item)); _keyTuple[2] = bytes32(uint256(itemVariant)); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((amount)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((amount)), getValueSchema()); } /** Tightly pack full data using this table's schema */ @@ -131,7 +127,7 @@ library Inventory { _keyTuple[1] = bytes32(uint256(item)); _keyTuple[2] = bytes32(uint256(itemVariant)); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -141,6 +137,6 @@ library Inventory { _keyTuple[1] = bytes32(uint256(item)); _keyTuple[2] = bytes32(uint256(itemVariant)); - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/examples/minimal/packages/contracts/src/codegen/tables/MessageTable.sol b/examples/minimal/packages/contracts/src/codegen/tables/MessageTable.sol index 701e58677b..402068d07e 100644 --- a/examples/minimal/packages/contracts/src/codegen/tables/MessageTable.sol +++ b/examples/minimal/packages/contracts/src/codegen/tables/MessageTable.sol @@ -21,47 +21,40 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Messa bytes32 constant MessageTableTableId = _tableId; library MessageTable { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.STRING; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](0); return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "value"; - return ("MessageTable", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](1); + _schema[0] = SchemaType.STRING; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](0); } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "value"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Emit the ephemeral event using individual values */ @@ -70,7 +63,7 @@ library MessageTable { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.emitEphemeralRecord(_tableId, _keyTuple, _data, getSchema()); + StoreSwitch.emitEphemeralRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Emit the ephemeral event using individual values (using the specified store) */ @@ -79,7 +72,7 @@ library MessageTable { bytes32[] memory _keyTuple = new bytes32[](0); - _store.emitEphemeralRecord(_tableId, _keyTuple, _data, getSchema()); + _store.emitEphemeralRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Tightly pack full data using this table's schema */ diff --git a/examples/minimal/packages/contracts/src/systems/IncrementSystem.sol b/examples/minimal/packages/contracts/src/systems/IncrementSystem.sol index e7220486a3..e5f0d89c3d 100644 --- a/examples/minimal/packages/contracts/src/systems/IncrementSystem.sol +++ b/examples/minimal/packages/contracts/src/systems/IncrementSystem.sol @@ -16,7 +16,7 @@ contract IncrementSystem is System { function willRevert() public { // revert("I told you it would revert"); - CounterTable.registerSchema(); + CounterTable.register(); // revert MyCustomError(); } } diff --git a/examples/minimal/packages/contracts/types/ethers-contracts/IWorld.ts b/examples/minimal/packages/contracts/types/ethers-contracts/IWorld.ts index 75d740e54c..a66dde03e0 100644 --- a/examples/minimal/packages/contracts/types/ethers-contracts/IWorld.ts +++ b/examples/minimal/packages/contracts/types/ethers-contracts/IWorld.ts @@ -50,7 +50,7 @@ export interface IWorldInterface extends utils.Interface { "getFieldSlice(bytes32,bytes32[],uint8,bytes32,uint256,uint256)": FunctionFragment; "getKeySchema(bytes32)": FunctionFragment; "getRecord(bytes32,bytes32[],bytes32)": FunctionFragment; - "getSchema(bytes32)": FunctionFragment; + "getValueSchema(bytes32)": FunctionFragment; "grantAccess(bytes16,bytes16,address)": FunctionFragment; "increment()": FunctionFragment; "installModule(address,bytes)": FunctionFragment; @@ -64,18 +64,16 @@ export interface IWorldInterface extends utils.Interface { "registerHook(bytes16,bytes16,address)": FunctionFragment; "registerNamespace(bytes16)": FunctionFragment; "registerRootFunctionSelector(bytes16,bytes16,bytes4,bytes4)": FunctionFragment; - "registerSchema(bytes32,bytes32,bytes32)": FunctionFragment; "registerStoreHook(bytes32,address)": FunctionFragment; "registerSystem(bytes16,bytes16,address,bool)": FunctionFragment; "registerSystemHook(bytes16,bytes16,address)": FunctionFragment; - "registerTable(bytes16,bytes16,bytes32,bytes32)": FunctionFragment; + "registerTable(bytes16,bytes16,bytes32,bytes32,string[],string[])": FunctionFragment; + "registerTable(bytes32,bytes32,bytes32,string[],string[])": FunctionFragment; "registerTableHook(bytes16,bytes16,address)": FunctionFragment; "revokeAccess(bytes16,bytes16,address)": FunctionFragment; "sendMessage(string)": FunctionFragment; "setField(bytes16,bytes16,bytes32[],uint8,bytes,bytes32)": FunctionFragment; "setField(bytes32,bytes32[],uint8,bytes,bytes32)": FunctionFragment; - "setMetadata(bytes16,bytes16,string,string[])": FunctionFragment; - "setMetadata(bytes32,string,string[])": FunctionFragment; "setRecord(bytes32,bytes32[],bytes,bytes32)": FunctionFragment; "setRecord(bytes16,bytes16,bytes32[],bytes,bytes32)": FunctionFragment; "staticArrayBytesStruct(tuple[1])": FunctionFragment; @@ -99,7 +97,7 @@ export interface IWorldInterface extends utils.Interface { | "getFieldSlice" | "getKeySchema" | "getRecord" - | "getSchema" + | "getValueSchema" | "grantAccess" | "increment" | "installModule" @@ -113,18 +111,16 @@ export interface IWorldInterface extends utils.Interface { | "registerHook" | "registerNamespace" | "registerRootFunctionSelector" - | "registerSchema" | "registerStoreHook" | "registerSystem" | "registerSystemHook" - | "registerTable" + | "registerTable(bytes16,bytes16,bytes32,bytes32,string[],string[])" + | "registerTable(bytes32,bytes32,bytes32,string[],string[])" | "registerTableHook" | "revokeAccess" | "sendMessage" | "setField(bytes16,bytes16,bytes32[],uint8,bytes,bytes32)" | "setField(bytes32,bytes32[],uint8,bytes,bytes32)" - | "setMetadata(bytes16,bytes16,string,string[])" - | "setMetadata(bytes32,string,string[])" | "setRecord(bytes32,bytes32[],bytes,bytes32)" | "setRecord(bytes16,bytes16,bytes32[],bytes,bytes32)" | "staticArrayBytesStruct" @@ -228,7 +224,7 @@ export interface IWorldInterface extends utils.Interface { ] ): string; encodeFunctionData( - functionFragment: "getSchema", + functionFragment: "getValueSchema", values: [PromiseOrValue] ): string; encodeFunctionData( @@ -324,14 +320,6 @@ export interface IWorldInterface extends utils.Interface { PromiseOrValue ] ): string; - encodeFunctionData( - functionFragment: "registerSchema", - values: [ - PromiseOrValue, - PromiseOrValue, - PromiseOrValue - ] - ): string; encodeFunctionData( functionFragment: "registerStoreHook", values: [PromiseOrValue, PromiseOrValue] @@ -354,12 +342,24 @@ export interface IWorldInterface extends utils.Interface { ] ): string; encodeFunctionData( - functionFragment: "registerTable", + functionFragment: "registerTable(bytes16,bytes16,bytes32,bytes32,string[],string[])", values: [ PromiseOrValue, PromiseOrValue, PromiseOrValue, - PromiseOrValue + PromiseOrValue, + PromiseOrValue[], + PromiseOrValue[] + ] + ): string; + encodeFunctionData( + functionFragment: "registerTable(bytes32,bytes32,bytes32,string[],string[])", + values: [ + PromiseOrValue, + PromiseOrValue, + PromiseOrValue, + PromiseOrValue[], + PromiseOrValue[] ] ): string; encodeFunctionData( @@ -403,23 +403,6 @@ export interface IWorldInterface extends utils.Interface { PromiseOrValue ] ): string; - encodeFunctionData( - functionFragment: "setMetadata(bytes16,bytes16,string,string[])", - values: [ - PromiseOrValue, - PromiseOrValue, - PromiseOrValue, - PromiseOrValue[] - ] - ): string; - encodeFunctionData( - functionFragment: "setMetadata(bytes32,string,string[])", - values: [ - PromiseOrValue, - PromiseOrValue, - PromiseOrValue[] - ] - ): string; encodeFunctionData( functionFragment: "setRecord(bytes32,bytes32[],bytes,bytes32)", values: [ @@ -514,7 +497,10 @@ export interface IWorldInterface extends utils.Interface { data: BytesLike ): Result; decodeFunctionResult(functionFragment: "getRecord", data: BytesLike): Result; - decodeFunctionResult(functionFragment: "getSchema", data: BytesLike): Result; + decodeFunctionResult( + functionFragment: "getValueSchema", + data: BytesLike + ): Result; decodeFunctionResult( functionFragment: "grantAccess", data: BytesLike @@ -561,10 +547,6 @@ export interface IWorldInterface extends utils.Interface { functionFragment: "registerRootFunctionSelector", data: BytesLike ): Result; - decodeFunctionResult( - functionFragment: "registerSchema", - data: BytesLike - ): Result; decodeFunctionResult( functionFragment: "registerStoreHook", data: BytesLike @@ -578,7 +560,11 @@ export interface IWorldInterface extends utils.Interface { data: BytesLike ): Result; decodeFunctionResult( - functionFragment: "registerTable", + functionFragment: "registerTable(bytes16,bytes16,bytes32,bytes32,string[],string[])", + data: BytesLike + ): Result; + decodeFunctionResult( + functionFragment: "registerTable(bytes32,bytes32,bytes32,string[],string[])", data: BytesLike ): Result; decodeFunctionResult( @@ -601,14 +587,6 @@ export interface IWorldInterface extends utils.Interface { functionFragment: "setField(bytes32,bytes32[],uint8,bytes,bytes32)", data: BytesLike ): Result; - decodeFunctionResult( - functionFragment: "setMetadata(bytes16,bytes16,string,string[])", - data: BytesLike - ): Result; - decodeFunctionResult( - functionFragment: "setMetadata(bytes32,string,string[])", - data: BytesLike - ): Result; decodeFunctionResult( functionFragment: "setRecord(bytes32,bytes32[],bytes,bytes32)", data: BytesLike @@ -819,7 +797,7 @@ export interface IWorld extends BaseContract { overrides?: CallOverrides ): Promise<[string] & { data: string }>; - getSchema( + getValueSchema( table: PromiseOrValue, overrides?: CallOverrides ): Promise<[string] & { schema: string }>; @@ -919,13 +897,6 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - registerSchema( - table: PromiseOrValue, - schema: PromiseOrValue, - keySchema: PromiseOrValue, - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - registerStoreHook( table: PromiseOrValue, hook: PromiseOrValue, @@ -947,11 +918,22 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - registerTable( + "registerTable(bytes16,bytes16,bytes32,bytes32,string[],string[])"( namespace: PromiseOrValue, name: PromiseOrValue, + keySchema: PromiseOrValue, valueSchema: PromiseOrValue, + keyNames: PromiseOrValue[], + fieldNames: PromiseOrValue[], + overrides?: Overrides & { from?: PromiseOrValue } + ): Promise; + + "registerTable(bytes32,bytes32,bytes32,string[],string[])"( + table: PromiseOrValue, keySchema: PromiseOrValue, + valueSchema: PromiseOrValue, + keyNames: PromiseOrValue[], + fieldNames: PromiseOrValue[], overrides?: Overrides & { from?: PromiseOrValue } ): Promise; @@ -993,21 +975,6 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - "setMetadata(bytes16,bytes16,string,string[])"( - namespace: PromiseOrValue, - name: PromiseOrValue, - tableName: PromiseOrValue, - fieldNames: PromiseOrValue[], - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - - "setMetadata(bytes32,string,string[])"( - table: PromiseOrValue, - tableName: PromiseOrValue, - fieldNames: PromiseOrValue[], - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - "setRecord(bytes32,bytes32[],bytes,bytes32)"( table: PromiseOrValue, key: PromiseOrValue[], @@ -1148,7 +1115,7 @@ export interface IWorld extends BaseContract { overrides?: CallOverrides ): Promise; - getSchema( + getValueSchema( table: PromiseOrValue, overrides?: CallOverrides ): Promise; @@ -1248,13 +1215,6 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - registerSchema( - table: PromiseOrValue, - schema: PromiseOrValue, - keySchema: PromiseOrValue, - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - registerStoreHook( table: PromiseOrValue, hook: PromiseOrValue, @@ -1276,11 +1236,22 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - registerTable( + "registerTable(bytes16,bytes16,bytes32,bytes32,string[],string[])"( namespace: PromiseOrValue, name: PromiseOrValue, + keySchema: PromiseOrValue, valueSchema: PromiseOrValue, + keyNames: PromiseOrValue[], + fieldNames: PromiseOrValue[], + overrides?: Overrides & { from?: PromiseOrValue } + ): Promise; + + "registerTable(bytes32,bytes32,bytes32,string[],string[])"( + table: PromiseOrValue, keySchema: PromiseOrValue, + valueSchema: PromiseOrValue, + keyNames: PromiseOrValue[], + fieldNames: PromiseOrValue[], overrides?: Overrides & { from?: PromiseOrValue } ): Promise; @@ -1322,21 +1293,6 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - "setMetadata(bytes16,bytes16,string,string[])"( - namespace: PromiseOrValue, - name: PromiseOrValue, - tableName: PromiseOrValue, - fieldNames: PromiseOrValue[], - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - - "setMetadata(bytes32,string,string[])"( - table: PromiseOrValue, - tableName: PromiseOrValue, - fieldNames: PromiseOrValue[], - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - "setRecord(bytes32,bytes32[],bytes,bytes32)"( table: PromiseOrValue, key: PromiseOrValue[], @@ -1477,7 +1433,7 @@ export interface IWorld extends BaseContract { overrides?: CallOverrides ): Promise; - getSchema( + getValueSchema( table: PromiseOrValue, overrides?: CallOverrides ): Promise; @@ -1575,13 +1531,6 @@ export interface IWorld extends BaseContract { overrides?: CallOverrides ): Promise; - registerSchema( - table: PromiseOrValue, - schema: PromiseOrValue, - keySchema: PromiseOrValue, - overrides?: CallOverrides - ): Promise; - registerStoreHook( table: PromiseOrValue, hook: PromiseOrValue, @@ -1603,14 +1552,25 @@ export interface IWorld extends BaseContract { overrides?: CallOverrides ): Promise; - registerTable( + "registerTable(bytes16,bytes16,bytes32,bytes32,string[],string[])"( namespace: PromiseOrValue, name: PromiseOrValue, - valueSchema: PromiseOrValue, keySchema: PromiseOrValue, + valueSchema: PromiseOrValue, + keyNames: PromiseOrValue[], + fieldNames: PromiseOrValue[], overrides?: CallOverrides ): Promise; + "registerTable(bytes32,bytes32,bytes32,string[],string[])"( + table: PromiseOrValue, + keySchema: PromiseOrValue, + valueSchema: PromiseOrValue, + keyNames: PromiseOrValue[], + fieldNames: PromiseOrValue[], + overrides?: CallOverrides + ): Promise; + registerTableHook( namespace: PromiseOrValue, name: PromiseOrValue, @@ -1649,21 +1609,6 @@ export interface IWorld extends BaseContract { overrides?: CallOverrides ): Promise; - "setMetadata(bytes16,bytes16,string,string[])"( - namespace: PromiseOrValue, - name: PromiseOrValue, - tableName: PromiseOrValue, - fieldNames: PromiseOrValue[], - overrides?: CallOverrides - ): Promise; - - "setMetadata(bytes32,string,string[])"( - table: PromiseOrValue, - tableName: PromiseOrValue, - fieldNames: PromiseOrValue[], - overrides?: CallOverrides - ): Promise; - "setRecord(bytes32,bytes32[],bytes,bytes32)"( table: PromiseOrValue, key: PromiseOrValue[], @@ -1849,7 +1794,7 @@ export interface IWorld extends BaseContract { overrides?: CallOverrides ): Promise; - getSchema( + getValueSchema( table: PromiseOrValue, overrides?: CallOverrides ): Promise; @@ -1949,13 +1894,6 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - registerSchema( - table: PromiseOrValue, - schema: PromiseOrValue, - keySchema: PromiseOrValue, - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - registerStoreHook( table: PromiseOrValue, hook: PromiseOrValue, @@ -1977,11 +1915,22 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - registerTable( + "registerTable(bytes16,bytes16,bytes32,bytes32,string[],string[])"( namespace: PromiseOrValue, name: PromiseOrValue, + keySchema: PromiseOrValue, valueSchema: PromiseOrValue, + keyNames: PromiseOrValue[], + fieldNames: PromiseOrValue[], + overrides?: Overrides & { from?: PromiseOrValue } + ): Promise; + + "registerTable(bytes32,bytes32,bytes32,string[],string[])"( + table: PromiseOrValue, keySchema: PromiseOrValue, + valueSchema: PromiseOrValue, + keyNames: PromiseOrValue[], + fieldNames: PromiseOrValue[], overrides?: Overrides & { from?: PromiseOrValue } ): Promise; @@ -2023,21 +1972,6 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - "setMetadata(bytes16,bytes16,string,string[])"( - namespace: PromiseOrValue, - name: PromiseOrValue, - tableName: PromiseOrValue, - fieldNames: PromiseOrValue[], - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - - "setMetadata(bytes32,string,string[])"( - table: PromiseOrValue, - tableName: PromiseOrValue, - fieldNames: PromiseOrValue[], - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - "setRecord(bytes32,bytes32[],bytes,bytes32)"( table: PromiseOrValue, key: PromiseOrValue[], @@ -2179,7 +2113,7 @@ export interface IWorld extends BaseContract { overrides?: CallOverrides ): Promise; - getSchema( + getValueSchema( table: PromiseOrValue, overrides?: CallOverrides ): Promise; @@ -2279,13 +2213,6 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - registerSchema( - table: PromiseOrValue, - schema: PromiseOrValue, - keySchema: PromiseOrValue, - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - registerStoreHook( table: PromiseOrValue, hook: PromiseOrValue, @@ -2307,11 +2234,22 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - registerTable( + "registerTable(bytes16,bytes16,bytes32,bytes32,string[],string[])"( namespace: PromiseOrValue, name: PromiseOrValue, + keySchema: PromiseOrValue, valueSchema: PromiseOrValue, + keyNames: PromiseOrValue[], + fieldNames: PromiseOrValue[], + overrides?: Overrides & { from?: PromiseOrValue } + ): Promise; + + "registerTable(bytes32,bytes32,bytes32,string[],string[])"( + table: PromiseOrValue, keySchema: PromiseOrValue, + valueSchema: PromiseOrValue, + keyNames: PromiseOrValue[], + fieldNames: PromiseOrValue[], overrides?: Overrides & { from?: PromiseOrValue } ): Promise; @@ -2353,21 +2291,6 @@ export interface IWorld extends BaseContract { overrides?: Overrides & { from?: PromiseOrValue } ): Promise; - "setMetadata(bytes16,bytes16,string,string[])"( - namespace: PromiseOrValue, - name: PromiseOrValue, - tableName: PromiseOrValue, - fieldNames: PromiseOrValue[], - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - - "setMetadata(bytes32,string,string[])"( - table: PromiseOrValue, - tableName: PromiseOrValue, - fieldNames: PromiseOrValue[], - overrides?: Overrides & { from?: PromiseOrValue } - ): Promise; - "setRecord(bytes32,bytes32[],bytes,bytes32)"( table: PromiseOrValue, key: PromiseOrValue[], diff --git a/examples/minimal/packages/contracts/types/ethers-contracts/factories/IWorld__factory.ts b/examples/minimal/packages/contracts/types/ethers-contracts/factories/IWorld__factory.ts index 202cb11229..41d884b448 100644 --- a/examples/minimal/packages/contracts/types/ethers-contracts/factories/IWorld__factory.ts +++ b/examples/minimal/packages/contracts/types/ethers-contracts/factories/IWorld__factory.ts @@ -142,6 +142,22 @@ const _abi = [ name: "StoreCore_InvalidFieldNamesLength", type: "error", }, + { + inputs: [ + { + internalType: "uint256", + name: "expected", + type: "uint256", + }, + { + internalType: "uint256", + name: "received", + type: "uint256", + }, + ], + name: "StoreCore_InvalidKeyNamesLength", + type: "error", + }, { inputs: [], name: "StoreCore_NotDynamicField", @@ -650,7 +666,7 @@ const _abi = [ type: "bytes32", }, ], - name: "getSchema", + name: "getValueSchema", outputs: [ { internalType: "Schema", @@ -997,29 +1013,6 @@ const _abi = [ stateMutability: "nonpayable", type: "function", }, - { - inputs: [ - { - internalType: "bytes32", - name: "table", - type: "bytes32", - }, - { - internalType: "Schema", - name: "schema", - type: "bytes32", - }, - { - internalType: "Schema", - name: "keySchema", - type: "bytes32", - }, - ], - name: "registerSchema", - outputs: [], - stateMutability: "nonpayable", - type: "function", - }, { inputs: [ { @@ -1109,14 +1102,24 @@ const _abi = [ }, { internalType: "Schema", - name: "valueSchema", + name: "keySchema", type: "bytes32", }, { internalType: "Schema", - name: "keySchema", + name: "valueSchema", type: "bytes32", }, + { + internalType: "string[]", + name: "keyNames", + type: "string[]", + }, + { + internalType: "string[]", + name: "fieldNames", + type: "string[]", + }, ], name: "registerTable", outputs: [ @@ -1129,6 +1132,39 @@ const _abi = [ stateMutability: "nonpayable", type: "function", }, + { + inputs: [ + { + internalType: "bytes32", + name: "table", + type: "bytes32", + }, + { + internalType: "Schema", + name: "keySchema", + type: "bytes32", + }, + { + internalType: "Schema", + name: "valueSchema", + type: "bytes32", + }, + { + internalType: "string[]", + name: "keyNames", + type: "string[]", + }, + { + internalType: "string[]", + name: "fieldNames", + type: "string[]", + }, + ], + name: "registerTable", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, { inputs: [ { @@ -1259,57 +1295,6 @@ const _abi = [ stateMutability: "nonpayable", type: "function", }, - { - inputs: [ - { - internalType: "bytes16", - name: "namespace", - type: "bytes16", - }, - { - internalType: "bytes16", - name: "name", - type: "bytes16", - }, - { - internalType: "string", - name: "tableName", - type: "string", - }, - { - internalType: "string[]", - name: "fieldNames", - type: "string[]", - }, - ], - name: "setMetadata", - outputs: [], - stateMutability: "nonpayable", - type: "function", - }, - { - inputs: [ - { - internalType: "bytes32", - name: "table", - type: "bytes32", - }, - { - internalType: "string", - name: "tableName", - type: "string", - }, - { - internalType: "string[]", - name: "fieldNames", - type: "string[]", - }, - ], - name: "setMetadata", - outputs: [], - stateMutability: "nonpayable", - type: "function", - }, { inputs: [ { diff --git a/packages/cli/contracts/src/codegen/tables/Dynamics1.sol b/packages/cli/contracts/src/codegen/tables/Dynamics1.sol index 7ad4103b8f..cc7288b5a7 100644 --- a/packages/cli/contracts/src/codegen/tables/Dynamics1.sol +++ b/packages/cli/contracts/src/codegen/tables/Dynamics1.sol @@ -29,8 +29,16 @@ struct Dynamics1Data { } library Dynamics1 { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { + /** Get the table's key schema */ + function getKeySchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](1); + _schema[0] = SchemaType.BYTES32; + + return SchemaLib.encode(_schema); + } + + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](5); _schema[0] = SchemaType.BYTES32_ARRAY; _schema[1] = SchemaType.INT32_ARRAY; @@ -41,44 +49,30 @@ library Dynamics1 { return SchemaLib.encode(_schema); } - function getKeySchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BYTES32; - - return SchemaLib.encode(_schema); - } - - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](5); - _fieldNames[0] = "staticB32"; - _fieldNames[1] = "staticI32"; - _fieldNames[2] = "staticU128"; - _fieldNames[3] = "staticAddrs"; - _fieldNames[4] = "staticBools"; - return ("Dynamics1", _fieldNames); - } - - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "key"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](5); + fieldNames[0] = "staticB32"; + fieldNames[1] = "staticI32"; + fieldNames[2] = "staticU128"; + fieldNames[3] = "staticAddrs"; + fieldNames[4] = "staticBools"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get staticB32 */ @@ -86,7 +80,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return toStaticArray_bytes32_1(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -95,7 +89,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return toStaticArray_bytes32_1(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -104,7 +98,13 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode(fromStaticArray_bytes32_1(staticB32)), getSchema()); + StoreSwitch.setField( + _tableId, + _keyTuple, + 0, + EncodeArray.encode(fromStaticArray_bytes32_1(staticB32)), + getValueSchema() + ); } /** Set staticB32 (using the specified store) */ @@ -112,7 +112,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode(fromStaticArray_bytes32_1(staticB32)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode(fromStaticArray_bytes32_1(staticB32)), getValueSchema()); } /** Get the length of staticB32 */ @@ -120,7 +120,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); unchecked { return _byteLength / 32; } @@ -131,7 +131,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); unchecked { return _byteLength / 32; } @@ -150,7 +150,7 @@ library Dynamics1 { _tableId, _keyTuple, 0, - getSchema(), + getValueSchema(), _index * 32, (_index + 1) * 32 ); @@ -167,7 +167,14 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 32, (_index + 1) * 32); + bytes memory _blob = _store.getFieldSlice( + _tableId, + _keyTuple, + 0, + getValueSchema(), + _index * 32, + (_index + 1) * 32 + ); return (Bytes.slice32(_blob, 0)); } } @@ -177,7 +184,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to staticB32 (using the specified store) */ @@ -185,7 +192,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from staticB32 */ @@ -193,7 +200,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 32, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 0, 32, getValueSchema()); } /** Pop an element from staticB32 (using the specified store) */ @@ -201,7 +208,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 0, 32, getSchema()); + _store.popFromField(_tableId, _keyTuple, 0, 32, getValueSchema()); } /** @@ -213,7 +220,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getValueSchema()); } } @@ -226,7 +233,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getValueSchema()); } } @@ -235,7 +242,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getValueSchema()); return toStaticArray_int32_2(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_int32()); } @@ -244,7 +251,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getValueSchema()); return toStaticArray_int32_2(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_int32()); } @@ -253,7 +260,13 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 1, EncodeArray.encode(fromStaticArray_int32_2(staticI32)), getSchema()); + StoreSwitch.setField( + _tableId, + _keyTuple, + 1, + EncodeArray.encode(fromStaticArray_int32_2(staticI32)), + getValueSchema() + ); } /** Set staticI32 (using the specified store) */ @@ -261,7 +274,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 1, EncodeArray.encode(fromStaticArray_int32_2(staticI32)), getSchema()); + _store.setField(_tableId, _keyTuple, 1, EncodeArray.encode(fromStaticArray_int32_2(staticI32)), getValueSchema()); } /** Get the length of staticI32 */ @@ -269,7 +282,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 1, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 1, getValueSchema()); unchecked { return _byteLength / 4; } @@ -280,7 +293,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 1, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 1, getValueSchema()); unchecked { return _byteLength / 4; } @@ -295,7 +308,14 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 1, getSchema(), _index * 4, (_index + 1) * 4); + bytes memory _blob = StoreSwitch.getFieldSlice( + _tableId, + _keyTuple, + 1, + getValueSchema(), + _index * 4, + (_index + 1) * 4 + ); return (int32(uint32(Bytes.slice4(_blob, 0)))); } } @@ -309,7 +329,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 1, getSchema(), _index * 4, (_index + 1) * 4); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 1, getValueSchema(), _index * 4, (_index + 1) * 4); return (int32(uint32(Bytes.slice4(_blob, 0)))); } } @@ -319,7 +339,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to staticI32 (using the specified store) */ @@ -327,7 +347,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from staticI32 */ @@ -335,7 +355,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 1, 4, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 1, 4, getValueSchema()); } /** Pop an element from staticI32 (using the specified store) */ @@ -343,7 +363,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 1, 4, getSchema()); + _store.popFromField(_tableId, _keyTuple, 1, 4, getValueSchema()); } /** @@ -355,7 +375,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 1, _index * 4, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 1, _index * 4, abi.encodePacked((_element)), getValueSchema()); } } @@ -368,7 +388,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 1, _index * 4, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 1, _index * 4, abi.encodePacked((_element)), getValueSchema()); } } @@ -377,7 +397,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getValueSchema()); return toStaticArray_uint128_3(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint128()); } @@ -386,7 +406,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getValueSchema()); return toStaticArray_uint128_3(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint128()); } @@ -400,7 +420,7 @@ library Dynamics1 { _keyTuple, 2, EncodeArray.encode(fromStaticArray_uint128_3(staticU128)), - getSchema() + getValueSchema() ); } @@ -409,7 +429,13 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 2, EncodeArray.encode(fromStaticArray_uint128_3(staticU128)), getSchema()); + _store.setField( + _tableId, + _keyTuple, + 2, + EncodeArray.encode(fromStaticArray_uint128_3(staticU128)), + getValueSchema() + ); } /** Get the length of staticU128 */ @@ -417,7 +443,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 2, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 2, getValueSchema()); unchecked { return _byteLength / 16; } @@ -428,7 +454,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 2, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 2, getValueSchema()); unchecked { return _byteLength / 16; } @@ -447,7 +473,7 @@ library Dynamics1 { _tableId, _keyTuple, 2, - getSchema(), + getValueSchema(), _index * 16, (_index + 1) * 16 ); @@ -464,7 +490,14 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 2, getSchema(), _index * 16, (_index + 1) * 16); + bytes memory _blob = _store.getFieldSlice( + _tableId, + _keyTuple, + 2, + getValueSchema(), + _index * 16, + (_index + 1) * 16 + ); return (uint128(Bytes.slice16(_blob, 0))); } } @@ -474,7 +507,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to staticU128 (using the specified store) */ @@ -482,7 +515,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from staticU128 */ @@ -490,7 +523,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 2, 16, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 2, 16, getValueSchema()); } /** Pop an element from staticU128 (using the specified store) */ @@ -498,7 +531,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 2, 16, getSchema()); + _store.popFromField(_tableId, _keyTuple, 2, 16, getValueSchema()); } /** @@ -510,7 +543,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 2, _index * 16, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 2, _index * 16, abi.encodePacked((_element)), getValueSchema()); } } @@ -523,7 +556,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 2, _index * 16, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 2, _index * 16, abi.encodePacked((_element)), getValueSchema()); } } @@ -532,7 +565,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, getValueSchema()); return toStaticArray_address_4(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_address()); } @@ -541,7 +574,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, getValueSchema()); return toStaticArray_address_4(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_address()); } @@ -555,7 +588,7 @@ library Dynamics1 { _keyTuple, 3, EncodeArray.encode(fromStaticArray_address_4(staticAddrs)), - getSchema() + getValueSchema() ); } @@ -564,7 +597,13 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 3, EncodeArray.encode(fromStaticArray_address_4(staticAddrs)), getSchema()); + _store.setField( + _tableId, + _keyTuple, + 3, + EncodeArray.encode(fromStaticArray_address_4(staticAddrs)), + getValueSchema() + ); } /** Get the length of staticAddrs */ @@ -572,7 +611,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 3, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 3, getValueSchema()); unchecked { return _byteLength / 20; } @@ -583,7 +622,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 3, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 3, getValueSchema()); unchecked { return _byteLength / 20; } @@ -602,7 +641,7 @@ library Dynamics1 { _tableId, _keyTuple, 3, - getSchema(), + getValueSchema(), _index * 20, (_index + 1) * 20 ); @@ -619,7 +658,14 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 3, getSchema(), _index * 20, (_index + 1) * 20); + bytes memory _blob = _store.getFieldSlice( + _tableId, + _keyTuple, + 3, + getValueSchema(), + _index * 20, + (_index + 1) * 20 + ); return (address(Bytes.slice20(_blob, 0))); } } @@ -629,7 +675,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to staticAddrs (using the specified store) */ @@ -637,7 +683,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from staticAddrs */ @@ -645,7 +691,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 3, 20, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 3, 20, getValueSchema()); } /** Pop an element from staticAddrs (using the specified store) */ @@ -653,7 +699,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 3, 20, getSchema()); + _store.popFromField(_tableId, _keyTuple, 3, 20, getValueSchema()); } /** @@ -665,7 +711,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 3, _index * 20, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 3, _index * 20, abi.encodePacked((_element)), getValueSchema()); } } @@ -678,7 +724,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 3, _index * 20, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 3, _index * 20, abi.encodePacked((_element)), getValueSchema()); } } @@ -687,7 +733,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 4, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 4, getValueSchema()); return toStaticArray_bool_5(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bool()); } @@ -696,7 +742,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 4, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 4, getValueSchema()); return toStaticArray_bool_5(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bool()); } @@ -705,7 +751,13 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 4, EncodeArray.encode(fromStaticArray_bool_5(staticBools)), getSchema()); + StoreSwitch.setField( + _tableId, + _keyTuple, + 4, + EncodeArray.encode(fromStaticArray_bool_5(staticBools)), + getValueSchema() + ); } /** Set staticBools (using the specified store) */ @@ -713,7 +765,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 4, EncodeArray.encode(fromStaticArray_bool_5(staticBools)), getSchema()); + _store.setField(_tableId, _keyTuple, 4, EncodeArray.encode(fromStaticArray_bool_5(staticBools)), getValueSchema()); } /** Get the length of staticBools */ @@ -721,7 +773,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 4, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 4, getValueSchema()); unchecked { return _byteLength / 1; } @@ -732,7 +784,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 4, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 4, getValueSchema()); unchecked { return _byteLength / 1; } @@ -747,7 +799,14 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 4, getSchema(), _index * 1, (_index + 1) * 1); + bytes memory _blob = StoreSwitch.getFieldSlice( + _tableId, + _keyTuple, + 4, + getValueSchema(), + _index * 1, + (_index + 1) * 1 + ); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } } @@ -761,7 +820,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 4, getSchema(), _index * 1, (_index + 1) * 1); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 4, getValueSchema(), _index * 1, (_index + 1) * 1); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } } @@ -771,7 +830,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to staticBools (using the specified store) */ @@ -779,7 +838,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from staticBools */ @@ -787,7 +846,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 4, 1, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 4, 1, getValueSchema()); } /** Pop an element from staticBools (using the specified store) */ @@ -795,7 +854,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 4, 1, getSchema()); + _store.popFromField(_tableId, _keyTuple, 4, 1, getValueSchema()); } /** @@ -807,7 +866,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 4, _index * 1, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 4, _index * 1, abi.encodePacked((_element)), getValueSchema()); } } @@ -820,7 +879,7 @@ library Dynamics1 { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 4, _index * 1, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 4, _index * 1, abi.encodePacked((_element)), getValueSchema()); } } @@ -829,7 +888,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -838,7 +897,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -856,7 +915,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setRecord(_tableId, _keyTuple, _data, getSchema()); + StoreSwitch.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using individual values (using the specified store) */ @@ -874,7 +933,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setRecord(_tableId, _keyTuple, _data, getSchema()); + _store.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using the data struct */ @@ -971,7 +1030,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -979,7 +1038,7 @@ library Dynamics1 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/cli/contracts/src/codegen/tables/Dynamics2.sol b/packages/cli/contracts/src/codegen/tables/Dynamics2.sol index 598db942c5..2a131c165a 100644 --- a/packages/cli/contracts/src/codegen/tables/Dynamics2.sol +++ b/packages/cli/contracts/src/codegen/tables/Dynamics2.sol @@ -27,16 +27,7 @@ struct Dynamics2Data { } library Dynamics2 { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](3); - _schema[0] = SchemaType.UINT64_ARRAY; - _schema[1] = SchemaType.STRING; - _schema[2] = SchemaType.BYTES; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); _schema[0] = SchemaType.BYTES32; @@ -44,35 +35,38 @@ library Dynamics2 { return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](3); - _fieldNames[0] = "u64"; - _fieldNames[1] = "str"; - _fieldNames[2] = "b"; - return ("Dynamics2", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](3); + _schema[0] = SchemaType.UINT64_ARRAY; + _schema[1] = SchemaType.STRING; + _schema[2] = SchemaType.BYTES; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "key"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](3); + fieldNames[0] = "u64"; + fieldNames[1] = "str"; + fieldNames[2] = "b"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get u64 */ @@ -80,7 +74,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint64()); } @@ -89,7 +83,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint64()); } @@ -98,7 +92,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((u64)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((u64)), getValueSchema()); } /** Set u64 (using the specified store) */ @@ -106,7 +100,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((u64)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((u64)), getValueSchema()); } /** Get the length of u64 */ @@ -114,7 +108,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); unchecked { return _byteLength / 8; } @@ -125,7 +119,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); unchecked { return _byteLength / 8; } @@ -140,7 +134,14 @@ library Dynamics2 { _keyTuple[0] = key; unchecked { - bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 8, (_index + 1) * 8); + bytes memory _blob = StoreSwitch.getFieldSlice( + _tableId, + _keyTuple, + 0, + getValueSchema(), + _index * 8, + (_index + 1) * 8 + ); return (uint64(Bytes.slice8(_blob, 0))); } } @@ -154,7 +155,7 @@ library Dynamics2 { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 8, (_index + 1) * 8); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getValueSchema(), _index * 8, (_index + 1) * 8); return (uint64(Bytes.slice8(_blob, 0))); } } @@ -164,7 +165,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to u64 (using the specified store) */ @@ -172,7 +173,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from u64 */ @@ -180,7 +181,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 8, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 0, 8, getValueSchema()); } /** Pop an element from u64 (using the specified store) */ @@ -188,7 +189,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 0, 8, getSchema()); + _store.popFromField(_tableId, _keyTuple, 0, 8, getValueSchema()); } /** @@ -200,7 +201,7 @@ library Dynamics2 { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 8, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 8, abi.encodePacked((_element)), getValueSchema()); } } @@ -213,7 +214,7 @@ library Dynamics2 { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 0, _index * 8, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 0, _index * 8, abi.encodePacked((_element)), getValueSchema()); } } @@ -222,7 +223,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getValueSchema()); return (string(_blob)); } @@ -231,7 +232,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getValueSchema()); return (string(_blob)); } @@ -240,7 +241,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 1, bytes((str)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 1, bytes((str)), getValueSchema()); } /** Set str (using the specified store) */ @@ -248,7 +249,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 1, bytes((str)), getSchema()); + _store.setField(_tableId, _keyTuple, 1, bytes((str)), getValueSchema()); } /** Get the length of str */ @@ -256,7 +257,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 1, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 1, getValueSchema()); unchecked { return _byteLength / 1; } @@ -267,7 +268,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 1, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 1, getValueSchema()); unchecked { return _byteLength / 1; } @@ -282,7 +283,14 @@ library Dynamics2 { _keyTuple[0] = key; unchecked { - bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 1, getSchema(), _index * 1, (_index + 1) * 1); + bytes memory _blob = StoreSwitch.getFieldSlice( + _tableId, + _keyTuple, + 1, + getValueSchema(), + _index * 1, + (_index + 1) * 1 + ); return (string(_blob)); } } @@ -296,7 +304,7 @@ library Dynamics2 { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 1, getSchema(), _index * 1, (_index + 1) * 1); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 1, getValueSchema(), _index * 1, (_index + 1) * 1); return (string(_blob)); } } @@ -306,7 +314,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 1, bytes((_slice)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 1, bytes((_slice)), getValueSchema()); } /** Push a slice to str (using the specified store) */ @@ -314,7 +322,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 1, bytes((_slice)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 1, bytes((_slice)), getValueSchema()); } /** Pop a slice from str */ @@ -322,7 +330,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 1, 1, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 1, 1, getValueSchema()); } /** Pop a slice from str (using the specified store) */ @@ -330,7 +338,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 1, 1, getSchema()); + _store.popFromField(_tableId, _keyTuple, 1, 1, getValueSchema()); } /** @@ -342,7 +350,7 @@ library Dynamics2 { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 1, _index * 1, bytes((_slice)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 1, _index * 1, bytes((_slice)), getValueSchema()); } } @@ -355,7 +363,7 @@ library Dynamics2 { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 1, _index * 1, bytes((_slice)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 1, _index * 1, bytes((_slice)), getValueSchema()); } } @@ -364,7 +372,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getValueSchema()); return (bytes(_blob)); } @@ -373,7 +381,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getValueSchema()); return (bytes(_blob)); } @@ -382,7 +390,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 2, bytes((b)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 2, bytes((b)), getValueSchema()); } /** Set b (using the specified store) */ @@ -390,7 +398,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 2, bytes((b)), getSchema()); + _store.setField(_tableId, _keyTuple, 2, bytes((b)), getValueSchema()); } /** Get the length of b */ @@ -398,7 +406,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 2, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 2, getValueSchema()); unchecked { return _byteLength / 1; } @@ -409,7 +417,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 2, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 2, getValueSchema()); unchecked { return _byteLength / 1; } @@ -424,7 +432,14 @@ library Dynamics2 { _keyTuple[0] = key; unchecked { - bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 2, getSchema(), _index * 1, (_index + 1) * 1); + bytes memory _blob = StoreSwitch.getFieldSlice( + _tableId, + _keyTuple, + 2, + getValueSchema(), + _index * 1, + (_index + 1) * 1 + ); return (bytes(_blob)); } } @@ -438,7 +453,7 @@ library Dynamics2 { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 2, getSchema(), _index * 1, (_index + 1) * 1); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 2, getValueSchema(), _index * 1, (_index + 1) * 1); return (bytes(_blob)); } } @@ -448,7 +463,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 2, bytes((_slice)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 2, bytes((_slice)), getValueSchema()); } /** Push a slice to b (using the specified store) */ @@ -456,7 +471,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 2, bytes((_slice)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 2, bytes((_slice)), getValueSchema()); } /** Pop a slice from b */ @@ -464,7 +479,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 2, 1, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 2, 1, getValueSchema()); } /** Pop a slice from b (using the specified store) */ @@ -472,7 +487,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 2, 1, getSchema()); + _store.popFromField(_tableId, _keyTuple, 2, 1, getValueSchema()); } /** @@ -484,7 +499,7 @@ library Dynamics2 { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 2, _index * 1, bytes((_slice)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 2, _index * 1, bytes((_slice)), getValueSchema()); } } @@ -497,7 +512,7 @@ library Dynamics2 { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 2, _index * 1, bytes((_slice)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 2, _index * 1, bytes((_slice)), getValueSchema()); } } @@ -506,7 +521,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -515,7 +530,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -526,7 +541,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setRecord(_tableId, _keyTuple, _data, getSchema()); + StoreSwitch.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using individual values (using the specified store) */ @@ -536,7 +551,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setRecord(_tableId, _keyTuple, _data, getSchema()); + _store.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using the data struct */ @@ -605,7 +620,7 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -613,6 +628,6 @@ library Dynamics2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/cli/contracts/src/codegen/tables/Ephemeral.sol b/packages/cli/contracts/src/codegen/tables/Ephemeral.sol index 380302a1dc..16f69fc94b 100644 --- a/packages/cli/contracts/src/codegen/tables/Ephemeral.sol +++ b/packages/cli/contracts/src/codegen/tables/Ephemeral.sol @@ -21,48 +21,42 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Ephem bytes32 constant EphemeralTableId = _tableId; library Ephemeral { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { + /** Get the table's key schema */ + function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.UINT256; + _schema[0] = SchemaType.BYTES32; return SchemaLib.encode(_schema); } - function getKeySchema() internal pure returns (Schema) { + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BYTES32; + _schema[0] = SchemaType.UINT256; return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "value"; - return ("Ephemeral", _fieldNames); - } - - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "key"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "value"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Emit the ephemeral event using individual values */ @@ -72,7 +66,7 @@ library Ephemeral { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.emitEphemeralRecord(_tableId, _keyTuple, _data, getSchema()); + StoreSwitch.emitEphemeralRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Emit the ephemeral event using individual values (using the specified store) */ @@ -82,7 +76,7 @@ library Ephemeral { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.emitEphemeralRecord(_tableId, _keyTuple, _data, getSchema()); + _store.emitEphemeralRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Tightly pack full 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 cdaa73c153..009f0ce7ee 100644 --- a/packages/cli/contracts/src/codegen/tables/Singleton.sol +++ b/packages/cli/contracts/src/codegen/tables/Singleton.sol @@ -21,8 +21,15 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Singl bytes32 constant SingletonTableId = _tableId; library Singleton { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { + /** Get the table's key schema */ + function getKeySchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](0); + + return SchemaLib.encode(_schema); + } + + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](4); _schema[0] = SchemaType.INT256; _schema[1] = SchemaType.UINT32_ARRAY; @@ -32,49 +39,35 @@ library Singleton { return SchemaLib.encode(_schema); } - function getKeySchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](0); - - return SchemaLib.encode(_schema); - } - - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](4); - _fieldNames[0] = "v1"; - _fieldNames[1] = "v2"; - _fieldNames[2] = "v3"; - _fieldNames[3] = "v4"; - return ("Singleton", _fieldNames); - } - - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](0); } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](4); + fieldNames[0] = "v1"; + fieldNames[1] = "v2"; + fieldNames[2] = "v3"; + fieldNames[3] = "v4"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, 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, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (int256(uint256(Bytes.slice32(_blob, 0)))); } @@ -82,7 +75,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, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (int256(uint256(Bytes.slice32(_blob, 0)))); } @@ -90,21 +83,21 @@ library Singleton { function setV1(int256 v1) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((v1)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((v1)), getValueSchema()); } /** 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)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((v1)), getValueSchema()); } /** 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, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getValueSchema()); return toStaticArray_uint32_2(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } @@ -112,7 +105,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, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getValueSchema()); return toStaticArray_uint32_2(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } @@ -120,21 +113,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)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 1, EncodeArray.encode(fromStaticArray_uint32_2(v2)), getValueSchema()); } /** 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)), getSchema()); + _store.setField(_tableId, _keyTuple, 1, EncodeArray.encode(fromStaticArray_uint32_2(v2)), getValueSchema()); } /** Get the length of v2 */ function lengthV2() internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](0); - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 1, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 1, getValueSchema()); unchecked { return _byteLength / 4; } @@ -144,7 +137,7 @@ library Singleton { function lengthV2(IStore _store) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](0); - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 1, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 1, getValueSchema()); unchecked { return _byteLength / 4; } @@ -158,7 +151,14 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 1, getSchema(), _index * 4, (_index + 1) * 4); + bytes memory _blob = StoreSwitch.getFieldSlice( + _tableId, + _keyTuple, + 1, + getValueSchema(), + _index * 4, + (_index + 1) * 4 + ); return (uint32(Bytes.slice4(_blob, 0))); } } @@ -171,7 +171,7 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 1, getSchema(), _index * 4, (_index + 1) * 4); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 1, getValueSchema(), _index * 4, (_index + 1) * 4); return (uint32(Bytes.slice4(_blob, 0))); } } @@ -180,28 +180,28 @@ library Singleton { function pushV2(uint32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), getValueSchema()); } /** 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)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from v2 */ function popV2() internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.popFromField(_tableId, _keyTuple, 1, 4, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 1, 4, getValueSchema()); } /** 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, getSchema()); + _store.popFromField(_tableId, _keyTuple, 1, 4, getValueSchema()); } /** @@ -212,7 +212,7 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 1, _index * 4, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 1, _index * 4, abi.encodePacked((_element)), getValueSchema()); } } @@ -224,7 +224,7 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - _store.updateInField(_tableId, _keyTuple, 1, _index * 4, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 1, _index * 4, abi.encodePacked((_element)), getValueSchema()); } } @@ -232,7 +232,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, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getValueSchema()); return toStaticArray_uint32_2(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } @@ -240,7 +240,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, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getValueSchema()); return toStaticArray_uint32_2(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } @@ -248,21 +248,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)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 2, EncodeArray.encode(fromStaticArray_uint32_2(v3)), getValueSchema()); } /** 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)), getSchema()); + _store.setField(_tableId, _keyTuple, 2, EncodeArray.encode(fromStaticArray_uint32_2(v3)), getValueSchema()); } /** Get the length of v3 */ function lengthV3() internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](0); - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 2, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 2, getValueSchema()); unchecked { return _byteLength / 4; } @@ -272,7 +272,7 @@ library Singleton { function lengthV3(IStore _store) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](0); - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 2, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 2, getValueSchema()); unchecked { return _byteLength / 4; } @@ -286,7 +286,14 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 2, getSchema(), _index * 4, (_index + 1) * 4); + bytes memory _blob = StoreSwitch.getFieldSlice( + _tableId, + _keyTuple, + 2, + getValueSchema(), + _index * 4, + (_index + 1) * 4 + ); return (uint32(Bytes.slice4(_blob, 0))); } } @@ -299,7 +306,7 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 2, getSchema(), _index * 4, (_index + 1) * 4); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 2, getValueSchema(), _index * 4, (_index + 1) * 4); return (uint32(Bytes.slice4(_blob, 0))); } } @@ -308,28 +315,28 @@ library Singleton { function pushV3(uint32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getValueSchema()); } /** 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)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from v3 */ function popV3() internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.popFromField(_tableId, _keyTuple, 2, 4, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 2, 4, getValueSchema()); } /** 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, getSchema()); + _store.popFromField(_tableId, _keyTuple, 2, 4, getValueSchema()); } /** @@ -340,7 +347,7 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 2, _index * 4, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 2, _index * 4, abi.encodePacked((_element)), getValueSchema()); } } @@ -352,7 +359,7 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - _store.updateInField(_tableId, _keyTuple, 2, _index * 4, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 2, _index * 4, abi.encodePacked((_element)), getValueSchema()); } } @@ -360,7 +367,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, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, getValueSchema()); return toStaticArray_uint32_1(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } @@ -368,7 +375,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, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, getValueSchema()); return toStaticArray_uint32_1(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } @@ -376,21 +383,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)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 3, EncodeArray.encode(fromStaticArray_uint32_1(v4)), getValueSchema()); } /** 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)), getSchema()); + _store.setField(_tableId, _keyTuple, 3, EncodeArray.encode(fromStaticArray_uint32_1(v4)), getValueSchema()); } /** Get the length of v4 */ function lengthV4() internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](0); - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 3, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 3, getValueSchema()); unchecked { return _byteLength / 4; } @@ -400,7 +407,7 @@ library Singleton { function lengthV4(IStore _store) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](0); - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 3, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 3, getValueSchema()); unchecked { return _byteLength / 4; } @@ -414,7 +421,14 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 3, getSchema(), _index * 4, (_index + 1) * 4); + bytes memory _blob = StoreSwitch.getFieldSlice( + _tableId, + _keyTuple, + 3, + getValueSchema(), + _index * 4, + (_index + 1) * 4 + ); return (uint32(Bytes.slice4(_blob, 0))); } } @@ -427,7 +441,7 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 3, getSchema(), _index * 4, (_index + 1) * 4); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 3, getValueSchema(), _index * 4, (_index + 1) * 4); return (uint32(Bytes.slice4(_blob, 0))); } } @@ -436,28 +450,28 @@ library Singleton { function pushV4(uint32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), getValueSchema()); } /** 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)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from v4 */ function popV4() internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.popFromField(_tableId, _keyTuple, 3, 4, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 3, 4, getValueSchema()); } /** 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, getSchema()); + _store.popFromField(_tableId, _keyTuple, 3, 4, getValueSchema()); } /** @@ -468,7 +482,7 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 3, _index * 4, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 3, _index * 4, abi.encodePacked((_element)), getValueSchema()); } } @@ -480,7 +494,7 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); unchecked { - _store.updateInField(_tableId, _keyTuple, 3, _index * 4, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 3, _index * 4, abi.encodePacked((_element)), getValueSchema()); } } @@ -488,7 +502,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, getSchema()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -498,7 +512,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, getSchema()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -508,7 +522,7 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setRecord(_tableId, _keyTuple, _data, getSchema()); + StoreSwitch.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using individual values (using the specified store) */ @@ -517,7 +531,7 @@ library Singleton { bytes32[] memory _keyTuple = new bytes32[](0); - _store.setRecord(_tableId, _keyTuple, _data, getSchema()); + _store.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** @@ -590,14 +604,14 @@ library Singleton { function deleteRecord() internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* 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, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/cli/contracts/src/codegen/tables/Statics.sol b/packages/cli/contracts/src/codegen/tables/Statics.sol index ffd1d1cdc2..c4356780c3 100644 --- a/packages/cli/contracts/src/codegen/tables/Statics.sol +++ b/packages/cli/contracts/src/codegen/tables/Statics.sol @@ -34,8 +34,8 @@ struct StaticsData { } library Statics { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { + /** Get the table's key schema */ + function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](7); _schema[0] = SchemaType.UINT256; _schema[1] = SchemaType.INT32; @@ -48,7 +48,8 @@ library Statics { return SchemaLib.encode(_schema); } - function getKeySchema() internal pure returns (Schema) { + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](7); _schema[0] = SchemaType.UINT256; _schema[1] = SchemaType.INT32; @@ -61,39 +62,38 @@ library Statics { return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](7); - _fieldNames[0] = "v1"; - _fieldNames[1] = "v2"; - _fieldNames[2] = "v3"; - _fieldNames[3] = "v4"; - _fieldNames[4] = "v5"; - _fieldNames[5] = "v6"; - _fieldNames[6] = "v7"; - return ("Statics", _fieldNames); - } - - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](7); + keyNames[0] = "k1"; + keyNames[1] = "k2"; + keyNames[2] = "k3"; + keyNames[3] = "k4"; + keyNames[4] = "k5"; + keyNames[5] = "k6"; + keyNames[6] = "k7"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](7); + fieldNames[0] = "v1"; + fieldNames[1] = "v2"; + fieldNames[2] = "v3"; + fieldNames[3] = "v4"; + fieldNames[4] = "v5"; + fieldNames[5] = "v6"; + fieldNames[6] = "v7"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get v1 */ @@ -115,7 +115,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (uint256(Bytes.slice32(_blob, 0))); } @@ -139,7 +139,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (uint256(Bytes.slice32(_blob, 0))); } @@ -154,7 +154,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((v1)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((v1)), getValueSchema()); } /** Set v1 (using the specified store) */ @@ -178,7 +178,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((v1)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((v1)), getValueSchema()); } /** Get v2 */ @@ -200,7 +200,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getValueSchema()); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -224,7 +224,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getValueSchema()); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -239,7 +239,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((v2)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((v2)), getValueSchema()); } /** Set v2 (using the specified store) */ @@ -263,7 +263,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((v2)), getSchema()); + _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((v2)), getValueSchema()); } /** Get v3 */ @@ -285,7 +285,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getValueSchema()); return (Bytes.slice16(_blob, 0)); } @@ -309,7 +309,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getValueSchema()); return (Bytes.slice16(_blob, 0)); } @@ -324,7 +324,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - StoreSwitch.setField(_tableId, _keyTuple, 2, abi.encodePacked((v3)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 2, abi.encodePacked((v3)), getValueSchema()); } /** Set v3 (using the specified store) */ @@ -348,7 +348,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - _store.setField(_tableId, _keyTuple, 2, abi.encodePacked((v3)), getSchema()); + _store.setField(_tableId, _keyTuple, 2, abi.encodePacked((v3)), getValueSchema()); } /** Get v4 */ @@ -370,7 +370,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, getValueSchema()); return (address(Bytes.slice20(_blob, 0))); } @@ -394,7 +394,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, getValueSchema()); return (address(Bytes.slice20(_blob, 0))); } @@ -409,7 +409,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - StoreSwitch.setField(_tableId, _keyTuple, 3, abi.encodePacked((v4)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 3, abi.encodePacked((v4)), getValueSchema()); } /** Set v4 (using the specified store) */ @@ -433,7 +433,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - _store.setField(_tableId, _keyTuple, 3, abi.encodePacked((v4)), getSchema()); + _store.setField(_tableId, _keyTuple, 3, abi.encodePacked((v4)), getValueSchema()); } /** Get v5 */ @@ -455,7 +455,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 4, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 4, getValueSchema()); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -479,7 +479,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 4, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 4, getValueSchema()); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -494,7 +494,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - StoreSwitch.setField(_tableId, _keyTuple, 4, abi.encodePacked((v5)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 4, abi.encodePacked((v5)), getValueSchema()); } /** Set v5 (using the specified store) */ @@ -518,7 +518,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - _store.setField(_tableId, _keyTuple, 4, abi.encodePacked((v5)), getSchema()); + _store.setField(_tableId, _keyTuple, 4, abi.encodePacked((v5)), getValueSchema()); } /** Get v6 */ @@ -540,7 +540,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 5, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 5, getValueSchema()); return Enum1(uint8(Bytes.slice1(_blob, 0))); } @@ -564,7 +564,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 5, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 5, getValueSchema()); return Enum1(uint8(Bytes.slice1(_blob, 0))); } @@ -579,7 +579,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - StoreSwitch.setField(_tableId, _keyTuple, 5, abi.encodePacked(uint8(v6)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 5, abi.encodePacked(uint8(v6)), getValueSchema()); } /** Set v6 (using the specified store) */ @@ -603,7 +603,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - _store.setField(_tableId, _keyTuple, 5, abi.encodePacked(uint8(v6)), getSchema()); + _store.setField(_tableId, _keyTuple, 5, abi.encodePacked(uint8(v6)), getValueSchema()); } /** Get v7 */ @@ -625,7 +625,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 6, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 6, getValueSchema()); return Enum2(uint8(Bytes.slice1(_blob, 0))); } @@ -649,7 +649,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 6, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 6, getValueSchema()); return Enum2(uint8(Bytes.slice1(_blob, 0))); } @@ -664,7 +664,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - StoreSwitch.setField(_tableId, _keyTuple, 6, abi.encodePacked(uint8(v7)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 6, abi.encodePacked(uint8(v7)), getValueSchema()); } /** Set v7 (using the specified store) */ @@ -688,7 +688,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - _store.setField(_tableId, _keyTuple, 6, abi.encodePacked(uint8(v7)), getSchema()); + _store.setField(_tableId, _keyTuple, 6, abi.encodePacked(uint8(v7)), getValueSchema()); } /** Get the full data */ @@ -710,7 +710,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -734,7 +734,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -766,7 +766,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - StoreSwitch.setRecord(_tableId, _keyTuple, _data, getSchema()); + StoreSwitch.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using individual values (using the specified store) */ @@ -798,7 +798,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - _store.setRecord(_tableId, _keyTuple, _data, getSchema()); + _store.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using the data struct */ @@ -909,7 +909,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -932,7 +932,7 @@ library Statics { _keyTuple[5] = bytes32(uint256(uint8(k6))); _keyTuple[6] = bytes32(uint256(uint8(k7))); - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/cli/contracts/test/Tablegen.t.sol b/packages/cli/contracts/test/Tablegen.t.sol index bd34ff44d8..cf840e6387 100644 --- a/packages/cli/contracts/test/Tablegen.t.sol +++ b/packages/cli/contracts/test/Tablegen.t.sol @@ -10,7 +10,7 @@ import { Enum1, Enum2 } from "../src/codegen/Types.sol"; contract TablegenTest is Test, StoreReadWithStubs { function testStaticsSetAndGet() public { - Statics.registerSchema(); + Statics.register(); uint256 k1 = 1; int32 k2 = -1; @@ -29,8 +29,8 @@ contract TablegenTest is Test, StoreReadWithStubs { } function testDynamicsSetAndGet() public { - Dynamics1.registerSchema(); - Dynamics2.registerSchema(); + Dynamics1.register(); + Dynamics2.register(); bytes32 key = keccak256("key"); @@ -93,7 +93,7 @@ contract TablegenTest is Test, StoreReadWithStubs { } function testDynamicsPushAndPop() public { - Dynamics2.registerSchema(); + Dynamics2.register(); bytes32 key = keccak256("key"); @@ -123,7 +123,7 @@ contract TablegenTest is Test, StoreReadWithStubs { } function testSingletonSetAndGet() public { - Singleton.registerSchema(); + Singleton.register(); Singleton.set(-10, [uint32(1), 2], [uint32(3), 4], [uint32(5)]); assertEq(Singleton.getV1(), -10); @@ -145,7 +145,7 @@ contract TablegenTest is Test, StoreReadWithStubs { } function testEphemeral() public { - Ephemeral.registerSchema(); + Ephemeral.register(); Ephemeral.emitEphemeral("key", 123); } diff --git a/packages/cli/src/commands/trace.ts b/packages/cli/src/commands/trace.ts index 1a80c0fea9..1224c157b4 100644 --- a/packages/cli/src/commands/trace.ts +++ b/packages/cli/src/commands/trace.ts @@ -73,7 +73,7 @@ const commandModule: CommandModule = { const names = Object.values(resolvedConfig.systems).map(({ name }) => name); // Fetch system table schema from chain - const systemTableSchema = await WorldContract.getSchema(systemsTableId); + const systemTableSchema = await WorldContract.getValueSchema(systemsTableId); const labels: { name: string; address: string }[] = []; for (const name of names) { const systemSelector = tableIdToHex(namespace, name); diff --git a/packages/cli/src/utils/deploy.ts b/packages/cli/src/utils/deploy.ts index f50a15dc30..6d57d65f29 100644 --- a/packages/cli/src/utils/deploy.ts +++ b/packages/cli/src/utils/deploy.ts @@ -163,16 +163,15 @@ export async function deploy( await fastTxExecute( WorldContract, - "registerTable", - [toBytes16(namespace), toBytes16(name), encodeSchema(schemaTypes), encodeSchema(keyTypes)], - confirmations - ); - - // Register table metadata - await fastTxExecute( - WorldContract, - "setMetadata(bytes16,bytes16,string,string[])", - [toBytes16(namespace), toBytes16(name), tableName, Object.keys(schema)], + "registerTable(bytes16,bytes16,bytes32,bytes32,string[],string[])", + [ + toBytes16(namespace), + toBytes16(name), + encodeSchema(keyTypes), + encodeSchema(schemaTypes), + Object.keys(keySchema), + Object.keys(schema), + ], confirmations ); diff --git a/packages/store-sync/src/blockLogsToStorage.test.ts b/packages/store-sync/src/blockLogsToStorage.test.ts index 30a682321c..843d76aea4 100644 --- a/packages/store-sync/src/blockLogsToStorage.test.ts +++ b/packages/store-sync/src/blockLogsToStorage.test.ts @@ -2,7 +2,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; import { BlockLogsToStorageOptions, blockLogsToStorage } from "./blockLogsToStorage"; import storeConfig from "@latticexyz/store/mud.config"; import { isDefined } from "@latticexyz/common/utils"; -import { TableId } from "@latticexyz/common/deprecated"; +import { tableIdToHex } from "@latticexyz/common"; const mockedCallbacks = { registerTables: vi.fn< @@ -35,7 +35,7 @@ describe("blockLogsToStorage", () => { if (table.namespace === "" && table.name === "Inventory") { return { ...table, - tableId: TableId.toHex("", "Inventory"), + tableId: tableIdToHex("", "Inventory"), keySchema: { owner: "address", item: "uint32", diff --git a/packages/store-sync/src/blockLogsToStorage.ts b/packages/store-sync/src/blockLogsToStorage.ts index 652c92069b..5651f254a3 100644 --- a/packages/store-sync/src/blockLogsToStorage.ts +++ b/packages/store-sync/src/blockLogsToStorage.ts @@ -1,23 +1,20 @@ +import { decodeField, decodeKeyTuple, decodeRecord, abiTypesToSchema, hexToSchema } from "@latticexyz/protocol-parser"; import { - decodeField, - decodeKeyTuple, - decodeRecord, - hexToTableSchema, - abiTypesToSchema, - TableSchema, -} from "@latticexyz/protocol-parser"; -import { StoreConfig, ConfigToKeyPrimitives as Key, ConfigToValuePrimitives as Value } from "@latticexyz/store"; -import { TableId } from "@latticexyz/common/deprecated"; -import { Address, Hex, decodeAbiParameters, getAddress, parseAbiParameters } from "viem"; + StoreConfig, + ConfigToKeyPrimitives as Key, + ConfigToValuePrimitives as Value, + ConfigToValuePrimitives, +} from "@latticexyz/store"; +import { decodeAbiParameters, getAddress, parseAbiParameters } from "viem"; import { debug } from "./debug"; import { isDefined } from "@latticexyz/common/utils"; -import { BlockLogs, StorageOperation, Table, TableName, TableNamespace } from "./common"; +import { BlockLogs, StorageOperation, Table } from "./common"; +import { hexToTableId, tableIdToHex } from "@latticexyz/common"; +import storeConfig from "@latticexyz/store/mud.config"; -// TODO: change table schema/metadata APIs once we get both schema and field names in the same event (https://github.com/latticexyz/mud/pull/1182) - -// TODO: export these from store or world -export const schemaTableId = new TableId("mudstore", "schema"); -export const metadataTableId = new TableId("mudstore", "StoreMetadata"); +// TODO: adjust when we get namespace support (https://github.com/latticexyz/mud/issues/994) and when table has namespace key (https://github.com/latticexyz/mud/issues/1201) +const schemasTable = storeConfig.tables.Tables; +const schemasTableId = tableIdToHex(storeConfig.namespace, schemasTable.name); export type BlockLogsToStorageOptions = { registerTables: (opts: { blockNumber: BlockLogs["blockNumber"]; tables: Table[] }) => Promise; @@ -40,141 +37,83 @@ export type BlockLogsToStorageResult block: BlockLogs ) => Promise>; -type TableKey = `${Address}:${TableNamespace}:${TableName}`; - -// hacky fix for schema registration + metadata events spanning multiple blocks -// TODO: remove this once schema registration+metadata is one event or tx (https://github.com/latticexyz/mud/pull/1182) -const visitedSchemas = new Map(); -const visitedMetadata = new Map< - TableKey, - { address: Address; tableId: TableId; keyNames: readonly string[]; valueNames: readonly string[] } ->(); - export function blockLogsToStorage({ registerTables, getTables, storeOperations, }: BlockLogsToStorageOptions): BlockLogsToStorageResult { return async (block) => { - const newTableKeys = new Set(); - - // First find all schema registration events. - block.logs.forEach((log) => { - if (log.eventName !== "StoreSetRecord") return; - if (log.args.table !== schemaTableId.toHex()) return; - - const [tableForSchema, ...otherKeys] = log.args.key; - if (otherKeys.length) { - debug("registerSchema event is expected to have only one key in key tuple, but got multiple", log); - } - - const tableId = TableId.fromHex(tableForSchema); - const schema = hexToTableSchema(log.args.data); - - const key: TableKey = `${getAddress(log.address)}:${tableId.namespace}:${tableId.name}`; - if (!visitedSchemas.has(key)) { - visitedSchemas.set(key, { address: getAddress(log.address), tableId, schema }); - newTableKeys.add(key); - } - }); + // Find table schema registration events + const newTables = block.logs + .map((log) => { + if (log.eventName !== "StoreSetRecord") return; + if (log.args.table !== schemasTableId) return; + + // TODO: refactor encode/decode to use Record schemas + // TODO: refactor to decode key with protocol-parser utils + + const [tableId, ...otherKeys] = log.args.key; + if (otherKeys.length) { + console.warn("registerSchema event is expected to have only one key in key tuple, but got multiple", log); + } - // Then find all metadata events. These should follow schema registration events and be in the same block (since they're in the same tx). - // TODO: rework contracts so schemas+tables are combined and immutable (https://github.com/latticexyz/mud/pull/1182) - block.logs.forEach((log) => { - if (log.eventName !== "StoreSetRecord") return; - if (log.args.table !== metadataTableId.toHex()) return; - - const [tableForSchema, ...otherKeys] = log.args.key; - if (otherKeys.length) { - debug("setMetadata event is expected to have only one key in key tuple, but got multiple", log); - } - - const tableId = TableId.fromHex(tableForSchema); - const [tableName, abiEncodedFieldNames] = decodeRecord( - // TODO: this is hardcoded for now while metadata is separate from table registration (https://github.com/latticexyz/mud/pull/1182) - { staticFields: [], dynamicFields: ["string", "bytes"] }, - log.args.data - ); - const valueNames = decodeAbiParameters(parseAbiParameters("string[]"), abiEncodedFieldNames as Hex)[0]; - // TODO: add key names to table registration when we refactor it (https://github.com/latticexyz/mud/pull/1182) - const key: TableKey = `${getAddress(log.address)}:${tableId.namespace}:${tableName}`; - if (!visitedMetadata.has(key)) { - visitedMetadata.set(key, { address: getAddress(log.address), tableId, keyNames: [], valueNames }); - newTableKeys.add(key); - } - }); + const table = hexToTableId(tableId); - const newTableIds = Array.from(newTableKeys).map((tableKey) => { - const [address, namespace, name] = tableKey.split(":"); - return { address: address as Hex, tableId: new TableId(namespace, name) }; - }); + const valueTuple = decodeRecord(abiTypesToSchema(Object.values(schemasTable.schema)), log.args.data); + const value = Object.fromEntries( + Object.keys(schemasTable.schema).map((name, i) => [name, valueTuple[i]]) + ) as ConfigToValuePrimitives; + + const keySchema = hexToSchema(value.keySchema); + const valueSchema = hexToSchema(value.valueSchema); + const keyNames = decodeAbiParameters(parseAbiParameters("string[]"), value.abiEncodedKeyNames)[0]; + const fieldNames = decodeAbiParameters(parseAbiParameters("string[]"), value.abiEncodedFieldNames)[0]; + + const valueAbiTypes = [...valueSchema.staticFields, ...valueSchema.dynamicFields]; + return { + address: log.address, + tableId, + namespace: table.namespace, + name: table.name, + keySchema: Object.fromEntries(keySchema.staticFields.map((abiType, i) => [keyNames[i], abiType])), + valueSchema: Object.fromEntries(valueAbiTypes.map((abiType, i) => [fieldNames[i], abiType])), + }; + }) + .filter(isDefined); + + // Then register tables before we start storing data in them await registerTables({ blockNumber: block.blockNumber, - tables: newTableIds - .map(({ address, tableId }) => { - const schema = Array.from(visitedSchemas.values()).find( - ({ address: schemaAddress, tableId: schemaTableId }) => - schemaAddress === address && schemaTableId.toHex() === tableId.toHex() - ); - const metadata = Array.from(visitedMetadata.values()).find( - ({ address: metadataAddress, tableId: metadataTableId }) => - metadataAddress === address && metadataTableId.toHex() === tableId.toHex() - ); - if (!schema) { - debug( - `no schema registration found for table ${tableId.toString()} in block ${block.blockNumber}, skipping` - ); - return; - } - if (!metadata) { - debug( - `no metadata registration found for table ${tableId.toString()} in block ${block.blockNumber}, skipping` - ); - return; - } - - const valueAbiTypes = [...schema.schema.valueSchema.staticFields, ...schema.schema.valueSchema.dynamicFields]; - - return { - address, - tableId: schema.tableId.toHex(), - namespace: schema.tableId.namespace, - name: schema.tableId.name, - // TODO: replace with proper named key tuple (https://github.com/latticexyz/mud/pull/1182) - keySchema: Object.fromEntries(schema.schema.keySchema.staticFields.map((abiType, i) => [i, abiType])), - valueSchema: Object.fromEntries(valueAbiTypes.map((abiType, i) => [metadata.valueNames[i], abiType])), - }; - }) - .filter(isDefined), + tables: newTables, }); - const tableIds = Array.from( + const tablesToFetch = Array.from( new Set( block.logs.map((log) => JSON.stringify({ address: getAddress(log.address), - ...TableId.fromHex(log.args.table), + tableId: log.args.table, + ...hexToTableId(log.args.table), }) ) ) - ); - // TODO: combine these once we refactor table registration (https://github.com/latticexyz/mud/pull/1182) + ).map((json) => JSON.parse(json)); + const tables = Object.fromEntries( ( await getTables({ blockNumber: block.blockNumber, - tables: tableIds.map((json) => JSON.parse(json)), + tables: tablesToFetch, }) - ).map((table) => [`${table.address}:${new TableId(table.namespace, table.name).toHex()}`, table]) - ) as Record; + ).map((table) => [`${getAddress(table.address)}:${table.tableId}`, table]) + ) as Record; const operations = block.logs .map((log): StorageOperation | undefined => { - const tableId = TableId.fromHex(log.args.table); const table = tables[`${getAddress(log.address)}:${log.args.table}`]; if (!table) { - debug("no table found for event, skipping", tableId.toString(), log); + debug("no table found for event, skipping", hexToTableId(log.args.table), log); return; } @@ -192,19 +131,19 @@ export function blockLogsToStorage({ const valueSchema = abiTypesToSchema(valueAbiTypes); const fieldNames = Object.keys(table.valueSchema); + // TODO: decide if we should split these up into distinct operations so the storage adapter can decide whether to combine or not if (log.eventName === "StoreSetRecord" || log.eventName === "StoreEphemeralRecord") { const valueTuple = decodeRecord(valueSchema, log.args.data); const value = Object.fromEntries(fieldNames.map((name, i) => [name, valueTuple[i]])) as Value< TConfig, keyof TConfig["tables"] >; - // TODO: decide if we should handle ephemeral records separately? - // they'll eventually be turned into "events", but unclear if that should translate to client storage operations return { log, address: getAddress(log.address), + namespace: table.namespace, + name: table.name, type: "SetRecord", - ...tableId, key, value, }; @@ -219,8 +158,9 @@ export function blockLogsToStorage({ return { log, address: getAddress(log.address), + namespace: table.namespace, + name: table.name, type: "SetField", - ...tableId, key, fieldName, fieldValue, @@ -231,8 +171,9 @@ export function blockLogsToStorage({ return { log, address: getAddress(log.address), + namespace: table.namespace, + name: table.name, type: "DeleteRecord", - ...tableId, key, }; } diff --git a/packages/store-sync/src/recs/defineInternalComponents.ts b/packages/store-sync/src/recs/defineInternalComponents.ts index 5f4a461549..992b878ce4 100644 --- a/packages/store-sync/src/recs/defineInternalComponents.ts +++ b/packages/store-sync/src/recs/defineInternalComponents.ts @@ -4,10 +4,10 @@ import { Table } from "../common"; // eslint-disable-next-line @typescript-eslint/explicit-function-return-type export function defineInternalComponents(world: World) { return { - TableMetadata: defineComponent<{ table: Type.T }, Metadata, Table>( + RegisteredTables: defineComponent<{ table: Type.T }, Metadata, Table>( world, { table: Type.T }, - { metadata: { componentName: "TableMetadata" } } + { metadata: { componentName: "RegisteredTables" } } ), SyncProgress: defineComponent( world, diff --git a/packages/store-sync/src/recs/recsStorage.ts b/packages/store-sync/src/recs/recsStorage.ts index d0db64f0c2..d41548c485 100644 --- a/packages/store-sync/src/recs/recsStorage.ts +++ b/packages/store-sync/src/recs/recsStorage.ts @@ -36,19 +36,19 @@ export function recsStorage({ async registerTables({ tables }) { for (const table of tables) { // TODO: check if table exists already and skip/warn? - setComponent(components.TableMetadata, getTableKey(table) as Entity, { table }); + setComponent(components.RegisteredTables, getTableKey(table) as Entity, { table }); } }, async getTables({ tables }) { // TODO: fetch schema from RPC if table not found? return tables - .map((table) => getComponentValue(components.TableMetadata, getTableKey(table) as Entity)?.table) + .map((table) => getComponentValue(components.RegisteredTables, getTableKey(table) as Entity)?.table) .filter(isDefined); }, async storeOperations({ operations }) { for (const operation of operations) { const table = getComponentValue( - components.TableMetadata, + components.RegisteredTables, getTableKey({ address: operation.address, namespace: operation.namespace, diff --git a/packages/store-sync/src/sqlite/sqliteStorage.test.ts b/packages/store-sync/src/sqlite/sqliteStorage.test.ts index 386bd61220..4035fc9833 100644 --- a/packages/store-sync/src/sqlite/sqliteStorage.test.ts +++ b/packages/store-sync/src/sqlite/sqliteStorage.test.ts @@ -43,56 +43,39 @@ describe("sqliteStorage", async () => { logs: [ { address: "0x5fbdb2315678afecb367f032d93f642f64180aa3", - topics: ["0x912af873e852235aae78a1d25ae9bb28b616a67c36898c53a14fd8184504ee32"], - data: "0x6d756473746f72650000000000000000736368656d6100000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000496e76656e746f72790000000000000000000000000000000000000000000000000000000000000000000000000000400004010003000000000000000000000000000000000000000000000000000000002001005f000000000000000000000000000000000000000000000000000000", - blockNumber: 5448n, - transactionHash: "0x2766c01dd2290a80e2b54c27e95ca303d7d362643a68bd71c7d8fdb620f2a3a6", - transactionIndex: 18, - blockHash: "0xc65212ced76e80c3d59fd210fca434d9ceebfc25b544989d5eaecec3d31f9ac9", - logIndex: 18, + topics: ["0xd01f9f1368f831528fc9fe6442366b2b7d957fbfff3bcf7c24d9ab5fe51f8c46"], + data: "0x000000000000000000000000000000005265736f7572636554797065000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000496e76656e746f72790000000000000000000000000000000000000000000000000000000000000000000000000000010200000000000000000000000000000000000000000000000000000000000000", + blockHash: "0x4ad3752c86f900332e0d2d8903480e7206747d233586574d16f006eebdb5138b", + blockNumber: 2n, + transactionHash: "0xaa54bf18053cce5d4d2906538a60cb1d9958cc3c10c34b5f9fdc92fe6a6abab4", + transactionIndex: 16, + logIndex: 54, removed: false, args: { - table: "0x6d756473746f72650000000000000000736368656d6100000000000000000000", + table: "0x000000000000000000000000000000005265736f757263655479706500000000", key: ["0x00000000000000000000000000000000496e76656e746f727900000000000000"], - data: "0x0004010003000000000000000000000000000000000000000000000000000000002001005f000000000000000000000000000000000000000000000000000000", + schemaIndex: 0, + data: "0x02", }, - eventName: "StoreSetRecord", + eventName: "StoreSetField", }, { address: "0x5fbdb2315678afecb367f032d93f642f64180aa3", topics: ["0x912af873e852235aae78a1d25ae9bb28b616a67c36898c53a14fd8184504ee32"], - data: "0x6d756473746f7265000000000000000053746f72654d65746164617461000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000496e76656e746f72790000000000000000000000000000000000000000000000000000000000000000000000000000c9000000000000a9000000000900000000a0000000000000000000000000000000496e76656e746f7279000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000576616c75650000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", - blockNumber: 5448n, - transactionHash: "0x80d6650fdd6656461e6639988d7baa8d6d228297df505d8bbd0a4efc273b382b", - transactionIndex: 44, - blockHash: "0x930656a2399ed2473449118a030cf9a3b3f770db4f74e9b565e2e0035c49bc6e", - logIndex: 44, + data: "0x6d756473746f726500000000000000005461626c657300000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000496e76656e746f7279000000000000000000000000000000000000000000000000000000000000000000000000000260001c030061030300000000000000000000000000000000000000000000000000000401000300000000000000000000000000000000000000000000000000000000000000000200000000016000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000056f776e657200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000046974656d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b6974656d56617269616e740000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000006616d6f756e740000000000000000000000000000000000000000000000000000", + blockHash: "0x4ad3752c86f900332e0d2d8903480e7206747d233586574d16f006eebdb5138b", + blockNumber: 2n, + transactionHash: "0xaa54bf18053cce5d4d2906538a60cb1d9958cc3c10c34b5f9fdc92fe6a6abab4", + transactionIndex: 16, + logIndex: 55, removed: false, args: { - table: "0x6d756473746f7265000000000000000053746f72654d65746164617461000000", + table: "0x6d756473746f726500000000000000005461626c657300000000000000000000", key: ["0x00000000000000000000000000000000496e76656e746f727900000000000000"], - data: "0x000000000000a9000000000900000000a0000000000000000000000000000000496e76656e746f7279000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000576616c7565000000000000000000000000000000000000000000000000000000", + data: "0x001c030061030300000000000000000000000000000000000000000000000000000401000300000000000000000000000000000000000000000000000000000000000000000200000000016000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000056f776e657200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000046974656d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b6974656d56617269616e740000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000006616d6f756e740000000000000000000000000000000000000000000000000000", }, eventName: "StoreSetRecord", }, - { - address: "0x5fbdb2315678afecb367f032d93f642f64180aa3", - topics: ["0xd01f9f1368f831528fc9fe6442366b2b7d957fbfff3bcf7c24d9ab5fe51f8c46"], - data: "0x00000000000000000000000000000000496e76656e746f7279000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000796eb990a3f9c431c69149c7a168b91596d87f600000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000040000000800000000000000000000000000000000000000000000000000000000", - blockHash: "0x03e962e7402b2ab295b92feac342a132111dd14b0d1fd4d4a0456fdc77981577", - blockNumber: 5448n, - transactionHash: "0xa6986924609542dc4c2d81c53799d8eab47109ef34ee1e422de595e19ee9bfa4", - transactionIndex: 88, - logIndex: 88, - removed: false, - args: { - table: "0x00000000000000000000000000000000496e76656e746f727900000000000000", - key: ["0x000000000000000000000000796eb990a3f9c431c69149c7a168b91596d87f60"], - schemaIndex: 0, - data: "0x00000008", - }, - eventName: "StoreSetField", - }, ], }); @@ -113,7 +96,9 @@ describe("sqliteStorage", async () => { "address": "0x5FbDB2315678afecb367f032d93F642f64180aa3", "id": "0x5FbDB2315678afecb367f032d93F642f64180aa3____Inventory", "keySchema": { - "0": "bytes32", + "item": "uint32", + "itemVariant": "uint32", + "owner": "address", }, "lastError": null, "lastUpdatedBlockNumber": 5448n, @@ -122,7 +107,7 @@ describe("sqliteStorage", async () => { "schemaVersion": 1, "tableId": "0x00000000000000000000000000000000496e76656e746f727900000000000000", "valueSchema": { - "value": "uint32", + "amount": "uint32", }, }, ] @@ -135,30 +120,22 @@ describe("sqliteStorage", async () => { "address": "0x5FbDB2315678afecb367f032d93F642f64180aa3", "id": "0x5FbDB2315678afecb367f032d93F642f64180aa3____Inventory", "keySchema": { - "0": "bytes32", + "item": "uint32", + "itemVariant": "uint32", + "owner": "address", }, "lastUpdatedBlockNumber": 5448n, "name": "Inventory", "namespace": "", "tableId": "0x00000000000000000000000000000000496e76656e746f727900000000000000", "valueSchema": { - "value": "uint32", + "amount": "uint32", }, }, ] `); const sqliteTable = createSqliteTable(tables[0]); - expect(db.select().from(sqliteTable).all()).toMatchInlineSnapshot(` - [ - { - "0": "0x000000000000000000000000796eb990a3f9c431c69149c7a168b91596d87f60", - "__isDeleted": false, - "__key": "0x000000000000000000000000796eb990a3f9c431c69149c7a168b91596d87f60", - "__lastUpdatedBlockNumber": 5448n, - "value": 8, - }, - ] - `); + expect(db.select().from(sqliteTable).all()).toMatchInlineSnapshot("[]"); }); }); diff --git a/packages/store/abi/IStore.sol/IStore.abi.json b/packages/store/abi/IStore.sol/IStore.abi.json index 3dc7bc3b3a..d21e6f935f 100644 --- a/packages/store/abi/IStore.sol/IStore.abi.json +++ b/packages/store/abi/IStore.sol/IStore.abi.json @@ -47,6 +47,22 @@ "name": "StoreCore_InvalidFieldNamesLength", "type": "error" }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "expected", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "received", + "type": "uint256" + } + ], + "name": "StoreCore_InvalidKeyNamesLength", + "type": "error" + }, { "inputs": [], "name": "StoreCore_NotDynamicField", @@ -408,7 +424,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", @@ -493,17 +509,12 @@ "type": "bytes32" }, { - "internalType": "Schema", - "name": "schema", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "keySchema", - "type": "bytes32" + "internalType": "contract IStoreHook", + "name": "hook", + "type": "address" } ], - "name": "registerSchema", + "name": "registerStoreHook", "outputs": [], "stateMutability": "nonpayable", "type": "function" @@ -516,12 +527,27 @@ "type": "bytes32" }, { - "internalType": "contract IStoreHook", - "name": "hook", - "type": "address" + "internalType": "Schema", + "name": "keySchema", + "type": "bytes32" + }, + { + "internalType": "Schema", + "name": "valueSchema", + "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" + }, + { + "internalType": "string[]", + "name": "fieldNames", + "type": "string[]" } ], - "name": "registerStoreHook", + "name": "registerTable", "outputs": [], "stateMutability": "nonpayable", "type": "function" @@ -559,29 +585,6 @@ "stateMutability": "nonpayable", "type": "function" }, - { - "inputs": [ - { - "internalType": "bytes32", - "name": "table", - "type": "bytes32" - }, - { - "internalType": "string", - "name": "tableName", - "type": "string" - }, - { - "internalType": "string[]", - "name": "fieldNames", - "type": "string[]" - } - ], - "name": "setMetadata", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, { "inputs": [ { diff --git a/packages/store/abi/IStore.sol/IStoreData.abi.json b/packages/store/abi/IStore.sol/IStoreData.abi.json index 8bdc4379fa..f02203d5e9 100644 --- a/packages/store/abi/IStore.sol/IStoreData.abi.json +++ b/packages/store/abi/IStore.sol/IStoreData.abi.json @@ -265,7 +265,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", diff --git a/packages/store/abi/IStore.sol/IStoreRead.abi.json b/packages/store/abi/IStore.sol/IStoreRead.abi.json index ca89eb4ba5..0f47144f3d 100644 --- a/packages/store/abi/IStore.sol/IStoreRead.abi.json +++ b/packages/store/abi/IStore.sol/IStoreRead.abi.json @@ -167,7 +167,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", diff --git a/packages/store/abi/IStore.sol/IStoreRegistration.abi.json b/packages/store/abi/IStore.sol/IStoreRegistration.abi.json index 2bf0123223..0a30f85789 100644 --- a/packages/store/abi/IStore.sol/IStoreRegistration.abi.json +++ b/packages/store/abi/IStore.sol/IStoreRegistration.abi.json @@ -1,27 +1,4 @@ [ - { - "inputs": [ - { - "internalType": "bytes32", - "name": "table", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "schema", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "keySchema", - "type": "bytes32" - } - ], - "name": "registerSchema", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, { "inputs": [ { @@ -48,9 +25,19 @@ "type": "bytes32" }, { - "internalType": "string", - "name": "tableName", - "type": "string" + "internalType": "Schema", + "name": "keySchema", + "type": "bytes32" + }, + { + "internalType": "Schema", + "name": "valueSchema", + "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" }, { "internalType": "string[]", @@ -58,7 +45,7 @@ "type": "string[]" } ], - "name": "setMetadata", + "name": "registerTable", "outputs": [], "stateMutability": "nonpayable", "type": "function" diff --git a/packages/store/abi/IStoreErrors.sol/IStoreErrors.abi.json b/packages/store/abi/IStoreErrors.sol/IStoreErrors.abi.json index d38ae0cc0d..cf6d375396 100644 --- a/packages/store/abi/IStoreErrors.sol/IStoreErrors.abi.json +++ b/packages/store/abi/IStoreErrors.sol/IStoreErrors.abi.json @@ -47,6 +47,22 @@ "name": "StoreCore_InvalidFieldNamesLength", "type": "error" }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "expected", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "received", + "type": "uint256" + } + ], + "name": "StoreCore_InvalidKeyNamesLength", + "type": "error" + }, { "inputs": [], "name": "StoreCore_NotDynamicField", diff --git a/packages/store/abi/MirrorSubscriber.sol/MirrorSubscriber.abi.json b/packages/store/abi/MirrorSubscriber.sol/MirrorSubscriber.abi.json index b915714321..494ec13752 100644 --- a/packages/store/abi/MirrorSubscriber.sol/MirrorSubscriber.abi.json +++ b/packages/store/abi/MirrorSubscriber.sol/MirrorSubscriber.abi.json @@ -8,13 +8,23 @@ }, { "internalType": "Schema", - "name": "schema", + "name": "keySchema", "type": "bytes32" }, { "internalType": "Schema", - "name": "keySchema", + "name": "valueSchema", "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" + }, + { + "internalType": "string[]", + "name": "fieldNames", + "type": "string[]" } ], "stateMutability": "nonpayable", diff --git a/packages/store/abi/StoreMock.sol/StoreMock.abi.json b/packages/store/abi/StoreMock.sol/StoreMock.abi.json index b0bd44ca92..34cbc541dc 100644 --- a/packages/store/abi/StoreMock.sol/StoreMock.abi.json +++ b/packages/store/abi/StoreMock.sol/StoreMock.abi.json @@ -84,6 +84,22 @@ "name": "StoreCore_InvalidFieldNamesLength", "type": "error" }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "expected", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "received", + "type": "uint256" + } + ], + "name": "StoreCore_InvalidKeyNamesLength", + "type": "error" + }, { "inputs": [], "name": "StoreCore_NotDynamicField", @@ -445,7 +461,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", @@ -530,17 +546,12 @@ "type": "bytes32" }, { - "internalType": "Schema", - "name": "schema", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "keySchema", - "type": "bytes32" + "internalType": "contract IStoreHook", + "name": "hook", + "type": "address" } ], - "name": "registerSchema", + "name": "registerStoreHook", "outputs": [], "stateMutability": "nonpayable", "type": "function" @@ -553,12 +564,27 @@ "type": "bytes32" }, { - "internalType": "contract IStoreHook", - "name": "hook", - "type": "address" + "internalType": "Schema", + "name": "keySchema", + "type": "bytes32" + }, + { + "internalType": "Schema", + "name": "valueSchema", + "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" + }, + { + "internalType": "string[]", + "name": "fieldNames", + "type": "string[]" } ], - "name": "registerStoreHook", + "name": "registerTable", "outputs": [], "stateMutability": "nonpayable", "type": "function" @@ -596,29 +622,6 @@ "stateMutability": "nonpayable", "type": "function" }, - { - "inputs": [ - { - "internalType": "bytes32", - "name": "table", - "type": "bytes32" - }, - { - "internalType": "string", - "name": "tableName", - "type": "string" - }, - { - "internalType": "string[]", - "name": "fieldNames", - "type": "string[]" - } - ], - "name": "setMetadata", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, { "inputs": [ { diff --git a/packages/store/abi/StoreRead.sol/StoreRead.abi.json b/packages/store/abi/StoreRead.sol/StoreRead.abi.json index b05f278530..93a0e17cf5 100644 --- a/packages/store/abi/StoreRead.sol/StoreRead.abi.json +++ b/packages/store/abi/StoreRead.sol/StoreRead.abi.json @@ -73,6 +73,22 @@ "name": "StoreCore_InvalidFieldNamesLength", "type": "error" }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "expected", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "received", + "type": "uint256" + } + ], + "name": "StoreCore_InvalidKeyNamesLength", + "type": "error" + }, { "inputs": [], "name": "StoreCore_NotDynamicField", @@ -278,7 +294,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", diff --git a/packages/store/abi/StoreReadWithStubs.sol/StoreReadWithStubs.abi.json b/packages/store/abi/StoreReadWithStubs.sol/StoreReadWithStubs.abi.json index e2c74d7f0c..46ca3e084b 100644 --- a/packages/store/abi/StoreReadWithStubs.sol/StoreReadWithStubs.abi.json +++ b/packages/store/abi/StoreReadWithStubs.sol/StoreReadWithStubs.abi.json @@ -84,6 +84,22 @@ "name": "StoreCore_InvalidFieldNamesLength", "type": "error" }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "expected", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "received", + "type": "uint256" + } + ], + "name": "StoreCore_InvalidKeyNamesLength", + "type": "error" + }, { "inputs": [], "name": "StoreCore_NotDynamicField", @@ -450,7 +466,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", @@ -535,17 +551,12 @@ "type": "bytes32" }, { - "internalType": "Schema", - "name": "", - "type": "bytes32" - }, - { - "internalType": "Schema", + "internalType": "contract IStoreHook", "name": "", - "type": "bytes32" + "type": "address" } ], - "name": "registerSchema", + "name": "registerStoreHook", "outputs": [], "stateMutability": "nonpayable", "type": "function" @@ -558,12 +569,27 @@ "type": "bytes32" }, { - "internalType": "contract IStoreHook", + "internalType": "Schema", "name": "", - "type": "address" + "type": "bytes32" + }, + { + "internalType": "Schema", + "name": "", + "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "", + "type": "string[]" + }, + { + "internalType": "string[]", + "name": "", + "type": "string[]" } ], - "name": "registerStoreHook", + "name": "registerTable", "outputs": [], "stateMutability": "nonpayable", "type": "function" @@ -601,29 +627,6 @@ "stateMutability": "nonpayable", "type": "function" }, - { - "inputs": [ - { - "internalType": "bytes32", - "name": "", - "type": "bytes32" - }, - { - "internalType": "string", - "name": "", - "type": "string" - }, - { - "internalType": "string[]", - "name": "", - "type": "string[]" - } - ], - "name": "setMetadata", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, { "inputs": [ { diff --git a/packages/store/abi/StoreMetadata.sol/StoreMetadata.abi.json b/packages/store/abi/Tables.sol/Tables.abi.json similarity index 100% rename from packages/store/abi/StoreMetadata.sol/StoreMetadata.abi.json rename to packages/store/abi/Tables.sol/Tables.abi.json diff --git a/packages/store/gas-report.json b/packages/store/gas-report.json index 02ec7cb886..1e94000ce1 100644 --- a/packages/store/gas-report.json +++ b/packages/store/gas-report.json @@ -243,7 +243,7 @@ "file": "test/KeyEncoding.t.sol", "test": "testRegisterAndGetSchema", "name": "register KeyEncoding schema", - "gasUsed": 65010 + "gasUsed": 679626 }, { "file": "test/Mixed.t.sol", @@ -255,19 +255,19 @@ "file": "test/Mixed.t.sol", "test": "testRegisterAndGetSchema", "name": "register Mixed schema", - "gasUsed": 61992 + "gasUsed": 540065 }, { "file": "test/Mixed.t.sol", "test": "testSetAndGet", "name": "set record in Mixed", - "gasUsed": 111113 + "gasUsed": 111151 }, { "file": "test/Mixed.t.sol", "test": "testSetAndGet", "name": "get record from Mixed", - "gasUsed": 12270 + "gasUsed": 12294 }, { "file": "test/PackedCounter.t.sol", @@ -477,169 +477,169 @@ "file": "test/StoreCoreDynamic.t.sol", "test": "testGetThirdFieldLength", "name": "get field length (warm, 2 slots)", - "gasUsed": 1955 + "gasUsed": 1956 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromSecondField", "name": "pop from field (cold, 1 slot, 1 uint32 item)", - "gasUsed": 25220 + "gasUsed": 23215 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromSecondField", "name": "pop from field (warm, 1 slot, 1 uint32 item)", - "gasUsed": 17251 + "gasUsed": 17245 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromThirdField", "name": "pop from field (cold, 2 slots, 10 uint32 items)", - "gasUsed": 26970 + "gasUsed": 24964 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromThirdField", "name": "pop from field (warm, 2 slots, 10 uint32 items)", - "gasUsed": 17001 + "gasUsed": 16995 }, { "file": "test/StoreCoreGas.t.sol", "test": "testAccessEmptyData", "name": "access non-existing record", - "gasUsed": 6085 + "gasUsed": 6090 }, { "file": "test/StoreCoreGas.t.sol", "test": "testAccessEmptyData", "name": "access static field of non-existing record", - "gasUsed": 1569 + "gasUsed": 1571 }, { "file": "test/StoreCoreGas.t.sol", "test": "testAccessEmptyData", "name": "access dynamic field of non-existing record", - "gasUsed": 2227 + "gasUsed": 2233 }, { "file": "test/StoreCoreGas.t.sol", "test": "testAccessEmptyData", "name": "access length of dynamic field of non-existing record", - "gasUsed": 1327 + "gasUsed": 1329 }, { "file": "test/StoreCoreGas.t.sol", "test": "testAccessEmptyData", "name": "access slice of dynamic field of non-existing record", - "gasUsed": 1201 + "gasUsed": 1203 }, { "file": "test/StoreCoreGas.t.sol", "test": "testDeleteData", "name": "delete record (complex data, 3 slots)", - "gasUsed": 8973 + "gasUsed": 8983 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHasSchema", "name": "Check for existence of table (existent)", - "gasUsed": 1127 + "gasUsed": 6819 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHasSchema", "name": "check for existence of table (non-existent)", - "gasUsed": 3129 + "gasUsed": 8821 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooks", "name": "register subscriber", - "gasUsed": 64193 + "gasUsed": 62181 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooks", "name": "set record on table with subscriber", - "gasUsed": 71618 + "gasUsed": 71606 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooks", "name": "set static field on table with subscriber", - "gasUsed": 27532 + "gasUsed": 27542 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooks", "name": "delete record on table with subscriber", - "gasUsed": 20037 + "gasUsed": 19988 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooksDynamicData", "name": "register subscriber", - "gasUsed": 64193 + "gasUsed": 62181 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooksDynamicData", "name": "set (dynamic) record on table with subscriber", - "gasUsed": 164922 + "gasUsed": 164934 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooksDynamicData", "name": "set (dynamic) field on table with subscriber", - "gasUsed": 30414 + "gasUsed": 30424 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooksDynamicData", "name": "delete (dynamic) record on table with subscriber", - "gasUsed": 21597 + "gasUsed": 21545 }, { "file": "test/StoreCoreGas.t.sol", "test": "testPushToField", "name": "push to field (1 slot, 1 uint32 item)", - "gasUsed": 14866 + "gasUsed": 14881 }, { "file": "test/StoreCoreGas.t.sol", "test": "testPushToField", "name": "push to field (2 slots, 10 uint32 items)", - "gasUsed": 37512 + "gasUsed": 37528 }, { "file": "test/StoreCoreGas.t.sol", "test": "testRegisterAndGetSchema", "name": "StoreCore: register schema", - "gasUsed": 54509 + "gasUsed": 601182 }, { "file": "test/StoreCoreGas.t.sol", "test": "testRegisterAndGetSchema", "name": "StoreCore: get schema (warm)", - "gasUsed": 1128 + "gasUsed": 6833 }, { "file": "test/StoreCoreGas.t.sol", "test": "testRegisterAndGetSchema", "name": "StoreCore: get key schema (warm)", - "gasUsed": 2324 + "gasUsed": 13048 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetDynamicData", "name": "set complex record with dynamic data (4 slots)", - "gasUsed": 105598 + "gasUsed": 103624 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetDynamicData", "name": "get complex record with dynamic data (4 slots)", - "gasUsed": 5089 + "gasUsed": 5101 }, { "file": "test/StoreCoreGas.t.sol", @@ -651,127 +651,109 @@ "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetDynamicData", "name": "compare: Set complex record with dynamic data using abi.encode", - "gasUsed": 267368 + "gasUsed": 267372 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetDynamicDataLength", "name": "set dynamic length of dynamic index 0", - "gasUsed": 23610 + "gasUsed": 23613 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetDynamicDataLength", "name": "set dynamic length of dynamic index 1", - "gasUsed": 1711 + "gasUsed": 1714 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetDynamicDataLength", "name": "reduce dynamic length of dynamic index 0", - "gasUsed": 1699 + "gasUsed": 1702 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetField", "name": "set static field (1 slot)", - "gasUsed": 35914 + "gasUsed": 33919 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetField", "name": "get static field (1 slot)", - "gasUsed": 1569 + "gasUsed": 1572 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetField", "name": "set static field (overlap 2 slot)", - "gasUsed": 32849 + "gasUsed": 32854 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetField", "name": "get static field (overlap 2 slot)", - "gasUsed": 2383 + "gasUsed": 2386 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetField", "name": "set dynamic field (1 slot, first dynamic field)", - "gasUsed": 55302 + "gasUsed": 55312 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetField", "name": "get dynamic field (1 slot, first dynamic field)", - "gasUsed": 2404 + "gasUsed": 2410 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetField", "name": "set dynamic field (1 slot, second dynamic field)", - "gasUsed": 33420 + "gasUsed": 33430 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetField", "name": "get dynamic field (1 slot, second dynamic field)", - "gasUsed": 2411 + "gasUsed": 2418 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetStaticData", "name": "set static record (1 slot)", - "gasUsed": 35388 + "gasUsed": 33393 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetStaticData", "name": "get static record (1 slot)", - "gasUsed": 1266 + "gasUsed": 1269 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetStaticDataSpanningWords", "name": "set static record (2 slots)", - "gasUsed": 57893 + "gasUsed": 55897 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetStaticDataSpanningWords", "name": "get static record (2 slots)", - "gasUsed": 1455 - }, - { - "file": "test/StoreCoreGas.t.sol", - "test": "testSetMetadata", - "name": "StoreCore: set table metadata", - "gasUsed": 248815 + "gasUsed": 1457 }, { "file": "test/StoreCoreGas.t.sol", "test": "testUpdateInField", "name": "update in field (1 slot, 1 uint32 item)", - "gasUsed": 14402 + "gasUsed": 14416 }, { "file": "test/StoreCoreGas.t.sol", "test": "testUpdateInField", "name": "push to field (2 slots, 6 uint64 items)", - "gasUsed": 15431 - }, - { - "file": "test/StoreMetadata.t.sol", - "test": "testSetAndGet", - "name": "set record in StoreMetadataTable", - "gasUsed": 247577 - }, - { - "file": "test/StoreMetadata.t.sol", - "test": "testSetAndGet", - "name": "get record from StoreMetadataTable", - "gasUsed": 11408 + "gasUsed": 15445 }, { "file": "test/StoreSwitch.t.sol", @@ -789,19 +771,19 @@ "file": "test/tables/Callbacks.t.sol", "test": "testSetAndGet", "name": "Callbacks: set field", - "gasUsed": 59929 + "gasUsed": 59939 }, { "file": "test/tables/Callbacks.t.sol", "test": "testSetAndGet", "name": "Callbacks: get field (warm)", - "gasUsed": 5278 + "gasUsed": 5287 }, { "file": "test/tables/Callbacks.t.sol", "test": "testSetAndGet", "name": "Callbacks: push 1 element", - "gasUsed": 39797 + "gasUsed": 39813 }, { "file": "test/tables/Hooks.t.sol", @@ -879,7 +861,7 @@ "file": "test/tables/HooksColdLoad.t.sol", "test": "testPop", "name": "Hooks: pop 1 element (cold)", - "gasUsed": 26387 + "gasUsed": 26386 }, { "file": "test/tables/HooksColdLoad.t.sol", @@ -939,18 +921,18 @@ "file": "test/Vector2.t.sol", "test": "testRegisterAndGetSchema", "name": "register Vector2 schema", - "gasUsed": 59169 + "gasUsed": 400685 }, { "file": "test/Vector2.t.sol", "test": "testSetAndGet", "name": "set Vector2 record", - "gasUsed": 36699 + "gasUsed": 36708 }, { "file": "test/Vector2.t.sol", "test": "testSetAndGet", "name": "get Vector2 record", - "gasUsed": 4548 + "gasUsed": 4556 } ] diff --git a/packages/store/mud.config.ts b/packages/store/mud.config.ts index 58a8de8b5a..6957d88632 100644 --- a/packages/store/mud.config.ts +++ b/packages/store/mud.config.ts @@ -9,12 +9,14 @@ export default mudConfig({ tables: { Hooks: "address[]", Callbacks: "bytes24[]", - StoreMetadata: { + Tables: { keySchema: { tableId: "bytes32", }, schema: { - tableName: "string", + keySchema: "bytes32", + valueSchema: "bytes32", + abiEncodedKeyNames: "bytes", abiEncodedFieldNames: "bytes", }, }, diff --git a/packages/store/src/IStore.sol b/packages/store/src/IStore.sol index 6f66e7699c..cc38319efb 100644 --- a/packages/store/src/IStore.sol +++ b/packages/store/src/IStore.sol @@ -5,7 +5,7 @@ import { IStoreErrors } from "./IStoreErrors.sol"; import { Schema } from "./Schema.sol"; interface IStoreRead { - function getSchema(bytes32 table) external view returns (Schema schema); + function getValueSchema(bytes32 table) external view returns (Schema schema); function getKeySchema(bytes32 table) external view returns (Schema schema); @@ -114,9 +114,13 @@ interface IStoreData is IStoreRead, IStoreWrite { * making them less performance critical than the IStoreData methods. */ interface IStoreRegistration { - function registerSchema(bytes32 table, Schema schema, Schema keySchema) external; - - function setMetadata(bytes32 table, string calldata tableName, string[] calldata fieldNames) external; + function registerTable( + bytes32 table, + Schema keySchema, + Schema valueSchema, + string[] calldata keyNames, + string[] calldata fieldNames + ) external; // Register hook to be called when a record or field is set or deleted function registerStoreHook(bytes32 table, IStoreHook hook) external; diff --git a/packages/store/src/IStoreErrors.sol b/packages/store/src/IStoreErrors.sol index b0b25c651b..4b74b0e321 100644 --- a/packages/store/src/IStoreErrors.sol +++ b/packages/store/src/IStoreErrors.sol @@ -9,6 +9,7 @@ interface IStoreErrors { error StoreCore_NotImplemented(); error StoreCore_NotDynamicField(); error StoreCore_InvalidDataLength(uint256 expected, uint256 received); + error StoreCore_InvalidKeyNamesLength(uint256 expected, uint256 received); error StoreCore_InvalidFieldNamesLength(uint256 expected, uint256 received); error StoreCore_DataIndexOverflow(uint256 length, uint256 received); } diff --git a/packages/store/src/StoreCore.sol b/packages/store/src/StoreCore.sol index 14aabf2961..82e1b5005b 100644 --- a/packages/store/src/StoreCore.sol +++ b/packages/store/src/StoreCore.sol @@ -8,7 +8,7 @@ import { Memory } from "./Memory.sol"; import { Schema, SchemaLib } from "./Schema.sol"; import { PackedCounter } from "./PackedCounter.sol"; import { Slice, SliceLib } from "./Slice.sol"; -import { StoreMetadata, Hooks, HooksTableId } from "./codegen/Tables.sol"; +import { Hooks, Tables, HooksTableId } from "./codegen/Tables.sol"; import { IStoreErrors } from "./IStoreErrors.sol"; import { IStoreHook } from "./IStore.sol"; import { StoreSwitch } from "./StoreSwitch.sol"; @@ -32,34 +32,9 @@ library StoreCore { // StoreSwitch uses internal methods to write data instead of external calls. StoreSwitch.setStoreAddress(address(this)); - // Register internal schema table - SchemaType[] memory _valueSchema = new SchemaType[](2); - _valueSchema[0] = SchemaType.BYTES32; - _valueSchema[1] = SchemaType.BYTES32; - SchemaType[] memory _keySchema = new SchemaType[](1); - _keySchema[0] = SchemaType.BYTES32; - registerSchema( - StoreCoreInternal.SCHEMA_TABLE, - SchemaLib.encode(_valueSchema), // The Schema table's valueSchema is { valueSchema: BYTES32, keySchema: BYTES32 } - SchemaLib.encode(_keySchema) // The Schema table's keySchema is { tableId: BYTES32 } - ); - - // Register other internal tables - // - // For hooks and metadata tables, we need to register the schemas first and - // then their metadata. This is because setMetadata (a store set record) - // triggers a hook call, which uses getField, which will fail if the schema - // is not registered yet. - Hooks.registerSchema(); - StoreMetadata.registerSchema(); - Hooks.setMetadata(); - StoreMetadata.setMetadata(); - - // Set metadata for the schema table - string[] memory fieldNames = new string[](2); - fieldNames[0] = "valueSchema"; - fieldNames[1] = "keySchema"; - StoreMetadata.set(StoreCoreInternal.SCHEMA_TABLE, "schema", abi.encode(fieldNames)); + // Register internal tables + Tables.register(); + Hooks.register(); } /************************************************************************ @@ -71,8 +46,8 @@ library StoreCore { /** * Get the schema for the given tableId */ - function getSchema(bytes32 tableId) internal view returns (Schema valueSchema) { - valueSchema = StoreCoreInternal._getSchema(tableId); + function getValueSchema(bytes32 tableId) internal view returns (Schema valueSchema) { + valueSchema = Schema.wrap(Tables.getValueSchema(tableId)); if (valueSchema.isEmpty()) { revert IStoreErrors.StoreCore_TableNotFound(tableId, string(abi.encodePacked(tableId))); } @@ -82,7 +57,7 @@ library StoreCore { * Get the key schema for the given tableId */ function getKeySchema(bytes32 tableId) internal view returns (Schema keySchema) { - keySchema = StoreCoreInternal._getKeySchema(tableId); + keySchema = Schema.wrap(Tables.getKeySchema(tableId)); // key schemas can be empty for singleton tables, so we can't depend on key schema for table check if (!hasTable(tableId)) { revert IStoreErrors.StoreCore_TableNotFound(tableId, string(abi.encodePacked(tableId))); @@ -93,16 +68,32 @@ library StoreCore { * Check if a table with the given tableId exists */ function hasTable(bytes32 tableId) internal view returns (bool) { - return !StoreCoreInternal._getSchema(tableId).isEmpty(); + return Tables.getValueSchema(tableId) != bytes32(0); } /** - * Register a new tableId schema + * Register a new table with key schema, value schema, key names, and field names */ - function registerSchema(bytes32 tableId, Schema valueSchema, Schema keySchema) internal { + function registerTable( + bytes32 tableId, + Schema keySchema, + Schema valueSchema, + string[] memory keyNames, + string[] memory fieldNames + ) internal { // Verify the schema is valid - valueSchema.validate({ allowEmpty: false }); keySchema.validate({ allowEmpty: true }); + valueSchema.validate({ allowEmpty: false }); + + // Verify the number of key names corresponds to the key schema length + if (keyNames.length != keySchema.numFields()) { + revert IStoreErrors.StoreCore_InvalidKeyNamesLength(keySchema.numFields(), keyNames.length); + } + + // Verify the number of field names corresponds to the value schema length + if (fieldNames.length != valueSchema.numFields()) { + revert IStoreErrors.StoreCore_InvalidFieldNamesLength(valueSchema.numFields(), fieldNames.length); + } // Verify the schema doesn't exist yet if (hasTable(tableId)) { @@ -110,22 +101,13 @@ library StoreCore { } // Register the schema - StoreCoreInternal._registerSchemaUnchecked(tableId, valueSchema, keySchema); - } - - /** - * Set metadata for a given tableId - */ - function setMetadata(bytes32 tableId, string memory tableName, string[] memory fieldNames) internal { - Schema valueSchema = getSchema(tableId); - - // Verify the number of field names corresponds to the schema length - if (!(fieldNames.length == 0 || fieldNames.length == valueSchema.numFields())) { - revert IStoreErrors.StoreCore_InvalidFieldNamesLength(valueSchema.numFields(), fieldNames.length); - } - - // Set metadata - StoreMetadata.set(tableId, tableName, abi.encode(fieldNames)); + Tables.set( + tableId, + Schema.unwrap(keySchema), + Schema.unwrap(valueSchema), + abi.encode(keyNames), + abi.encode(fieldNames) + ); } /************************************************************************ @@ -537,41 +519,6 @@ library StoreCore { library StoreCoreInternal { bytes32 internal constant SLOT = keccak256("mud.store"); - bytes32 internal constant SCHEMA_TABLE = bytes32(abi.encodePacked(bytes16("mudstore"), bytes16("schema"))); - - /************************************************************************ - * - * SCHEMA - * - ************************************************************************/ - - function _getSchema(bytes32 tableId) internal view returns (Schema) { - bytes32[] memory key = new bytes32[](1); - key[0] = tableId; - uint256 location = StoreCoreInternal._getStaticDataLocation(SCHEMA_TABLE, key); - return Schema.wrap(Storage.load({ storagePointer: location })); - } - - function _getKeySchema(bytes32 tableId) internal view returns (Schema) { - bytes32[] memory key = new bytes32[](1); - key[0] = tableId; - uint256 location = StoreCoreInternal._getStaticDataLocation(SCHEMA_TABLE, key); - return Schema.wrap(Storage.load({ storagePointer: location + 1 })); - } - - /** - * Register a new tableId schema without validity checks - */ - function _registerSchemaUnchecked(bytes32 tableId, Schema valueSchema, Schema keySchema) internal { - bytes32[] memory key = new bytes32[](1); - key[0] = tableId; - uint256 location = _getStaticDataLocation(SCHEMA_TABLE, key); - Storage.store({ storagePointer: location, data: valueSchema.unwrap() }); - Storage.store({ storagePointer: location + 1, data: keySchema.unwrap() }); - - // Emit an event to notify indexers - emit StoreCore.StoreSetRecord(SCHEMA_TABLE, key, abi.encodePacked(valueSchema.unwrap(), keySchema.unwrap())); - } /************************************************************************ * diff --git a/packages/store/src/StoreRead.sol b/packages/store/src/StoreRead.sol index f15ba8a646..7c6ffe0a30 100644 --- a/packages/store/src/StoreRead.sol +++ b/packages/store/src/StoreRead.sol @@ -10,8 +10,8 @@ contract StoreRead is IStoreRead { StoreCore.initialize(); } - function getSchema(bytes32 table) public view virtual returns (Schema schema) { - schema = StoreCore.getSchema(table); + function getValueSchema(bytes32 table) public view virtual returns (Schema schema) { + schema = StoreCore.getValueSchema(table); } function getKeySchema(bytes32 table) public view virtual returns (Schema schema) { diff --git a/packages/store/src/StoreReadWithStubs.sol b/packages/store/src/StoreReadWithStubs.sol index 3da61bb020..ee655284f2 100644 --- a/packages/store/src/StoreReadWithStubs.sol +++ b/packages/store/src/StoreReadWithStubs.sol @@ -17,14 +17,7 @@ contract StoreReadWithStubs is IStore, StoreRead { /** * Not implemented in StoreReadWithStubs */ - function registerSchema(bytes32, Schema, Schema) public virtual { - revert StoreReadWithStubs_NotImplemented(); - } - - /** - * Not implemented in StoreReadWithStubs - */ - function setMetadata(bytes32, string calldata, string[] calldata) public virtual { + function registerTable(bytes32, Schema, Schema, string[] calldata, string[] calldata) public virtual { revert StoreReadWithStubs_NotImplemented(); } diff --git a/packages/store/src/StoreSwitch.sol b/packages/store/src/StoreSwitch.sol index 515a1bc16c..9350a80635 100644 --- a/packages/store/src/StoreSwitch.sol +++ b/packages/store/src/StoreSwitch.sol @@ -53,12 +53,12 @@ library StoreSwitch { } } - function getSchema(bytes32 table) internal view returns (Schema schema) { + function getValueSchema(bytes32 table) internal view returns (Schema valueSchema) { address _storeAddress = getStoreAddress(); if (_storeAddress == address(this)) { - schema = StoreCore.getSchema(table); + valueSchema = StoreCore.getValueSchema(table); } else { - schema = IStore(_storeAddress).getSchema(table); + valueSchema = IStore(_storeAddress).getValueSchema(table); } } @@ -71,21 +71,18 @@ library StoreSwitch { } } - function setMetadata(bytes32 table, string memory tableName, string[] memory fieldNames) internal { - address _storeAddress = getStoreAddress(); - if (_storeAddress == address(this)) { - StoreCore.setMetadata(table, tableName, fieldNames); - } else { - IStore(_storeAddress).setMetadata(table, tableName, fieldNames); - } - } - - function registerSchema(bytes32 table, Schema schema, Schema keySchema) internal { + function registerTable( + bytes32 table, + Schema keySchema, + Schema valueSchema, + string[] memory keyNames, + string[] memory fieldNames + ) internal { address _storeAddress = getStoreAddress(); if (_storeAddress == address(this)) { - StoreCore.registerSchema(table, schema, keySchema); + StoreCore.registerTable(table, keySchema, valueSchema, keyNames, fieldNames); } else { - IStore(_storeAddress).registerSchema(table, schema, keySchema); + IStore(_storeAddress).registerTable(table, keySchema, valueSchema, keyNames, fieldNames); } } diff --git a/packages/store/src/codegen/Tables.sol b/packages/store/src/codegen/Tables.sol index b4756a2d06..00c5e8a600 100644 --- a/packages/store/src/codegen/Tables.sol +++ b/packages/store/src/codegen/Tables.sol @@ -5,7 +5,7 @@ pragma solidity >=0.8.0; import { Hooks, HooksTableId } from "./tables/Hooks.sol"; import { Callbacks, CallbacksTableId } from "./tables/Callbacks.sol"; -import { StoreMetadata, StoreMetadataData, StoreMetadataTableId } from "./tables/StoreMetadata.sol"; +import { Tables, TablesData, TablesTableId } from "./tables/Tables.sol"; import { Mixed, MixedData, MixedTableId } from "./tables/Mixed.sol"; import { Vector2, Vector2Data, Vector2TableId } from "./tables/Vector2.sol"; import { KeyEncoding, KeyEncodingTableId } from "./tables/KeyEncoding.sol"; diff --git a/packages/store/src/codegen/tables/Callbacks.sol b/packages/store/src/codegen/tables/Callbacks.sol index 6ba8336ef2..9c074695b1 100644 --- a/packages/store/src/codegen/tables/Callbacks.sol +++ b/packages/store/src/codegen/tables/Callbacks.sol @@ -21,48 +21,42 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16("mudstore"), bytes1 bytes32 constant CallbacksTableId = _tableId; library Callbacks { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { + /** Get the table's key schema */ + function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BYTES24_ARRAY; + _schema[0] = SchemaType.BYTES32; return SchemaLib.encode(_schema); } - function getKeySchema() internal pure returns (Schema) { + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BYTES32; + _schema[0] = SchemaType.BYTES24_ARRAY; return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "value"; - return ("Callbacks", _fieldNames); - } - - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "key"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "value"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ @@ -70,7 +64,7 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes24()); } @@ -79,7 +73,7 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes24()); } @@ -88,7 +82,7 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getValueSchema()); } /** Set value (using the specified store) */ @@ -96,7 +90,7 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getValueSchema()); } /** Get the length of value */ @@ -104,7 +98,7 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); unchecked { return _byteLength / 24; } @@ -115,7 +109,7 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); unchecked { return _byteLength / 24; } @@ -134,7 +128,7 @@ library Callbacks { _tableId, _keyTuple, 0, - getSchema(), + getValueSchema(), _index * 24, (_index + 1) * 24 ); @@ -151,7 +145,14 @@ library Callbacks { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 24, (_index + 1) * 24); + bytes memory _blob = _store.getFieldSlice( + _tableId, + _keyTuple, + 0, + getValueSchema(), + _index * 24, + (_index + 1) * 24 + ); return (Bytes.slice24(_blob, 0)); } } @@ -161,7 +162,7 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to value (using the specified store) */ @@ -169,7 +170,7 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from value */ @@ -177,7 +178,7 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 24, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 0, 24, getValueSchema()); } /** Pop an element from value (using the specified store) */ @@ -185,7 +186,7 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 0, 24, getSchema()); + _store.popFromField(_tableId, _keyTuple, 0, 24, getValueSchema()); } /** @@ -197,7 +198,7 @@ library Callbacks { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 24, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 24, abi.encodePacked((_element)), getValueSchema()); } } @@ -210,7 +211,7 @@ library Callbacks { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 0, _index * 24, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 0, _index * 24, abi.encodePacked((_element)), getValueSchema()); } } @@ -236,7 +237,7 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -244,6 +245,6 @@ library Callbacks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/store/src/codegen/tables/Hooks.sol b/packages/store/src/codegen/tables/Hooks.sol index 23168cf52b..018eb88dd6 100644 --- a/packages/store/src/codegen/tables/Hooks.sol +++ b/packages/store/src/codegen/tables/Hooks.sol @@ -21,48 +21,42 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16("mudstore"), bytes1 bytes32 constant HooksTableId = _tableId; library Hooks { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { + /** Get the table's key schema */ + function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.ADDRESS_ARRAY; + _schema[0] = SchemaType.BYTES32; return SchemaLib.encode(_schema); } - function getKeySchema() internal pure returns (Schema) { + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BYTES32; + _schema[0] = SchemaType.ADDRESS_ARRAY; return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "value"; - return ("Hooks", _fieldNames); - } - - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "key"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "value"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ @@ -70,7 +64,7 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_address()); } @@ -79,7 +73,7 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_address()); } @@ -88,7 +82,7 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getValueSchema()); } /** Set value (using the specified store) */ @@ -96,7 +90,7 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getValueSchema()); } /** Get the length of value */ @@ -104,7 +98,7 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); unchecked { return _byteLength / 20; } @@ -115,7 +109,7 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); unchecked { return _byteLength / 20; } @@ -134,7 +128,7 @@ library Hooks { _tableId, _keyTuple, 0, - getSchema(), + getValueSchema(), _index * 20, (_index + 1) * 20 ); @@ -151,7 +145,14 @@ library Hooks { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 20, (_index + 1) * 20); + bytes memory _blob = _store.getFieldSlice( + _tableId, + _keyTuple, + 0, + getValueSchema(), + _index * 20, + (_index + 1) * 20 + ); return (address(Bytes.slice20(_blob, 0))); } } @@ -161,7 +162,7 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to value (using the specified store) */ @@ -169,7 +170,7 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from value */ @@ -177,7 +178,7 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 20, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 0, 20, getValueSchema()); } /** Pop an element from value (using the specified store) */ @@ -185,7 +186,7 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 0, 20, getSchema()); + _store.popFromField(_tableId, _keyTuple, 0, 20, getValueSchema()); } /** @@ -197,7 +198,7 @@ library Hooks { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), getValueSchema()); } } @@ -210,7 +211,7 @@ library Hooks { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), getValueSchema()); } } @@ -236,7 +237,7 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -244,6 +245,6 @@ library Hooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/store/src/codegen/tables/KeyEncoding.sol b/packages/store/src/codegen/tables/KeyEncoding.sol index b9867fe1d2..a0fb759651 100644 --- a/packages/store/src/codegen/tables/KeyEncoding.sol +++ b/packages/store/src/codegen/tables/KeyEncoding.sol @@ -24,14 +24,7 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16("mudstore"), bytes1 bytes32 constant KeyEncodingTableId = _tableId; library KeyEncoding { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BOOL; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](6); _schema[0] = SchemaType.UINT256; @@ -44,33 +37,39 @@ library KeyEncoding { return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "value"; - return ("KeyEncoding", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](1); + _schema[0] = SchemaType.BOOL; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](6); + keyNames[0] = "k1"; + keyNames[1] = "k2"; + keyNames[2] = "k3"; + keyNames[3] = "k4"; + keyNames[4] = "k5"; + keyNames[5] = "k6"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "value"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ @@ -90,7 +89,7 @@ library KeyEncoding { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -112,7 +111,7 @@ library KeyEncoding { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -126,7 +125,7 @@ library KeyEncoding { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getValueSchema()); } /** Set value (using the specified store) */ @@ -148,7 +147,7 @@ library KeyEncoding { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getValueSchema()); } /** Tightly pack full data using this table's schema */ @@ -186,7 +185,7 @@ library KeyEncoding { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -199,7 +198,7 @@ library KeyEncoding { _keyTuple[4] = _boolToBytes32(k5); _keyTuple[5] = bytes32(uint256(uint8(k6))); - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/store/src/codegen/tables/Mixed.sol b/packages/store/src/codegen/tables/Mixed.sol index cf039016a3..acc2f23fae 100644 --- a/packages/store/src/codegen/tables/Mixed.sol +++ b/packages/store/src/codegen/tables/Mixed.sol @@ -28,17 +28,7 @@ struct MixedData { } library Mixed { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](4); - _schema[0] = SchemaType.UINT32; - _schema[1] = SchemaType.UINT128; - _schema[2] = SchemaType.UINT32_ARRAY; - _schema[3] = SchemaType.STRING; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); _schema[0] = SchemaType.BYTES32; @@ -46,36 +36,40 @@ library Mixed { return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](4); - _fieldNames[0] = "u32"; - _fieldNames[1] = "u128"; - _fieldNames[2] = "a32"; - _fieldNames[3] = "s"; - return ("Mixed", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](4); + _schema[0] = SchemaType.UINT32; + _schema[1] = SchemaType.UINT128; + _schema[2] = SchemaType.UINT32_ARRAY; + _schema[3] = SchemaType.STRING; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "key"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](4); + fieldNames[0] = "u32"; + fieldNames[1] = "u128"; + fieldNames[2] = "a32"; + fieldNames[3] = "s"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get u32 */ @@ -83,7 +77,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (uint32(Bytes.slice4(_blob, 0))); } @@ -92,7 +86,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (uint32(Bytes.slice4(_blob, 0))); } @@ -101,7 +95,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((u32)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((u32)), getValueSchema()); } /** Set u32 (using the specified store) */ @@ -109,7 +103,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((u32)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((u32)), getValueSchema()); } /** Get u128 */ @@ -117,7 +111,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getValueSchema()); return (uint128(Bytes.slice16(_blob, 0))); } @@ -126,7 +120,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getValueSchema()); return (uint128(Bytes.slice16(_blob, 0))); } @@ -135,7 +129,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((u128)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((u128)), getValueSchema()); } /** Set u128 (using the specified store) */ @@ -143,7 +137,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((u128)), getSchema()); + _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((u128)), getValueSchema()); } /** Get a32 */ @@ -151,7 +145,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } @@ -160,7 +154,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } @@ -169,7 +163,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 2, EncodeArray.encode((a32)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 2, EncodeArray.encode((a32)), getValueSchema()); } /** Set a32 (using the specified store) */ @@ -177,7 +171,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 2, EncodeArray.encode((a32)), getSchema()); + _store.setField(_tableId, _keyTuple, 2, EncodeArray.encode((a32)), getValueSchema()); } /** Get the length of a32 */ @@ -185,7 +179,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 2, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 2, getValueSchema()); unchecked { return _byteLength / 4; } @@ -196,7 +190,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 2, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 2, getValueSchema()); unchecked { return _byteLength / 4; } @@ -211,7 +205,14 @@ library Mixed { _keyTuple[0] = key; unchecked { - bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 2, getSchema(), _index * 4, (_index + 1) * 4); + bytes memory _blob = StoreSwitch.getFieldSlice( + _tableId, + _keyTuple, + 2, + getValueSchema(), + _index * 4, + (_index + 1) * 4 + ); return (uint32(Bytes.slice4(_blob, 0))); } } @@ -225,7 +226,7 @@ library Mixed { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 2, getSchema(), _index * 4, (_index + 1) * 4); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 2, getValueSchema(), _index * 4, (_index + 1) * 4); return (uint32(Bytes.slice4(_blob, 0))); } } @@ -235,7 +236,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to a32 (using the specified store) */ @@ -243,7 +244,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from a32 */ @@ -251,7 +252,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 2, 4, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 2, 4, getValueSchema()); } /** Pop an element from a32 (using the specified store) */ @@ -259,7 +260,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 2, 4, getSchema()); + _store.popFromField(_tableId, _keyTuple, 2, 4, getValueSchema()); } /** @@ -271,7 +272,7 @@ library Mixed { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 2, _index * 4, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 2, _index * 4, abi.encodePacked((_element)), getValueSchema()); } } @@ -284,7 +285,7 @@ library Mixed { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 2, _index * 4, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 2, _index * 4, abi.encodePacked((_element)), getValueSchema()); } } @@ -293,7 +294,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, getValueSchema()); return (string(_blob)); } @@ -302,7 +303,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, getValueSchema()); return (string(_blob)); } @@ -311,7 +312,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 3, bytes((s)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 3, bytes((s)), getValueSchema()); } /** Set s (using the specified store) */ @@ -319,7 +320,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 3, bytes((s)), getSchema()); + _store.setField(_tableId, _keyTuple, 3, bytes((s)), getValueSchema()); } /** Get the length of s */ @@ -327,7 +328,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 3, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 3, getValueSchema()); unchecked { return _byteLength / 1; } @@ -338,7 +339,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 3, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 3, getValueSchema()); unchecked { return _byteLength / 1; } @@ -353,7 +354,14 @@ library Mixed { _keyTuple[0] = key; unchecked { - bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 3, getSchema(), _index * 1, (_index + 1) * 1); + bytes memory _blob = StoreSwitch.getFieldSlice( + _tableId, + _keyTuple, + 3, + getValueSchema(), + _index * 1, + (_index + 1) * 1 + ); return (string(_blob)); } } @@ -367,7 +375,7 @@ library Mixed { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 3, getSchema(), _index * 1, (_index + 1) * 1); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 3, getValueSchema(), _index * 1, (_index + 1) * 1); return (string(_blob)); } } @@ -377,7 +385,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 3, bytes((_slice)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 3, bytes((_slice)), getValueSchema()); } /** Push a slice to s (using the specified store) */ @@ -385,7 +393,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 3, bytes((_slice)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 3, bytes((_slice)), getValueSchema()); } /** Pop a slice from s */ @@ -393,7 +401,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 3, 1, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 3, 1, getValueSchema()); } /** Pop a slice from s (using the specified store) */ @@ -401,7 +409,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 3, 1, getSchema()); + _store.popFromField(_tableId, _keyTuple, 3, 1, getValueSchema()); } /** @@ -413,7 +421,7 @@ library Mixed { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 3, _index * 1, bytes((_slice)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 3, _index * 1, bytes((_slice)), getValueSchema()); } } @@ -426,7 +434,7 @@ library Mixed { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 3, _index * 1, bytes((_slice)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 3, _index * 1, bytes((_slice)), getValueSchema()); } } @@ -435,7 +443,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -444,7 +452,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -455,7 +463,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setRecord(_tableId, _keyTuple, _data, getSchema()); + StoreSwitch.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using individual values (using the specified store) */ @@ -465,7 +473,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setRecord(_tableId, _keyTuple, _data, getSchema()); + _store.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using the data struct */ @@ -531,7 +539,7 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -539,6 +547,6 @@ library Mixed { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/store/src/codegen/tables/StoreMetadata.sol b/packages/store/src/codegen/tables/StoreMetadata.sol deleted file mode 100644 index e37ece34cb..0000000000 --- a/packages/store/src/codegen/tables/StoreMetadata.sol +++ /dev/null @@ -1,473 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity >=0.8.0; - -/* Autogenerated file. Do not edit manually. */ - -// Import schema type -import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol"; - -// Import store internals -import { IStore } from "../../IStore.sol"; -import { StoreSwitch } from "../../StoreSwitch.sol"; -import { StoreCore } from "../../StoreCore.sol"; -import { Bytes } from "../../Bytes.sol"; -import { Memory } from "../../Memory.sol"; -import { SliceLib } from "../../Slice.sol"; -import { EncodeArray } from "../../tightcoder/EncodeArray.sol"; -import { Schema, SchemaLib } from "../../Schema.sol"; -import { PackedCounter, PackedCounterLib } from "../../PackedCounter.sol"; - -bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16("mudstore"), bytes16("StoreMetadata"))); -bytes32 constant StoreMetadataTableId = _tableId; - -struct StoreMetadataData { - string tableName; - bytes abiEncodedFieldNames; -} - -library StoreMetadata { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](2); - _schema[0] = SchemaType.STRING; - _schema[1] = SchemaType.BYTES; - - return SchemaLib.encode(_schema); - } - - function getKeySchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BYTES32; - - return SchemaLib.encode(_schema); - } - - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](2); - _fieldNames[0] = "tableName"; - _fieldNames[1] = "abiEncodedFieldNames"; - return ("StoreMetadata", _fieldNames); - } - - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); - } - - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); - } - - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); - } - - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); - } - - /** Get tableName */ - function getTableName(bytes32 tableId) internal view returns (string memory tableName) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); - return (string(_blob)); - } - - /** Get tableName (using the specified store) */ - function getTableName(IStore _store, bytes32 tableId) internal view returns (string memory tableName) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); - return (string(_blob)); - } - - /** Set tableName */ - function setTableName(bytes32 tableId, string memory tableName) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - StoreSwitch.setField(_tableId, _keyTuple, 0, bytes((tableName)), getSchema()); - } - - /** Set tableName (using the specified store) */ - function setTableName(IStore _store, bytes32 tableId, string memory tableName) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - _store.setField(_tableId, _keyTuple, 0, bytes((tableName)), getSchema()); - } - - /** Get the length of tableName */ - function lengthTableName(bytes32 tableId) internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getSchema()); - unchecked { - return _byteLength / 1; - } - } - - /** Get the length of tableName (using the specified store) */ - function lengthTableName(IStore _store, bytes32 tableId) internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getSchema()); - unchecked { - return _byteLength / 1; - } - } - - /** - * Get an item of tableName - * (unchecked, returns invalid data if index overflows) - */ - function getItemTableName(bytes32 tableId, uint256 _index) internal view returns (string memory) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - unchecked { - bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 1, (_index + 1) * 1); - return (string(_blob)); - } - } - - /** - * Get an item of tableName (using the specified store) - * (unchecked, returns invalid data if index overflows) - */ - function getItemTableName(IStore _store, bytes32 tableId, uint256 _index) internal view returns (string memory) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 1, (_index + 1) * 1); - return (string(_blob)); - } - } - - /** Push a slice to tableName */ - function pushTableName(bytes32 tableId, string memory _slice) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - StoreSwitch.pushToField(_tableId, _keyTuple, 0, bytes((_slice)), getSchema()); - } - - /** Push a slice to tableName (using the specified store) */ - function pushTableName(IStore _store, bytes32 tableId, string memory _slice) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - _store.pushToField(_tableId, _keyTuple, 0, bytes((_slice)), getSchema()); - } - - /** Pop a slice from tableName */ - function popTableName(bytes32 tableId) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 1, getSchema()); - } - - /** Pop a slice from tableName (using the specified store) */ - function popTableName(IStore _store, bytes32 tableId) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - _store.popFromField(_tableId, _keyTuple, 0, 1, getSchema()); - } - - /** - * Update a slice of tableName at `_index` - * (checked only to prevent modifying other tables; can corrupt own data if index overflows) - */ - function updateTableName(bytes32 tableId, uint256 _index, string memory _slice) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 1, bytes((_slice)), getSchema()); - } - } - - /** - * Update a slice of tableName (using the specified store) at `_index` - * (checked only to prevent modifying other tables; can corrupt own data if index overflows) - */ - function updateTableName(IStore _store, bytes32 tableId, uint256 _index, string memory _slice) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - unchecked { - _store.updateInField(_tableId, _keyTuple, 0, _index * 1, bytes((_slice)), getSchema()); - } - } - - /** Get abiEncodedFieldNames */ - function getAbiEncodedFieldNames(bytes32 tableId) internal view returns (bytes memory abiEncodedFieldNames) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getSchema()); - return (bytes(_blob)); - } - - /** Get abiEncodedFieldNames (using the specified store) */ - function getAbiEncodedFieldNames( - IStore _store, - bytes32 tableId - ) internal view returns (bytes memory abiEncodedFieldNames) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getSchema()); - return (bytes(_blob)); - } - - /** Set abiEncodedFieldNames */ - function setAbiEncodedFieldNames(bytes32 tableId, bytes memory abiEncodedFieldNames) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - StoreSwitch.setField(_tableId, _keyTuple, 1, bytes((abiEncodedFieldNames)), getSchema()); - } - - /** Set abiEncodedFieldNames (using the specified store) */ - function setAbiEncodedFieldNames(IStore _store, bytes32 tableId, bytes memory abiEncodedFieldNames) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - _store.setField(_tableId, _keyTuple, 1, bytes((abiEncodedFieldNames)), getSchema()); - } - - /** Get the length of abiEncodedFieldNames */ - function lengthAbiEncodedFieldNames(bytes32 tableId) internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 1, getSchema()); - unchecked { - return _byteLength / 1; - } - } - - /** Get the length of abiEncodedFieldNames (using the specified store) */ - function lengthAbiEncodedFieldNames(IStore _store, bytes32 tableId) internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 1, getSchema()); - unchecked { - return _byteLength / 1; - } - } - - /** - * Get an item of abiEncodedFieldNames - * (unchecked, returns invalid data if index overflows) - */ - function getItemAbiEncodedFieldNames(bytes32 tableId, uint256 _index) internal view returns (bytes memory) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - unchecked { - bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 1, getSchema(), _index * 1, (_index + 1) * 1); - return (bytes(_blob)); - } - } - - /** - * Get an item of abiEncodedFieldNames (using the specified store) - * (unchecked, returns invalid data if index overflows) - */ - function getItemAbiEncodedFieldNames( - IStore _store, - bytes32 tableId, - uint256 _index - ) internal view returns (bytes memory) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 1, getSchema(), _index * 1, (_index + 1) * 1); - return (bytes(_blob)); - } - } - - /** Push a slice to abiEncodedFieldNames */ - function pushAbiEncodedFieldNames(bytes32 tableId, bytes memory _slice) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - StoreSwitch.pushToField(_tableId, _keyTuple, 1, bytes((_slice)), getSchema()); - } - - /** Push a slice to abiEncodedFieldNames (using the specified store) */ - function pushAbiEncodedFieldNames(IStore _store, bytes32 tableId, bytes memory _slice) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - _store.pushToField(_tableId, _keyTuple, 1, bytes((_slice)), getSchema()); - } - - /** Pop a slice from abiEncodedFieldNames */ - function popAbiEncodedFieldNames(bytes32 tableId) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - StoreSwitch.popFromField(_tableId, _keyTuple, 1, 1, getSchema()); - } - - /** Pop a slice from abiEncodedFieldNames (using the specified store) */ - function popAbiEncodedFieldNames(IStore _store, bytes32 tableId) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - _store.popFromField(_tableId, _keyTuple, 1, 1, getSchema()); - } - - /** - * Update a slice of abiEncodedFieldNames at `_index` - * (checked only to prevent modifying other tables; can corrupt own data if index overflows) - */ - function updateAbiEncodedFieldNames(bytes32 tableId, uint256 _index, bytes memory _slice) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 1, _index * 1, bytes((_slice)), getSchema()); - } - } - - /** - * Update a slice of abiEncodedFieldNames (using the specified store) at `_index` - * (checked only to prevent modifying other tables; can corrupt own data if index overflows) - */ - function updateAbiEncodedFieldNames(IStore _store, bytes32 tableId, uint256 _index, bytes memory _slice) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - unchecked { - _store.updateInField(_tableId, _keyTuple, 1, _index * 1, bytes((_slice)), getSchema()); - } - } - - /** Get the full data */ - function get(bytes32 tableId) internal view returns (StoreMetadataData memory _table) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getSchema()); - return decode(_blob); - } - - /** Get the full data (using the specified store) */ - function get(IStore _store, bytes32 tableId) internal view returns (StoreMetadataData memory _table) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getSchema()); - return decode(_blob); - } - - /** Set the full data using individual values */ - function set(bytes32 tableId, string memory tableName, bytes memory abiEncodedFieldNames) internal { - bytes memory _data = encode(tableName, abiEncodedFieldNames); - - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - StoreSwitch.setRecord(_tableId, _keyTuple, _data, getSchema()); - } - - /** Set the full data using individual values (using the specified store) */ - function set(IStore _store, bytes32 tableId, string memory tableName, bytes memory abiEncodedFieldNames) internal { - bytes memory _data = encode(tableName, abiEncodedFieldNames); - - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - _store.setRecord(_tableId, _keyTuple, _data, getSchema()); - } - - /** Set the full data using the data struct */ - function set(bytes32 tableId, StoreMetadataData memory _table) internal { - set(tableId, _table.tableName, _table.abiEncodedFieldNames); - } - - /** Set the full data using the data struct (using the specified store) */ - function set(IStore _store, bytes32 tableId, StoreMetadataData memory _table) internal { - set(_store, tableId, _table.tableName, _table.abiEncodedFieldNames); - } - - /** - * Decode the tightly packed blob using this table's schema. - * Undefined behaviour for invalid blobs. - */ - function decode(bytes memory _blob) internal pure returns (StoreMetadataData memory _table) { - // 0 is the total byte length of static data - PackedCounter _encodedLengths = PackedCounter.wrap(Bytes.slice32(_blob, 0)); - - // Store trims the blob if dynamic fields are all empty - if (_blob.length > 0) { - // skip static data length + dynamic lengths word - uint256 _start = 32; - uint256 _end; - unchecked { - _end = 32 + _encodedLengths.atIndex(0); - } - _table.tableName = (string(SliceLib.getSubslice(_blob, _start, _end).toBytes())); - - _start = _end; - unchecked { - _end += _encodedLengths.atIndex(1); - } - _table.abiEncodedFieldNames = (bytes(SliceLib.getSubslice(_blob, _start, _end).toBytes())); - } - } - - /** Tightly pack full data using this table's schema */ - function encode(string memory tableName, bytes memory abiEncodedFieldNames) internal pure returns (bytes memory) { - uint40[] memory _counters = new uint40[](2); - _counters[0] = uint40(bytes(tableName).length); - _counters[1] = uint40(bytes(abiEncodedFieldNames).length); - PackedCounter _encodedLengths = PackedCounterLib.pack(_counters); - - return abi.encodePacked(_encodedLengths.unwrap(), bytes((tableName)), bytes((abiEncodedFieldNames))); - } - - /** Encode keys as a bytes32 array using this table's schema */ - function encodeKeyTuple(bytes32 tableId) internal pure returns (bytes32[] memory) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - return _keyTuple; - } - - /* Delete all data for given keys */ - function deleteRecord(bytes32 tableId) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); - } - - /* Delete all data for given keys (using the specified store) */ - function deleteRecord(IStore _store, bytes32 tableId) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; - - _store.deleteRecord(_tableId, _keyTuple, getSchema()); - } -} diff --git a/packages/store/src/codegen/tables/Tables.sol b/packages/store/src/codegen/tables/Tables.sol new file mode 100644 index 0000000000..fca77f4ba8 --- /dev/null +++ b/packages/store/src/codegen/tables/Tables.sol @@ -0,0 +1,591 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.8.0; + +/* Autogenerated file. Do not edit manually. */ + +// Import schema type +import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol"; + +// Import store internals +import { IStore } from "../../IStore.sol"; +import { StoreSwitch } from "../../StoreSwitch.sol"; +import { StoreCore } from "../../StoreCore.sol"; +import { Bytes } from "../../Bytes.sol"; +import { Memory } from "../../Memory.sol"; +import { SliceLib } from "../../Slice.sol"; +import { EncodeArray } from "../../tightcoder/EncodeArray.sol"; +import { Schema, SchemaLib } from "../../Schema.sol"; +import { PackedCounter, PackedCounterLib } from "../../PackedCounter.sol"; + +bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16("mudstore"), bytes16("Tables"))); +bytes32 constant TablesTableId = _tableId; + +struct TablesData { + bytes32 keySchema; + bytes32 valueSchema; + bytes abiEncodedKeyNames; + bytes abiEncodedFieldNames; +} + +library Tables { + /** Get the table's key schema */ + function getKeySchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](1); + _schema[0] = SchemaType.BYTES32; + + return SchemaLib.encode(_schema); + } + + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](4); + _schema[0] = SchemaType.BYTES32; + _schema[1] = SchemaType.BYTES32; + _schema[2] = SchemaType.BYTES; + _schema[3] = SchemaType.BYTES; + + return SchemaLib.encode(_schema); + } + + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "tableId"; + } + + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](4); + fieldNames[0] = "keySchema"; + fieldNames[1] = "valueSchema"; + fieldNames[2] = "abiEncodedKeyNames"; + fieldNames[3] = "abiEncodedFieldNames"; + } + + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + + /** Get keySchema */ + function getKeySchema(bytes32 tableId) internal view returns (bytes32 keySchema) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); + return (Bytes.slice32(_blob, 0)); + } + + /** Get keySchema (using the specified store) */ + function getKeySchema(IStore _store, bytes32 tableId) internal view returns (bytes32 keySchema) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); + return (Bytes.slice32(_blob, 0)); + } + + /** Set keySchema */ + function setKeySchema(bytes32 tableId, bytes32 keySchema) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((keySchema)), getValueSchema()); + } + + /** Set keySchema (using the specified store) */ + function setKeySchema(IStore _store, bytes32 tableId, bytes32 keySchema) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((keySchema)), getValueSchema()); + } + + /** Get valueSchema */ + function getValueSchema(bytes32 tableId) internal view returns (bytes32 valueSchema) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getValueSchema()); + return (Bytes.slice32(_blob, 0)); + } + + /** Get valueSchema (using the specified store) */ + function getValueSchema(IStore _store, bytes32 tableId) internal view returns (bytes32 valueSchema) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getValueSchema()); + return (Bytes.slice32(_blob, 0)); + } + + /** Set valueSchema */ + function setValueSchema(bytes32 tableId, bytes32 valueSchema) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((valueSchema)), getValueSchema()); + } + + /** Set valueSchema (using the specified store) */ + function setValueSchema(IStore _store, bytes32 tableId, bytes32 valueSchema) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((valueSchema)), getValueSchema()); + } + + /** Get abiEncodedKeyNames */ + function getAbiEncodedKeyNames(bytes32 tableId) internal view returns (bytes memory abiEncodedKeyNames) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getValueSchema()); + return (bytes(_blob)); + } + + /** Get abiEncodedKeyNames (using the specified store) */ + function getAbiEncodedKeyNames( + IStore _store, + bytes32 tableId + ) internal view returns (bytes memory abiEncodedKeyNames) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getValueSchema()); + return (bytes(_blob)); + } + + /** Set abiEncodedKeyNames */ + function setAbiEncodedKeyNames(bytes32 tableId, bytes memory abiEncodedKeyNames) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + StoreSwitch.setField(_tableId, _keyTuple, 2, bytes((abiEncodedKeyNames)), getValueSchema()); + } + + /** Set abiEncodedKeyNames (using the specified store) */ + function setAbiEncodedKeyNames(IStore _store, bytes32 tableId, bytes memory abiEncodedKeyNames) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + _store.setField(_tableId, _keyTuple, 2, bytes((abiEncodedKeyNames)), getValueSchema()); + } + + /** Get the length of abiEncodedKeyNames */ + function lengthAbiEncodedKeyNames(bytes32 tableId) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 2, getValueSchema()); + unchecked { + return _byteLength / 1; + } + } + + /** Get the length of abiEncodedKeyNames (using the specified store) */ + function lengthAbiEncodedKeyNames(IStore _store, bytes32 tableId) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 2, getValueSchema()); + unchecked { + return _byteLength / 1; + } + } + + /** + * Get an item of abiEncodedKeyNames + * (unchecked, returns invalid data if index overflows) + */ + function getItemAbiEncodedKeyNames(bytes32 tableId, uint256 _index) internal view returns (bytes memory) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + unchecked { + bytes memory _blob = StoreSwitch.getFieldSlice( + _tableId, + _keyTuple, + 2, + getValueSchema(), + _index * 1, + (_index + 1) * 1 + ); + return (bytes(_blob)); + } + } + + /** + * Get an item of abiEncodedKeyNames (using the specified store) + * (unchecked, returns invalid data if index overflows) + */ + function getItemAbiEncodedKeyNames( + IStore _store, + bytes32 tableId, + uint256 _index + ) internal view returns (bytes memory) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + unchecked { + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 2, getValueSchema(), _index * 1, (_index + 1) * 1); + return (bytes(_blob)); + } + } + + /** Push a slice to abiEncodedKeyNames */ + function pushAbiEncodedKeyNames(bytes32 tableId, bytes memory _slice) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + StoreSwitch.pushToField(_tableId, _keyTuple, 2, bytes((_slice)), getValueSchema()); + } + + /** Push a slice to abiEncodedKeyNames (using the specified store) */ + function pushAbiEncodedKeyNames(IStore _store, bytes32 tableId, bytes memory _slice) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + _store.pushToField(_tableId, _keyTuple, 2, bytes((_slice)), getValueSchema()); + } + + /** Pop a slice from abiEncodedKeyNames */ + function popAbiEncodedKeyNames(bytes32 tableId) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + StoreSwitch.popFromField(_tableId, _keyTuple, 2, 1, getValueSchema()); + } + + /** Pop a slice from abiEncodedKeyNames (using the specified store) */ + function popAbiEncodedKeyNames(IStore _store, bytes32 tableId) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + _store.popFromField(_tableId, _keyTuple, 2, 1, getValueSchema()); + } + + /** + * Update a slice of abiEncodedKeyNames at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function updateAbiEncodedKeyNames(bytes32 tableId, uint256 _index, bytes memory _slice) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + unchecked { + StoreSwitch.updateInField(_tableId, _keyTuple, 2, _index * 1, bytes((_slice)), getValueSchema()); + } + } + + /** + * Update a slice of abiEncodedKeyNames (using the specified store) at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function updateAbiEncodedKeyNames(IStore _store, bytes32 tableId, uint256 _index, bytes memory _slice) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + unchecked { + _store.updateInField(_tableId, _keyTuple, 2, _index * 1, bytes((_slice)), getValueSchema()); + } + } + + /** Get abiEncodedFieldNames */ + function getAbiEncodedFieldNames(bytes32 tableId) internal view returns (bytes memory abiEncodedFieldNames) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, getValueSchema()); + return (bytes(_blob)); + } + + /** Get abiEncodedFieldNames (using the specified store) */ + function getAbiEncodedFieldNames( + IStore _store, + bytes32 tableId + ) internal view returns (bytes memory abiEncodedFieldNames) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, getValueSchema()); + return (bytes(_blob)); + } + + /** Set abiEncodedFieldNames */ + function setAbiEncodedFieldNames(bytes32 tableId, bytes memory abiEncodedFieldNames) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + StoreSwitch.setField(_tableId, _keyTuple, 3, bytes((abiEncodedFieldNames)), getValueSchema()); + } + + /** Set abiEncodedFieldNames (using the specified store) */ + function setAbiEncodedFieldNames(IStore _store, bytes32 tableId, bytes memory abiEncodedFieldNames) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + _store.setField(_tableId, _keyTuple, 3, bytes((abiEncodedFieldNames)), getValueSchema()); + } + + /** Get the length of abiEncodedFieldNames */ + function lengthAbiEncodedFieldNames(bytes32 tableId) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 3, getValueSchema()); + unchecked { + return _byteLength / 1; + } + } + + /** Get the length of abiEncodedFieldNames (using the specified store) */ + function lengthAbiEncodedFieldNames(IStore _store, bytes32 tableId) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 3, getValueSchema()); + unchecked { + return _byteLength / 1; + } + } + + /** + * Get an item of abiEncodedFieldNames + * (unchecked, returns invalid data if index overflows) + */ + function getItemAbiEncodedFieldNames(bytes32 tableId, uint256 _index) internal view returns (bytes memory) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + unchecked { + bytes memory _blob = StoreSwitch.getFieldSlice( + _tableId, + _keyTuple, + 3, + getValueSchema(), + _index * 1, + (_index + 1) * 1 + ); + return (bytes(_blob)); + } + } + + /** + * Get an item of abiEncodedFieldNames (using the specified store) + * (unchecked, returns invalid data if index overflows) + */ + function getItemAbiEncodedFieldNames( + IStore _store, + bytes32 tableId, + uint256 _index + ) internal view returns (bytes memory) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + unchecked { + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 3, getValueSchema(), _index * 1, (_index + 1) * 1); + return (bytes(_blob)); + } + } + + /** Push a slice to abiEncodedFieldNames */ + function pushAbiEncodedFieldNames(bytes32 tableId, bytes memory _slice) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + StoreSwitch.pushToField(_tableId, _keyTuple, 3, bytes((_slice)), getValueSchema()); + } + + /** Push a slice to abiEncodedFieldNames (using the specified store) */ + function pushAbiEncodedFieldNames(IStore _store, bytes32 tableId, bytes memory _slice) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + _store.pushToField(_tableId, _keyTuple, 3, bytes((_slice)), getValueSchema()); + } + + /** Pop a slice from abiEncodedFieldNames */ + function popAbiEncodedFieldNames(bytes32 tableId) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + StoreSwitch.popFromField(_tableId, _keyTuple, 3, 1, getValueSchema()); + } + + /** Pop a slice from abiEncodedFieldNames (using the specified store) */ + function popAbiEncodedFieldNames(IStore _store, bytes32 tableId) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + _store.popFromField(_tableId, _keyTuple, 3, 1, getValueSchema()); + } + + /** + * Update a slice of abiEncodedFieldNames at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function updateAbiEncodedFieldNames(bytes32 tableId, uint256 _index, bytes memory _slice) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + unchecked { + StoreSwitch.updateInField(_tableId, _keyTuple, 3, _index * 1, bytes((_slice)), getValueSchema()); + } + } + + /** + * Update a slice of abiEncodedFieldNames (using the specified store) at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function updateAbiEncodedFieldNames(IStore _store, bytes32 tableId, uint256 _index, bytes memory _slice) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + unchecked { + _store.updateInField(_tableId, _keyTuple, 3, _index * 1, bytes((_slice)), getValueSchema()); + } + } + + /** Get the full data */ + function get(bytes32 tableId) internal view returns (TablesData memory _table) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getValueSchema()); + return decode(_blob); + } + + /** Get the full data (using the specified store) */ + function get(IStore _store, bytes32 tableId) internal view returns (TablesData memory _table) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getValueSchema()); + return decode(_blob); + } + + /** Set the full data using individual values */ + function set( + bytes32 tableId, + bytes32 keySchema, + bytes32 valueSchema, + bytes memory abiEncodedKeyNames, + bytes memory abiEncodedFieldNames + ) internal { + bytes memory _data = encode(keySchema, valueSchema, abiEncodedKeyNames, abiEncodedFieldNames); + + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + StoreSwitch.setRecord(_tableId, _keyTuple, _data, getValueSchema()); + } + + /** Set the full data using individual values (using the specified store) */ + function set( + IStore _store, + bytes32 tableId, + bytes32 keySchema, + bytes32 valueSchema, + bytes memory abiEncodedKeyNames, + bytes memory abiEncodedFieldNames + ) internal { + bytes memory _data = encode(keySchema, valueSchema, abiEncodedKeyNames, abiEncodedFieldNames); + + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + _store.setRecord(_tableId, _keyTuple, _data, getValueSchema()); + } + + /** Set the full data using the data struct */ + function set(bytes32 tableId, TablesData memory _table) internal { + set(tableId, _table.keySchema, _table.valueSchema, _table.abiEncodedKeyNames, _table.abiEncodedFieldNames); + } + + /** Set the full data using the data struct (using the specified store) */ + function set(IStore _store, bytes32 tableId, TablesData memory _table) internal { + set(_store, tableId, _table.keySchema, _table.valueSchema, _table.abiEncodedKeyNames, _table.abiEncodedFieldNames); + } + + /** + * Decode the tightly packed blob using this table's schema. + * Undefined behaviour for invalid blobs. + */ + function decode(bytes memory _blob) internal pure returns (TablesData memory _table) { + // 64 is the total byte length of static data + PackedCounter _encodedLengths = PackedCounter.wrap(Bytes.slice32(_blob, 64)); + + _table.keySchema = (Bytes.slice32(_blob, 0)); + + _table.valueSchema = (Bytes.slice32(_blob, 32)); + + // Store trims the blob if dynamic fields are all empty + if (_blob.length > 64) { + // skip static data length + dynamic lengths word + uint256 _start = 96; + uint256 _end; + unchecked { + _end = 96 + _encodedLengths.atIndex(0); + } + _table.abiEncodedKeyNames = (bytes(SliceLib.getSubslice(_blob, _start, _end).toBytes())); + + _start = _end; + unchecked { + _end += _encodedLengths.atIndex(1); + } + _table.abiEncodedFieldNames = (bytes(SliceLib.getSubslice(_blob, _start, _end).toBytes())); + } + } + + /** Tightly pack full data using this table's schema */ + function encode( + bytes32 keySchema, + bytes32 valueSchema, + bytes memory abiEncodedKeyNames, + bytes memory abiEncodedFieldNames + ) internal pure returns (bytes memory) { + uint40[] memory _counters = new uint40[](2); + _counters[0] = uint40(bytes(abiEncodedKeyNames).length); + _counters[1] = uint40(bytes(abiEncodedFieldNames).length); + PackedCounter _encodedLengths = PackedCounterLib.pack(_counters); + + return + abi.encodePacked( + keySchema, + valueSchema, + _encodedLengths.unwrap(), + bytes((abiEncodedKeyNames)), + bytes((abiEncodedFieldNames)) + ); + } + + /** Encode keys as a bytes32 array using this table's schema */ + function encodeKeyTuple(bytes32 tableId) internal pure returns (bytes32[] memory) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + return _keyTuple; + } + + /* Delete all data for given keys */ + function deleteRecord(bytes32 tableId) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); + } + + /* Delete all data for given keys (using the specified store) */ + function deleteRecord(IStore _store, bytes32 tableId) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); + } +} diff --git a/packages/store/src/codegen/tables/Vector2.sol b/packages/store/src/codegen/tables/Vector2.sol index 3780408232..e46e40fe0c 100644 --- a/packages/store/src/codegen/tables/Vector2.sol +++ b/packages/store/src/codegen/tables/Vector2.sol @@ -26,15 +26,7 @@ struct Vector2Data { } library Vector2 { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](2); - _schema[0] = SchemaType.UINT32; - _schema[1] = SchemaType.UINT32; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); _schema[0] = SchemaType.BYTES32; @@ -42,34 +34,36 @@ library Vector2 { return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](2); - _fieldNames[0] = "x"; - _fieldNames[1] = "y"; - return ("Vector2", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](2); + _schema[0] = SchemaType.UINT32; + _schema[1] = SchemaType.UINT32; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "key"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](2); + fieldNames[0] = "x"; + fieldNames[1] = "y"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get x */ @@ -77,7 +71,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (uint32(Bytes.slice4(_blob, 0))); } @@ -86,7 +80,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (uint32(Bytes.slice4(_blob, 0))); } @@ -95,7 +89,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), getValueSchema()); } /** Set x (using the specified store) */ @@ -103,7 +97,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), getValueSchema()); } /** Get y */ @@ -111,7 +105,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getValueSchema()); return (uint32(Bytes.slice4(_blob, 0))); } @@ -120,7 +114,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getValueSchema()); return (uint32(Bytes.slice4(_blob, 0))); } @@ -129,7 +123,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), getValueSchema()); } /** Set y (using the specified store) */ @@ -137,7 +131,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), getSchema()); + _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), getValueSchema()); } /** Get the full data */ @@ -145,7 +139,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -154,7 +148,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -165,7 +159,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setRecord(_tableId, _keyTuple, _data, getSchema()); + StoreSwitch.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using individual values (using the specified store) */ @@ -175,7 +169,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setRecord(_tableId, _keyTuple, _data, getSchema()); + _store.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using the data struct */ @@ -213,7 +207,7 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -221,6 +215,6 @@ library Vector2 { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/store/test/KeyEncoding.t.sol b/packages/store/test/KeyEncoding.t.sol index a6cc367fa1..ff34948b30 100644 --- a/packages/store/test/KeyEncoding.t.sol +++ b/packages/store/test/KeyEncoding.t.sol @@ -12,17 +12,17 @@ import { Schema } from "../src/Schema.sol"; contract KeyEncodingTest is Test, GasReporter, StoreReadWithStubs { function testRegisterAndGetSchema() public { startGasReport("register KeyEncoding schema"); - KeyEncoding.registerSchema(); + KeyEncoding.register(); endGasReport(); - Schema registeredSchema = StoreCore.getSchema(KeyEncodingTableId); - Schema declaredSchema = KeyEncoding.getSchema(); + Schema registeredSchema = StoreCore.getValueSchema(KeyEncodingTableId); + Schema declaredSchema = KeyEncoding.getValueSchema(); assertEq(keccak256(abi.encode(registeredSchema)), keccak256(abi.encode(declaredSchema))); } function testSetAndGet() public { - KeyEncoding.registerSchema(); + KeyEncoding.register(); KeyEncoding.set( 42, @@ -47,7 +47,7 @@ contract KeyEncodingTest is Test, GasReporter, StoreReadWithStubs { } function testKeyEncoding() public { - KeyEncoding.registerSchema(); + KeyEncoding.register(); bytes32[] memory keyTuple = KeyEncoding.encodeKeyTuple( 42, diff --git a/packages/store/test/MirrorSubscriber.sol b/packages/store/test/MirrorSubscriber.sol index 15e616711b..9276111340 100644 --- a/packages/store/test/MirrorSubscriber.sol +++ b/packages/store/test/MirrorSubscriber.sol @@ -10,8 +10,14 @@ bytes32 constant indexerTableId = keccak256("indexer.table"); contract MirrorSubscriber is IStoreHook { bytes32 _table; - constructor(bytes32 table, Schema schema, Schema keySchema) { - IStore(msg.sender).registerSchema(indexerTableId, schema, keySchema); + constructor( + bytes32 table, + Schema keySchema, + Schema valueSchema, + string[] memory keyNames, + string[] memory fieldNames + ) { + IStore(msg.sender).registerTable(indexerTableId, keySchema, valueSchema, keyNames, fieldNames); _table = table; } diff --git a/packages/store/test/Mixed.t.sol b/packages/store/test/Mixed.t.sol index bf335cf496..6e209a17fc 100644 --- a/packages/store/test/Mixed.t.sol +++ b/packages/store/test/Mixed.t.sol @@ -14,17 +14,17 @@ contract MixedTest is Test, GasReporter, StoreReadWithStubs { function testRegisterAndGetSchema() public { startGasReport("register Mixed schema"); - Mixed.registerSchema(); + Mixed.register(); endGasReport(); - Schema registeredSchema = StoreCore.getSchema(MixedTableId); - Schema declaredSchema = Mixed.getSchema(); + Schema registeredSchema = StoreCore.getValueSchema(MixedTableId); + Schema declaredSchema = Mixed.getValueSchema(); assertEq(keccak256(abi.encode(registeredSchema)), keccak256(abi.encode(declaredSchema))); } function testSetAndGet() public { - Mixed.registerSchema(); + Mixed.register(); bytes32 key = keccak256("somekey"); uint32[] memory a32 = new uint32[](2); diff --git a/packages/store/test/StoreCore.t.sol b/packages/store/test/StoreCore.t.sol index ed558a19ec..5e66762dc8 100644 --- a/packages/store/test/StoreCore.t.sol +++ b/packages/store/test/StoreCore.t.sol @@ -13,7 +13,7 @@ import { StoreReadWithStubs } from "../src/StoreReadWithStubs.sol"; import { IStoreErrors } from "../src/IStoreErrors.sol"; import { IStore } from "../src/IStore.sol"; import { StoreSwitch } from "../src/StoreSwitch.sol"; -import { StoreMetadataData, StoreMetadata } from "../src/codegen/Tables.sol"; +import { Tables, TablesTableId } from "../src/codegen/Tables.sol"; import { SchemaEncodeHelper } from "./SchemaEncodeHelper.sol"; import { StoreMock } from "./StoreMock.sol"; import { MirrorSubscriber, indexerTableId } from "./MirrorSubscriber.sol"; @@ -29,10 +29,24 @@ contract StoreCoreTest is Test, StoreMock { mapping(uint256 => bytes) private testMapping; Schema defaultKeySchema = SchemaEncodeHelper.encode(SchemaType.BYTES32); + string[] defaultKeyNames = new string[](1); function testRegisterAndGetSchema() public { - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); Schema keySchema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT16); + Schema valueSchema = SchemaEncodeHelper.encode( + SchemaType.UINT8, + SchemaType.UINT16, + SchemaType.UINT8, + SchemaType.UINT16 + ); + string[] memory keyNames = new string[](2); + keyNames[0] = "key1"; + keyNames[1] = "key2"; + string[] memory fieldNames = new string[](4); + fieldNames[0] = "value1"; + fieldNames[1] = "value2"; + fieldNames[2] = "value3"; + fieldNames[3] = "value4"; bytes32 table = keccak256("some.table"); @@ -40,46 +54,61 @@ contract StoreCoreTest is Test, StoreMock { bytes32[] memory key = new bytes32[](1); key[0] = bytes32(table); vm.expectEmit(true, true, true, true); - emit StoreSetRecord(StoreCoreInternal.SCHEMA_TABLE, key, abi.encodePacked(schema.unwrap(), keySchema.unwrap())); - - IStore(this).registerSchema(table, schema, keySchema); - - Schema loadedSchema = IStore(this).getSchema(table); + emit StoreSetRecord( + TablesTableId, + key, + Tables.encode(keySchema.unwrap(), valueSchema.unwrap(), abi.encode(keyNames), abi.encode(fieldNames)) + ); + IStore(this).registerTable(table, keySchema, valueSchema, keyNames, fieldNames); - assertEq(loadedSchema.unwrap(), schema.unwrap()); + Schema loadedValueSchema = IStore(this).getValueSchema(table); + assertEq(loadedValueSchema.unwrap(), valueSchema.unwrap()); Schema loadedKeySchema = IStore(this).getKeySchema(table); assertEq(loadedKeySchema.unwrap(), keySchema.unwrap()); - Schema schemaTableSchema = SchemaEncodeHelper.encode(SchemaType.BYTES32, SchemaType.BYTES32); - bytes memory schemaRecord = IStore(this).getRecord(StoreCoreInternal.SCHEMA_TABLE, key, schemaTableSchema); - assertEq(schemaRecord, abi.encodePacked(schema.unwrap(), keySchema.unwrap())); + bytes memory loadedKeyNames = Tables.getAbiEncodedKeyNames(IStore(this), table); + assertEq(loadedKeyNames, abi.encode(keyNames)); + + bytes memory loadedFieldNames = Tables.getAbiEncodedFieldNames(IStore(this), table); + assertEq(loadedFieldNames, abi.encode(fieldNames)); } function testFailRegisterInvalidSchema() public { - IStore(this).registerSchema( + string[] memory keyNames = new string[](2); + string[] memory fieldNames = new string[](4); + IStore(this).registerTable( keccak256("table"), Schema.wrap(keccak256("random bytes as schema")), - Schema.wrap(keccak256("random bytes as key schema")) + Schema.wrap(keccak256("random bytes as key schema")), + keyNames, + fieldNames ); } function testHasSchema() public { - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); + string[] memory keyNames = new string[](1); + string[] memory fieldNames = new string[](4); + Schema valueSchema = SchemaEncodeHelper.encode( + SchemaType.UINT8, + SchemaType.UINT16, + SchemaType.UINT8, + SchemaType.UINT16 + ); bytes32 table = keccak256("some.table"); bytes32 table2 = keccak256("other.table"); - IStore(this).registerSchema(table, schema, defaultKeySchema); + IStore(this).registerTable(table, defaultKeySchema, valueSchema, keyNames, fieldNames); assertTrue(StoreCore.hasTable(table)); assertFalse(StoreCore.hasTable(table2)); - IStore(this).getSchema(table); + IStore(this).getValueSchema(table); IStore(this).getKeySchema(table); vm.expectRevert( abi.encodeWithSelector(IStoreErrors.StoreCore_TableNotFound.selector, table2, string(abi.encodePacked(table2))) ); - IStore(this).getSchema(table2); + IStore(this).getValueSchema(table2); vm.expectRevert( abi.encodeWithSelector(IStoreErrors.StoreCore_TableNotFound.selector, table2, string(abi.encodePacked(table2))) @@ -87,57 +116,31 @@ contract StoreCoreTest is Test, StoreMock { IStore(this).getKeySchema(table2); } - function testSetMetadata() public { - bytes32 table = keccak256("some.table"); - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT16); - Schema keySchema = SchemaEncodeHelper.encode( - SchemaType.UINT8, - SchemaType.UINT16, - SchemaType.UINT8, - SchemaType.UINT16 - ); - string memory tableName = "someTable"; - string[] memory fieldNames = new string[](2); - fieldNames[0] = "field1"; - fieldNames[1] = "field2"; - - // Register table - IStore(this).registerSchema(table, schema, keySchema); - - IStore(this).setMetadata(table, tableName, fieldNames); - - // Get metadata for table - StoreMetadataData memory metadata = StoreMetadata.get(table); - - assertEq(metadata.tableName, tableName); - assertEq(metadata.abiEncodedFieldNames, abi.encode(fieldNames)); - } - - function testSetMetadataRevert() public { + function testRegisterTableRevertNames() public { bytes32 table = keccak256("some.table"); - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT8); Schema keySchema = SchemaEncodeHelper.encode( SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16 ); - string memory tableName = "someTable"; - string[] memory fieldNames = new string[](2); - fieldNames[0] = "field1"; - fieldNames[1] = "field2"; + Schema valueSchema = SchemaEncodeHelper.encode(SchemaType.UINT8); + string[] memory fourNames = new string[](4); + string[] memory oneName = new string[](1); - // Register table - IStore(this).registerSchema(table, schema, keySchema); + // Register table with invalid key names + vm.expectRevert(abi.encodeWithSelector(IStoreErrors.StoreCore_InvalidKeyNamesLength.selector, 4, 1)); + IStore(this).registerTable(table, keySchema, valueSchema, oneName, oneName); - vm.expectRevert(abi.encodeWithSelector(IStoreErrors.StoreCore_InvalidFieldNamesLength.selector, 1, 2)); - IStore(this).setMetadata(table, tableName, fieldNames); + // Register table with invalid value names + vm.expectRevert(abi.encodeWithSelector(IStoreErrors.StoreCore_InvalidFieldNamesLength.selector, 1, 4)); + IStore(this).registerTable(table, keySchema, valueSchema, fourNames, fourNames); } function testSetAndGetDynamicDataLength() public { bytes32 table = keccak256("some.table"); - Schema schema = SchemaEncodeHelper.encode( + Schema valueSchema = SchemaEncodeHelper.encode( SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT32, @@ -146,7 +149,7 @@ contract StoreCoreTest is Test, StoreMock { ); // Register schema - IStore(this).registerSchema(table, schema, defaultKeySchema); + IStore(this).registerTable(table, defaultKeySchema, valueSchema, new string[](1), new string[](5)); // Create some key bytes32[] memory key = new bytes32[](1); @@ -179,10 +182,15 @@ contract StoreCoreTest is Test, StoreMock { function testSetAndGetStaticData() public { // Register table's schema - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); + Schema valueSchema = SchemaEncodeHelper.encode( + SchemaType.UINT8, + SchemaType.UINT16, + SchemaType.UINT8, + SchemaType.UINT16 + ); bytes32 table = keccak256("some.table"); - IStore(this).registerSchema(table, schema, defaultKeySchema); + IStore(this).registerTable(table, defaultKeySchema, valueSchema, new string[](1), new string[](4)); // Set data bytes memory data = abi.encodePacked(bytes1(0x01), bytes2(0x0203), bytes1(0x04), bytes2(0x0506)); @@ -194,19 +202,24 @@ contract StoreCoreTest is Test, StoreMock { vm.expectEmit(true, true, true, true); emit StoreSetRecord(table, key, data); - IStore(this).setRecord(table, key, data, schema); + IStore(this).setRecord(table, key, data, valueSchema); // Get data - bytes memory loadedData = IStore(this).getRecord(table, key, schema); + bytes memory loadedData = IStore(this).getRecord(table, key, valueSchema); assertTrue(Bytes.equals(data, loadedData)); } function testFailSetAndGetStaticData() public { // Register table's schema - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); + Schema valueSchema = SchemaEncodeHelper.encode( + SchemaType.UINT8, + SchemaType.UINT16, + SchemaType.UINT8, + SchemaType.UINT16 + ); bytes32 table = keccak256("some.table"); - IStore(this).registerSchema(table, schema, defaultKeySchema); + IStore(this).registerTable(table, defaultKeySchema, valueSchema, new string[](1), new string[](4)); // Set data bytes memory data = abi.encodePacked(bytes1(0x01), bytes2(0x0203), bytes1(0x04)); @@ -215,14 +228,14 @@ contract StoreCoreTest is Test, StoreMock { key[0] = keccak256("some.key"); // This should fail because the data is not 6 bytes long - IStore(this).setRecord(table, key, data, schema); + IStore(this).setRecord(table, key, data, valueSchema); } function testSetAndGetStaticDataSpanningWords() public { // Register table's schema - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT128, SchemaType.UINT256); + Schema valueSchema = SchemaEncodeHelper.encode(SchemaType.UINT128, SchemaType.UINT256); bytes32 table = keccak256("some.table"); - IStore(this).registerSchema(table, schema, defaultKeySchema); + IStore(this).registerTable(table, defaultKeySchema, valueSchema, new string[](1), new string[](2)); // Set data bytes memory data = abi.encodePacked( @@ -237,10 +250,10 @@ contract StoreCoreTest is Test, StoreMock { vm.expectEmit(true, true, true, true); emit StoreSetRecord(table, key, data); - IStore(this).setRecord(table, key, data, schema); + IStore(this).setRecord(table, key, data, valueSchema); // Get data - bytes memory loadedData = IStore(this).getRecord(table, key, schema); + bytes memory loadedData = IStore(this).getRecord(table, key, valueSchema); assertTrue(Bytes.equals(data, loadedData)); } @@ -249,8 +262,12 @@ contract StoreCoreTest is Test, StoreMock { bytes32 table = keccak256("some.table"); // Register table's schema - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT128, SchemaType.UINT32_ARRAY, SchemaType.UINT32_ARRAY); - IStore(this).registerSchema(table, schema, defaultKeySchema); + Schema valueSchema = SchemaEncodeHelper.encode( + SchemaType.UINT128, + SchemaType.UINT32_ARRAY, + SchemaType.UINT32_ARRAY + ); + IStore(this).registerTable(table, defaultKeySchema, valueSchema, new string[](1), new string[](3)); bytes16 firstDataBytes = bytes16(0x0102030405060708090a0b0c0d0e0f10); @@ -296,10 +313,10 @@ contract StoreCoreTest is Test, StoreMock { emit StoreSetRecord(table, key, data); // Set data - IStore(this).setRecord(table, key, data, schema); + IStore(this).setRecord(table, key, data, valueSchema); // Get data - bytes memory loadedData = IStore(this).getRecord(table, key, schema); + bytes memory loadedData = IStore(this).getRecord(table, key, valueSchema); assertEq(loadedData.length, data.length); assertEq(keccak256(loadedData), keccak256(data)); @@ -322,13 +339,13 @@ contract StoreCoreTest is Test, StoreMock { bytes32 table = keccak256("some.table"); // Register table's schema - Schema schema = SchemaEncodeHelper.encode( + Schema valueSchema = SchemaEncodeHelper.encode( SchemaType.UINT128, SchemaType.UINT256, SchemaType.UINT32_ARRAY, SchemaType.UINT32_ARRAY ); - IStore(this).registerSchema(table, schema, defaultKeySchema); + IStore(this).registerTable(table, defaultKeySchema, valueSchema, new string[](1), new string[](4)); bytes16 firstDataBytes = bytes16(0x0102030405060708090a0b0c0d0e0f10); @@ -343,21 +360,21 @@ contract StoreCoreTest is Test, StoreMock { emit StoreSetField(table, key, 0, firstDataPacked); // Set first field - IStore(this).setField(table, key, 0, firstDataPacked, schema); + IStore(this).setField(table, key, 0, firstDataPacked, valueSchema); //////////////// // Static data //////////////// // Get first field - bytes memory loadedData = IStore(this).getField(table, key, 0, schema); + bytes memory loadedData = IStore(this).getField(table, key, 0, valueSchema); // Verify loaded data is correct assertEq(loadedData.length, 16); assertEq(bytes16(loadedData), bytes16(firstDataBytes)); // Verify the second index is not set yet - assertEq(uint256(bytes32(IStore(this).getField(table, key, 1, schema))), 0); + assertEq(uint256(bytes32(IStore(this).getField(table, key, 1, valueSchema))), 0); // Set second field bytes32 secondDataBytes = keccak256("some data"); @@ -368,24 +385,24 @@ contract StoreCoreTest is Test, StoreMock { vm.expectEmit(true, true, true, true); emit StoreSetField(table, key, 1, secondDataPacked); - IStore(this).setField(table, key, 1, secondDataPacked, schema); + IStore(this).setField(table, key, 1, secondDataPacked, valueSchema); // Get second field - loadedData = IStore(this).getField(table, key, 1, schema); + loadedData = IStore(this).getField(table, key, 1, valueSchema); // Verify loaded data is correct assertEq(loadedData.length, 32); assertEq(bytes32(loadedData), secondDataBytes); // Verify the first field didn't change - assertEq(bytes16(IStore(this).getField(table, key, 0, schema)), bytes16(firstDataBytes)); + assertEq(bytes16(IStore(this).getField(table, key, 0, valueSchema)), bytes16(firstDataBytes)); // Verify the full static data is correct - assertEq(IStore(this).getSchema(table).staticDataLength(), 48); - assertEq(Bytes.slice16(IStore(this).getRecord(table, key, schema), 0), firstDataBytes); - assertEq(Bytes.slice32(IStore(this).getRecord(table, key, schema), 16), secondDataBytes); + assertEq(IStore(this).getValueSchema(table).staticDataLength(), 48); + assertEq(Bytes.slice16(IStore(this).getRecord(table, key, valueSchema), 0), firstDataBytes); + assertEq(Bytes.slice32(IStore(this).getRecord(table, key, valueSchema), 16), secondDataBytes); assertEq( - keccak256(SliceLib.getSubslice(IStore(this).getRecord(table, key, schema), 0, 48).toBytes()), + keccak256(SliceLib.getSubslice(IStore(this).getRecord(table, key, valueSchema), 0, 48).toBytes()), keccak256(abi.encodePacked(firstDataBytes, secondDataBytes)) ); @@ -415,10 +432,10 @@ contract StoreCoreTest is Test, StoreMock { emit StoreSetField(table, key, 2, thirdDataBytes); // Set third field - IStore(this).setField(table, key, 2, thirdDataBytes, schema); + IStore(this).setField(table, key, 2, thirdDataBytes, valueSchema); // Get third field - loadedData = IStore(this).getField(table, key, 2, schema); + loadedData = IStore(this).getField(table, key, 2, valueSchema); // Verify loaded data is correct assertEq(SliceLib.fromBytes(loadedData).decodeArray_uint32().length, 2); @@ -426,21 +443,21 @@ contract StoreCoreTest is Test, StoreMock { assertEq(keccak256(loadedData), keccak256(thirdDataBytes)); // Verify the fourth field is not set yet - assertEq(IStore(this).getField(table, key, 3, schema).length, 0); + assertEq(IStore(this).getField(table, key, 3, valueSchema).length, 0); // Verify none of the previous fields were impacted - assertEq(bytes16(IStore(this).getField(table, key, 0, schema)), bytes16(firstDataBytes)); - assertEq(bytes32(IStore(this).getField(table, key, 1, schema)), bytes32(secondDataBytes)); + assertEq(bytes16(IStore(this).getField(table, key, 0, valueSchema)), bytes16(firstDataBytes)); + assertEq(bytes32(IStore(this).getField(table, key, 1, valueSchema)), bytes32(secondDataBytes)); // Expect a StoreSetField event to be emitted vm.expectEmit(true, true, true, true); emit StoreSetField(table, key, 3, fourthDataBytes); // Set fourth field - IStore(this).setField(table, key, 3, fourthDataBytes, schema); + IStore(this).setField(table, key, 3, fourthDataBytes, valueSchema); // Get fourth field - loadedData = IStore(this).getField(table, key, 3, schema); + loadedData = IStore(this).getField(table, key, 3, valueSchema); // Verify loaded data is correct assertEq(loadedData.length, fourthDataBytes.length); @@ -449,7 +466,7 @@ contract StoreCoreTest is Test, StoreMock { // Verify all fields are correct PackedCounter encodedLengths = PackedCounterLib.pack(uint40(thirdDataBytes.length), uint40(fourthDataBytes.length)); assertEq( - keccak256(IStore(this).getRecord(table, key, schema)), + keccak256(IStore(this).getRecord(table, key, valueSchema)), keccak256( abi.encodePacked(firstDataBytes, secondDataBytes, encodedLengths.unwrap(), thirdDataBytes, fourthDataBytes) ) @@ -460,8 +477,12 @@ contract StoreCoreTest is Test, StoreMock { bytes32 table = keccak256("some.table"); // Register table's schema - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT128, SchemaType.UINT32_ARRAY, SchemaType.UINT32_ARRAY); - IStore(this).registerSchema(table, schema, defaultKeySchema); + Schema valueSchema = SchemaEncodeHelper.encode( + SchemaType.UINT128, + SchemaType.UINT32_ARRAY, + SchemaType.UINT32_ARRAY + ); + IStore(this).registerTable(table, defaultKeySchema, valueSchema, new string[](1), new string[](3)); bytes16 firstDataBytes = bytes16(0x0102030405060708090a0b0c0d0e0f10); @@ -503,10 +524,10 @@ contract StoreCoreTest is Test, StoreMock { key[0] = bytes32("some.key"); // Set data - IStore(this).setRecord(table, key, data, schema); + IStore(this).setRecord(table, key, data, valueSchema); // Get data - bytes memory loadedData = IStore(this).getRecord(table, key, schema); + bytes memory loadedData = IStore(this).getRecord(table, key, valueSchema); assertEq(loadedData.length, data.length); assertEq(keccak256(loadedData), keccak256(data)); @@ -516,11 +537,11 @@ contract StoreCoreTest is Test, StoreMock { emit StoreDeleteRecord(table, key); // Delete data - IStore(this).deleteRecord(table, key, schema); + IStore(this).deleteRecord(table, key, valueSchema); // Verify data is deleted - loadedData = IStore(this).getRecord(table, key, schema); - assertEq(keccak256(loadedData), keccak256(new bytes(schema.staticDataLength()))); + loadedData = IStore(this).getRecord(table, key, valueSchema); + assertEq(keccak256(loadedData), keccak256(new bytes(valueSchema.staticDataLength()))); } struct TestPushToFieldData { @@ -542,8 +563,12 @@ contract StoreCoreTest is Test, StoreMock { data.table = keccak256("some.table"); // Register table's schema - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT256, SchemaType.UINT32_ARRAY, SchemaType.UINT32_ARRAY); - IStore(this).registerSchema(data.table, schema, defaultKeySchema); + Schema valueSchema = SchemaEncodeHelper.encode( + SchemaType.UINT256, + SchemaType.UINT32_ARRAY, + SchemaType.UINT32_ARRAY + ); + IStore(this).registerTable(data.table, defaultKeySchema, valueSchema, new string[](1), new string[](3)); // Create key data.key = new bytes32[](1); @@ -567,10 +592,10 @@ contract StoreCoreTest is Test, StoreMock { } // Set fields - IStore(this).setField(data.table, data.key, 0, abi.encodePacked(data.firstDataBytes), schema); - IStore(this).setField(data.table, data.key, 1, data.secondDataBytes, schema); + IStore(this).setField(data.table, data.key, 0, abi.encodePacked(data.firstDataBytes), valueSchema); + IStore(this).setField(data.table, data.key, 1, data.secondDataBytes, valueSchema); // Initialize a field with push - IStore(this).pushToField(data.table, data.key, 2, data.thirdDataBytes, schema); + IStore(this).pushToField(data.table, data.key, 2, data.thirdDataBytes, valueSchema); // Create data to push { @@ -585,10 +610,10 @@ contract StoreCoreTest is Test, StoreMock { emit StoreSetField(data.table, data.key, 1, data.newSecondDataBytes); // Push to second field - IStore(this).pushToField(data.table, data.key, 1, data.secondDataToPush, schema); + IStore(this).pushToField(data.table, data.key, 1, data.secondDataToPush, valueSchema); // Get second field - data.loadedData = IStore(this).getField(data.table, data.key, 1, schema); + data.loadedData = IStore(this).getField(data.table, data.key, 1, valueSchema); // Verify loaded data is correct assertEq(SliceLib.fromBytes(data.loadedData).decodeArray_uint32().length, 2 + 1); @@ -596,8 +621,8 @@ contract StoreCoreTest is Test, StoreMock { assertEq(data.loadedData, data.newSecondDataBytes); // Verify none of the other fields were impacted - assertEq(bytes32(IStore(this).getField(data.table, data.key, 0, schema)), data.firstDataBytes); - assertEq(IStore(this).getField(data.table, data.key, 2, schema), data.thirdDataBytes); + assertEq(bytes32(IStore(this).getField(data.table, data.key, 0, valueSchema)), data.firstDataBytes); + assertEq(IStore(this).getField(data.table, data.key, 2, valueSchema), data.thirdDataBytes); // Create data to push { @@ -621,10 +646,10 @@ contract StoreCoreTest is Test, StoreMock { emit StoreSetField(data.table, data.key, 2, data.newThirdDataBytes); // Push to third field - IStore(this).pushToField(data.table, data.key, 2, data.thirdDataToPush, schema); + IStore(this).pushToField(data.table, data.key, 2, data.thirdDataToPush, valueSchema); // Get third field - data.loadedData = IStore(this).getField(data.table, data.key, 2, schema); + data.loadedData = IStore(this).getField(data.table, data.key, 2, valueSchema); // Verify loaded data is correct assertEq(SliceLib.fromBytes(data.loadedData).decodeArray_uint32().length, 3 + 10); @@ -632,8 +657,8 @@ contract StoreCoreTest is Test, StoreMock { assertEq(data.loadedData, data.newThirdDataBytes); // Verify none of the other fields were impacted - assertEq(bytes32(IStore(this).getField(data.table, data.key, 0, schema)), data.firstDataBytes); - assertEq(IStore(this).getField(data.table, data.key, 1, schema), data.newSecondDataBytes); + assertEq(bytes32(IStore(this).getField(data.table, data.key, 0, valueSchema)), data.firstDataBytes); + assertEq(IStore(this).getField(data.table, data.key, 1, valueSchema), data.newSecondDataBytes); } struct TestUpdateInFieldData { @@ -670,8 +695,12 @@ contract StoreCoreTest is Test, StoreMock { data.table = keccak256("some.table"); // Register table's schema - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT256, SchemaType.UINT32_ARRAY, SchemaType.UINT64_ARRAY); - IStore(this).registerSchema(data.table, schema, defaultKeySchema); + Schema valueSchema = SchemaEncodeHelper.encode( + SchemaType.UINT256, + SchemaType.UINT32_ARRAY, + SchemaType.UINT64_ARRAY + ); + IStore(this).registerTable(data.table, defaultKeySchema, valueSchema, new string[](1), new string[](3)); // Create key data.key = new bytes32[](1); @@ -694,9 +723,9 @@ contract StoreCoreTest is Test, StoreMock { data.thirdDataBytes = EncodeArray.encode(data.thirdData); // Set fields - IStore(this).setField(data.table, data.key, 0, abi.encodePacked(data.firstDataBytes), schema); - IStore(this).setField(data.table, data.key, 1, data.secondDataBytes, schema); - IStore(this).setField(data.table, data.key, 2, data.thirdDataBytes, schema); + IStore(this).setField(data.table, data.key, 0, abi.encodePacked(data.firstDataBytes), valueSchema); + IStore(this).setField(data.table, data.key, 1, data.secondDataBytes, valueSchema); + IStore(this).setField(data.table, data.key, 2, data.thirdDataBytes, valueSchema); // Create data to use for the update { @@ -712,10 +741,10 @@ contract StoreCoreTest is Test, StoreMock { emit StoreSetField(data.table, data.key, 1, data.newSecondDataBytes); // Update index 1 in second field (4 = byte length of uint32) - IStore(this).updateInField(data.table, data.key, 1, 4 * 1, data.secondDataForUpdate, schema); + IStore(this).updateInField(data.table, data.key, 1, 4 * 1, data.secondDataForUpdate, valueSchema); // Get second field - data.loadedData = IStore(this).getField(data.table, data.key, 1, schema); + data.loadedData = IStore(this).getField(data.table, data.key, 1, valueSchema); // Verify loaded data is correct assertEq(SliceLib.fromBytes(data.loadedData).decodeArray_uint32().length, data.secondData.length); @@ -723,8 +752,8 @@ contract StoreCoreTest is Test, StoreMock { assertEq(data.loadedData, data.newSecondDataBytes); // Verify none of the other fields were impacted - assertEq(bytes32(IStore(this).getField(data.table, data.key, 0, schema)), data.firstDataBytes); - assertEq(IStore(this).getField(data.table, data.key, 2, schema), data.thirdDataBytes); + assertEq(bytes32(IStore(this).getField(data.table, data.key, 0, valueSchema)), data.firstDataBytes); + assertEq(IStore(this).getField(data.table, data.key, 2, valueSchema), data.thirdDataBytes); // Create data for update { @@ -750,10 +779,10 @@ contract StoreCoreTest is Test, StoreMock { emit StoreSetField(data.table, data.key, 2, data.newThirdDataBytes); // Update indexes 1,2,3,4 in third field (8 = byte length of uint64) - IStore(this).updateInField(data.table, data.key, 2, 8 * 1, data.thirdDataForUpdate, schema); + IStore(this).updateInField(data.table, data.key, 2, 8 * 1, data.thirdDataForUpdate, valueSchema); // Get third field - data.loadedData = IStore(this).getField(data.table, data.key, 2, schema); + data.loadedData = IStore(this).getField(data.table, data.key, 2, valueSchema); // Verify loaded data is correct assertEq(SliceLib.fromBytes(data.loadedData).decodeArray_uint64().length, data.thirdData.length); @@ -761,39 +790,39 @@ contract StoreCoreTest is Test, StoreMock { assertEq(data.loadedData, data.newThirdDataBytes); // Verify none of the other fields were impacted - assertEq(bytes32(IStore(this).getField(data.table, data.key, 0, schema)), data.firstDataBytes); - assertEq(IStore(this).getField(data.table, data.key, 1, schema), data.newSecondDataBytes); + assertEq(bytes32(IStore(this).getField(data.table, data.key, 0, valueSchema)), data.firstDataBytes); + assertEq(IStore(this).getField(data.table, data.key, 1, valueSchema), data.newSecondDataBytes); // startByteIndex must not overflow vm.expectRevert( abi.encodeWithSelector(IStoreErrors.StoreCore_DataIndexOverflow.selector, type(uint40).max, type(uint56).max) ); - IStore(this).updateInField(data.table, data.key, 2, type(uint56).max, data.thirdDataForUpdate, schema); + IStore(this).updateInField(data.table, data.key, 2, type(uint56).max, data.thirdDataForUpdate, valueSchema); } function testAccessEmptyData() public { bytes32 table = keccak256("some.table"); - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT32, SchemaType.UINT32_ARRAY); + Schema valueSchema = SchemaEncodeHelper.encode(SchemaType.UINT32, SchemaType.UINT32_ARRAY); - IStore(this).registerSchema(table, schema, defaultKeySchema); + IStore(this).registerTable(table, defaultKeySchema, valueSchema, new string[](1), new string[](2)); // Create key bytes32[] memory key = new bytes32[](1); key[0] = bytes32("some.key"); - bytes memory data1 = IStore(this).getRecord(table, key, schema); - assertEq(data1.length, schema.staticDataLength()); + bytes memory data1 = IStore(this).getRecord(table, key, valueSchema); + assertEq(data1.length, valueSchema.staticDataLength()); - bytes memory data2 = IStore(this).getField(table, key, 0, schema); - assertEq(data2.length, schema.staticDataLength()); + bytes memory data2 = IStore(this).getField(table, key, 0, valueSchema); + assertEq(data2.length, valueSchema.staticDataLength()); - bytes memory data3 = IStore(this).getField(table, key, 1, schema); + bytes memory data3 = IStore(this).getField(table, key, 1, valueSchema); assertEq(data3.length, 0); - uint256 data3Length = IStore(this).getFieldLength(table, key, 1, schema); + uint256 data3Length = IStore(this).getFieldLength(table, key, 1, valueSchema); assertEq(data3Length, 0); - bytes memory data3Slice = IStore(this).getFieldSlice(table, key, 1, schema, 0, 0); + bytes memory data3Slice = IStore(this).getFieldSlice(table, key, 1, valueSchema, 0, 0); assertEq(data3Slice.length, 0); } @@ -803,34 +832,40 @@ contract StoreCoreTest is Test, StoreMock { key[0] = keccak256("some key"); // Register table's schema - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT128); - IStore(this).registerSchema(table, schema, defaultKeySchema); + Schema valueSchema = SchemaEncodeHelper.encode(SchemaType.UINT128); + IStore(this).registerTable(table, defaultKeySchema, valueSchema, new string[](1), new string[](1)); // Create subscriber - MirrorSubscriber subscriber = new MirrorSubscriber(table, schema, defaultKeySchema); + MirrorSubscriber subscriber = new MirrorSubscriber( + table, + defaultKeySchema, + valueSchema, + new string[](1), + new string[](1) + ); IStore(this).registerStoreHook(table, subscriber); bytes memory data = abi.encodePacked(bytes16(0x0102030405060708090a0b0c0d0e0f10)); - IStore(this).setRecord(table, key, data, schema); + IStore(this).setRecord(table, key, data, valueSchema); // Get data from indexed table - the indexer should have mirrored the data there - bytes memory indexedData = IStore(this).getRecord(indexerTableId, key, schema); + bytes memory indexedData = IStore(this).getRecord(indexerTableId, key, valueSchema); assertEq(keccak256(data), keccak256(indexedData)); data = abi.encodePacked(bytes16(0x1112131415161718191a1b1c1d1e1f20)); - IStore(this).setField(table, key, 0, data, schema); + IStore(this).setField(table, key, 0, data, valueSchema); // Get data from indexed table - the indexer should have mirrored the data there - indexedData = IStore(this).getRecord(indexerTableId, key, schema); + indexedData = IStore(this).getRecord(indexerTableId, key, valueSchema); assertEq(keccak256(data), keccak256(indexedData)); - IStore(this).deleteRecord(table, key, schema); + IStore(this).deleteRecord(table, key, valueSchema); // Get data from indexed table - the indexer should have mirrored the data there - indexedData = IStore(this).getRecord(indexerTableId, key, schema); + indexedData = IStore(this).getRecord(indexerTableId, key, valueSchema); assertEq(keccak256(indexedData), keccak256(abi.encodePacked(bytes16(0)))); } @@ -840,11 +875,17 @@ contract StoreCoreTest is Test, StoreMock { key[0] = keccak256("some key"); // Register table's schema - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT128, SchemaType.UINT32_ARRAY); - IStore(this).registerSchema(table, schema, defaultKeySchema); + Schema valueSchema = SchemaEncodeHelper.encode(SchemaType.UINT128, SchemaType.UINT32_ARRAY); + IStore(this).registerTable(table, defaultKeySchema, valueSchema, new string[](1), new string[](2)); // Create subscriber - MirrorSubscriber subscriber = new MirrorSubscriber(table, schema, defaultKeySchema); + MirrorSubscriber subscriber = new MirrorSubscriber( + table, + defaultKeySchema, + valueSchema, + new string[](1), + new string[](2) + ); IStore(this).registerStoreHook(table, subscriber); @@ -856,10 +897,10 @@ contract StoreCoreTest is Test, StoreMock { bytes memory staticData = abi.encodePacked(bytes16(0x0102030405060708090a0b0c0d0e0f10)); bytes memory data = abi.encodePacked(staticData, dynamicData); - IStore(this).setRecord(table, key, data, schema); + IStore(this).setRecord(table, key, data, valueSchema); // Get data from indexed table - the indexer should have mirrored the data there - bytes memory indexedData = IStore(this).getRecord(indexerTableId, key, schema); + bytes memory indexedData = IStore(this).getRecord(indexerTableId, key, valueSchema); assertEq(keccak256(data), keccak256(indexedData)); // Update dynamic data @@ -868,16 +909,16 @@ contract StoreCoreTest is Test, StoreMock { dynamicData = abi.encodePacked(encodedArrayDataLength.unwrap(), arrayDataBytes); data = abi.encodePacked(staticData, dynamicData); - IStore(this).setField(table, key, 1, arrayDataBytes, schema); + IStore(this).setField(table, key, 1, arrayDataBytes, valueSchema); // Get data from indexed table - the indexer should have mirrored the data there - indexedData = IStore(this).getRecord(indexerTableId, key, schema); + indexedData = IStore(this).getRecord(indexerTableId, key, valueSchema); assertEq(keccak256(data), keccak256(indexedData)); - IStore(this).deleteRecord(table, key, schema); + IStore(this).deleteRecord(table, key, valueSchema); // Get data from indexed table - the indexer should have mirrored the data there - indexedData = IStore(this).getRecord(indexerTableId, key, schema); + indexedData = IStore(this).getRecord(indexerTableId, key, valueSchema); assertEq(keccak256(indexedData), keccak256(abi.encodePacked(bytes16(0)))); } } diff --git a/packages/store/test/StoreCoreDynamic.t.sol b/packages/store/test/StoreCoreDynamic.t.sol index bbeddf0b08..8f94084138 100644 --- a/packages/store/test/StoreCoreDynamic.t.sol +++ b/packages/store/test/StoreCoreDynamic.t.sol @@ -36,8 +36,12 @@ contract StoreCoreDynamicTest is Test, GasReporter, StoreReadWithStubs { function setUp() public { // Register table's schema - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT256, SchemaType.UINT32_ARRAY, SchemaType.UINT32_ARRAY); - StoreCore.registerSchema(_table, schema, defaultKeySchema); + Schema valueSchema = SchemaEncodeHelper.encode( + SchemaType.UINT256, + SchemaType.UINT32_ARRAY, + SchemaType.UINT32_ARRAY + ); + StoreCore.registerTable(_table, defaultKeySchema, valueSchema, new string[](1), new string[](3)); // Create key _key = new bytes32[](1); @@ -66,14 +70,14 @@ contract StoreCoreDynamicTest is Test, GasReporter, StoreReadWithStubs { thirdDataBytes = EncodeArray.encode(thirdData); // Set fields - StoreCore.setField(_table, _key, 0, abi.encodePacked(firstDataBytes), schema); - StoreCore.setField(_table, _key, 1, secondDataBytes, schema); + StoreCore.setField(_table, _key, 0, abi.encodePacked(firstDataBytes), valueSchema); + StoreCore.setField(_table, _key, 1, secondDataBytes, valueSchema); // Initialize a field with push - StoreCore.pushToField(_table, _key, 2, thirdDataBytes, schema); + StoreCore.pushToField(_table, _key, 2, thirdDataBytes, valueSchema); } function testPopFromSecondField() public { - Schema schema = StoreCore.getSchema(_table); + Schema valueSchema = StoreCore.getValueSchema(_table); bytes memory dataBytes = secondDataBytes; // Prepare expected data @@ -89,30 +93,30 @@ contract StoreCoreDynamicTest is Test, GasReporter, StoreReadWithStubs { // Pop from second field startGasReport("pop from field (cold, 1 slot, 1 uint32 item)"); - StoreCore.popFromField(_table, _key, 1, byteLengthToPop, schema); + StoreCore.popFromField(_table, _key, 1, byteLengthToPop, valueSchema); endGasReport(); // Get second field - bytes memory loadedData = StoreCore.getField(_table, _key, 1, schema); + bytes memory loadedData = StoreCore.getField(_table, _key, 1, valueSchema); // Verify loaded data is correct assertEq(loadedData, newDataBytes); // Reset the second field and pop again (but warm this time) - StoreCore.setField(_table, _key, 1, dataBytes, schema); + StoreCore.setField(_table, _key, 1, dataBytes, valueSchema); startGasReport("pop from field (warm, 1 slot, 1 uint32 item)"); - StoreCore.popFromField(_table, _key, 1, byteLengthToPop, schema); + StoreCore.popFromField(_table, _key, 1, byteLengthToPop, valueSchema); endGasReport(); // Get second field - loadedData = StoreCore.getField(_table, _key, 1, schema); + loadedData = StoreCore.getField(_table, _key, 1, valueSchema); // Verify loaded data is correct assertEq(loadedData, newDataBytes); // Verify none of the other fields were impacted - assertEq(bytes32(StoreCore.getField(_table, _key, 0, schema)), firstDataBytes); - assertEq(StoreCore.getField(_table, _key, 2, schema), thirdDataBytes); + assertEq(bytes32(StoreCore.getField(_table, _key, 0, valueSchema)), firstDataBytes); + assertEq(StoreCore.getField(_table, _key, 2, valueSchema), thirdDataBytes); } function testPopFromThirdField() public { - Schema schema = StoreCore.getSchema(_table); + Schema valueSchema = StoreCore.getValueSchema(_table); bytes memory dataBytes = thirdDataBytes; // Prepare expected data @@ -128,70 +132,70 @@ contract StoreCoreDynamicTest is Test, GasReporter, StoreReadWithStubs { // Pop from the field startGasReport("pop from field (cold, 2 slots, 10 uint32 items)"); - StoreCore.popFromField(_table, _key, 2, byteLengthToPop, schema); + StoreCore.popFromField(_table, _key, 2, byteLengthToPop, valueSchema); endGasReport(); // Load and verify the field - bytes memory loadedData = StoreCore.getField(_table, _key, 2, schema); + bytes memory loadedData = StoreCore.getField(_table, _key, 2, valueSchema); assertEq(loadedData, newDataBytes); // Reset the field and pop again (but warm this time) - StoreCore.setField(_table, _key, 2, dataBytes, schema); + StoreCore.setField(_table, _key, 2, dataBytes, valueSchema); startGasReport("pop from field (warm, 2 slots, 10 uint32 items)"); - StoreCore.popFromField(_table, _key, 2, byteLengthToPop, schema); + StoreCore.popFromField(_table, _key, 2, byteLengthToPop, valueSchema); endGasReport(); // Load and verify the field - loadedData = StoreCore.getField(_table, _key, 2, schema); + loadedData = StoreCore.getField(_table, _key, 2, valueSchema); assertEq(loadedData, newDataBytes); // Verify none of the other fields were impacted - assertEq(bytes32(StoreCore.getField(_table, _key, 0, schema)), firstDataBytes); - assertEq(StoreCore.getField(_table, _key, 1, schema), secondDataBytes); + assertEq(bytes32(StoreCore.getField(_table, _key, 0, valueSchema)), firstDataBytes); + assertEq(StoreCore.getField(_table, _key, 1, valueSchema), secondDataBytes); } function testGetSecondFieldLength() public { - Schema schema = StoreCore.getSchema(_table); + Schema valueSchema = StoreCore.getValueSchema(_table); startGasReport("get field length (cold, 1 slot)"); - uint256 length = StoreCore.getFieldLength(_table, _key, 1, schema); + uint256 length = StoreCore.getFieldLength(_table, _key, 1, valueSchema); endGasReport(); assertEq(length, secondDataBytes.length); startGasReport("get field length (warm, 1 slot)"); - length = StoreCore.getFieldLength(_table, _key, 1, schema); + length = StoreCore.getFieldLength(_table, _key, 1, valueSchema); endGasReport(); assertEq(length, secondDataBytes.length); } function testGetThirdFieldLength() public { - Schema schema = StoreCore.getSchema(_table); + Schema valueSchema = StoreCore.getValueSchema(_table); startGasReport("get field length (warm due to , 2 slots)"); - uint256 length = StoreCore.getFieldLength(_table, _key, 2, schema); + uint256 length = StoreCore.getFieldLength(_table, _key, 2, valueSchema); endGasReport(); assertEq(length, thirdDataBytes.length); startGasReport("get field length (warm, 2 slots)"); - length = StoreCore.getFieldLength(_table, _key, 2, schema); + length = StoreCore.getFieldLength(_table, _key, 2, valueSchema); endGasReport(); assertEq(length, thirdDataBytes.length); } function testGetFieldSlice() public { - Schema schema = StoreCore.getSchema(_table); + Schema valueSchema = StoreCore.getValueSchema(_table); startGasReport("get field slice (cold, 1 slot)"); - bytes memory secondFieldSlice = StoreCore.getFieldSlice(_table, _key, 1, schema, 0, 4); + bytes memory secondFieldSlice = StoreCore.getFieldSlice(_table, _key, 1, valueSchema, 0, 4); endGasReport(); assertEq(secondFieldSlice, SliceLib.getSubslice(secondDataBytes, 0, 4).toBytes()); startGasReport("get field slice (warm, 1 slot)"); - secondFieldSlice = StoreCore.getFieldSlice(_table, _key, 1, schema, 4, 8); + secondFieldSlice = StoreCore.getFieldSlice(_table, _key, 1, valueSchema, 4, 8); endGasReport(); assertEq(secondFieldSlice, SliceLib.getSubslice(secondDataBytes, 4, 8).toBytes()); startGasReport("get field slice (semi-cold, 1 slot)"); - bytes memory thirdFieldSlice = StoreCore.getFieldSlice(_table, _key, 2, schema, 4, 32); + bytes memory thirdFieldSlice = StoreCore.getFieldSlice(_table, _key, 2, valueSchema, 4, 32); endGasReport(); assertEq(thirdFieldSlice, SliceLib.getSubslice(thirdDataBytes, 4, 32).toBytes()); startGasReport("get field slice (warm, 2 slots)"); - thirdFieldSlice = StoreCore.getFieldSlice(_table, _key, 2, schema, 8, 40); + thirdFieldSlice = StoreCore.getFieldSlice(_table, _key, 2, valueSchema, 8, 40); endGasReport(); assertEq(thirdFieldSlice, SliceLib.getSubslice(thirdDataBytes, 8, 40).toBytes()); } diff --git a/packages/store/test/StoreCoreGas.t.sol b/packages/store/test/StoreCoreGas.t.sol index f4849288ef..4ba03ac530 100644 --- a/packages/store/test/StoreCoreGas.t.sol +++ b/packages/store/test/StoreCoreGas.t.sol @@ -13,7 +13,6 @@ import { PackedCounter, PackedCounterLib } from "../src/PackedCounter.sol"; import { StoreReadWithStubs } from "../src/StoreReadWithStubs.sol"; import { IStoreErrors } from "../src/IStoreErrors.sol"; import { IStore } from "../src/IStore.sol"; -import { StoreMetadataData, StoreMetadata } from "../src/codegen/Tables.sol"; import { SchemaEncodeHelper } from "./SchemaEncodeHelper.sol"; import { StoreMock } from "./StoreMock.sol"; import { MirrorSubscriber } from "./MirrorSubscriber.sol"; @@ -31,16 +30,30 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { Schema defaultKeySchema = SchemaEncodeHelper.encode(SchemaType.BYTES32); function testRegisterAndGetSchema() public { - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); + Schema valueSchema = SchemaEncodeHelper.encode( + SchemaType.UINT8, + SchemaType.UINT16, + SchemaType.UINT8, + SchemaType.UINT16 + ); Schema keySchema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT16); bytes32 table = keccak256("some.table"); + string[] memory keyNames = new string[](2); + keyNames[0] = "key1"; + keyNames[1] = "key2"; + string[] memory fieldNames = new string[](4); + fieldNames[0] = "value1"; + fieldNames[1] = "value2"; + fieldNames[2] = "value3"; + fieldNames[3] = "value4"; + startGasReport("StoreCore: register schema"); - StoreCore.registerSchema(table, schema, keySchema); + StoreCore.registerTable(table, keySchema, valueSchema, keyNames, fieldNames); endGasReport(); startGasReport("StoreCore: get schema (warm)"); - StoreCore.getSchema(table); + StoreCore.getValueSchema(table); endGasReport(); startGasReport("StoreCore: get key schema (warm)"); @@ -49,10 +62,15 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { } function testHasSchema() public { - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); + Schema valueSchema = SchemaEncodeHelper.encode( + SchemaType.UINT8, + SchemaType.UINT16, + SchemaType.UINT8, + SchemaType.UINT16 + ); bytes32 table = keccak256("some.table"); bytes32 table2 = keccak256("other.table"); - StoreCore.registerSchema(table, schema, defaultKeySchema); + StoreCore.registerTable(table, defaultKeySchema, valueSchema, new string[](1), new string[](4)); startGasReport("Check for existence of table (existent)"); StoreCore.hasTable(table); @@ -63,32 +81,10 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { endGasReport(); } - function testSetMetadata() public { - bytes32 table = keccak256("some.table"); - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT16); - Schema keySchema = SchemaEncodeHelper.encode( - SchemaType.UINT8, - SchemaType.UINT16, - SchemaType.UINT8, - SchemaType.UINT16 - ); - string memory tableName = "someTable"; - string[] memory fieldNames = new string[](2); - fieldNames[0] = "field1"; - fieldNames[1] = "field2"; - - // Register table - StoreCore.registerSchema(table, schema, keySchema); - - startGasReport("StoreCore: set table metadata"); - StoreCore.setMetadata(table, tableName, fieldNames); - endGasReport(); - } - function testSetAndGetDynamicDataLength() public { bytes32 table = keccak256("some.table"); - Schema schema = SchemaEncodeHelper.encode( + Schema valueSchema = SchemaEncodeHelper.encode( SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT32, @@ -97,7 +93,7 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { ); // Register schema - StoreCore.registerSchema(table, schema, defaultKeySchema); + StoreCore.registerTable(table, defaultKeySchema, valueSchema, new string[](1), new string[](5)); // Create some key bytes32[] memory key = new bytes32[](1); @@ -121,9 +117,14 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { function testSetAndGetStaticData() public { // Register table's schema - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT16, SchemaType.UINT8, SchemaType.UINT16); + Schema valueSchema = SchemaEncodeHelper.encode( + SchemaType.UINT8, + SchemaType.UINT16, + SchemaType.UINT8, + SchemaType.UINT16 + ); bytes32 table = keccak256("some.table"); - StoreCore.registerSchema(table, schema, defaultKeySchema); + StoreCore.registerTable(table, defaultKeySchema, valueSchema, new string[](1), new string[](4)); // Set data bytes memory data = abi.encodePacked(bytes1(0x01), bytes2(0x0203), bytes1(0x04), bytes2(0x0506)); @@ -131,20 +132,20 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { key[0] = keccak256("some.key"); startGasReport("set static record (1 slot)"); - StoreCore.setRecord(table, key, data, schema); + StoreCore.setRecord(table, key, data, valueSchema); endGasReport(); // Get data startGasReport("get static record (1 slot)"); - StoreCore.getRecord(table, key, schema); + StoreCore.getRecord(table, key, valueSchema); endGasReport(); } function testSetAndGetStaticDataSpanningWords() public { // Register table's schema - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT128, SchemaType.UINT256); + Schema valueSchema = SchemaEncodeHelper.encode(SchemaType.UINT128, SchemaType.UINT256); bytes32 table = keccak256("some.table"); - StoreCore.registerSchema(table, schema, defaultKeySchema); + StoreCore.registerTable(table, defaultKeySchema, valueSchema, new string[](1), new string[](2)); // Set data bytes memory data = abi.encodePacked( @@ -156,12 +157,12 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { key[0] = keccak256("some.key"); startGasReport("set static record (2 slots)"); - StoreCore.setRecord(table, key, data, schema); + StoreCore.setRecord(table, key, data, valueSchema); endGasReport(); // Get data startGasReport("get static record (2 slots)"); - StoreCore.getRecord(table, key, schema); + StoreCore.getRecord(table, key, valueSchema); endGasReport(); } @@ -169,8 +170,12 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { bytes32 table = keccak256("some.table"); // Register table's schema - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT128, SchemaType.UINT32_ARRAY, SchemaType.UINT32_ARRAY); - StoreCore.registerSchema(table, schema, defaultKeySchema); + Schema valueSchema = SchemaEncodeHelper.encode( + SchemaType.UINT128, + SchemaType.UINT32_ARRAY, + SchemaType.UINT32_ARRAY + ); + StoreCore.registerTable(table, defaultKeySchema, valueSchema, new string[](1), new string[](3)); bytes16 firstDataBytes = bytes16(0x0102030405060708090a0b0c0d0e0f10); @@ -213,12 +218,12 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { // Set data startGasReport("set complex record with dynamic data (4 slots)"); - StoreCore.setRecord(table, key, data, schema); + StoreCore.setRecord(table, key, data, valueSchema); endGasReport(); // Get data startGasReport("get complex record with dynamic data (4 slots)"); - StoreCore.getRecord(table, key, schema); + StoreCore.getRecord(table, key, valueSchema); endGasReport(); // Compare gas - setting the data as raw struct @@ -243,13 +248,13 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { bytes32 table = keccak256("some.table"); // Register table's schema - Schema schema = SchemaEncodeHelper.encode( + Schema valueSchema = SchemaEncodeHelper.encode( SchemaType.UINT128, SchemaType.UINT256, SchemaType.UINT32_ARRAY, SchemaType.UINT32_ARRAY ); - StoreCore.registerSchema(table, schema, defaultKeySchema); + StoreCore.registerTable(table, defaultKeySchema, valueSchema, new string[](1), new string[](4)); bytes16 firstDataBytes = bytes16(0x0102030405060708090a0b0c0d0e0f10); @@ -261,7 +266,7 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { // Set first field startGasReport("set static field (1 slot)"); - StoreCore.setField(table, key, 0, firstDataPacked, schema); + StoreCore.setField(table, key, 0, firstDataPacked, valueSchema); endGasReport(); //////////////// @@ -270,7 +275,7 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { // Get first field startGasReport("get static field (1 slot)"); - StoreCore.getField(table, key, 0, schema); + StoreCore.getField(table, key, 0, valueSchema); endGasReport(); // Set second field @@ -278,12 +283,12 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { bytes memory secondDataPacked = abi.encodePacked(secondDataBytes); startGasReport("set static field (overlap 2 slot)"); - StoreCore.setField(table, key, 1, secondDataPacked, schema); + StoreCore.setField(table, key, 1, secondDataPacked, valueSchema); endGasReport(); // Get second field startGasReport("get static field (overlap 2 slot)"); - StoreCore.getField(table, key, 1, schema); + StoreCore.getField(table, key, 1, valueSchema); endGasReport(); //////////////// @@ -309,22 +314,22 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { // Set third field startGasReport("set dynamic field (1 slot, first dynamic field)"); - StoreCore.setField(table, key, 2, thirdDataBytes, schema); + StoreCore.setField(table, key, 2, thirdDataBytes, valueSchema); endGasReport(); // Get third field startGasReport("get dynamic field (1 slot, first dynamic field)"); - StoreCore.getField(table, key, 2, schema); + StoreCore.getField(table, key, 2, valueSchema); endGasReport(); // Set fourth field startGasReport("set dynamic field (1 slot, second dynamic field)"); - StoreCore.setField(table, key, 3, fourthDataBytes, schema); + StoreCore.setField(table, key, 3, fourthDataBytes, valueSchema); endGasReport(); // Get fourth field startGasReport("get dynamic field (1 slot, second dynamic field)"); - StoreCore.getField(table, key, 3, schema); + StoreCore.getField(table, key, 3, valueSchema); endGasReport(); } @@ -332,8 +337,12 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { bytes32 table = keccak256("some.table"); // Register table's schema - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT128, SchemaType.UINT32_ARRAY, SchemaType.UINT32_ARRAY); - StoreCore.registerSchema(table, schema, defaultKeySchema); + Schema valueSchema = SchemaEncodeHelper.encode( + SchemaType.UINT128, + SchemaType.UINT32_ARRAY, + SchemaType.UINT32_ARRAY + ); + StoreCore.registerTable(table, defaultKeySchema, valueSchema, new string[](1), new string[](3)); bytes16 firstDataBytes = bytes16(0x0102030405060708090a0b0c0d0e0f10); @@ -375,11 +384,11 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { key[0] = bytes32("some.key"); // Set data - StoreCore.setRecord(table, key, data, schema); + StoreCore.setRecord(table, key, data, valueSchema); // Delete data startGasReport("delete record (complex data, 3 slots)"); - StoreCore.deleteRecord(table, key, schema); + StoreCore.deleteRecord(table, key, valueSchema); endGasReport(); } @@ -387,8 +396,12 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { bytes32 table = keccak256("some.table"); // Register table's schema - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT256, SchemaType.UINT32_ARRAY, SchemaType.UINT32_ARRAY); - StoreCore.registerSchema(table, schema, defaultKeySchema); + Schema valueSchema = SchemaEncodeHelper.encode( + SchemaType.UINT256, + SchemaType.UINT32_ARRAY, + SchemaType.UINT32_ARRAY + ); + StoreCore.registerTable(table, defaultKeySchema, valueSchema, new string[](1), new string[](3)); // Create key bytes32[] memory key = new bytes32[](1); @@ -413,10 +426,10 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { } // Set fields - StoreCore.setField(table, key, 0, abi.encodePacked(firstDataBytes), schema); - StoreCore.setField(table, key, 1, secondDataBytes, schema); + StoreCore.setField(table, key, 0, abi.encodePacked(firstDataBytes), valueSchema); + StoreCore.setField(table, key, 1, secondDataBytes, valueSchema); // Initialize a field with push - StoreCore.pushToField(table, key, 2, thirdDataBytes, schema); + StoreCore.pushToField(table, key, 2, thirdDataBytes, valueSchema); // Create data to push bytes memory secondDataToPush; @@ -425,11 +438,10 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { secondData[0] = 0x25262728; secondDataToPush = EncodeArray.encode(secondData); } - bytes memory newSecondDataBytes = abi.encodePacked(secondDataBytes, secondDataToPush); // Push to second field startGasReport("push to field (1 slot, 1 uint32 item)"); - StoreCore.pushToField(table, key, 1, secondDataToPush, schema); + StoreCore.pushToField(table, key, 1, secondDataToPush, valueSchema); endGasReport(); // Create data to push @@ -451,7 +463,7 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { // Push to third field startGasReport("push to field (2 slots, 10 uint32 items)"); - StoreCore.pushToField(table, key, 2, thirdDataToPush, schema); + StoreCore.pushToField(table, key, 2, thirdDataToPush, valueSchema); endGasReport(); } @@ -470,8 +482,12 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { bytes32 table = keccak256("some.table"); // Register table's schema - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT256, SchemaType.UINT32_ARRAY, SchemaType.UINT64_ARRAY); - StoreCore.registerSchema(table, schema, defaultKeySchema); + Schema valueSchema = SchemaEncodeHelper.encode( + SchemaType.UINT256, + SchemaType.UINT32_ARRAY, + SchemaType.UINT64_ARRAY + ); + StoreCore.registerTable(table, defaultKeySchema, valueSchema, new string[](1), new string[](3)); // Create key bytes32[] memory key = new bytes32[](1); @@ -494,9 +510,9 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { data.thirdDataBytes = EncodeArray.encode(thirdData); // Set fields - StoreCore.setField(table, key, 0, abi.encodePacked(data.firstDataBytes), schema); - StoreCore.setField(table, key, 1, data.secondDataBytes, schema); - StoreCore.setField(table, key, 2, data.thirdDataBytes, schema); + StoreCore.setField(table, key, 0, abi.encodePacked(data.firstDataBytes), valueSchema); + StoreCore.setField(table, key, 1, data.secondDataBytes, valueSchema); + StoreCore.setField(table, key, 2, data.thirdDataBytes, valueSchema); // Create data to use for the update { @@ -509,7 +525,7 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { // Update index 1 in second field (4 = byte length of uint32) startGasReport("update in field (1 slot, 1 uint32 item)"); - StoreCore.updateInField(table, key, 1, 4 * 1, data.secondDataForUpdate, schema); + StoreCore.updateInField(table, key, 1, 4 * 1, data.secondDataForUpdate, valueSchema); endGasReport(); // Create data for update @@ -533,38 +549,38 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { // Update indexes 1,2,3,4 in third field (8 = byte length of uint64) startGasReport("push to field (2 slots, 6 uint64 items)"); - StoreCore.updateInField(table, key, 2, 8 * 1, data.thirdDataForUpdate, schema); + StoreCore.updateInField(table, key, 2, 8 * 1, data.thirdDataForUpdate, valueSchema); endGasReport(); } function testAccessEmptyData() public { bytes32 table = keccak256("some.table"); - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT32, SchemaType.UINT32_ARRAY); + Schema valueSchema = SchemaEncodeHelper.encode(SchemaType.UINT32, SchemaType.UINT32_ARRAY); - StoreCore.registerSchema(table, schema, defaultKeySchema); + StoreCore.registerTable(table, defaultKeySchema, valueSchema, new string[](1), new string[](2)); // Create key bytes32[] memory key = new bytes32[](1); key[0] = bytes32("some.key"); startGasReport("access non-existing record"); - StoreCore.getRecord(table, key, schema); + StoreCore.getRecord(table, key, valueSchema); endGasReport(); startGasReport("access static field of non-existing record"); - StoreCore.getField(table, key, 0, schema); + StoreCore.getField(table, key, 0, valueSchema); endGasReport(); startGasReport("access dynamic field of non-existing record"); - StoreCore.getField(table, key, 1, schema); + StoreCore.getField(table, key, 1, valueSchema); endGasReport(); startGasReport("access length of dynamic field of non-existing record"); - StoreCore.getFieldLength(table, key, 1, schema); + StoreCore.getFieldLength(table, key, 1, valueSchema); endGasReport(); startGasReport("access slice of dynamic field of non-existing record"); - StoreCore.getFieldSlice(table, key, 1, schema, 0, 0); + StoreCore.getFieldSlice(table, key, 1, valueSchema, 0, 0); endGasReport(); } @@ -574,11 +590,17 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { key[0] = keccak256("some key"); // Register table's schema - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT128); - StoreCore.registerSchema(table, schema, defaultKeySchema); + Schema valueSchema = SchemaEncodeHelper.encode(SchemaType.UINT128); + StoreCore.registerTable(table, defaultKeySchema, valueSchema, new string[](1), new string[](1)); // Create subscriber - MirrorSubscriber subscriber = new MirrorSubscriber(table, schema, defaultKeySchema); + MirrorSubscriber subscriber = new MirrorSubscriber( + table, + defaultKeySchema, + valueSchema, + new string[](1), + new string[](1) + ); startGasReport("register subscriber"); StoreCore.registerStoreHook(table, subscriber); @@ -587,17 +609,17 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { bytes memory data = abi.encodePacked(bytes16(0x0102030405060708090a0b0c0d0e0f10)); startGasReport("set record on table with subscriber"); - StoreCore.setRecord(table, key, data, schema); + StoreCore.setRecord(table, key, data, valueSchema); endGasReport(); data = abi.encodePacked(bytes16(0x1112131415161718191a1b1c1d1e1f20)); startGasReport("set static field on table with subscriber"); - StoreCore.setField(table, key, 0, data, schema); + StoreCore.setField(table, key, 0, data, valueSchema); endGasReport(); startGasReport("delete record on table with subscriber"); - StoreCore.deleteRecord(table, key, schema); + StoreCore.deleteRecord(table, key, valueSchema); endGasReport(); } @@ -607,11 +629,17 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { key[0] = keccak256("some key"); // Register table's schema - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT128, SchemaType.UINT32_ARRAY); - StoreCore.registerSchema(table, schema, defaultKeySchema); + Schema valueSchema = SchemaEncodeHelper.encode(SchemaType.UINT128, SchemaType.UINT32_ARRAY); + StoreCore.registerTable(table, defaultKeySchema, valueSchema, new string[](1), new string[](2)); // Create subscriber - MirrorSubscriber subscriber = new MirrorSubscriber(table, schema, defaultKeySchema); + MirrorSubscriber subscriber = new MirrorSubscriber( + table, + defaultKeySchema, + valueSchema, + new string[](1), + new string[](2) + ); startGasReport("register subscriber"); StoreCore.registerStoreHook(table, subscriber); @@ -626,7 +654,7 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { bytes memory data = abi.encodePacked(staticData, dynamicData); startGasReport("set (dynamic) record on table with subscriber"); - StoreCore.setRecord(table, key, data, schema); + StoreCore.setRecord(table, key, data, valueSchema); endGasReport(); // Update dynamic data @@ -636,11 +664,11 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { data = abi.encodePacked(staticData, dynamicData); startGasReport("set (dynamic) field on table with subscriber"); - StoreCore.setField(table, key, 1, arrayDataBytes, schema); + StoreCore.setField(table, key, 1, arrayDataBytes, valueSchema); endGasReport(); startGasReport("delete (dynamic) record on table with subscriber"); - StoreCore.deleteRecord(table, key, schema); + StoreCore.deleteRecord(table, key, valueSchema); endGasReport(); } } diff --git a/packages/store/test/StoreMetadata.t.sol b/packages/store/test/StoreMetadata.t.sol deleted file mode 100644 index 811cb5d18c..0000000000 --- a/packages/store/test/StoreMetadata.t.sol +++ /dev/null @@ -1,30 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity >=0.8.0; - -import { Test } from "forge-std/Test.sol"; -import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol"; -import { StoreMetadata, StoreMetadataData } from "../src/codegen/Tables.sol"; -import { StoreCore } from "../src/StoreCore.sol"; -import { StoreReadWithStubs } from "../src/StoreReadWithStubs.sol"; -import { Schema } from "../src/Schema.sol"; - -contract StoreMetadataTest is Test, GasReporter, StoreReadWithStubs { - function testSetAndGet() public { - bytes32 tableId = "1"; - string memory tableName = "firstTable"; - string[] memory fieldNames = new string[](2); - fieldNames[0] = "firstField"; - fieldNames[1] = "secondField"; - - startGasReport("set record in StoreMetadataTable"); - StoreMetadata.set({ tableId: tableId, tableName: tableName, abiEncodedFieldNames: abi.encode(fieldNames) }); - endGasReport(); - - startGasReport("get record from StoreMetadataTable"); - StoreMetadataData memory metadata = StoreMetadata.get(tableId); - endGasReport(); - - assertEq(metadata.tableName, tableName); - assertEq(metadata.abiEncodedFieldNames, abi.encode(fieldNames)); - } -} diff --git a/packages/store/test/StoreMock.sol b/packages/store/test/StoreMock.sol index 6f457de05e..d58e7299c9 100644 --- a/packages/store/test/StoreMock.sol +++ b/packages/store/test/StoreMock.sol @@ -70,12 +70,14 @@ contract StoreMock is IStore, StoreRead { StoreCore.emitEphemeralRecord(table, key, data, valueSchema); } - function registerSchema(bytes32 table, Schema schema, Schema keySchema) public { - StoreCore.registerSchema(table, schema, keySchema); - } - - function setMetadata(bytes32 table, string calldata tableName, string[] calldata fieldNames) public { - StoreCore.setMetadata(table, tableName, fieldNames); + function registerTable( + bytes32 table, + Schema keySchema, + Schema valueSchema, + string[] calldata keyNames, + string[] calldata fieldNames + ) public { + StoreCore.registerTable(table, keySchema, valueSchema, keyNames, fieldNames); } // Register hook to be called when a record or field is set or deleted diff --git a/packages/store/test/Vector2.t.sol b/packages/store/test/Vector2.t.sol index 53fbd4c2a1..a10aafcc72 100644 --- a/packages/store/test/Vector2.t.sol +++ b/packages/store/test/Vector2.t.sol @@ -11,17 +11,17 @@ import { Schema } from "../src/Schema.sol"; contract Vector2Test is Test, GasReporter, StoreReadWithStubs { function testRegisterAndGetSchema() public { startGasReport("register Vector2 schema"); - Vector2.registerSchema(); + Vector2.register(); endGasReport(); - Schema registeredSchema = StoreCore.getSchema(Vector2TableId); - Schema declaredSchema = Vector2.getSchema(); + Schema registeredSchema = StoreCore.getValueSchema(Vector2TableId); + Schema declaredSchema = Vector2.getValueSchema(); - assertEq(keccak256(abi.encode(registeredSchema)), keccak256(abi.encode(declaredSchema))); + assertEq(Schema.unwrap(registeredSchema), Schema.unwrap(declaredSchema)); } function testSetAndGet() public { - Vector2.registerSchema(); + Vector2.register(); bytes32 key = keccak256("somekey"); startGasReport("set Vector2 record"); diff --git a/packages/store/test/tables/Callbacks.t.sol b/packages/store/test/tables/Callbacks.t.sol index d96e1ffe9e..3213bdb3ee 100644 --- a/packages/store/test/tables/Callbacks.t.sol +++ b/packages/store/test/tables/Callbacks.t.sol @@ -8,7 +8,7 @@ import { Callbacks } from "../../src/codegen/Tables.sol"; contract CallbacksTest is Test, GasReporter, StoreReadWithStubs { function testSetAndGet() public { - Callbacks.registerSchema(); + Callbacks.register(); bytes32 key = keccak256("somekey"); bytes24[] memory callbacks = new bytes24[](1); diff --git a/packages/store/ts/codegen/ephemeral.ts b/packages/store/ts/codegen/ephemeral.ts index 7e3d5b24da..21759a1c74 100644 --- a/packages/store/ts/codegen/ephemeral.ts +++ b/packages/store/ts/codegen/ephemeral.ts @@ -19,7 +19,7 @@ export function renderEphemeralMethods(options: RenderTableOptions) { ${_keyTupleDefinition} - ${_store}.emitEphemeralRecord(_tableId, _keyTuple, _data, getSchema()); + ${_store}.emitEphemeralRecord(_tableId, _keyTuple, _data, getValueSchema()); } ` ); diff --git a/packages/store/ts/codegen/field.ts b/packages/store/ts/codegen/field.ts index db86a03a66..928a68750b 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}, getSchema()); + bytes memory _blob = ${_store}.getField(_tableId, _keyTuple, ${schemaIndex}, getValueSchema()); return ${renderDecodeFieldSingle(field)}; } ` @@ -42,7 +42,7 @@ export function renderFieldMethods(options: RenderTableOptions) { _typedFieldName, ])}) internal { ${_keyTupleDefinition} - ${_store}.setField(_tableId, _keyTuple, ${schemaIndex}, ${renderEncodeFieldSingle(field)}, getSchema()); + ${_store}.setField(_tableId, _keyTuple, ${schemaIndex}, ${renderEncodeFieldSingle(field)}, getValueSchema()); } ` ); @@ -60,7 +60,7 @@ export function renderFieldMethods(options: RenderTableOptions) { _typedKeyArgs, ])}) internal view returns (uint256) { ${_keyTupleDefinition} - uint256 _byteLength = ${_store}.getFieldLength(_tableId, _keyTuple, ${schemaIndex}, getSchema()); + uint256 _byteLength = ${_store}.getFieldLength(_tableId, _keyTuple, ${schemaIndex}, getValueSchema()); unchecked { return _byteLength / ${portionData.elementLength}; } @@ -87,7 +87,7 @@ export function renderFieldMethods(options: RenderTableOptions) { _tableId, _keyTuple, ${schemaIndex}, - getSchema(), + getValueSchema(), _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}, getSchema()); + ${_store}.pushToField(_tableId, _keyTuple, ${schemaIndex}, ${portionData.encoded}, getValueSchema()); } ` ); @@ -123,7 +123,7 @@ export function renderFieldMethods(options: RenderTableOptions) { _typedKeyArgs, ])}) internal { ${_keyTupleDefinition} - ${_store}.popFromField(_tableId, _keyTuple, ${schemaIndex}, ${portionData.elementLength}, getSchema()); + ${_store}.popFromField(_tableId, _keyTuple, ${schemaIndex}, ${portionData.elementLength}, getValueSchema()); } ` ); @@ -150,7 +150,7 @@ export function renderFieldMethods(options: RenderTableOptions) { ${schemaIndex}, _index * ${portionData.elementLength}, ${portionData.encoded}, - getSchema() + getValueSchema() ); } } diff --git a/packages/store/ts/codegen/record.ts b/packages/store/ts/codegen/record.ts index 1a6aab3e86..d2182eb8f3 100644 --- a/packages/store/ts/codegen/record.ts +++ b/packages/store/ts/codegen/record.ts @@ -22,7 +22,7 @@ export function renderRecordMethods(options: RenderTableOptions) { _typedKeyArgs, ])}) internal view returns (${renderDecodedRecord(options)}) { ${_keyTupleDefinition} - bytes memory _blob = ${_store}.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = ${_store}.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } ` @@ -42,7 +42,7 @@ export function renderRecordMethods(options: RenderTableOptions) { ${_keyTupleDefinition} - ${_store}.setRecord(_tableId, _keyTuple, _data, getSchema()); + ${_store}.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } ` ); diff --git a/packages/store/ts/codegen/renderTable.ts b/packages/store/ts/codegen/renderTable.ts index ecc482660c..7549a52ad6 100644 --- a/packages/store/ts/codegen/renderTable.ts +++ b/packages/store/ts/codegen/renderTable.ts @@ -73,44 +73,40 @@ ${ } library ${libraryName} { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](${fields.length}); - ${renderList(fields, ({ enumName }, index) => `_schema[${index}] = SchemaType.${enumName};`)} + /** Get the table's key schema */ + function getKeySchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](${keyTuple.length}); + ${renderList(keyTuple, ({ enumName }, index) => `_schema[${index}] = SchemaType.${enumName};`)} return SchemaLib.encode(_schema); } - function getKeySchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](${keyTuple.length}); - ${renderList(keyTuple, ({ enumName }, index) => `_schema[${index}] = SchemaType.${enumName};`)} + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](${fields.length}); + ${renderList(fields, ({ enumName }, index) => `_schema[${index}] = SchemaType.${enumName};`)} return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](${fields.length}); - ${renderList(fields, (field, index) => `_fieldNames[${index}] = "${field.name}";`)} - return ("${libraryName}", _fieldNames); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](${keyTuple.length}); + ${renderList(keyTuple, (keyElement, index) => `keyNames[${index}] = "${keyElement.name}";`)} + } + + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](${fields.length}); + ${renderList(fields, (field, index) => `fieldNames[${index}] = "${field.name}";`)} } ${renderWithStore( storeArgument, (_typedStore, _store, _commentSuffix) => ` - /** Register the table's schema${_commentSuffix} */ - function registerSchema(${renderArguments([_typedStore, _typedTableId])}) internal { - ${_store}.registerSchema(_tableId, getSchema(), getKeySchema()); - } - ` - )} - ${renderWithStore( - storeArgument, - (_typedStore, _store, _commentSuffix) => ` - /** Set the table's metadata${_commentSuffix} */ - function setMetadata(${renderArguments([_typedStore, _typedTableId])}) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - ${_store}.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names${_commentSuffix} */ + function register(${renderArguments([_typedStore, _typedTableId])}) internal { + ${_store}.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } ` )} @@ -148,7 +144,7 @@ library ${libraryName} { /* Delete all data for given keys${_commentSuffix} */ function deleteRecord(${renderArguments([_typedStore, _typedTableId, _typedKeyArgs])}) internal { ${_keyTupleDefinition} - ${_store}.deleteRecord(_tableId, _keyTuple, getSchema()); + ${_store}.deleteRecord(_tableId, _keyTuple, getValueSchema()); } ` ) diff --git a/packages/world/abi/CoreModule.sol/CoreModule.abi.json b/packages/world/abi/CoreModule.sol/CoreModule.abi.json index 1bb5e1625b..7aca0b168b 100644 --- a/packages/world/abi/CoreModule.sol/CoreModule.abi.json +++ b/packages/world/abi/CoreModule.sol/CoreModule.abi.json @@ -82,17 +82,17 @@ { "inputs": [ { - "internalType": "bytes32", - "name": "tableId", - "type": "bytes32" + "internalType": "uint256", + "name": "expected", + "type": "uint256" }, { - "internalType": "string", - "name": "tableIdString", - "type": "string" + "internalType": "uint256", + "name": "received", + "type": "uint256" } ], - "name": "StoreCore_TableAlreadyExists", + "name": "StoreCore_InvalidKeyNamesLength", "type": "error" }, { @@ -108,7 +108,7 @@ "type": "string" } ], - "name": "StoreCore_TableNotFound", + "name": "StoreCore_TableAlreadyExists", "type": "error" }, { diff --git a/packages/world/abi/CoreSystem.sol/CoreSystem.abi.json b/packages/world/abi/CoreSystem.sol/CoreSystem.abi.json index a887a2245e..acc9523276 100644 --- a/packages/world/abi/CoreSystem.sol/CoreSystem.abi.json +++ b/packages/world/abi/CoreSystem.sol/CoreSystem.abi.json @@ -150,25 +150,25 @@ "name": "StoreCore_InvalidFieldNamesLength", "type": "error" }, - { - "inputs": [], - "name": "StoreCore_NotDynamicField", - "type": "error" - }, { "inputs": [ { - "internalType": "bytes32", - "name": "tableId", - "type": "bytes32" + "internalType": "uint256", + "name": "expected", + "type": "uint256" }, { - "internalType": "string", - "name": "tableIdString", - "type": "string" + "internalType": "uint256", + "name": "received", + "type": "uint256" } ], - "name": "StoreCore_TableAlreadyExists", + "name": "StoreCore_InvalidKeyNamesLength", + "type": "error" + }, + { + "inputs": [], + "name": "StoreCore_NotDynamicField", "type": "error" }, { @@ -184,7 +184,7 @@ "type": "string" } ], - "name": "StoreCore_TableNotFound", + "name": "StoreCore_TableAlreadyExists", "type": "error" }, { @@ -429,29 +429,6 @@ "stateMutability": "nonpayable", "type": "function" }, - { - "inputs": [ - { - "internalType": "bytes32", - "name": "tableId", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "valueSchema", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "keySchema", - "type": "bytes32" - } - ], - "name": "registerSchema", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, { "inputs": [ { @@ -541,13 +518,23 @@ }, { "internalType": "Schema", - "name": "valueSchema", + "name": "keySchema", "type": "bytes32" }, { "internalType": "Schema", - "name": "keySchema", + "name": "valueSchema", "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" + }, + { + "internalType": "string[]", + "name": "fieldNames", + "type": "string[]" } ], "name": "registerTable", @@ -564,22 +551,32 @@ { "inputs": [ { - "internalType": "bytes16", - "name": "namespace", - "type": "bytes16" + "internalType": "bytes32", + "name": "tableId", + "type": "bytes32" }, { - "internalType": "bytes16", - "name": "name", - "type": "bytes16" + "internalType": "Schema", + "name": "keySchema", + "type": "bytes32" }, { - "internalType": "contract IStoreHook", - "name": "hook", - "type": "address" + "internalType": "Schema", + "name": "valueSchema", + "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" + }, + { + "internalType": "string[]", + "name": "fieldNames", + "type": "string[]" } ], - "name": "registerTableHook", + "name": "registerTable", "outputs": [], "stateMutability": "nonpayable", "type": "function" @@ -597,12 +594,12 @@ "type": "bytes16" }, { - "internalType": "address", - "name": "grantee", + "internalType": "contract IStoreHook", + "name": "hook", "type": "address" } ], - "name": "revokeAccess", + "name": "registerTableHook", "outputs": [], "stateMutability": "nonpayable", "type": "function" @@ -620,40 +617,12 @@ "type": "bytes16" }, { - "internalType": "string", - "name": "tableName", - "type": "string" - }, - { - "internalType": "string[]", - "name": "fieldNames", - "type": "string[]" - } - ], - "name": "setMetadata", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, - { - "inputs": [ - { - "internalType": "bytes32", - "name": "tableId", - "type": "bytes32" - }, - { - "internalType": "string", - "name": "tableName", - "type": "string" - }, - { - "internalType": "string[]", - "name": "fieldNames", - "type": "string[]" + "internalType": "address", + "name": "grantee", + "type": "address" } ], - "name": "setMetadata", + "name": "revokeAccess", "outputs": [], "stateMutability": "nonpayable", "type": "function" diff --git a/packages/world/abi/IBaseWorld.sol/IBaseWorld.abi.json b/packages/world/abi/IBaseWorld.sol/IBaseWorld.abi.json index 29c6d35666..760cba3ce8 100644 --- a/packages/world/abi/IBaseWorld.sol/IBaseWorld.abi.json +++ b/packages/world/abi/IBaseWorld.sol/IBaseWorld.abi.json @@ -129,6 +129,22 @@ "name": "StoreCore_InvalidFieldNamesLength", "type": "error" }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "expected", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "received", + "type": "uint256" + } + ], + "name": "StoreCore_InvalidKeyNamesLength", + "type": "error" + }, { "inputs": [], "name": "StoreCore_NotDynamicField", @@ -597,7 +613,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", @@ -913,29 +929,6 @@ "stateMutability": "nonpayable", "type": "function" }, - { - "inputs": [ - { - "internalType": "bytes32", - "name": "table", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "schema", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "keySchema", - "type": "bytes32" - } - ], - "name": "registerSchema", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, { "inputs": [ { @@ -1025,13 +1018,23 @@ }, { "internalType": "Schema", - "name": "valueSchema", + "name": "keySchema", "type": "bytes32" }, { "internalType": "Schema", - "name": "keySchema", + "name": "valueSchema", "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" + }, + { + "internalType": "string[]", + "name": "fieldNames", + "type": "string[]" } ], "name": "registerTable", @@ -1045,6 +1048,39 @@ "stateMutability": "nonpayable", "type": "function" }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "table", + "type": "bytes32" + }, + { + "internalType": "Schema", + "name": "keySchema", + "type": "bytes32" + }, + { + "internalType": "Schema", + "name": "valueSchema", + "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" + }, + { + "internalType": "string[]", + "name": "fieldNames", + "type": "string[]" + } + ], + "name": "registerTable", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, { "inputs": [ { @@ -1162,57 +1198,6 @@ "stateMutability": "nonpayable", "type": "function" }, - { - "inputs": [ - { - "internalType": "bytes16", - "name": "namespace", - "type": "bytes16" - }, - { - "internalType": "bytes16", - "name": "name", - "type": "bytes16" - }, - { - "internalType": "string", - "name": "tableName", - "type": "string" - }, - { - "internalType": "string[]", - "name": "fieldNames", - "type": "string[]" - } - ], - "name": "setMetadata", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, - { - "inputs": [ - { - "internalType": "bytes32", - "name": "table", - "type": "bytes32" - }, - { - "internalType": "string", - "name": "tableName", - "type": "string" - }, - { - "internalType": "string[]", - "name": "fieldNames", - "type": "string[]" - } - ], - "name": "setMetadata", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, { "inputs": [ { diff --git a/packages/world/abi/IStore.sol/IStore.abi.json b/packages/world/abi/IStore.sol/IStore.abi.json index 3dc7bc3b3a..d21e6f935f 100644 --- a/packages/world/abi/IStore.sol/IStore.abi.json +++ b/packages/world/abi/IStore.sol/IStore.abi.json @@ -47,6 +47,22 @@ "name": "StoreCore_InvalidFieldNamesLength", "type": "error" }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "expected", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "received", + "type": "uint256" + } + ], + "name": "StoreCore_InvalidKeyNamesLength", + "type": "error" + }, { "inputs": [], "name": "StoreCore_NotDynamicField", @@ -408,7 +424,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", @@ -493,17 +509,12 @@ "type": "bytes32" }, { - "internalType": "Schema", - "name": "schema", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "keySchema", - "type": "bytes32" + "internalType": "contract IStoreHook", + "name": "hook", + "type": "address" } ], - "name": "registerSchema", + "name": "registerStoreHook", "outputs": [], "stateMutability": "nonpayable", "type": "function" @@ -516,12 +527,27 @@ "type": "bytes32" }, { - "internalType": "contract IStoreHook", - "name": "hook", - "type": "address" + "internalType": "Schema", + "name": "keySchema", + "type": "bytes32" + }, + { + "internalType": "Schema", + "name": "valueSchema", + "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" + }, + { + "internalType": "string[]", + "name": "fieldNames", + "type": "string[]" } ], - "name": "registerStoreHook", + "name": "registerTable", "outputs": [], "stateMutability": "nonpayable", "type": "function" @@ -559,29 +585,6 @@ "stateMutability": "nonpayable", "type": "function" }, - { - "inputs": [ - { - "internalType": "bytes32", - "name": "table", - "type": "bytes32" - }, - { - "internalType": "string", - "name": "tableName", - "type": "string" - }, - { - "internalType": "string[]", - "name": "fieldNames", - "type": "string[]" - } - ], - "name": "setMetadata", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, { "inputs": [ { diff --git a/packages/world/abi/IStore.sol/IStoreData.abi.json b/packages/world/abi/IStore.sol/IStoreData.abi.json index 8bdc4379fa..f02203d5e9 100644 --- a/packages/world/abi/IStore.sol/IStoreData.abi.json +++ b/packages/world/abi/IStore.sol/IStoreData.abi.json @@ -265,7 +265,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", diff --git a/packages/world/abi/IStore.sol/IStoreRead.abi.json b/packages/world/abi/IStore.sol/IStoreRead.abi.json index ca89eb4ba5..0f47144f3d 100644 --- a/packages/world/abi/IStore.sol/IStoreRead.abi.json +++ b/packages/world/abi/IStore.sol/IStoreRead.abi.json @@ -167,7 +167,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", diff --git a/packages/world/abi/IStore.sol/IStoreRegistration.abi.json b/packages/world/abi/IStore.sol/IStoreRegistration.abi.json index 2bf0123223..0a30f85789 100644 --- a/packages/world/abi/IStore.sol/IStoreRegistration.abi.json +++ b/packages/world/abi/IStore.sol/IStoreRegistration.abi.json @@ -1,27 +1,4 @@ [ - { - "inputs": [ - { - "internalType": "bytes32", - "name": "table", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "schema", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "keySchema", - "type": "bytes32" - } - ], - "name": "registerSchema", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, { "inputs": [ { @@ -48,9 +25,19 @@ "type": "bytes32" }, { - "internalType": "string", - "name": "tableName", - "type": "string" + "internalType": "Schema", + "name": "keySchema", + "type": "bytes32" + }, + { + "internalType": "Schema", + "name": "valueSchema", + "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" }, { "internalType": "string[]", @@ -58,7 +45,7 @@ "type": "string[]" } ], - "name": "setMetadata", + "name": "registerTable", "outputs": [], "stateMutability": "nonpayable", "type": "function" diff --git a/packages/world/abi/IStoreErrors.sol/IStoreErrors.abi.json b/packages/world/abi/IStoreErrors.sol/IStoreErrors.abi.json index d38ae0cc0d..cf6d375396 100644 --- a/packages/world/abi/IStoreErrors.sol/IStoreErrors.abi.json +++ b/packages/world/abi/IStoreErrors.sol/IStoreErrors.abi.json @@ -47,6 +47,22 @@ "name": "StoreCore_InvalidFieldNamesLength", "type": "error" }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "expected", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "received", + "type": "uint256" + } + ], + "name": "StoreCore_InvalidKeyNamesLength", + "type": "error" + }, { "inputs": [], "name": "StoreCore_NotDynamicField", diff --git a/packages/world/abi/IWorldRegistrationSystem.sol/IWorldRegistrationSystem.abi.json b/packages/world/abi/IWorldRegistrationSystem.sol/IWorldRegistrationSystem.abi.json index 5552356eb8..6c5efc1fd5 100644 --- a/packages/world/abi/IWorldRegistrationSystem.sol/IWorldRegistrationSystem.abi.json +++ b/packages/world/abi/IWorldRegistrationSystem.sol/IWorldRegistrationSystem.abi.json @@ -174,13 +174,23 @@ }, { "internalType": "Schema", - "name": "valueSchema", + "name": "keySchema", "type": "bytes32" }, { "internalType": "Schema", - "name": "keySchema", + "name": "valueSchema", "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" + }, + { + "internalType": "string[]", + "name": "fieldNames", + "type": "string[]" } ], "name": "registerTable", @@ -216,33 +226,5 @@ "outputs": [], "stateMutability": "nonpayable", "type": "function" - }, - { - "inputs": [ - { - "internalType": "bytes16", - "name": "namespace", - "type": "bytes16" - }, - { - "internalType": "bytes16", - "name": "name", - "type": "bytes16" - }, - { - "internalType": "string", - "name": "tableName", - "type": "string" - }, - { - "internalType": "string[]", - "name": "fieldNames", - "type": "string[]" - } - ], - "name": "setMetadata", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" } ] \ No newline at end of file diff --git a/packages/world/abi/StoreRead.sol/StoreRead.abi.json b/packages/world/abi/StoreRead.sol/StoreRead.abi.json index b05f278530..93a0e17cf5 100644 --- a/packages/world/abi/StoreRead.sol/StoreRead.abi.json +++ b/packages/world/abi/StoreRead.sol/StoreRead.abi.json @@ -73,6 +73,22 @@ "name": "StoreCore_InvalidFieldNamesLength", "type": "error" }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "expected", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "received", + "type": "uint256" + } + ], + "name": "StoreCore_InvalidKeyNamesLength", + "type": "error" + }, { "inputs": [], "name": "StoreCore_NotDynamicField", @@ -278,7 +294,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", diff --git a/packages/world/abi/StoreRegistrationSystem.sol/StoreRegistrationSystem.abi.json b/packages/world/abi/StoreRegistrationSystem.sol/StoreRegistrationSystem.abi.json index 7815ac594a..fb6658c07a 100644 --- a/packages/world/abi/StoreRegistrationSystem.sol/StoreRegistrationSystem.abi.json +++ b/packages/world/abi/StoreRegistrationSystem.sol/StoreRegistrationSystem.abi.json @@ -15,29 +15,6 @@ "name": "SchemaLib_StaticTypeAfterDynamicType", "type": "error" }, - { - "inputs": [ - { - "internalType": "bytes32", - "name": "tableId", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "valueSchema", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "keySchema", - "type": "bytes32" - } - ], - "name": "registerSchema", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, { "inputs": [ { @@ -64,9 +41,19 @@ "type": "bytes32" }, { - "internalType": "string", - "name": "tableName", - "type": "string" + "internalType": "Schema", + "name": "keySchema", + "type": "bytes32" + }, + { + "internalType": "Schema", + "name": "valueSchema", + "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" }, { "internalType": "string[]", @@ -74,7 +61,7 @@ "type": "string[]" } ], - "name": "setMetadata", + "name": "registerTable", "outputs": [], "stateMutability": "nonpayable", "type": "function" diff --git a/packages/world/abi/StoreMetadata.sol/StoreMetadata.abi.json b/packages/world/abi/Tables.sol/Tables.abi.json similarity index 100% rename from packages/world/abi/StoreMetadata.sol/StoreMetadata.abi.json rename to packages/world/abi/Tables.sol/Tables.abi.json diff --git a/packages/world/abi/World.sol/World.abi.json b/packages/world/abi/World.sol/World.abi.json index aad1a60dad..24c4c756dc 100644 --- a/packages/world/abi/World.sol/World.abi.json +++ b/packages/world/abi/World.sol/World.abi.json @@ -171,6 +171,22 @@ "name": "StoreCore_InvalidFieldNamesLength", "type": "error" }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "expected", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "received", + "type": "uint256" + } + ], + "name": "StoreCore_InvalidKeyNamesLength", + "type": "error" + }, { "inputs": [], "name": "StoreCore_NotDynamicField", @@ -552,7 +568,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", diff --git a/packages/world/abi/WorldRegistrationSystem.sol/WorldRegistrationSystem.abi.json b/packages/world/abi/WorldRegistrationSystem.sol/WorldRegistrationSystem.abi.json index c0f14dc135..7bf047f4f4 100644 --- a/packages/world/abi/WorldRegistrationSystem.sol/WorldRegistrationSystem.abi.json +++ b/packages/world/abi/WorldRegistrationSystem.sol/WorldRegistrationSystem.abi.json @@ -150,25 +150,25 @@ "name": "StoreCore_InvalidFieldNamesLength", "type": "error" }, - { - "inputs": [], - "name": "StoreCore_NotDynamicField", - "type": "error" - }, { "inputs": [ { - "internalType": "bytes32", - "name": "tableId", - "type": "bytes32" + "internalType": "uint256", + "name": "expected", + "type": "uint256" }, { - "internalType": "string", - "name": "tableIdString", - "type": "string" + "internalType": "uint256", + "name": "received", + "type": "uint256" } ], - "name": "StoreCore_TableAlreadyExists", + "name": "StoreCore_InvalidKeyNamesLength", + "type": "error" + }, + { + "inputs": [], + "name": "StoreCore_NotDynamicField", "type": "error" }, { @@ -184,7 +184,7 @@ "type": "string" } ], - "name": "StoreCore_TableNotFound", + "name": "StoreCore_TableAlreadyExists", "type": "error" }, { @@ -373,13 +373,23 @@ }, { "internalType": "Schema", - "name": "valueSchema", + "name": "keySchema", "type": "bytes32" }, { "internalType": "Schema", - "name": "keySchema", + "name": "valueSchema", "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" + }, + { + "internalType": "string[]", + "name": "fieldNames", + "type": "string[]" } ], "name": "registerTable", @@ -415,33 +425,5 @@ "outputs": [], "stateMutability": "nonpayable", "type": "function" - }, - { - "inputs": [ - { - "internalType": "bytes16", - "name": "namespace", - "type": "bytes16" - }, - { - "internalType": "bytes16", - "name": "name", - "type": "bytes16" - }, - { - "internalType": "string", - "name": "tableName", - "type": "string" - }, - { - "internalType": "string[]", - "name": "fieldNames", - "type": "string[]" - } - ], - "name": "setMetadata", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" } ] \ No newline at end of file diff --git a/packages/world/abi/src/IStore.sol/IStore.abi.json b/packages/world/abi/src/IStore.sol/IStore.abi.json index 3dc7bc3b3a..d21e6f935f 100644 --- a/packages/world/abi/src/IStore.sol/IStore.abi.json +++ b/packages/world/abi/src/IStore.sol/IStore.abi.json @@ -47,6 +47,22 @@ "name": "StoreCore_InvalidFieldNamesLength", "type": "error" }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "expected", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "received", + "type": "uint256" + } + ], + "name": "StoreCore_InvalidKeyNamesLength", + "type": "error" + }, { "inputs": [], "name": "StoreCore_NotDynamicField", @@ -408,7 +424,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", @@ -493,17 +509,12 @@ "type": "bytes32" }, { - "internalType": "Schema", - "name": "schema", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "keySchema", - "type": "bytes32" + "internalType": "contract IStoreHook", + "name": "hook", + "type": "address" } ], - "name": "registerSchema", + "name": "registerStoreHook", "outputs": [], "stateMutability": "nonpayable", "type": "function" @@ -516,12 +527,27 @@ "type": "bytes32" }, { - "internalType": "contract IStoreHook", - "name": "hook", - "type": "address" + "internalType": "Schema", + "name": "keySchema", + "type": "bytes32" + }, + { + "internalType": "Schema", + "name": "valueSchema", + "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" + }, + { + "internalType": "string[]", + "name": "fieldNames", + "type": "string[]" } ], - "name": "registerStoreHook", + "name": "registerTable", "outputs": [], "stateMutability": "nonpayable", "type": "function" @@ -559,29 +585,6 @@ "stateMutability": "nonpayable", "type": "function" }, - { - "inputs": [ - { - "internalType": "bytes32", - "name": "table", - "type": "bytes32" - }, - { - "internalType": "string", - "name": "tableName", - "type": "string" - }, - { - "internalType": "string[]", - "name": "fieldNames", - "type": "string[]" - } - ], - "name": "setMetadata", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, { "inputs": [ { diff --git a/packages/world/abi/src/IStore.sol/IStoreData.abi.json b/packages/world/abi/src/IStore.sol/IStoreData.abi.json index 8bdc4379fa..f02203d5e9 100644 --- a/packages/world/abi/src/IStore.sol/IStoreData.abi.json +++ b/packages/world/abi/src/IStore.sol/IStoreData.abi.json @@ -265,7 +265,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", diff --git a/packages/world/abi/src/IStore.sol/IStoreRead.abi.json b/packages/world/abi/src/IStore.sol/IStoreRead.abi.json index ca89eb4ba5..0f47144f3d 100644 --- a/packages/world/abi/src/IStore.sol/IStoreRead.abi.json +++ b/packages/world/abi/src/IStore.sol/IStoreRead.abi.json @@ -167,7 +167,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", diff --git a/packages/world/abi/src/IStore.sol/IStoreRegistration.abi.json b/packages/world/abi/src/IStore.sol/IStoreRegistration.abi.json index 2bf0123223..0a30f85789 100644 --- a/packages/world/abi/src/IStore.sol/IStoreRegistration.abi.json +++ b/packages/world/abi/src/IStore.sol/IStoreRegistration.abi.json @@ -1,27 +1,4 @@ [ - { - "inputs": [ - { - "internalType": "bytes32", - "name": "table", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "schema", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "keySchema", - "type": "bytes32" - } - ], - "name": "registerSchema", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, { "inputs": [ { @@ -48,9 +25,19 @@ "type": "bytes32" }, { - "internalType": "string", - "name": "tableName", - "type": "string" + "internalType": "Schema", + "name": "keySchema", + "type": "bytes32" + }, + { + "internalType": "Schema", + "name": "valueSchema", + "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" }, { "internalType": "string[]", @@ -58,7 +45,7 @@ "type": "string[]" } ], - "name": "setMetadata", + "name": "registerTable", "outputs": [], "stateMutability": "nonpayable", "type": "function" diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json index 16d2f3c61b..5a1369fc66 100644 --- a/packages/world/gas-report.json +++ b/packages/world/gas-report.json @@ -3,73 +3,73 @@ "file": "test/KeysInTableModule.t.sol", "test": "testInstallComposite", "name": "install keys in table module", - "gasUsed": 1245500 + "gasUsed": 1489422 }, { "file": "test/KeysInTableModule.t.sol", "test": "testInstallGas", "name": "install keys in table module", - "gasUsed": 1245500 + "gasUsed": 1489422 }, { "file": "test/KeysInTableModule.t.sol", "test": "testInstallGas", "name": "set a record on a table with keysInTableModule installed", - "gasUsed": 192374 + "gasUsed": 192180 }, { "file": "test/KeysInTableModule.t.sol", "test": "testInstallSingleton", "name": "install keys in table module", - "gasUsed": 1245500 + "gasUsed": 1489422 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookCompositeGas", "name": "install keys in table module", - "gasUsed": 1245500 + "gasUsed": 1489422 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookCompositeGas", "name": "change a composite record on a table with keysInTableModule installed", - "gasUsed": 28061 + "gasUsed": 28011 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookCompositeGas", "name": "delete a composite record on a table with keysInTableModule installed", - "gasUsed": 285045 + "gasUsed": 284310 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookGas", "name": "install keys in table module", - "gasUsed": 1245500 + "gasUsed": 1489422 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookGas", "name": "change a record on a table with keysInTableModule installed", - "gasUsed": 26681 + "gasUsed": 26631 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookGas", "name": "delete a record on a table with keysInTableModule installed", - "gasUsed": 146383 + "gasUsed": 146068 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testGetKeysWithValueGas", "name": "install keys with value module", - "gasUsed": 598260 + "gasUsed": 682738 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testGetKeysWithValueGas", "name": "Get list of keys with a given value", - "gasUsed": 7507 + "gasUsed": 7485 }, { "file": "test/KeysWithValueModule.t.sol", @@ -81,228 +81,222 @@ "file": "test/KeysWithValueModule.t.sol", "test": "testInstall", "name": "install keys with value module", - "gasUsed": 598260 + "gasUsed": 682738 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testInstall", "name": "set a record on a table with KeysWithValueModule installed", - "gasUsed": 159027 + "gasUsed": 158900 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetAndDeleteRecordHook", "name": "install keys with value module", - "gasUsed": 598260 + "gasUsed": 682738 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetAndDeleteRecordHook", "name": "change a record on a table with KeysWithValueModule installed", - "gasUsed": 126064 + "gasUsed": 125858 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetAndDeleteRecordHook", "name": "delete a record on a table with KeysWithValueModule installed", - "gasUsed": 47905 + "gasUsed": 47849 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetField", "name": "install keys with value module", - "gasUsed": 598260 + "gasUsed": 682738 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetField", "name": "set a field on a table with KeysWithValueModule installed", - "gasUsed": 166095 + "gasUsed": 165968 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetField", "name": "change a field on a table with KeysWithValueModule installed", - "gasUsed": 128353 + "gasUsed": 128225 }, { "file": "test/query.t.sol", "test": "testCombinedHasHasValueNotQuery", "name": "CombinedHasHasValueNotQuery", - "gasUsed": 158332 + "gasUsed": 189957 }, { "file": "test/query.t.sol", "test": "testCombinedHasHasValueQuery", "name": "CombinedHasHasValueQuery", - "gasUsed": 74736 + "gasUsed": 85236 }, { "file": "test/query.t.sol", "test": "testCombinedHasNotQuery", "name": "CombinedHasNotQuery", - "gasUsed": 215752 + "gasUsed": 268520 }, { "file": "test/query.t.sol", "test": "testCombinedHasQuery", "name": "CombinedHasQuery", - "gasUsed": 135884 + "gasUsed": 178262 }, { "file": "test/query.t.sol", "test": "testCombinedHasValueNotQuery", "name": "CombinedHasValueNotQuery", - "gasUsed": 134907 + "gasUsed": 166573 }, { "file": "test/query.t.sol", "test": "testCombinedHasValueQuery", "name": "CombinedHasValueQuery", - "gasUsed": 19146 + "gasUsed": 19104 }, { "file": "test/query.t.sol", "test": "testHasQuery", "name": "HasQuery", - "gasUsed": 30667 + "gasUsed": 41272 }, { "file": "test/query.t.sol", "test": "testHasQuery1000Keys", "name": "HasQuery with 1000 keys", - "gasUsed": 10429469 + "gasUsed": 10397005 }, { "file": "test/query.t.sol", "test": "testHasQuery100Keys", "name": "HasQuery with 100 keys", - "gasUsed": 971172 + "gasUsed": 977548 }, { "file": "test/query.t.sol", "test": "testHasValueQuery", "name": "HasValueQuery", - "gasUsed": 9250 + "gasUsed": 9228 }, { "file": "test/query.t.sol", "test": "testNotValueQuery", "name": "NotValueQuery", - "gasUsed": 68332 + "gasUsed": 78830 }, { "file": "test/UniqueEntityModule.t.sol", "test": "testInstall", "name": "install unique entity module", - "gasUsed": 780716 + "gasUsed": 769429 }, { "file": "test/UniqueEntityModule.t.sol", "test": "testInstall", "name": "get a unique entity nonce (non-root module)", - "gasUsed": 70532 + "gasUsed": 70471 }, { "file": "test/UniqueEntityModule.t.sol", "test": "testInstallRoot", "name": "installRoot unique entity module", - "gasUsed": 752491 + "gasUsed": 744949 }, { "file": "test/UniqueEntityModule.t.sol", "test": "testInstallRoot", "name": "get a unique entity nonce (root module)", - "gasUsed": 70532 + "gasUsed": 70471 }, { "file": "test/World.t.sol", "test": "testDeleteRecord", "name": "Delete record", - "gasUsed": 13644 + "gasUsed": 13595 }, { "file": "test/World.t.sol", "test": "testPushToField", "name": "Push data to the table", - "gasUsed": 93813 + "gasUsed": 93742 }, { "file": "test/World.t.sol", "test": "testRegisterFallbackSystem", "name": "Register a fallback system", - "gasUsed": 84310 + "gasUsed": 84303 }, { "file": "test/World.t.sol", "test": "testRegisterFallbackSystem", "name": "Register a root fallback system", - "gasUsed": 77491 + "gasUsed": 77463 }, { "file": "test/World.t.sol", "test": "testRegisterFunctionSelector", "name": "Register a function selector", - "gasUsed": 104901 + "gasUsed": 104894 }, { "file": "test/World.t.sol", "test": "testRegisterNamespace", "name": "Register a new namespace", - "gasUsed": 146851 + "gasUsed": 146789 }, { "file": "test/World.t.sol", "test": "testRegisterRootFunctionSelector", "name": "Register a root function selector", - "gasUsed": 93406 + "gasUsed": 93378 }, { "file": "test/World.t.sol", "test": "testRegisterTable", "name": "Register a new table in the namespace", - "gasUsed": 244857 + "gasUsed": 660749 }, { "file": "test/World.t.sol", "test": "testSetField", "name": "Write data to a table field", - "gasUsed": 42256 - }, - { - "file": "test/World.t.sol", - "test": "testSetMetadata", - "name": "Set metadata", - "gasUsed": 277166 + "gasUsed": 42202 }, { "file": "test/World.t.sol", "test": "testSetRecord", "name": "Write data to the table", - "gasUsed": 41450 + "gasUsed": 41397 }, { "file": "test/WorldDynamicUpdate.t.sol", "test": "testPopFromField", "name": "pop 1 address (cold)", - "gasUsed": 33369 + "gasUsed": 33297 }, { "file": "test/WorldDynamicUpdate.t.sol", "test": "testPopFromField", "name": "pop 1 address (warm)", - "gasUsed": 20160 + "gasUsed": 20088 }, { "file": "test/WorldDynamicUpdate.t.sol", "test": "testUpdateInField", "name": "updateInField 1 item (cold)", - "gasUsed": 35035 + "gasUsed": 34963 }, { "file": "test/WorldDynamicUpdate.t.sol", "test": "testUpdateInField", "name": "updateInField 1 item (warm)", - "gasUsed": 22239 + "gasUsed": 22167 } ] diff --git a/packages/world/src/World.sol b/packages/world/src/World.sol index 1b24600f46..9ef8361508 100644 --- a/packages/world/src/World.sol +++ b/packages/world/src/World.sol @@ -31,7 +31,7 @@ contract World is StoreRead, IStoreData, IWorldKernel { // Register internal NamespaceOwner table and give ownership of the root // namespace to msg.sender. This is done in the constructor instead of a // module, so that we can use it for access control checks in `installRootModule`. - NamespaceOwner.registerSchema(); + NamespaceOwner.register(); NamespaceOwner.set(ROOT_NAMESPACE, msg.sender); // Other internal tables are registered by the CoreModule to reduce World's bytecode size. diff --git a/packages/world/src/interfaces/IWorldRegistrationSystem.sol b/packages/world/src/interfaces/IWorldRegistrationSystem.sol index 2db43b87ac..63b7273234 100644 --- a/packages/world/src/interfaces/IWorldRegistrationSystem.sol +++ b/packages/world/src/interfaces/IWorldRegistrationSystem.sol @@ -14,16 +14,11 @@ interface IWorldRegistrationSystem { function registerTable( bytes16 namespace, bytes16 name, + Schema keySchema, Schema valueSchema, - Schema keySchema - ) external returns (bytes32 resourceSelector); - - function setMetadata( - bytes16 namespace, - bytes16 name, - string calldata tableName, + string[] calldata keyNames, string[] calldata fieldNames - ) external; + ) external returns (bytes32 resourceSelector); function registerHook(bytes16 namespace, bytes16 name, address hook) external; diff --git a/packages/world/src/modules/core/CoreModule.sol b/packages/world/src/modules/core/CoreModule.sol index 3f9ceca643..c6fe5b0875 100644 --- a/packages/world/src/modules/core/CoreModule.sol +++ b/packages/world/src/modules/core/CoreModule.sol @@ -57,29 +57,15 @@ contract CoreModule is IModule, WorldContext { * Register core tables in the World */ function _registerCoreTables() internal { - NamespaceOwner.setMetadata(); + InstalledModules.register(); + ResourceAccess.register(); + Systems.register(); + FunctionSelectors.register(); + SystemHooks.register(); + SystemRegistry.register(); + ResourceType.register(); - InstalledModules.registerSchema(); - InstalledModules.setMetadata(); - - ResourceAccess.registerSchema(); - ResourceAccess.setMetadata(); ResourceAccess.set(ROOT_NAMESPACE, _msgSender(), true); - - Systems.registerSchema(); - Systems.setMetadata(); - - FunctionSelectors.registerSchema(); - FunctionSelectors.setMetadata(); - - SystemHooks.registerSchema(); - SystemHooks.setMetadata(); - - SystemRegistry.registerSchema(); - SystemRegistry.setMetadata(); - - ResourceType.registerSchema(); - ResourceType.setMetadata(); ResourceType.set(ROOT_NAMESPACE, Resource.NAMESPACE); } @@ -107,11 +93,10 @@ contract CoreModule is IModule, WorldContext { * Register function selectors for all CoreSystem functions in the World */ function _registerFunctionSelectors() internal { - bytes4[17] memory functionSelectors = [ + bytes4[15] memory functionSelectors = [ // --- WorldRegistrationSystem --- WorldRegistrationSystem.registerNamespace.selector, WorldRegistrationSystem.registerTable.selector, - WorldRegistrationSystem.setMetadata.selector, WorldRegistrationSystem.registerHook.selector, WorldRegistrationSystem.registerTableHook.selector, WorldRegistrationSystem.registerSystemHook.selector, @@ -119,8 +104,7 @@ contract CoreModule is IModule, WorldContext { WorldRegistrationSystem.registerFunctionSelector.selector, WorldRegistrationSystem.registerRootFunctionSelector.selector, // --- StoreRegistrationSystem --- - StoreRegistrationSystem.registerSchema.selector, - StoreRegistrationSystem.setMetadata.selector, + StoreRegistrationSystem.registerTable.selector, StoreRegistrationSystem.registerStoreHook.selector, // --- ModuleInstallationSystem --- ModuleInstallationSystem.installModule.selector, diff --git a/packages/world/src/modules/core/implementations/StoreRegistrationSystem.sol b/packages/world/src/modules/core/implementations/StoreRegistrationSystem.sol index 6d155c2175..581937e1b5 100644 --- a/packages/world/src/modules/core/implementations/StoreRegistrationSystem.sol +++ b/packages/world/src/modules/core/implementations/StoreRegistrationSystem.sol @@ -23,14 +23,20 @@ contract StoreRegistrationSystem is IStoreRegistration, System { using ResourceSelector for bytes32; /** - * Register the given schema for the given table id. + * Register the given key schema, value schema, key names and value names for the given table id. * This overload exists to conform with the IStore interface. * Access is checked based on the namespace or name (encoded in the tableId). */ - function registerSchema(bytes32 tableId, Schema valueSchema, Schema keySchema) public virtual { + function registerTable( + bytes32 tableId, + Schema keySchema, + Schema valueSchema, + string[] calldata keyNames, + string[] calldata fieldNames + ) public virtual { (address systemAddress, ) = Systems.get(ResourceSelector.from(ROOT_NAMESPACE, CORE_SYSTEM_NAME)); - // We can't call IBaseWorld(this).registerSchema directly because it would be handled like + // We can't call IBaseWorld(this).registerTable directly because it would be handled like // an external call, so msg.sender would be the address of the World contract Call.withSender({ msgSender: _msgSender(), @@ -39,32 +45,9 @@ contract StoreRegistrationSystem is IStoreRegistration, System { WorldRegistrationSystem.registerTable.selector, tableId.getNamespace(), tableId.getName(), + keySchema, valueSchema, - keySchema - ), - delegate: true, - value: 0 - }); - } - - /** - * Register metadata (tableName, fieldNames) for the table at the given tableId. - * This overload exists to conform with the `IStore` interface. - * Access is checked based on the namespace or name (encoded in the tableId). - */ - function setMetadata(bytes32 tableId, string calldata tableName, string[] calldata fieldNames) public virtual { - (address systemAddress, ) = Systems.get(ResourceSelector.from(ROOT_NAMESPACE, CORE_SYSTEM_NAME)); - - // We can't call IBaseWorld(this).setMetadata directly because it would be handled like - // an external call, so msg.sender would be the address of the World contract - Call.withSender({ - msgSender: _msgSender(), - target: systemAddress, - funcSelectorAndArgs: abi.encodeWithSelector( - WorldRegistrationSystem.setMetadata.selector, - tableId.getNamespace(), - tableId.getName(), - tableName, + keyNames, fieldNames ), delegate: true, diff --git a/packages/world/src/modules/core/implementations/WorldRegistrationSystem.sol b/packages/world/src/modules/core/implementations/WorldRegistrationSystem.sol index 7f92199fb5..b87f34b2ae 100644 --- a/packages/world/src/modules/core/implementations/WorldRegistrationSystem.sol +++ b/packages/world/src/modules/core/implementations/WorldRegistrationSystem.sol @@ -52,8 +52,10 @@ contract WorldRegistrationSystem is System, IWorldErrors { function registerTable( bytes16 namespace, bytes16 name, + Schema keySchema, Schema valueSchema, - Schema keySchema + string[] calldata keyNames, + string[] calldata fieldNames ) public virtual returns (bytes32 resourceSelector) { resourceSelector = ResourceSelector.from(namespace, name); @@ -74,24 +76,7 @@ contract WorldRegistrationSystem is System, IWorldErrors { ResourceType.set(resourceSelector, Resource.TABLE); // Register the table's schema - StoreCore.registerSchema(resourceSelector, valueSchema, keySchema); - } - - /** - * Register metadata (tableName, fieldNames) for the table at the given namespace and name. - * Requires the caller to own the namespace. - */ - function setMetadata( - bytes16 namespace, - bytes16 name, - string calldata tableName, - string[] calldata fieldNames - ) public virtual { - // Require caller to own the namespace - bytes32 tableId = AccessControl.requireOwnerOrSelf(namespace, name, _msgSender()); - - // Set the metadata - StoreCore.setMetadata(tableId, tableName, fieldNames); + StoreCore.registerTable(resourceSelector, keySchema, valueSchema, keyNames, fieldNames); } /** diff --git a/packages/world/src/modules/core/tables/FunctionSelectors.sol b/packages/world/src/modules/core/tables/FunctionSelectors.sol index a45a9489d7..93468d6327 100644 --- a/packages/world/src/modules/core/tables/FunctionSelectors.sol +++ b/packages/world/src/modules/core/tables/FunctionSelectors.sol @@ -21,16 +21,7 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Funct bytes32 constant FunctionSelectorsTableId = _tableId; library FunctionSelectors { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](3); - _schema[0] = SchemaType.BYTES16; - _schema[1] = SchemaType.BYTES16; - _schema[2] = SchemaType.BYTES4; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); _schema[0] = SchemaType.BYTES4; @@ -38,35 +29,38 @@ library FunctionSelectors { return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](3); - _fieldNames[0] = "namespace"; - _fieldNames[1] = "name"; - _fieldNames[2] = "systemFunctionSelector"; - return ("FunctionSelectors", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](3); + _schema[0] = SchemaType.BYTES16; + _schema[1] = SchemaType.BYTES16; + _schema[2] = SchemaType.BYTES4; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "functionSelector"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](3); + fieldNames[0] = "namespace"; + fieldNames[1] = "name"; + fieldNames[2] = "systemFunctionSelector"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get namespace */ @@ -74,7 +68,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (Bytes.slice16(_blob, 0)); } @@ -83,7 +77,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (Bytes.slice16(_blob, 0)); } @@ -92,7 +86,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((namespace)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((namespace)), getValueSchema()); } /** Set namespace (using the specified store) */ @@ -100,7 +94,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((namespace)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((namespace)), getValueSchema()); } /** Get name */ @@ -108,7 +102,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getValueSchema()); return (Bytes.slice16(_blob, 0)); } @@ -117,7 +111,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getValueSchema()); return (Bytes.slice16(_blob, 0)); } @@ -126,7 +120,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((name)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((name)), getValueSchema()); } /** Set name (using the specified store) */ @@ -134,7 +128,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((name)), getSchema()); + _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((name)), getValueSchema()); } /** Get systemFunctionSelector */ @@ -142,7 +136,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getValueSchema()); return (Bytes.slice4(_blob, 0)); } @@ -154,7 +148,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getValueSchema()); return (Bytes.slice4(_blob, 0)); } @@ -163,7 +157,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - StoreSwitch.setField(_tableId, _keyTuple, 2, abi.encodePacked((systemFunctionSelector)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 2, abi.encodePacked((systemFunctionSelector)), getValueSchema()); } /** Set systemFunctionSelector (using the specified store) */ @@ -171,7 +165,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - _store.setField(_tableId, _keyTuple, 2, abi.encodePacked((systemFunctionSelector)), getSchema()); + _store.setField(_tableId, _keyTuple, 2, abi.encodePacked((systemFunctionSelector)), getValueSchema()); } /** Get the full data */ @@ -181,7 +175,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -193,7 +187,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -204,7 +198,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - StoreSwitch.setRecord(_tableId, _keyTuple, _data, getSchema()); + StoreSwitch.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using individual values (using the specified store) */ @@ -220,7 +214,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - _store.setRecord(_tableId, _keyTuple, _data, getSchema()); + _store.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Decode the tightly packed blob using this table's schema */ @@ -252,7 +246,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -260,6 +254,6 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/src/modules/core/tables/ResourceType.sol b/packages/world/src/modules/core/tables/ResourceType.sol index 9332605b34..74bd1179fd 100644 --- a/packages/world/src/modules/core/tables/ResourceType.sol +++ b/packages/world/src/modules/core/tables/ResourceType.sol @@ -24,48 +24,42 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Resou bytes32 constant ResourceTypeTableId = _tableId; library ResourceType { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { + /** Get the table's key schema */ + function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.UINT8; + _schema[0] = SchemaType.BYTES32; return SchemaLib.encode(_schema); } - function getKeySchema() internal pure returns (Schema) { + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BYTES32; + _schema[0] = SchemaType.UINT8; return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "resourceType"; - return ("ResourceType", _fieldNames); - } - - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "resourceSelector"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "resourceType"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get resourceType */ @@ -73,7 +67,7 @@ library ResourceType { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return Resource(uint8(Bytes.slice1(_blob, 0))); } @@ -82,7 +76,7 @@ library ResourceType { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return Resource(uint8(Bytes.slice1(_blob, 0))); } @@ -91,7 +85,7 @@ library ResourceType { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked(uint8(resourceType)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked(uint8(resourceType)), getValueSchema()); } /** Set resourceType (using the specified store) */ @@ -99,7 +93,7 @@ library ResourceType { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked(uint8(resourceType)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked(uint8(resourceType)), getValueSchema()); } /** Tightly pack full data using this table's schema */ @@ -120,7 +114,7 @@ library ResourceType { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -128,6 +122,6 @@ library ResourceType { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/src/modules/core/tables/SystemHooks.sol b/packages/world/src/modules/core/tables/SystemHooks.sol index c9fe64ef94..8ee3d6aeee 100644 --- a/packages/world/src/modules/core/tables/SystemHooks.sol +++ b/packages/world/src/modules/core/tables/SystemHooks.sol @@ -21,48 +21,42 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Syste bytes32 constant SystemHooksTableId = _tableId; library SystemHooks { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { + /** Get the table's key schema */ + function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.ADDRESS_ARRAY; + _schema[0] = SchemaType.BYTES32; return SchemaLib.encode(_schema); } - function getKeySchema() internal pure returns (Schema) { + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BYTES32; + _schema[0] = SchemaType.ADDRESS_ARRAY; return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "value"; - return ("SystemHooks", _fieldNames); - } - - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "resourceSelector"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "value"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ @@ -70,7 +64,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_address()); } @@ -79,7 +73,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_address()); } @@ -88,7 +82,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getValueSchema()); } /** Set value (using the specified store) */ @@ -96,7 +90,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getValueSchema()); } /** Get the length of value */ @@ -104,7 +98,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); unchecked { return _byteLength / 20; } @@ -115,7 +109,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); unchecked { return _byteLength / 20; } @@ -134,7 +128,7 @@ library SystemHooks { _tableId, _keyTuple, 0, - getSchema(), + getValueSchema(), _index * 20, (_index + 1) * 20 ); @@ -151,7 +145,14 @@ library SystemHooks { _keyTuple[0] = resourceSelector; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 20, (_index + 1) * 20); + bytes memory _blob = _store.getFieldSlice( + _tableId, + _keyTuple, + 0, + getValueSchema(), + _index * 20, + (_index + 1) * 20 + ); return (address(Bytes.slice20(_blob, 0))); } } @@ -161,7 +162,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to value (using the specified store) */ @@ -169,7 +170,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from value */ @@ -177,7 +178,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 20, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 0, 20, getValueSchema()); } /** Pop an element from value (using the specified store) */ @@ -185,7 +186,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.popFromField(_tableId, _keyTuple, 0, 20, getSchema()); + _store.popFromField(_tableId, _keyTuple, 0, 20, getValueSchema()); } /** @@ -197,7 +198,7 @@ library SystemHooks { _keyTuple[0] = resourceSelector; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), getValueSchema()); } } @@ -210,7 +211,7 @@ library SystemHooks { _keyTuple[0] = resourceSelector; unchecked { - _store.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), getValueSchema()); } } @@ -236,7 +237,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -244,6 +245,6 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/src/modules/core/tables/SystemRegistry.sol b/packages/world/src/modules/core/tables/SystemRegistry.sol index 8f6b5abcc0..f1923a50af 100644 --- a/packages/world/src/modules/core/tables/SystemRegistry.sol +++ b/packages/world/src/modules/core/tables/SystemRegistry.sol @@ -21,48 +21,42 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Syste bytes32 constant SystemRegistryTableId = _tableId; library SystemRegistry { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { + /** Get the table's key schema */ + function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BYTES32; + _schema[0] = SchemaType.ADDRESS; return SchemaLib.encode(_schema); } - function getKeySchema() internal pure returns (Schema) { + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.ADDRESS; + _schema[0] = SchemaType.BYTES32; return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "resourceSelector"; - return ("SystemRegistry", _fieldNames); - } - - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "system"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "resourceSelector"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get resourceSelector */ @@ -70,7 +64,7 @@ library SystemRegistry { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (Bytes.slice32(_blob, 0)); } @@ -79,7 +73,7 @@ library SystemRegistry { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (Bytes.slice32(_blob, 0)); } @@ -88,7 +82,7 @@ library SystemRegistry { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((resourceSelector)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((resourceSelector)), getValueSchema()); } /** Set resourceSelector (using the specified store) */ @@ -96,7 +90,7 @@ library SystemRegistry { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((resourceSelector)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((resourceSelector)), getValueSchema()); } /** Tightly pack full data using this table's schema */ @@ -117,7 +111,7 @@ library SystemRegistry { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -125,6 +119,6 @@ library SystemRegistry { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/src/modules/core/tables/Systems.sol b/packages/world/src/modules/core/tables/Systems.sol index ad0accbaa3..9b0c9568e3 100644 --- a/packages/world/src/modules/core/tables/Systems.sol +++ b/packages/world/src/modules/core/tables/Systems.sol @@ -21,15 +21,7 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Syste bytes32 constant SystemsTableId = _tableId; library Systems { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](2); - _schema[0] = SchemaType.ADDRESS; - _schema[1] = SchemaType.BOOL; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); _schema[0] = SchemaType.BYTES32; @@ -37,34 +29,36 @@ library Systems { return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](2); - _fieldNames[0] = "system"; - _fieldNames[1] = "publicAccess"; - return ("Systems", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](2); + _schema[0] = SchemaType.ADDRESS; + _schema[1] = SchemaType.BOOL; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "resourceSelector"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](2); + fieldNames[0] = "system"; + fieldNames[1] = "publicAccess"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get system */ @@ -72,7 +66,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (address(Bytes.slice20(_blob, 0))); } @@ -81,7 +75,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (address(Bytes.slice20(_blob, 0))); } @@ -90,7 +84,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((system)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((system)), getValueSchema()); } /** Set system (using the specified store) */ @@ -98,7 +92,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((system)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((system)), getValueSchema()); } /** Get publicAccess */ @@ -106,7 +100,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getValueSchema()); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -115,7 +109,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getValueSchema()); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -124,7 +118,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((publicAccess)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((publicAccess)), getValueSchema()); } /** Set publicAccess (using the specified store) */ @@ -132,7 +126,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((publicAccess)), getSchema()); + _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((publicAccess)), getValueSchema()); } /** Get the full data */ @@ -140,7 +134,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -149,7 +143,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -160,7 +154,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.setRecord(_tableId, _keyTuple, _data, getSchema()); + StoreSwitch.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using individual values (using the specified store) */ @@ -170,7 +164,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.setRecord(_tableId, _keyTuple, _data, getSchema()); + _store.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Decode the tightly packed blob using this table's schema */ @@ -198,7 +192,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -206,7 +200,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/src/modules/keysintable/KeysInTableModule.sol b/packages/world/src/modules/keysintable/KeysInTableModule.sol index e908d01a55..74abc6ad84 100644 --- a/packages/world/src/modules/keysintable/KeysInTableModule.sol +++ b/packages/world/src/modules/keysintable/KeysInTableModule.sol @@ -43,24 +43,8 @@ contract KeysInTableModule is IModule, WorldContext { if (ResourceType.get(KeysInTableTableId) == Resource.NONE) { // Register the tables - world.registerTable( - KeysInTableTableId.getNamespace(), - KeysInTableTableId.getName(), - KeysInTable.getSchema(), - KeysInTable.getKeySchema() - ); - world.registerTable( - UsedKeysIndexTableId.getNamespace(), - UsedKeysIndexTableId.getName(), - UsedKeysIndex.getSchema(), - UsedKeysIndex.getKeySchema() - ); - - // Register metadata for the tables - (string memory tableName1, string[] memory fieldNames1) = KeysInTable.getMetadata(); - world.setMetadata(KeysInTableTableId.getNamespace(), KeysInTableTableId.getName(), tableName1, fieldNames1); - (string memory tableName2, string[] memory fieldNames2) = UsedKeysIndex.getMetadata(); - world.setMetadata(UsedKeysIndexTableId.getNamespace(), UsedKeysIndexTableId.getName(), tableName2, fieldNames2); + KeysInTable.register(world); + UsedKeysIndex.register(world); // Grant the hook access to the tables world.grantAccess(KeysInTableTableId.getNamespace(), KeysInTableTableId.getName(), address(hook)); diff --git a/packages/world/src/modules/keysintable/tables/KeysInTable.sol b/packages/world/src/modules/keysintable/tables/KeysInTable.sol index 4710be73d6..a81be52b10 100644 --- a/packages/world/src/modules/keysintable/tables/KeysInTable.sol +++ b/packages/world/src/modules/keysintable/tables/KeysInTable.sol @@ -29,8 +29,16 @@ struct KeysInTableData { } library KeysInTable { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { + /** Get the table's key schema */ + function getKeySchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](1); + _schema[0] = SchemaType.BYTES32; + + return SchemaLib.encode(_schema); + } + + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](5); _schema[0] = SchemaType.BYTES32_ARRAY; _schema[1] = SchemaType.BYTES32_ARRAY; @@ -41,44 +49,30 @@ library KeysInTable { return SchemaLib.encode(_schema); } - function getKeySchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BYTES32; - - return SchemaLib.encode(_schema); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "sourceTable"; } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](5); - _fieldNames[0] = "keys0"; - _fieldNames[1] = "keys1"; - _fieldNames[2] = "keys2"; - _fieldNames[3] = "keys3"; - _fieldNames[4] = "keys4"; - return ("KeysInTable", _fieldNames); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](5); + fieldNames[0] = "keys0"; + fieldNames[1] = "keys1"; + fieldNames[2] = "keys2"; + fieldNames[3] = "keys3"; + fieldNames[4] = "keys4"; } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); - } - - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); - } - - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get keys0 */ @@ -86,7 +80,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -95,7 +89,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -104,7 +98,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keys0)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keys0)), getValueSchema()); } /** Set keys0 (using the specified store) */ @@ -112,7 +106,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keys0)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keys0)), getValueSchema()); } /** Get the length of keys0 */ @@ -120,7 +114,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); unchecked { return _byteLength / 32; } @@ -131,7 +125,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); unchecked { return _byteLength / 32; } @@ -150,7 +144,7 @@ library KeysInTable { _tableId, _keyTuple, 0, - getSchema(), + getValueSchema(), _index * 32, (_index + 1) * 32 ); @@ -167,7 +161,14 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 32, (_index + 1) * 32); + bytes memory _blob = _store.getFieldSlice( + _tableId, + _keyTuple, + 0, + getValueSchema(), + _index * 32, + (_index + 1) * 32 + ); return (Bytes.slice32(_blob, 0)); } } @@ -177,7 +178,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to keys0 (using the specified store) */ @@ -185,7 +186,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from keys0 */ @@ -193,7 +194,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 32, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 0, 32, getValueSchema()); } /** Pop an element from keys0 (using the specified store) */ @@ -201,7 +202,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.popFromField(_tableId, _keyTuple, 0, 32, getSchema()); + _store.popFromField(_tableId, _keyTuple, 0, 32, getValueSchema()); } /** @@ -213,7 +214,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getValueSchema()); } } @@ -226,7 +227,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - _store.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getValueSchema()); } } @@ -235,7 +236,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -244,7 +245,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -253,7 +254,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.setField(_tableId, _keyTuple, 1, EncodeArray.encode((keys1)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 1, EncodeArray.encode((keys1)), getValueSchema()); } /** Set keys1 (using the specified store) */ @@ -261,7 +262,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.setField(_tableId, _keyTuple, 1, EncodeArray.encode((keys1)), getSchema()); + _store.setField(_tableId, _keyTuple, 1, EncodeArray.encode((keys1)), getValueSchema()); } /** Get the length of keys1 */ @@ -269,7 +270,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 1, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 1, getValueSchema()); unchecked { return _byteLength / 32; } @@ -280,7 +281,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 1, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 1, getValueSchema()); unchecked { return _byteLength / 32; } @@ -299,7 +300,7 @@ library KeysInTable { _tableId, _keyTuple, 1, - getSchema(), + getValueSchema(), _index * 32, (_index + 1) * 32 ); @@ -316,7 +317,14 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 1, getSchema(), _index * 32, (_index + 1) * 32); + bytes memory _blob = _store.getFieldSlice( + _tableId, + _keyTuple, + 1, + getValueSchema(), + _index * 32, + (_index + 1) * 32 + ); return (Bytes.slice32(_blob, 0)); } } @@ -326,7 +334,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to keys1 (using the specified store) */ @@ -334,7 +342,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from keys1 */ @@ -342,7 +350,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.popFromField(_tableId, _keyTuple, 1, 32, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 1, 32, getValueSchema()); } /** Pop an element from keys1 (using the specified store) */ @@ -350,7 +358,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.popFromField(_tableId, _keyTuple, 1, 32, getSchema()); + _store.popFromField(_tableId, _keyTuple, 1, 32, getValueSchema()); } /** @@ -362,7 +370,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 1, _index * 32, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 1, _index * 32, abi.encodePacked((_element)), getValueSchema()); } } @@ -375,7 +383,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - _store.updateInField(_tableId, _keyTuple, 1, _index * 32, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 1, _index * 32, abi.encodePacked((_element)), getValueSchema()); } } @@ -384,7 +392,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -393,7 +401,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -402,7 +410,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.setField(_tableId, _keyTuple, 2, EncodeArray.encode((keys2)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 2, EncodeArray.encode((keys2)), getValueSchema()); } /** Set keys2 (using the specified store) */ @@ -410,7 +418,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.setField(_tableId, _keyTuple, 2, EncodeArray.encode((keys2)), getSchema()); + _store.setField(_tableId, _keyTuple, 2, EncodeArray.encode((keys2)), getValueSchema()); } /** Get the length of keys2 */ @@ -418,7 +426,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 2, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 2, getValueSchema()); unchecked { return _byteLength / 32; } @@ -429,7 +437,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 2, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 2, getValueSchema()); unchecked { return _byteLength / 32; } @@ -448,7 +456,7 @@ library KeysInTable { _tableId, _keyTuple, 2, - getSchema(), + getValueSchema(), _index * 32, (_index + 1) * 32 ); @@ -465,7 +473,14 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 2, getSchema(), _index * 32, (_index + 1) * 32); + bytes memory _blob = _store.getFieldSlice( + _tableId, + _keyTuple, + 2, + getValueSchema(), + _index * 32, + (_index + 1) * 32 + ); return (Bytes.slice32(_blob, 0)); } } @@ -475,7 +490,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to keys2 (using the specified store) */ @@ -483,7 +498,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from keys2 */ @@ -491,7 +506,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.popFromField(_tableId, _keyTuple, 2, 32, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 2, 32, getValueSchema()); } /** Pop an element from keys2 (using the specified store) */ @@ -499,7 +514,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.popFromField(_tableId, _keyTuple, 2, 32, getSchema()); + _store.popFromField(_tableId, _keyTuple, 2, 32, getValueSchema()); } /** @@ -511,7 +526,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 2, _index * 32, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 2, _index * 32, abi.encodePacked((_element)), getValueSchema()); } } @@ -524,7 +539,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - _store.updateInField(_tableId, _keyTuple, 2, _index * 32, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 2, _index * 32, abi.encodePacked((_element)), getValueSchema()); } } @@ -533,7 +548,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -542,7 +557,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -551,7 +566,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.setField(_tableId, _keyTuple, 3, EncodeArray.encode((keys3)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 3, EncodeArray.encode((keys3)), getValueSchema()); } /** Set keys3 (using the specified store) */ @@ -559,7 +574,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.setField(_tableId, _keyTuple, 3, EncodeArray.encode((keys3)), getSchema()); + _store.setField(_tableId, _keyTuple, 3, EncodeArray.encode((keys3)), getValueSchema()); } /** Get the length of keys3 */ @@ -567,7 +582,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 3, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 3, getValueSchema()); unchecked { return _byteLength / 32; } @@ -578,7 +593,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 3, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 3, getValueSchema()); unchecked { return _byteLength / 32; } @@ -597,7 +612,7 @@ library KeysInTable { _tableId, _keyTuple, 3, - getSchema(), + getValueSchema(), _index * 32, (_index + 1) * 32 ); @@ -614,7 +629,14 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 3, getSchema(), _index * 32, (_index + 1) * 32); + bytes memory _blob = _store.getFieldSlice( + _tableId, + _keyTuple, + 3, + getValueSchema(), + _index * 32, + (_index + 1) * 32 + ); return (Bytes.slice32(_blob, 0)); } } @@ -624,7 +646,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to keys3 (using the specified store) */ @@ -632,7 +654,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from keys3 */ @@ -640,7 +662,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.popFromField(_tableId, _keyTuple, 3, 32, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 3, 32, getValueSchema()); } /** Pop an element from keys3 (using the specified store) */ @@ -648,7 +670,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.popFromField(_tableId, _keyTuple, 3, 32, getSchema()); + _store.popFromField(_tableId, _keyTuple, 3, 32, getValueSchema()); } /** @@ -660,7 +682,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 3, _index * 32, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 3, _index * 32, abi.encodePacked((_element)), getValueSchema()); } } @@ -673,7 +695,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - _store.updateInField(_tableId, _keyTuple, 3, _index * 32, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 3, _index * 32, abi.encodePacked((_element)), getValueSchema()); } } @@ -682,7 +704,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 4, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 4, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -691,7 +713,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 4, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 4, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -700,7 +722,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.setField(_tableId, _keyTuple, 4, EncodeArray.encode((keys4)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 4, EncodeArray.encode((keys4)), getValueSchema()); } /** Set keys4 (using the specified store) */ @@ -708,7 +730,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.setField(_tableId, _keyTuple, 4, EncodeArray.encode((keys4)), getSchema()); + _store.setField(_tableId, _keyTuple, 4, EncodeArray.encode((keys4)), getValueSchema()); } /** Get the length of keys4 */ @@ -716,7 +738,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 4, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 4, getValueSchema()); unchecked { return _byteLength / 32; } @@ -727,7 +749,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 4, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 4, getValueSchema()); unchecked { return _byteLength / 32; } @@ -746,7 +768,7 @@ library KeysInTable { _tableId, _keyTuple, 4, - getSchema(), + getValueSchema(), _index * 32, (_index + 1) * 32 ); @@ -763,7 +785,14 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 4, getSchema(), _index * 32, (_index + 1) * 32); + bytes memory _blob = _store.getFieldSlice( + _tableId, + _keyTuple, + 4, + getValueSchema(), + _index * 32, + (_index + 1) * 32 + ); return (Bytes.slice32(_blob, 0)); } } @@ -773,7 +802,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to keys4 (using the specified store) */ @@ -781,7 +810,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from keys4 */ @@ -789,7 +818,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.popFromField(_tableId, _keyTuple, 4, 32, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 4, 32, getValueSchema()); } /** Pop an element from keys4 (using the specified store) */ @@ -797,7 +826,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.popFromField(_tableId, _keyTuple, 4, 32, getSchema()); + _store.popFromField(_tableId, _keyTuple, 4, 32, getValueSchema()); } /** @@ -809,7 +838,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 4, _index * 32, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 4, _index * 32, abi.encodePacked((_element)), getValueSchema()); } } @@ -822,7 +851,7 @@ library KeysInTable { _keyTuple[0] = sourceTable; unchecked { - _store.updateInField(_tableId, _keyTuple, 4, _index * 32, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 4, _index * 32, abi.encodePacked((_element)), getValueSchema()); } } @@ -831,7 +860,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -840,7 +869,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -858,7 +887,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.setRecord(_tableId, _keyTuple, _data, getSchema()); + StoreSwitch.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using individual values (using the specified store) */ @@ -876,7 +905,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.setRecord(_tableId, _keyTuple, _data, getSchema()); + _store.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using the data struct */ @@ -973,7 +1002,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -981,6 +1010,6 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol b/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol index 833dd2228d..c83423843e 100644 --- a/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol +++ b/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol @@ -21,15 +21,7 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("UsedK bytes32 constant UsedKeysIndexTableId = _tableId; library UsedKeysIndex { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](2); - _schema[0] = SchemaType.BOOL; - _schema[1] = SchemaType.UINT40; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](2); _schema[0] = SchemaType.BYTES32; @@ -38,34 +30,37 @@ library UsedKeysIndex { return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](2); - _fieldNames[0] = "has"; - _fieldNames[1] = "index"; - return ("UsedKeysIndex", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](2); + _schema[0] = SchemaType.BOOL; + _schema[1] = SchemaType.UINT40; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](2); + keyNames[0] = "sourceTable"; + keyNames[1] = "keysHash"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](2); + fieldNames[0] = "has"; + fieldNames[1] = "index"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get has */ @@ -74,7 +69,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -84,7 +79,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -94,7 +89,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((has)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((has)), getValueSchema()); } /** Set has (using the specified store) */ @@ -103,7 +98,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((has)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((has)), getValueSchema()); } /** Get index */ @@ -112,7 +107,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getValueSchema()); return (uint40(Bytes.slice5(_blob, 0))); } @@ -122,7 +117,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getValueSchema()); return (uint40(Bytes.slice5(_blob, 0))); } @@ -132,7 +127,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((index)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((index)), getValueSchema()); } /** Set index (using the specified store) */ @@ -141,7 +136,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((index)), getSchema()); + _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((index)), getValueSchema()); } /** Get the full data */ @@ -150,7 +145,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -160,7 +155,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -172,7 +167,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - StoreSwitch.setRecord(_tableId, _keyTuple, _data, getSchema()); + StoreSwitch.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using individual values (using the specified store) */ @@ -183,7 +178,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - _store.setRecord(_tableId, _keyTuple, _data, getSchema()); + _store.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Decode the tightly packed blob using this table's schema */ @@ -213,7 +208,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -222,7 +217,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/src/modules/keyswithvalue/KeysWithValueModule.sol b/packages/world/src/modules/keyswithvalue/KeysWithValueModule.sol index f6af846bd5..43c8ca34cb 100644 --- a/packages/world/src/modules/keyswithvalue/KeysWithValueModule.sol +++ b/packages/world/src/modules/keyswithvalue/KeysWithValueModule.sol @@ -42,21 +42,7 @@ contract KeysWithValueModule is IModule, WorldContext { bytes32 targetTableSelector = getTargetTableSelector(MODULE_NAMESPACE, sourceTableId); // Register the target table - IBaseWorld(_world()).registerTable( - targetTableSelector.getNamespace(), - targetTableSelector.getName(), - KeysWithValue.getSchema(), - KeysWithValue.getKeySchema() - ); - - // Register metadata for the target table - (string memory tableName, string[] memory fieldNames) = KeysWithValue.getMetadata(); - IBaseWorld(_world()).setMetadata( - targetTableSelector.getNamespace(), - targetTableSelector.getName(), - tableName, - fieldNames - ); + KeysWithValue.register(IBaseWorld(_world()), targetTableSelector); // Grant the hook access to the target table IBaseWorld(_world()).grantAccess(targetTableSelector.getNamespace(), targetTableSelector.getName(), address(hook)); diff --git a/packages/world/src/modules/keyswithvalue/tables/KeysWithValue.sol b/packages/world/src/modules/keyswithvalue/tables/KeysWithValue.sol index 0d35ca9b74..a37a92117e 100644 --- a/packages/world/src/modules/keyswithvalue/tables/KeysWithValue.sol +++ b/packages/world/src/modules/keyswithvalue/tables/KeysWithValue.sol @@ -18,48 +18,42 @@ import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCounter.sol"; library KeysWithValue { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { + /** Get the table's key schema */ + function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BYTES32_ARRAY; + _schema[0] = SchemaType.BYTES32; return SchemaLib.encode(_schema); } - function getKeySchema() internal pure returns (Schema) { + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BYTES32; + _schema[0] = SchemaType.BYTES32_ARRAY; return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "keysWithValue"; - return ("KeysWithValue", _fieldNames); - } - - /** Register the table's schema */ - function registerSchema(bytes32 _tableId) internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "valueHash"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store, bytes32 _tableId) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "keysWithValue"; } - /** Set the table's metadata */ - function setMetadata(bytes32 _tableId) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register(bytes32 _tableId) internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store, bytes32 _tableId) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store, bytes32 _tableId) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get keysWithValue */ @@ -67,7 +61,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -80,7 +74,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -89,7 +83,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keysWithValue)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keysWithValue)), getValueSchema()); } /** Set keysWithValue (using the specified store) */ @@ -97,7 +91,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keysWithValue)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keysWithValue)), getValueSchema()); } /** Get the length of keysWithValue */ @@ -105,7 +99,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); unchecked { return _byteLength / 32; } @@ -116,7 +110,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); unchecked { return _byteLength / 32; } @@ -135,7 +129,7 @@ library KeysWithValue { _tableId, _keyTuple, 0, - getSchema(), + getValueSchema(), _index * 32, (_index + 1) * 32 ); @@ -152,7 +146,14 @@ library KeysWithValue { _keyTuple[0] = valueHash; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 32, (_index + 1) * 32); + bytes memory _blob = _store.getFieldSlice( + _tableId, + _keyTuple, + 0, + getValueSchema(), + _index * 32, + (_index + 1) * 32 + ); return (Bytes.slice32(_blob, 0)); } } @@ -162,7 +163,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to keysWithValue (using the specified store) */ @@ -170,7 +171,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from keysWithValue */ @@ -178,7 +179,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 32, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 0, 32, getValueSchema()); } /** Pop an element from keysWithValue (using the specified store) */ @@ -186,7 +187,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - _store.popFromField(_tableId, _keyTuple, 0, 32, getSchema()); + _store.popFromField(_tableId, _keyTuple, 0, 32, getValueSchema()); } /** @@ -198,7 +199,7 @@ library KeysWithValue { _keyTuple[0] = valueHash; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getValueSchema()); } } @@ -211,7 +212,7 @@ library KeysWithValue { _keyTuple[0] = valueHash; unchecked { - _store.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getValueSchema()); } } @@ -237,7 +238,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -245,6 +246,6 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/src/modules/uniqueentity/UniqueEntityModule.sol b/packages/world/src/modules/uniqueentity/UniqueEntityModule.sol index d86a040b6e..05ffefa7dc 100644 --- a/packages/world/src/modules/uniqueentity/UniqueEntityModule.sol +++ b/packages/world/src/modules/uniqueentity/UniqueEntityModule.sol @@ -5,6 +5,7 @@ import { IBaseWorld } from "../../interfaces/IBaseWorld.sol"; import { IModule } from "../../interfaces/IModule.sol"; import { WorldContext } from "../../WorldContext.sol"; +import { ResourceSelector } from "../../ResourceSelector.sol"; import { UniqueEntity } from "./tables/UniqueEntity.sol"; import { UniqueEntitySystem } from "./UniqueEntitySystem.sol"; @@ -28,13 +29,11 @@ contract UniqueEntityModule is IModule, WorldContext { IBaseWorld world = IBaseWorld(_world()); // Register table - world.registerTable(NAMESPACE, TABLE_NAME, UniqueEntity.getSchema(), UniqueEntity.getKeySchema()); - // Set table's metadata - (string memory tableName, string[] memory fieldNames) = UniqueEntity.getMetadata(); - world.setMetadata(NAMESPACE, TABLE_NAME, tableName, fieldNames); + UniqueEntity.register(world, ResourceSelector.from(NAMESPACE, TABLE_NAME)); // Register system world.registerSystem(NAMESPACE, SYSTEM_NAME, uniqueEntitySystem, true); + // Register system's functions world.registerFunctionSelector(NAMESPACE, SYSTEM_NAME, "getUniqueEntity", "()"); } diff --git a/packages/world/src/modules/uniqueentity/tables/UniqueEntity.sol b/packages/world/src/modules/uniqueentity/tables/UniqueEntity.sol index 273500b356..f7c9cc8fae 100644 --- a/packages/world/src/modules/uniqueentity/tables/UniqueEntity.sol +++ b/packages/world/src/modules/uniqueentity/tables/UniqueEntity.sol @@ -18,54 +18,47 @@ import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCounter.sol"; library UniqueEntity { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.UINT256; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](0); return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "value"; - return ("UniqueEntity", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](1); + _schema[0] = SchemaType.UINT256; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema(bytes32 _tableId) internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](0); } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store, bytes32 _tableId) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "value"; } - /** Set the table's metadata */ - function setMetadata(bytes32 _tableId) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register(bytes32 _tableId) internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store, bytes32 _tableId) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store, bytes32 _tableId) internal { + _store.registerTable(_tableId, 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, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (uint256(Bytes.slice32(_blob, 0))); } @@ -73,7 +66,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, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (uint256(Bytes.slice32(_blob, 0))); } @@ -81,14 +74,14 @@ library UniqueEntity { function set(bytes32 _tableId, uint256 value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getValueSchema()); } /** 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)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getValueSchema()); } /** Tightly pack full data using this table's schema */ @@ -107,13 +100,13 @@ library UniqueEntity { function deleteRecord(bytes32 _tableId) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* 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, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/src/tables/InstalledModules.sol b/packages/world/src/tables/InstalledModules.sol index 9370712a20..6add672ab5 100644 --- a/packages/world/src/tables/InstalledModules.sol +++ b/packages/world/src/tables/InstalledModules.sol @@ -25,14 +25,7 @@ struct InstalledModulesData { } library InstalledModules { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.ADDRESS; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](2); _schema[0] = SchemaType.BYTES16; @@ -41,33 +34,35 @@ library InstalledModules { return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "moduleAddress"; - return ("InstalledModules", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](1); + _schema[0] = SchemaType.ADDRESS; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](2); + keyNames[0] = "moduleName"; + keyNames[1] = "argumentsHash"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "moduleAddress"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get moduleAddress */ @@ -76,7 +71,7 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (address(Bytes.slice20(_blob, 0))); } @@ -90,7 +85,7 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (address(Bytes.slice20(_blob, 0))); } @@ -100,7 +95,7 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((moduleAddress)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((moduleAddress)), getValueSchema()); } /** Set moduleAddress (using the specified store) */ @@ -109,7 +104,7 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((moduleAddress)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((moduleAddress)), getValueSchema()); } /** Get the full data */ @@ -118,7 +113,7 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -132,7 +127,7 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -144,7 +139,7 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - StoreSwitch.setRecord(_tableId, _keyTuple, _data, getSchema()); + StoreSwitch.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using individual values (using the specified store) */ @@ -155,7 +150,7 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - _store.setRecord(_tableId, _keyTuple, _data, getSchema()); + _store.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using the data struct */ @@ -193,7 +188,7 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -202,6 +197,6 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/src/tables/NamespaceOwner.sol b/packages/world/src/tables/NamespaceOwner.sol index 79c2a002a5..4685b734fc 100644 --- a/packages/world/src/tables/NamespaceOwner.sol +++ b/packages/world/src/tables/NamespaceOwner.sol @@ -21,48 +21,42 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Names bytes32 constant NamespaceOwnerTableId = _tableId; library NamespaceOwner { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { + /** Get the table's key schema */ + function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.ADDRESS; + _schema[0] = SchemaType.BYTES16; return SchemaLib.encode(_schema); } - function getKeySchema() internal pure returns (Schema) { + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BYTES16; + _schema[0] = SchemaType.ADDRESS; return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "owner"; - return ("NamespaceOwner", _fieldNames); - } - - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "namespace"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "owner"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get owner */ @@ -70,7 +64,7 @@ library NamespaceOwner { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (address(Bytes.slice20(_blob, 0))); } @@ -79,7 +73,7 @@ library NamespaceOwner { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (address(Bytes.slice20(_blob, 0))); } @@ -88,7 +82,7 @@ library NamespaceOwner { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((owner)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((owner)), getValueSchema()); } /** Set owner (using the specified store) */ @@ -96,7 +90,7 @@ library NamespaceOwner { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((owner)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((owner)), getValueSchema()); } /** Tightly pack full data using this table's schema */ @@ -117,7 +111,7 @@ library NamespaceOwner { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -125,6 +119,6 @@ library NamespaceOwner { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/src/tables/ResourceAccess.sol b/packages/world/src/tables/ResourceAccess.sol index a5d8fde47f..1d2e090231 100644 --- a/packages/world/src/tables/ResourceAccess.sol +++ b/packages/world/src/tables/ResourceAccess.sol @@ -21,14 +21,7 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Resou bytes32 constant ResourceAccessTableId = _tableId; library ResourceAccess { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BOOL; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](2); _schema[0] = SchemaType.BYTES32; @@ -37,33 +30,35 @@ library ResourceAccess { return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "access"; - return ("ResourceAccess", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](1); + _schema[0] = SchemaType.BOOL; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](2); + keyNames[0] = "resourceSelector"; + keyNames[1] = "caller"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "access"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get access */ @@ -72,7 +67,7 @@ library ResourceAccess { _keyTuple[0] = resourceSelector; _keyTuple[1] = bytes32(uint256(uint160(caller))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -82,7 +77,7 @@ library ResourceAccess { _keyTuple[0] = resourceSelector; _keyTuple[1] = bytes32(uint256(uint160(caller))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -92,7 +87,7 @@ library ResourceAccess { _keyTuple[0] = resourceSelector; _keyTuple[1] = bytes32(uint256(uint160(caller))); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((access)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((access)), getValueSchema()); } /** Set access (using the specified store) */ @@ -101,7 +96,7 @@ library ResourceAccess { _keyTuple[0] = resourceSelector; _keyTuple[1] = bytes32(uint256(uint160(caller))); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((access)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((access)), getValueSchema()); } /** Tightly pack full data using this table's schema */ @@ -124,7 +119,7 @@ library ResourceAccess { _keyTuple[0] = resourceSelector; _keyTuple[1] = bytes32(uint256(uint160(caller))); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -133,7 +128,7 @@ library ResourceAccess { _keyTuple[0] = resourceSelector; _keyTuple[1] = bytes32(uint256(uint160(caller))); - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/test/AccessControl.t.sol b/packages/world/test/AccessControl.t.sol index ad7accb5f9..fcafb4f67d 100644 --- a/packages/world/test/AccessControl.t.sol +++ b/packages/world/test/AccessControl.t.sol @@ -17,8 +17,8 @@ contract AccessControlTest is Test, StoreReadWithStubs { address caller = address(0x01); function setUp() public { - ResourceAccess.registerSchema(); - NamespaceOwner.registerSchema(); + ResourceAccess.register(); + NamespaceOwner.register(); NamespaceOwner.set(namespace, address(this)); ResourceAccess.set(ResourceSelector.from(namespace, 0), address(this), true); diff --git a/packages/world/test/KeysInTableModule.t.sol b/packages/world/test/KeysInTableModule.t.sol index b330eaaa0c..a1547985c1 100644 --- a/packages/world/test/KeysInTableModule.t.sol +++ b/packages/world/test/KeysInTableModule.t.sol @@ -34,7 +34,7 @@ contract KeysInTableModuleTest is Test, GasReporter { bytes32 key3 = keccak256("test3"); bytes32[] keyTuple3; - Schema tableSchema; + Schema tableValueSchema; Schema tableKeySchema; Schema singletonKeySchema; Schema compositeKeySchema; @@ -46,7 +46,7 @@ contract KeysInTableModuleTest is Test, GasReporter { uint256 val2 = 42; function setUp() public { - tableSchema = SchemaEncodeHelper.encode(SchemaType.UINT256); + tableValueSchema = SchemaEncodeHelper.encode(SchemaType.UINT256); tableKeySchema = SchemaEncodeHelper.encode(SchemaType.BYTES32); compositeKeySchema = SchemaEncodeHelper.encode(SchemaType.BYTES32, SchemaType.BYTES32, SchemaType.BYTES32); @@ -65,9 +65,23 @@ contract KeysInTableModuleTest is Test, GasReporter { function _installKeysInTableModule() internal { // Register source table - tableId = world.registerTable(namespace, name, tableSchema, tableKeySchema); - singletonTableId = world.registerTable(namespace, singletonName, tableSchema, singletonKeySchema); - compositeTableId = world.registerTable(namespace, compositeName, tableSchema, compositeKeySchema); + tableId = world.registerTable(namespace, name, tableKeySchema, tableValueSchema, new string[](1), new string[](1)); + singletonTableId = world.registerTable( + namespace, + singletonName, + singletonKeySchema, + tableValueSchema, + new string[](0), + new string[](1) + ); + compositeTableId = world.registerTable( + namespace, + compositeName, + compositeKeySchema, + tableValueSchema, + new string[](3), + new string[](1) + ); // Install the index module // TODO: add support for installing this via installModule @@ -86,7 +100,7 @@ contract KeysInTableModuleTest is Test, GasReporter { bytes32[] memory keyTuple = new bytes32[](0); - world.setRecord(namespace, singletonName, keyTuple, abi.encodePacked(val1), tableSchema); + world.setRecord(namespace, singletonName, keyTuple, abi.encodePacked(val1), tableValueSchema); // Get the list of keys in this target table bytes32[][] memory keysInTable = getKeysInTable(world, singletonTableId); @@ -103,7 +117,7 @@ contract KeysInTableModuleTest is Test, GasReporter { keyTuple[1] = "two"; keyTuple[2] = "three"; - world.setRecord(namespace, compositeName, keyTuple, abi.encodePacked(val1), tableSchema); + world.setRecord(namespace, compositeName, keyTuple, abi.encodePacked(val1), tableValueSchema); // Get the list of keys in this target table bytes32[][] memory keysInTable = getKeysInTable(world, compositeTableId); @@ -128,7 +142,7 @@ contract KeysInTableModuleTest is Test, GasReporter { _installKeysInTableModule(); // Set a value in the source table startGasReport("set a record on a table with keysInTableModule installed"); - world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value), tableSchema); + world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value), tableValueSchema); endGasReport(); // Get the list of keys in this target table @@ -147,7 +161,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Set a value in the source table startGasReport("set a record on a table with keysInTableModule installed (first)"); - world.setRecord(namespace, name, keyTuple, abi.encodePacked(value1), tableSchema); + world.setRecord(namespace, name, keyTuple, abi.encodePacked(value1), tableValueSchema); endGasReport(); // Get the list of keys in the first target table @@ -159,7 +173,14 @@ contract KeysInTableModuleTest is Test, GasReporter { // Install the hook on the second table bytes16 sourceFile2 = bytes16("source2"); - bytes32 sourceTableId2 = world.registerTable(namespace, sourceFile2, tableSchema, tableKeySchema); + bytes32 sourceTableId2 = world.registerTable( + namespace, + sourceFile2, + tableValueSchema, + tableKeySchema, + new string[](1), + new string[](1) + ); world.installRootModule(keysInTableModule, abi.encode(sourceTableId2)); keyTuple = new bytes32[](1); @@ -167,7 +188,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Set a value in the source table startGasReport("set a record on a table with keysInTableModule installed (second)"); - world.setRecord(namespace, sourceFile2, keyTuple, abi.encodePacked(value2), tableSchema); + world.setRecord(namespace, sourceFile2, keyTuple, abi.encodePacked(value2), tableValueSchema); endGasReport(); // Get the list of keys in the second target table @@ -187,7 +208,7 @@ contract KeysInTableModuleTest is Test, GasReporter { _installKeysInTableModule(); // Set a value in the source table - world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value1), tableSchema); + world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value1), tableValueSchema); // Get the list of keys in the target table bytes32[][] memory keysInTable = getKeysInTable(world, tableId); @@ -197,7 +218,7 @@ contract KeysInTableModuleTest is Test, GasReporter { assertEq(keysInTable[0][0], key1, "2"); // Set another key with the same value - world.setRecord(namespace, name, keyTuple2, abi.encodePacked(value1), tableSchema); + world.setRecord(namespace, name, keyTuple2, abi.encodePacked(value1), tableValueSchema); // Get the list of keys in the target table keysInTable = getKeysInTable(world, tableId); @@ -209,7 +230,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Change the value of the first key startGasReport("change a record on a table with keysInTableModule installed"); - world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value2), tableSchema); + world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value2), tableValueSchema); endGasReport(); // Get the list of keys in the target table @@ -222,7 +243,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Delete the first key startGasReport("delete a record on a table with keysInTableModule installed"); - world.deleteRecord(namespace, name, keyTuple1, tableSchema); + world.deleteRecord(namespace, name, keyTuple1, tableValueSchema); endGasReport(); // Get the list of keys in the target table @@ -252,7 +273,7 @@ contract KeysInTableModuleTest is Test, GasReporter { keyTupleB[2] = "charlie"; // Set a value in the source table - world.setRecord(namespace, compositeName, keyTupleA, abi.encodePacked(value1), tableSchema); + world.setRecord(namespace, compositeName, keyTupleA, abi.encodePacked(value1), tableValueSchema); // Get the list of keys in the target table bytes32[][] memory keysInTable = getKeysInTable(world, compositeTableId); @@ -264,7 +285,7 @@ contract KeysInTableModuleTest is Test, GasReporter { } // Set another key with the same value - world.setRecord(namespace, compositeName, keyTupleB, abi.encodePacked(value1), tableSchema); + world.setRecord(namespace, compositeName, keyTupleB, abi.encodePacked(value1), tableValueSchema); // Get the list of keys in the target table keysInTable = getKeysInTable(world, compositeTableId); @@ -280,7 +301,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Change the value of the first key startGasReport("change a composite record on a table with keysInTableModule installed"); - world.setRecord(namespace, compositeName, keyTupleA, abi.encodePacked(value2), tableSchema); + world.setRecord(namespace, compositeName, keyTupleA, abi.encodePacked(value2), tableValueSchema); endGasReport(); // Get the list of keys in the target table @@ -297,7 +318,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Delete the first key startGasReport("delete a composite record on a table with keysInTableModule installed"); - world.deleteRecord(namespace, compositeName, keyTupleA, tableSchema); + world.deleteRecord(namespace, compositeName, keyTupleA, tableValueSchema); endGasReport(); // Get the list of keys in the target table @@ -315,7 +336,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Set a value in the source table startGasReport("set a field on a table with keysInTableModule installed"); - world.setField(namespace, name, keyTuple1, 0, abi.encodePacked(value1), tableSchema); + world.setField(namespace, name, keyTuple1, 0, abi.encodePacked(value1), tableValueSchema); endGasReport(); // Get the list of keys in the target table @@ -327,7 +348,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Change the value using setField startGasReport("change a field on a table with keysInTableModule installed"); - world.setField(namespace, name, keyTuple1, 0, abi.encodePacked(value2), tableSchema); + world.setField(namespace, name, keyTuple1, 0, abi.encodePacked(value2), tableValueSchema); endGasReport(); // Get the list of keys in the target table @@ -342,7 +363,7 @@ contract KeysInTableModuleTest is Test, GasReporter { _installKeysInTableModule(); // Set a value in the source table - world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value1), tableSchema); + world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value1), tableValueSchema); startGasReport("Get list of keys in a given table"); bytes32[][] memory keysInTable = getKeysInTable(world, tableId); @@ -353,7 +374,7 @@ contract KeysInTableModuleTest is Test, GasReporter { assertEq(keysInTable[0][0], key1); // Set another key with a different value - world.setRecord(namespace, name, keyTuple2, abi.encodePacked(value2), tableSchema); + world.setRecord(namespace, name, keyTuple2, abi.encodePacked(value2), tableValueSchema); // Get the list of keys in the target table keysInTable = getKeysInTable(world, tableId); @@ -368,14 +389,14 @@ contract KeysInTableModuleTest is Test, GasReporter { _installKeysInTableModule(); // Add 3 values - world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value), tableSchema); - world.setRecord(namespace, name, keyTuple2, abi.encodePacked(value), tableSchema); - world.setRecord(namespace, name, keyTuple3, abi.encodePacked(value), tableSchema); + world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value), tableValueSchema); + world.setRecord(namespace, name, keyTuple2, abi.encodePacked(value), tableValueSchema); + world.setRecord(namespace, name, keyTuple3, abi.encodePacked(value), tableValueSchema); // Remove 2, starting from the middle // This tests that KeysInTable correctly tracks swaps indexes - world.deleteRecord(namespace, name, keyTuple2, tableSchema); - world.deleteRecord(namespace, name, keyTuple3, tableSchema); + world.deleteRecord(namespace, name, keyTuple2, tableValueSchema); + world.deleteRecord(namespace, name, keyTuple3, tableValueSchema); // Get the list of keys in the target table bytes32[][] memory keysInTable = getKeysInTable(world, tableId); diff --git a/packages/world/test/KeysWithValueModule.t.sol b/packages/world/test/KeysWithValueModule.t.sol index 5ab148ca00..3f55d0b8d7 100644 --- a/packages/world/test/KeysWithValueModule.t.sol +++ b/packages/world/test/KeysWithValueModule.t.sol @@ -52,7 +52,14 @@ contract KeysWithValueModuleTest is Test, GasReporter { function _installKeysWithValueModule() internal { // Register source table - sourceTableId = world.registerTable(namespace, sourceName, sourceTableSchema, sourceTableKeySchema); + sourceTableId = world.registerTable( + namespace, + sourceName, + sourceTableSchema, + sourceTableKeySchema, + new string[](1), + new string[](1) + ); // Install the index module // TODO: add support for installing this via installModule diff --git a/packages/world/test/World.t.sol b/packages/world/test/World.t.sol index ac82db2502..e0888a7328 100644 --- a/packages/world/test/World.t.sol +++ b/packages/world/test/World.t.sol @@ -11,7 +11,8 @@ import { StoreCore, StoreCoreInternal } from "@latticexyz/store/src/StoreCore.so import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol"; import { Schema } from "@latticexyz/store/src/Schema.sol"; import { SchemaEncodeHelper } from "@latticexyz/store/test/SchemaEncodeHelper.sol"; -import { StoreMetadataData, StoreMetadata } from "@latticexyz/store/src/codegen/Tables.sol"; +import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; +import { Tables, TablesTableId } from "@latticexyz/store/src/codegen/Tables.sol"; import { EncodeArray } from "@latticexyz/store/src/tightcoder/EncodeArray.sol"; import { World } from "../src/World.sol"; @@ -19,7 +20,7 @@ import { System } from "../src/System.sol"; import { ResourceSelector } from "../src/ResourceSelector.sol"; import { ROOT_NAMESPACE, ROOT_NAME } from "../src/constants.sol"; -import { NamespaceOwner } from "../src/tables/NamespaceOwner.sol"; +import { NamespaceOwner, NamespaceOwnerTableId } from "../src/tables/NamespaceOwner.sol"; import { ResourceAccess } from "../src/tables/ResourceAccess.sol"; import { CoreModule } from "../src/modules/core/CoreModule.sol"; @@ -77,7 +78,7 @@ contract WorldTestSystem is System { function writeData(bytes16 namespace, bytes16 name, bool data) public { bytes32[] memory key = new bytes32[](0); bytes32 tableId = ResourceSelector.from(namespace, name); - Schema valueSchema = StoreSwitch.getSchema(tableId); + Schema valueSchema = StoreSwitch.getValueSchema(tableId); if (StoreSwitch.getStoreAddress() == address(this)) { StoreCore.setRecord(tableId, key, abi.encodePacked(data), valueSchema); @@ -191,20 +192,17 @@ contract WorldTest is Test, GasReporter { emit HelloWorld(); new World(); - bytes32[] memory schemaKey = new bytes32[](1); - schemaKey[0] = StoreCoreInternal.SCHEMA_TABLE; - bytes memory value = world.getRecord( - StoreCoreInternal.SCHEMA_TABLE, - schemaKey, - SchemaEncodeHelper.encode(SchemaType.BYTES32, SchemaType.BYTES32) - ); - assertEq( - value, - abi.encodePacked( - SchemaEncodeHelper.encode(SchemaType.BYTES32, SchemaType.BYTES32), - SchemaEncodeHelper.encode(SchemaType.BYTES32) - ) - ); + // Should have registered the table data table (fka schema table) + assertEq(Tables.getKeySchema(world, TablesTableId), Schema.unwrap(Tables.getKeySchema())); + assertEq(Tables.getValueSchema(world, TablesTableId), Schema.unwrap(Tables.getValueSchema())); + assertEq(Tables.getAbiEncodedKeyNames(world, TablesTableId), abi.encode(Tables.getKeyNames())); + assertEq(Tables.getAbiEncodedFieldNames(world, TablesTableId), abi.encode(Tables.getFieldNames())); + + // Should have registered the namespace owner table + assertEq(Tables.getKeySchema(world, NamespaceOwnerTableId), Schema.unwrap(NamespaceOwner.getKeySchema())); + assertEq(Tables.getValueSchema(world, NamespaceOwnerTableId), Schema.unwrap(NamespaceOwner.getValueSchema())); + assertEq(Tables.getAbiEncodedKeyNames(world, NamespaceOwnerTableId), abi.encode(NamespaceOwner.getKeyNames())); + assertEq(Tables.getAbiEncodedFieldNames(world, NamespaceOwnerTableId), abi.encode(NamespaceOwner.getFieldNames())); } function testRootNamespace() public { @@ -248,74 +246,52 @@ contract WorldTest is Test, GasReporter { } function testRegisterTable() public { - Schema schema = SchemaEncodeHelper.encode(SchemaType.BOOL, SchemaType.UINT256, SchemaType.STRING); + Schema valueSchema = SchemaEncodeHelper.encode(SchemaType.BOOL, SchemaType.UINT256, SchemaType.STRING); bytes16 namespace = "testNamespace"; - bytes16 table = "testTable"; + bytes16 tableName = "testTable"; + + string[] memory keyNames = new string[](1); + keyNames[0] = "key1"; + string[] memory fieldNames = new string[](3); + fieldNames[0] = "value1"; + fieldNames[1] = "value2"; + fieldNames[2] = "value3"; startGasReport("Register a new table in the namespace"); - bytes32 tableSelector = world.registerTable(namespace, table, schema, defaultKeySchema); + bytes32 tableSelector = world.registerTable( + namespace, + tableName, + defaultKeySchema, + valueSchema, + keyNames, + fieldNames + ); endGasReport(); // Expect the namespace to be created and owned by the caller - assertEq(NamespaceOwner.get(world, namespace), address(this)); + assertEq(NamespaceOwner.get(world, namespace), address(this), "namespace should be created by caller"); // Expect the table to be registered + assertEq(world.getKeySchema(tableSelector).unwrap(), defaultKeySchema.unwrap(), "key schema should be registered"); + assertEq(world.getValueSchema(tableSelector).unwrap(), valueSchema.unwrap(), "value schema should be registered"); + + bytes memory loadedKeyNames = Tables.getAbiEncodedKeyNames(world, tableSelector); + assertEq(loadedKeyNames, abi.encode(keyNames), "key names should be registered"); - assertEq(world.getSchema(tableSelector).unwrap(), schema.unwrap(), "schema should be registered"); + bytes memory loadedfieldNames = Tables.getAbiEncodedFieldNames(world, tableSelector); + assertEq(loadedfieldNames, abi.encode(fieldNames), "value names should be registered"); // Expect an error when registering an existing table vm.expectRevert(abi.encodeWithSelector(IWorldErrors.ResourceExists.selector, tableSelector.toString())); - world.registerTable(namespace, table, schema, defaultKeySchema); + world.registerTable(namespace, tableName, defaultKeySchema, valueSchema, keyNames, fieldNames); // Expect an error when registering a table in a namespace that is not owned by the caller _expectAccessDenied(address(0x01), namespace, ""); - world.registerTable(namespace, "otherTable", schema, defaultKeySchema); + world.registerTable(namespace, "otherTable", defaultKeySchema, valueSchema, keyNames, fieldNames); // Expect the World to be allowed to call registerTable vm.prank(address(world)); - world.registerTable(namespace, "otherTable", schema, defaultKeySchema); - } - - function testSetMetadata() public { - string memory tableName = "testTable"; - bytes16 namespace = "testNamespace"; - bytes16 name = "tableName"; - bytes32 tableId = ResourceSelector.from(namespace, name); - - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT8); - string[] memory fieldNames = new string[](2); - fieldNames[0] = "testField1"; - fieldNames[1] = "testField2"; - - // Expect an error when setting metadata on a resource that does not exist - vm.expectRevert(); - world.setMetadata("invalid", "invalid", tableName, fieldNames); - - // Register a table - world.registerTable(namespace, name, schema, defaultKeySchema); - - startGasReport("Set metadata"); - world.setMetadata(namespace, name, tableName, fieldNames); - endGasReport(); - - // Expect the metadata to be set - StoreMetadataData memory metadata = StoreMetadata.get(world, tableId); - assertEq(metadata.tableName, tableName); - assertEq(metadata.abiEncodedFieldNames, abi.encode(fieldNames)); - - // Expect it to be possible to change metadata - world.setMetadata(namespace, name, "newTableName", fieldNames); - metadata = StoreMetadata.get(world, tableId); - assertEq(metadata.tableName, "newTableName"); - assertEq(metadata.abiEncodedFieldNames, abi.encode(fieldNames)); - - // Expect an error when setting metadata on a route that is not owned by the caller - _expectAccessDenied(address(1), namespace, name); - world.setMetadata(namespace, name, tableName, fieldNames); - - // Expect the World to be allowed to set metadata - vm.prank(address(world)); - world.setMetadata(namespace, name, tableName, fieldNames); + world.registerTable(namespace, "otherTable", defaultKeySchema, valueSchema, keyNames, fieldNames); } function testRegisterSystem() public { @@ -374,7 +350,14 @@ contract WorldTest is Test, GasReporter { function testDuplicateSelectors() public { // Register a new table - bytes32 resourceSelector = world.registerTable("namespace", "name", Bool.getSchema(), defaultKeySchema); + bytes32 resourceSelector = world.registerTable( + "namespace", + "name", + defaultKeySchema, + Bool.getValueSchema(), + new string[](1), + new string[](1) + ); // Deploy a new system System system = new System(); @@ -388,7 +371,14 @@ contract WorldTest is Test, GasReporter { // Expect an error when trying to register a table at the same selector vm.expectRevert(abi.encodeWithSelector(IWorldErrors.ResourceExists.selector, resourceSelector.toString())); - world.registerTable("namespace2", "name", Bool.getSchema(), defaultKeySchema); + world.registerTable( + "namespace2", + "name", + defaultKeySchema, + Bool.getValueSchema(), + new string[](1), + new string[](1) + ); } function testGrantAccess() public { @@ -401,7 +391,14 @@ contract WorldTest is Test, GasReporter { function testSetRecord() public { // Register a new table - bytes32 tableId = world.registerTable("testSetRecord", "testTable", Bool.getSchema(), defaultKeySchema); + bytes32 tableId = world.registerTable( + "testSetRecord", + "testTable", + defaultKeySchema, + Bool.getValueSchema(), + new string[](1), + new string[](1) + ); startGasReport("Write data to the table"); Bool.set(world, tableId, true); @@ -422,44 +419,58 @@ contract WorldTest is Test, GasReporter { function testSetField() public { bytes16 namespace = "testSetField"; bytes16 name = "testTable"; - Schema schema = Bool.getSchema(); + Schema valueSchema = Bool.getValueSchema(); // Register a new table - bytes32 tableId = world.registerTable(namespace, name, schema, defaultKeySchema); + bytes32 tableId = world.registerTable( + namespace, + name, + defaultKeySchema, + valueSchema, + new string[](1), + new string[](1) + ); startGasReport("Write data to a table field"); - world.setField(namespace, name, singletonKey, 0, abi.encodePacked(true), schema); + world.setField(namespace, name, singletonKey, 0, abi.encodePacked(true), valueSchema); endGasReport(); // Expect the data to be written assertTrue(Bool.get(world, tableId)); // Write data to the table via its tableId - world.setField(tableId, singletonKey, 0, abi.encodePacked(false), schema); + world.setField(tableId, singletonKey, 0, abi.encodePacked(false), valueSchema); // Expect the data to be written assertFalse(Bool.get(world, tableId)); // Expect an error when trying to write from an address that doesn't have access when calling via the namespace _expectAccessDenied(address(0x01), "testSetField", "testTable"); - world.setField("testSetField", "testTable", singletonKey, 0, abi.encodePacked(true), schema); + world.setField("testSetField", "testTable", singletonKey, 0, abi.encodePacked(true), valueSchema); // Expect an error when trying to write from an address that doesn't have access when calling via the tableId _expectAccessDenied(address(0x01), "testSetField", "testTable"); - world.setField(tableId, singletonKey, 0, abi.encodePacked(true), schema); + world.setField(tableId, singletonKey, 0, abi.encodePacked(true), valueSchema); // Expect the World to have access vm.prank(address(world)); - world.setField("testSetField", "testTable", singletonKey, 0, abi.encodePacked(true), schema); + world.setField("testSetField", "testTable", singletonKey, 0, abi.encodePacked(true), valueSchema); } function testPushToField() public { bytes16 namespace = "testPushToField"; bytes16 name = "testTable"; - Schema schema = AddressArray.getSchema(); + Schema valueSchema = AddressArray.getValueSchema(); // Register a new table - bytes32 tableId = world.registerTable(namespace, name, schema, defaultKeySchema); + bytes32 tableId = world.registerTable( + namespace, + name, + defaultKeySchema, + valueSchema, + new string[](1), + new string[](1) + ); // Create data address[] memory dataToPush = new address[](3); @@ -469,76 +480,83 @@ contract WorldTest is Test, GasReporter { bytes memory encodedData = EncodeArray.encode(dataToPush); startGasReport("Push data to the table"); - world.pushToField(namespace, name, keyTuple, 0, encodedData, schema); + world.pushToField(namespace, name, keyTuple, 0, encodedData, valueSchema); endGasReport(); // Expect the data to be written assertEq(AddressArray.get(world, tableId, key), dataToPush); // Delete the data - world.deleteRecord(namespace, name, keyTuple, schema); + world.deleteRecord(namespace, name, keyTuple, valueSchema); // Push data to the table via direct access - world.pushToField(tableId, keyTuple, 0, encodedData, schema); + world.pushToField(tableId, keyTuple, 0, encodedData, valueSchema); // Expect the data to be written assertEq(AddressArray.get(world, tableId, key), dataToPush); // Expect an error when trying to write from an address that doesn't have access (via namespace/name) _expectAccessDenied(address(0x01), namespace, name); - world.pushToField(namespace, name, keyTuple, 0, encodedData, schema); + world.pushToField(namespace, name, keyTuple, 0, encodedData, valueSchema); // Expect an error when trying to write from an address that doesn't have access (via tableId) _expectAccessDenied(address(0x01), namespace, name); - world.pushToField(tableId, keyTuple, 0, encodedData, schema); + world.pushToField(tableId, keyTuple, 0, encodedData, valueSchema); // Expect the World to have access vm.prank(address(world)); - world.pushToField(namespace, name, keyTuple, 0, encodedData, schema); + world.pushToField(namespace, name, keyTuple, 0, encodedData, valueSchema); } function testDeleteRecord() public { bytes16 namespace = "testDeleteRecord"; bytes16 name = "testTable"; - Schema schema = Bool.getSchema(); + Schema valueSchema = Bool.getValueSchema(); // Register a new table - bytes32 tableId = world.registerTable(namespace, name, schema, defaultKeySchema); + bytes32 tableId = world.registerTable( + namespace, + name, + defaultKeySchema, + valueSchema, + new string[](1), + new string[](1) + ); // Write data to the table via the namespace and expect it to be written - world.setRecord(namespace, name, singletonKey, abi.encodePacked(true), schema); + world.setRecord(namespace, name, singletonKey, abi.encodePacked(true), valueSchema); assertTrue(Bool.get(world, tableId)); startGasReport("Delete record"); - world.deleteRecord(namespace, name, singletonKey, schema); + world.deleteRecord(namespace, name, singletonKey, valueSchema); endGasReport(); // expect it to be deleted assertFalse(Bool.get(world, tableId)); // Write data to the table via the namespace and expect it to be written - world.setRecord("testDeleteRecord", "testTable", singletonKey, abi.encodePacked(true), schema); + world.setRecord("testDeleteRecord", "testTable", singletonKey, abi.encodePacked(true), valueSchema); assertTrue(Bool.get(world, tableId)); // Delete the record via the tableId and expect it to be deleted - world.deleteRecord(tableId, singletonKey, schema); + world.deleteRecord(tableId, singletonKey, valueSchema); assertFalse(Bool.get(world, tableId)); // Write data to the table via the namespace and expect it to be written - world.setRecord("testDeleteRecord", "testTable", singletonKey, abi.encodePacked(true), schema); + world.setRecord("testDeleteRecord", "testTable", singletonKey, abi.encodePacked(true), valueSchema); assertTrue(Bool.get(world, tableId)); // Expect an error when trying to delete from an address that doesn't have access when calling via the namespace _expectAccessDenied(address(0x01), "testDeleteRecord", "testTable"); - world.deleteRecord("testDeleteRecord", "testTable", singletonKey, schema); + world.deleteRecord("testDeleteRecord", "testTable", singletonKey, valueSchema); // Expect an error when trying to delete from an address that doesn't have access when calling via the tableId _expectAccessDenied(address(0x02), "testDeleteRecord", "testTable"); - world.deleteRecord(tableId, singletonKey, schema); + world.deleteRecord(tableId, singletonKey, valueSchema); // Expect the World to have access vm.prank(address(world)); - world.deleteRecord("testDeleteRecord", "testTable", singletonKey, schema); + world.deleteRecord("testDeleteRecord", "testTable", singletonKey, valueSchema); } function testCall() public { @@ -613,10 +631,17 @@ contract WorldTest is Test, GasReporter { } function testRegisterTableHook() public { - Schema schema = Bool.getSchema(); + Schema valueSchema = Bool.getValueSchema(); // Register a new table - bytes32 tableId = world.registerTable("", "testTable", Bool.getSchema(), defaultKeySchema); + bytes32 tableId = world.registerTable( + "", + "testTable", + defaultKeySchema, + valueSchema, + new string[](1), + new string[](1) + ); // Register a new hook IStoreHook tableHook = new WorldTestTableHook(); @@ -627,8 +652,8 @@ contract WorldTest is Test, GasReporter { // Expect the hook to be notified when a record is written vm.expectEmit(true, true, true, true); - emit HookCalled(abi.encode(tableId, singletonKey, value, schema)); - world.setRecord(tableId, singletonKey, value, schema); + emit HookCalled(abi.encode(tableId, singletonKey, value, valueSchema)); + world.setRecord(tableId, singletonKey, value, valueSchema); // TODO: add tests for other hook methods (onBeforeSetField, onAfterSetField, onDeleteRecord) // (See https://github.com/latticexyz/mud/issues/444) @@ -661,7 +686,14 @@ contract WorldTest is Test, GasReporter { function testWriteRootSystem() public { // Register a new table - bytes32 tableId = world.registerTable("namespace", "testTable", Bool.getSchema(), defaultKeySchema); + bytes32 tableId = world.registerTable( + "namespace", + "testTable", + defaultKeySchema, + Bool.getValueSchema(), + new string[](1), + new string[](1) + ); // Register a new system WorldTestSystem system = new WorldTestSystem(); @@ -680,7 +712,14 @@ contract WorldTest is Test, GasReporter { function testWriteAutonomousSystem() public { // Register a new table - bytes32 tableId = world.registerTable("namespace", "testTable", Bool.getSchema(), defaultKeySchema); + bytes32 tableId = world.registerTable( + "namespace", + "testTable", + defaultKeySchema, + Bool.getValueSchema(), + new string[](1), + new string[](1) + ); // Register a new system WorldTestSystem system = new WorldTestSystem(); diff --git a/packages/world/test/WorldDynamicUpdate.t.sol b/packages/world/test/WorldDynamicUpdate.t.sol index 5e4f0cd411..809d704923 100644 --- a/packages/world/test/WorldDynamicUpdate.t.sol +++ b/packages/world/test/WorldDynamicUpdate.t.sol @@ -50,7 +50,7 @@ contract UpdateInFieldTest is Test, GasReporter { keyTuple = new bytes32[](1); keyTuple[0] = key; singletonKey = new bytes32[](0); - Schema schema = AddressArray.getSchema(); + Schema valueSchema = AddressArray.getValueSchema(); // Initialize the data in setUp so that slots aren't warm in tests (to test cold update) @@ -58,7 +58,7 @@ contract UpdateInFieldTest is Test, GasReporter { bytes16 name = "testTable"; // Register a new table - tableId = world.registerTable(namespace, name, schema, defaultKeySchema); + tableId = world.registerTable(namespace, name, defaultKeySchema, valueSchema, new string[](1), new string[](1)); // Create data initData = new address[](3); @@ -67,7 +67,7 @@ contract UpdateInFieldTest is Test, GasReporter { initData[2] = address(bytes20(keccak256("another address"))); encodedData = EncodeArray.encode(initData); - world.setField(namespace, name, keyTuple, 0, encodedData, schema); + world.setField(namespace, name, keyTuple, 0, encodedData, valueSchema); } // Expect an error when trying to write from an address that doesn't have access @@ -85,7 +85,7 @@ contract UpdateInFieldTest is Test, GasReporter { function testPopFromField() public { bytes16 namespace = "DynamicUpdTest"; bytes16 name = "testTable"; - Schema schema = AddressArray.getSchema(); + Schema valueSchema = AddressArray.getValueSchema(); // Expect the data to be written assertEq(AddressArray.get(world, tableId, key), initData); @@ -94,7 +94,7 @@ contract UpdateInFieldTest is Test, GasReporter { uint256 byteLengthToPop = 20; startGasReport("pop 1 address (cold)"); - world.popFromField(namespace, name, keyTuple, 0, byteLengthToPop, schema); + world.popFromField(namespace, name, keyTuple, 0, byteLengthToPop, valueSchema); endGasReport(); // Expect the data to be updated @@ -108,7 +108,7 @@ contract UpdateInFieldTest is Test, GasReporter { byteLengthToPop = 20; startGasReport("pop 1 address (warm)"); - world.popFromField(namespace, name, keyTuple, 0, byteLengthToPop, schema); + world.popFromField(namespace, name, keyTuple, 0, byteLengthToPop, valueSchema); endGasReport(); // Expect the data to be updated @@ -119,10 +119,10 @@ contract UpdateInFieldTest is Test, GasReporter { } // Reset data - world.setField(namespace, name, keyTuple, 0, encodedData, schema); + world.setField(namespace, name, keyTuple, 0, encodedData, valueSchema); // Pop 2 items via direct access byteLengthToPop = 20 * 2; - world.popFromField(tableId, keyTuple, 0, byteLengthToPop, schema); + world.popFromField(tableId, keyTuple, 0, byteLengthToPop, valueSchema); // Expect the data to be updated loadedData = AddressArray.get(world, tableId, key); assertEq(loadedData.length, initData.length - 2); @@ -132,21 +132,21 @@ contract UpdateInFieldTest is Test, GasReporter { // Expect an error when trying to write from an address that doesn't have access (via namespace/name) _expectAccessDenied(address(0x01), namespace, name); - world.popFromField(namespace, name, keyTuple, 0, 20, schema); + world.popFromField(namespace, name, keyTuple, 0, 20, valueSchema); // Expect an error when trying to write from an address that doesn't have access (via tableId) _expectAccessDenied(address(0x01), namespace, name); - world.popFromField(tableId, keyTuple, 0, 20, schema); + world.popFromField(tableId, keyTuple, 0, 20, valueSchema); // Expect the World to have access vm.prank(address(world)); - world.popFromField(namespace, name, keyTuple, 0, 20, schema); + world.popFromField(namespace, name, keyTuple, 0, 20, valueSchema); } function testUpdateInField() public { bytes16 namespace = "DynamicUpdTest"; bytes16 name = "testTable"; - Schema schema = AddressArray.getSchema(); + Schema valueSchema = AddressArray.getValueSchema(); // Expect the data to be written assertEq(AddressArray.get(world, tableId, key), initData); @@ -156,11 +156,11 @@ contract UpdateInFieldTest is Test, GasReporter { dataForUpdate[0] = address(bytes20(keccak256("address for update"))); startGasReport("updateInField 1 item (cold)"); - world.updateInField(namespace, name, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), schema); + world.updateInField(namespace, name, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), valueSchema); endGasReport(); startGasReport("updateInField 1 item (warm)"); - world.updateInField(namespace, name, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), schema); + world.updateInField(namespace, name, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), valueSchema); endGasReport(); // Expect the data to be updated @@ -168,7 +168,7 @@ contract UpdateInFieldTest is Test, GasReporter { assertEq(AddressArray.get(world, tableId, key), initData); // Update index 1 via direct access - world.updateInField(tableId, keyTuple, 0, 20 * 1, EncodeArray.encode(dataForUpdate), schema); + world.updateInField(tableId, keyTuple, 0, 20 * 1, EncodeArray.encode(dataForUpdate), valueSchema); // Expect the data to be updated initData[1] = dataForUpdate[0]; @@ -176,14 +176,14 @@ contract UpdateInFieldTest is Test, GasReporter { // Expect an error when trying to write from an address that doesn't have access (via namespace/name) _expectAccessDenied(address(0x01), namespace, name); - world.updateInField(namespace, name, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), schema); + world.updateInField(namespace, name, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), valueSchema); // Expect an error when trying to write from an address that doesn't have access (via tableId) _expectAccessDenied(address(0x01), namespace, name); - world.updateInField(tableId, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), schema); + world.updateInField(tableId, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), valueSchema); // Expect the World to have access vm.prank(address(world)); - world.updateInField(namespace, name, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), schema); + world.updateInField(namespace, name, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), valueSchema); } } diff --git a/packages/world/test/query.t.sol b/packages/world/test/query.t.sol index 73913ee7ea..6a40efae56 100644 --- a/packages/world/test/query.t.sol +++ b/packages/world/test/query.t.sol @@ -29,7 +29,7 @@ contract QueryTest is Test, GasReporter { bytes16 name2 = bytes16("source2"); bytes16 name3 = bytes16("source3"); - Schema tableSchema; + Schema tableValueSchema; Schema tableKeySchema; bytes32 table1 = ResourceSelector.from(namespace, name1); bytes32 table2 = ResourceSelector.from(namespace, name2); @@ -44,7 +44,7 @@ contract QueryTest is Test, GasReporter { QueryFragment[] fragmentsHasNot; function setUp() public { - tableSchema = SchemaEncodeHelper.encode(SchemaType.UINT256); + tableValueSchema = SchemaEncodeHelper.encode(SchemaType.UINT256); tableKeySchema = SchemaEncodeHelper.encode(SchemaType.BYTES32); world = IBaseWorld(address(new World())); world.installRootModule(new CoreModule(), new bytes(0)); @@ -62,9 +62,9 @@ contract QueryTest is Test, GasReporter { function _installKeysInTableModule() internal { // Register source table - table1 = world.registerTable(namespace, name1, tableSchema, tableKeySchema); - table2 = world.registerTable(namespace, name2, tableSchema, tableKeySchema); - table3 = world.registerTable(namespace, name3, tableSchema, tableKeySchema); + table1 = world.registerTable(namespace, name1, tableKeySchema, tableValueSchema, new string[](1), new string[](1)); + table2 = world.registerTable(namespace, name2, tableKeySchema, tableValueSchema, new string[](1), new string[](1)); + table3 = world.registerTable(namespace, name3, tableKeySchema, tableValueSchema, new string[](1), new string[](1)); // Install the index module // TODO: add support for installing this via installModule @@ -86,9 +86,9 @@ contract QueryTest is Test, GasReporter { function testHasQuery() public { _installKeysInTableModule(); - world.setRecord(namespace, name1, key1, abi.encode(1), tableSchema); - world.setRecord(namespace, name1, key2, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key1, abi.encode(0), tableSchema); + world.setRecord(namespace, name1, key1, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name1, key2, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key1, abi.encode(0), tableValueSchema); // Query should return all keys in table1 QueryFragment[] memory fragments = new QueryFragment[](1); @@ -107,9 +107,9 @@ contract QueryTest is Test, GasReporter { _installKeysInTableModule(); _installKeysWithValueModule(); - world.setRecord(namespace, name1, key1, abi.encode(2), tableSchema); - world.setRecord(namespace, name1, key2, abi.encode(1), tableSchema); - world.setRecord(namespace, name1, key3, abi.encode(1), tableSchema); + world.setRecord(namespace, name1, key1, abi.encode(2), tableValueSchema); + world.setRecord(namespace, name1, key2, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name1, key3, abi.encode(1), tableValueSchema); // Query should return all keys in table1 with value 1 QueryFragment[] memory fragments = new QueryFragment[](1); fragments[0] = QueryFragment(QueryType.HasValue, table1, abi.encode(1)); @@ -125,12 +125,12 @@ contract QueryTest is Test, GasReporter { function testCombinedHasQuery() public { _installKeysInTableModule(); - world.setRecord(namespace, name1, key1, abi.encode(2), tableSchema); - world.setRecord(namespace, name1, key2, abi.encode(1), tableSchema); - world.setRecord(namespace, name1, key3, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key2, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key3, abi.encode(1), tableSchema); - world.setRecord(namespace, name3, key1, abi.encode(1), tableSchema); + world.setRecord(namespace, name1, key1, abi.encode(2), tableValueSchema); + world.setRecord(namespace, name1, key2, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name1, key3, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key2, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key3, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name3, key1, abi.encode(1), tableValueSchema); // Query should return all entities that have table1 and table2 QueryFragment[] memory fragments = new QueryFragment[](2); @@ -149,12 +149,12 @@ contract QueryTest is Test, GasReporter { _installKeysInTableModule(); _installKeysWithValueModule(); - world.setRecord(namespace, name1, key1, abi.encode(2), tableSchema); - world.setRecord(namespace, name1, key2, abi.encode(2), tableSchema); - world.setRecord(namespace, name1, key3, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key2, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key3, abi.encode(1), tableSchema); - world.setRecord(namespace, name3, key1, abi.encode(1), tableSchema); + world.setRecord(namespace, name1, key1, abi.encode(2), tableValueSchema); + world.setRecord(namespace, name1, key2, abi.encode(2), tableValueSchema); + world.setRecord(namespace, name1, key3, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key2, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key3, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name3, key1, abi.encode(1), tableValueSchema); // Query should return all entities that have table1 and table2 QueryFragment[] memory fragments = new QueryFragment[](2); @@ -172,13 +172,13 @@ contract QueryTest is Test, GasReporter { _installKeysInTableModule(); _installKeysWithValueModule(); - world.setRecord(namespace, name1, key1, abi.encode(1), tableSchema); - world.setRecord(namespace, name1, key2, abi.encode(1), tableSchema); - world.setRecord(namespace, name1, key3, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key1, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key2, abi.encode(2), tableSchema); - world.setRecord(namespace, name2, key3, abi.encode(2), tableSchema); - world.setRecord(namespace, name2, key4, abi.encode(2), tableSchema); + world.setRecord(namespace, name1, key1, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name1, key2, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name1, key3, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key1, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key2, abi.encode(2), tableValueSchema); + world.setRecord(namespace, name2, key3, abi.encode(2), tableValueSchema); + world.setRecord(namespace, name2, key4, abi.encode(2), tableValueSchema); // Query should return all entities that have table1 and table2 QueryFragment[] memory fragments = new QueryFragment[](2); @@ -196,13 +196,13 @@ contract QueryTest is Test, GasReporter { function testCombinedHasNotQuery() public { _installKeysInTableModule(); - world.setRecord(namespace, name1, key1, abi.encode(1), tableSchema); - world.setRecord(namespace, name1, key2, abi.encode(1), tableSchema); - world.setRecord(namespace, name1, key3, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key1, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key2, abi.encode(2), tableSchema); - world.setRecord(namespace, name2, key3, abi.encode(2), tableSchema); - world.setRecord(namespace, name2, key4, abi.encode(2), tableSchema); + world.setRecord(namespace, name1, key1, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name1, key2, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name1, key3, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key1, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key2, abi.encode(2), tableValueSchema); + world.setRecord(namespace, name2, key3, abi.encode(2), tableValueSchema); + world.setRecord(namespace, name2, key4, abi.encode(2), tableValueSchema); // Query should return all entities that have table1 and table2 QueryFragment[] memory fragments = new QueryFragment[](2); @@ -220,13 +220,13 @@ contract QueryTest is Test, GasReporter { _installKeysInTableModule(); _installKeysWithValueModule(); - world.setRecord(namespace, name1, key1, abi.encode(1), tableSchema); - world.setRecord(namespace, name1, key2, abi.encode(1), tableSchema); - world.setRecord(namespace, name1, key3, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key1, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key2, abi.encode(2), tableSchema); - world.setRecord(namespace, name2, key3, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key4, abi.encode(1), tableSchema); + world.setRecord(namespace, name1, key1, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name1, key2, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name1, key3, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key1, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key2, abi.encode(2), tableValueSchema); + world.setRecord(namespace, name2, key3, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key4, abi.encode(1), tableValueSchema); // Query should return all entities that have table1 and table2 QueryFragment[] memory fragments = new QueryFragment[](2); @@ -244,16 +244,16 @@ contract QueryTest is Test, GasReporter { _installKeysInTableModule(); _installKeysWithValueModule(); - world.setRecord(namespace, name1, key1, abi.encode(1), tableSchema); - world.setRecord(namespace, name1, key2, abi.encode(1), tableSchema); - world.setRecord(namespace, name1, key3, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key1, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key2, abi.encode(2), tableSchema); - world.setRecord(namespace, name2, key3, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key4, abi.encode(1), tableSchema); - world.setRecord(namespace, name3, key2, abi.encode(1), tableSchema); - world.setRecord(namespace, name3, key3, abi.encode(1), tableSchema); - world.setRecord(namespace, name3, key4, abi.encode(1), tableSchema); + world.setRecord(namespace, name1, key1, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name1, key2, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name1, key3, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key1, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key2, abi.encode(2), tableValueSchema); + world.setRecord(namespace, name2, key3, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key4, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name3, key2, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name3, key3, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name3, key4, abi.encode(1), tableValueSchema); // Query should return all entities that have table2 and not table1 QueryFragment[] memory fragments = new QueryFragment[](3); @@ -272,9 +272,9 @@ contract QueryTest is Test, GasReporter { _installKeysInTableModule(); _installKeysWithValueModule(); - world.setRecord(namespace, name1, key1, abi.encode(4), tableSchema); - world.setRecord(namespace, name1, key2, abi.encode(5), tableSchema); - world.setRecord(namespace, name1, key3, abi.encode(6), tableSchema); + world.setRecord(namespace, name1, key1, abi.encode(4), tableValueSchema); + world.setRecord(namespace, name1, key2, abi.encode(5), tableValueSchema); + world.setRecord(namespace, name1, key3, abi.encode(6), tableValueSchema); // Query should return all entities with table1 except value 6 QueryFragment[] memory fragments = new QueryFragment[](2); @@ -295,9 +295,9 @@ contract QueryTest is Test, GasReporter { for (uint256 i; i < 100; i++) { bytes32[] memory key = new bytes32[](1); key[0] = bytes32(i); - world.setRecord(namespace, name1, key, abi.encode(1), tableSchema); + world.setRecord(namespace, name1, key, abi.encode(1), tableValueSchema); } - world.setRecord(namespace, name2, key1, abi.encode(0), tableSchema); + world.setRecord(namespace, name2, key1, abi.encode(0), tableValueSchema); // Query should return all keys in table1 QueryFragment[] memory fragments = new QueryFragment[](1); @@ -316,9 +316,9 @@ contract QueryTest is Test, GasReporter { for (uint256 i; i < 1000; i++) { bytes32[] memory key = new bytes32[](1); key[0] = bytes32(i); - world.setRecord(namespace, name1, key, abi.encode(1), tableSchema); + world.setRecord(namespace, name1, key, abi.encode(1), tableValueSchema); } - world.setRecord(namespace, name2, key1, abi.encode(0), tableSchema); + world.setRecord(namespace, name2, key1, abi.encode(0), tableValueSchema); // Query should return all keys in table1 QueryFragment[] memory fragments = new QueryFragment[](1); diff --git a/packages/world/test/tables/AddressArray.sol b/packages/world/test/tables/AddressArray.sol index 1e908a3ff9..d7872f3825 100644 --- a/packages/world/test/tables/AddressArray.sol +++ b/packages/world/test/tables/AddressArray.sol @@ -18,48 +18,42 @@ import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCounter.sol"; library AddressArray { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { + /** Get the table's key schema */ + function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.ADDRESS_ARRAY; + _schema[0] = SchemaType.BYTES32; return SchemaLib.encode(_schema); } - function getKeySchema() internal pure returns (Schema) { + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BYTES32; + _schema[0] = SchemaType.ADDRESS_ARRAY; return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "value"; - return ("AddressArray", _fieldNames); - } - - /** Register the table's schema */ - function registerSchema(bytes32 _tableId) internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "key"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store, bytes32 _tableId) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "value"; } - /** Set the table's metadata */ - function setMetadata(bytes32 _tableId) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register(bytes32 _tableId) internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store, bytes32 _tableId) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store, bytes32 _tableId) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ @@ -67,7 +61,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_address()); } @@ -76,7 +70,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_address()); } @@ -85,7 +79,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getValueSchema()); } /** Set value (using the specified store) */ @@ -93,7 +87,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getValueSchema()); } /** Get the length of value */ @@ -101,7 +95,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); unchecked { return _byteLength / 20; } @@ -112,7 +106,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); unchecked { return _byteLength / 20; } @@ -131,7 +125,7 @@ library AddressArray { _tableId, _keyTuple, 0, - getSchema(), + getValueSchema(), _index * 20, (_index + 1) * 20 ); @@ -148,7 +142,14 @@ library AddressArray { _keyTuple[0] = key; unchecked { - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 20, (_index + 1) * 20); + bytes memory _blob = _store.getFieldSlice( + _tableId, + _keyTuple, + 0, + getValueSchema(), + _index * 20, + (_index + 1) * 20 + ); return (address(Bytes.slice20(_blob, 0))); } } @@ -158,7 +159,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to value (using the specified store) */ @@ -166,7 +167,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from value */ @@ -174,7 +175,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 20, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 0, 20, getValueSchema()); } /** Pop an element from value (using the specified store) */ @@ -182,7 +183,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 0, 20, getSchema()); + _store.popFromField(_tableId, _keyTuple, 0, 20, getValueSchema()); } /** @@ -194,7 +195,7 @@ library AddressArray { _keyTuple[0] = key; unchecked { - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), getValueSchema()); } } @@ -207,7 +208,7 @@ library AddressArray { _keyTuple[0] = key; unchecked { - _store.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), getValueSchema()); } } @@ -233,7 +234,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -241,6 +242,6 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/test/tables/Bool.sol b/packages/world/test/tables/Bool.sol index c26547f043..828eb8bb45 100644 --- a/packages/world/test/tables/Bool.sol +++ b/packages/world/test/tables/Bool.sol @@ -18,54 +18,47 @@ import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCounter.sol"; library Bool { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BOOL; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](0); return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "value"; - return ("Bool", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](1); + _schema[0] = SchemaType.BOOL; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema(bytes32 _tableId) internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](0); } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store, bytes32 _tableId) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "value"; } - /** Set the table's metadata */ - function setMetadata(bytes32 _tableId) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register(bytes32 _tableId) internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store, bytes32 _tableId) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store, bytes32 _tableId) internal { + _store.registerTable(_tableId, 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, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -73,7 +66,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, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -81,14 +74,14 @@ library Bool { function set(bytes32 _tableId, bool value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getValueSchema()); } /** 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)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getValueSchema()); } /** Tightly pack full data using this table's schema */ @@ -107,14 +100,14 @@ library Bool { function deleteRecord(bytes32 _tableId) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* 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, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/templates/phaser/packages/contracts/src/codegen/tables/Counter.sol b/templates/phaser/packages/contracts/src/codegen/tables/Counter.sol index 703fec8f7b..23789be1ec 100644 --- a/templates/phaser/packages/contracts/src/codegen/tables/Counter.sol +++ b/templates/phaser/packages/contracts/src/codegen/tables/Counter.sol @@ -21,54 +21,47 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Count bytes32 constant CounterTableId = _tableId; library Counter { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.UINT32; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](0); return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "value"; - return ("Counter", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](1); + _schema[0] = SchemaType.UINT32; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](0); } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "value"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, 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, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (uint32(Bytes.slice4(_blob, 0))); } @@ -76,7 +69,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, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (uint32(Bytes.slice4(_blob, 0))); } @@ -84,14 +77,14 @@ library Counter { function set(uint32 value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getValueSchema()); } /** 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)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getValueSchema()); } /** Tightly pack full data using this table's schema */ @@ -110,13 +103,13 @@ library Counter { function deleteRecord() internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* 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, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/templates/react/packages/contracts/src/codegen/tables/Counter.sol b/templates/react/packages/contracts/src/codegen/tables/Counter.sol index 703fec8f7b..23789be1ec 100644 --- a/templates/react/packages/contracts/src/codegen/tables/Counter.sol +++ b/templates/react/packages/contracts/src/codegen/tables/Counter.sol @@ -21,54 +21,47 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Count bytes32 constant CounterTableId = _tableId; library Counter { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.UINT32; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](0); return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "value"; - return ("Counter", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](1); + _schema[0] = SchemaType.UINT32; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](0); } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "value"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, 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, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (uint32(Bytes.slice4(_blob, 0))); } @@ -76,7 +69,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, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (uint32(Bytes.slice4(_blob, 0))); } @@ -84,14 +77,14 @@ library Counter { function set(uint32 value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getValueSchema()); } /** 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)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getValueSchema()); } /** Tightly pack full data using this table's schema */ @@ -110,13 +103,13 @@ library Counter { function deleteRecord() internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* 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, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/templates/threejs/packages/contracts/src/codegen/tables/Position.sol b/templates/threejs/packages/contracts/src/codegen/tables/Position.sol index 1571a36216..b2cc2fb59c 100644 --- a/templates/threejs/packages/contracts/src/codegen/tables/Position.sol +++ b/templates/threejs/packages/contracts/src/codegen/tables/Position.sol @@ -27,16 +27,7 @@ struct PositionData { } library Position { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](3); - _schema[0] = SchemaType.INT32; - _schema[1] = SchemaType.INT32; - _schema[2] = SchemaType.INT32; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); _schema[0] = SchemaType.BYTES32; @@ -44,35 +35,38 @@ library Position { return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](3); - _fieldNames[0] = "x"; - _fieldNames[1] = "y"; - _fieldNames[2] = "z"; - return ("Position", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](3); + _schema[0] = SchemaType.INT32; + _schema[1] = SchemaType.INT32; + _schema[2] = SchemaType.INT32; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "key"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](3); + fieldNames[0] = "x"; + fieldNames[1] = "y"; + fieldNames[2] = "z"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get x */ @@ -80,7 +74,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -89,7 +83,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -98,7 +92,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), getValueSchema()); } /** Set x (using the specified store) */ @@ -106,7 +100,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), getValueSchema()); } /** Get y */ @@ -114,7 +108,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getValueSchema()); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -123,7 +117,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getValueSchema()); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -132,7 +126,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), getValueSchema()); } /** Set y (using the specified store) */ @@ -140,7 +134,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), getSchema()); + _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), getValueSchema()); } /** Get z */ @@ -148,7 +142,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getValueSchema()); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -157,7 +151,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getValueSchema()); return (int32(uint32(Bytes.slice4(_blob, 0)))); } @@ -166,7 +160,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 2, abi.encodePacked((z)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 2, abi.encodePacked((z)), getValueSchema()); } /** Set z (using the specified store) */ @@ -174,7 +168,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 2, abi.encodePacked((z)), getSchema()); + _store.setField(_tableId, _keyTuple, 2, abi.encodePacked((z)), getValueSchema()); } /** Get the full data */ @@ -182,7 +176,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -191,7 +185,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -202,7 +196,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setRecord(_tableId, _keyTuple, _data, getSchema()); + StoreSwitch.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using individual values (using the specified store) */ @@ -212,7 +206,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setRecord(_tableId, _keyTuple, _data, getSchema()); + _store.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using the data struct */ @@ -252,7 +246,7 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -260,6 +254,6 @@ library Position { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/templates/vanilla/packages/contracts/src/codegen/tables/Counter.sol b/templates/vanilla/packages/contracts/src/codegen/tables/Counter.sol index 703fec8f7b..23789be1ec 100644 --- a/templates/vanilla/packages/contracts/src/codegen/tables/Counter.sol +++ b/templates/vanilla/packages/contracts/src/codegen/tables/Counter.sol @@ -21,54 +21,47 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Count bytes32 constant CounterTableId = _tableId; library Counter { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.UINT32; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](0); return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "value"; - return ("Counter", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](1); + _schema[0] = SchemaType.UINT32; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](0); } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "value"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, 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, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (uint32(Bytes.slice4(_blob, 0))); } @@ -76,7 +69,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, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (uint32(Bytes.slice4(_blob, 0))); } @@ -84,14 +77,14 @@ library Counter { function set(uint32 value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getValueSchema()); } /** 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)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getValueSchema()); } /** Tightly pack full data using this table's schema */ @@ -110,13 +103,13 @@ library Counter { function deleteRecord() internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* 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, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } }