From 22ee4470047e4611a3cae62e9d0af4713aa1e612 Mon Sep 17 00:00:00 2001 From: alvarius Date: Mon, 25 Sep 2023 00:09:06 +0100 Subject: [PATCH] feat(store,world): use user-types for `ResourceId`, `FieldLayout` and `Schema` in table libraries (#1586) --- .changeset/seven-mangos-roll.md | 7 + .../contracts/test/ChatNamespaced.t.sol | 2 +- packages/store-sync/src/common.ts | 9 +- packages/store-sync/src/logToTable.ts | 21 +- packages/store/gas-report.json | 24 +- packages/store/mud.config.ts | 33 +- packages/store/src/Hook.sol | 4 +- packages/store/src/IStore.sol | 4 +- packages/store/src/StoreCore.sol | 37 +- packages/store/src/codegen/index.sol | 2 +- packages/store/src/codegen/tables/Hooks.sol | 328 ++++++------ .../store/src/codegen/tables/ResourceIds.sol | 67 +-- .../store/src/codegen/tables/StoreHooks.sol | 301 +++++------ packages/store/src/codegen/tables/Tables.sol | 377 +++++++------- packages/store/test/MirrorSubscriber.sol | 8 +- packages/store/test/StoreCore.t.sol | 40 +- packages/store/test/StoreCoreDynamic.t.sol | 71 +-- packages/store/test/StoreCoreGas.t.sol | 4 +- packages/store/test/tables/StoreHooks.t.sol | 11 +- .../test/tables/StoreHooksColdLoad.t.sol | 15 +- packages/store/ts/config/storeConfig.ts | 20 + packages/world/gas-report.json | 24 +- packages/world/mud.config.ts | 25 +- packages/world/src/AccessControl.sol | 8 +- packages/world/src/Delegation.sol | 28 +- packages/world/src/SystemCall.sol | 8 +- packages/world/src/Utils.sol | 2 +- packages/world/src/World.sol | 24 +- .../world/src/modules/core/CoreModule.sol | 6 +- .../AccessManagementSystem.sol | 10 +- .../implementations/BalanceTransferSystem.sol | 10 +- .../StoreRegistrationSystem.sol | 4 +- .../WorldRegistrationSystem.sol | 62 ++- .../src/modules/core/tables/Balances.sol | 67 +-- .../modules/core/tables/FunctionSelectors.sol | 49 +- .../src/modules/core/tables/SystemHooks.sol | 187 +++---- .../modules/core/tables/SystemRegistry.sol | 55 +- .../world/src/modules/core/tables/Systems.sol | 91 ++-- .../modules/keysintable/KeysInTableHook.sol | 56 +- .../modules/keysintable/KeysInTableModule.sol | 2 +- .../modules/keysintable/getKeysInTable.sol | 24 +- .../world/src/modules/keysintable/hasKey.sol | 4 +- .../keysintable/tables/KeysInTable.sol | 477 +++++++++--------- .../keysintable/tables/UsedKeysIndex.sol | 97 ++-- .../keyswithvalue/KeysWithValueHook.sol | 8 +- .../CallboundDelegationControl.sol | 8 +- .../src/modules/std-delegations/constants.sol | 3 +- .../tables/CallboundDelegations.sol | 67 +-- .../uniqueentity/UniqueEntityModule.sol | 2 +- packages/world/src/tables/Delegations.sol | 91 +++- packages/world/src/tables/NamespaceOwner.sol | 67 +-- packages/world/src/tables/ResourceAccess.sol | 67 +-- packages/world/test/AccessControl.t.sol | 31 +- packages/world/test/Factories.t.sol | 2 +- packages/world/test/World.t.sol | 128 ++--- packages/world/test/WorldBalance.t.sol | 96 ++-- packages/world/test/WorldDynamicUpdate.t.sol | 2 - 57 files changed, 1709 insertions(+), 1568 deletions(-) create mode 100644 .changeset/seven-mangos-roll.md diff --git a/.changeset/seven-mangos-roll.md b/.changeset/seven-mangos-roll.md new file mode 100644 index 0000000000..dad7550c9e --- /dev/null +++ b/.changeset/seven-mangos-roll.md @@ -0,0 +1,7 @@ +--- +"@latticexyz/store-sync": patch +"@latticexyz/store": patch +"@latticexyz/world": patch +--- + +All `Store` and `World` tables now use the appropriate user-types for `ResourceId`, `FieldLayout` and `Schema` to avoid manual `wrap`/`unwrap`. diff --git a/examples/minimal/packages/contracts/test/ChatNamespaced.t.sol b/examples/minimal/packages/contracts/test/ChatNamespaced.t.sol index b69f399523..d22d8dd57c 100644 --- a/examples/minimal/packages/contracts/test/ChatNamespaced.t.sol +++ b/examples/minimal/packages/contracts/test/ChatNamespaced.t.sol @@ -19,7 +19,7 @@ contract ChatNamespacedTest is MudTest { MessageTableTableId, keyTuple, new bytes(0), - MessageTable.encodeLengths(value).unwrap(), + MessageTable.encodeLengths(value), MessageTable.encodeDynamic(value) ); IChatNamespacedSystem(worldAddress).namespace_ChatNamespaced_sendMessage(value); diff --git a/packages/store-sync/src/common.ts b/packages/store-sync/src/common.ts index 41caa5d2f9..56211edb3d 100644 --- a/packages/store-sync/src/common.ts +++ b/packages/store-sync/src/common.ts @@ -1,5 +1,5 @@ import { Address, Block, Hex, Log, PublicClient } from "viem"; -import { StoreConfig, StoreEventsAbiItem, StoreEventsAbi } from "@latticexyz/store"; +import { StoreConfig, StoreEventsAbiItem, StoreEventsAbi, resolveUserTypes } from "@latticexyz/store"; import storeConfig from "@latticexyz/store/mud.config"; import { Observable } from "rxjs"; import { resourceIdToHex } from "@latticexyz/common"; @@ -76,7 +76,12 @@ export type StorageAdapterBlock = { blockNumber: BlockLogs["blockNumber"]; logs: export type StorageAdapter = (block: StorageAdapterBlock) => Promise; // 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) -export const schemasTable = storeConfig.tables.Tables; +// TODO: adjust when schemas are automatically resolved +export const schemasTable = { + ...storeConfig.tables.Tables, + valueSchema: resolveUserTypes(storeConfig.tables.Tables.valueSchema, storeConfig.userTypes), +}; + export const schemasTableId = resourceIdToHex({ type: schemasTable.offchainOnly ? "offchainTable" : "table", namespace: storeConfig.namespace, diff --git a/packages/store-sync/src/logToTable.ts b/packages/store-sync/src/logToTable.ts index eae0ae03b0..5dcdfd29e6 100644 --- a/packages/store-sync/src/logToTable.ts +++ b/packages/store-sync/src/logToTable.ts @@ -1,5 +1,5 @@ -import { hexToSchema, decodeValue } from "@latticexyz/protocol-parser"; -import { concatHex, decodeAbiParameters, parseAbiParameters } from "viem"; +import { hexToSchema, decodeValue, ValueSchema } from "@latticexyz/protocol-parser"; +import { Hex, concatHex, decodeAbiParameters, parseAbiParameters } from "viem"; import { StorageAdapterLog, Table, schemasTable } from "./common"; import { hexToResourceId } from "@latticexyz/common"; @@ -14,15 +14,22 @@ export function logToTable(log: StorageAdapterLog & { eventName: "Store_SetRecor const table = hexToResourceId(tableId); const value = decodeValue( - schemasTable.valueSchema, + // TODO: remove cast when we have strong types for user types + schemasTable.valueSchema as ValueSchema, concatHex([log.args.staticData, log.args.encodedLengths, log.args.dynamicData]) ); - const keySchema = hexToSchema(value.keySchema); - const valueSchema = hexToSchema(value.valueSchema); + // TODO: remove cast when we have strong types for user types + const keySchema = hexToSchema(value.keySchema as Hex); - const keyNames = decodeAbiParameters(parseAbiParameters("string[]"), value.abiEncodedKeyNames)[0]; - const fieldNames = decodeAbiParameters(parseAbiParameters("string[]"), value.abiEncodedFieldNames)[0]; + // TODO: remove cast when we have strong types for user types + const valueSchema = hexToSchema(value.valueSchema as Hex); + + // TODO: remove cast when we have strong types for user types + const keyNames = decodeAbiParameters(parseAbiParameters("string[]"), value.abiEncodedKeyNames as Hex)[0]; + + // TODO: remove cast when we have strong types for user types + const fieldNames = decodeAbiParameters(parseAbiParameters("string[]"), value.abiEncodedFieldNames as Hex)[0]; const valueAbiTypes = [...valueSchema.staticFields, ...valueSchema.dynamicFields]; diff --git a/packages/store/gas-report.json b/packages/store/gas-report.json index 8d0a243835..abc84a43d3 100644 --- a/packages/store/gas-report.json +++ b/packages/store/gas-report.json @@ -603,73 +603,73 @@ "file": "test/StoreCoreDynamic.t.sol", "test": "testGetDynamicFieldSlice", "name": "get field slice (cold, 1 slot)", - "gasUsed": 10240 + "gasUsed": 5609 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testGetDynamicFieldSlice", "name": "get field slice (warm, 1 slot)", - "gasUsed": 2308 + "gasUsed": 1677 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testGetDynamicFieldSlice", "name": "get field slice (semi-cold, 1 slot)", - "gasUsed": 4313 + "gasUsed": 3680 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testGetDynamicFieldSlice", "name": "get field slice (warm, 2 slots)", - "gasUsed": 4539 + "gasUsed": 3907 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testGetSecondFieldLength", "name": "get field length (cold, 1 slot)", - "gasUsed": 7795 + "gasUsed": 3163 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testGetSecondFieldLength", "name": "get field length (warm, 1 slot)", - "gasUsed": 1790 + "gasUsed": 1160 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testGetThirdFieldLength", "name": "get field length (warm due to , 2 slots)", - "gasUsed": 7794 + "gasUsed": 3163 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testGetThirdFieldLength", "name": "get field length (warm, 2 slots)", - "gasUsed": 1790 + "gasUsed": 1160 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromSecondField", "name": "pop from field (cold, 1 slot, 1 uint32 item)", - "gasUsed": 18728 + "gasUsed": 18097 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromSecondField", "name": "pop from field (warm, 1 slot, 1 uint32 item)", - "gasUsed": 12736 + "gasUsed": 12104 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromThirdField", "name": "pop from field (cold, 2 slots, 10 uint32 items)", - "gasUsed": 16496 + "gasUsed": 15865 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromThirdField", "name": "pop from field (warm, 2 slots, 10 uint32 items)", - "gasUsed": 12504 + "gasUsed": 11872 }, { "file": "test/StoreCoreGas.t.sol", diff --git a/packages/store/mud.config.ts b/packages/store/mud.config.ts index e871948207..f825dd5c8f 100644 --- a/packages/store/mud.config.ts +++ b/packages/store/mud.config.ts @@ -6,24 +6,35 @@ export default mudConfig({ enums: { ExampleEnum: ["None", "First", "Second", "Third"], }, + userTypes: { + ResourceId: { filePath: "./src/ResourceId.sol", internalType: "bytes32" }, + FieldLayout: { filePath: "./src/FieldLayout.sol", internalType: "bytes32" }, + Schema: { filePath: "./src/Schema.sol", internalType: "bytes32" }, + }, tables: { - StoreHooks: "bytes21[]", - Callbacks: "bytes24[]", + StoreHooks: { + keySchema: { + tableId: "ResourceId", + }, + valueSchema: { + hooks: "bytes21[]", + }, + }, Tables: { keySchema: { - tableId: "bytes32", + tableId: "ResourceId", }, valueSchema: { - fieldLayout: "bytes32", - keySchema: "bytes32", - valueSchema: "bytes32", + fieldLayout: "FieldLayout", + keySchema: "Schema", + valueSchema: "Schema", abiEncodedKeyNames: "bytes", abiEncodedFieldNames: "bytes", }, }, ResourceIds: { keySchema: { - resourceId: "bytes32", + resourceId: "ResourceId", }, valueSchema: { exists: "bool", @@ -31,10 +42,16 @@ export default mudConfig({ }, // The Hooks table is a generic table used by the `filterFromList` util in `Hook.sol` Hooks: { - valueSchema: "bytes21[]", + keySchema: { + resourceId: "ResourceId", + }, + valueSchema: { + hooks: "bytes21[]", + }, tableIdArgument: true, }, // TODO: move these test tables to a separate mud config + Callbacks: "bytes24[]", Mixed: { valueSchema: { u32: "uint32", diff --git a/packages/store/src/Hook.sol b/packages/store/src/Hook.sol index cbe73fe03b..34e329bf6b 100644 --- a/packages/store/src/Hook.sol +++ b/packages/store/src/Hook.sol @@ -26,7 +26,7 @@ library HookLib { ResourceId tableWithHooks, address hookAddressToRemove ) internal { - bytes21[] memory currentHooks = Hooks._get(hookTableId, ResourceId.unwrap(tableWithHooks)); + bytes21[] memory currentHooks = Hooks._get(hookTableId, tableWithHooks); // Initialize the new hooks array with the same length because we don't know if the hook is registered yet bytes21[] memory newHooks = new bytes21[](currentHooks.length); @@ -49,7 +49,7 @@ library HookLib { } // Set the new hooks table - Hooks._set(hookTableId, ResourceId.unwrap(tableWithHooks), newHooks); + Hooks._set(hookTableId, tableWithHooks, newHooks); } } diff --git a/packages/store/src/IStore.sol b/packages/store/src/IStore.sol index 192968fb27..c9902e2e06 100644 --- a/packages/store/src/IStore.sol +++ b/packages/store/src/IStore.sol @@ -123,7 +123,7 @@ interface IStoreWrite { ResourceId indexed tableId, bytes32[] keyTuple, bytes staticData, - bytes32 encodedLengths, + PackedCounter encodedLengths, bytes dynamicData ); event Store_SpliceStaticData(ResourceId indexed tableId, bytes32[] keyTuple, uint48 start, bytes data); @@ -133,7 +133,7 @@ interface IStoreWrite { uint48 start, uint40 deleteCount, bytes data, - bytes32 encodedLengths + PackedCounter encodedLengths ); event Store_DeleteRecord(ResourceId indexed tableId, bytes32[] keyTuple); diff --git a/packages/store/src/StoreCore.sol b/packages/store/src/StoreCore.sol index 8679adb77d..4acfcd931a 100644 --- a/packages/store/src/StoreCore.sol +++ b/packages/store/src/StoreCore.sol @@ -32,7 +32,7 @@ library StoreCore { ResourceId indexed tableId, bytes32[] keyTuple, bytes staticData, - bytes32 encodedLengths, + PackedCounter encodedLengths, bytes dynamicData ); event Store_SpliceStaticData(ResourceId indexed tableId, bytes32[] keyTuple, uint48 start, bytes data); @@ -42,7 +42,7 @@ library StoreCore { uint48 start, uint40 deleteCount, bytes data, - bytes32 encodedLengths + PackedCounter encodedLengths ); event Store_DeleteRecord(ResourceId indexed tableId, bytes32[] keyTuple); @@ -99,9 +99,9 @@ library StoreCore { * Get the key schema for the given tableId */ function getKeySchema(ResourceId tableId) internal view returns (Schema keySchema) { - keySchema = Schema.wrap(Tables._getKeySchema(ResourceId.unwrap(tableId))); + keySchema = Tables._getKeySchema(tableId); // key schemas can be empty for singleton tables, so we can't depend on key schema for table check - if (!ResourceIds._getExists(ResourceId.unwrap(tableId))) { + if (!ResourceIds._getExists(tableId)) { revert IStoreErrors.Store_TableNotFound(tableId, string(abi.encodePacked(tableId))); } } @@ -110,7 +110,7 @@ library StoreCore { * Get the schema for the given tableId */ function getValueSchema(ResourceId tableId) internal view returns (Schema valueSchema) { - valueSchema = Schema.wrap(Tables._getValueSchema(ResourceId.unwrap(tableId))); + valueSchema = Tables._getValueSchema(tableId); if (valueSchema.isEmpty()) { revert IStoreErrors.Store_TableNotFound(tableId, string(abi.encodePacked(tableId))); } @@ -155,22 +155,15 @@ library StoreCore { } // Verify there is no resource with this ID yet - if (ResourceIds._getExists(ResourceId.unwrap(tableId))) { + if (ResourceIds._getExists(tableId)) { revert IStoreErrors.Store_TableAlreadyExists(tableId, string(abi.encodePacked(tableId))); } // Register the table metadata - Tables._set( - ResourceId.unwrap(tableId), - FieldLayout.unwrap(fieldLayout), - Schema.unwrap(keySchema), - Schema.unwrap(valueSchema), - abi.encode(keyNames), - abi.encode(fieldNames) - ); + Tables._set(tableId, fieldLayout, keySchema, valueSchema, abi.encode(keyNames), abi.encode(fieldNames)); // Register the table ID - ResourceIds._setExists(ResourceId.unwrap(tableId), true); + ResourceIds._setExists(tableId, true); } /************************************************************************ @@ -188,7 +181,7 @@ library StoreCore { revert IStoreErrors.Store_InvalidResourceType(RESOURCE_TABLE, tableId, string(abi.encodePacked(tableId))); } - StoreHooks.push(ResourceId.unwrap(tableId), Hook.unwrap(HookLib.encode(address(hookAddress), enabledHooksBitmap))); + StoreHooks.push(tableId, Hook.unwrap(HookLib.encode(address(hookAddress), enabledHooksBitmap))); } /** @@ -231,7 +224,7 @@ library StoreCore { FieldLayout fieldLayout ) internal { // Emit event to notify indexers - emit Store_SetRecord(tableId, keyTuple, staticData, encodedLengths.unwrap(), dynamicData); + emit Store_SetRecord(tableId, keyTuple, staticData, encodedLengths, dynamicData); // Early return if the table is an offchain table if (tableId.getType() != RESOURCE_TABLE) { @@ -239,7 +232,7 @@ library StoreCore { } // Call onBeforeSetRecord hooks (before actually modifying the state, so observers have access to the previous state if needed) - bytes21[] memory hooks = StoreHooks._get(ResourceId.unwrap(tableId)); + bytes21[] memory hooks = StoreHooks._get(tableId); for (uint256 i; i < hooks.length; i++) { Hook hook = Hook.wrap(hooks[i]); if (hook.isEnabled(BEFORE_SET_RECORD)) { @@ -320,7 +313,7 @@ library StoreCore { } // Call onBeforeSpliceStaticData hooks (before actually modifying the state, so observers have access to the previous state if needed) - bytes21[] memory hooks = StoreHooks._get(ResourceId.unwrap(tableId)); + bytes21[] memory hooks = StoreHooks._get(tableId); for (uint256 i; i < hooks.length; i++) { Hook hook = Hook.wrap(hooks[i]); if (hook.isEnabled(BEFORE_SPLICE_STATIC_DATA)) { @@ -451,7 +444,7 @@ library StoreCore { } // Call onBeforeDeleteRecord hooks (before actually modifying the state, so observers have access to the previous state if needed) - bytes21[] memory hooks = StoreHooks._get(ResourceId.unwrap(tableId)); + bytes21[] memory hooks = StoreHooks._get(tableId); for (uint256 i; i < hooks.length; i++) { Hook hook = Hook.wrap(hooks[i]); if (hook.isEnabled(BEFORE_DELETE_RECORD)) { @@ -771,11 +764,11 @@ library StoreCoreInternal { start: uint48(start), deleteCount: deleteCount, data: data, - encodedLengths: updatedEncodedLengths.unwrap() + encodedLengths: updatedEncodedLengths }); // Call onBeforeSpliceDynamicData hooks (before actually modifying the state, so observers have access to the previous state if needed) - bytes21[] memory hooks = StoreHooks._get(ResourceId.unwrap(tableId)); + bytes21[] memory hooks = StoreHooks._get(tableId); for (uint256 i; i < hooks.length; i++) { Hook hook = Hook.wrap(hooks[i]); if (hook.isEnabled(BEFORE_SPLICE_DYNAMIC_DATA)) { diff --git a/packages/store/src/codegen/index.sol b/packages/store/src/codegen/index.sol index 62f886fe46..6c2596d938 100644 --- a/packages/store/src/codegen/index.sol +++ b/packages/store/src/codegen/index.sol @@ -4,10 +4,10 @@ pragma solidity >=0.8.21; /* Autogenerated file. Do not edit manually. */ import { StoreHooks, StoreHooksTableId } from "./tables/StoreHooks.sol"; -import { Callbacks, CallbacksTableId } from "./tables/Callbacks.sol"; import { Tables, TablesData, TablesTableId } from "./tables/Tables.sol"; import { ResourceIds, ResourceIdsTableId } from "./tables/ResourceIds.sol"; import { Hooks } from "./tables/Hooks.sol"; +import { Callbacks, CallbacksTableId } from "./tables/Callbacks.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/Hooks.sol b/packages/store/src/codegen/tables/Hooks.sol index 3210316a6b..38c3365681 100644 --- a/packages/store/src/codegen/tables/Hooks.sol +++ b/packages/store/src/codegen/tables/Hooks.sol @@ -20,6 +20,9 @@ import { PackedCounter, PackedCounterLib } from "../../PackedCounter.sol"; import { ResourceId } from "../../ResourceId.sol"; import { RESOURCE_TABLE, RESOURCE_OFFCHAIN_TABLE } from "../../storeResourceTypes.sol"; +// Import user types +import { ResourceId } from "./../../ResourceId.sol"; + FieldLayout constant _fieldLayout = FieldLayout.wrap( 0x0000000100000000000000000000000000000000000000000000000000000000 ); @@ -49,13 +52,13 @@ library Hooks { /** Get the table's key names */ function getKeyNames() internal pure returns (string[] memory keyNames) { keyNames = new string[](1); - keyNames[0] = "key"; + keyNames[0] = "resourceId"; } /** Get the table's field names */ function getFieldNames() internal pure returns (string[] memory fieldNames) { fieldNames = new string[](1); - fieldNames[0] = "value"; + fieldNames[0] = "hooks"; } /** Register the table with its config */ @@ -73,112 +76,120 @@ library Hooks { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Get value */ - function getValue(ResourceId _tableId, bytes32 key) internal view returns (bytes21[] memory value) { + /** Get hooks */ + function getHooks(ResourceId _tableId, ResourceId resourceId) internal view returns (bytes21[] memory hooks) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); bytes memory _blob = StoreSwitch.getDynamicField(_tableId, _keyTuple, 0); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } - /** Get value */ - function _getValue(ResourceId _tableId, bytes32 key) internal view returns (bytes21[] memory value) { + /** Get hooks */ + function _getHooks(ResourceId _tableId, ResourceId resourceId) internal view returns (bytes21[] memory hooks) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } - /** Get value (using the specified store) */ - function getValue(IStore _store, ResourceId _tableId, bytes32 key) internal view returns (bytes21[] memory value) { + /** Get hooks (using the specified store) */ + function getHooks( + IStore _store, + ResourceId _tableId, + ResourceId resourceId + ) internal view returns (bytes21[] memory hooks) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); bytes memory _blob = _store.getDynamicField(_tableId, _keyTuple, 0); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } - /** Get value */ - function get(ResourceId _tableId, bytes32 key) internal view returns (bytes21[] memory value) { + /** Get hooks */ + function get(ResourceId _tableId, ResourceId resourceId) internal view returns (bytes21[] memory hooks) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); bytes memory _blob = StoreSwitch.getDynamicField(_tableId, _keyTuple, 0); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } - /** Get value */ - function _get(ResourceId _tableId, bytes32 key) internal view returns (bytes21[] memory value) { + /** Get hooks */ + function _get(ResourceId _tableId, ResourceId resourceId) internal view returns (bytes21[] memory hooks) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } - /** Get value (using the specified store) */ - function get(IStore _store, ResourceId _tableId, bytes32 key) internal view returns (bytes21[] memory value) { + /** Get hooks (using the specified store) */ + function get( + IStore _store, + ResourceId _tableId, + ResourceId resourceId + ) internal view returns (bytes21[] memory hooks) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); bytes memory _blob = _store.getDynamicField(_tableId, _keyTuple, 0); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } - /** Set value */ - function setValue(ResourceId _tableId, bytes32 key, bytes21[] memory value) internal { + /** Set hooks */ + function setHooks(ResourceId _tableId, ResourceId resourceId, bytes21[] memory hooks) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); - StoreSwitch.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((value))); + StoreSwitch.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((hooks))); } - /** Set value */ - function _setValue(ResourceId _tableId, bytes32 key, bytes21[] memory value) internal { + /** Set hooks */ + function _setHooks(ResourceId _tableId, ResourceId resourceId, bytes21[] memory hooks) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); - StoreCore.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((value))); + StoreCore.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((hooks))); } - /** Set value (using the specified store) */ - function setValue(IStore _store, ResourceId _tableId, bytes32 key, bytes21[] memory value) internal { + /** Set hooks (using the specified store) */ + function setHooks(IStore _store, ResourceId _tableId, ResourceId resourceId, bytes21[] memory hooks) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); - _store.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((value))); + _store.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((hooks))); } - /** Set value */ - function set(ResourceId _tableId, bytes32 key, bytes21[] memory value) internal { + /** Set hooks */ + function set(ResourceId _tableId, ResourceId resourceId, bytes21[] memory hooks) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); - StoreSwitch.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((value))); + StoreSwitch.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((hooks))); } - /** Set value */ - function _set(ResourceId _tableId, bytes32 key, bytes21[] memory value) internal { + /** Set hooks */ + function _set(ResourceId _tableId, ResourceId resourceId, bytes21[] memory hooks) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); - StoreCore.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((value))); + StoreCore.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((hooks))); } - /** Set value (using the specified store) */ - function set(IStore _store, ResourceId _tableId, bytes32 key, bytes21[] memory value) internal { + /** Set hooks (using the specified store) */ + function set(IStore _store, ResourceId _tableId, ResourceId resourceId, bytes21[] memory hooks) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); - _store.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((value))); + _store.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((hooks))); } - /** Get the length of value */ - function lengthValue(ResourceId _tableId, bytes32 key) internal view returns (uint256) { + /** Get the length of hooks */ + function lengthHooks(ResourceId _tableId, ResourceId resourceId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -186,10 +197,10 @@ library Hooks { } } - /** Get the length of value */ - function _lengthValue(ResourceId _tableId, bytes32 key) internal view returns (uint256) { + /** Get the length of hooks */ + function _lengthHooks(ResourceId _tableId, ResourceId resourceId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -197,10 +208,10 @@ library Hooks { } } - /** Get the length of value (using the specified store) */ - function lengthValue(IStore _store, ResourceId _tableId, bytes32 key) internal view returns (uint256) { + /** Get the length of hooks (using the specified store) */ + function lengthHooks(IStore _store, ResourceId _tableId, ResourceId resourceId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); uint256 _byteLength = _store.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -208,10 +219,10 @@ library Hooks { } } - /** Get the length of value */ - function length(ResourceId _tableId, bytes32 key) internal view returns (uint256) { + /** Get the length of hooks */ + function length(ResourceId _tableId, ResourceId resourceId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -219,10 +230,10 @@ library Hooks { } } - /** Get the length of value */ - function _length(ResourceId _tableId, bytes32 key) internal view returns (uint256) { + /** Get the length of hooks */ + function _length(ResourceId _tableId, ResourceId resourceId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -230,10 +241,10 @@ library Hooks { } } - /** Get the length of value (using the specified store) */ - function length(IStore _store, ResourceId _tableId, bytes32 key) internal view returns (uint256) { + /** Get the length of hooks (using the specified store) */ + function length(IStore _store, ResourceId _tableId, ResourceId resourceId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); uint256 _byteLength = _store.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -242,12 +253,12 @@ library Hooks { } /** - * Get an item of value + * Get an item of hooks * (unchecked, returns invalid data if index overflows) */ - function getItemValue(ResourceId _tableId, bytes32 key, uint256 _index) internal view returns (bytes21) { + function getItemHooks(ResourceId _tableId, ResourceId resourceId, uint256 _index) internal view returns (bytes21) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); unchecked { bytes memory _blob = StoreSwitch.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 21, (_index + 1) * 21); @@ -256,12 +267,12 @@ library Hooks { } /** - * Get an item of value + * Get an item of hooks * (unchecked, returns invalid data if index overflows) */ - function _getItemValue(ResourceId _tableId, bytes32 key, uint256 _index) internal view returns (bytes21) { + function _getItemHooks(ResourceId _tableId, ResourceId resourceId, uint256 _index) internal view returns (bytes21) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); unchecked { bytes memory _blob = StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 21, (_index + 1) * 21); @@ -270,17 +281,17 @@ library Hooks { } /** - * Get an item of value (using the specified store) + * Get an item of hooks (using the specified store) * (unchecked, returns invalid data if index overflows) */ - function getItemValue( + function getItemHooks( IStore _store, ResourceId _tableId, - bytes32 key, + ResourceId resourceId, uint256 _index ) internal view returns (bytes21) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); unchecked { bytes memory _blob = _store.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 21, (_index + 1) * 21); @@ -289,12 +300,12 @@ library Hooks { } /** - * Get an item of value + * Get an item of hooks * (unchecked, returns invalid data if index overflows) */ - function getItem(ResourceId _tableId, bytes32 key, uint256 _index) internal view returns (bytes21) { + function getItem(ResourceId _tableId, ResourceId resourceId, uint256 _index) internal view returns (bytes21) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); unchecked { bytes memory _blob = StoreSwitch.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 21, (_index + 1) * 21); @@ -303,12 +314,12 @@ library Hooks { } /** - * Get an item of value + * Get an item of hooks * (unchecked, returns invalid data if index overflows) */ - function _getItem(ResourceId _tableId, bytes32 key, uint256 _index) internal view returns (bytes21) { + function _getItem(ResourceId _tableId, ResourceId resourceId, uint256 _index) internal view returns (bytes21) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); unchecked { bytes memory _blob = StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 21, (_index + 1) * 21); @@ -317,12 +328,17 @@ library Hooks { } /** - * Get an item of value (using the specified store) + * Get an item of hooks (using the specified store) * (unchecked, returns invalid data if index overflows) */ - function getItem(IStore _store, ResourceId _tableId, bytes32 key, uint256 _index) internal view returns (bytes21) { + function getItem( + IStore _store, + ResourceId _tableId, + ResourceId resourceId, + uint256 _index + ) internal view returns (bytes21) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); unchecked { bytes memory _blob = _store.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 21, (_index + 1) * 21); @@ -330,109 +346,109 @@ library Hooks { } } - /** Push an element to value */ - function pushValue(ResourceId _tableId, bytes32 key, bytes21 _element) internal { + /** Push an element to hooks */ + function pushHooks(ResourceId _tableId, ResourceId resourceId, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); } - /** Push an element to value */ - function _pushValue(ResourceId _tableId, bytes32 key, bytes21 _element) internal { + /** Push an element to hooks */ + function _pushHooks(ResourceId _tableId, ResourceId resourceId, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); StoreCore.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); } - /** Push an element to value (using the specified store) */ - function pushValue(IStore _store, ResourceId _tableId, bytes32 key, bytes21 _element) internal { + /** Push an element to hooks (using the specified store) */ + function pushHooks(IStore _store, ResourceId _tableId, ResourceId resourceId, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); _store.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); } - /** Push an element to value */ - function push(ResourceId _tableId, bytes32 key, bytes21 _element) internal { + /** Push an element to hooks */ + function push(ResourceId _tableId, ResourceId resourceId, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); } - /** Push an element to value */ - function _push(ResourceId _tableId, bytes32 key, bytes21 _element) internal { + /** Push an element to hooks */ + function _push(ResourceId _tableId, ResourceId resourceId, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); StoreCore.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); } - /** Push an element to value (using the specified store) */ - function push(IStore _store, ResourceId _tableId, bytes32 key, bytes21 _element) internal { + /** Push an element to hooks (using the specified store) */ + function push(IStore _store, ResourceId _tableId, ResourceId resourceId, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); _store.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); } - /** Pop an element from value */ - function popValue(ResourceId _tableId, bytes32 key) internal { + /** Pop an element from hooks */ + function popHooks(ResourceId _tableId, ResourceId resourceId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 0, 21); } - /** Pop an element from value */ - function _popValue(ResourceId _tableId, bytes32 key) internal { + /** Pop an element from hooks */ + function _popHooks(ResourceId _tableId, ResourceId resourceId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); StoreCore.popFromDynamicField(_tableId, _keyTuple, 0, 21); } - /** Pop an element from value (using the specified store) */ - function popValue(IStore _store, ResourceId _tableId, bytes32 key) internal { + /** Pop an element from hooks (using the specified store) */ + function popHooks(IStore _store, ResourceId _tableId, ResourceId resourceId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); _store.popFromDynamicField(_tableId, _keyTuple, 0, 21); } - /** Pop an element from value */ - function pop(ResourceId _tableId, bytes32 key) internal { + /** Pop an element from hooks */ + function pop(ResourceId _tableId, ResourceId resourceId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 0, 21); } - /** Pop an element from value */ - function _pop(ResourceId _tableId, bytes32 key) internal { + /** Pop an element from hooks */ + function _pop(ResourceId _tableId, ResourceId resourceId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); StoreCore.popFromDynamicField(_tableId, _keyTuple, 0, 21); } - /** Pop an element from value (using the specified store) */ - function pop(IStore _store, ResourceId _tableId, bytes32 key) internal { + /** Pop an element from hooks (using the specified store) */ + function pop(IStore _store, ResourceId _tableId, ResourceId resourceId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); _store.popFromDynamicField(_tableId, _keyTuple, 0, 21); } /** - * Update an element of value at `_index` + * Update an element of hooks at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function updateValue(ResourceId _tableId, bytes32 key, uint256 _index, bytes21 _element) internal { + function updateHooks(ResourceId _tableId, ResourceId resourceId, uint256 _index, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -441,12 +457,12 @@ library Hooks { } /** - * Update an element of value at `_index` + * Update an element of hooks at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function _updateValue(ResourceId _tableId, bytes32 key, uint256 _index, bytes21 _element) internal { + function _updateHooks(ResourceId _tableId, ResourceId resourceId, uint256 _index, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -455,12 +471,18 @@ library Hooks { } /** - * Update an element of value (using the specified store) at `_index` + * Update an element of hooks (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function updateValue(IStore _store, ResourceId _tableId, bytes32 key, uint256 _index, bytes21 _element) internal { + function updateHooks( + IStore _store, + ResourceId _tableId, + ResourceId resourceId, + uint256 _index, + bytes21 _element + ) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -469,12 +491,12 @@ library Hooks { } /** - * Update an element of value at `_index` + * Update an element of hooks at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function update(ResourceId _tableId, bytes32 key, uint256 _index, bytes21 _element) internal { + function update(ResourceId _tableId, ResourceId resourceId, uint256 _index, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -483,12 +505,12 @@ library Hooks { } /** - * Update an element of value at `_index` + * Update an element of hooks at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function _update(ResourceId _tableId, bytes32 key, uint256 _index, bytes21 _element) internal { + function _update(ResourceId _tableId, ResourceId resourceId, uint256 _index, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -497,12 +519,18 @@ library Hooks { } /** - * Update an element of value (using the specified store) at `_index` + * Update an element of hooks (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function update(IStore _store, ResourceId _tableId, bytes32 key, uint256 _index, bytes21 _element) internal { + function update( + IStore _store, + ResourceId _tableId, + ResourceId resourceId, + uint256 _index, + bytes21 _element + ) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -511,55 +539,55 @@ library Hooks { } /** Delete all data for given keys */ - function deleteRecord(ResourceId _tableId, bytes32 key) internal { + function deleteRecord(ResourceId _tableId, ResourceId resourceId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); StoreSwitch.deleteRecord(_tableId, _keyTuple); } /** Delete all data for given keys */ - function _deleteRecord(ResourceId _tableId, bytes32 key) internal { + function _deleteRecord(ResourceId _tableId, ResourceId resourceId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /** Delete all data for given keys (using the specified store) */ - function deleteRecord(IStore _store, ResourceId _tableId, bytes32 key) internal { + function deleteRecord(IStore _store, ResourceId _tableId, ResourceId resourceId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); _store.deleteRecord(_tableId, _keyTuple); } /** Tightly pack dynamic data using this table's schema */ - function encodeLengths(bytes21[] memory value) internal pure returns (PackedCounter _encodedLengths) { + function encodeLengths(bytes21[] memory hooks) internal pure returns (PackedCounter _encodedLengths) { // Lengths are effectively checked during copy by 2**40 bytes exceeding gas limits unchecked { - _encodedLengths = PackedCounterLib.pack(value.length * 21); + _encodedLengths = PackedCounterLib.pack(hooks.length * 21); } } /** Tightly pack dynamic data using this table's schema */ - function encodeDynamic(bytes21[] memory value) internal pure returns (bytes memory) { - return abi.encodePacked(EncodeArray.encode((value))); + function encodeDynamic(bytes21[] memory hooks) internal pure returns (bytes memory) { + return abi.encodePacked(EncodeArray.encode((hooks))); } /** Tightly pack full data using this table's field layout */ - function encode(bytes21[] memory value) internal pure returns (bytes memory, PackedCounter, bytes memory) { + function encode(bytes21[] memory hooks) internal pure returns (bytes memory, PackedCounter, bytes memory) { bytes memory _staticData; - PackedCounter _encodedLengths = encodeLengths(value); - bytes memory _dynamicData = encodeDynamic(value); + PackedCounter _encodedLengths = encodeLengths(hooks); + bytes memory _dynamicData = encodeDynamic(hooks); return (_staticData, _encodedLengths, _dynamicData); } /** Encode keys as a bytes32 array using this table's field layout */ - function encodeKeyTuple(bytes32 key) internal pure returns (bytes32[] memory) { + function encodeKeyTuple(ResourceId resourceId) internal pure returns (bytes32[] memory) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(resourceId); return _keyTuple; } diff --git a/packages/store/src/codegen/tables/ResourceIds.sol b/packages/store/src/codegen/tables/ResourceIds.sol index 742af60ce9..15a264e5b8 100644 --- a/packages/store/src/codegen/tables/ResourceIds.sol +++ b/packages/store/src/codegen/tables/ResourceIds.sol @@ -20,6 +20,9 @@ import { PackedCounter, PackedCounterLib } from "../../PackedCounter.sol"; import { ResourceId } from "../../ResourceId.sol"; import { RESOURCE_TABLE, RESOURCE_OFFCHAIN_TABLE } from "../../storeResourceTypes.sol"; +// Import user types +import { ResourceId } from "./../../ResourceId.sol"; + ResourceId constant _tableId = ResourceId.wrap( bytes32(abi.encodePacked(RESOURCE_TABLE, bytes14("mudstore"), bytes16("ResourceIds"))) ); @@ -79,127 +82,127 @@ library ResourceIds { } /** Get exists */ - function getExists(bytes32 resourceId) internal view returns (bool exists) { + function getExists(ResourceId resourceId) internal view returns (bool exists) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (_toBool(uint8(bytes1(_blob)))); } /** Get exists */ - function _getExists(bytes32 resourceId) internal view returns (bool exists) { + function _getExists(ResourceId resourceId) internal view returns (bool exists) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (_toBool(uint8(bytes1(_blob)))); } /** Get exists (using the specified store) */ - function getExists(IStore _store, bytes32 resourceId) internal view returns (bool exists) { + function getExists(IStore _store, ResourceId resourceId) internal view returns (bool exists) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (_toBool(uint8(bytes1(_blob)))); } /** Get exists */ - function get(bytes32 resourceId) internal view returns (bool exists) { + function get(ResourceId resourceId) internal view returns (bool exists) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (_toBool(uint8(bytes1(_blob)))); } /** Get exists */ - function _get(bytes32 resourceId) internal view returns (bool exists) { + function _get(ResourceId resourceId) internal view returns (bool exists) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (_toBool(uint8(bytes1(_blob)))); } /** Get exists (using the specified store) */ - function get(IStore _store, bytes32 resourceId) internal view returns (bool exists) { + function get(IStore _store, ResourceId resourceId) internal view returns (bool exists) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (_toBool(uint8(bytes1(_blob)))); } /** Set exists */ - function setExists(bytes32 resourceId, bool exists) internal { + function setExists(ResourceId resourceId, bool exists) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((exists)), _fieldLayout); } /** Set exists */ - function _setExists(bytes32 resourceId, bool exists) internal { + function _setExists(ResourceId resourceId, bool exists) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((exists)), _fieldLayout); } /** Set exists (using the specified store) */ - function setExists(IStore _store, bytes32 resourceId, bool exists) internal { + function setExists(IStore _store, ResourceId resourceId, bool exists) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); _store.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((exists)), _fieldLayout); } /** Set exists */ - function set(bytes32 resourceId, bool exists) internal { + function set(ResourceId resourceId, bool exists) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((exists)), _fieldLayout); } /** Set exists */ - function _set(bytes32 resourceId, bool exists) internal { + function _set(ResourceId resourceId, bool exists) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((exists)), _fieldLayout); } /** Set exists (using the specified store) */ - function set(IStore _store, bytes32 resourceId, bool exists) internal { + function set(IStore _store, ResourceId resourceId, bool exists) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); _store.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((exists)), _fieldLayout); } /** Delete all data for given keys */ - function deleteRecord(bytes32 resourceId) internal { + function deleteRecord(ResourceId resourceId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); StoreSwitch.deleteRecord(_tableId, _keyTuple); } /** Delete all data for given keys */ - function _deleteRecord(bytes32 resourceId) internal { + function _deleteRecord(ResourceId resourceId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /** Delete all data for given keys (using the specified store) */ - function deleteRecord(IStore _store, bytes32 resourceId) internal { + function deleteRecord(IStore _store, ResourceId resourceId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); _store.deleteRecord(_tableId, _keyTuple); } @@ -220,9 +223,9 @@ library ResourceIds { } /** Encode keys as a bytes32 array using this table's field layout */ - function encodeKeyTuple(bytes32 resourceId) internal pure returns (bytes32[] memory) { + function encodeKeyTuple(ResourceId resourceId) internal pure returns (bytes32[] memory) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); return _keyTuple; } diff --git a/packages/store/src/codegen/tables/StoreHooks.sol b/packages/store/src/codegen/tables/StoreHooks.sol index 964bf84104..43c887cf35 100644 --- a/packages/store/src/codegen/tables/StoreHooks.sol +++ b/packages/store/src/codegen/tables/StoreHooks.sol @@ -20,6 +20,9 @@ import { PackedCounter, PackedCounterLib } from "../../PackedCounter.sol"; import { ResourceId } from "../../ResourceId.sol"; import { RESOURCE_TABLE, RESOURCE_OFFCHAIN_TABLE } from "../../storeResourceTypes.sol"; +// Import user types +import { ResourceId } from "./../../ResourceId.sol"; + ResourceId constant _tableId = ResourceId.wrap( bytes32(abi.encodePacked(RESOURCE_TABLE, bytes14("mudstore"), bytes16("StoreHooks"))) ); @@ -54,13 +57,13 @@ library StoreHooks { /** Get the table's key names */ function getKeyNames() internal pure returns (string[] memory keyNames) { keyNames = new string[](1); - keyNames[0] = "key"; + keyNames[0] = "tableId"; } /** Get the table's field names */ function getFieldNames() internal pure returns (string[] memory fieldNames) { fieldNames = new string[](1); - fieldNames[0] = "value"; + fieldNames[0] = "hooks"; } /** Register the table with its config */ @@ -78,112 +81,112 @@ library StoreHooks { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Get value */ - function getValue(bytes32 key) internal view returns (bytes21[] memory value) { + /** Get hooks */ + function getHooks(ResourceId tableId) internal view returns (bytes21[] memory hooks) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); bytes memory _blob = StoreSwitch.getDynamicField(_tableId, _keyTuple, 0); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } - /** Get value */ - function _getValue(bytes32 key) internal view returns (bytes21[] memory value) { + /** Get hooks */ + function _getHooks(ResourceId tableId) internal view returns (bytes21[] memory hooks) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } - /** Get value (using the specified store) */ - function getValue(IStore _store, bytes32 key) internal view returns (bytes21[] memory value) { + /** Get hooks (using the specified store) */ + function getHooks(IStore _store, ResourceId tableId) internal view returns (bytes21[] memory hooks) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); bytes memory _blob = _store.getDynamicField(_tableId, _keyTuple, 0); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } - /** Get value */ - function get(bytes32 key) internal view returns (bytes21[] memory value) { + /** Get hooks */ + function get(ResourceId tableId) internal view returns (bytes21[] memory hooks) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); bytes memory _blob = StoreSwitch.getDynamicField(_tableId, _keyTuple, 0); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } - /** Get value */ - function _get(bytes32 key) internal view returns (bytes21[] memory value) { + /** Get hooks */ + function _get(ResourceId tableId) internal view returns (bytes21[] memory hooks) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } - /** Get value (using the specified store) */ - function get(IStore _store, bytes32 key) internal view returns (bytes21[] memory value) { + /** Get hooks (using the specified store) */ + function get(IStore _store, ResourceId tableId) internal view returns (bytes21[] memory hooks) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); bytes memory _blob = _store.getDynamicField(_tableId, _keyTuple, 0); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } - /** Set value */ - function setValue(bytes32 key, bytes21[] memory value) internal { + /** Set hooks */ + function setHooks(ResourceId tableId, bytes21[] memory hooks) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); - StoreSwitch.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((value))); + StoreSwitch.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((hooks))); } - /** Set value */ - function _setValue(bytes32 key, bytes21[] memory value) internal { + /** Set hooks */ + function _setHooks(ResourceId tableId, bytes21[] memory hooks) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); - StoreCore.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((value))); + StoreCore.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((hooks))); } - /** Set value (using the specified store) */ - function setValue(IStore _store, bytes32 key, bytes21[] memory value) internal { + /** Set hooks (using the specified store) */ + function setHooks(IStore _store, ResourceId tableId, bytes21[] memory hooks) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); - _store.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((value))); + _store.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((hooks))); } - /** Set value */ - function set(bytes32 key, bytes21[] memory value) internal { + /** Set hooks */ + function set(ResourceId tableId, bytes21[] memory hooks) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); - StoreSwitch.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((value))); + StoreSwitch.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((hooks))); } - /** Set value */ - function _set(bytes32 key, bytes21[] memory value) internal { + /** Set hooks */ + function _set(ResourceId tableId, bytes21[] memory hooks) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); - StoreCore.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((value))); + StoreCore.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((hooks))); } - /** Set value (using the specified store) */ - function set(IStore _store, bytes32 key, bytes21[] memory value) internal { + /** Set hooks (using the specified store) */ + function set(IStore _store, ResourceId tableId, bytes21[] memory hooks) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); - _store.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((value))); + _store.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((hooks))); } - /** Get the length of value */ - function lengthValue(bytes32 key) internal view returns (uint256) { + /** Get the length of hooks */ + function lengthHooks(ResourceId tableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -191,10 +194,10 @@ library StoreHooks { } } - /** Get the length of value */ - function _lengthValue(bytes32 key) internal view returns (uint256) { + /** Get the length of hooks */ + function _lengthHooks(ResourceId tableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -202,10 +205,10 @@ library StoreHooks { } } - /** Get the length of value (using the specified store) */ - function lengthValue(IStore _store, bytes32 key) internal view returns (uint256) { + /** Get the length of hooks (using the specified store) */ + function lengthHooks(IStore _store, ResourceId tableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); uint256 _byteLength = _store.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -213,10 +216,10 @@ library StoreHooks { } } - /** Get the length of value */ - function length(bytes32 key) internal view returns (uint256) { + /** Get the length of hooks */ + function length(ResourceId tableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -224,10 +227,10 @@ library StoreHooks { } } - /** Get the length of value */ - function _length(bytes32 key) internal view returns (uint256) { + /** Get the length of hooks */ + function _length(ResourceId tableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -235,10 +238,10 @@ library StoreHooks { } } - /** Get the length of value (using the specified store) */ - function length(IStore _store, bytes32 key) internal view returns (uint256) { + /** Get the length of hooks (using the specified store) */ + function length(IStore _store, ResourceId tableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); uint256 _byteLength = _store.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -247,12 +250,12 @@ library StoreHooks { } /** - * Get an item of value + * Get an item of hooks * (unchecked, returns invalid data if index overflows) */ - function getItemValue(bytes32 key, uint256 _index) internal view returns (bytes21) { + function getItemHooks(ResourceId tableId, uint256 _index) internal view returns (bytes21) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _blob = StoreSwitch.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 21, (_index + 1) * 21); @@ -261,12 +264,12 @@ library StoreHooks { } /** - * Get an item of value + * Get an item of hooks * (unchecked, returns invalid data if index overflows) */ - function _getItemValue(bytes32 key, uint256 _index) internal view returns (bytes21) { + function _getItemHooks(ResourceId tableId, uint256 _index) internal view returns (bytes21) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _blob = StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 21, (_index + 1) * 21); @@ -275,12 +278,12 @@ library StoreHooks { } /** - * Get an item of value (using the specified store) + * Get an item of hooks (using the specified store) * (unchecked, returns invalid data if index overflows) */ - function getItemValue(IStore _store, bytes32 key, uint256 _index) internal view returns (bytes21) { + function getItemHooks(IStore _store, ResourceId tableId, uint256 _index) internal view returns (bytes21) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _blob = _store.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 21, (_index + 1) * 21); @@ -289,12 +292,12 @@ library StoreHooks { } /** - * Get an item of value + * Get an item of hooks * (unchecked, returns invalid data if index overflows) */ - function getItem(bytes32 key, uint256 _index) internal view returns (bytes21) { + function getItem(ResourceId tableId, uint256 _index) internal view returns (bytes21) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _blob = StoreSwitch.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 21, (_index + 1) * 21); @@ -303,12 +306,12 @@ library StoreHooks { } /** - * Get an item of value + * Get an item of hooks * (unchecked, returns invalid data if index overflows) */ - function _getItem(bytes32 key, uint256 _index) internal view returns (bytes21) { + function _getItem(ResourceId tableId, uint256 _index) internal view returns (bytes21) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _blob = StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 21, (_index + 1) * 21); @@ -317,12 +320,12 @@ library StoreHooks { } /** - * Get an item of value (using the specified store) + * Get an item of hooks (using the specified store) * (unchecked, returns invalid data if index overflows) */ - function getItem(IStore _store, bytes32 key, uint256 _index) internal view returns (bytes21) { + function getItem(IStore _store, ResourceId tableId, uint256 _index) internal view returns (bytes21) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _blob = _store.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 21, (_index + 1) * 21); @@ -330,109 +333,109 @@ library StoreHooks { } } - /** Push an element to value */ - function pushValue(bytes32 key, bytes21 _element) internal { + /** Push an element to hooks */ + function pushHooks(ResourceId tableId, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); } - /** Push an element to value */ - function _pushValue(bytes32 key, bytes21 _element) internal { + /** Push an element to hooks */ + function _pushHooks(ResourceId tableId, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreCore.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); } - /** Push an element to value (using the specified store) */ - function pushValue(IStore _store, bytes32 key, bytes21 _element) internal { + /** Push an element to hooks (using the specified store) */ + function pushHooks(IStore _store, ResourceId tableId, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); _store.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); } - /** Push an element to value */ - function push(bytes32 key, bytes21 _element) internal { + /** Push an element to hooks */ + function push(ResourceId tableId, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); } - /** Push an element to value */ - function _push(bytes32 key, bytes21 _element) internal { + /** Push an element to hooks */ + function _push(ResourceId tableId, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreCore.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); } - /** Push an element to value (using the specified store) */ - function push(IStore _store, bytes32 key, bytes21 _element) internal { + /** Push an element to hooks (using the specified store) */ + function push(IStore _store, ResourceId tableId, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); _store.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); } - /** Pop an element from value */ - function popValue(bytes32 key) internal { + /** Pop an element from hooks */ + function popHooks(ResourceId tableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 0, 21); } - /** Pop an element from value */ - function _popValue(bytes32 key) internal { + /** Pop an element from hooks */ + function _popHooks(ResourceId tableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreCore.popFromDynamicField(_tableId, _keyTuple, 0, 21); } - /** Pop an element from value (using the specified store) */ - function popValue(IStore _store, bytes32 key) internal { + /** Pop an element from hooks (using the specified store) */ + function popHooks(IStore _store, ResourceId tableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); _store.popFromDynamicField(_tableId, _keyTuple, 0, 21); } - /** Pop an element from value */ - function pop(bytes32 key) internal { + /** Pop an element from hooks */ + function pop(ResourceId tableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 0, 21); } - /** Pop an element from value */ - function _pop(bytes32 key) internal { + /** Pop an element from hooks */ + function _pop(ResourceId tableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreCore.popFromDynamicField(_tableId, _keyTuple, 0, 21); } - /** Pop an element from value (using the specified store) */ - function pop(IStore _store, bytes32 key) internal { + /** Pop an element from hooks (using the specified store) */ + function pop(IStore _store, ResourceId tableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); _store.popFromDynamicField(_tableId, _keyTuple, 0, 21); } /** - * Update an element of value at `_index` + * Update an element of hooks at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function updateValue(bytes32 key, uint256 _index, bytes21 _element) internal { + function updateHooks(ResourceId tableId, uint256 _index, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -441,12 +444,12 @@ library StoreHooks { } /** - * Update an element of value at `_index` + * Update an element of hooks at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function _updateValue(bytes32 key, uint256 _index, bytes21 _element) internal { + function _updateHooks(ResourceId tableId, uint256 _index, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -455,12 +458,12 @@ library StoreHooks { } /** - * Update an element of value (using the specified store) at `_index` + * Update an element of hooks (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function updateValue(IStore _store, bytes32 key, uint256 _index, bytes21 _element) internal { + function updateHooks(IStore _store, ResourceId tableId, uint256 _index, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -469,12 +472,12 @@ library StoreHooks { } /** - * Update an element of value at `_index` + * Update an element of hooks at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function update(bytes32 key, uint256 _index, bytes21 _element) internal { + function update(ResourceId tableId, uint256 _index, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -483,12 +486,12 @@ library StoreHooks { } /** - * Update an element of value at `_index` + * Update an element of hooks at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function _update(bytes32 key, uint256 _index, bytes21 _element) internal { + function _update(ResourceId tableId, uint256 _index, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -497,12 +500,12 @@ library StoreHooks { } /** - * Update an element of value (using the specified store) at `_index` + * Update an element of hooks (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function update(IStore _store, bytes32 key, uint256 _index, bytes21 _element) internal { + function update(IStore _store, ResourceId tableId, uint256 _index, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -511,55 +514,55 @@ library StoreHooks { } /** Delete all data for given keys */ - function deleteRecord(bytes32 key) internal { + function deleteRecord(ResourceId tableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreSwitch.deleteRecord(_tableId, _keyTuple); } /** Delete all data for given keys */ - function _deleteRecord(bytes32 key) internal { + function _deleteRecord(ResourceId tableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /** Delete all data for given keys (using the specified store) */ - function deleteRecord(IStore _store, bytes32 key) internal { + function deleteRecord(IStore _store, ResourceId tableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); _store.deleteRecord(_tableId, _keyTuple); } /** Tightly pack dynamic data using this table's schema */ - function encodeLengths(bytes21[] memory value) internal pure returns (PackedCounter _encodedLengths) { + function encodeLengths(bytes21[] memory hooks) internal pure returns (PackedCounter _encodedLengths) { // Lengths are effectively checked during copy by 2**40 bytes exceeding gas limits unchecked { - _encodedLengths = PackedCounterLib.pack(value.length * 21); + _encodedLengths = PackedCounterLib.pack(hooks.length * 21); } } /** Tightly pack dynamic data using this table's schema */ - function encodeDynamic(bytes21[] memory value) internal pure returns (bytes memory) { - return abi.encodePacked(EncodeArray.encode((value))); + function encodeDynamic(bytes21[] memory hooks) internal pure returns (bytes memory) { + return abi.encodePacked(EncodeArray.encode((hooks))); } /** Tightly pack full data using this table's field layout */ - function encode(bytes21[] memory value) internal pure returns (bytes memory, PackedCounter, bytes memory) { + function encode(bytes21[] memory hooks) internal pure returns (bytes memory, PackedCounter, bytes memory) { bytes memory _staticData; - PackedCounter _encodedLengths = encodeLengths(value); - bytes memory _dynamicData = encodeDynamic(value); + PackedCounter _encodedLengths = encodeLengths(hooks); + bytes memory _dynamicData = encodeDynamic(hooks); return (_staticData, _encodedLengths, _dynamicData); } /** Encode keys as a bytes32 array using this table's field layout */ - function encodeKeyTuple(bytes32 key) internal pure returns (bytes32[] memory) { + function encodeKeyTuple(ResourceId tableId) internal pure returns (bytes32[] memory) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; + _keyTuple[0] = ResourceId.unwrap(tableId); return _keyTuple; } diff --git a/packages/store/src/codegen/tables/Tables.sol b/packages/store/src/codegen/tables/Tables.sol index 36a03a5a66..191dc883d3 100644 --- a/packages/store/src/codegen/tables/Tables.sol +++ b/packages/store/src/codegen/tables/Tables.sol @@ -20,6 +20,11 @@ import { PackedCounter, PackedCounterLib } from "../../PackedCounter.sol"; import { ResourceId } from "../../ResourceId.sol"; import { RESOURCE_TABLE, RESOURCE_OFFCHAIN_TABLE } from "../../storeResourceTypes.sol"; +// Import user types +import { ResourceId } from "./../../ResourceId.sol"; +import { FieldLayout } from "./../../FieldLayout.sol"; +import { Schema } from "./../../Schema.sol"; + ResourceId constant _tableId = ResourceId.wrap( bytes32(abi.encodePacked(RESOURCE_TABLE, bytes14("mudstore"), bytes16("Tables"))) ); @@ -30,9 +35,9 @@ FieldLayout constant _fieldLayout = FieldLayout.wrap( ); struct TablesData { - bytes32 fieldLayout; - bytes32 keySchema; - bytes32 valueSchema; + FieldLayout fieldLayout; + Schema keySchema; + Schema valueSchema; bytes abiEncodedKeyNames; bytes abiEncodedFieldNames; } @@ -95,171 +100,171 @@ library Tables { } /** Get fieldLayout */ - function getFieldLayout(bytes32 tableId) internal view returns (bytes32 fieldLayout) { + function getFieldLayout(ResourceId tableId) internal view returns (FieldLayout fieldLayout) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (bytes32(_blob)); + return FieldLayout.wrap(bytes32(_blob)); } /** Get fieldLayout */ - function _getFieldLayout(bytes32 tableId) internal view returns (bytes32 fieldLayout) { + function _getFieldLayout(ResourceId tableId) internal view returns (FieldLayout fieldLayout) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (bytes32(_blob)); + return FieldLayout.wrap(bytes32(_blob)); } /** Get fieldLayout (using the specified store) */ - function getFieldLayout(IStore _store, bytes32 tableId) internal view returns (bytes32 fieldLayout) { + function getFieldLayout(IStore _store, ResourceId tableId) internal view returns (FieldLayout fieldLayout) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (bytes32(_blob)); + return FieldLayout.wrap(bytes32(_blob)); } /** Set fieldLayout */ - function setFieldLayout(bytes32 tableId, bytes32 fieldLayout) internal { + function setFieldLayout(ResourceId tableId, FieldLayout fieldLayout) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); - StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((fieldLayout)), _fieldLayout); + StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked(FieldLayout.unwrap(fieldLayout)), _fieldLayout); } /** Set fieldLayout */ - function _setFieldLayout(bytes32 tableId, bytes32 fieldLayout) internal { + function _setFieldLayout(ResourceId tableId, FieldLayout fieldLayout) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); - StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((fieldLayout)), _fieldLayout); + StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked(FieldLayout.unwrap(fieldLayout)), _fieldLayout); } /** Set fieldLayout (using the specified store) */ - function setFieldLayout(IStore _store, bytes32 tableId, bytes32 fieldLayout) internal { + function setFieldLayout(IStore _store, ResourceId tableId, FieldLayout fieldLayout) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); - _store.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((fieldLayout)), _fieldLayout); + _store.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked(FieldLayout.unwrap(fieldLayout)), _fieldLayout); } /** Get keySchema */ - function getKeySchema(bytes32 tableId) internal view returns (bytes32 keySchema) { + function getKeySchema(ResourceId tableId) internal view returns (Schema keySchema) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); - return (bytes32(_blob)); + return Schema.wrap(bytes32(_blob)); } /** Get keySchema */ - function _getKeySchema(bytes32 tableId) internal view returns (bytes32 keySchema) { + function _getKeySchema(ResourceId tableId) internal view returns (Schema keySchema) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); - return (bytes32(_blob)); + return Schema.wrap(bytes32(_blob)); } /** Get keySchema (using the specified store) */ - function getKeySchema(IStore _store, bytes32 tableId) internal view returns (bytes32 keySchema) { + function getKeySchema(IStore _store, ResourceId tableId) internal view returns (Schema keySchema) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); - return (bytes32(_blob)); + return Schema.wrap(bytes32(_blob)); } /** Set keySchema */ - function setKeySchema(bytes32 tableId, bytes32 keySchema) internal { + function setKeySchema(ResourceId tableId, Schema keySchema) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); - StoreSwitch.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked((keySchema)), _fieldLayout); + StoreSwitch.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked(Schema.unwrap(keySchema)), _fieldLayout); } /** Set keySchema */ - function _setKeySchema(bytes32 tableId, bytes32 keySchema) internal { + function _setKeySchema(ResourceId tableId, Schema keySchema) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); - StoreCore.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked((keySchema)), _fieldLayout); + StoreCore.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked(Schema.unwrap(keySchema)), _fieldLayout); } /** Set keySchema (using the specified store) */ - function setKeySchema(IStore _store, bytes32 tableId, bytes32 keySchema) internal { + function setKeySchema(IStore _store, ResourceId tableId, Schema keySchema) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); - _store.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked((keySchema)), _fieldLayout); + _store.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked(Schema.unwrap(keySchema)), _fieldLayout); } /** Get valueSchema */ - function getValueSchema(bytes32 tableId) internal view returns (bytes32 valueSchema) { + function getValueSchema(ResourceId tableId) internal view returns (Schema valueSchema) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 2, _fieldLayout); - return (bytes32(_blob)); + return Schema.wrap(bytes32(_blob)); } /** Get valueSchema */ - function _getValueSchema(bytes32 tableId) internal view returns (bytes32 valueSchema) { + function _getValueSchema(ResourceId tableId) internal view returns (Schema valueSchema) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 2, _fieldLayout); - return (bytes32(_blob)); + return Schema.wrap(bytes32(_blob)); } /** Get valueSchema (using the specified store) */ - function getValueSchema(IStore _store, bytes32 tableId) internal view returns (bytes32 valueSchema) { + function getValueSchema(IStore _store, ResourceId tableId) internal view returns (Schema valueSchema) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 2, _fieldLayout); - return (bytes32(_blob)); + return Schema.wrap(bytes32(_blob)); } /** Set valueSchema */ - function setValueSchema(bytes32 tableId, bytes32 valueSchema) internal { + function setValueSchema(ResourceId tableId, Schema valueSchema) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); - StoreSwitch.setStaticField(_tableId, _keyTuple, 2, abi.encodePacked((valueSchema)), _fieldLayout); + StoreSwitch.setStaticField(_tableId, _keyTuple, 2, abi.encodePacked(Schema.unwrap(valueSchema)), _fieldLayout); } /** Set valueSchema */ - function _setValueSchema(bytes32 tableId, bytes32 valueSchema) internal { + function _setValueSchema(ResourceId tableId, Schema valueSchema) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); - StoreCore.setStaticField(_tableId, _keyTuple, 2, abi.encodePacked((valueSchema)), _fieldLayout); + StoreCore.setStaticField(_tableId, _keyTuple, 2, abi.encodePacked(Schema.unwrap(valueSchema)), _fieldLayout); } /** Set valueSchema (using the specified store) */ - function setValueSchema(IStore _store, bytes32 tableId, bytes32 valueSchema) internal { + function setValueSchema(IStore _store, ResourceId tableId, Schema valueSchema) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); - _store.setStaticField(_tableId, _keyTuple, 2, abi.encodePacked((valueSchema)), _fieldLayout); + _store.setStaticField(_tableId, _keyTuple, 2, abi.encodePacked(Schema.unwrap(valueSchema)), _fieldLayout); } /** Get abiEncodedKeyNames */ - function getAbiEncodedKeyNames(bytes32 tableId) internal view returns (bytes memory abiEncodedKeyNames) { + function getAbiEncodedKeyNames(ResourceId tableId) internal view returns (bytes memory abiEncodedKeyNames) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); bytes memory _blob = StoreSwitch.getDynamicField(_tableId, _keyTuple, 0); return (bytes(_blob)); } /** Get abiEncodedKeyNames */ - function _getAbiEncodedKeyNames(bytes32 tableId) internal view returns (bytes memory abiEncodedKeyNames) { + function _getAbiEncodedKeyNames(ResourceId tableId) internal view returns (bytes memory abiEncodedKeyNames) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0); return (bytes(_blob)); @@ -268,43 +273,43 @@ library Tables { /** Get abiEncodedKeyNames (using the specified store) */ function getAbiEncodedKeyNames( IStore _store, - bytes32 tableId + ResourceId tableId ) internal view returns (bytes memory abiEncodedKeyNames) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); bytes memory _blob = _store.getDynamicField(_tableId, _keyTuple, 0); return (bytes(_blob)); } /** Set abiEncodedKeyNames */ - function setAbiEncodedKeyNames(bytes32 tableId, bytes memory abiEncodedKeyNames) internal { + function setAbiEncodedKeyNames(ResourceId tableId, bytes memory abiEncodedKeyNames) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreSwitch.setDynamicField(_tableId, _keyTuple, 0, bytes((abiEncodedKeyNames))); } /** Set abiEncodedKeyNames */ - function _setAbiEncodedKeyNames(bytes32 tableId, bytes memory abiEncodedKeyNames) internal { + function _setAbiEncodedKeyNames(ResourceId tableId, bytes memory abiEncodedKeyNames) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreCore.setDynamicField(_tableId, _keyTuple, 0, bytes((abiEncodedKeyNames))); } /** Set abiEncodedKeyNames (using the specified store) */ - function setAbiEncodedKeyNames(IStore _store, bytes32 tableId, bytes memory abiEncodedKeyNames) internal { + function setAbiEncodedKeyNames(IStore _store, ResourceId tableId, bytes memory abiEncodedKeyNames) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); _store.setDynamicField(_tableId, _keyTuple, 0, bytes((abiEncodedKeyNames))); } /** Get the length of abiEncodedKeyNames */ - function lengthAbiEncodedKeyNames(bytes32 tableId) internal view returns (uint256) { + function lengthAbiEncodedKeyNames(ResourceId tableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -313,9 +318,9 @@ library Tables { } /** Get the length of abiEncodedKeyNames */ - function _lengthAbiEncodedKeyNames(bytes32 tableId) internal view returns (uint256) { + function _lengthAbiEncodedKeyNames(ResourceId tableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -324,9 +329,9 @@ library Tables { } /** Get the length of abiEncodedKeyNames (using the specified store) */ - function lengthAbiEncodedKeyNames(IStore _store, bytes32 tableId) internal view returns (uint256) { + function lengthAbiEncodedKeyNames(IStore _store, ResourceId tableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); uint256 _byteLength = _store.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -338,9 +343,9 @@ library Tables { * Get an item of abiEncodedKeyNames * (unchecked, returns invalid data if index overflows) */ - function getItemAbiEncodedKeyNames(bytes32 tableId, uint256 _index) internal view returns (bytes memory) { + function getItemAbiEncodedKeyNames(ResourceId tableId, uint256 _index) internal view returns (bytes memory) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _blob = StoreSwitch.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 1, (_index + 1) * 1); @@ -352,9 +357,9 @@ library Tables { * Get an item of abiEncodedKeyNames * (unchecked, returns invalid data if index overflows) */ - function _getItemAbiEncodedKeyNames(bytes32 tableId, uint256 _index) internal view returns (bytes memory) { + function _getItemAbiEncodedKeyNames(ResourceId tableId, uint256 _index) internal view returns (bytes memory) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _blob = StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 1, (_index + 1) * 1); @@ -368,11 +373,11 @@ library Tables { */ function getItemAbiEncodedKeyNames( IStore _store, - bytes32 tableId, + ResourceId tableId, uint256 _index ) internal view returns (bytes memory) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _blob = _store.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 1, (_index + 1) * 1); @@ -381,49 +386,49 @@ library Tables { } /** Push a slice to abiEncodedKeyNames */ - function pushAbiEncodedKeyNames(bytes32 tableId, bytes memory _slice) internal { + function pushAbiEncodedKeyNames(ResourceId tableId, bytes memory _slice) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 0, bytes((_slice))); } /** Push a slice to abiEncodedKeyNames */ - function _pushAbiEncodedKeyNames(bytes32 tableId, bytes memory _slice) internal { + function _pushAbiEncodedKeyNames(ResourceId tableId, bytes memory _slice) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreCore.pushToDynamicField(_tableId, _keyTuple, 0, bytes((_slice))); } /** Push a slice to abiEncodedKeyNames (using the specified store) */ - function pushAbiEncodedKeyNames(IStore _store, bytes32 tableId, bytes memory _slice) internal { + function pushAbiEncodedKeyNames(IStore _store, ResourceId tableId, bytes memory _slice) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); _store.pushToDynamicField(_tableId, _keyTuple, 0, bytes((_slice))); } /** Pop a slice from abiEncodedKeyNames */ - function popAbiEncodedKeyNames(bytes32 tableId) internal { + function popAbiEncodedKeyNames(ResourceId tableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 0, 1); } /** Pop a slice from abiEncodedKeyNames */ - function _popAbiEncodedKeyNames(bytes32 tableId) internal { + function _popAbiEncodedKeyNames(ResourceId tableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreCore.popFromDynamicField(_tableId, _keyTuple, 0, 1); } /** Pop a slice from abiEncodedKeyNames (using the specified store) */ - function popAbiEncodedKeyNames(IStore _store, bytes32 tableId) internal { + function popAbiEncodedKeyNames(IStore _store, ResourceId tableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); _store.popFromDynamicField(_tableId, _keyTuple, 0, 1); } @@ -432,9 +437,9 @@ library Tables { * 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 { + function updateAbiEncodedKeyNames(ResourceId tableId, uint256 _index, bytes memory _slice) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _encoded = bytes((_slice)); @@ -446,9 +451,9 @@ library Tables { * 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 { + function _updateAbiEncodedKeyNames(ResourceId tableId, uint256 _index, bytes memory _slice) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _encoded = bytes((_slice)); @@ -460,9 +465,9 @@ library Tables { * 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 { + function updateAbiEncodedKeyNames(IStore _store, ResourceId tableId, uint256 _index, bytes memory _slice) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _encoded = bytes((_slice)); @@ -471,18 +476,18 @@ library Tables { } /** Get abiEncodedFieldNames */ - function getAbiEncodedFieldNames(bytes32 tableId) internal view returns (bytes memory abiEncodedFieldNames) { + function getAbiEncodedFieldNames(ResourceId tableId) internal view returns (bytes memory abiEncodedFieldNames) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); bytes memory _blob = StoreSwitch.getDynamicField(_tableId, _keyTuple, 1); return (bytes(_blob)); } /** Get abiEncodedFieldNames */ - function _getAbiEncodedFieldNames(bytes32 tableId) internal view returns (bytes memory abiEncodedFieldNames) { + function _getAbiEncodedFieldNames(ResourceId tableId) internal view returns (bytes memory abiEncodedFieldNames) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 1); return (bytes(_blob)); @@ -491,43 +496,43 @@ library Tables { /** Get abiEncodedFieldNames (using the specified store) */ function getAbiEncodedFieldNames( IStore _store, - bytes32 tableId + ResourceId tableId ) internal view returns (bytes memory abiEncodedFieldNames) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); bytes memory _blob = _store.getDynamicField(_tableId, _keyTuple, 1); return (bytes(_blob)); } /** Set abiEncodedFieldNames */ - function setAbiEncodedFieldNames(bytes32 tableId, bytes memory abiEncodedFieldNames) internal { + function setAbiEncodedFieldNames(ResourceId tableId, bytes memory abiEncodedFieldNames) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreSwitch.setDynamicField(_tableId, _keyTuple, 1, bytes((abiEncodedFieldNames))); } /** Set abiEncodedFieldNames */ - function _setAbiEncodedFieldNames(bytes32 tableId, bytes memory abiEncodedFieldNames) internal { + function _setAbiEncodedFieldNames(ResourceId tableId, bytes memory abiEncodedFieldNames) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreCore.setDynamicField(_tableId, _keyTuple, 1, bytes((abiEncodedFieldNames))); } /** Set abiEncodedFieldNames (using the specified store) */ - function setAbiEncodedFieldNames(IStore _store, bytes32 tableId, bytes memory abiEncodedFieldNames) internal { + function setAbiEncodedFieldNames(IStore _store, ResourceId tableId, bytes memory abiEncodedFieldNames) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); _store.setDynamicField(_tableId, _keyTuple, 1, bytes((abiEncodedFieldNames))); } /** Get the length of abiEncodedFieldNames */ - function lengthAbiEncodedFieldNames(bytes32 tableId) internal view returns (uint256) { + function lengthAbiEncodedFieldNames(ResourceId tableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 1); unchecked { @@ -536,9 +541,9 @@ library Tables { } /** Get the length of abiEncodedFieldNames */ - function _lengthAbiEncodedFieldNames(bytes32 tableId) internal view returns (uint256) { + function _lengthAbiEncodedFieldNames(ResourceId tableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 1); unchecked { @@ -547,9 +552,9 @@ library Tables { } /** Get the length of abiEncodedFieldNames (using the specified store) */ - function lengthAbiEncodedFieldNames(IStore _store, bytes32 tableId) internal view returns (uint256) { + function lengthAbiEncodedFieldNames(IStore _store, ResourceId tableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); uint256 _byteLength = _store.getDynamicFieldLength(_tableId, _keyTuple, 1); unchecked { @@ -561,9 +566,9 @@ library Tables { * Get an item of abiEncodedFieldNames * (unchecked, returns invalid data if index overflows) */ - function getItemAbiEncodedFieldNames(bytes32 tableId, uint256 _index) internal view returns (bytes memory) { + function getItemAbiEncodedFieldNames(ResourceId tableId, uint256 _index) internal view returns (bytes memory) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _blob = StoreSwitch.getDynamicFieldSlice(_tableId, _keyTuple, 1, _index * 1, (_index + 1) * 1); @@ -575,9 +580,9 @@ library Tables { * Get an item of abiEncodedFieldNames * (unchecked, returns invalid data if index overflows) */ - function _getItemAbiEncodedFieldNames(bytes32 tableId, uint256 _index) internal view returns (bytes memory) { + function _getItemAbiEncodedFieldNames(ResourceId tableId, uint256 _index) internal view returns (bytes memory) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _blob = StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 1, _index * 1, (_index + 1) * 1); @@ -591,11 +596,11 @@ library Tables { */ function getItemAbiEncodedFieldNames( IStore _store, - bytes32 tableId, + ResourceId tableId, uint256 _index ) internal view returns (bytes memory) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _blob = _store.getDynamicFieldSlice(_tableId, _keyTuple, 1, _index * 1, (_index + 1) * 1); @@ -604,49 +609,49 @@ library Tables { } /** Push a slice to abiEncodedFieldNames */ - function pushAbiEncodedFieldNames(bytes32 tableId, bytes memory _slice) internal { + function pushAbiEncodedFieldNames(ResourceId tableId, bytes memory _slice) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 1, bytes((_slice))); } /** Push a slice to abiEncodedFieldNames */ - function _pushAbiEncodedFieldNames(bytes32 tableId, bytes memory _slice) internal { + function _pushAbiEncodedFieldNames(ResourceId tableId, bytes memory _slice) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreCore.pushToDynamicField(_tableId, _keyTuple, 1, bytes((_slice))); } /** Push a slice to abiEncodedFieldNames (using the specified store) */ - function pushAbiEncodedFieldNames(IStore _store, bytes32 tableId, bytes memory _slice) internal { + function pushAbiEncodedFieldNames(IStore _store, ResourceId tableId, bytes memory _slice) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); _store.pushToDynamicField(_tableId, _keyTuple, 1, bytes((_slice))); } /** Pop a slice from abiEncodedFieldNames */ - function popAbiEncodedFieldNames(bytes32 tableId) internal { + function popAbiEncodedFieldNames(ResourceId tableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 1, 1); } /** Pop a slice from abiEncodedFieldNames */ - function _popAbiEncodedFieldNames(bytes32 tableId) internal { + function _popAbiEncodedFieldNames(ResourceId tableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreCore.popFromDynamicField(_tableId, _keyTuple, 1, 1); } /** Pop a slice from abiEncodedFieldNames (using the specified store) */ - function popAbiEncodedFieldNames(IStore _store, bytes32 tableId) internal { + function popAbiEncodedFieldNames(IStore _store, ResourceId tableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); _store.popFromDynamicField(_tableId, _keyTuple, 1, 1); } @@ -655,9 +660,9 @@ library Tables { * 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 { + function updateAbiEncodedFieldNames(ResourceId tableId, uint256 _index, bytes memory _slice) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _encoded = bytes((_slice)); @@ -669,9 +674,9 @@ library Tables { * 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 { + function _updateAbiEncodedFieldNames(ResourceId tableId, uint256 _index, bytes memory _slice) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _encoded = bytes((_slice)); @@ -683,9 +688,9 @@ library Tables { * 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 { + function updateAbiEncodedFieldNames(IStore _store, ResourceId tableId, uint256 _index, bytes memory _slice) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); unchecked { bytes memory _encoded = bytes((_slice)); @@ -694,9 +699,9 @@ library Tables { } /** Get the full data */ - function get(bytes32 tableId) internal view returns (TablesData memory _table) { + function get(ResourceId tableId) internal view returns (TablesData memory _table) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); (bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = StoreSwitch.getRecord( _tableId, @@ -707,9 +712,9 @@ library Tables { } /** Get the full data */ - function _get(bytes32 tableId) internal view returns (TablesData memory _table) { + function _get(ResourceId tableId) internal view returns (TablesData memory _table) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); (bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = StoreCore.getRecord( _tableId, @@ -720,9 +725,9 @@ library Tables { } /** Get the full data (using the specified store) */ - function get(IStore _store, bytes32 tableId) internal view returns (TablesData memory _table) { + function get(IStore _store, ResourceId tableId) internal view returns (TablesData memory _table) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); (bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = _store.getRecord( _tableId, @@ -734,10 +739,10 @@ library Tables { /** Set the full data using individual values */ function set( - bytes32 tableId, - bytes32 fieldLayout, - bytes32 keySchema, - bytes32 valueSchema, + ResourceId tableId, + FieldLayout fieldLayout, + Schema keySchema, + Schema valueSchema, bytes memory abiEncodedKeyNames, bytes memory abiEncodedFieldNames ) internal { @@ -747,17 +752,17 @@ library Tables { bytes memory _dynamicData = encodeDynamic(abiEncodedKeyNames, abiEncodedFieldNames); bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData); } /** Set the full data using individual values */ function _set( - bytes32 tableId, - bytes32 fieldLayout, - bytes32 keySchema, - bytes32 valueSchema, + ResourceId tableId, + FieldLayout fieldLayout, + Schema keySchema, + Schema valueSchema, bytes memory abiEncodedKeyNames, bytes memory abiEncodedFieldNames ) internal { @@ -767,7 +772,7 @@ library Tables { bytes memory _dynamicData = encodeDynamic(abiEncodedKeyNames, abiEncodedFieldNames); bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } @@ -775,10 +780,10 @@ library Tables { /** Set the full data using individual values (using the specified store) */ function set( IStore _store, - bytes32 tableId, - bytes32 fieldLayout, - bytes32 keySchema, - bytes32 valueSchema, + ResourceId tableId, + FieldLayout fieldLayout, + Schema keySchema, + Schema valueSchema, bytes memory abiEncodedKeyNames, bytes memory abiEncodedFieldNames ) internal { @@ -788,46 +793,46 @@ library Tables { bytes memory _dynamicData = encodeDynamic(abiEncodedKeyNames, abiEncodedFieldNames); bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData); } /** Set the full data using the data struct */ - function set(bytes32 tableId, TablesData memory _table) internal { + function set(ResourceId tableId, TablesData memory _table) internal { bytes memory _staticData = encodeStatic(_table.fieldLayout, _table.keySchema, _table.valueSchema); PackedCounter _encodedLengths = encodeLengths(_table.abiEncodedKeyNames, _table.abiEncodedFieldNames); bytes memory _dynamicData = encodeDynamic(_table.abiEncodedKeyNames, _table.abiEncodedFieldNames); bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData); } /** Set the full data using the data struct */ - function _set(bytes32 tableId, TablesData memory _table) internal { + function _set(ResourceId tableId, TablesData memory _table) internal { bytes memory _staticData = encodeStatic(_table.fieldLayout, _table.keySchema, _table.valueSchema); PackedCounter _encodedLengths = encodeLengths(_table.abiEncodedKeyNames, _table.abiEncodedFieldNames); bytes memory _dynamicData = encodeDynamic(_table.abiEncodedKeyNames, _table.abiEncodedFieldNames); bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using the data struct (using the specified store) */ - function set(IStore _store, bytes32 tableId, TablesData memory _table) internal { + function set(IStore _store, ResourceId tableId, TablesData memory _table) internal { bytes memory _staticData = encodeStatic(_table.fieldLayout, _table.keySchema, _table.valueSchema); PackedCounter _encodedLengths = encodeLengths(_table.abiEncodedKeyNames, _table.abiEncodedFieldNames); bytes memory _dynamicData = encodeDynamic(_table.abiEncodedKeyNames, _table.abiEncodedFieldNames); bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData); } @@ -838,12 +843,12 @@ library Tables { */ function decodeStatic( bytes memory _blob - ) internal pure returns (bytes32 fieldLayout, bytes32 keySchema, bytes32 valueSchema) { - fieldLayout = (Bytes.slice32(_blob, 0)); + ) internal pure returns (FieldLayout fieldLayout, Schema keySchema, Schema valueSchema) { + fieldLayout = FieldLayout.wrap(Bytes.slice32(_blob, 0)); - keySchema = (Bytes.slice32(_blob, 32)); + keySchema = Schema.wrap(Bytes.slice32(_blob, 32)); - valueSchema = (Bytes.slice32(_blob, 64)); + valueSchema = Schema.wrap(Bytes.slice32(_blob, 64)); } /** @@ -883,34 +888,34 @@ library Tables { } /** Delete all data for given keys */ - function deleteRecord(bytes32 tableId) internal { + function deleteRecord(ResourceId tableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreSwitch.deleteRecord(_tableId, _keyTuple); } /** Delete all data for given keys */ - function _deleteRecord(bytes32 tableId) internal { + function _deleteRecord(ResourceId tableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /** Delete all data for given keys (using the specified store) */ - function deleteRecord(IStore _store, bytes32 tableId) internal { + function deleteRecord(IStore _store, ResourceId tableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); _store.deleteRecord(_tableId, _keyTuple); } /** Tightly pack static data using this table's schema */ function encodeStatic( - bytes32 fieldLayout, - bytes32 keySchema, - bytes32 valueSchema + FieldLayout fieldLayout, + Schema keySchema, + Schema valueSchema ) internal pure returns (bytes memory) { return abi.encodePacked(fieldLayout, keySchema, valueSchema); } @@ -936,9 +941,9 @@ library Tables { /** Tightly pack full data using this table's field layout */ function encode( - bytes32 fieldLayout, - bytes32 keySchema, - bytes32 valueSchema, + FieldLayout fieldLayout, + Schema keySchema, + Schema valueSchema, bytes memory abiEncodedKeyNames, bytes memory abiEncodedFieldNames ) internal pure returns (bytes memory, PackedCounter, bytes memory) { @@ -951,9 +956,9 @@ library Tables { } /** Encode keys as a bytes32 array using this table's field layout */ - function encodeKeyTuple(bytes32 tableId) internal pure returns (bytes32[] memory) { + function encodeKeyTuple(ResourceId tableId) internal pure returns (bytes32[] memory) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = tableId; + _keyTuple[0] = ResourceId.unwrap(tableId); return _keyTuple; } diff --git a/packages/store/test/MirrorSubscriber.sol b/packages/store/test/MirrorSubscriber.sol index 137fb36a45..295fc63fce 100644 --- a/packages/store/test/MirrorSubscriber.sol +++ b/packages/store/test/MirrorSubscriber.sol @@ -35,7 +35,7 @@ contract MirrorSubscriber is StoreHook { bytes memory staticData, PackedCounter encodedLengths, bytes memory dynamicData, - FieldLayout fieldLayout + FieldLayout ) public override { if (ResourceId.unwrap(tableId) != _tableId) revert("invalid table"); StoreSwitch.setRecord(indexerTableId, keyTuple, staticData, encodedLengths, dynamicData); @@ -64,11 +64,7 @@ contract MirrorSubscriber is StoreHook { StoreSwitch.spliceDynamicData(indexerTableId, keyTuple, dynamicFieldIndex, startWithinField, deleteCount, data); } - function onBeforeDeleteRecord( - ResourceId tableId, - bytes32[] memory keyTuple, - FieldLayout fieldLayout - ) public override { + function onBeforeDeleteRecord(ResourceId tableId, bytes32[] memory keyTuple, FieldLayout) public override { if (ResourceId.unwrap(tableId) != _tableId) revert("invalid tableId"); StoreSwitch.deleteRecord(indexerTableId, keyTuple); } diff --git a/packages/store/test/StoreCore.t.sol b/packages/store/test/StoreCore.t.sol index 61dc2cddcd..fc918ea0ab 100644 --- a/packages/store/test/StoreCore.t.sol +++ b/packages/store/test/StoreCore.t.sol @@ -85,8 +85,8 @@ contract StoreCoreTest is Test, StoreMock { emit Store_SetRecord( TablesTableId, keyTuple, - Tables.encodeStatic(fieldLayout.unwrap(), keySchema.unwrap(), valueSchema.unwrap()), - Tables.encodeLengths(abi.encode(keyNames), abi.encode(fieldNames)).unwrap(), + Tables.encodeStatic(fieldLayout, keySchema, valueSchema), + Tables.encodeLengths(abi.encode(keyNames), abi.encode(fieldNames)), Tables.encodeDynamic(abi.encode(keyNames), abi.encode(fieldNames)) ); IStore(this).registerTable(tableId, fieldLayout, keySchema, valueSchema, keyNames, fieldNames); @@ -95,14 +95,14 @@ contract StoreCoreTest is Test, StoreMock { assertEq(IStore(this).getValueSchema(tableId).unwrap(), valueSchema.unwrap()); assertEq(IStore(this).getKeySchema(tableId).unwrap(), keySchema.unwrap()); - bytes memory loadedKeyNames = Tables.getAbiEncodedKeyNames(IStore(this), ResourceId.unwrap(tableId)); + bytes memory loadedKeyNames = Tables.getAbiEncodedKeyNames(IStore(this), tableId); assertEq(loadedKeyNames, abi.encode(keyNames)); - bytes memory loadedFieldNames = Tables.getAbiEncodedFieldNames(IStore(this), ResourceId.unwrap(tableId)); + bytes memory loadedFieldNames = Tables.getAbiEncodedFieldNames(IStore(this), tableId); assertEq(loadedFieldNames, abi.encode(fieldNames)); // Expect the table ID to be registered - assertTrue(ResourceIds._getExists(ResourceId.unwrap(tableId))); + assertTrue(ResourceIds._getExists(tableId)); } function testRevertTableExists() public { @@ -117,11 +117,7 @@ contract StoreCoreTest is Test, StoreMock { // Expect a revert when registering a table that already exists vm.expectRevert( - abi.encodeWithSelector( - IStoreErrors.Store_TableAlreadyExists.selector, - ResourceId.unwrap(tableId), - string(bytes.concat(ResourceId.unwrap(tableId))) - ) + abi.encodeWithSelector(IStoreErrors.Store_TableAlreadyExists.selector, tableId, string(abi.encodePacked(tableId))) ); IStore(this).registerTable(tableId, fieldLayout, keySchema, valueSchema, keyNames, fieldNames); } @@ -186,8 +182,8 @@ contract StoreCoreTest is Test, StoreMock { ); IStore(this).registerTable(tableId, fieldLayout, defaultKeySchema, valueSchema, keyNames, fieldNames); - assertTrue(ResourceIds._getExists(ResourceId.unwrap(tableId))); - assertFalse(ResourceIds._getExists(ResourceId.unwrap(tableId2))); + assertTrue(ResourceIds._getExists(tableId)); + assertFalse(ResourceIds._getExists(tableId2)); assertEq(FieldLayout.unwrap(IStore(this).getFieldLayout(tableId)), FieldLayout.unwrap(fieldLayout)); assertEq(Schema.unwrap(IStore(this).getValueSchema(tableId)), Schema.unwrap(valueSchema)); @@ -295,7 +291,7 @@ contract StoreCoreTest is Test, StoreMock { // Expect a Store_SetRecord event to be emitted vm.expectEmit(true, true, true, true); - emit Store_SetRecord(tableId, keyTuple, staticData, bytes32(0), new bytes(0)); + emit Store_SetRecord(tableId, keyTuple, staticData, PackedCounter.wrap(bytes32(0)), new bytes(0)); IStore(this).setRecord(tableId, keyTuple, staticData, PackedCounter.wrap(bytes32(0)), new bytes(0)); @@ -332,7 +328,7 @@ contract StoreCoreTest is Test, StoreMock { // Expect a Store_SetRecord event to be emitted vm.expectEmit(true, true, true, true); - emit Store_SetRecord(tableId, keyTuple, staticData, bytes32(0), new bytes(0)); + emit Store_SetRecord(tableId, keyTuple, staticData, PackedCounter.wrap(bytes32(0)), new bytes(0)); IStore(this).setRecord(tableId, keyTuple, staticData, PackedCounter.wrap(bytes32(0)), new bytes(0)); @@ -396,7 +392,7 @@ contract StoreCoreTest is Test, StoreMock { // Expect a Store_SetRecord event to be emitted vm.expectEmit(true, true, true, true); - emit Store_SetRecord(tableId, keyTuple, staticData, encodedDynamicLength.unwrap(), dynamicData); + emit Store_SetRecord(tableId, keyTuple, staticData, encodedDynamicLength, dynamicData); // Set data IStore(this).setRecord(tableId, keyTuple, staticData, encodedDynamicLength, dynamicData); @@ -551,7 +547,7 @@ contract StoreCoreTest is Test, StoreMock { uint48(0), 0, _data.thirdDataBytes, - PackedCounterLib.pack(_data.thirdDataBytes.length, 0).unwrap() + PackedCounterLib.pack(_data.thirdDataBytes.length, 0) ); // Set third field @@ -586,7 +582,7 @@ contract StoreCoreTest is Test, StoreMock { uint48(_data.thirdDataBytes.length), 0, _data.fourthDataBytes, - PackedCounterLib.pack(_data.thirdDataBytes.length, _data.fourthDataBytes.length).unwrap() + PackedCounterLib.pack(_data.thirdDataBytes.length, _data.fourthDataBytes.length) ); // Set fourth field @@ -631,7 +627,7 @@ contract StoreCoreTest is Test, StoreMock { uint48(_data.thirdDataBytes.length), uint40(_data.fourthDataBytes.length), _data.thirdDataBytes, - PackedCounterLib.pack(_data.thirdDataBytes.length, _data.thirdDataBytes.length).unwrap() + PackedCounterLib.pack(_data.thirdDataBytes.length, _data.thirdDataBytes.length) ); // Set fourth field @@ -812,7 +808,7 @@ contract StoreCoreTest is Test, StoreMock { uint48(data.secondDataBytes.length), 0, data.secondDataToPush, - PackedCounterLib.pack(data.newSecondDataBytes.length, data.thirdDataBytes.length).unwrap() + PackedCounterLib.pack(data.newSecondDataBytes.length, data.thirdDataBytes.length) ); // Push to second field @@ -855,7 +851,7 @@ contract StoreCoreTest is Test, StoreMock { uint48(data.newSecondDataBytes.length + data.thirdDataBytes.length), 0, data.thirdDataToPush, - PackedCounterLib.pack(data.newSecondDataBytes.length, data.newThirdDataBytes.length).unwrap() + PackedCounterLib.pack(data.newSecondDataBytes.length, data.newThirdDataBytes.length) ); // Push to third field @@ -965,7 +961,7 @@ contract StoreCoreTest is Test, StoreMock { uint48(4 * 1), 4 * 1, data.secondDataForUpdate, - PackedCounterLib.pack(data.newSecondDataBytes.length, data.thirdDataBytes.length).unwrap() + PackedCounterLib.pack(data.newSecondDataBytes.length, data.thirdDataBytes.length) ); // Update index 1 in second field (4 = byte length of uint32) @@ -1017,7 +1013,7 @@ contract StoreCoreTest is Test, StoreMock { uint48(data.newSecondDataBytes.length + 8 * 1), 8 * 4, data.thirdDataForUpdate, - PackedCounterLib.pack(data.newSecondDataBytes.length, data.newThirdDataBytes.length).unwrap() + PackedCounterLib.pack(data.newSecondDataBytes.length, data.newThirdDataBytes.length) ); // Update indexes 1,2,3,4 in third field (8 = byte length of uint64) diff --git a/packages/store/test/StoreCoreDynamic.t.sol b/packages/store/test/StoreCoreDynamic.t.sol index e91eaf498c..835be5fc95 100644 --- a/packages/store/test/StoreCoreDynamic.t.sol +++ b/packages/store/test/StoreCoreDynamic.t.sol @@ -85,6 +85,8 @@ contract StoreCoreDynamicTest is Test, GasReporter, StoreMock { function testPopFromSecondField() public { FieldLayout fieldLayout = StoreCore.getFieldLayout(_tableId); bytes memory dataBytes = secondDataBytes; + ResourceId tableId = _tableId; + bytes32[] memory keyTuple = _keyTuple; // Prepare expected data uint256 byteLengthToPop = 1 * 4; @@ -96,41 +98,43 @@ contract StoreCoreDynamicTest is Test, GasReporter, StoreMock { // Expect a StoreSpliceRecord event to be emitted vm.expectEmit(true, true, true, true); emit Store_SpliceDynamicData( - _tableId, - _keyTuple, + tableId, + keyTuple, uint48(secondDataBytes.length - byteLengthToPop), uint40(byteLengthToPop), new bytes(0), - PackedCounterLib.pack(newDataBytes.length, thirdDataBytes.length).unwrap() + PackedCounterLib.pack(newDataBytes.length, thirdDataBytes.length) ); // Pop from second field startGasReport("pop from field (cold, 1 slot, 1 uint32 item)"); - StoreCore.popFromDynamicField(_tableId, _keyTuple, 0, byteLengthToPop); + StoreCore.popFromDynamicField(tableId, keyTuple, 0, byteLengthToPop); endGasReport(); // Get second field - bytes memory loadedData = StoreCore.getField(_tableId, _keyTuple, 1, fieldLayout); + bytes memory loadedData = StoreCore.getField(tableId, keyTuple, 1, fieldLayout); // Verify loaded data is correct assertEq(loadedData, newDataBytes); // Reset the second field and pop again (but warm this time) - StoreCore.setField(_tableId, _keyTuple, 1, dataBytes, fieldLayout); + StoreCore.setField(tableId, keyTuple, 1, dataBytes, fieldLayout); startGasReport("pop from field (warm, 1 slot, 1 uint32 item)"); - StoreCore.popFromDynamicField(_tableId, _keyTuple, 0, byteLengthToPop); + StoreCore.popFromDynamicField(tableId, keyTuple, 0, byteLengthToPop); endGasReport(); // Get second field - loadedData = StoreCore.getField(_tableId, _keyTuple, 1, fieldLayout); + loadedData = StoreCore.getField(tableId, keyTuple, 1, fieldLayout); // Verify loaded data is correct assertEq(loadedData, newDataBytes); // Verify none of the other fields were impacted - assertEq(bytes32(StoreCore.getField(_tableId, _keyTuple, 0, fieldLayout)), firstDataBytes); - assertEq(StoreCore.getField(_tableId, _keyTuple, 2, fieldLayout), thirdDataBytes); + assertEq(bytes32(StoreCore.getField(tableId, keyTuple, 0, fieldLayout)), firstDataBytes); + assertEq(StoreCore.getField(tableId, keyTuple, 2, fieldLayout), thirdDataBytes); } function testPopFromThirdField() public { FieldLayout fieldLayout = StoreCore.getFieldLayout(_tableId); bytes memory dataBytes = thirdDataBytes; + ResourceId tableId = _tableId; + bytes32[] memory keyTuple = _keyTuple; // Prepare expected data uint256 byteLengthToPop = 10 * 4; @@ -142,90 +146,95 @@ contract StoreCoreDynamicTest is Test, GasReporter, StoreMock { // Expect a StoreSpliceRecord event to be emitted after pop vm.expectEmit(true, true, true, true); emit Store_SpliceDynamicData( - _tableId, - _keyTuple, + tableId, + keyTuple, uint48(secondDataBytes.length + thirdDataBytes.length - byteLengthToPop), uint40(byteLengthToPop), new bytes(0), - PackedCounterLib.pack(secondDataBytes.length, newDataBytes.length).unwrap() + PackedCounterLib.pack(secondDataBytes.length, newDataBytes.length) ); // Pop from the field startGasReport("pop from field (cold, 2 slots, 10 uint32 items)"); - StoreCore.popFromDynamicField(_tableId, _keyTuple, 1, byteLengthToPop); + StoreCore.popFromDynamicField(tableId, keyTuple, 1, byteLengthToPop); endGasReport(); // Load and verify the field - bytes memory loadedData = StoreCore.getField(_tableId, _keyTuple, 2, fieldLayout); + bytes memory loadedData = StoreCore.getField(tableId, keyTuple, 2, fieldLayout); assertEq(loadedData, newDataBytes); // Reset the field and pop again (but warm this time) - StoreCore.setField(_tableId, _keyTuple, 2, dataBytes, fieldLayout); + StoreCore.setField(tableId, keyTuple, 2, dataBytes, fieldLayout); startGasReport("pop from field (warm, 2 slots, 10 uint32 items)"); - StoreCore.popFromDynamicField(_tableId, _keyTuple, 1, byteLengthToPop); + StoreCore.popFromDynamicField(tableId, keyTuple, 1, byteLengthToPop); endGasReport(); // Load and verify the field - loadedData = StoreCore.getField(_tableId, _keyTuple, 2, fieldLayout); + loadedData = StoreCore.getField(tableId, keyTuple, 2, fieldLayout); assertEq(loadedData, newDataBytes); // Verify none of the other fields were impacted - assertEq(bytes32(StoreCore.getField(_tableId, _keyTuple, 0, fieldLayout)), firstDataBytes); - assertEq(StoreCore.getField(_tableId, _keyTuple, 1, fieldLayout), secondDataBytes); + assertEq(bytes32(StoreCore.getField(tableId, keyTuple, 0, fieldLayout)), firstDataBytes); + assertEq(StoreCore.getField(tableId, keyTuple, 1, fieldLayout), secondDataBytes); } function testGetSecondFieldLength() public { FieldLayout fieldLayout = StoreCore.getFieldLayout(_tableId); + ResourceId tableId = _tableId; + bytes32[] memory keyTuple = _keyTuple; startGasReport("get field length (cold, 1 slot)"); - uint256 length = StoreCore.getFieldLength(_tableId, _keyTuple, 1, fieldLayout); + uint256 length = StoreCore.getFieldLength(tableId, keyTuple, 1, fieldLayout); endGasReport(); assertEq(length, secondDataBytes.length); startGasReport("get field length (warm, 1 slot)"); - length = StoreCore.getFieldLength(_tableId, _keyTuple, 1, fieldLayout); + length = StoreCore.getFieldLength(tableId, keyTuple, 1, fieldLayout); endGasReport(); assertEq(length, secondDataBytes.length); } function testGetThirdFieldLength() public { FieldLayout fieldLayout = StoreCore.getFieldLayout(_tableId); + ResourceId tableId = _tableId; + bytes32[] memory keyTuple = _keyTuple; startGasReport("get field length (warm due to , 2 slots)"); - uint256 length = StoreCore.getFieldLength(_tableId, _keyTuple, 2, fieldLayout); + uint256 length = StoreCore.getFieldLength(tableId, keyTuple, 2, fieldLayout); endGasReport(); assertEq(length, thirdDataBytes.length); startGasReport("get field length (warm, 2 slots)"); - length = StoreCore.getFieldLength(_tableId, _keyTuple, 2, fieldLayout); + length = StoreCore.getFieldLength(tableId, keyTuple, 2, fieldLayout); endGasReport(); assertEq(length, thirdDataBytes.length); } function testGetDynamicFieldSlice() public { - FieldLayout fieldLayout = StoreCore.getFieldLayout(_tableId); + ResourceId tableId = _tableId; + bytes32[] memory keyTuple = _keyTuple; startGasReport("get field slice (cold, 1 slot)"); - bytes memory secondFieldSlice = StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 0, 0, 4); + bytes memory secondFieldSlice = StoreCore.getDynamicFieldSlice(tableId, keyTuple, 0, 0, 4); endGasReport(); assertEq(secondFieldSlice, SliceLib.getSubslice(secondDataBytes, 0, 4).toBytes()); startGasReport("get field slice (warm, 1 slot)"); - secondFieldSlice = StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 0, 4, 8); + secondFieldSlice = StoreCore.getDynamicFieldSlice(tableId, keyTuple, 0, 4, 8); endGasReport(); assertEq(secondFieldSlice, SliceLib.getSubslice(secondDataBytes, 4, 8).toBytes()); startGasReport("get field slice (semi-cold, 1 slot)"); - bytes memory thirdFieldSlice = StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 1, 4, 32); + bytes memory thirdFieldSlice = StoreCore.getDynamicFieldSlice(tableId, keyTuple, 1, 4, 32); endGasReport(); assertEq(thirdFieldSlice, SliceLib.getSubslice(thirdDataBytes, 4, 32).toBytes()); startGasReport("get field slice (warm, 2 slots)"); - thirdFieldSlice = StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 1, 8, 40); + thirdFieldSlice = StoreCore.getDynamicFieldSlice(tableId, keyTuple, 1, 8, 40); endGasReport(); assertEq(thirdFieldSlice, SliceLib.getSubslice(thirdDataBytes, 8, 40).toBytes()); // Expect a revert if the end index is out of bounds uint256 length = secondDataBytes.length; vm.expectRevert(abi.encodeWithSelector(IStoreErrors.Store_IndexOutOfBounds.selector, length, length)); - StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 0, 0, length + 1); + StoreCore.getDynamicFieldSlice(tableId, keyTuple, 0, 0, length + 1); // Expect a revert if the start index is out of bounds vm.expectRevert(abi.encodeWithSelector(IStoreErrors.Store_IndexOutOfBounds.selector, length, length)); - StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 0, length, length); + StoreCore.getDynamicFieldSlice(tableId, keyTuple, 0, length, length); } } diff --git a/packages/store/test/StoreCoreGas.t.sol b/packages/store/test/StoreCoreGas.t.sol index dfe4f86597..3b65a03fc2 100644 --- a/packages/store/test/StoreCoreGas.t.sol +++ b/packages/store/test/StoreCoreGas.t.sol @@ -105,11 +105,11 @@ contract StoreCoreGasTest is Test, GasReporter, StoreMock { StoreCore.registerTable(tableId, fieldLayout, defaultKeySchema, valueSchema, new string[](1), new string[](4)); startGasReport("Check for existence of table (existent)"); - ResourceIds._getExists(ResourceId.unwrap(tableId)); + ResourceIds._getExists(tableId); endGasReport(); startGasReport("check for existence of table (non-existent)"); - ResourceIds._getExists(ResourceId.unwrap(tableId2)); + ResourceIds._getExists(tableId2); endGasReport(); } diff --git a/packages/store/test/tables/StoreHooks.t.sol b/packages/store/test/tables/StoreHooks.t.sol index 4e72e4ec89..78cdd70f70 100644 --- a/packages/store/test/tables/StoreHooks.t.sol +++ b/packages/store/test/tables/StoreHooks.t.sol @@ -3,13 +3,14 @@ pragma solidity >=0.8.21; import { Test } from "forge-std/Test.sol"; import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol"; -import { StoreMock } from "../../test/StoreMock.sol"; +import { ResourceId } from "../../src/ResourceId.sol"; import { StoreHooks } from "../../src/codegen/index.sol"; +import { StoreMock } from "../StoreMock.sol"; contract StoreHooksTest is Test, GasReporter, StoreMock { function testTable() public { // StoreHooks table is already registered by StoreMock - bytes32 key = keccak256("somekey"); + ResourceId key = ResourceId.wrap(keccak256("somekey")); bytes21[] memory hooks = new bytes21[](1); hooks[0] = bytes21("some data"); @@ -75,7 +76,7 @@ contract StoreHooksTest is Test, GasReporter, StoreMock { } function testOneSlot() public { - bytes32 key1 = keccak256("somekey"); + ResourceId key1 = ResourceId.wrap(keccak256("somekey")); bytes21[] memory hooks = new bytes21[](1); hooks[0] = bytes21("some data"); @@ -85,7 +86,7 @@ contract StoreHooksTest is Test, GasReporter, StoreMock { } function testTwoSlots() public { - bytes32 key2 = keccak256("somekey"); + ResourceId key2 = ResourceId.wrap(keccak256("somekey")); bytes21[] memory hooks = new bytes21[](2); hooks[0] = bytes21("some data"); hooks[1] = bytes21("some other data"); @@ -96,7 +97,7 @@ contract StoreHooksTest is Test, GasReporter, StoreMock { } function testThreeSlots() public { - bytes32 key3 = keccak256("somekey"); + ResourceId key3 = ResourceId.wrap(keccak256("somekey")); bytes21[] memory hooks = new bytes21[](3); hooks[0] = bytes21("some data"); hooks[1] = bytes21("some other data"); diff --git a/packages/store/test/tables/StoreHooksColdLoad.t.sol b/packages/store/test/tables/StoreHooksColdLoad.t.sol index f706166a34..3a9ca2e686 100644 --- a/packages/store/test/tables/StoreHooksColdLoad.t.sol +++ b/packages/store/test/tables/StoreHooksColdLoad.t.sol @@ -3,6 +3,7 @@ pragma solidity >=0.8.21; import { Test } from "forge-std/Test.sol"; import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol"; +import { ResourceId } from "../../src/ResourceId.sol"; import { StoreMock } from "../../test/StoreMock.sol"; import { StoreHooks } from "../../src/codegen/index.sol"; @@ -11,7 +12,7 @@ contract StoreHooksColdLoadTest is Test, GasReporter, StoreMock { function setUp() public { // StoreHooks schema is already registered by StoreCore - bytes32 key = keccak256("somekey"); + ResourceId key = ResourceId.wrap(keccak256("somekey")); hooks = new bytes21[](1); hooks[0] = bytes21("some data"); @@ -20,7 +21,7 @@ contract StoreHooksColdLoadTest is Test, GasReporter, StoreMock { } function testGet() public { - bytes32 key = keccak256("somekey"); + ResourceId key = ResourceId.wrap(keccak256("somekey")); startGasReport("StoreHooks: get field (cold)"); bytes21[] memory returnedAddresses = StoreHooks.get(key); @@ -31,7 +32,7 @@ contract StoreHooksColdLoadTest is Test, GasReporter, StoreMock { } function testLength() public { - bytes32 key = keccak256("somekey"); + ResourceId key = ResourceId.wrap(keccak256("somekey")); startGasReport("StoreHooks: get length (cold)"); uint256 length = StoreHooks.length(key); @@ -41,7 +42,7 @@ contract StoreHooksColdLoadTest is Test, GasReporter, StoreMock { } function testGetItem() public { - bytes32 key = keccak256("somekey"); + ResourceId key = ResourceId.wrap(keccak256("somekey")); startGasReport("StoreHooks: get 1 element (cold)"); bytes21 returnedAddress = StoreHooks.getItem(key, 0); @@ -51,7 +52,7 @@ contract StoreHooksColdLoadTest is Test, GasReporter, StoreMock { } function testPop() public { - bytes32 key = keccak256("somekey"); + ResourceId key = ResourceId.wrap(keccak256("somekey")); startGasReport("StoreHooks: pop 1 element (cold)"); StoreHooks.pop(key); @@ -63,7 +64,7 @@ contract StoreHooksColdLoadTest is Test, GasReporter, StoreMock { } function testUpdate() public { - bytes32 key = keccak256("somekey"); + ResourceId key = ResourceId.wrap(keccak256("somekey")); bytes21 newAddress = bytes21(bytes20(keccak256("alice"))); startGasReport("StoreHooks: update 1 element (cold)"); @@ -76,7 +77,7 @@ contract StoreHooksColdLoadTest is Test, GasReporter, StoreMock { } function testDelete() public { - bytes32 key = keccak256("somekey"); + ResourceId key = ResourceId.wrap(keccak256("somekey")); startGasReport("StoreHooks: delete record (cold)"); StoreHooks.deleteRecord(key); diff --git a/packages/store/ts/config/storeConfig.ts b/packages/store/ts/config/storeConfig.ts index cd8959311f..d096608d1b 100644 --- a/packages/store/ts/config/storeConfig.ts +++ b/packages/store/ts/config/storeConfig.ts @@ -23,6 +23,7 @@ import { } from "@latticexyz/config"; import { DEFAULTS, PATH_DEFAULTS, TABLE_DEFAULTS } from "./defaults"; import { UserType } from "@latticexyz/common/codegen"; +import { SchemaAbiType } from "@latticexyz/schema-type"; const zTableName = zObjectName; const zKeyName = zValueName; @@ -70,6 +71,25 @@ const zShorthandSchemaConfig = zFieldData.transform((fieldData) => { export const zSchemaConfig = zFullSchemaConfig.or(zShorthandSchemaConfig); +type ResolvedSchema, TUserTypes extends Record> = { + [key in keyof TSchema]: TSchema[key] extends keyof TUserTypes + ? TUserTypes[TSchema[key]]["internalType"] + : TSchema[key]; +}; + +// TODO: add strong types to UserTypes config and use them here +// (see https://github.com/latticexyz/mud/pull/1588) +export function resolveUserTypes, TUserTypes extends Record>( + schema: TSchema, + userTypes: TUserTypes +): ResolvedSchema { + const resolvedSchema: Record = {}; + for (const [key, value] of Object.entries(schema)) { + resolvedSchema[key] = (userTypes[value]?.internalType as SchemaAbiType) ?? value; + } + return resolvedSchema as ResolvedSchema; +} + /************************************************************************ * * TABLE diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json index ca76f2c34a..2dc7d4e7a6 100644 --- a/packages/world/gas-report.json +++ b/packages/world/gas-report.json @@ -29,6 +29,18 @@ "name": "AccessControl: requireAccess (warm)", "gasUsed": 3030 }, + { + "file": "test/AccessControl.t.sol", + "test": "testRequireOwner", + "name": "AccessControl: requireOwner (cold)", + "gasUsed": 3325 + }, + { + "file": "test/AccessControl.t.sol", + "test": "testRequireOwner", + "name": "AccessControl: requireOwner (warm)", + "gasUsed": 1326 + }, { "file": "test/CallBatch.t.sol", "test": "testCallBatchReturnData", @@ -231,7 +243,7 @@ "file": "test/StandardDelegationsModule.t.sol", "test": "testCallFromCallboundDelegation", "name": "register a callbound delegation", - "gasUsed": 113124 + "gasUsed": 112955 }, { "file": "test/StandardDelegationsModule.t.sol", @@ -243,7 +255,7 @@ "file": "test/StandardDelegationsModule.t.sol", "test": "testCallFromTimeboundDelegation", "name": "register a timebound delegation", - "gasUsed": 107619 + "gasUsed": 107450 }, { "file": "test/StandardDelegationsModule.t.sol", @@ -255,7 +267,7 @@ "file": "test/UniqueEntityModule.t.sol", "test": "testInstall", "name": "install unique entity module", - "gasUsed": 676007 + "gasUsed": 676006 }, { "file": "test/UniqueEntityModule.t.sol", @@ -267,7 +279,7 @@ "file": "test/UniqueEntityModule.t.sol", "test": "testInstallRoot", "name": "installRoot unique entity module", - "gasUsed": 643281 + "gasUsed": 643280 }, { "file": "test/UniqueEntityModule.t.sol", @@ -285,7 +297,7 @@ "file": "test/World.t.sol", "test": "testCallFromUnlimitedDelegation", "name": "register an unlimited delegation", - "gasUsed": 49845 + "gasUsed": 47676 }, { "file": "test/World.t.sol", @@ -327,7 +339,7 @@ "file": "test/World.t.sol", "test": "testRegisterSystem", "name": "register a system", - "gasUsed": 162836 + "gasUsed": 162835 }, { "file": "test/World.t.sol", diff --git a/packages/world/mud.config.ts b/packages/world/mud.config.ts index 9ce2191b4b..b7468bff86 100644 --- a/packages/world/mud.config.ts +++ b/packages/world/mud.config.ts @@ -5,6 +5,9 @@ export default mudConfig({ worldgenDirectory: "interfaces", worldInterfaceName: "IBaseWorld", codegenDirectory: "", + userTypes: { + ResourceId: { filePath: "@latticexyz/store/src/ResourceId.sol", internalType: "bytes32" }, + }, tables: { /************************************************************************ * @@ -13,7 +16,7 @@ export default mudConfig({ ************************************************************************/ NamespaceOwner: { keySchema: { - namespaceId: "bytes32", + namespaceId: "ResourceId", }, valueSchema: { owner: "address", @@ -21,7 +24,7 @@ export default mudConfig({ }, ResourceAccess: { keySchema: { - resourceId: "bytes32", + resourceId: "ResourceId", caller: "address", }, valueSchema: { @@ -43,7 +46,7 @@ export default mudConfig({ delegatee: "address", }, valueSchema: { - delegationControlId: "bytes32", + delegationControlId: "ResourceId", }, }, /************************************************************************ @@ -54,7 +57,7 @@ export default mudConfig({ Balances: { directory: "modules/core/tables", keySchema: { - namespaceId: "bytes32", + namespaceId: "ResourceId", }, valueSchema: { balance: "uint256", @@ -63,7 +66,7 @@ export default mudConfig({ Systems: { directory: "modules/core/tables", keySchema: { - systemId: "bytes32", + systemId: "ResourceId", }, valueSchema: { system: "address", @@ -77,13 +80,13 @@ export default mudConfig({ system: "address", }, valueSchema: { - systemId: "bytes32", + systemId: "ResourceId", }, }, SystemHooks: { directory: "modules/core/tables", keySchema: { - systemId: "bytes32", + systemId: "ResourceId", }, valueSchema: "bytes21[]", }, @@ -93,7 +96,7 @@ export default mudConfig({ functionSelector: "bytes4", }, valueSchema: { - systemId: "bytes32", + systemId: "ResourceId", systemFunctionSelector: "bytes4", }, dataStruct: false, @@ -120,7 +123,7 @@ export default mudConfig({ }, KeysInTable: { directory: "modules/keysintable/tables", - keySchema: { sourceTable: "bytes32" }, + keySchema: { sourceTableId: "ResourceId" }, valueSchema: { keys0: "bytes32[]", keys1: "bytes32[]", @@ -132,7 +135,7 @@ export default mudConfig({ UsedKeysIndex: { directory: "modules/keysintable/tables", keySchema: { - sourceTable: "bytes32", + sourceTableId: "ResourceId", keysHash: "bytes32", }, valueSchema: { has: "bool", index: "uint40" }, @@ -150,7 +153,7 @@ export default mudConfig({ keySchema: { delegator: "address", delegatee: "address", - systemId: "bytes32", + systemId: "ResourceId", callDataHash: "bytes32", }, valueSchema: { diff --git a/packages/world/src/AccessControl.sol b/packages/world/src/AccessControl.sol index 2b57c5f581..e1fe7879cc 100644 --- a/packages/world/src/AccessControl.sol +++ b/packages/world/src/AccessControl.sol @@ -15,12 +15,12 @@ library AccessControl { */ function hasAccess(ResourceId resourceId, address caller) internal view returns (bool) { return - ResourceAccess._get(ResourceId.unwrap(resourceId.getNamespaceId()), caller) || // First check access based on the namespace - ResourceAccess._get(ResourceId.unwrap(resourceId), caller); // If caller has no namespace access, check access on the name + // First check access based on the namespace. If caller has no namespace access, check access on the resource. + ResourceAccess._get(resourceId.getNamespaceId(), caller) || ResourceAccess._get(resourceId, caller); } /** - * Check for access at the given namespace or name. + * Check for access at the given namespace or resource. * Reverts with World_AccessDenied if the caller has no access. */ function requireAccess(ResourceId resourceId, address caller) internal view { @@ -35,7 +35,7 @@ library AccessControl { * Reverts with World_AccessDenied if the check fails. */ function requireOwner(ResourceId resourceId, address caller) internal view { - if (NamespaceOwner._get(ResourceId.unwrap(resourceId.getNamespaceId())) != caller) { + if (NamespaceOwner._get(resourceId.getNamespaceId()) != caller) { revert IWorldErrors.World_AccessDenied(resourceId.toString(), caller); } } diff --git a/packages/world/src/Delegation.sol b/packages/world/src/Delegation.sol index 78b6ba4f4e..34ce243cc7 100644 --- a/packages/world/src/Delegation.sol +++ b/packages/world/src/Delegation.sol @@ -1,26 +1,22 @@ // SPDX-License-Identifier: MIT pragma solidity >=0.8.21; +import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; import { UNLIMITED_DELEGATION } from "./constants.sol"; import { IDelegationControl } from "./interfaces/IDelegationControl.sol"; import { SystemCall } from "./SystemCall.sol"; -import { ResourceId } from "./WorldResourceId.sol"; -type Delegation is bytes32; - -using DelegationInstance for Delegation global; - -library DelegationInstance { - function exists(Delegation self) internal pure returns (bool) { - return Delegation.unwrap(self) != bytes32(""); +library Delegation { + function exists(ResourceId delegationControlId) internal pure returns (bool) { + return ResourceId.unwrap(delegationControlId) != bytes32(""); } - function isUnlimited(Delegation self) internal pure returns (bool) { - return Delegation.unwrap(self) == ResourceId.unwrap(UNLIMITED_DELEGATION); + function isUnlimited(ResourceId delegationControlId) internal pure returns (bool) { + return ResourceId.unwrap(delegationControlId) == ResourceId.unwrap(UNLIMITED_DELEGATION); } - function isLimited(Delegation self) internal pure returns (bool) { - return exists(self) && !isUnlimited(self); + function isLimited(ResourceId delegationControlId) internal pure returns (bool) { + return exists(delegationControlId) && !isUnlimited(delegationControlId); } /** @@ -29,22 +25,22 @@ library DelegationInstance { * Note: verifying the delegation might have side effects in the delegation control contract. */ function verify( - Delegation self, + ResourceId delegationControlId, address delegator, address delegatee, ResourceId systemId, bytes memory callData ) internal returns (bool) { // Early return if there is an unlimited delegation - if (isUnlimited(self)) return true; + if (isUnlimited(delegationControlId)) return true; // Early return if there is no valid delegation - if (!exists(self)) return false; + if (!exists(delegationControlId)) return false; // Call the delegation control contract to check if the delegator has granted access to the delegatee (bool success, bytes memory data) = SystemCall.call({ caller: delegatee, - systemId: ResourceId.wrap(Delegation.unwrap(self)), + systemId: delegationControlId, callData: abi.encodeCall(IDelegationControl.verify, (delegator, systemId, callData)), value: 0 }); diff --git a/packages/world/src/SystemCall.sol b/packages/world/src/SystemCall.sol index 0a241c86eb..10c68e76af 100644 --- a/packages/world/src/SystemCall.sol +++ b/packages/world/src/SystemCall.sol @@ -33,7 +33,7 @@ library SystemCall { bytes memory callData ) internal returns (bool success, bytes memory data) { // Load the system data - (address systemAddress, bool publicAccess) = Systems._get(ResourceId.unwrap(systemId)); + (address systemAddress, bool publicAccess) = Systems._get(systemId); // Check if the system exists if (systemAddress == address(0)) revert IWorldErrors.World_ResourceNotFound(systemId, systemId.toString()); @@ -44,8 +44,8 @@ library SystemCall { // If the msg.value is non-zero, update the namespace's balance if (value > 0) { ResourceId namespaceId = systemId.getNamespaceId(); - uint256 currentBalance = Balances._get(ResourceId.unwrap(namespaceId)); - Balances._set(ResourceId.unwrap(namespaceId), currentBalance + value); + uint256 currentBalance = Balances._get(namespaceId); + Balances._set(namespaceId, currentBalance + value); } // Call the system and forward any return data @@ -75,7 +75,7 @@ library SystemCall { uint256 value ) internal returns (bool success, bytes memory data) { // Get system hooks - bytes21[] memory hooks = SystemHooks._get(ResourceId.unwrap(systemId)); + bytes21[] memory hooks = SystemHooks._get(systemId); // Call onBeforeCallSystem hooks (before calling the system) for (uint256 i; i < hooks.length; i++) { diff --git a/packages/world/src/Utils.sol b/packages/world/src/Utils.sol index 973fc8beb0..5e8b16fb31 100644 --- a/packages/world/src/Utils.sol +++ b/packages/world/src/Utils.sol @@ -19,7 +19,7 @@ library Utils { if (StoreSwitch.getStoreAddress() == address(this)) { return ""; } else { - ResourceId systemId = ResourceId.wrap(SystemRegistry.get(address(this))); + ResourceId systemId = SystemRegistry.get(address(this)); return systemId.getNamespace(); } } diff --git a/packages/world/src/World.sol b/packages/world/src/World.sol index 5cead664e8..d93d4357b6 100644 --- a/packages/world/src/World.sol +++ b/packages/world/src/World.sol @@ -37,6 +37,7 @@ import { CORE_MODULE_NAME } from "./modules/core/constants.sol"; contract World is StoreRead, IStoreData, IWorldKernel { using WorldResourceIdInstance for ResourceId; + address public immutable creator; function worldVersion() public pure returns (bytes32) { @@ -302,16 +303,16 @@ contract World is StoreRead, IStoreData, IWorldKernel { } // Check if there is an explicit authorization for this caller to perform actions on behalf of the delegator - Delegation explicitDelegation = Delegation.wrap(Delegations._get({ delegator: delegator, delegatee: msg.sender })); + ResourceId explicitDelegationId = Delegations._get({ delegator: delegator, delegatee: msg.sender }); - if (explicitDelegation.verify(delegator, msg.sender, systemId, callData)) { + if (Delegation.verify(explicitDelegationId, delegator, msg.sender, systemId, callData)) { // forward the call as `delegator` return SystemCall.callWithHooksOrRevert(delegator, systemId, callData, msg.value); } // Check if the delegator has a fallback delegation control set - Delegation fallbackDelegation = Delegation.wrap(Delegations._get({ delegator: delegator, delegatee: address(0) })); - if (fallbackDelegation.verify(delegator, msg.sender, systemId, callData)) { + ResourceId fallbackDelegationId = Delegations._get({ delegator: delegator, delegatee: address(0) }); + if (Delegation.verify(fallbackDelegationId, delegator, msg.sender, systemId, callData)) { // forward the call with `from` as `msgSender` return SystemCall.callWithHooksOrRevert(delegator, systemId, callData, msg.value); } @@ -329,28 +330,23 @@ contract World is StoreRead, IStoreData, IWorldKernel { * ETH sent to the World without calldata is added to the root namespace's balance */ receive() external payable { - uint256 rootBalance = Balances._get(ResourceId.unwrap(ROOT_NAMESPACE_ID)); - Balances._set(ResourceId.unwrap(ROOT_NAMESPACE_ID), rootBalance + msg.value); + uint256 rootBalance = Balances._get(ROOT_NAMESPACE_ID); + Balances._set(ROOT_NAMESPACE_ID, rootBalance + msg.value); } /** * Fallback function to call registered function selectors */ fallback() external payable requireNoCallback { - (bytes32 systemId, bytes4 systemFunctionSelector) = FunctionSelectors._get(msg.sig); + (ResourceId systemId, bytes4 systemFunctionSelector) = FunctionSelectors._get(msg.sig); - if (systemId == 0) revert World_FunctionSelectorNotFound(msg.sig); + if (ResourceId.unwrap(systemId) == 0) revert World_FunctionSelectorNotFound(msg.sig); // Replace function selector in the calldata with the system function selector bytes memory callData = Bytes.setBytes4(msg.data, 0, systemFunctionSelector); // Call the function and forward the call data - bytes memory returnData = SystemCall.callWithHooksOrRevert( - msg.sender, - ResourceId.wrap(systemId), - callData, - msg.value - ); + bytes memory returnData = SystemCall.callWithHooksOrRevert(msg.sender, systemId, callData, msg.value); // If the call was successful, return the return data assembly { diff --git a/packages/world/src/modules/core/CoreModule.sol b/packages/world/src/modules/core/CoreModule.sol index a2dc4333ea..b167f416ac 100644 --- a/packages/world/src/modules/core/CoreModule.sol +++ b/packages/world/src/modules/core/CoreModule.sol @@ -74,9 +74,9 @@ contract CoreModule is Module { SystemHooks.register(); SystemRegistry.register(); - ResourceIds._setExists(ResourceId.unwrap(ROOT_NAMESPACE_ID), true); - NamespaceOwner._set(ResourceId.unwrap(ROOT_NAMESPACE_ID), _msgSender()); - ResourceAccess._set(ResourceId.unwrap(ROOT_NAMESPACE_ID), _msgSender(), true); + ResourceIds._setExists(ROOT_NAMESPACE_ID, true); + NamespaceOwner._set(ROOT_NAMESPACE_ID, _msgSender()); + ResourceAccess._set(ROOT_NAMESPACE_ID, _msgSender(), true); } /** diff --git a/packages/world/src/modules/core/implementations/AccessManagementSystem.sol b/packages/world/src/modules/core/implementations/AccessManagementSystem.sol index 6a07d956eb..6a3f8317cd 100644 --- a/packages/world/src/modules/core/implementations/AccessManagementSystem.sol +++ b/packages/world/src/modules/core/implementations/AccessManagementSystem.sol @@ -22,7 +22,7 @@ contract AccessManagementSystem is System { AccessControl.requireOwner(resourceId, _msgSender()); // Grant access to the given resource - ResourceAccess._set(ResourceId.unwrap(resourceId), grantee, true); + ResourceAccess._set(resourceId, grantee, true); } /** @@ -34,7 +34,7 @@ contract AccessManagementSystem is System { AccessControl.requireOwner(resourceId, _msgSender()); // Revoke access from the given resource - ResourceAccess._deleteRecord(ResourceId.unwrap(resourceId), grantee); + ResourceAccess._deleteRecord(resourceId, grantee); } /** @@ -47,12 +47,12 @@ contract AccessManagementSystem is System { AccessControl.requireOwner(namespaceId, _msgSender()); // Set namespace new owner - NamespaceOwner._set(ResourceId.unwrap(namespaceId), newOwner); + NamespaceOwner._set(namespaceId, newOwner); // Revoke access from old owner - ResourceAccess._deleteRecord(ResourceId.unwrap(namespaceId), _msgSender()); + ResourceAccess._deleteRecord(namespaceId, _msgSender()); // Grant access to new owner - ResourceAccess._set(ResourceId.unwrap(namespaceId), newOwner, true); + ResourceAccess._set(namespaceId, newOwner, true); } } diff --git a/packages/world/src/modules/core/implementations/BalanceTransferSystem.sol b/packages/world/src/modules/core/implementations/BalanceTransferSystem.sol index 9bae047ac2..30d7165c74 100644 --- a/packages/world/src/modules/core/implementations/BalanceTransferSystem.sol +++ b/packages/world/src/modules/core/implementations/BalanceTransferSystem.sol @@ -33,14 +33,14 @@ contract BalanceTransferSystem is System, IWorldErrors { AccessControl.requireAccess(fromNamespaceId, _msgSender()); // Get current namespace balance - uint256 balance = Balances._get(ResourceId.unwrap(fromNamespaceId)); + uint256 balance = Balances._get(fromNamespaceId); // Require the balance balance to be greater or equal to the amount to transfer if (amount > balance) revert World_InsufficientBalance(balance, amount); // Update the balances - Balances._set(ResourceId.unwrap(fromNamespaceId), balance - amount); - Balances._set(ResourceId.unwrap(toNamespaceId), Balances._get(ResourceId.unwrap(toNamespaceId)) + amount); + Balances._set(fromNamespaceId, balance - amount); + Balances._set(toNamespaceId, Balances._get(toNamespaceId) + amount); } /** @@ -51,13 +51,13 @@ contract BalanceTransferSystem is System, IWorldErrors { AccessControl.requireAccess(fromNamespaceId, _msgSender()); // Get current namespace balance - uint256 balance = Balances._get(ResourceId.unwrap(fromNamespaceId)); + uint256 balance = Balances._get(fromNamespaceId); // Require the balance balance to be greater or equal to the amount to transfer if (amount > balance) revert World_InsufficientBalance(balance, amount); // Update the balances - Balances._set(ResourceId.unwrap(fromNamespaceId), balance - amount); + Balances._set(fromNamespaceId, balance - amount); // Transfer the balance to the given address, revert on failure (bool success, bytes memory data) = payable(toAddress).call{ value: amount }(""); diff --git a/packages/world/src/modules/core/implementations/StoreRegistrationSystem.sol b/packages/world/src/modules/core/implementations/StoreRegistrationSystem.sol index 65c20fc66d..b903392262 100644 --- a/packages/world/src/modules/core/implementations/StoreRegistrationSystem.sol +++ b/packages/world/src/modules/core/implementations/StoreRegistrationSystem.sol @@ -49,10 +49,10 @@ contract StoreRegistrationSystem is System, IWorldErrors { // If the namespace doesn't exist yet, register it ResourceId namespaceId = tableId.getNamespaceId(); - if (!ResourceIds._getExists(ResourceId.unwrap(namespaceId))) { + if (!ResourceIds._getExists(namespaceId)) { // Since this is a root system, we're in the context of the World contract already, // so we can use delegatecall to register the namespace - (address coreSystemAddress, ) = Systems._get(ResourceId.unwrap(CORE_SYSTEM_ID)); + (address coreSystemAddress, ) = Systems._get(CORE_SYSTEM_ID); (bool success, bytes memory data) = coreSystemAddress.delegatecall( abi.encodeCall(WorldRegistrationSystem.registerNamespace, (namespaceId)) ); diff --git a/packages/world/src/modules/core/implementations/WorldRegistrationSystem.sol b/packages/world/src/modules/core/implementations/WorldRegistrationSystem.sol index 30e40316ea..25fadbab85 100644 --- a/packages/world/src/modules/core/implementations/WorldRegistrationSystem.sol +++ b/packages/world/src/modules/core/implementations/WorldRegistrationSystem.sol @@ -9,9 +9,10 @@ import { System } from "../../../System.sol"; import { WorldContextConsumer, WORLD_CONTEXT_CONSUMER_INTERFACE_ID } from "../../../WorldContext.sol"; import { WorldResourceIdLib, WorldResourceIdInstance } from "../../../WorldResourceId.sol"; import { SystemCall } from "../../../SystemCall.sol"; -import { ROOT_NAMESPACE_ID, ROOT_NAME, UNLIMITED_DELEGATION } from "../../../constants.sol"; +import { ROOT_NAMESPACE_ID, ROOT_NAME } from "../../../constants.sol"; import { RESOURCE_NAMESPACE, RESOURCE_SYSTEM } from "../../../worldResourceTypes.sol"; import { AccessControl } from "../../../AccessControl.sol"; +import { Delegation } from "../../../Delegation.sol"; import { requireInterface } from "../../../requireInterface.sol"; import { NamespaceOwner } from "../../../tables/NamespaceOwner.sol"; import { ResourceAccess } from "../../../tables/ResourceAccess.sol"; @@ -44,18 +45,18 @@ contract WorldRegistrationSystem is System, IWorldErrors { } // Require namespace to not exist yet - if (ResourceIds._getExists(ResourceId.unwrap(namespaceId))) { + if (ResourceIds._getExists(namespaceId)) { revert World_ResourceAlreadyExists(namespaceId, namespaceId.toString()); } // Register namespace resource ID - ResourceIds._setExists(ResourceId.unwrap(namespaceId), true); + ResourceIds._setExists(namespaceId, true); // Register caller as the namespace owner - NamespaceOwner._set(ResourceId.unwrap(namespaceId), _msgSender()); + NamespaceOwner._set(namespaceId, _msgSender()); // Give caller access to the new namespace - ResourceAccess._set(ResourceId.unwrap(namespaceId), _msgSender(), true); + ResourceAccess._set(namespaceId, _msgSender(), true); } /** @@ -69,10 +70,7 @@ contract WorldRegistrationSystem is System, IWorldErrors { AccessControl.requireOwner(systemId, _msgSender()); // Register the hook - SystemHooks.push( - ResourceId.unwrap(systemId), - Hook.unwrap(HookLib.encode(address(hookAddress), enabledHooksBitmap)) - ); + SystemHooks.push(systemId, Hook.unwrap(HookLib.encode(address(hookAddress), enabledHooksBitmap))); } /** @@ -108,14 +106,16 @@ contract WorldRegistrationSystem is System, IWorldErrors { if (systemId.getName() == ROOT_NAME) revert World_InvalidResourceId(systemId, systemId.toString()); // Require this system to not be registered at a different system ID yet - bytes32 existingSystemId = SystemRegistry._get(address(system)); - if (existingSystemId != 0 && existingSystemId != ResourceId.unwrap(systemId)) { + ResourceId existingSystemId = SystemRegistry._get(address(system)); + if ( + ResourceId.unwrap(existingSystemId) != 0 && ResourceId.unwrap(existingSystemId) != ResourceId.unwrap(systemId) + ) { revert World_SystemAlreadyExists(address(system)); } // If the namespace doesn't exist yet, register it ResourceId namespaceId = systemId.getNamespaceId(); - if (!ResourceIds._getExists(ResourceId.unwrap(namespaceId))) { + if (!ResourceIds._getExists(namespaceId)) { registerNamespace(namespaceId); } else { // otherwise require caller to own the namespace @@ -123,7 +123,7 @@ contract WorldRegistrationSystem is System, IWorldErrors { } // Check if a system already exists at this system ID - address existingSystem = Systems._getSystem(ResourceId.unwrap(systemId)); + address existingSystem = Systems._getSystem(systemId); // If there is an existing system with this system ID, remove it if (existingSystem != address(0)) { @@ -131,20 +131,20 @@ contract WorldRegistrationSystem is System, IWorldErrors { SystemRegistry._deleteRecord(existingSystem); // Remove the existing system's access to its namespace - ResourceAccess._deleteRecord(ResourceId.unwrap(namespaceId), existingSystem); + ResourceAccess._deleteRecord(namespaceId, existingSystem); } else { // Otherwise, this is a new system, so register its resource ID - ResourceIds._setExists(ResourceId.unwrap(systemId), true); + ResourceIds._setExists(systemId, true); } // Systems = mapping from system ID to system address and public access flag - Systems._set(ResourceId.unwrap(systemId), address(system), publicAccess); + Systems._set(systemId, address(system), publicAccess); // SystemRegistry = mapping from system address to system ID - SystemRegistry._set(address(system), ResourceId.unwrap(systemId)); + SystemRegistry._set(address(system), systemId); // Grant the system access to its namespace - ResourceAccess._set(ResourceId.unwrap(namespaceId), address(system), true); + ResourceAccess._set(namespaceId, address(system), true); } /** @@ -170,13 +170,13 @@ contract WorldRegistrationSystem is System, IWorldErrors { worldFunctionSelector = bytes4(keccak256(worldFunctionSignature)); // Require the function selector to be globally unique - bytes32 existingSystemId = FunctionSelectors._getSystemId(worldFunctionSelector); + ResourceId existingSystemId = FunctionSelectors._getSystemId(worldFunctionSelector); - if (existingSystemId != 0) revert World_FunctionSelectorAlreadyExists(worldFunctionSelector); + if (ResourceId.unwrap(existingSystemId) != 0) revert World_FunctionSelectorAlreadyExists(worldFunctionSelector); // Register the function selector bytes4 systemFunctionSelector = bytes4(keccak256(bytes(systemFunctionSignature))); - FunctionSelectors._set(worldFunctionSelector, ResourceId.unwrap(systemId), systemFunctionSelector); + FunctionSelectors._set(worldFunctionSelector, systemId, systemFunctionSelector); // Register the function signature for offchain use FunctionSignatures._set(worldFunctionSelector, string(worldFunctionSignature)); @@ -198,12 +198,12 @@ contract WorldRegistrationSystem is System, IWorldErrors { worldFunctionSelector = bytes4(keccak256(bytes(worldFunctionSignature))); // Require the function selector to be globally unique - bytes32 existingSystemId = FunctionSelectors._getSystemId(worldFunctionSelector); + ResourceId existingSystemId = FunctionSelectors._getSystemId(worldFunctionSelector); - if (existingSystemId != 0) revert World_FunctionSelectorAlreadyExists(worldFunctionSelector); + if (ResourceId.unwrap(existingSystemId) != 0) revert World_FunctionSelectorAlreadyExists(worldFunctionSelector); // Register the function selector - FunctionSelectors._set(worldFunctionSelector, ResourceId.unwrap(systemId), systemFunctionSelector); + FunctionSelectors._set(worldFunctionSelector, systemId, systemFunctionSelector); // Register the function signature for offchain use FunctionSignatures._set(worldFunctionSelector, worldFunctionSignature); @@ -214,16 +214,12 @@ contract WorldRegistrationSystem is System, IWorldErrors { */ function registerDelegation(address delegatee, ResourceId delegationControlId, bytes memory initCallData) public { // Store the delegation control contract address - Delegations.set({ - delegator: _msgSender(), - delegatee: delegatee, - delegationControlId: ResourceId.unwrap(delegationControlId) - }); - - // If the delegation is not unlimited... - if (ResourceId.unwrap(delegationControlId) != ResourceId.unwrap(UNLIMITED_DELEGATION) && initCallData.length > 0) { + Delegations._set({ delegator: _msgSender(), delegatee: delegatee, delegationControlId: delegationControlId }); + + // If the delegation is limited... + if (Delegation.isLimited(delegationControlId) && initCallData.length > 0) { // Require the delegationControl contract to implement the IDelegationControl interface - (address delegationControl, ) = Systems._get(ResourceId.unwrap(delegationControlId)); + (address delegationControl, ) = Systems._get(delegationControlId); requireInterface(delegationControl, DELEGATION_CONTROL_INTERFACE_ID); // Call the delegation control contract's init function diff --git a/packages/world/src/modules/core/tables/Balances.sol b/packages/world/src/modules/core/tables/Balances.sol index eef3ceabe6..b1397c6233 100644 --- a/packages/world/src/modules/core/tables/Balances.sol +++ b/packages/world/src/modules/core/tables/Balances.sol @@ -20,6 +20,9 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; import { RESOURCE_TABLE, RESOURCE_OFFCHAIN_TABLE } from "@latticexyz/store/src/storeResourceTypes.sol"; +// Import user types +import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; + ResourceId constant _tableId = ResourceId.wrap( bytes32(abi.encodePacked(RESOURCE_TABLE, bytes14(""), bytes16("Balances"))) ); @@ -79,127 +82,127 @@ library Balances { } /** Get balance */ - function getBalance(bytes32 namespaceId) internal view returns (uint256 balance) { + function getBalance(ResourceId namespaceId) internal view returns (uint256 balance) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (uint256(bytes32(_blob))); } /** Get balance */ - function _getBalance(bytes32 namespaceId) internal view returns (uint256 balance) { + function _getBalance(ResourceId namespaceId) internal view returns (uint256 balance) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (uint256(bytes32(_blob))); } /** Get balance (using the specified store) */ - function getBalance(IStore _store, bytes32 namespaceId) internal view returns (uint256 balance) { + function getBalance(IStore _store, ResourceId namespaceId) internal view returns (uint256 balance) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (uint256(bytes32(_blob))); } /** Get balance */ - function get(bytes32 namespaceId) internal view returns (uint256 balance) { + function get(ResourceId namespaceId) internal view returns (uint256 balance) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (uint256(bytes32(_blob))); } /** Get balance */ - function _get(bytes32 namespaceId) internal view returns (uint256 balance) { + function _get(ResourceId namespaceId) internal view returns (uint256 balance) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (uint256(bytes32(_blob))); } /** Get balance (using the specified store) */ - function get(IStore _store, bytes32 namespaceId) internal view returns (uint256 balance) { + function get(IStore _store, ResourceId namespaceId) internal view returns (uint256 balance) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (uint256(bytes32(_blob))); } /** Set balance */ - function setBalance(bytes32 namespaceId, uint256 balance) internal { + function setBalance(ResourceId namespaceId, uint256 balance) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((balance)), _fieldLayout); } /** Set balance */ - function _setBalance(bytes32 namespaceId, uint256 balance) internal { + function _setBalance(ResourceId namespaceId, uint256 balance) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((balance)), _fieldLayout); } /** Set balance (using the specified store) */ - function setBalance(IStore _store, bytes32 namespaceId, uint256 balance) internal { + function setBalance(IStore _store, ResourceId namespaceId, uint256 balance) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); _store.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((balance)), _fieldLayout); } /** Set balance */ - function set(bytes32 namespaceId, uint256 balance) internal { + function set(ResourceId namespaceId, uint256 balance) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((balance)), _fieldLayout); } /** Set balance */ - function _set(bytes32 namespaceId, uint256 balance) internal { + function _set(ResourceId namespaceId, uint256 balance) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((balance)), _fieldLayout); } /** Set balance (using the specified store) */ - function set(IStore _store, bytes32 namespaceId, uint256 balance) internal { + function set(IStore _store, ResourceId namespaceId, uint256 balance) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); _store.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((balance)), _fieldLayout); } /** Delete all data for given keys */ - function deleteRecord(bytes32 namespaceId) internal { + function deleteRecord(ResourceId namespaceId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); StoreSwitch.deleteRecord(_tableId, _keyTuple); } /** Delete all data for given keys */ - function _deleteRecord(bytes32 namespaceId) internal { + function _deleteRecord(ResourceId namespaceId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /** Delete all data for given keys (using the specified store) */ - function deleteRecord(IStore _store, bytes32 namespaceId) internal { + function deleteRecord(IStore _store, ResourceId namespaceId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); _store.deleteRecord(_tableId, _keyTuple); } @@ -220,9 +223,9 @@ library Balances { } /** Encode keys as a bytes32 array using this table's field layout */ - function encodeKeyTuple(bytes32 namespaceId) internal pure returns (bytes32[] memory) { + function encodeKeyTuple(ResourceId namespaceId) internal pure returns (bytes32[] memory) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); return _keyTuple; } diff --git a/packages/world/src/modules/core/tables/FunctionSelectors.sol b/packages/world/src/modules/core/tables/FunctionSelectors.sol index 9e8cdb6c2f..a7ce212804 100644 --- a/packages/world/src/modules/core/tables/FunctionSelectors.sol +++ b/packages/world/src/modules/core/tables/FunctionSelectors.sol @@ -20,6 +20,9 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; import { RESOURCE_TABLE, RESOURCE_OFFCHAIN_TABLE } from "@latticexyz/store/src/storeResourceTypes.sol"; +// Import user types +import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; + ResourceId constant _tableId = ResourceId.wrap( bytes32(abi.encodePacked(RESOURCE_TABLE, bytes14(""), bytes16("FunctionSelector"))) ); @@ -81,54 +84,54 @@ library FunctionSelectors { } /** Get systemId */ - function getSystemId(bytes4 functionSelector) internal view returns (bytes32 systemId) { + function getSystemId(bytes4 functionSelector) internal view returns (ResourceId systemId) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (bytes32(_blob)); + return ResourceId.wrap(bytes32(_blob)); } /** Get systemId */ - function _getSystemId(bytes4 functionSelector) internal view returns (bytes32 systemId) { + function _getSystemId(bytes4 functionSelector) internal view returns (ResourceId systemId) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (bytes32(_blob)); + return ResourceId.wrap(bytes32(_blob)); } /** Get systemId (using the specified store) */ - function getSystemId(IStore _store, bytes4 functionSelector) internal view returns (bytes32 systemId) { + function getSystemId(IStore _store, bytes4 functionSelector) internal view returns (ResourceId systemId) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (bytes32(_blob)); + return ResourceId.wrap(bytes32(_blob)); } /** Set systemId */ - function setSystemId(bytes4 functionSelector, bytes32 systemId) internal { + function setSystemId(bytes4 functionSelector, ResourceId systemId) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((systemId)), _fieldLayout); + StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked(ResourceId.unwrap(systemId)), _fieldLayout); } /** Set systemId */ - function _setSystemId(bytes4 functionSelector, bytes32 systemId) internal { + function _setSystemId(bytes4 functionSelector, ResourceId systemId) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((systemId)), _fieldLayout); + StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked(ResourceId.unwrap(systemId)), _fieldLayout); } /** Set systemId (using the specified store) */ - function setSystemId(IStore _store, bytes4 functionSelector, bytes32 systemId) internal { + function setSystemId(IStore _store, bytes4 functionSelector, ResourceId systemId) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - _store.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((systemId)), _fieldLayout); + _store.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked(ResourceId.unwrap(systemId)), _fieldLayout); } /** Get systemFunctionSelector */ @@ -186,7 +189,7 @@ library FunctionSelectors { } /** Get the full data */ - function get(bytes4 functionSelector) internal view returns (bytes32 systemId, bytes4 systemFunctionSelector) { + function get(bytes4 functionSelector) internal view returns (ResourceId systemId, bytes4 systemFunctionSelector) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); @@ -199,7 +202,7 @@ library FunctionSelectors { } /** Get the full data */ - function _get(bytes4 functionSelector) internal view returns (bytes32 systemId, bytes4 systemFunctionSelector) { + function _get(bytes4 functionSelector) internal view returns (ResourceId systemId, bytes4 systemFunctionSelector) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); @@ -215,7 +218,7 @@ library FunctionSelectors { function get( IStore _store, bytes4 functionSelector - ) internal view returns (bytes32 systemId, bytes4 systemFunctionSelector) { + ) internal view returns (ResourceId systemId, bytes4 systemFunctionSelector) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); @@ -228,7 +231,7 @@ library FunctionSelectors { } /** Set the full data using individual values */ - function set(bytes4 functionSelector, bytes32 systemId, bytes4 systemFunctionSelector) internal { + function set(bytes4 functionSelector, ResourceId systemId, bytes4 systemFunctionSelector) internal { bytes memory _staticData = encodeStatic(systemId, systemFunctionSelector); PackedCounter _encodedLengths; @@ -241,7 +244,7 @@ library FunctionSelectors { } /** Set the full data using individual values */ - function _set(bytes4 functionSelector, bytes32 systemId, bytes4 systemFunctionSelector) internal { + function _set(bytes4 functionSelector, ResourceId systemId, bytes4 systemFunctionSelector) internal { bytes memory _staticData = encodeStatic(systemId, systemFunctionSelector); PackedCounter _encodedLengths; @@ -254,7 +257,7 @@ library FunctionSelectors { } /** Set the full data using individual values (using the specified store) */ - function set(IStore _store, bytes4 functionSelector, bytes32 systemId, bytes4 systemFunctionSelector) internal { + function set(IStore _store, bytes4 functionSelector, ResourceId systemId, bytes4 systemFunctionSelector) internal { bytes memory _staticData = encodeStatic(systemId, systemFunctionSelector); PackedCounter _encodedLengths; @@ -270,8 +273,8 @@ library FunctionSelectors { * Decode the tightly packed blob of static data using this table's field layout * Undefined behaviour for invalid blobs */ - function decodeStatic(bytes memory _blob) internal pure returns (bytes32 systemId, bytes4 systemFunctionSelector) { - systemId = (Bytes.slice32(_blob, 0)); + function decodeStatic(bytes memory _blob) internal pure returns (ResourceId systemId, bytes4 systemFunctionSelector) { + systemId = ResourceId.wrap(Bytes.slice32(_blob, 0)); systemFunctionSelector = (Bytes.slice4(_blob, 32)); } @@ -284,7 +287,7 @@ library FunctionSelectors { bytes memory _staticData, PackedCounter, bytes memory - ) internal pure returns (bytes32 systemId, bytes4 systemFunctionSelector) { + ) internal pure returns (ResourceId systemId, bytes4 systemFunctionSelector) { (systemId, systemFunctionSelector) = decodeStatic(_staticData); } @@ -313,13 +316,13 @@ library FunctionSelectors { } /** Tightly pack static data using this table's schema */ - function encodeStatic(bytes32 systemId, bytes4 systemFunctionSelector) internal pure returns (bytes memory) { + function encodeStatic(ResourceId systemId, bytes4 systemFunctionSelector) internal pure returns (bytes memory) { return abi.encodePacked(systemId, systemFunctionSelector); } /** Tightly pack full data using this table's field layout */ function encode( - bytes32 systemId, + ResourceId systemId, bytes4 systemFunctionSelector ) internal pure returns (bytes memory, PackedCounter, bytes memory) { bytes memory _staticData = encodeStatic(systemId, systemFunctionSelector); diff --git a/packages/world/src/modules/core/tables/SystemHooks.sol b/packages/world/src/modules/core/tables/SystemHooks.sol index 28ca84f61c..d2ddbfcdae 100644 --- a/packages/world/src/modules/core/tables/SystemHooks.sol +++ b/packages/world/src/modules/core/tables/SystemHooks.sol @@ -20,6 +20,9 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; import { RESOURCE_TABLE, RESOURCE_OFFCHAIN_TABLE } from "@latticexyz/store/src/storeResourceTypes.sol"; +// Import user types +import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; + ResourceId constant _tableId = ResourceId.wrap( bytes32(abi.encodePacked(RESOURCE_TABLE, bytes14(""), bytes16("SystemHooks"))) ); @@ -79,111 +82,111 @@ library SystemHooks { } /** Get value */ - function getValue(bytes32 systemId) internal view returns (bytes21[] memory value) { + function getValue(ResourceId systemId) internal view returns (bytes21[] memory value) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); bytes memory _blob = StoreSwitch.getDynamicField(_tableId, _keyTuple, 0); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } /** Get value */ - function _getValue(bytes32 systemId) internal view returns (bytes21[] memory value) { + function _getValue(ResourceId systemId) internal view returns (bytes21[] memory value) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } /** Get value (using the specified store) */ - function getValue(IStore _store, bytes32 systemId) internal view returns (bytes21[] memory value) { + function getValue(IStore _store, ResourceId systemId) internal view returns (bytes21[] memory value) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); bytes memory _blob = _store.getDynamicField(_tableId, _keyTuple, 0); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } /** Get value */ - function get(bytes32 systemId) internal view returns (bytes21[] memory value) { + function get(ResourceId systemId) internal view returns (bytes21[] memory value) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); bytes memory _blob = StoreSwitch.getDynamicField(_tableId, _keyTuple, 0); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } /** Get value */ - function _get(bytes32 systemId) internal view returns (bytes21[] memory value) { + function _get(ResourceId systemId) internal view returns (bytes21[] memory value) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } /** Get value (using the specified store) */ - function get(IStore _store, bytes32 systemId) internal view returns (bytes21[] memory value) { + function get(IStore _store, ResourceId systemId) internal view returns (bytes21[] memory value) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); bytes memory _blob = _store.getDynamicField(_tableId, _keyTuple, 0); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } /** Set value */ - function setValue(bytes32 systemId, bytes21[] memory value) internal { + function setValue(ResourceId systemId, bytes21[] memory value) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); StoreSwitch.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((value))); } /** Set value */ - function _setValue(bytes32 systemId, bytes21[] memory value) internal { + function _setValue(ResourceId systemId, bytes21[] memory value) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); StoreCore.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((value))); } /** Set value (using the specified store) */ - function setValue(IStore _store, bytes32 systemId, bytes21[] memory value) internal { + function setValue(IStore _store, ResourceId systemId, bytes21[] memory value) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); _store.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((value))); } /** Set value */ - function set(bytes32 systemId, bytes21[] memory value) internal { + function set(ResourceId systemId, bytes21[] memory value) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); StoreSwitch.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((value))); } /** Set value */ - function _set(bytes32 systemId, bytes21[] memory value) internal { + function _set(ResourceId systemId, bytes21[] memory value) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); StoreCore.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((value))); } /** Set value (using the specified store) */ - function set(IStore _store, bytes32 systemId, bytes21[] memory value) internal { + function set(IStore _store, ResourceId systemId, bytes21[] memory value) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); _store.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((value))); } /** Get the length of value */ - function lengthValue(bytes32 systemId) internal view returns (uint256) { + function lengthValue(ResourceId systemId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -192,9 +195,9 @@ library SystemHooks { } /** Get the length of value */ - function _lengthValue(bytes32 systemId) internal view returns (uint256) { + function _lengthValue(ResourceId systemId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -203,9 +206,9 @@ library SystemHooks { } /** Get the length of value (using the specified store) */ - function lengthValue(IStore _store, bytes32 systemId) internal view returns (uint256) { + function lengthValue(IStore _store, ResourceId systemId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); uint256 _byteLength = _store.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -214,9 +217,9 @@ library SystemHooks { } /** Get the length of value */ - function length(bytes32 systemId) internal view returns (uint256) { + function length(ResourceId systemId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -225,9 +228,9 @@ library SystemHooks { } /** Get the length of value */ - function _length(bytes32 systemId) internal view returns (uint256) { + function _length(ResourceId systemId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -236,9 +239,9 @@ library SystemHooks { } /** Get the length of value (using the specified store) */ - function length(IStore _store, bytes32 systemId) internal view returns (uint256) { + function length(IStore _store, ResourceId systemId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); uint256 _byteLength = _store.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -250,9 +253,9 @@ library SystemHooks { * Get an item of value * (unchecked, returns invalid data if index overflows) */ - function getItemValue(bytes32 systemId, uint256 _index) internal view returns (bytes21) { + function getItemValue(ResourceId systemId, uint256 _index) internal view returns (bytes21) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); unchecked { bytes memory _blob = StoreSwitch.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 21, (_index + 1) * 21); @@ -264,9 +267,9 @@ library SystemHooks { * Get an item of value * (unchecked, returns invalid data if index overflows) */ - function _getItemValue(bytes32 systemId, uint256 _index) internal view returns (bytes21) { + function _getItemValue(ResourceId systemId, uint256 _index) internal view returns (bytes21) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); unchecked { bytes memory _blob = StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 21, (_index + 1) * 21); @@ -278,9 +281,9 @@ library SystemHooks { * Get an item of value (using the specified store) * (unchecked, returns invalid data if index overflows) */ - function getItemValue(IStore _store, bytes32 systemId, uint256 _index) internal view returns (bytes21) { + function getItemValue(IStore _store, ResourceId systemId, uint256 _index) internal view returns (bytes21) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); unchecked { bytes memory _blob = _store.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 21, (_index + 1) * 21); @@ -292,9 +295,9 @@ library SystemHooks { * Get an item of value * (unchecked, returns invalid data if index overflows) */ - function getItem(bytes32 systemId, uint256 _index) internal view returns (bytes21) { + function getItem(ResourceId systemId, uint256 _index) internal view returns (bytes21) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); unchecked { bytes memory _blob = StoreSwitch.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 21, (_index + 1) * 21); @@ -306,9 +309,9 @@ library SystemHooks { * Get an item of value * (unchecked, returns invalid data if index overflows) */ - function _getItem(bytes32 systemId, uint256 _index) internal view returns (bytes21) { + function _getItem(ResourceId systemId, uint256 _index) internal view returns (bytes21) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); unchecked { bytes memory _blob = StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 21, (_index + 1) * 21); @@ -320,9 +323,9 @@ library SystemHooks { * Get an item of value (using the specified store) * (unchecked, returns invalid data if index overflows) */ - function getItem(IStore _store, bytes32 systemId, uint256 _index) internal view returns (bytes21) { + function getItem(IStore _store, ResourceId systemId, uint256 _index) internal view returns (bytes21) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); unchecked { bytes memory _blob = _store.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 21, (_index + 1) * 21); @@ -331,97 +334,97 @@ library SystemHooks { } /** Push an element to value */ - function pushValue(bytes32 systemId, bytes21 _element) internal { + function pushValue(ResourceId systemId, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); } /** Push an element to value */ - function _pushValue(bytes32 systemId, bytes21 _element) internal { + function _pushValue(ResourceId systemId, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); StoreCore.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); } /** Push an element to value (using the specified store) */ - function pushValue(IStore _store, bytes32 systemId, bytes21 _element) internal { + function pushValue(IStore _store, ResourceId systemId, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); _store.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); } /** Push an element to value */ - function push(bytes32 systemId, bytes21 _element) internal { + function push(ResourceId systemId, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); } /** Push an element to value */ - function _push(bytes32 systemId, bytes21 _element) internal { + function _push(ResourceId systemId, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); StoreCore.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); } /** Push an element to value (using the specified store) */ - function push(IStore _store, bytes32 systemId, bytes21 _element) internal { + function push(IStore _store, ResourceId systemId, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); _store.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); } /** Pop an element from value */ - function popValue(bytes32 systemId) internal { + function popValue(ResourceId systemId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 0, 21); } /** Pop an element from value */ - function _popValue(bytes32 systemId) internal { + function _popValue(ResourceId systemId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); StoreCore.popFromDynamicField(_tableId, _keyTuple, 0, 21); } /** Pop an element from value (using the specified store) */ - function popValue(IStore _store, bytes32 systemId) internal { + function popValue(IStore _store, ResourceId systemId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); _store.popFromDynamicField(_tableId, _keyTuple, 0, 21); } /** Pop an element from value */ - function pop(bytes32 systemId) internal { + function pop(ResourceId systemId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 0, 21); } /** Pop an element from value */ - function _pop(bytes32 systemId) internal { + function _pop(ResourceId systemId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); StoreCore.popFromDynamicField(_tableId, _keyTuple, 0, 21); } /** Pop an element from value (using the specified store) */ - function pop(IStore _store, bytes32 systemId) internal { + function pop(IStore _store, ResourceId systemId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); _store.popFromDynamicField(_tableId, _keyTuple, 0, 21); } @@ -430,9 +433,9 @@ library SystemHooks { * Update an element of value at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function updateValue(bytes32 systemId, uint256 _index, bytes21 _element) internal { + function updateValue(ResourceId systemId, uint256 _index, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -444,9 +447,9 @@ library SystemHooks { * Update an element of value at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function _updateValue(bytes32 systemId, uint256 _index, bytes21 _element) internal { + function _updateValue(ResourceId systemId, uint256 _index, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -458,9 +461,9 @@ library SystemHooks { * Update an element of value (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function updateValue(IStore _store, bytes32 systemId, uint256 _index, bytes21 _element) internal { + function updateValue(IStore _store, ResourceId systemId, uint256 _index, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -472,9 +475,9 @@ library SystemHooks { * Update an element of value at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function update(bytes32 systemId, uint256 _index, bytes21 _element) internal { + function update(ResourceId systemId, uint256 _index, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -486,9 +489,9 @@ library SystemHooks { * Update an element of value at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function _update(bytes32 systemId, uint256 _index, bytes21 _element) internal { + function _update(ResourceId systemId, uint256 _index, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -500,9 +503,9 @@ library SystemHooks { * Update an element of value (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function update(IStore _store, bytes32 systemId, uint256 _index, bytes21 _element) internal { + function update(IStore _store, ResourceId systemId, uint256 _index, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -511,25 +514,25 @@ library SystemHooks { } /** Delete all data for given keys */ - function deleteRecord(bytes32 systemId) internal { + function deleteRecord(ResourceId systemId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); StoreSwitch.deleteRecord(_tableId, _keyTuple); } /** Delete all data for given keys */ - function _deleteRecord(bytes32 systemId) internal { + function _deleteRecord(ResourceId systemId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /** Delete all data for given keys (using the specified store) */ - function deleteRecord(IStore _store, bytes32 systemId) internal { + function deleteRecord(IStore _store, ResourceId systemId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); _store.deleteRecord(_tableId, _keyTuple); } @@ -557,9 +560,9 @@ library SystemHooks { } /** Encode keys as a bytes32 array using this table's field layout */ - function encodeKeyTuple(bytes32 systemId) internal pure returns (bytes32[] memory) { + function encodeKeyTuple(ResourceId systemId) internal pure returns (bytes32[] memory) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); return _keyTuple; } diff --git a/packages/world/src/modules/core/tables/SystemRegistry.sol b/packages/world/src/modules/core/tables/SystemRegistry.sol index b68751af45..9de98f6acb 100644 --- a/packages/world/src/modules/core/tables/SystemRegistry.sol +++ b/packages/world/src/modules/core/tables/SystemRegistry.sol @@ -20,6 +20,9 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; import { RESOURCE_TABLE, RESOURCE_OFFCHAIN_TABLE } from "@latticexyz/store/src/storeResourceTypes.sol"; +// Import user types +import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; + ResourceId constant _tableId = ResourceId.wrap( bytes32(abi.encodePacked(RESOURCE_TABLE, bytes14(""), bytes16("SystemRegistry"))) ); @@ -79,105 +82,105 @@ library SystemRegistry { } /** Get systemId */ - function getSystemId(address system) internal view returns (bytes32 systemId) { + function getSystemId(address system) internal view returns (ResourceId systemId) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (bytes32(_blob)); + return ResourceId.wrap(bytes32(_blob)); } /** Get systemId */ - function _getSystemId(address system) internal view returns (bytes32 systemId) { + function _getSystemId(address system) internal view returns (ResourceId systemId) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (bytes32(_blob)); + return ResourceId.wrap(bytes32(_blob)); } /** Get systemId (using the specified store) */ - function getSystemId(IStore _store, address system) internal view returns (bytes32 systemId) { + function getSystemId(IStore _store, address system) internal view returns (ResourceId systemId) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (bytes32(_blob)); + return ResourceId.wrap(bytes32(_blob)); } /** Get systemId */ - function get(address system) internal view returns (bytes32 systemId) { + function get(address system) internal view returns (ResourceId systemId) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (bytes32(_blob)); + return ResourceId.wrap(bytes32(_blob)); } /** Get systemId */ - function _get(address system) internal view returns (bytes32 systemId) { + function _get(address system) internal view returns (ResourceId systemId) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (bytes32(_blob)); + return ResourceId.wrap(bytes32(_blob)); } /** Get systemId (using the specified store) */ - function get(IStore _store, address system) internal view returns (bytes32 systemId) { + function get(IStore _store, address system) internal view returns (ResourceId systemId) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (bytes32(_blob)); + return ResourceId.wrap(bytes32(_blob)); } /** Set systemId */ - function setSystemId(address system, bytes32 systemId) internal { + function setSystemId(address system, ResourceId systemId) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((systemId)), _fieldLayout); + StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked(ResourceId.unwrap(systemId)), _fieldLayout); } /** Set systemId */ - function _setSystemId(address system, bytes32 systemId) internal { + function _setSystemId(address system, ResourceId systemId) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((systemId)), _fieldLayout); + StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked(ResourceId.unwrap(systemId)), _fieldLayout); } /** Set systemId (using the specified store) */ - function setSystemId(IStore _store, address system, bytes32 systemId) internal { + function setSystemId(IStore _store, address system, ResourceId systemId) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - _store.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((systemId)), _fieldLayout); + _store.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked(ResourceId.unwrap(systemId)), _fieldLayout); } /** Set systemId */ - function set(address system, bytes32 systemId) internal { + function set(address system, ResourceId systemId) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((systemId)), _fieldLayout); + StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked(ResourceId.unwrap(systemId)), _fieldLayout); } /** Set systemId */ - function _set(address system, bytes32 systemId) internal { + function _set(address system, ResourceId systemId) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((systemId)), _fieldLayout); + StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked(ResourceId.unwrap(systemId)), _fieldLayout); } /** Set systemId (using the specified store) */ - function set(IStore _store, address system, bytes32 systemId) internal { + function set(IStore _store, address system, ResourceId systemId) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - _store.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((systemId)), _fieldLayout); + _store.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked(ResourceId.unwrap(systemId)), _fieldLayout); } /** Delete all data for given keys */ @@ -205,12 +208,12 @@ library SystemRegistry { } /** Tightly pack static data using this table's schema */ - function encodeStatic(bytes32 systemId) internal pure returns (bytes memory) { + function encodeStatic(ResourceId systemId) internal pure returns (bytes memory) { return abi.encodePacked(systemId); } /** Tightly pack full data using this table's field layout */ - function encode(bytes32 systemId) internal pure returns (bytes memory, PackedCounter, bytes memory) { + function encode(ResourceId systemId) internal pure returns (bytes memory, PackedCounter, bytes memory) { bytes memory _staticData = encodeStatic(systemId); PackedCounter _encodedLengths; diff --git a/packages/world/src/modules/core/tables/Systems.sol b/packages/world/src/modules/core/tables/Systems.sol index 0f3f44acc3..a7a8044ca3 100644 --- a/packages/world/src/modules/core/tables/Systems.sol +++ b/packages/world/src/modules/core/tables/Systems.sol @@ -20,6 +20,9 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; import { RESOURCE_TABLE, RESOURCE_OFFCHAIN_TABLE } from "@latticexyz/store/src/storeResourceTypes.sol"; +// Import user types +import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; + ResourceId constant _tableId = ResourceId.wrap( bytes32(abi.encodePacked(RESOURCE_TABLE, bytes14(""), bytes16("Systems"))) ); @@ -81,111 +84,111 @@ library Systems { } /** Get system */ - function getSystem(bytes32 systemId) internal view returns (address system) { + function getSystem(ResourceId systemId) internal view returns (address system) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (address(bytes20(_blob))); } /** Get system */ - function _getSystem(bytes32 systemId) internal view returns (address system) { + function _getSystem(ResourceId systemId) internal view returns (address system) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (address(bytes20(_blob))); } /** Get system (using the specified store) */ - function getSystem(IStore _store, bytes32 systemId) internal view returns (address system) { + function getSystem(IStore _store, ResourceId systemId) internal view returns (address system) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (address(bytes20(_blob))); } /** Set system */ - function setSystem(bytes32 systemId, address system) internal { + function setSystem(ResourceId systemId, address system) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((system)), _fieldLayout); } /** Set system */ - function _setSystem(bytes32 systemId, address system) internal { + function _setSystem(ResourceId systemId, address system) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((system)), _fieldLayout); } /** Set system (using the specified store) */ - function setSystem(IStore _store, bytes32 systemId, address system) internal { + function setSystem(IStore _store, ResourceId systemId, address system) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); _store.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((system)), _fieldLayout); } /** Get publicAccess */ - function getPublicAccess(bytes32 systemId) internal view returns (bool publicAccess) { + function getPublicAccess(ResourceId systemId) internal view returns (bool publicAccess) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); return (_toBool(uint8(bytes1(_blob)))); } /** Get publicAccess */ - function _getPublicAccess(bytes32 systemId) internal view returns (bool publicAccess) { + function _getPublicAccess(ResourceId systemId) internal view returns (bool publicAccess) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); return (_toBool(uint8(bytes1(_blob)))); } /** Get publicAccess (using the specified store) */ - function getPublicAccess(IStore _store, bytes32 systemId) internal view returns (bool publicAccess) { + function getPublicAccess(IStore _store, ResourceId systemId) internal view returns (bool publicAccess) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); return (_toBool(uint8(bytes1(_blob)))); } /** Set publicAccess */ - function setPublicAccess(bytes32 systemId, bool publicAccess) internal { + function setPublicAccess(ResourceId systemId, bool publicAccess) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); StoreSwitch.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked((publicAccess)), _fieldLayout); } /** Set publicAccess */ - function _setPublicAccess(bytes32 systemId, bool publicAccess) internal { + function _setPublicAccess(ResourceId systemId, bool publicAccess) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); StoreCore.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked((publicAccess)), _fieldLayout); } /** Set publicAccess (using the specified store) */ - function setPublicAccess(IStore _store, bytes32 systemId, bool publicAccess) internal { + function setPublicAccess(IStore _store, ResourceId systemId, bool publicAccess) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); _store.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked((publicAccess)), _fieldLayout); } /** Get the full data */ - function get(bytes32 systemId) internal view returns (address system, bool publicAccess) { + function get(ResourceId systemId) internal view returns (address system, bool publicAccess) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); (bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = StoreSwitch.getRecord( _tableId, @@ -196,9 +199,9 @@ library Systems { } /** Get the full data */ - function _get(bytes32 systemId) internal view returns (address system, bool publicAccess) { + function _get(ResourceId systemId) internal view returns (address system, bool publicAccess) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); (bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = StoreCore.getRecord( _tableId, @@ -209,9 +212,9 @@ library Systems { } /** Get the full data (using the specified store) */ - function get(IStore _store, bytes32 systemId) internal view returns (address system, bool publicAccess) { + function get(IStore _store, ResourceId systemId) internal view returns (address system, bool publicAccess) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); (bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = _store.getRecord( _tableId, @@ -222,40 +225,40 @@ library Systems { } /** Set the full data using individual values */ - function set(bytes32 systemId, address system, bool publicAccess) internal { + function set(ResourceId systemId, address system, bool publicAccess) internal { bytes memory _staticData = encodeStatic(system, publicAccess); PackedCounter _encodedLengths; bytes memory _dynamicData; bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData); } /** Set the full data using individual values */ - function _set(bytes32 systemId, address system, bool publicAccess) internal { + function _set(ResourceId systemId, address system, bool publicAccess) internal { bytes memory _staticData = encodeStatic(system, publicAccess); PackedCounter _encodedLengths; bytes memory _dynamicData; bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using individual values (using the specified store) */ - function set(IStore _store, bytes32 systemId, address system, bool publicAccess) internal { + function set(IStore _store, ResourceId systemId, address system, bool publicAccess) internal { bytes memory _staticData = encodeStatic(system, publicAccess); PackedCounter _encodedLengths; bytes memory _dynamicData; bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData); } @@ -283,25 +286,25 @@ library Systems { } /** Delete all data for given keys */ - function deleteRecord(bytes32 systemId) internal { + function deleteRecord(ResourceId systemId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); StoreSwitch.deleteRecord(_tableId, _keyTuple); } /** Delete all data for given keys */ - function _deleteRecord(bytes32 systemId) internal { + function _deleteRecord(ResourceId systemId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /** Delete all data for given keys (using the specified store) */ - function deleteRecord(IStore _store, bytes32 systemId) internal { + function deleteRecord(IStore _store, ResourceId systemId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); _store.deleteRecord(_tableId, _keyTuple); } @@ -322,9 +325,9 @@ library Systems { } /** Encode keys as a bytes32 array using this table's field layout */ - function encodeKeyTuple(bytes32 systemId) internal pure returns (bytes32[] memory) { + function encodeKeyTuple(ResourceId systemId) internal pure returns (bytes32[] memory) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = systemId; + _keyTuple[0] = ResourceId.unwrap(systemId); return _keyTuple; } diff --git a/packages/world/src/modules/keysintable/KeysInTableHook.sol b/packages/world/src/modules/keysintable/KeysInTableHook.sol index f3bada4a12..dbc0ff4d0a 100644 --- a/packages/world/src/modules/keysintable/KeysInTableHook.sol +++ b/packages/world/src/modules/keysintable/KeysInTableHook.sol @@ -17,20 +17,20 @@ contract KeysInTableHook is StoreHook { bytes32 keysHash = keccak256(abi.encode(keyTuple)); // If the keyTuple has not yet been set in the table... - if (!UsedKeysIndex.getHas(ResourceId.unwrap(tableId), keysHash)) { - uint40 length = uint40(KeysInTable.lengthKeys0(ResourceId.unwrap(tableId))); + if (!UsedKeysIndex.getHas(tableId, keysHash)) { + uint40 length = uint40(KeysInTable.lengthKeys0(tableId)); // Push the keyTuple to the list of keys in this table if (keyTuple.length > 0) { - KeysInTable.pushKeys0(ResourceId.unwrap(tableId), keyTuple[0]); + KeysInTable.pushKeys0(tableId, keyTuple[0]); if (keyTuple.length > 1) { - KeysInTable.pushKeys1(ResourceId.unwrap(tableId), keyTuple[1]); + KeysInTable.pushKeys1(tableId, keyTuple[1]); if (keyTuple.length > 2) { - KeysInTable.pushKeys2(ResourceId.unwrap(tableId), keyTuple[2]); + KeysInTable.pushKeys2(tableId, keyTuple[2]); if (keyTuple.length > 3) { - KeysInTable.pushKeys3(ResourceId.unwrap(tableId), keyTuple[3]); + KeysInTable.pushKeys3(tableId, keyTuple[3]); if (keyTuple.length > 4) { - KeysInTable.pushKeys4(ResourceId.unwrap(tableId), keyTuple[4]); + KeysInTable.pushKeys4(tableId, keyTuple[4]); } } } @@ -38,7 +38,7 @@ contract KeysInTableHook is StoreHook { } // Update the index to avoid duplicating this keyTuple in the array - UsedKeysIndex.set(ResourceId.unwrap(tableId), keysHash, true, length); + UsedKeysIndex.set(tableId, keysHash, true, length); } } @@ -76,56 +76,56 @@ contract KeysInTableHook is StoreHook { function onBeforeDeleteRecord(ResourceId tableId, bytes32[] memory keyTuple, FieldLayout) public override { bytes32 keysHash = keccak256(abi.encode(keyTuple)); - (bool has, uint40 index) = UsedKeysIndex.get(ResourceId.unwrap(tableId), keysHash); + (bool has, uint40 index) = UsedKeysIndex.get(tableId, keysHash); // If the keyTuple was part of the table... if (has) { // Delete the index as the keyTuple is not in the table anymore - UsedKeysIndex.deleteRecord(ResourceId.unwrap(tableId), keysHash); + UsedKeysIndex.deleteRecord(tableId, keysHash); - uint40 length = uint40(KeysInTable.lengthKeys0(ResourceId.unwrap(tableId))); + uint40 length = uint40(KeysInTable.lengthKeys0(tableId)); if (length == 1) { // Delete the list of keys in this table - KeysInTable.deleteRecord(ResourceId.unwrap(tableId)); + KeysInTable.deleteRecord(tableId); } else { if (keyTuple.length > 0) { bytes32[] memory lastKeyTuple = new bytes32[](keyTuple.length); - bytes32 lastKey = KeysInTable.getItemKeys0(ResourceId.unwrap(tableId), length - 1); + bytes32 lastKey = KeysInTable.getItemKeys0(tableId, length - 1); lastKeyTuple[0] = lastKey; // Remove the keyTuple from the list of keys in this table - KeysInTable.updateKeys0(ResourceId.unwrap(tableId), index, lastKey); - KeysInTable.popKeys0(ResourceId.unwrap(tableId)); + KeysInTable.updateKeys0(tableId, index, lastKey); + KeysInTable.popKeys0(tableId); if (keyTuple.length > 1) { - lastKey = KeysInTable.getItemKeys1(ResourceId.unwrap(tableId), length - 1); + lastKey = KeysInTable.getItemKeys1(tableId, length - 1); lastKeyTuple[1] = lastKey; - KeysInTable.updateKeys1(ResourceId.unwrap(tableId), index, lastKey); - KeysInTable.popKeys1(ResourceId.unwrap(tableId)); + KeysInTable.updateKeys1(tableId, index, lastKey); + KeysInTable.popKeys1(tableId); if (keyTuple.length > 2) { - lastKey = KeysInTable.getItemKeys2(ResourceId.unwrap(tableId), length - 1); + lastKey = KeysInTable.getItemKeys2(tableId, length - 1); lastKeyTuple[2] = lastKey; - KeysInTable.updateKeys2(ResourceId.unwrap(tableId), index, lastKey); - KeysInTable.popKeys2(ResourceId.unwrap(tableId)); + KeysInTable.updateKeys2(tableId, index, lastKey); + KeysInTable.popKeys2(tableId); if (keyTuple.length > 3) { - lastKey = KeysInTable.getItemKeys3(ResourceId.unwrap(tableId), length - 1); + lastKey = KeysInTable.getItemKeys3(tableId, length - 1); lastKeyTuple[3] = lastKey; - KeysInTable.updateKeys3(ResourceId.unwrap(tableId), index, lastKey); - KeysInTable.popKeys3(ResourceId.unwrap(tableId)); + KeysInTable.updateKeys3(tableId, index, lastKey); + KeysInTable.popKeys3(tableId); if (keyTuple.length > 4) { - lastKey = KeysInTable.getItemKeys4(ResourceId.unwrap(tableId), length - 1); + lastKey = KeysInTable.getItemKeys4(tableId, length - 1); lastKeyTuple[4] = lastKey; - KeysInTable.updateKeys4(ResourceId.unwrap(tableId), index, lastKey); - KeysInTable.popKeys4(ResourceId.unwrap(tableId)); + KeysInTable.updateKeys4(tableId, index, lastKey); + KeysInTable.popKeys4(tableId); } } } @@ -133,7 +133,7 @@ contract KeysInTableHook is StoreHook { // Update the index of lastKeyTuple after swapping it with the deleted keyTuple bytes32 lastKeyHash = keccak256(abi.encode(lastKeyTuple)); - UsedKeysIndex.setIndex(ResourceId.unwrap(tableId), lastKeyHash, index); + UsedKeysIndex.setIndex(tableId, lastKeyHash, index); } } } diff --git a/packages/world/src/modules/keysintable/KeysInTableModule.sol b/packages/world/src/modules/keysintable/KeysInTableModule.sol index 5a702c0b18..2577ba0a7c 100644 --- a/packages/world/src/modules/keysintable/KeysInTableModule.sol +++ b/packages/world/src/modules/keysintable/KeysInTableModule.sol @@ -46,7 +46,7 @@ contract KeysInTableModule is Module { bool success; bytes memory returnData; - if (!ResourceIds._getExists(ResourceId.unwrap(KeysInTableTableId))) { + if (!ResourceIds._getExists(KeysInTableTableId)) { // Register the tables (success, returnData) = address(world).delegatecall( abi.encodeCall( diff --git a/packages/world/src/modules/keysintable/getKeysInTable.sol b/packages/world/src/modules/keysintable/getKeysInTable.sol index 89a9b96fa2..100ff42977 100644 --- a/packages/world/src/modules/keysintable/getKeysInTable.sol +++ b/packages/world/src/modules/keysintable/getKeysInTable.sol @@ -21,22 +21,22 @@ function getKeysInTable(ResourceId tableId) view returns (bytes32[][] memory key Schema keySchema = StoreSwitch.getKeySchema(tableId); uint256 numFields = keySchema.numFields(); - uint256 length = KeysInTable.lengthKeys0(ResourceId.unwrap(tableId)); + uint256 length = KeysInTable.lengthKeys0(tableId); keyTuples = new bytes32[][](length); for (uint256 i; i < length; i++) { keyTuples[i] = new bytes32[](numFields); // the length of the key tuple depends on the key schema if (numFields > 0) { - keyTuples[i][0] = KeysInTable.getItemKeys0(ResourceId.unwrap(tableId), i); + keyTuples[i][0] = KeysInTable.getItemKeys0(tableId, i); if (numFields > 1) { - keyTuples[i][1] = KeysInTable.getItemKeys1(ResourceId.unwrap(tableId), i); + keyTuples[i][1] = KeysInTable.getItemKeys1(tableId, i); if (numFields > 2) { - keyTuples[i][2] = KeysInTable.getItemKeys2(ResourceId.unwrap(tableId), i); + keyTuples[i][2] = KeysInTable.getItemKeys2(tableId, i); if (numFields > 3) { - keyTuples[i][3] = KeysInTable.getItemKeys3(ResourceId.unwrap(tableId), i); + keyTuples[i][3] = KeysInTable.getItemKeys3(tableId, i); if (numFields > 4) { - keyTuples[i][4] = KeysInTable.getItemKeys4(ResourceId.unwrap(tableId), i); + keyTuples[i][4] = KeysInTable.getItemKeys4(tableId, i); } } } @@ -56,22 +56,22 @@ function getKeysInTable(IStore store, ResourceId tableId) view returns (bytes32[ Schema keySchema = store.getKeySchema(tableId); uint256 numFields = keySchema.numFields(); - uint256 length = KeysInTable.lengthKeys0(store, ResourceId.unwrap(tableId)); + uint256 length = KeysInTable.lengthKeys0(store, tableId); keyTuples = new bytes32[][](length); for (uint256 i; i < length; i++) { keyTuples[i] = new bytes32[](numFields); // the length of the key tuple depends on the key schema if (numFields > 0) { - keyTuples[i][0] = KeysInTable.getItemKeys0(store, ResourceId.unwrap(tableId), i); + keyTuples[i][0] = KeysInTable.getItemKeys0(store, tableId, i); if (numFields > 1) { - keyTuples[i][1] = KeysInTable.getItemKeys1(store, ResourceId.unwrap(tableId), i); + keyTuples[i][1] = KeysInTable.getItemKeys1(store, tableId, i); if (numFields > 2) { - keyTuples[i][2] = KeysInTable.getItemKeys2(store, ResourceId.unwrap(tableId), i); + keyTuples[i][2] = KeysInTable.getItemKeys2(store, tableId, i); if (numFields > 3) { - keyTuples[i][3] = KeysInTable.getItemKeys3(store, ResourceId.unwrap(tableId), i); + keyTuples[i][3] = KeysInTable.getItemKeys3(store, tableId, i); if (numFields > 4) { - keyTuples[i][4] = KeysInTable.getItemKeys4(store, ResourceId.unwrap(tableId), i); + keyTuples[i][4] = KeysInTable.getItemKeys4(store, tableId, i); } } } diff --git a/packages/world/src/modules/keysintable/hasKey.sol b/packages/world/src/modules/keysintable/hasKey.sol index 7916e6f30b..8eb32d4f48 100644 --- a/packages/world/src/modules/keysintable/hasKey.sol +++ b/packages/world/src/modules/keysintable/hasKey.sol @@ -15,7 +15,7 @@ import { UsedKeysIndex } from "./tables/UsedKeysIndex.sol"; function hasKey(ResourceId tableId, bytes32[] memory keyTuple) view returns (bool) { bytes32 keysHash = keccak256(abi.encode(keyTuple)); - return UsedKeysIndex.getHas(ResourceId.unwrap(tableId), keysHash); + return UsedKeysIndex.getHas(tableId, keysHash); } /** @@ -24,5 +24,5 @@ function hasKey(ResourceId tableId, bytes32[] memory keyTuple) view returns (boo function hasKey(IStore store, ResourceId tableId, bytes32[] memory keyTuple) view returns (bool) { bytes32 keysHash = keccak256(abi.encode(keyTuple)); - return UsedKeysIndex.getHas(store, ResourceId.unwrap(tableId), keysHash); + return UsedKeysIndex.getHas(store, tableId, keysHash); } diff --git a/packages/world/src/modules/keysintable/tables/KeysInTable.sol b/packages/world/src/modules/keysintable/tables/KeysInTable.sol index 5c049370a7..ccaab3133b 100644 --- a/packages/world/src/modules/keysintable/tables/KeysInTable.sol +++ b/packages/world/src/modules/keysintable/tables/KeysInTable.sol @@ -20,6 +20,9 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; import { RESOURCE_TABLE, RESOURCE_OFFCHAIN_TABLE } from "@latticexyz/store/src/storeResourceTypes.sol"; +// Import user types +import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; + ResourceId constant _tableId = ResourceId.wrap( bytes32(abi.encodePacked(RESOURCE_TABLE, bytes14(""), bytes16("KeysInTable"))) ); @@ -66,7 +69,7 @@ library KeysInTable { /** Get the table's key names */ function getKeyNames() internal pure returns (string[] memory keyNames) { keyNames = new string[](1); - keyNames[0] = "sourceTable"; + keyNames[0] = "sourceTableId"; } /** Get the table's field names */ @@ -95,60 +98,60 @@ library KeysInTable { } /** Get keys0 */ - function getKeys0(bytes32 sourceTable) internal view returns (bytes32[] memory keys0) { + function getKeys0(ResourceId sourceTableId) internal view returns (bytes32[] memory keys0) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); bytes memory _blob = StoreSwitch.getDynamicField(_tableId, _keyTuple, 0); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } /** Get keys0 */ - function _getKeys0(bytes32 sourceTable) internal view returns (bytes32[] memory keys0) { + function _getKeys0(ResourceId sourceTableId) internal view returns (bytes32[] memory keys0) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } /** Get keys0 (using the specified store) */ - function getKeys0(IStore _store, bytes32 sourceTable) internal view returns (bytes32[] memory keys0) { + function getKeys0(IStore _store, ResourceId sourceTableId) internal view returns (bytes32[] memory keys0) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); bytes memory _blob = _store.getDynamicField(_tableId, _keyTuple, 0); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } /** Set keys0 */ - function setKeys0(bytes32 sourceTable, bytes32[] memory keys0) internal { + function setKeys0(ResourceId sourceTableId, bytes32[] memory keys0) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreSwitch.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((keys0))); } /** Set keys0 */ - function _setKeys0(bytes32 sourceTable, bytes32[] memory keys0) internal { + function _setKeys0(ResourceId sourceTableId, bytes32[] memory keys0) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreCore.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((keys0))); } /** Set keys0 (using the specified store) */ - function setKeys0(IStore _store, bytes32 sourceTable, bytes32[] memory keys0) internal { + function setKeys0(IStore _store, ResourceId sourceTableId, bytes32[] memory keys0) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _store.setDynamicField(_tableId, _keyTuple, 0, EncodeArray.encode((keys0))); } /** Get the length of keys0 */ - function lengthKeys0(bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys0(ResourceId sourceTableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -157,9 +160,9 @@ library KeysInTable { } /** Get the length of keys0 */ - function _lengthKeys0(bytes32 sourceTable) internal view returns (uint256) { + function _lengthKeys0(ResourceId sourceTableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -168,9 +171,9 @@ library KeysInTable { } /** Get the length of keys0 (using the specified store) */ - function lengthKeys0(IStore _store, bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys0(IStore _store, ResourceId sourceTableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); uint256 _byteLength = _store.getDynamicFieldLength(_tableId, _keyTuple, 0); unchecked { @@ -182,9 +185,9 @@ library KeysInTable { * Get an item of keys0 * (unchecked, returns invalid data if index overflows) */ - function getItemKeys0(bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + function getItemKeys0(ResourceId sourceTableId, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _blob = StoreSwitch.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 32, (_index + 1) * 32); @@ -196,9 +199,9 @@ library KeysInTable { * Get an item of keys0 * (unchecked, returns invalid data if index overflows) */ - function _getItemKeys0(bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + function _getItemKeys0(ResourceId sourceTableId, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _blob = StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 32, (_index + 1) * 32); @@ -210,9 +213,9 @@ library KeysInTable { * Get an item of keys0 (using the specified store) * (unchecked, returns invalid data if index overflows) */ - function getItemKeys0(IStore _store, bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + function getItemKeys0(IStore _store, ResourceId sourceTableId, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _blob = _store.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 32, (_index + 1) * 32); @@ -221,49 +224,49 @@ library KeysInTable { } /** Push an element to keys0 */ - function pushKeys0(bytes32 sourceTable, bytes32 _element) internal { + function pushKeys0(ResourceId sourceTableId, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); } /** Push an element to keys0 */ - function _pushKeys0(bytes32 sourceTable, bytes32 _element) internal { + function _pushKeys0(ResourceId sourceTableId, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreCore.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); } /** Push an element to keys0 (using the specified store) */ - function pushKeys0(IStore _store, bytes32 sourceTable, bytes32 _element) internal { + function pushKeys0(IStore _store, ResourceId sourceTableId, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _store.pushToDynamicField(_tableId, _keyTuple, 0, abi.encodePacked((_element))); } /** Pop an element from keys0 */ - function popKeys0(bytes32 sourceTable) internal { + function popKeys0(ResourceId sourceTableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 0, 32); } /** Pop an element from keys0 */ - function _popKeys0(bytes32 sourceTable) internal { + function _popKeys0(ResourceId sourceTableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreCore.popFromDynamicField(_tableId, _keyTuple, 0, 32); } /** Pop an element from keys0 (using the specified store) */ - function popKeys0(IStore _store, bytes32 sourceTable) internal { + function popKeys0(IStore _store, ResourceId sourceTableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _store.popFromDynamicField(_tableId, _keyTuple, 0, 32); } @@ -272,9 +275,9 @@ library KeysInTable { * Update an element of keys0 at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function updateKeys0(bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + function updateKeys0(ResourceId sourceTableId, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -286,9 +289,9 @@ library KeysInTable { * Update an element of keys0 at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function _updateKeys0(bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + function _updateKeys0(ResourceId sourceTableId, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -300,9 +303,9 @@ library KeysInTable { * Update an element of keys0 (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function updateKeys0(IStore _store, bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + function updateKeys0(IStore _store, ResourceId sourceTableId, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -311,60 +314,60 @@ library KeysInTable { } /** Get keys1 */ - function getKeys1(bytes32 sourceTable) internal view returns (bytes32[] memory keys1) { + function getKeys1(ResourceId sourceTableId) internal view returns (bytes32[] memory keys1) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); bytes memory _blob = StoreSwitch.getDynamicField(_tableId, _keyTuple, 1); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } /** Get keys1 */ - function _getKeys1(bytes32 sourceTable) internal view returns (bytes32[] memory keys1) { + function _getKeys1(ResourceId sourceTableId) internal view returns (bytes32[] memory keys1) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 1); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } /** Get keys1 (using the specified store) */ - function getKeys1(IStore _store, bytes32 sourceTable) internal view returns (bytes32[] memory keys1) { + function getKeys1(IStore _store, ResourceId sourceTableId) internal view returns (bytes32[] memory keys1) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); bytes memory _blob = _store.getDynamicField(_tableId, _keyTuple, 1); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } /** Set keys1 */ - function setKeys1(bytes32 sourceTable, bytes32[] memory keys1) internal { + function setKeys1(ResourceId sourceTableId, bytes32[] memory keys1) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreSwitch.setDynamicField(_tableId, _keyTuple, 1, EncodeArray.encode((keys1))); } /** Set keys1 */ - function _setKeys1(bytes32 sourceTable, bytes32[] memory keys1) internal { + function _setKeys1(ResourceId sourceTableId, bytes32[] memory keys1) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreCore.setDynamicField(_tableId, _keyTuple, 1, EncodeArray.encode((keys1))); } /** Set keys1 (using the specified store) */ - function setKeys1(IStore _store, bytes32 sourceTable, bytes32[] memory keys1) internal { + function setKeys1(IStore _store, ResourceId sourceTableId, bytes32[] memory keys1) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _store.setDynamicField(_tableId, _keyTuple, 1, EncodeArray.encode((keys1))); } /** Get the length of keys1 */ - function lengthKeys1(bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys1(ResourceId sourceTableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 1); unchecked { @@ -373,9 +376,9 @@ library KeysInTable { } /** Get the length of keys1 */ - function _lengthKeys1(bytes32 sourceTable) internal view returns (uint256) { + function _lengthKeys1(ResourceId sourceTableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 1); unchecked { @@ -384,9 +387,9 @@ library KeysInTable { } /** Get the length of keys1 (using the specified store) */ - function lengthKeys1(IStore _store, bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys1(IStore _store, ResourceId sourceTableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); uint256 _byteLength = _store.getDynamicFieldLength(_tableId, _keyTuple, 1); unchecked { @@ -398,9 +401,9 @@ library KeysInTable { * Get an item of keys1 * (unchecked, returns invalid data if index overflows) */ - function getItemKeys1(bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + function getItemKeys1(ResourceId sourceTableId, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _blob = StoreSwitch.getDynamicFieldSlice(_tableId, _keyTuple, 1, _index * 32, (_index + 1) * 32); @@ -412,9 +415,9 @@ library KeysInTable { * Get an item of keys1 * (unchecked, returns invalid data if index overflows) */ - function _getItemKeys1(bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + function _getItemKeys1(ResourceId sourceTableId, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _blob = StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 1, _index * 32, (_index + 1) * 32); @@ -426,9 +429,9 @@ library KeysInTable { * Get an item of keys1 (using the specified store) * (unchecked, returns invalid data if index overflows) */ - function getItemKeys1(IStore _store, bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + function getItemKeys1(IStore _store, ResourceId sourceTableId, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _blob = _store.getDynamicFieldSlice(_tableId, _keyTuple, 1, _index * 32, (_index + 1) * 32); @@ -437,49 +440,49 @@ library KeysInTable { } /** Push an element to keys1 */ - function pushKeys1(bytes32 sourceTable, bytes32 _element) internal { + function pushKeys1(ResourceId sourceTableId, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 1, abi.encodePacked((_element))); } /** Push an element to keys1 */ - function _pushKeys1(bytes32 sourceTable, bytes32 _element) internal { + function _pushKeys1(ResourceId sourceTableId, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreCore.pushToDynamicField(_tableId, _keyTuple, 1, abi.encodePacked((_element))); } /** Push an element to keys1 (using the specified store) */ - function pushKeys1(IStore _store, bytes32 sourceTable, bytes32 _element) internal { + function pushKeys1(IStore _store, ResourceId sourceTableId, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _store.pushToDynamicField(_tableId, _keyTuple, 1, abi.encodePacked((_element))); } /** Pop an element from keys1 */ - function popKeys1(bytes32 sourceTable) internal { + function popKeys1(ResourceId sourceTableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 1, 32); } /** Pop an element from keys1 */ - function _popKeys1(bytes32 sourceTable) internal { + function _popKeys1(ResourceId sourceTableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreCore.popFromDynamicField(_tableId, _keyTuple, 1, 32); } /** Pop an element from keys1 (using the specified store) */ - function popKeys1(IStore _store, bytes32 sourceTable) internal { + function popKeys1(IStore _store, ResourceId sourceTableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _store.popFromDynamicField(_tableId, _keyTuple, 1, 32); } @@ -488,9 +491,9 @@ library KeysInTable { * Update an element of keys1 at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function updateKeys1(bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + function updateKeys1(ResourceId sourceTableId, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -502,9 +505,9 @@ library KeysInTable { * Update an element of keys1 at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function _updateKeys1(bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + function _updateKeys1(ResourceId sourceTableId, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -516,9 +519,9 @@ library KeysInTable { * Update an element of keys1 (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function updateKeys1(IStore _store, bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + function updateKeys1(IStore _store, ResourceId sourceTableId, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -527,60 +530,60 @@ library KeysInTable { } /** Get keys2 */ - function getKeys2(bytes32 sourceTable) internal view returns (bytes32[] memory keys2) { + function getKeys2(ResourceId sourceTableId) internal view returns (bytes32[] memory keys2) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); bytes memory _blob = StoreSwitch.getDynamicField(_tableId, _keyTuple, 2); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } /** Get keys2 */ - function _getKeys2(bytes32 sourceTable) internal view returns (bytes32[] memory keys2) { + function _getKeys2(ResourceId sourceTableId) internal view returns (bytes32[] memory keys2) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 2); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } /** Get keys2 (using the specified store) */ - function getKeys2(IStore _store, bytes32 sourceTable) internal view returns (bytes32[] memory keys2) { + function getKeys2(IStore _store, ResourceId sourceTableId) internal view returns (bytes32[] memory keys2) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); bytes memory _blob = _store.getDynamicField(_tableId, _keyTuple, 2); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } /** Set keys2 */ - function setKeys2(bytes32 sourceTable, bytes32[] memory keys2) internal { + function setKeys2(ResourceId sourceTableId, bytes32[] memory keys2) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreSwitch.setDynamicField(_tableId, _keyTuple, 2, EncodeArray.encode((keys2))); } /** Set keys2 */ - function _setKeys2(bytes32 sourceTable, bytes32[] memory keys2) internal { + function _setKeys2(ResourceId sourceTableId, bytes32[] memory keys2) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreCore.setDynamicField(_tableId, _keyTuple, 2, EncodeArray.encode((keys2))); } /** Set keys2 (using the specified store) */ - function setKeys2(IStore _store, bytes32 sourceTable, bytes32[] memory keys2) internal { + function setKeys2(IStore _store, ResourceId sourceTableId, bytes32[] memory keys2) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _store.setDynamicField(_tableId, _keyTuple, 2, EncodeArray.encode((keys2))); } /** Get the length of keys2 */ - function lengthKeys2(bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys2(ResourceId sourceTableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 2); unchecked { @@ -589,9 +592,9 @@ library KeysInTable { } /** Get the length of keys2 */ - function _lengthKeys2(bytes32 sourceTable) internal view returns (uint256) { + function _lengthKeys2(ResourceId sourceTableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 2); unchecked { @@ -600,9 +603,9 @@ library KeysInTable { } /** Get the length of keys2 (using the specified store) */ - function lengthKeys2(IStore _store, bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys2(IStore _store, ResourceId sourceTableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); uint256 _byteLength = _store.getDynamicFieldLength(_tableId, _keyTuple, 2); unchecked { @@ -614,9 +617,9 @@ library KeysInTable { * Get an item of keys2 * (unchecked, returns invalid data if index overflows) */ - function getItemKeys2(bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + function getItemKeys2(ResourceId sourceTableId, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _blob = StoreSwitch.getDynamicFieldSlice(_tableId, _keyTuple, 2, _index * 32, (_index + 1) * 32); @@ -628,9 +631,9 @@ library KeysInTable { * Get an item of keys2 * (unchecked, returns invalid data if index overflows) */ - function _getItemKeys2(bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + function _getItemKeys2(ResourceId sourceTableId, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _blob = StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 2, _index * 32, (_index + 1) * 32); @@ -642,9 +645,9 @@ library KeysInTable { * Get an item of keys2 (using the specified store) * (unchecked, returns invalid data if index overflows) */ - function getItemKeys2(IStore _store, bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + function getItemKeys2(IStore _store, ResourceId sourceTableId, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _blob = _store.getDynamicFieldSlice(_tableId, _keyTuple, 2, _index * 32, (_index + 1) * 32); @@ -653,49 +656,49 @@ library KeysInTable { } /** Push an element to keys2 */ - function pushKeys2(bytes32 sourceTable, bytes32 _element) internal { + function pushKeys2(ResourceId sourceTableId, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 2, abi.encodePacked((_element))); } /** Push an element to keys2 */ - function _pushKeys2(bytes32 sourceTable, bytes32 _element) internal { + function _pushKeys2(ResourceId sourceTableId, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreCore.pushToDynamicField(_tableId, _keyTuple, 2, abi.encodePacked((_element))); } /** Push an element to keys2 (using the specified store) */ - function pushKeys2(IStore _store, bytes32 sourceTable, bytes32 _element) internal { + function pushKeys2(IStore _store, ResourceId sourceTableId, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _store.pushToDynamicField(_tableId, _keyTuple, 2, abi.encodePacked((_element))); } /** Pop an element from keys2 */ - function popKeys2(bytes32 sourceTable) internal { + function popKeys2(ResourceId sourceTableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 2, 32); } /** Pop an element from keys2 */ - function _popKeys2(bytes32 sourceTable) internal { + function _popKeys2(ResourceId sourceTableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreCore.popFromDynamicField(_tableId, _keyTuple, 2, 32); } /** Pop an element from keys2 (using the specified store) */ - function popKeys2(IStore _store, bytes32 sourceTable) internal { + function popKeys2(IStore _store, ResourceId sourceTableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _store.popFromDynamicField(_tableId, _keyTuple, 2, 32); } @@ -704,9 +707,9 @@ library KeysInTable { * Update an element of keys2 at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function updateKeys2(bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + function updateKeys2(ResourceId sourceTableId, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -718,9 +721,9 @@ library KeysInTable { * Update an element of keys2 at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function _updateKeys2(bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + function _updateKeys2(ResourceId sourceTableId, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -732,9 +735,9 @@ library KeysInTable { * Update an element of keys2 (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function updateKeys2(IStore _store, bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + function updateKeys2(IStore _store, ResourceId sourceTableId, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -743,60 +746,60 @@ library KeysInTable { } /** Get keys3 */ - function getKeys3(bytes32 sourceTable) internal view returns (bytes32[] memory keys3) { + function getKeys3(ResourceId sourceTableId) internal view returns (bytes32[] memory keys3) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); bytes memory _blob = StoreSwitch.getDynamicField(_tableId, _keyTuple, 3); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } /** Get keys3 */ - function _getKeys3(bytes32 sourceTable) internal view returns (bytes32[] memory keys3) { + function _getKeys3(ResourceId sourceTableId) internal view returns (bytes32[] memory keys3) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 3); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } /** Get keys3 (using the specified store) */ - function getKeys3(IStore _store, bytes32 sourceTable) internal view returns (bytes32[] memory keys3) { + function getKeys3(IStore _store, ResourceId sourceTableId) internal view returns (bytes32[] memory keys3) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); bytes memory _blob = _store.getDynamicField(_tableId, _keyTuple, 3); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } /** Set keys3 */ - function setKeys3(bytes32 sourceTable, bytes32[] memory keys3) internal { + function setKeys3(ResourceId sourceTableId, bytes32[] memory keys3) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreSwitch.setDynamicField(_tableId, _keyTuple, 3, EncodeArray.encode((keys3))); } /** Set keys3 */ - function _setKeys3(bytes32 sourceTable, bytes32[] memory keys3) internal { + function _setKeys3(ResourceId sourceTableId, bytes32[] memory keys3) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreCore.setDynamicField(_tableId, _keyTuple, 3, EncodeArray.encode((keys3))); } /** Set keys3 (using the specified store) */ - function setKeys3(IStore _store, bytes32 sourceTable, bytes32[] memory keys3) internal { + function setKeys3(IStore _store, ResourceId sourceTableId, bytes32[] memory keys3) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _store.setDynamicField(_tableId, _keyTuple, 3, EncodeArray.encode((keys3))); } /** Get the length of keys3 */ - function lengthKeys3(bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys3(ResourceId sourceTableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 3); unchecked { @@ -805,9 +808,9 @@ library KeysInTable { } /** Get the length of keys3 */ - function _lengthKeys3(bytes32 sourceTable) internal view returns (uint256) { + function _lengthKeys3(ResourceId sourceTableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 3); unchecked { @@ -816,9 +819,9 @@ library KeysInTable { } /** Get the length of keys3 (using the specified store) */ - function lengthKeys3(IStore _store, bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys3(IStore _store, ResourceId sourceTableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); uint256 _byteLength = _store.getDynamicFieldLength(_tableId, _keyTuple, 3); unchecked { @@ -830,9 +833,9 @@ library KeysInTable { * Get an item of keys3 * (unchecked, returns invalid data if index overflows) */ - function getItemKeys3(bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + function getItemKeys3(ResourceId sourceTableId, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _blob = StoreSwitch.getDynamicFieldSlice(_tableId, _keyTuple, 3, _index * 32, (_index + 1) * 32); @@ -844,9 +847,9 @@ library KeysInTable { * Get an item of keys3 * (unchecked, returns invalid data if index overflows) */ - function _getItemKeys3(bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + function _getItemKeys3(ResourceId sourceTableId, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _blob = StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 3, _index * 32, (_index + 1) * 32); @@ -858,9 +861,9 @@ library KeysInTable { * Get an item of keys3 (using the specified store) * (unchecked, returns invalid data if index overflows) */ - function getItemKeys3(IStore _store, bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + function getItemKeys3(IStore _store, ResourceId sourceTableId, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _blob = _store.getDynamicFieldSlice(_tableId, _keyTuple, 3, _index * 32, (_index + 1) * 32); @@ -869,49 +872,49 @@ library KeysInTable { } /** Push an element to keys3 */ - function pushKeys3(bytes32 sourceTable, bytes32 _element) internal { + function pushKeys3(ResourceId sourceTableId, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 3, abi.encodePacked((_element))); } /** Push an element to keys3 */ - function _pushKeys3(bytes32 sourceTable, bytes32 _element) internal { + function _pushKeys3(ResourceId sourceTableId, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreCore.pushToDynamicField(_tableId, _keyTuple, 3, abi.encodePacked((_element))); } /** Push an element to keys3 (using the specified store) */ - function pushKeys3(IStore _store, bytes32 sourceTable, bytes32 _element) internal { + function pushKeys3(IStore _store, ResourceId sourceTableId, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _store.pushToDynamicField(_tableId, _keyTuple, 3, abi.encodePacked((_element))); } /** Pop an element from keys3 */ - function popKeys3(bytes32 sourceTable) internal { + function popKeys3(ResourceId sourceTableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 3, 32); } /** Pop an element from keys3 */ - function _popKeys3(bytes32 sourceTable) internal { + function _popKeys3(ResourceId sourceTableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreCore.popFromDynamicField(_tableId, _keyTuple, 3, 32); } /** Pop an element from keys3 (using the specified store) */ - function popKeys3(IStore _store, bytes32 sourceTable) internal { + function popKeys3(IStore _store, ResourceId sourceTableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _store.popFromDynamicField(_tableId, _keyTuple, 3, 32); } @@ -920,9 +923,9 @@ library KeysInTable { * Update an element of keys3 at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function updateKeys3(bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + function updateKeys3(ResourceId sourceTableId, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -934,9 +937,9 @@ library KeysInTable { * Update an element of keys3 at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function _updateKeys3(bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + function _updateKeys3(ResourceId sourceTableId, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -948,9 +951,9 @@ library KeysInTable { * Update an element of keys3 (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function updateKeys3(IStore _store, bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + function updateKeys3(IStore _store, ResourceId sourceTableId, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -959,60 +962,60 @@ library KeysInTable { } /** Get keys4 */ - function getKeys4(bytes32 sourceTable) internal view returns (bytes32[] memory keys4) { + function getKeys4(ResourceId sourceTableId) internal view returns (bytes32[] memory keys4) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); bytes memory _blob = StoreSwitch.getDynamicField(_tableId, _keyTuple, 4); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } /** Get keys4 */ - function _getKeys4(bytes32 sourceTable) internal view returns (bytes32[] memory keys4) { + function _getKeys4(ResourceId sourceTableId) internal view returns (bytes32[] memory keys4) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 4); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } /** Get keys4 (using the specified store) */ - function getKeys4(IStore _store, bytes32 sourceTable) internal view returns (bytes32[] memory keys4) { + function getKeys4(IStore _store, ResourceId sourceTableId) internal view returns (bytes32[] memory keys4) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); bytes memory _blob = _store.getDynamicField(_tableId, _keyTuple, 4); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } /** Set keys4 */ - function setKeys4(bytes32 sourceTable, bytes32[] memory keys4) internal { + function setKeys4(ResourceId sourceTableId, bytes32[] memory keys4) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreSwitch.setDynamicField(_tableId, _keyTuple, 4, EncodeArray.encode((keys4))); } /** Set keys4 */ - function _setKeys4(bytes32 sourceTable, bytes32[] memory keys4) internal { + function _setKeys4(ResourceId sourceTableId, bytes32[] memory keys4) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreCore.setDynamicField(_tableId, _keyTuple, 4, EncodeArray.encode((keys4))); } /** Set keys4 (using the specified store) */ - function setKeys4(IStore _store, bytes32 sourceTable, bytes32[] memory keys4) internal { + function setKeys4(IStore _store, ResourceId sourceTableId, bytes32[] memory keys4) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _store.setDynamicField(_tableId, _keyTuple, 4, EncodeArray.encode((keys4))); } /** Get the length of keys4 */ - function lengthKeys4(bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys4(ResourceId sourceTableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 4); unchecked { @@ -1021,9 +1024,9 @@ library KeysInTable { } /** Get the length of keys4 */ - function _lengthKeys4(bytes32 sourceTable) internal view returns (uint256) { + function _lengthKeys4(ResourceId sourceTableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 4); unchecked { @@ -1032,9 +1035,9 @@ library KeysInTable { } /** Get the length of keys4 (using the specified store) */ - function lengthKeys4(IStore _store, bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys4(IStore _store, ResourceId sourceTableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); uint256 _byteLength = _store.getDynamicFieldLength(_tableId, _keyTuple, 4); unchecked { @@ -1046,9 +1049,9 @@ library KeysInTable { * Get an item of keys4 * (unchecked, returns invalid data if index overflows) */ - function getItemKeys4(bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + function getItemKeys4(ResourceId sourceTableId, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _blob = StoreSwitch.getDynamicFieldSlice(_tableId, _keyTuple, 4, _index * 32, (_index + 1) * 32); @@ -1060,9 +1063,9 @@ library KeysInTable { * Get an item of keys4 * (unchecked, returns invalid data if index overflows) */ - function _getItemKeys4(bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + function _getItemKeys4(ResourceId sourceTableId, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _blob = StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 4, _index * 32, (_index + 1) * 32); @@ -1074,9 +1077,9 @@ library KeysInTable { * Get an item of keys4 (using the specified store) * (unchecked, returns invalid data if index overflows) */ - function getItemKeys4(IStore _store, bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + function getItemKeys4(IStore _store, ResourceId sourceTableId, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _blob = _store.getDynamicFieldSlice(_tableId, _keyTuple, 4, _index * 32, (_index + 1) * 32); @@ -1085,49 +1088,49 @@ library KeysInTable { } /** Push an element to keys4 */ - function pushKeys4(bytes32 sourceTable, bytes32 _element) internal { + function pushKeys4(ResourceId sourceTableId, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 4, abi.encodePacked((_element))); } /** Push an element to keys4 */ - function _pushKeys4(bytes32 sourceTable, bytes32 _element) internal { + function _pushKeys4(ResourceId sourceTableId, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreCore.pushToDynamicField(_tableId, _keyTuple, 4, abi.encodePacked((_element))); } /** Push an element to keys4 (using the specified store) */ - function pushKeys4(IStore _store, bytes32 sourceTable, bytes32 _element) internal { + function pushKeys4(IStore _store, ResourceId sourceTableId, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _store.pushToDynamicField(_tableId, _keyTuple, 4, abi.encodePacked((_element))); } /** Pop an element from keys4 */ - function popKeys4(bytes32 sourceTable) internal { + function popKeys4(ResourceId sourceTableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 4, 32); } /** Pop an element from keys4 */ - function _popKeys4(bytes32 sourceTable) internal { + function _popKeys4(ResourceId sourceTableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreCore.popFromDynamicField(_tableId, _keyTuple, 4, 32); } /** Pop an element from keys4 (using the specified store) */ - function popKeys4(IStore _store, bytes32 sourceTable) internal { + function popKeys4(IStore _store, ResourceId sourceTableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _store.popFromDynamicField(_tableId, _keyTuple, 4, 32); } @@ -1136,9 +1139,9 @@ library KeysInTable { * Update an element of keys4 at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function updateKeys4(bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + function updateKeys4(ResourceId sourceTableId, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -1150,9 +1153,9 @@ library KeysInTable { * Update an element of keys4 at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function _updateKeys4(bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + function _updateKeys4(ResourceId sourceTableId, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -1164,9 +1167,9 @@ library KeysInTable { * Update an element of keys4 (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function updateKeys4(IStore _store, bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + function updateKeys4(IStore _store, ResourceId sourceTableId, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); unchecked { bytes memory _encoded = abi.encodePacked((_element)); @@ -1175,9 +1178,9 @@ library KeysInTable { } /** Get the full data */ - function get(bytes32 sourceTable) internal view returns (KeysInTableData memory _table) { + function get(ResourceId sourceTableId) internal view returns (KeysInTableData memory _table) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); (bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = StoreSwitch.getRecord( _tableId, @@ -1188,9 +1191,9 @@ library KeysInTable { } /** Get the full data */ - function _get(bytes32 sourceTable) internal view returns (KeysInTableData memory _table) { + function _get(ResourceId sourceTableId) internal view returns (KeysInTableData memory _table) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); (bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = StoreCore.getRecord( _tableId, @@ -1201,9 +1204,9 @@ library KeysInTable { } /** Get the full data (using the specified store) */ - function get(IStore _store, bytes32 sourceTable) internal view returns (KeysInTableData memory _table) { + function get(IStore _store, ResourceId sourceTableId) internal view returns (KeysInTableData memory _table) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); (bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = _store.getRecord( _tableId, @@ -1215,7 +1218,7 @@ library KeysInTable { /** Set the full data using individual values */ function set( - bytes32 sourceTable, + ResourceId sourceTableId, bytes32[] memory keys0, bytes32[] memory keys1, bytes32[] memory keys2, @@ -1227,14 +1230,14 @@ library KeysInTable { bytes memory _dynamicData = encodeDynamic(keys0, keys1, keys2, keys3, keys4); bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData); } /** Set the full data using individual values */ function _set( - bytes32 sourceTable, + ResourceId sourceTableId, bytes32[] memory keys0, bytes32[] memory keys1, bytes32[] memory keys2, @@ -1246,7 +1249,7 @@ library KeysInTable { bytes memory _dynamicData = encodeDynamic(keys0, keys1, keys2, keys3, keys4); bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } @@ -1254,7 +1257,7 @@ library KeysInTable { /** Set the full data using individual values (using the specified store) */ function set( IStore _store, - bytes32 sourceTable, + ResourceId sourceTableId, bytes32[] memory keys0, bytes32[] memory keys1, bytes32[] memory keys2, @@ -1266,43 +1269,43 @@ library KeysInTable { bytes memory _dynamicData = encodeDynamic(keys0, keys1, keys2, keys3, keys4); bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData); } /** Set the full data using the data struct */ - function set(bytes32 sourceTable, KeysInTableData memory _table) internal { + function set(ResourceId sourceTableId, KeysInTableData memory _table) internal { bytes memory _staticData; PackedCounter _encodedLengths = encodeLengths(_table.keys0, _table.keys1, _table.keys2, _table.keys3, _table.keys4); bytes memory _dynamicData = encodeDynamic(_table.keys0, _table.keys1, _table.keys2, _table.keys3, _table.keys4); bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData); } /** Set the full data using the data struct */ - function _set(bytes32 sourceTable, KeysInTableData memory _table) internal { + function _set(ResourceId sourceTableId, KeysInTableData memory _table) internal { bytes memory _staticData; PackedCounter _encodedLengths = encodeLengths(_table.keys0, _table.keys1, _table.keys2, _table.keys3, _table.keys4); bytes memory _dynamicData = encodeDynamic(_table.keys0, _table.keys1, _table.keys2, _table.keys3, _table.keys4); bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using the data struct (using the specified store) */ - function set(IStore _store, bytes32 sourceTable, KeysInTableData memory _table) internal { + function set(IStore _store, ResourceId sourceTableId, KeysInTableData memory _table) internal { bytes memory _staticData; PackedCounter _encodedLengths = encodeLengths(_table.keys0, _table.keys1, _table.keys2, _table.keys3, _table.keys4); bytes memory _dynamicData = encodeDynamic(_table.keys0, _table.keys1, _table.keys2, _table.keys3, _table.keys4); bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData); } @@ -1373,25 +1376,25 @@ library KeysInTable { } /** Delete all data for given keys */ - function deleteRecord(bytes32 sourceTable) internal { + function deleteRecord(ResourceId sourceTableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreSwitch.deleteRecord(_tableId, _keyTuple); } /** Delete all data for given keys */ - function _deleteRecord(bytes32 sourceTable) internal { + function _deleteRecord(ResourceId sourceTableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /** Delete all data for given keys (using the specified store) */ - function deleteRecord(IStore _store, bytes32 sourceTable) internal { + function deleteRecord(IStore _store, ResourceId sourceTableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _store.deleteRecord(_tableId, _keyTuple); } @@ -1450,9 +1453,9 @@ library KeysInTable { } /** Encode keys as a bytes32 array using this table's field layout */ - function encodeKeyTuple(bytes32 sourceTable) internal pure returns (bytes32[] memory) { + function encodeKeyTuple(ResourceId sourceTableId) internal pure returns (bytes32[] memory) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); return _keyTuple; } diff --git a/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol b/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol index 5aa4220250..afb8a8ebd8 100644 --- a/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol +++ b/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol @@ -20,6 +20,9 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; import { RESOURCE_TABLE, RESOURCE_OFFCHAIN_TABLE } from "@latticexyz/store/src/storeResourceTypes.sol"; +// Import user types +import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; + ResourceId constant _tableId = ResourceId.wrap( bytes32(abi.encodePacked(RESOURCE_TABLE, bytes14(""), bytes16("UsedKeysIndex"))) ); @@ -56,7 +59,7 @@ library UsedKeysIndex { /** Get the table's key names */ function getKeyNames() internal pure returns (string[] memory keyNames) { keyNames = new string[](2); - keyNames[0] = "sourceTable"; + keyNames[0] = "sourceTableId"; keyNames[1] = "keysHash"; } @@ -83,9 +86,9 @@ library UsedKeysIndex { } /** Get has */ - function getHas(bytes32 sourceTable, bytes32 keysHash) internal view returns (bool has) { + function getHas(ResourceId sourceTableId, bytes32 keysHash) internal view returns (bool has) { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _keyTuple[1] = keysHash; bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); @@ -93,9 +96,9 @@ library UsedKeysIndex { } /** Get has */ - function _getHas(bytes32 sourceTable, bytes32 keysHash) internal view returns (bool has) { + function _getHas(ResourceId sourceTableId, bytes32 keysHash) internal view returns (bool has) { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _keyTuple[1] = keysHash; bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); @@ -103,9 +106,9 @@ library UsedKeysIndex { } /** Get has (using the specified store) */ - function getHas(IStore _store, bytes32 sourceTable, bytes32 keysHash) internal view returns (bool has) { + function getHas(IStore _store, ResourceId sourceTableId, bytes32 keysHash) internal view returns (bool has) { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _keyTuple[1] = keysHash; bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); @@ -113,36 +116,36 @@ library UsedKeysIndex { } /** Set has */ - function setHas(bytes32 sourceTable, bytes32 keysHash, bool has) internal { + function setHas(ResourceId sourceTableId, bytes32 keysHash, bool has) internal { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _keyTuple[1] = keysHash; StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((has)), _fieldLayout); } /** Set has */ - function _setHas(bytes32 sourceTable, bytes32 keysHash, bool has) internal { + function _setHas(ResourceId sourceTableId, bytes32 keysHash, bool has) internal { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _keyTuple[1] = keysHash; StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((has)), _fieldLayout); } /** Set has (using the specified store) */ - function setHas(IStore _store, bytes32 sourceTable, bytes32 keysHash, bool has) internal { + function setHas(IStore _store, ResourceId sourceTableId, bytes32 keysHash, bool has) internal { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _keyTuple[1] = keysHash; _store.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((has)), _fieldLayout); } /** Get index */ - function getIndex(bytes32 sourceTable, bytes32 keysHash) internal view returns (uint40 index) { + function getIndex(ResourceId sourceTableId, bytes32 keysHash) internal view returns (uint40 index) { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _keyTuple[1] = keysHash; bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); @@ -150,9 +153,9 @@ library UsedKeysIndex { } /** Get index */ - function _getIndex(bytes32 sourceTable, bytes32 keysHash) internal view returns (uint40 index) { + function _getIndex(ResourceId sourceTableId, bytes32 keysHash) internal view returns (uint40 index) { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _keyTuple[1] = keysHash; bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); @@ -160,9 +163,9 @@ library UsedKeysIndex { } /** Get index (using the specified store) */ - function getIndex(IStore _store, bytes32 sourceTable, bytes32 keysHash) internal view returns (uint40 index) { + function getIndex(IStore _store, ResourceId sourceTableId, bytes32 keysHash) internal view returns (uint40 index) { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _keyTuple[1] = keysHash; bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); @@ -170,36 +173,36 @@ library UsedKeysIndex { } /** Set index */ - function setIndex(bytes32 sourceTable, bytes32 keysHash, uint40 index) internal { + function setIndex(ResourceId sourceTableId, bytes32 keysHash, uint40 index) internal { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _keyTuple[1] = keysHash; StoreSwitch.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked((index)), _fieldLayout); } /** Set index */ - function _setIndex(bytes32 sourceTable, bytes32 keysHash, uint40 index) internal { + function _setIndex(ResourceId sourceTableId, bytes32 keysHash, uint40 index) internal { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _keyTuple[1] = keysHash; StoreCore.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked((index)), _fieldLayout); } /** Set index (using the specified store) */ - function setIndex(IStore _store, bytes32 sourceTable, bytes32 keysHash, uint40 index) internal { + function setIndex(IStore _store, ResourceId sourceTableId, bytes32 keysHash, uint40 index) internal { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _keyTuple[1] = keysHash; _store.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked((index)), _fieldLayout); } /** Get the full data */ - function get(bytes32 sourceTable, bytes32 keysHash) internal view returns (bool has, uint40 index) { + function get(ResourceId sourceTableId, bytes32 keysHash) internal view returns (bool has, uint40 index) { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _keyTuple[1] = keysHash; (bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = StoreSwitch.getRecord( @@ -211,9 +214,9 @@ library UsedKeysIndex { } /** Get the full data */ - function _get(bytes32 sourceTable, bytes32 keysHash) internal view returns (bool has, uint40 index) { + function _get(ResourceId sourceTableId, bytes32 keysHash) internal view returns (bool has, uint40 index) { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _keyTuple[1] = keysHash; (bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = StoreCore.getRecord( @@ -225,9 +228,13 @@ library UsedKeysIndex { } /** Get the full data (using the specified store) */ - function get(IStore _store, bytes32 sourceTable, bytes32 keysHash) internal view returns (bool has, uint40 index) { + function get( + IStore _store, + ResourceId sourceTableId, + bytes32 keysHash + ) internal view returns (bool has, uint40 index) { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _keyTuple[1] = keysHash; (bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = _store.getRecord( @@ -239,42 +246,42 @@ library UsedKeysIndex { } /** Set the full data using individual values */ - function set(bytes32 sourceTable, bytes32 keysHash, bool has, uint40 index) internal { + function set(ResourceId sourceTableId, bytes32 keysHash, bool has, uint40 index) internal { bytes memory _staticData = encodeStatic(has, index); PackedCounter _encodedLengths; bytes memory _dynamicData; bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _keyTuple[1] = keysHash; StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData); } /** Set the full data using individual values */ - function _set(bytes32 sourceTable, bytes32 keysHash, bool has, uint40 index) internal { + function _set(ResourceId sourceTableId, bytes32 keysHash, bool has, uint40 index) internal { bytes memory _staticData = encodeStatic(has, index); PackedCounter _encodedLengths; bytes memory _dynamicData; bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _keyTuple[1] = keysHash; StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } /** Set the full data using individual values (using the specified store) */ - function set(IStore _store, bytes32 sourceTable, bytes32 keysHash, bool has, uint40 index) internal { + function set(IStore _store, ResourceId sourceTableId, bytes32 keysHash, bool has, uint40 index) internal { bytes memory _staticData = encodeStatic(has, index); PackedCounter _encodedLengths; bytes memory _dynamicData; bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _keyTuple[1] = keysHash; _store.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData); @@ -303,27 +310,27 @@ library UsedKeysIndex { } /** Delete all data for given keys */ - function deleteRecord(bytes32 sourceTable, bytes32 keysHash) internal { + function deleteRecord(ResourceId sourceTableId, bytes32 keysHash) internal { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _keyTuple[1] = keysHash; StoreSwitch.deleteRecord(_tableId, _keyTuple); } /** Delete all data for given keys */ - function _deleteRecord(bytes32 sourceTable, bytes32 keysHash) internal { + function _deleteRecord(ResourceId sourceTableId, bytes32 keysHash) internal { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _keyTuple[1] = keysHash; StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /** Delete all data for given keys (using the specified store) */ - function deleteRecord(IStore _store, bytes32 sourceTable, bytes32 keysHash) internal { + function deleteRecord(IStore _store, ResourceId sourceTableId, bytes32 keysHash) internal { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _keyTuple[1] = keysHash; _store.deleteRecord(_tableId, _keyTuple); @@ -345,9 +352,9 @@ library UsedKeysIndex { } /** Encode keys as a bytes32 array using this table's field layout */ - function encodeKeyTuple(bytes32 sourceTable, bytes32 keysHash) internal pure returns (bytes32[] memory) { + function encodeKeyTuple(ResourceId sourceTableId, bytes32 keysHash) internal pure returns (bytes32[] memory) { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = sourceTable; + _keyTuple[0] = ResourceId.unwrap(sourceTableId); _keyTuple[1] = keysHash; return _keyTuple; diff --git a/packages/world/src/modules/keyswithvalue/KeysWithValueHook.sol b/packages/world/src/modules/keyswithvalue/KeysWithValueHook.sol index aef1be86c6..445142e3fc 100644 --- a/packages/world/src/modules/keyswithvalue/KeysWithValueHook.sol +++ b/packages/world/src/modules/keyswithvalue/KeysWithValueHook.sol @@ -64,7 +64,7 @@ contract KeysWithValueHook is StoreHook { bytes memory ) public override { // Remove the key from the list of keys with the previous value - FieldLayout fieldLayout = FieldLayout.wrap(Tables.getFieldLayout(ResourceId.unwrap(sourceTableId))); + FieldLayout fieldLayout = Tables.getFieldLayout(sourceTableId); bytes32 previousValue = _getRecordValueHash(sourceTableId, keyTuple, fieldLayout); ResourceId targetTableId = getTargetTableId(MODULE_NAMESPACE, sourceTableId); _removeKeyFromList(targetTableId, keyTuple[0], previousValue); @@ -77,7 +77,7 @@ contract KeysWithValueHook is StoreHook { bytes memory ) public override { // Add the key to the list of keys with the new value - FieldLayout fieldLayout = FieldLayout.wrap(Tables.getFieldLayout(ResourceId.unwrap(sourceTableId))); + FieldLayout fieldLayout = Tables.getFieldLayout(sourceTableId); bytes32 newValue = _getRecordValueHash(sourceTableId, keyTuple, fieldLayout); ResourceId targetTableId = getTargetTableId(MODULE_NAMESPACE, sourceTableId); KeysWithValue.push(targetTableId, newValue, keyTuple[0]); @@ -93,7 +93,7 @@ contract KeysWithValueHook is StoreHook { PackedCounter ) public override { // Remove the key from the list of keys with the previous value - FieldLayout fieldLayout = FieldLayout.wrap(Tables.getFieldLayout(ResourceId.unwrap(sourceTableId))); + FieldLayout fieldLayout = Tables.getFieldLayout(sourceTableId); bytes32 previousValue = _getRecordValueHash(sourceTableId, keyTuple, fieldLayout); ResourceId targetTableId = getTargetTableId(MODULE_NAMESPACE, sourceTableId); _removeKeyFromList(targetTableId, keyTuple[0], previousValue); @@ -109,7 +109,7 @@ contract KeysWithValueHook is StoreHook { PackedCounter ) public override { // Add the key to the list of keys with the new value - FieldLayout fieldLayout = FieldLayout.wrap(Tables.getFieldLayout(ResourceId.unwrap(sourceTableId))); + FieldLayout fieldLayout = Tables.getFieldLayout(sourceTableId); bytes32 newValue = _getRecordValueHash(sourceTableId, keyTuple, fieldLayout); ResourceId targetTableId = getTargetTableId(MODULE_NAMESPACE, sourceTableId); KeysWithValue.push(targetTableId, newValue, keyTuple[0]); diff --git a/packages/world/src/modules/std-delegations/CallboundDelegationControl.sol b/packages/world/src/modules/std-delegations/CallboundDelegationControl.sol index 13bda72d16..b489cf263d 100644 --- a/packages/world/src/modules/std-delegations/CallboundDelegationControl.sol +++ b/packages/world/src/modules/std-delegations/CallboundDelegationControl.sol @@ -16,7 +16,7 @@ contract CallboundDelegationControl is DelegationControl { uint256 availableCalls = CallboundDelegations.get({ delegator: delegator, delegatee: _msgSender(), - systemId: ResourceId.unwrap(systemId), + systemId: systemId, callDataHash: callDataHash }); @@ -25,7 +25,7 @@ contract CallboundDelegationControl is DelegationControl { CallboundDelegations.deleteRecord({ delegator: delegator, delegatee: _msgSender(), - systemId: ResourceId.unwrap(systemId), + systemId: systemId, callDataHash: callDataHash }); return true; @@ -39,7 +39,7 @@ contract CallboundDelegationControl is DelegationControl { CallboundDelegations.set({ delegator: delegator, delegatee: _msgSender(), - systemId: ResourceId.unwrap(systemId), + systemId: systemId, callDataHash: callDataHash, availableCalls: availableCalls }); @@ -56,7 +56,7 @@ contract CallboundDelegationControl is DelegationControl { CallboundDelegations.set({ delegator: _msgSender(), delegatee: delegatee, - systemId: ResourceId.unwrap(systemId), + systemId: systemId, callDataHash: keccak256(callData), availableCalls: numCalls }); diff --git a/packages/world/src/modules/std-delegations/constants.sol b/packages/world/src/modules/std-delegations/constants.sol index 09859c43ee..69a343c5ad 100644 --- a/packages/world/src/modules/std-delegations/constants.sol +++ b/packages/world/src/modules/std-delegations/constants.sol @@ -1,6 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity >=0.8.21; -import { ResourceId } from "../../WorldResourceId.sol"; + +import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; import { RESOURCE_SYSTEM } from "../../worldResourceTypes.sol"; import { ROOT_NAMESPACE } from "../../constants.sol"; diff --git a/packages/world/src/modules/std-delegations/tables/CallboundDelegations.sol b/packages/world/src/modules/std-delegations/tables/CallboundDelegations.sol index cdf4002f24..f97ebe09d7 100644 --- a/packages/world/src/modules/std-delegations/tables/CallboundDelegations.sol +++ b/packages/world/src/modules/std-delegations/tables/CallboundDelegations.sol @@ -20,6 +20,9 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; import { RESOURCE_TABLE, RESOURCE_OFFCHAIN_TABLE } from "@latticexyz/store/src/storeResourceTypes.sol"; +// Import user types +import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; + ResourceId constant _tableId = ResourceId.wrap( bytes32(abi.encodePacked(RESOURCE_TABLE, bytes14(""), bytes16("CallboundDelegat"))) ); @@ -88,13 +91,13 @@ library CallboundDelegations { function getAvailableCalls( address delegator, address delegatee, - bytes32 systemId, + ResourceId systemId, bytes32 callDataHash ) internal view returns (uint256 availableCalls) { bytes32[] memory _keyTuple = new bytes32[](4); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - _keyTuple[2] = systemId; + _keyTuple[2] = ResourceId.unwrap(systemId); _keyTuple[3] = callDataHash; bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); @@ -105,13 +108,13 @@ library CallboundDelegations { function _getAvailableCalls( address delegator, address delegatee, - bytes32 systemId, + ResourceId systemId, bytes32 callDataHash ) internal view returns (uint256 availableCalls) { bytes32[] memory _keyTuple = new bytes32[](4); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - _keyTuple[2] = systemId; + _keyTuple[2] = ResourceId.unwrap(systemId); _keyTuple[3] = callDataHash; bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); @@ -123,13 +126,13 @@ library CallboundDelegations { IStore _store, address delegator, address delegatee, - bytes32 systemId, + ResourceId systemId, bytes32 callDataHash ) internal view returns (uint256 availableCalls) { bytes32[] memory _keyTuple = new bytes32[](4); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - _keyTuple[2] = systemId; + _keyTuple[2] = ResourceId.unwrap(systemId); _keyTuple[3] = callDataHash; bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); @@ -140,13 +143,13 @@ library CallboundDelegations { function get( address delegator, address delegatee, - bytes32 systemId, + ResourceId systemId, bytes32 callDataHash ) internal view returns (uint256 availableCalls) { bytes32[] memory _keyTuple = new bytes32[](4); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - _keyTuple[2] = systemId; + _keyTuple[2] = ResourceId.unwrap(systemId); _keyTuple[3] = callDataHash; bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); @@ -157,13 +160,13 @@ library CallboundDelegations { function _get( address delegator, address delegatee, - bytes32 systemId, + ResourceId systemId, bytes32 callDataHash ) internal view returns (uint256 availableCalls) { bytes32[] memory _keyTuple = new bytes32[](4); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - _keyTuple[2] = systemId; + _keyTuple[2] = ResourceId.unwrap(systemId); _keyTuple[3] = callDataHash; bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); @@ -175,13 +178,13 @@ library CallboundDelegations { IStore _store, address delegator, address delegatee, - bytes32 systemId, + ResourceId systemId, bytes32 callDataHash ) internal view returns (uint256 availableCalls) { bytes32[] memory _keyTuple = new bytes32[](4); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - _keyTuple[2] = systemId; + _keyTuple[2] = ResourceId.unwrap(systemId); _keyTuple[3] = callDataHash; bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); @@ -192,14 +195,14 @@ library CallboundDelegations { function setAvailableCalls( address delegator, address delegatee, - bytes32 systemId, + ResourceId systemId, bytes32 callDataHash, uint256 availableCalls ) internal { bytes32[] memory _keyTuple = new bytes32[](4); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - _keyTuple[2] = systemId; + _keyTuple[2] = ResourceId.unwrap(systemId); _keyTuple[3] = callDataHash; StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((availableCalls)), _fieldLayout); @@ -209,14 +212,14 @@ library CallboundDelegations { function _setAvailableCalls( address delegator, address delegatee, - bytes32 systemId, + ResourceId systemId, bytes32 callDataHash, uint256 availableCalls ) internal { bytes32[] memory _keyTuple = new bytes32[](4); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - _keyTuple[2] = systemId; + _keyTuple[2] = ResourceId.unwrap(systemId); _keyTuple[3] = callDataHash; StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((availableCalls)), _fieldLayout); @@ -227,14 +230,14 @@ library CallboundDelegations { IStore _store, address delegator, address delegatee, - bytes32 systemId, + ResourceId systemId, bytes32 callDataHash, uint256 availableCalls ) internal { bytes32[] memory _keyTuple = new bytes32[](4); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - _keyTuple[2] = systemId; + _keyTuple[2] = ResourceId.unwrap(systemId); _keyTuple[3] = callDataHash; _store.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((availableCalls)), _fieldLayout); @@ -244,14 +247,14 @@ library CallboundDelegations { function set( address delegator, address delegatee, - bytes32 systemId, + ResourceId systemId, bytes32 callDataHash, uint256 availableCalls ) internal { bytes32[] memory _keyTuple = new bytes32[](4); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - _keyTuple[2] = systemId; + _keyTuple[2] = ResourceId.unwrap(systemId); _keyTuple[3] = callDataHash; StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((availableCalls)), _fieldLayout); @@ -261,14 +264,14 @@ library CallboundDelegations { function _set( address delegator, address delegatee, - bytes32 systemId, + ResourceId systemId, bytes32 callDataHash, uint256 availableCalls ) internal { bytes32[] memory _keyTuple = new bytes32[](4); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - _keyTuple[2] = systemId; + _keyTuple[2] = ResourceId.unwrap(systemId); _keyTuple[3] = callDataHash; StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((availableCalls)), _fieldLayout); @@ -279,36 +282,36 @@ library CallboundDelegations { IStore _store, address delegator, address delegatee, - bytes32 systemId, + ResourceId systemId, bytes32 callDataHash, uint256 availableCalls ) internal { bytes32[] memory _keyTuple = new bytes32[](4); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - _keyTuple[2] = systemId; + _keyTuple[2] = ResourceId.unwrap(systemId); _keyTuple[3] = callDataHash; _store.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((availableCalls)), _fieldLayout); } /** Delete all data for given keys */ - function deleteRecord(address delegator, address delegatee, bytes32 systemId, bytes32 callDataHash) internal { + function deleteRecord(address delegator, address delegatee, ResourceId systemId, bytes32 callDataHash) internal { bytes32[] memory _keyTuple = new bytes32[](4); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - _keyTuple[2] = systemId; + _keyTuple[2] = ResourceId.unwrap(systemId); _keyTuple[3] = callDataHash; StoreSwitch.deleteRecord(_tableId, _keyTuple); } /** Delete all data for given keys */ - function _deleteRecord(address delegator, address delegatee, bytes32 systemId, bytes32 callDataHash) internal { + function _deleteRecord(address delegator, address delegatee, ResourceId systemId, bytes32 callDataHash) internal { bytes32[] memory _keyTuple = new bytes32[](4); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - _keyTuple[2] = systemId; + _keyTuple[2] = ResourceId.unwrap(systemId); _keyTuple[3] = callDataHash; StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); @@ -319,13 +322,13 @@ library CallboundDelegations { IStore _store, address delegator, address delegatee, - bytes32 systemId, + ResourceId systemId, bytes32 callDataHash ) internal { bytes32[] memory _keyTuple = new bytes32[](4); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - _keyTuple[2] = systemId; + _keyTuple[2] = ResourceId.unwrap(systemId); _keyTuple[3] = callDataHash; _store.deleteRecord(_tableId, _keyTuple); @@ -350,13 +353,13 @@ library CallboundDelegations { function encodeKeyTuple( address delegator, address delegatee, - bytes32 systemId, + ResourceId systemId, bytes32 callDataHash ) internal pure returns (bytes32[] memory) { bytes32[] memory _keyTuple = new bytes32[](4); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - _keyTuple[2] = systemId; + _keyTuple[2] = ResourceId.unwrap(systemId); _keyTuple[3] = callDataHash; return _keyTuple; diff --git a/packages/world/src/modules/uniqueentity/UniqueEntityModule.sol b/packages/world/src/modules/uniqueentity/UniqueEntityModule.sol index c65d7c3ee5..a869cee541 100644 --- a/packages/world/src/modules/uniqueentity/UniqueEntityModule.sol +++ b/packages/world/src/modules/uniqueentity/UniqueEntityModule.sol @@ -25,7 +25,7 @@ contract UniqueEntityModule is Module { return MODULE_NAME; } - function installRoot(bytes memory args) public { + function installRoot(bytes memory) public { IBaseWorld world = IBaseWorld(_world()); // Register table diff --git a/packages/world/src/tables/Delegations.sol b/packages/world/src/tables/Delegations.sol index 5e02211c4e..62506e3b05 100644 --- a/packages/world/src/tables/Delegations.sol +++ b/packages/world/src/tables/Delegations.sol @@ -20,6 +20,9 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; import { RESOURCE_TABLE, RESOURCE_OFFCHAIN_TABLE } from "@latticexyz/store/src/storeResourceTypes.sol"; +// Import user types +import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; + ResourceId constant _tableId = ResourceId.wrap( bytes32(abi.encodePacked(RESOURCE_TABLE, bytes14(""), bytes16("Delegations"))) ); @@ -84,26 +87,26 @@ library Delegations { function getDelegationControlId( address delegator, address delegatee - ) internal view returns (bytes32 delegationControlId) { + ) internal view returns (ResourceId delegationControlId) { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (bytes32(_blob)); + return ResourceId.wrap(bytes32(_blob)); } /** Get delegationControlId */ function _getDelegationControlId( address delegator, address delegatee - ) internal view returns (bytes32 delegationControlId) { + ) internal view returns (ResourceId delegationControlId) { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (bytes32(_blob)); + return ResourceId.wrap(bytes32(_blob)); } /** Get delegationControlId (using the specified store) */ @@ -111,33 +114,33 @@ library Delegations { IStore _store, address delegator, address delegatee - ) internal view returns (bytes32 delegationControlId) { + ) internal view returns (ResourceId delegationControlId) { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (bytes32(_blob)); + return ResourceId.wrap(bytes32(_blob)); } /** Get delegationControlId */ - function get(address delegator, address delegatee) internal view returns (bytes32 delegationControlId) { + function get(address delegator, address delegatee) internal view returns (ResourceId delegationControlId) { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (bytes32(_blob)); + return ResourceId.wrap(bytes32(_blob)); } /** Get delegationControlId */ - function _get(address delegator, address delegatee) internal view returns (bytes32 delegationControlId) { + function _get(address delegator, address delegatee) internal view returns (ResourceId delegationControlId) { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (bytes32(_blob)); + return ResourceId.wrap(bytes32(_blob)); } /** Get delegationControlId (using the specified store) */ @@ -145,31 +148,43 @@ library Delegations { IStore _store, address delegator, address delegatee - ) internal view returns (bytes32 delegationControlId) { + ) internal view returns (ResourceId delegationControlId) { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (bytes32(_blob)); + return ResourceId.wrap(bytes32(_blob)); } /** Set delegationControlId */ - function setDelegationControlId(address delegator, address delegatee, bytes32 delegationControlId) internal { + function setDelegationControlId(address delegator, address delegatee, ResourceId delegationControlId) internal { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((delegationControlId)), _fieldLayout); + StoreSwitch.setStaticField( + _tableId, + _keyTuple, + 0, + abi.encodePacked(ResourceId.unwrap(delegationControlId)), + _fieldLayout + ); } /** Set delegationControlId */ - function _setDelegationControlId(address delegator, address delegatee, bytes32 delegationControlId) internal { + function _setDelegationControlId(address delegator, address delegatee, ResourceId delegationControlId) internal { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((delegationControlId)), _fieldLayout); + StoreCore.setStaticField( + _tableId, + _keyTuple, + 0, + abi.encodePacked(ResourceId.unwrap(delegationControlId)), + _fieldLayout + ); } /** Set delegationControlId (using the specified store) */ @@ -177,40 +192,64 @@ library Delegations { IStore _store, address delegator, address delegatee, - bytes32 delegationControlId + ResourceId delegationControlId ) internal { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - _store.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((delegationControlId)), _fieldLayout); + _store.setStaticField( + _tableId, + _keyTuple, + 0, + abi.encodePacked(ResourceId.unwrap(delegationControlId)), + _fieldLayout + ); } /** Set delegationControlId */ - function set(address delegator, address delegatee, bytes32 delegationControlId) internal { + function set(address delegator, address delegatee, ResourceId delegationControlId) internal { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((delegationControlId)), _fieldLayout); + StoreSwitch.setStaticField( + _tableId, + _keyTuple, + 0, + abi.encodePacked(ResourceId.unwrap(delegationControlId)), + _fieldLayout + ); } /** Set delegationControlId */ - function _set(address delegator, address delegatee, bytes32 delegationControlId) internal { + function _set(address delegator, address delegatee, ResourceId delegationControlId) internal { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((delegationControlId)), _fieldLayout); + StoreCore.setStaticField( + _tableId, + _keyTuple, + 0, + abi.encodePacked(ResourceId.unwrap(delegationControlId)), + _fieldLayout + ); } /** Set delegationControlId (using the specified store) */ - function set(IStore _store, address delegator, address delegatee, bytes32 delegationControlId) internal { + function set(IStore _store, address delegator, address delegatee, ResourceId delegationControlId) internal { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = bytes32(uint256(uint160(delegator))); _keyTuple[1] = bytes32(uint256(uint160(delegatee))); - _store.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((delegationControlId)), _fieldLayout); + _store.setStaticField( + _tableId, + _keyTuple, + 0, + abi.encodePacked(ResourceId.unwrap(delegationControlId)), + _fieldLayout + ); } /** Delete all data for given keys */ @@ -241,12 +280,12 @@ library Delegations { } /** Tightly pack static data using this table's schema */ - function encodeStatic(bytes32 delegationControlId) internal pure returns (bytes memory) { + function encodeStatic(ResourceId delegationControlId) internal pure returns (bytes memory) { return abi.encodePacked(delegationControlId); } /** Tightly pack full data using this table's field layout */ - function encode(bytes32 delegationControlId) internal pure returns (bytes memory, PackedCounter, bytes memory) { + function encode(ResourceId delegationControlId) internal pure returns (bytes memory, PackedCounter, bytes memory) { bytes memory _staticData = encodeStatic(delegationControlId); PackedCounter _encodedLengths; diff --git a/packages/world/src/tables/NamespaceOwner.sol b/packages/world/src/tables/NamespaceOwner.sol index b47c56e645..2db1b381fa 100644 --- a/packages/world/src/tables/NamespaceOwner.sol +++ b/packages/world/src/tables/NamespaceOwner.sol @@ -20,6 +20,9 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; import { RESOURCE_TABLE, RESOURCE_OFFCHAIN_TABLE } from "@latticexyz/store/src/storeResourceTypes.sol"; +// Import user types +import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; + ResourceId constant _tableId = ResourceId.wrap( bytes32(abi.encodePacked(RESOURCE_TABLE, bytes14(""), bytes16("NamespaceOwner"))) ); @@ -79,127 +82,127 @@ library NamespaceOwner { } /** Get owner */ - function getOwner(bytes32 namespaceId) internal view returns (address owner) { + function getOwner(ResourceId namespaceId) internal view returns (address owner) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (address(bytes20(_blob))); } /** Get owner */ - function _getOwner(bytes32 namespaceId) internal view returns (address owner) { + function _getOwner(ResourceId namespaceId) internal view returns (address owner) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (address(bytes20(_blob))); } /** Get owner (using the specified store) */ - function getOwner(IStore _store, bytes32 namespaceId) internal view returns (address owner) { + function getOwner(IStore _store, ResourceId namespaceId) internal view returns (address owner) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (address(bytes20(_blob))); } /** Get owner */ - function get(bytes32 namespaceId) internal view returns (address owner) { + function get(ResourceId namespaceId) internal view returns (address owner) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (address(bytes20(_blob))); } /** Get owner */ - function _get(bytes32 namespaceId) internal view returns (address owner) { + function _get(ResourceId namespaceId) internal view returns (address owner) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (address(bytes20(_blob))); } /** Get owner (using the specified store) */ - function get(IStore _store, bytes32 namespaceId) internal view returns (address owner) { + function get(IStore _store, ResourceId namespaceId) internal view returns (address owner) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (address(bytes20(_blob))); } /** Set owner */ - function setOwner(bytes32 namespaceId, address owner) internal { + function setOwner(ResourceId namespaceId, address owner) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((owner)), _fieldLayout); } /** Set owner */ - function _setOwner(bytes32 namespaceId, address owner) internal { + function _setOwner(ResourceId namespaceId, address owner) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((owner)), _fieldLayout); } /** Set owner (using the specified store) */ - function setOwner(IStore _store, bytes32 namespaceId, address owner) internal { + function setOwner(IStore _store, ResourceId namespaceId, address owner) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); _store.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((owner)), _fieldLayout); } /** Set owner */ - function set(bytes32 namespaceId, address owner) internal { + function set(ResourceId namespaceId, address owner) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((owner)), _fieldLayout); } /** Set owner */ - function _set(bytes32 namespaceId, address owner) internal { + function _set(ResourceId namespaceId, address owner) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((owner)), _fieldLayout); } /** Set owner (using the specified store) */ - function set(IStore _store, bytes32 namespaceId, address owner) internal { + function set(IStore _store, ResourceId namespaceId, address owner) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); _store.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((owner)), _fieldLayout); } /** Delete all data for given keys */ - function deleteRecord(bytes32 namespaceId) internal { + function deleteRecord(ResourceId namespaceId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); StoreSwitch.deleteRecord(_tableId, _keyTuple); } /** Delete all data for given keys */ - function _deleteRecord(bytes32 namespaceId) internal { + function _deleteRecord(ResourceId namespaceId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /** Delete all data for given keys (using the specified store) */ - function deleteRecord(IStore _store, bytes32 namespaceId) internal { + function deleteRecord(IStore _store, ResourceId namespaceId) internal { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); _store.deleteRecord(_tableId, _keyTuple); } @@ -220,9 +223,9 @@ library NamespaceOwner { } /** Encode keys as a bytes32 array using this table's field layout */ - function encodeKeyTuple(bytes32 namespaceId) internal pure returns (bytes32[] memory) { + function encodeKeyTuple(ResourceId namespaceId) internal pure returns (bytes32[] memory) { bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = namespaceId; + _keyTuple[0] = ResourceId.unwrap(namespaceId); return _keyTuple; } diff --git a/packages/world/src/tables/ResourceAccess.sol b/packages/world/src/tables/ResourceAccess.sol index 37c4d3266b..28e852d645 100644 --- a/packages/world/src/tables/ResourceAccess.sol +++ b/packages/world/src/tables/ResourceAccess.sol @@ -20,6 +20,9 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; import { RESOURCE_TABLE, RESOURCE_OFFCHAIN_TABLE } from "@latticexyz/store/src/storeResourceTypes.sol"; +// Import user types +import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; + ResourceId constant _tableId = ResourceId.wrap( bytes32(abi.encodePacked(RESOURCE_TABLE, bytes14(""), bytes16("ResourceAccess"))) ); @@ -81,9 +84,9 @@ library ResourceAccess { } /** Get access */ - function getAccess(bytes32 resourceId, address caller) internal view returns (bool access) { + function getAccess(ResourceId resourceId, address caller) internal view returns (bool access) { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); _keyTuple[1] = bytes32(uint256(uint160(caller))); bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); @@ -91,9 +94,9 @@ library ResourceAccess { } /** Get access */ - function _getAccess(bytes32 resourceId, address caller) internal view returns (bool access) { + function _getAccess(ResourceId resourceId, address caller) internal view returns (bool access) { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); _keyTuple[1] = bytes32(uint256(uint160(caller))); bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); @@ -101,9 +104,9 @@ library ResourceAccess { } /** Get access (using the specified store) */ - function getAccess(IStore _store, bytes32 resourceId, address caller) internal view returns (bool access) { + function getAccess(IStore _store, ResourceId resourceId, address caller) internal view returns (bool access) { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); _keyTuple[1] = bytes32(uint256(uint160(caller))); bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); @@ -111,9 +114,9 @@ library ResourceAccess { } /** Get access */ - function get(bytes32 resourceId, address caller) internal view returns (bool access) { + function get(ResourceId resourceId, address caller) internal view returns (bool access) { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); _keyTuple[1] = bytes32(uint256(uint160(caller))); bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); @@ -121,9 +124,9 @@ library ResourceAccess { } /** Get access */ - function _get(bytes32 resourceId, address caller) internal view returns (bool access) { + function _get(ResourceId resourceId, address caller) internal view returns (bool access) { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); _keyTuple[1] = bytes32(uint256(uint160(caller))); bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); @@ -131,9 +134,9 @@ library ResourceAccess { } /** Get access (using the specified store) */ - function get(IStore _store, bytes32 resourceId, address caller) internal view returns (bool access) { + function get(IStore _store, ResourceId resourceId, address caller) internal view returns (bool access) { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); _keyTuple[1] = bytes32(uint256(uint160(caller))); bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); @@ -141,81 +144,81 @@ library ResourceAccess { } /** Set access */ - function setAccess(bytes32 resourceId, address caller, bool access) internal { + function setAccess(ResourceId resourceId, address caller, bool access) internal { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); _keyTuple[1] = bytes32(uint256(uint160(caller))); StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((access)), _fieldLayout); } /** Set access */ - function _setAccess(bytes32 resourceId, address caller, bool access) internal { + function _setAccess(ResourceId resourceId, address caller, bool access) internal { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); _keyTuple[1] = bytes32(uint256(uint160(caller))); StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((access)), _fieldLayout); } /** Set access (using the specified store) */ - function setAccess(IStore _store, bytes32 resourceId, address caller, bool access) internal { + function setAccess(IStore _store, ResourceId resourceId, address caller, bool access) internal { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); _keyTuple[1] = bytes32(uint256(uint160(caller))); _store.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((access)), _fieldLayout); } /** Set access */ - function set(bytes32 resourceId, address caller, bool access) internal { + function set(ResourceId resourceId, address caller, bool access) internal { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); _keyTuple[1] = bytes32(uint256(uint160(caller))); StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((access)), _fieldLayout); } /** Set access */ - function _set(bytes32 resourceId, address caller, bool access) internal { + function _set(ResourceId resourceId, address caller, bool access) internal { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); _keyTuple[1] = bytes32(uint256(uint160(caller))); StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((access)), _fieldLayout); } /** Set access (using the specified store) */ - function set(IStore _store, bytes32 resourceId, address caller, bool access) internal { + function set(IStore _store, ResourceId resourceId, address caller, bool access) internal { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); _keyTuple[1] = bytes32(uint256(uint160(caller))); _store.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((access)), _fieldLayout); } /** Delete all data for given keys */ - function deleteRecord(bytes32 resourceId, address caller) internal { + function deleteRecord(ResourceId resourceId, address caller) internal { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); _keyTuple[1] = bytes32(uint256(uint160(caller))); StoreSwitch.deleteRecord(_tableId, _keyTuple); } /** Delete all data for given keys */ - function _deleteRecord(bytes32 resourceId, address caller) internal { + function _deleteRecord(ResourceId resourceId, address caller) internal { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); _keyTuple[1] = bytes32(uint256(uint160(caller))); StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); } /** Delete all data for given keys (using the specified store) */ - function deleteRecord(IStore _store, bytes32 resourceId, address caller) internal { + function deleteRecord(IStore _store, ResourceId resourceId, address caller) internal { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); _keyTuple[1] = bytes32(uint256(uint160(caller))); _store.deleteRecord(_tableId, _keyTuple); @@ -237,9 +240,9 @@ library ResourceAccess { } /** Encode keys as a bytes32 array using this table's field layout */ - function encodeKeyTuple(bytes32 resourceId, address caller) internal pure returns (bytes32[] memory) { + function encodeKeyTuple(ResourceId resourceId, address caller) internal pure returns (bytes32[] memory) { bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = resourceId; + _keyTuple[0] = ResourceId.unwrap(resourceId); _keyTuple[1] = bytes32(uint256(uint160(caller))); return _keyTuple; diff --git a/packages/world/test/AccessControl.t.sol b/packages/world/test/AccessControl.t.sol index 11b52a3c50..b0f9af2fa3 100644 --- a/packages/world/test/AccessControl.t.sol +++ b/packages/world/test/AccessControl.t.sol @@ -31,8 +31,8 @@ contract AccessControlTest is Test, GasReporter, StoreMock { _tableId = WorldResourceIdLib.encode({ typeId: RESOURCE_TABLE, namespace: namespace, name: name }); _namespaceId = WorldResourceIdLib.encodeNamespace(namespace); - NamespaceOwner.set(namespace, address(this)); - ResourceAccess.set(ResourceId.unwrap(_tableId), presetCaller, true); + NamespaceOwner.set(_namespaceId, address(this)); + ResourceAccess.set(_tableId, presetCaller, true); } function testAccessControl() public { @@ -47,7 +47,7 @@ contract AccessControlTest is Test, GasReporter, StoreMock { assertFalse(hasAccess, "caller should not have access to the table"); // Grant access to the namespace - ResourceAccess.set(ResourceId.unwrap(namespaceId), caller, true); + ResourceAccess.set(namespaceId, caller, true); // Check that the caller has access to the namespace or name startGasReport("AccessControl: hasAccess (warm, namespace only)"); @@ -56,7 +56,7 @@ contract AccessControlTest is Test, GasReporter, StoreMock { assertTrue(hasAccess, "caller should have access to the namespace"); // Revoke access to the namespace - ResourceAccess.set(ResourceId.unwrap(namespaceId), caller, false); + ResourceAccess.set(namespaceId, caller, false); // Check that the caller has no access to the namespace or name startGasReport("AccessControl: hasAccess (warm)"); @@ -65,13 +65,13 @@ contract AccessControlTest is Test, GasReporter, StoreMock { assertFalse(hasAccess, "access to the namespace should have been revoked"); // Grant access to the name - ResourceAccess.set(ResourceId.unwrap(tableId), caller, true); + ResourceAccess.set(tableId, caller, true); // Check that the caller has access to the name assertTrue(AccessControl.hasAccess(tableId, caller), "access to the table should have been granted"); // Revoke access to the name - ResourceAccess.set(ResourceId.unwrap(tableId), caller, false); + ResourceAccess.set(tableId, caller, false); // Check that the caller has no access to the namespace or name assertFalse(AccessControl.hasAccess(tableId, caller), "access to the table should have been revoked"); @@ -103,4 +103,23 @@ contract AccessControlTest is Test, GasReporter, StoreMock { vm.expectRevert(abi.encodeWithSelector(IWorldErrors.World_AccessDenied.selector, tableId.toString(), caller)); AccessControl.requireAccess(tableId, caller); } + + function testRequireOwner() public { + ResourceId tableId = _tableId; + + startGasReport("AccessControl: requireOwner (cold)"); + AccessControl.requireOwner(tableId, address(this)); + endGasReport(); + + startGasReport("AccessControl: requireOwner (warm)"); + AccessControl.requireOwner(tableId, address(this)); + endGasReport(); + } + + function testRequireOwnerRevert() public { + ResourceId tableId = _tableId; + + vm.expectRevert(abi.encodeWithSelector(IWorldErrors.World_AccessDenied.selector, tableId.toString(), caller)); + AccessControl.requireOwner(tableId, caller); + } } diff --git a/packages/world/test/Factories.t.sol b/packages/world/test/Factories.t.sol index c6a9ee6087..13115a184c 100644 --- a/packages/world/test/Factories.t.sol +++ b/packages/world/test/Factories.t.sol @@ -82,6 +82,6 @@ contract FactoriesTest is Test { assertEq(uint256(worldFactory.worldCount()), uint256(1)); // Confirm the msg.sender is owner of the root namespace of the new world - assertEq(NamespaceOwner.get(ResourceId.unwrap(ROOT_NAMESPACE_ID)), address(this)); + assertEq(NamespaceOwner.get(ROOT_NAMESPACE_ID), address(this)); } } diff --git a/packages/world/test/World.t.sol b/packages/world/test/World.t.sol index 62f9cd4796..1bdc6a56b3 100644 --- a/packages/world/test/World.t.sol +++ b/packages/world/test/World.t.sol @@ -95,7 +95,6 @@ contract WorldTestSystem is System { function writeData(bytes14 namespace, bytes16 name, bool data) public { bytes32[] memory keyTuple = new bytes32[](0); ResourceId tableId = WorldResourceIdLib.encode({ typeId: RESOURCE_TABLE, namespace: namespace, name: name }); - FieldLayout fieldLayout = StoreSwitch.getFieldLayout(tableId); if (StoreSwitch.getStoreAddress() == address(this)) { StoreCore.setRecord(tableId, keyTuple, abi.encodePacked(data), PackedCounter.wrap(bytes32(0)), new bytes(0)); @@ -215,7 +214,7 @@ contract WorldTest is Test, GasReporter { newWorld.initialize(coreModule); // Should have registered the core system function selectors - CoreSystem coreSystem = CoreSystem(Systems.getSystem(world, ResourceId.unwrap(CORE_SYSTEM_ID))); + CoreSystem coreSystem = CoreSystem(Systems.getSystem(world, CORE_SYSTEM_ID)); bytes4[17] memory coreFunctionSignatures = [ // --- AccessManagementSystem --- coreSystem.grantAccess.selector, @@ -251,29 +250,20 @@ contract WorldTest is Test, GasReporter { // Should have registered the table data table (fka schema table) assertEq( - Tables.getFieldLayout(newWorld, ResourceId.unwrap(TablesTableId)), + FieldLayout.unwrap(Tables.getFieldLayout(newWorld, TablesTableId)), FieldLayout.unwrap(Tables.getFieldLayout()) ); - assertEq( - Tables.getAbiEncodedKeyNames(newWorld, ResourceId.unwrap(TablesTableId)), - abi.encode(Tables.getKeyNames()) - ); - assertEq( - Tables.getAbiEncodedFieldNames(newWorld, ResourceId.unwrap(TablesTableId)), - abi.encode(Tables.getFieldNames()) - ); + assertEq(Tables.getAbiEncodedKeyNames(newWorld, TablesTableId), abi.encode(Tables.getKeyNames())); + assertEq(Tables.getAbiEncodedFieldNames(newWorld, TablesTableId), abi.encode(Tables.getFieldNames())); // Should have registered the namespace owner table assertEq( - Tables.getFieldLayout(newWorld, ResourceId.unwrap(NamespaceOwnerTableId)), + FieldLayout.unwrap(Tables.getFieldLayout(newWorld, NamespaceOwnerTableId)), FieldLayout.unwrap(NamespaceOwner.getFieldLayout()) ); + assertEq(Tables.getAbiEncodedKeyNames(newWorld, NamespaceOwnerTableId), abi.encode(NamespaceOwner.getKeyNames())); assertEq( - Tables.getAbiEncodedKeyNames(newWorld, ResourceId.unwrap(NamespaceOwnerTableId)), - abi.encode(NamespaceOwner.getKeyNames()) - ); - assertEq( - Tables.getAbiEncodedFieldNames(newWorld, ResourceId.unwrap(NamespaceOwnerTableId)), + Tables.getAbiEncodedFieldNames(newWorld, NamespaceOwnerTableId), abi.encode(NamespaceOwner.getFieldNames()) ); @@ -306,11 +296,11 @@ contract WorldTest is Test, GasReporter { function testRootNamespace() public { // Owner of root route should be the creator of the World - address rootOwner = NamespaceOwner.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)); + address rootOwner = NamespaceOwner.get(world, ROOT_NAMESPACE_ID); assertEq(rootOwner, address(this)); // The creator of the World should have access to the root namespace - assertTrue(ResourceAccess.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID), address(this))); + assertTrue(ResourceAccess.get(world, ROOT_NAMESPACE_ID, address(this))); } function testStoreAddress() public { @@ -336,21 +326,13 @@ contract WorldTest is Test, GasReporter { endGasReport(); // Expect the caller to be the namespace owner - assertEq( - NamespaceOwner.get(world, ResourceId.unwrap(namespaceId)), - address(this), - "caller should be namespace owner" - ); + assertEq(NamespaceOwner.get(world, namespaceId), address(this), "caller should be namespace owner"); // Expect the caller to have access - assertEq( - ResourceAccess.get(world, ResourceId.unwrap(namespaceId), address(this)), - true, - "caller should have access" - ); + assertEq(ResourceAccess.get(world, namespaceId, address(this)), true, "caller should have access"); // Expect the resource ID to have been registered - assertTrue(ResourceIds.getExists(world, ResourceId.unwrap(namespaceId))); + assertTrue(ResourceIds.getExists(world, namespaceId)); // Expect an error when registering an existing namespace vm.expectRevert( @@ -386,12 +368,12 @@ contract WorldTest is Test, GasReporter { // Expect the new owner to not be namespace owner before transfer assertFalse( - (NamespaceOwner.get(world, ResourceId.unwrap(namespaceId)) == address(1)), + (NamespaceOwner.get(world, namespaceId)) == address(1), "new owner should not be namespace owner before transfer" ); // Expect the new owner to not have access before transfer assertEq( - ResourceAccess.get(world, ResourceId.unwrap(namespaceId), address(1)), + ResourceAccess.get(world, namespaceId, address(1)), false, "new owner should not have access before transfer" ); @@ -399,31 +381,19 @@ contract WorldTest is Test, GasReporter { world.transferOwnership(namespaceId, address(1)); // Expect the new owner to be namespace owner - assertEq( - NamespaceOwner.get(world, ResourceId.unwrap(namespaceId)), - address(1), - "new owner should be namespace owner" - ); + assertEq(NamespaceOwner.get(world, namespaceId), address(1), "new owner should be namespace owner"); // Expect the new owner to have access - assertEq( - ResourceAccess.get(world, ResourceId.unwrap(namespaceId), address(1)), - true, - "new owner should have access" - ); + assertEq(ResourceAccess.get(world, namespaceId, address(1)), true, "new owner should have access"); // Expect previous owner to no longer be owner assertFalse( - (NamespaceOwner.get(world, ResourceId.unwrap(namespaceId)) == address(this)), + (NamespaceOwner.get(world, namespaceId)) == address(this), "caller should no longer be namespace owner" ); // Expect previous owner to no longer have access - assertEq( - ResourceAccess.get(world, ResourceId.unwrap(namespaceId), address(this)), - false, - "caller should no longer have access" - ); + assertEq(ResourceAccess.get(world, namespaceId, address(this)), false, "caller should no longer have access"); // Expect revert if caller is not the owner _expectAccessDenied(address(this), namespace, 0, RESOURCE_NAMESPACE); @@ -449,28 +419,20 @@ contract WorldTest is Test, GasReporter { endGasReport(); // Expect the namespace to be created and owned by the caller - assertEq( - NamespaceOwner.get(world, ResourceId.unwrap(namespaceId)), - address(this), - "namespace should be created by caller" - ); + assertEq(NamespaceOwner.get(world, namespaceId), address(this), "namespace should be created by caller"); // Expect the table to be registered assertEq(world.getFieldLayout(tableId).unwrap(), fieldLayout.unwrap(), "value schema should be registered"); - bytes memory loadedKeyNames = Tables.getAbiEncodedKeyNames(world, ResourceId.unwrap(tableId)); + bytes memory loadedKeyNames = Tables.getAbiEncodedKeyNames(world, tableId); assertEq(loadedKeyNames, abi.encode(keyNames), "key names should be registered"); - bytes memory loadedfieldNames = Tables.getAbiEncodedFieldNames(world, ResourceId.unwrap(tableId)); + bytes memory loadedfieldNames = Tables.getAbiEncodedFieldNames(world, tableId); assertEq(loadedfieldNames, abi.encode(fieldNames), "value names should be registered"); // Expect an error when registering an existing table vm.expectRevert( - abi.encodeWithSelector( - IStoreErrors.Store_TableAlreadyExists.selector, - tableId, - string(bytes.concat(ResourceId.unwrap(tableId))) - ) + abi.encodeWithSelector(IStoreErrors.Store_TableAlreadyExists.selector, tableId, string(abi.encodePacked(tableId))) ); world.registerTable(tableId, fieldLayout, defaultKeySchema, valueSchema, keyNames, fieldNames); @@ -503,43 +465,37 @@ contract WorldTest is Test, GasReporter { endGasReport(); // Expect the system to be registered - (address registeredAddress, bool publicAccess) = Systems.get(world, ResourceId.unwrap(systemId)); + (address registeredAddress, bool publicAccess) = Systems.get(world, systemId); assertEq(registeredAddress, address(system)); // Expect the system's resource ID to have been registered - assertTrue(ResourceIds.getExists(world, ResourceId.unwrap(systemId))); + assertTrue(ResourceIds.getExists(world, systemId)); // Expect the system namespace to be owned by the caller - address routeOwner = NamespaceOwner.get(world, ResourceId.unwrap(namespaceId)); + address routeOwner = NamespaceOwner.get(world, namespaceId); assertEq(routeOwner, address(this)); // Expect the system to not be publicly accessible assertFalse(publicAccess); // Expect the system to be accessible by the caller - assertTrue( - ResourceAccess.get({ _store: world, resourceId: ResourceId.unwrap(namespaceId), caller: address(this) }) - ); + assertTrue(ResourceAccess.get({ _store: world, resourceId: namespaceId, caller: address(this) })); // Expect the system to not be accessible by another address - assertFalse( - ResourceAccess.get({ _store: world, resourceId: ResourceId.unwrap(namespaceId), caller: address(0x1) }) - ); + assertFalse(ResourceAccess.get({ _store: world, resourceId: namespaceId, caller: address(0x1) })); // Expect the system to have access to its own namespace - assertTrue( - ResourceAccess.get({ _store: world, resourceId: ResourceId.unwrap(namespaceId), caller: address(system) }) - ); + assertTrue(ResourceAccess.get({ _store: world, resourceId: namespaceId, caller: address(system) })); ResourceId newNamespaceId = WorldResourceIdLib.encodeNamespace("newNamespace"); // Expect the namespace to be created if it doesn't exist yet - assertEq(NamespaceOwner.get(world, ResourceId.unwrap(newNamespaceId)), address(0)); + assertEq(NamespaceOwner.get(world, newNamespaceId), address(0)); world.registerSystem( WorldResourceIdLib.encode({ typeId: RESOURCE_SYSTEM, namespace: "newNamespace", name: "testSystem" }), new System(), false ); - assertEq(NamespaceOwner.get(world, ResourceId.unwrap(newNamespaceId)), address(this)); + assertEq(NamespaceOwner.get(world, newNamespaceId), address(this)); // Expect an error when registering an existing system at a new system ID vm.expectRevert(abi.encodeWithSelector(IWorldErrors.World_SystemAlreadyExists.selector, address(system))); @@ -627,32 +583,36 @@ contract WorldTest is Test, GasReporter { world.registerSystem(systemId, newSystem, false); // Expect the system address and public access to be updated in the System table - (address registeredAddress, bool publicAccess) = Systems.get(world, ResourceId.unwrap(systemId)); + (address registeredAddress, bool publicAccess) = Systems.get(world, systemId); assertEq(registeredAddress, address(newSystem), "system address should be updated"); assertEq(publicAccess, false, "public access should be updated"); // Expect the SystemRegistry table to not have a reference to the old system anymore - bytes32 registeredSystemId = SystemRegistry.get(world, address(oldSystem)); - assertEq(registeredSystemId, bytes32(0), "old system should be removed from SystemRegistry"); + ResourceId registeredSystemId = SystemRegistry.get(world, address(oldSystem)); + assertEq(ResourceId.unwrap(registeredSystemId), bytes32(0), "old system should be removed from SystemRegistry"); // Expect the SystemRegistry table to have a reference to the new system registeredSystemId = SystemRegistry.get(world, address(newSystem)); - assertEq(registeredSystemId, ResourceId.unwrap(systemId), "new system should be added to SystemRegistry"); + assertEq( + ResourceId.unwrap(registeredSystemId), + ResourceId.unwrap(systemId), + "new system should be added to SystemRegistry" + ); // Expect the old system to not have access to the namespace anymore assertFalse( - ResourceAccess.get(world, ResourceId.unwrap(namespaceId), address(oldSystem)), + ResourceAccess.get(world, namespaceId, address(oldSystem)), "old system should not have access to the namespace" ); // Expect the new system to have access to the namespace assertTrue( - ResourceAccess.get(world, ResourceId.unwrap(namespaceId), address(newSystem)), + ResourceAccess.get(world, namespaceId, address(newSystem)), "new system should have access to the namespace" ); // Expect the resource ID to still be registered - assertTrue(ResourceIds.getExists(world, ResourceId.unwrap(systemId)), "resource type should still be SYSTEM"); + assertTrue(ResourceIds.getExists(world, systemId), "resource type should still be SYSTEM"); } function testInvalidIds() public { @@ -705,11 +665,7 @@ contract WorldTest is Test, GasReporter { // Expect an error when trying to register a new table at an existing table ID vm.expectRevert( - abi.encodeWithSelector( - IStoreErrors.Store_TableAlreadyExists.selector, - ResourceId.unwrap(tableId), - string(bytes.concat(ResourceId.unwrap(tableId))) - ) + abi.encodeWithSelector(IStoreErrors.Store_TableAlreadyExists.selector, tableId, string(abi.encodePacked(tableId))) ); world.registerTable( tableId, diff --git a/packages/world/test/WorldBalance.t.sol b/packages/world/test/WorldBalance.t.sol index ca36645905..57e094e3af 100644 --- a/packages/world/test/WorldBalance.t.sol +++ b/packages/world/test/WorldBalance.t.sol @@ -47,8 +47,8 @@ contract WorldBalanceTest is Test, GasReporter { uint256 value = 1 ether; // Expect the root and non root namespaces to have no balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), 0); - assertEq(Balances.get(world, ResourceId.unwrap(namespaceId)), 0); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), 0); + assertEq(Balances.get(world, namespaceId), 0); // Call a function on the root system with value via call vm.deal(caller, value); @@ -57,7 +57,7 @@ contract WorldBalanceTest is Test, GasReporter { assertEq(abi.decode(data, (uint256)), value); // Expect the root namespace to have the value as balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), value); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), value); // Call a function on a non-root system with value via call vm.deal(caller, value); @@ -66,18 +66,18 @@ contract WorldBalanceTest is Test, GasReporter { assertEq(abi.decode(data, (uint256)), value); // Expect the non root namespace to have the value as balance - assertEq(Balances.get(world, ResourceId.unwrap(namespaceId)), value); + assertEq(Balances.get(world, namespaceId), value); // Expect the root namespace to still have the same balance as before - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), value); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), value); } function testCallFromWithValue() public { uint256 value = 1 ether; // Expect the root and non root namespaces to have no balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), 0); - assertEq(Balances.get(world, ResourceId.unwrap(namespaceId)), 0); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), 0); + assertEq(Balances.get(world, namespaceId), 0); // Call a function on the root system with value via callFrom vm.deal(caller, value); @@ -86,7 +86,7 @@ contract WorldBalanceTest is Test, GasReporter { assertEq(abi.decode(data, (uint256)), value); // Expect the root namespace to have the value as balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), value); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), value); // Call a function on a non-root system with value via callFrom vm.deal(caller, value); @@ -95,18 +95,18 @@ contract WorldBalanceTest is Test, GasReporter { assertEq(abi.decode(data, (uint256)), value); // Expect the non root namespace to have the value as balance - assertEq(Balances.get(world, ResourceId.unwrap(namespaceId)), value); + assertEq(Balances.get(world, namespaceId), value); // Expect the root namespace to still have the same balance as before - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), value); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), value); } function testFallbackWithValue() public { uint256 value = 1 ether; // Expect the root and non root namespaces to have no balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), 0); - assertEq(Balances.get(world, ResourceId.unwrap(namespaceId)), 0); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), 0); + assertEq(Balances.get(world, namespaceId), 0); // Call a function on the root system with value via the registered root function selector vm.deal(caller, value); @@ -116,7 +116,7 @@ contract WorldBalanceTest is Test, GasReporter { assertEq(abi.decode(data, (uint256)), value); // Expect the root namespace to have the value as balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), value); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), value); // Call a function on a non-root system with value vm.deal(caller, value); @@ -126,17 +126,17 @@ contract WorldBalanceTest is Test, GasReporter { assertEq(abi.decode(data, (uint256)), value); // Expect the non root namespace to have the value as balance - assertEq(Balances.get(world, ResourceId.unwrap(namespaceId)), value); + assertEq(Balances.get(world, namespaceId), value); // Expect the root namespace to still have the same balance as before - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), value); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), value); } function testReceiveWithValue() public { uint256 value = 1 ether; // Expect the root to have no balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), 0); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), 0); // Send value to the world without calldata vm.deal(caller, value); @@ -146,15 +146,15 @@ contract WorldBalanceTest is Test, GasReporter { assertEq(data.length, 0); // Expect the root namespace to have the value as balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), value); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), value); } function testTransferBalanceToNamespace() public { uint256 value = 1 ether; // Expect the root and non root namespaces to have no balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), 0); - assertEq(Balances.get(world, ResourceId.unwrap(namespaceId)), 0); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), 0); + assertEq(Balances.get(world, namespaceId), 0); // Send balance to root namespace vm.deal(caller, value); @@ -164,24 +164,24 @@ contract WorldBalanceTest is Test, GasReporter { assertEq(data.length, 0); // Expect the root namespace to have the value as balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), value); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), value); // Transfer the balance to another namespace world.transferBalanceToNamespace(ROOT_NAMESPACE_ID, namespaceId, value); // Expect the root namespace to have no balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), 0); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), 0); // Expect the non root namespace to have the value as balance - assertEq(Balances.get(world, ResourceId.unwrap(namespaceId)), value); + assertEq(Balances.get(world, namespaceId), value); } function testTransferBalanceToNamespaceRevertInsufficientBalance() public { uint256 value = 1 ether; // Expect the root and non root namespaces to have no balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), 0); - assertEq(Balances.get(world, ResourceId.unwrap(namespaceId)), 0); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), 0); + assertEq(Balances.get(world, namespaceId), 0); // Send balance to root namespace vm.deal(caller, value); @@ -191,25 +191,25 @@ contract WorldBalanceTest is Test, GasReporter { assertEq(data.length, 0); // Expect the root namespace to have the value as balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), value); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), value); // Expect revert when attempting to transfer more than the balance vm.expectRevert(abi.encodeWithSelector(IWorldErrors.World_InsufficientBalance.selector, value, value + 1)); world.transferBalanceToNamespace(ROOT_NAMESPACE_ID, namespaceId, value + 1); // Expect the root namespace to have the value as balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), value); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), value); // Expect the non root namespace to have no balance - assertEq(Balances.get(world, ResourceId.unwrap(namespaceId)), 0); + assertEq(Balances.get(world, namespaceId), 0); } function testTransferBalanceToNamespaceRevertAccessDenied() public { uint256 value = 1 ether; // Expect the root and non root namespaces to have no balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), 0); - assertEq(Balances.get(world, ResourceId.unwrap(namespaceId)), 0); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), 0); + assertEq(Balances.get(world, namespaceId), 0); // Send balance to root namespace vm.deal(caller, value); @@ -219,7 +219,7 @@ contract WorldBalanceTest is Test, GasReporter { assertEq(data.length, 0); // Expect the root namespace to have the value as balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), value); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), value); // Expect revert when attempting to transfer from a namespace without access vm.prank(caller); @@ -229,17 +229,17 @@ contract WorldBalanceTest is Test, GasReporter { world.transferBalanceToNamespace(ROOT_NAMESPACE_ID, namespaceId, value); // Expect the root namespace to have the value as balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), value); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), value); // Expect the non root namespace to have no balance - assertEq(Balances.get(world, ResourceId.unwrap(namespaceId)), 0); + assertEq(Balances.get(world, namespaceId), 0); } function testTransferBalanceToNamespaceRevertInvalidResourceType() public { uint256 value = 1 ether; // Expect the root namespace to have no balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), 0); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), 0); // Send balance to root namespace vm.deal(caller, value); @@ -249,7 +249,7 @@ contract WorldBalanceTest is Test, GasReporter { assertEq(data.length, 0); // Expect the root namespace to have the value as balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), value); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), value); // Expect revert when attempting to transfer to an invalid namespace ResourceId invalidNamespace = WorldResourceIdLib.encode({ typeId: "xx", namespace: "something", name: "invalid" }); @@ -265,18 +265,18 @@ contract WorldBalanceTest is Test, GasReporter { world.transferBalanceToNamespace(ROOT_NAMESPACE_ID, invalidNamespace, value); // Expect the root namespace to have the value as balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), value); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), value); // Expect the non root namespace to have no balance - assertEq(Balances.get(world, ResourceId.unwrap(invalidNamespace)), 0); + assertEq(Balances.get(world, invalidNamespace), 0); } function testTransferBalanceToAddress() public { uint256 value = 1 ether; // Expect the root and non root namespaces to have no balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), 0); - assertEq(Balances.get(world, ResourceId.unwrap(namespaceId)), 0); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), 0); + assertEq(Balances.get(world, namespaceId), 0); // Send balance to root namespace vm.deal(caller, value); @@ -286,7 +286,7 @@ contract WorldBalanceTest is Test, GasReporter { assertEq(data.length, 0); // Expect the root namespace to have the value as balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), value); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), value); // Expect the receiver to not have any balance address receiver = address(1234); @@ -296,7 +296,7 @@ contract WorldBalanceTest is Test, GasReporter { world.transferBalanceToAddress(ROOT_NAMESPACE_ID, receiver, value); // Expect the root namespace to have no balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), 0); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), 0); // Expect the receiver to have value as balance assertEq(receiver.balance, value); @@ -306,8 +306,8 @@ contract WorldBalanceTest is Test, GasReporter { uint256 value = 1 ether; // Expect the root and non root namespaces to have no balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), 0); - assertEq(Balances.get(world, ResourceId.unwrap(namespaceId)), 0); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), 0); + assertEq(Balances.get(world, namespaceId), 0); // Send balance to root namespace vm.deal(caller, value); @@ -317,7 +317,7 @@ contract WorldBalanceTest is Test, GasReporter { assertEq(data.length, 0); // Expect the root namespace to have the value as balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), value); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), value); // Expect the receiver to not have any balance address receiver = address(1234); @@ -328,7 +328,7 @@ contract WorldBalanceTest is Test, GasReporter { world.transferBalanceToAddress(ROOT_NAMESPACE_ID, receiver, value + 1); // Expect the root namespace to have value as balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), value); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), value); // Expect the receiver to have no balance assertEq(receiver.balance, 0); @@ -338,8 +338,8 @@ contract WorldBalanceTest is Test, GasReporter { uint256 value = 1 ether; // Expect the root and non root namespaces to have no balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), 0); - assertEq(Balances.get(world, ResourceId.unwrap(namespaceId)), 0); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), 0); + assertEq(Balances.get(world, namespaceId), 0); // Send balance to root namespace vm.deal(caller, value); @@ -349,7 +349,7 @@ contract WorldBalanceTest is Test, GasReporter { assertEq(data.length, 0); // Expect the root namespace to have the value as balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), value); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), value); // Expect the receiver to not have any balance address receiver = address(1234); @@ -367,7 +367,7 @@ contract WorldBalanceTest is Test, GasReporter { world.transferBalanceToAddress(ROOT_NAMESPACE_ID, receiver, value); // Expect the root namespace to have the value as balance - assertEq(Balances.get(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), value); + assertEq(Balances.get(world, ROOT_NAMESPACE_ID), value); // Expect the receiver to have no balance assertEq(receiver.balance, 0); diff --git a/packages/world/test/WorldDynamicUpdate.t.sol b/packages/world/test/WorldDynamicUpdate.t.sol index 493e120065..1d02ca0b48 100644 --- a/packages/world/test/WorldDynamicUpdate.t.sol +++ b/packages/world/test/WorldDynamicUpdate.t.sol @@ -140,8 +140,6 @@ contract UpdateInDynamicFieldTest is Test, GasReporter { } function testSpliceDynamicData() public { - FieldLayout fieldLayout = AddressArray.getFieldLayout(); - // Expect the data to be written assertEq(AddressArray.get(world, tableId, key), initData);