From 874190758641461c20ddea773e9f3eca12e59501 Mon Sep 17 00:00:00 2001 From: alvrs Date: Tue, 12 Sep 2023 15:34:28 +0100 Subject: [PATCH 1/4] do not handle external calls of world to itself as internal calls --- packages/world/mud.config.ts | 2 + packages/world/src/AccessControl.sol | 7 +- packages/world/src/Tables.sol | 4 +- packages/world/src/World.sol | 4 +- packages/world/src/interfaces/IModule.sol | 13 +- .../world/src/modules/core/CoreModule.sol | 24 +- .../AccessManagementSystem.sol | 6 +- .../StoreRegistrationSystem.sol | 6 +- .../WorldRegistrationSystem.sol | 10 +- .../modules/keysintable/KeysInTableHook.sol | 57 ++-- .../modules/keysintable/KeysInTableModule.sol | 53 ++-- .../src/modules/keysintable/constants.sol | 4 + .../modules/keysintable/getKeysInTable.sol | 25 +- .../world/src/modules/keysintable/hasKey.sol | 5 +- .../keysintable/tables/KeysInTable.sol | 244 ++++++++++++------ .../keysintable/tables/UsedKeysIndex.sol | 57 ++-- .../keyswithvalue/KeysWithValueModule.sol | 47 ++-- .../StandardDelegationsModule.sol | 22 +- .../uniqueentity/UniqueEntityModule.sol | 6 +- packages/world/test/KeysInTableModule.t.sol | 48 ++-- packages/world/test/World.t.sol | 12 +- 21 files changed, 408 insertions(+), 248 deletions(-) diff --git a/packages/world/mud.config.ts b/packages/world/mud.config.ts index b23b1e0919..208e6df813 100644 --- a/packages/world/mud.config.ts +++ b/packages/world/mud.config.ts @@ -132,6 +132,7 @@ export default mudConfig({ keys3: "bytes32[]", keys4: "bytes32[]", }, + tableIdArgument: true, }, UsedKeysIndex: { directory: "modules/keysintable/tables", @@ -141,6 +142,7 @@ export default mudConfig({ }, schema: { has: "bool", index: "uint40" }, dataStruct: false, + tableIdArgument: true, }, UniqueEntity: { directory: "modules/uniqueentity/tables", diff --git a/packages/world/src/AccessControl.sol b/packages/world/src/AccessControl.sol index ae42b34b02..ca637f7c3f 100644 --- a/packages/world/src/AccessControl.sol +++ b/packages/world/src/AccessControl.sol @@ -32,12 +32,11 @@ library AccessControl { } /** - * Check for ownership of the namespace of the given resource selector - * or identity of the caller to this contract's own address. + * Check for ownership of the namespace of the given resource selector. * Reverts with AccessDenied if the check fails. */ - function requireOwnerOrSelf(bytes32 resourceSelector, address caller) internal view { - if (address(this) != caller && NamespaceOwner.get(resourceSelector.getNamespace()) != caller) { + function requireOwner(bytes32 resourceSelector, address caller) internal view { + if (NamespaceOwner.get(resourceSelector.getNamespace()) != caller) { revert IWorldErrors.AccessDenied(resourceSelector.toString(), caller); } } diff --git a/packages/world/src/Tables.sol b/packages/world/src/Tables.sol index 27d8009070..3185ae02bc 100644 --- a/packages/world/src/Tables.sol +++ b/packages/world/src/Tables.sol @@ -14,8 +14,8 @@ import { SystemHooks, SystemHooksTableId } from "./modules/core/tables/SystemHoo import { ResourceType, ResourceTypeTableId } from "./modules/core/tables/ResourceType.sol"; import { FunctionSelectors, FunctionSelectorsTableId } from "./modules/core/tables/FunctionSelectors.sol"; import { KeysWithValue } from "./modules/keyswithvalue/tables/KeysWithValue.sol"; -import { KeysInTable, KeysInTableData, KeysInTableTableId } from "./modules/keysintable/tables/KeysInTable.sol"; -import { UsedKeysIndex, UsedKeysIndexTableId } from "./modules/keysintable/tables/UsedKeysIndex.sol"; +import { KeysInTable, KeysInTableData } from "./modules/keysintable/tables/KeysInTable.sol"; +import { UsedKeysIndex } from "./modules/keysintable/tables/UsedKeysIndex.sol"; import { UniqueEntity } from "./modules/uniqueentity/tables/UniqueEntity.sol"; import { CallboundDelegations, CallboundDelegationsTableId } from "./modules/std-delegations/tables/CallboundDelegations.sol"; import { TimeboundDelegations, TimeboundDelegationsTableId } from "./modules/std-delegations/tables/TimeboundDelegations.sol"; diff --git a/packages/world/src/World.sol b/packages/world/src/World.sol index 8f5256ef75..75839ca583 100644 --- a/packages/world/src/World.sol +++ b/packages/world/src/World.sol @@ -49,13 +49,13 @@ contract World is StoreRead, IStoreData, IWorldKernel { * The module is delegatecalled and installed in the root namespace. */ function installRootModule(IModule module, bytes memory args) public { - AccessControl.requireOwnerOrSelf(ROOT_NAMESPACE, msg.sender); + AccessControl.requireOwner(ROOT_NAMESPACE, msg.sender); WorldContextProvider.delegatecallWithContextOrRevert({ msgSender: msg.sender, msgValue: 0, target: address(module), - funcSelectorAndArgs: abi.encodeWithSelector(IModule.install.selector, args) + funcSelectorAndArgs: abi.encodeWithSelector(IModule.installRoot.selector, args) }); // Register the module in the InstalledModules table diff --git a/packages/world/src/interfaces/IModule.sol b/packages/world/src/interfaces/IModule.sol index 5e9dda86ed..990fbcde48 100644 --- a/packages/world/src/interfaces/IModule.sol +++ b/packages/world/src/interfaces/IModule.sol @@ -3,6 +3,8 @@ pragma solidity >=0.8.0; interface IModule { error RequiredModuleNotFound(string resourceSelector); + error RootInstallModeNotSupported(); + error NonRootInstallNotSupported(); /** * Return the module name as a bytes16. @@ -10,7 +12,16 @@ interface IModule { function getName() external view returns (bytes16 name); /** - * A module expects to be called via the World contract, and therefore installs itself on its `msg.sender`. + * This function is called by the World as part of `installRootModule`. + * The module expects to be called via the World contract, and therefore installs itself on the `msg.sender`. + */ + function installRoot(bytes memory args) external; + + /** + * This function is called by the World as part of `installModule`. + * The module expects to be called via the World contract, and therefore installs itself on the `msg.sender`. + * This function is separate from `installRoot` because the logic might be different (eg. accepting namespace parameters, + * or using `callFrom`) */ function install(bytes memory args) external; } diff --git a/packages/world/src/modules/core/CoreModule.sol b/packages/world/src/modules/core/CoreModule.sol index 72842d9c84..2a2e44dc7b 100644 --- a/packages/world/src/modules/core/CoreModule.sol +++ b/packages/world/src/modules/core/CoreModule.sol @@ -37,7 +37,7 @@ import { WorldRegistrationSystem } from "./implementations/WorldRegistrationSyst * The CoreModule registers internal World tables, the CoreSystem, and its function selectors. * Note: - * This module is required to be delegatecalled (via `World.registerRootSystem`), + * This module only supports `installRoot` (via `World.registerRootSystem`), * because it needs to install root tables, systems and function selectors. */ contract CoreModule is IModule, WorldContextConsumer { @@ -49,12 +49,16 @@ contract CoreModule is IModule, WorldContextConsumer { return CORE_MODULE_NAME; } - function install(bytes memory) public override { + function installRoot(bytes memory) public override { _registerCoreTables(); _registerCoreSystem(); _registerFunctionSelectors(); } + function install(bytes memory) public pure { + revert NonRootInstallNotSupported(); + } + /** * Register core tables in the World */ @@ -81,11 +85,9 @@ contract CoreModule is IModule, WorldContextConsumer { msgSender: _msgSender(), msgValue: 0, target: coreSystem, - funcSelectorAndArgs: abi.encodeWithSelector( - WorldRegistrationSystem.registerSystem.selector, - ResourceSelector.from(ROOT_NAMESPACE, CORE_SYSTEM_NAME), - coreSystem, - true + funcSelectorAndArgs: abi.encodeCall( + WorldRegistrationSystem.registerSystem, + (ResourceSelector.from(ROOT_NAMESPACE, CORE_SYSTEM_NAME), CoreSystem(coreSystem), true) ) }); } @@ -127,11 +129,9 @@ contract CoreModule is IModule, WorldContextConsumer { msgSender: _msgSender(), msgValue: 0, target: coreSystem, - funcSelectorAndArgs: abi.encodeWithSelector( - WorldRegistrationSystem.registerRootFunctionSelector.selector, - ResourceSelector.from(ROOT_NAMESPACE, CORE_SYSTEM_NAME), - functionSelectors[i], - functionSelectors[i] + funcSelectorAndArgs: abi.encodeCall( + WorldRegistrationSystem.registerRootFunctionSelector, + (ResourceSelector.from(ROOT_NAMESPACE, CORE_SYSTEM_NAME), functionSelectors[i], functionSelectors[i]) ) }); } diff --git a/packages/world/src/modules/core/implementations/AccessManagementSystem.sol b/packages/world/src/modules/core/implementations/AccessManagementSystem.sol index d85a759f84..c29d693ac0 100644 --- a/packages/world/src/modules/core/implementations/AccessManagementSystem.sol +++ b/packages/world/src/modules/core/implementations/AccessManagementSystem.sol @@ -19,7 +19,7 @@ contract AccessManagementSystem is System { */ function grantAccess(bytes32 resourceSelector, address grantee) public virtual { // Require the caller to own the namespace - AccessControl.requireOwnerOrSelf(resourceSelector, _msgSender()); + AccessControl.requireOwner(resourceSelector, _msgSender()); // Grant access to the given resource ResourceAccess.set(resourceSelector, grantee, true); @@ -31,7 +31,7 @@ contract AccessManagementSystem is System { */ function revokeAccess(bytes32 resourceSelector, address grantee) public virtual { // Require the caller to own the namespace - AccessControl.requireOwnerOrSelf(resourceSelector, _msgSender()); + AccessControl.requireOwner(resourceSelector, _msgSender()); // Revoke access from the given resource ResourceAccess.deleteRecord(resourceSelector, grantee); @@ -44,7 +44,7 @@ contract AccessManagementSystem is System { */ function transferOwnership(bytes16 namespace, address newOwner) public virtual { // Require the caller to own the namespace - AccessControl.requireOwnerOrSelf(namespace, _msgSender()); + AccessControl.requireOwner(namespace, _msgSender()); // Set namespace new owner NamespaceOwner.set(namespace, newOwner); diff --git a/packages/world/src/modules/core/implementations/StoreRegistrationSystem.sol b/packages/world/src/modules/core/implementations/StoreRegistrationSystem.sol index 4d9c77bc00..bf6a081237 100644 --- a/packages/world/src/modules/core/implementations/StoreRegistrationSystem.sol +++ b/packages/world/src/modules/core/implementations/StoreRegistrationSystem.sol @@ -59,7 +59,7 @@ contract StoreRegistrationSystem is System, IWorldErrors { }); } else { // otherwise require caller to own the namespace - AccessControl.requireOwnerOrSelf(namespace, _msgSender()); + AccessControl.requireOwner(namespace, _msgSender()); } // Require no resource to exist at this selector yet @@ -80,7 +80,7 @@ contract StoreRegistrationSystem is System, IWorldErrors { */ function registerStoreHook(bytes32 tableId, IStoreHook hookAddress, uint8 enabledHooksBitmap) public virtual { // Require caller to own the namespace - AccessControl.requireOwnerOrSelf(tableId, _msgSender()); + AccessControl.requireOwner(tableId, _msgSender()); // Register the hook StoreCore.registerStoreHook(tableId, hookAddress, enabledHooksBitmap); @@ -92,7 +92,7 @@ contract StoreRegistrationSystem is System, IWorldErrors { */ function unregisterStoreHook(bytes32 tableId, IStoreHook hookAddress) public virtual { // Require caller to own the namespace - AccessControl.requireOwnerOrSelf(tableId, _msgSender()); + AccessControl.requireOwner(tableId, _msgSender()); // Unregister the hook StoreCore.unregisterStoreHook(tableId, hookAddress); diff --git a/packages/world/src/modules/core/implementations/WorldRegistrationSystem.sol b/packages/world/src/modules/core/implementations/WorldRegistrationSystem.sol index e0153c7795..c64337b454 100644 --- a/packages/world/src/modules/core/implementations/WorldRegistrationSystem.sol +++ b/packages/world/src/modules/core/implementations/WorldRegistrationSystem.sol @@ -58,7 +58,7 @@ contract WorldRegistrationSystem is System, IWorldErrors { uint8 enabledHooksBitmap ) public virtual { // Require caller to own the namespace - AccessControl.requireOwnerOrSelf(resourceSelector, _msgSender()); + AccessControl.requireOwner(resourceSelector, _msgSender()); // Register the hook SystemHooks.push(resourceSelector, Hook.unwrap(SystemHookLib.encode(hookAddress, enabledHooksBitmap))); @@ -69,7 +69,7 @@ contract WorldRegistrationSystem is System, IWorldErrors { */ function unregisterSystemHook(bytes32 resourceSelector, ISystemHook hookAddress) public virtual { // Require caller to own the namespace - AccessControl.requireOwnerOrSelf(resourceSelector, _msgSender()); + AccessControl.requireOwner(resourceSelector, _msgSender()); // Remove the hook from the list of hooks for this resourceSelector in the system hooks table HookLib.filterListByAddress(SystemHooksTableId, resourceSelector, address(hookAddress)); @@ -98,7 +98,7 @@ contract WorldRegistrationSystem is System, IWorldErrors { // otherwise require caller to own the namespace bytes16 namespace = resourceSelector.getNamespace(); if (ResourceType.get(namespace) == Resource.NONE) registerNamespace(namespace); - else AccessControl.requireOwnerOrSelf(namespace, _msgSender()); + else AccessControl.requireOwner(namespace, _msgSender()); // Require no resource other than a system to exist at this selector yet Resource resourceType = ResourceType.get(resourceSelector); @@ -144,7 +144,7 @@ contract WorldRegistrationSystem is System, IWorldErrors { string memory systemFunctionArguments ) public returns (bytes4 worldFunctionSelector) { // Require the caller to own the namespace - AccessControl.requireOwnerOrSelf(resourceSelector, _msgSender()); + AccessControl.requireOwner(resourceSelector, _msgSender()); // Compute global function selector string memory namespaceString = ResourceSelector.toTrimmedString(resourceSelector.getNamespace()); @@ -179,7 +179,7 @@ contract WorldRegistrationSystem is System, IWorldErrors { bytes4 systemFunctionSelector ) public returns (bytes4) { // Require the caller to own the root namespace - AccessControl.requireOwnerOrSelf(ROOT_NAMESPACE, _msgSender()); + AccessControl.requireOwner(ROOT_NAMESPACE, _msgSender()); // Require the function selector to be globally unique bytes32 existingResourceSelector = FunctionSelectors.getResourceSelector(worldFunctionSelector); diff --git a/packages/world/src/modules/keysintable/KeysInTableHook.sol b/packages/world/src/modules/keysintable/KeysInTableHook.sol index 7d55c25b75..3d770693f1 100644 --- a/packages/world/src/modules/keysintable/KeysInTableHook.sol +++ b/packages/world/src/modules/keysintable/KeysInTableHook.sol @@ -6,6 +6,7 @@ import { Schema } from "@latticexyz/store/src/Schema.sol"; import { KeysInTable } from "./tables/KeysInTable.sol"; import { UsedKeysIndex } from "./tables/UsedKeysIndex.sol"; +import { KeysInTableTableId, UsedKeysIndexTableId } from "./constants.sol"; /** * Note: if a table with composite keys is used, only the first key is indexed @@ -15,20 +16,20 @@ contract KeysInTableHook is IStoreHook { bytes32 keysHash = keccak256(abi.encode(key)); // If the key has not yet been set in the table... - if (!UsedKeysIndex.getHas(tableId, keysHash)) { - uint40 length = uint40(KeysInTable.lengthKeys0(tableId)); + if (!UsedKeysIndex.getHas(UsedKeysIndexTableId, tableId, keysHash)) { + uint40 length = uint40(KeysInTable.lengthKeys0(KeysInTableTableId, tableId)); // Push the key to the list of keys in this table if (key.length > 0) { - KeysInTable.pushKeys0(tableId, key[0]); + KeysInTable.pushKeys0(KeysInTableTableId, tableId, key[0]); if (key.length > 1) { - KeysInTable.pushKeys1(tableId, key[1]); + KeysInTable.pushKeys1(KeysInTableTableId, tableId, key[1]); if (key.length > 2) { - KeysInTable.pushKeys2(tableId, key[2]); + KeysInTable.pushKeys2(KeysInTableTableId, tableId, key[2]); if (key.length > 3) { - KeysInTable.pushKeys3(tableId, key[3]); + KeysInTable.pushKeys3(KeysInTableTableId, tableId, key[3]); if (key.length > 4) { - KeysInTable.pushKeys4(tableId, key[4]); + KeysInTable.pushKeys4(KeysInTableTableId, tableId, key[4]); } } } @@ -36,7 +37,7 @@ contract KeysInTableHook is IStoreHook { } // Update the index to avoid duplicating this key in the array - UsedKeysIndex.set(tableId, keysHash, true, length); + UsedKeysIndex.set(UsedKeysIndexTableId, tableId, keysHash, true, length); } } @@ -58,60 +59,60 @@ contract KeysInTableHook is IStoreHook { function onBeforeDeleteRecord(bytes32 tableId, bytes32[] memory key, Schema) public { bytes32 keysHash = keccak256(abi.encode(key)); - (bool has, uint40 index) = UsedKeysIndex.get(tableId, keysHash); + (bool has, uint40 index) = UsedKeysIndex.get(UsedKeysIndexTableId, tableId, keysHash); // If the key was part of the table... if (has) { // Delete the index as the key is not in the table - UsedKeysIndex.deleteRecord(tableId, keysHash); + UsedKeysIndex.deleteRecord(UsedKeysIndexTableId, tableId, keysHash); - uint40 length = uint40(KeysInTable.lengthKeys0(tableId)); + uint40 length = uint40(KeysInTable.lengthKeys0(KeysInTableTableId, tableId)); if (length == 1) { // Delete the list of keys in this table - KeysInTable.deleteRecord(tableId); + KeysInTable.deleteRecord(KeysInTableTableId, tableId); } else { if (key.length > 0) { bytes32[] memory lastKeyTuple = new bytes32[](key.length); - bytes32 lastKey = KeysInTable.getItemKeys0(tableId, length - 1); + bytes32 lastKey = KeysInTable.getItemKeys0(KeysInTableTableId, tableId, length - 1); lastKeyTuple[0] = lastKey; // Remove the key from the list of keys in this table - KeysInTable.updateKeys0(tableId, index, lastKey); - KeysInTable.popKeys0(tableId); + KeysInTable.updateKeys0(KeysInTableTableId, tableId, index, lastKey); + KeysInTable.popKeys0(KeysInTableTableId, tableId); if (key.length > 1) { - lastKey = KeysInTable.getItemKeys1(tableId, length - 1); + lastKey = KeysInTable.getItemKeys1(KeysInTableTableId, tableId, length - 1); lastKeyTuple[1] = lastKey; // Remove the key from the list of keys in this table - KeysInTable.updateKeys1(tableId, index, lastKey); - KeysInTable.popKeys1(tableId); + KeysInTable.updateKeys1(KeysInTableTableId, tableId, index, lastKey); + KeysInTable.popKeys1(KeysInTableTableId, tableId); if (key.length > 2) { - lastKey = KeysInTable.getItemKeys2(tableId, length - 1); + lastKey = KeysInTable.getItemKeys2(KeysInTableTableId, tableId, length - 1); lastKeyTuple[2] = lastKey; // Swap and pop the key from the list of keys in this table - KeysInTable.updateKeys2(tableId, index, lastKey); - KeysInTable.popKeys2(tableId); + KeysInTable.updateKeys2(KeysInTableTableId, tableId, index, lastKey); + KeysInTable.popKeys2(KeysInTableTableId, tableId); if (key.length > 3) { - lastKey = KeysInTable.getItemKeys3(tableId, length - 1); + lastKey = KeysInTable.getItemKeys3(KeysInTableTableId, tableId, length - 1); lastKeyTuple[3] = lastKey; // Remove the key from the list of keys in this table - KeysInTable.updateKeys3(tableId, index, lastKey); - KeysInTable.popKeys3(tableId); + KeysInTable.updateKeys3(KeysInTableTableId, tableId, index, lastKey); + KeysInTable.popKeys3(KeysInTableTableId, tableId); if (key.length > 4) { - lastKey = KeysInTable.getItemKeys4(tableId, length - 1); + lastKey = KeysInTable.getItemKeys4(KeysInTableTableId, tableId, length - 1); lastKeyTuple[4] = lastKey; // Remove the key from the list of keys in this table - KeysInTable.updateKeys4(tableId, index, lastKey); - KeysInTable.popKeys4(tableId); + KeysInTable.updateKeys4(KeysInTableTableId, tableId, index, lastKey); + KeysInTable.popKeys4(KeysInTableTableId, tableId); } } } @@ -119,7 +120,7 @@ contract KeysInTableHook is IStoreHook { // Update the index of lastKey after swapping it with the deleted key bytes32 lastKeyHash = keccak256(abi.encode(lastKeyTuple)); - UsedKeysIndex.setIndex(tableId, lastKeyHash, index); + UsedKeysIndex.setIndex(UsedKeysIndexTableId, tableId, lastKeyHash, index); } } } diff --git a/packages/world/src/modules/keysintable/KeysInTableModule.sol b/packages/world/src/modules/keysintable/KeysInTableModule.sol index 253d9d0f9f..8ea6712b7f 100644 --- a/packages/world/src/modules/keysintable/KeysInTableModule.sol +++ b/packages/world/src/modules/keysintable/KeysInTableModule.sol @@ -11,10 +11,12 @@ import { IModule } from "../../interfaces/IModule.sol"; import { WorldContextConsumer } from "../../WorldContext.sol"; import { ResourceSelector } from "../../ResourceSelector.sol"; +import { revertWithBytes } from "../../revertWithBytes.sol"; import { KeysInTableHook } from "./KeysInTableHook.sol"; -import { KeysInTable, KeysInTableTableId } from "./tables/KeysInTable.sol"; -import { UsedKeysIndex, UsedKeysIndexTableId } from "./tables/UsedKeysIndex.sol"; +import { KeysInTable } from "./tables/KeysInTable.sol"; +import { UsedKeysIndex } from "./tables/UsedKeysIndex.sol"; +import { KeysInTableTableId, UsedKeysIndexTableId } from "./constants.sol"; /** * This module deploys a hook that is called when a value is set in the `sourceTableId` @@ -23,30 +25,34 @@ import { UsedKeysIndex, UsedKeysIndexTableId } from "./tables/UsedKeysIndex.sol" * * Note: if a table with composite keys is used, only the first key is indexed * - * Note: this module currently expects to be `delegatecalled` via World.installRootModule. - * Support for installing it via `World.installModule` depends on `World.callFrom` being implemented. + * Note: this module currently only supports `installRoot` (via `World.installRootModule`). + * TODO: add support for `install` (via `World.installModule`) by using `callFrom` with the `msgSender()` */ contract KeysInTableModule is IModule, WorldContextConsumer { using ResourceSelector for bytes32; // The KeysInTableHook is deployed once and infers the target table id // from the source table id (passed as argument to the hook methods) - KeysInTableHook immutable hook = new KeysInTableHook(); + KeysInTableHook private immutable hook = new KeysInTableHook(); function getName() public pure returns (bytes16) { return bytes16("keysInTable"); } - function install(bytes memory args) public override { + function installRoot(bytes memory args) public override { // Extract source table id from args bytes32 sourceTableId = abi.decode(args, (bytes32)); IBaseWorld world = IBaseWorld(_world()); + // Initialize variable to reuse in low level calls + bool success; + bytes memory returnData; + if (ResourceType.get(KeysInTableTableId) == Resource.NONE) { // Register the tables - KeysInTable.register(world); - UsedKeysIndex.register(world); + KeysInTable.register(world, KeysInTableTableId); + UsedKeysIndex.register(world, UsedKeysIndexTableId); // Grant the hook access to the tables world.grantAccess(KeysInTableTableId, address(hook)); @@ -54,17 +60,26 @@ contract KeysInTableModule is IModule, WorldContextConsumer { } // Register a hook that is called when a value is set in the source table - world.registerStoreHook( - sourceTableId, - hook, - StoreHookLib.encodeBitmap({ - onBeforeSetRecord: true, - onAfterSetRecord: false, - onBeforeSetField: false, - onAfterSetField: true, - onBeforeDeleteRecord: true, - onAfterDeleteRecord: false - }) + (success, returnData) = address(world).delegatecall( + abi.encodeCall( + world.registerStoreHook, + ( + sourceTableId, + hook, + StoreHookLib.encodeBitmap({ + onBeforeSetRecord: true, + onAfterSetRecord: false, + onBeforeSetField: false, + onAfterSetField: true, + onBeforeDeleteRecord: true, + onAfterDeleteRecord: false + }) + ) + ) ); } + + function install(bytes memory) public pure { + revert NonRootInstallNotSupported(); + } } diff --git a/packages/world/src/modules/keysintable/constants.sol b/packages/world/src/modules/keysintable/constants.sol index e39fa1032a..f1cbc12f40 100644 --- a/packages/world/src/modules/keysintable/constants.sol +++ b/packages/world/src/modules/keysintable/constants.sol @@ -5,3 +5,7 @@ pragma solidity >=0.8.0; // can be used for an identifier of the source table namespace to avoid // collisions between tables with the same name in different namespaces bytes8 constant MODULE_NAMESPACE = "keystab"; + +// TODO: once the config supports multiple namespaces, we don't have to manually construct the table id here +bytes32 constant KeysInTableTableId = bytes32(abi.encodePacked(bytes16(MODULE_NAMESPACE), bytes16("KeysInTable"))); +bytes32 constant UsedKeysIndexTableId = bytes32(abi.encodePacked(bytes16(MODULE_NAMESPACE), bytes16("UsedKeysIndex"))); diff --git a/packages/world/src/modules/keysintable/getKeysInTable.sol b/packages/world/src/modules/keysintable/getKeysInTable.sol index 9cdfb1abe0..beb58c2c27 100644 --- a/packages/world/src/modules/keysintable/getKeysInTable.sol +++ b/packages/world/src/modules/keysintable/getKeysInTable.sol @@ -6,6 +6,7 @@ import { Schema } from "@latticexyz/store/src/Schema.sol"; import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol"; import { KeysInTable } from "./tables/KeysInTable.sol"; +import { KeysInTableTableId } from "./constants.sol"; /** * Get a list of keys in the given table. @@ -21,22 +22,22 @@ function getKeysInTable(bytes32 tableId) view returns (bytes32[][] memory keyTup Schema schema = StoreSwitch.getKeySchema(tableId); uint256 numFields = schema.numFields(); - uint256 length = KeysInTable.lengthKeys0(tableId); + uint256 length = KeysInTable.lengthKeys0(KeysInTableTableId, 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 schema if (numFields > 0) { - keyTuples[i][0] = KeysInTable.getItemKeys0(tableId, i); + keyTuples[i][0] = KeysInTable.getItemKeys0(KeysInTableTableId, tableId, i); if (numFields > 1) { - keyTuples[i][1] = KeysInTable.getItemKeys1(tableId, i); + keyTuples[i][1] = KeysInTable.getItemKeys1(KeysInTableTableId, tableId, i); if (numFields > 2) { - keyTuples[i][2] = KeysInTable.getItemKeys2(tableId, i); + keyTuples[i][2] = KeysInTable.getItemKeys2(KeysInTableTableId, tableId, i); if (numFields > 3) { - keyTuples[i][3] = KeysInTable.getItemKeys3(tableId, i); + keyTuples[i][3] = KeysInTable.getItemKeys3(KeysInTableTableId, tableId, i); if (numFields > 4) { - keyTuples[i][4] = KeysInTable.getItemKeys4(tableId, i); + keyTuples[i][4] = KeysInTable.getItemKeys4(KeysInTableTableId, tableId, i); } } } @@ -56,22 +57,22 @@ function getKeysInTable(IStore store, bytes32 tableId) view returns (bytes32[][] Schema schema = store.getKeySchema(tableId); uint256 numFields = schema.numFields(); - uint256 length = KeysInTable.lengthKeys0(store, tableId); + uint256 length = KeysInTable.lengthKeys0(store, KeysInTableTableId, 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 schema if (numFields > 0) { - keyTuples[i][0] = KeysInTable.getItemKeys0(store, tableId, i); + keyTuples[i][0] = KeysInTable.getItemKeys0(store, KeysInTableTableId, tableId, i); if (numFields > 1) { - keyTuples[i][1] = KeysInTable.getItemKeys1(store, tableId, i); + keyTuples[i][1] = KeysInTable.getItemKeys1(store, KeysInTableTableId, tableId, i); if (numFields > 2) { - keyTuples[i][2] = KeysInTable.getItemKeys2(store, tableId, i); + keyTuples[i][2] = KeysInTable.getItemKeys2(store, KeysInTableTableId, tableId, i); if (numFields > 3) { - keyTuples[i][3] = KeysInTable.getItemKeys3(store, tableId, i); + keyTuples[i][3] = KeysInTable.getItemKeys3(store, KeysInTableTableId, tableId, i); if (numFields > 4) { - keyTuples[i][4] = KeysInTable.getItemKeys4(store, tableId, i); + keyTuples[i][4] = KeysInTable.getItemKeys4(store, KeysInTableTableId, tableId, i); } } } diff --git a/packages/world/src/modules/keysintable/hasKey.sol b/packages/world/src/modules/keysintable/hasKey.sol index d085a63e6c..c716bd0bd6 100644 --- a/packages/world/src/modules/keysintable/hasKey.sol +++ b/packages/world/src/modules/keysintable/hasKey.sol @@ -4,6 +4,7 @@ pragma solidity >=0.8.0; import { IStore } from "@latticexyz/store/src/IStore.sol"; import { UsedKeysIndex } from "./tables/UsedKeysIndex.sol"; +import { UsedKeysIndexTableId } from "./constants.sol"; /** * Get whether the key is in the given table. @@ -14,7 +15,7 @@ import { UsedKeysIndex } from "./tables/UsedKeysIndex.sol"; function hasKey(bytes32 tableId, bytes32[] memory key) view returns (bool) { bytes32 keysHash = keccak256(abi.encode(key)); - return UsedKeysIndex.getHas(tableId, keysHash); + return UsedKeysIndex.getHas(UsedKeysIndexTableId, tableId, keysHash); } /** @@ -23,5 +24,5 @@ function hasKey(bytes32 tableId, bytes32[] memory key) view returns (bool) { function hasKey(IStore store, bytes32 tableId, bytes32[] memory key) view returns (bool) { bytes32 keysHash = keccak256(abi.encode(key)); - return UsedKeysIndex.getHas(store, tableId, keysHash); + return UsedKeysIndex.getHas(store, UsedKeysIndexTableId, tableId, keysHash); } diff --git a/packages/world/src/modules/keysintable/tables/KeysInTable.sol b/packages/world/src/modules/keysintable/tables/KeysInTable.sol index c0a6832e7e..f919d8e848 100644 --- a/packages/world/src/modules/keysintable/tables/KeysInTable.sol +++ b/packages/world/src/modules/keysintable/tables/KeysInTable.sol @@ -17,9 +17,6 @@ import { EncodeArray } from "@latticexyz/store/src/tightcoder/EncodeArray.sol"; import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCounter.sol"; -bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("KeysInTable"))); -bytes32 constant KeysInTableTableId = _tableId; - struct KeysInTableData { bytes32[] keys0; bytes32[] keys1; @@ -66,17 +63,17 @@ library KeysInTable { } /** Register the table's key schema, value schema, key names and value names */ - function register() internal { + function register(bytes32 _tableId) internal { StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table's key schema, value schema, key names and value names (using the specified store) */ - function register(IStore _store) internal { + function register(IStore _store, bytes32 _tableId) internal { _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get keys0 */ - function getKeys0(bytes32 sourceTable) internal view returns (bytes32[] memory keys0) { + function getKeys0(bytes32 _tableId, bytes32 sourceTable) internal view returns (bytes32[] memory keys0) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -85,7 +82,11 @@ library KeysInTable { } /** Get keys0 (using the specified store) */ - function getKeys0(IStore _store, bytes32 sourceTable) internal view returns (bytes32[] memory keys0) { + function getKeys0( + IStore _store, + bytes32 _tableId, + bytes32 sourceTable + ) internal view returns (bytes32[] memory keys0) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -94,7 +95,7 @@ library KeysInTable { } /** Set keys0 */ - function setKeys0(bytes32 sourceTable, bytes32[] memory keys0) internal { + function setKeys0(bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys0) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -102,7 +103,7 @@ library KeysInTable { } /** Set keys0 (using the specified store) */ - function setKeys0(IStore _store, bytes32 sourceTable, bytes32[] memory keys0) internal { + function setKeys0(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys0) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -110,7 +111,7 @@ library KeysInTable { } /** Get the length of keys0 */ - function lengthKeys0(bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys0(bytes32 _tableId, bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -121,7 +122,7 @@ 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, bytes32 _tableId, bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -135,7 +136,7 @@ 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(bytes32 _tableId, bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -156,7 +157,12 @@ 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, + bytes32 _tableId, + bytes32 sourceTable, + uint256 _index + ) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -174,7 +180,7 @@ library KeysInTable { } /** Push an element to keys0 */ - function pushKeys0(bytes32 sourceTable, bytes32 _element) internal { + function pushKeys0(bytes32 _tableId, bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -182,7 +188,7 @@ library KeysInTable { } /** Push an element to keys0 (using the specified store) */ - function pushKeys0(IStore _store, bytes32 sourceTable, bytes32 _element) internal { + function pushKeys0(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -190,7 +196,7 @@ library KeysInTable { } /** Pop an element from keys0 */ - function popKeys0(bytes32 sourceTable) internal { + function popKeys0(bytes32 _tableId, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -198,7 +204,7 @@ library KeysInTable { } /** Pop an element from keys0 (using the specified store) */ - function popKeys0(IStore _store, bytes32 sourceTable) internal { + function popKeys0(IStore _store, bytes32 _tableId, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -209,7 +215,7 @@ 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(bytes32 _tableId, bytes32 sourceTable, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -222,7 +228,13 @@ 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, + bytes32 _tableId, + bytes32 sourceTable, + uint256 _index, + bytes32 _element + ) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -232,7 +244,7 @@ library KeysInTable { } /** Get keys1 */ - function getKeys1(bytes32 sourceTable) internal view returns (bytes32[] memory keys1) { + function getKeys1(bytes32 _tableId, bytes32 sourceTable) internal view returns (bytes32[] memory keys1) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -241,7 +253,11 @@ library KeysInTable { } /** Get keys1 (using the specified store) */ - function getKeys1(IStore _store, bytes32 sourceTable) internal view returns (bytes32[] memory keys1) { + function getKeys1( + IStore _store, + bytes32 _tableId, + bytes32 sourceTable + ) internal view returns (bytes32[] memory keys1) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -250,7 +266,7 @@ library KeysInTable { } /** Set keys1 */ - function setKeys1(bytes32 sourceTable, bytes32[] memory keys1) internal { + function setKeys1(bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys1) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -258,7 +274,7 @@ library KeysInTable { } /** Set keys1 (using the specified store) */ - function setKeys1(IStore _store, bytes32 sourceTable, bytes32[] memory keys1) internal { + function setKeys1(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys1) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -266,7 +282,7 @@ library KeysInTable { } /** Get the length of keys1 */ - function lengthKeys1(bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys1(bytes32 _tableId, bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -277,7 +293,7 @@ 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, bytes32 _tableId, bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -291,7 +307,7 @@ 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(bytes32 _tableId, bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -312,7 +328,12 @@ 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, + bytes32 _tableId, + bytes32 sourceTable, + uint256 _index + ) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -330,7 +351,7 @@ library KeysInTable { } /** Push an element to keys1 */ - function pushKeys1(bytes32 sourceTable, bytes32 _element) internal { + function pushKeys1(bytes32 _tableId, bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -338,7 +359,7 @@ library KeysInTable { } /** Push an element to keys1 (using the specified store) */ - function pushKeys1(IStore _store, bytes32 sourceTable, bytes32 _element) internal { + function pushKeys1(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -346,7 +367,7 @@ library KeysInTable { } /** Pop an element from keys1 */ - function popKeys1(bytes32 sourceTable) internal { + function popKeys1(bytes32 _tableId, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -354,7 +375,7 @@ library KeysInTable { } /** Pop an element from keys1 (using the specified store) */ - function popKeys1(IStore _store, bytes32 sourceTable) internal { + function popKeys1(IStore _store, bytes32 _tableId, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -365,7 +386,7 @@ 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(bytes32 _tableId, bytes32 sourceTable, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -378,7 +399,13 @@ 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, + bytes32 _tableId, + bytes32 sourceTable, + uint256 _index, + bytes32 _element + ) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -388,7 +415,7 @@ library KeysInTable { } /** Get keys2 */ - function getKeys2(bytes32 sourceTable) internal view returns (bytes32[] memory keys2) { + function getKeys2(bytes32 _tableId, bytes32 sourceTable) internal view returns (bytes32[] memory keys2) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -397,7 +424,11 @@ library KeysInTable { } /** Get keys2 (using the specified store) */ - function getKeys2(IStore _store, bytes32 sourceTable) internal view returns (bytes32[] memory keys2) { + function getKeys2( + IStore _store, + bytes32 _tableId, + bytes32 sourceTable + ) internal view returns (bytes32[] memory keys2) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -406,7 +437,7 @@ library KeysInTable { } /** Set keys2 */ - function setKeys2(bytes32 sourceTable, bytes32[] memory keys2) internal { + function setKeys2(bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys2) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -414,7 +445,7 @@ library KeysInTable { } /** Set keys2 (using the specified store) */ - function setKeys2(IStore _store, bytes32 sourceTable, bytes32[] memory keys2) internal { + function setKeys2(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys2) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -422,7 +453,7 @@ library KeysInTable { } /** Get the length of keys2 */ - function lengthKeys2(bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys2(bytes32 _tableId, bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -433,7 +464,7 @@ 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, bytes32 _tableId, bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -447,7 +478,7 @@ 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(bytes32 _tableId, bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -468,7 +499,12 @@ 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, + bytes32 _tableId, + bytes32 sourceTable, + uint256 _index + ) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -486,7 +522,7 @@ library KeysInTable { } /** Push an element to keys2 */ - function pushKeys2(bytes32 sourceTable, bytes32 _element) internal { + function pushKeys2(bytes32 _tableId, bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -494,7 +530,7 @@ library KeysInTable { } /** Push an element to keys2 (using the specified store) */ - function pushKeys2(IStore _store, bytes32 sourceTable, bytes32 _element) internal { + function pushKeys2(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -502,7 +538,7 @@ library KeysInTable { } /** Pop an element from keys2 */ - function popKeys2(bytes32 sourceTable) internal { + function popKeys2(bytes32 _tableId, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -510,7 +546,7 @@ library KeysInTable { } /** Pop an element from keys2 (using the specified store) */ - function popKeys2(IStore _store, bytes32 sourceTable) internal { + function popKeys2(IStore _store, bytes32 _tableId, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -521,7 +557,7 @@ 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(bytes32 _tableId, bytes32 sourceTable, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -534,7 +570,13 @@ 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, + bytes32 _tableId, + bytes32 sourceTable, + uint256 _index, + bytes32 _element + ) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -544,7 +586,7 @@ library KeysInTable { } /** Get keys3 */ - function getKeys3(bytes32 sourceTable) internal view returns (bytes32[] memory keys3) { + function getKeys3(bytes32 _tableId, bytes32 sourceTable) internal view returns (bytes32[] memory keys3) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -553,7 +595,11 @@ library KeysInTable { } /** Get keys3 (using the specified store) */ - function getKeys3(IStore _store, bytes32 sourceTable) internal view returns (bytes32[] memory keys3) { + function getKeys3( + IStore _store, + bytes32 _tableId, + bytes32 sourceTable + ) internal view returns (bytes32[] memory keys3) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -562,7 +608,7 @@ library KeysInTable { } /** Set keys3 */ - function setKeys3(bytes32 sourceTable, bytes32[] memory keys3) internal { + function setKeys3(bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys3) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -570,7 +616,7 @@ library KeysInTable { } /** Set keys3 (using the specified store) */ - function setKeys3(IStore _store, bytes32 sourceTable, bytes32[] memory keys3) internal { + function setKeys3(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys3) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -578,7 +624,7 @@ library KeysInTable { } /** Get the length of keys3 */ - function lengthKeys3(bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys3(bytes32 _tableId, bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -589,7 +635,7 @@ 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, bytes32 _tableId, bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -603,7 +649,7 @@ 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(bytes32 _tableId, bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -624,7 +670,12 @@ 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, + bytes32 _tableId, + bytes32 sourceTable, + uint256 _index + ) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -642,7 +693,7 @@ library KeysInTable { } /** Push an element to keys3 */ - function pushKeys3(bytes32 sourceTable, bytes32 _element) internal { + function pushKeys3(bytes32 _tableId, bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -650,7 +701,7 @@ library KeysInTable { } /** Push an element to keys3 (using the specified store) */ - function pushKeys3(IStore _store, bytes32 sourceTable, bytes32 _element) internal { + function pushKeys3(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -658,7 +709,7 @@ library KeysInTable { } /** Pop an element from keys3 */ - function popKeys3(bytes32 sourceTable) internal { + function popKeys3(bytes32 _tableId, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -666,7 +717,7 @@ library KeysInTable { } /** Pop an element from keys3 (using the specified store) */ - function popKeys3(IStore _store, bytes32 sourceTable) internal { + function popKeys3(IStore _store, bytes32 _tableId, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -677,7 +728,7 @@ 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(bytes32 _tableId, bytes32 sourceTable, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -690,7 +741,13 @@ 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, + bytes32 _tableId, + bytes32 sourceTable, + uint256 _index, + bytes32 _element + ) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -700,7 +757,7 @@ library KeysInTable { } /** Get keys4 */ - function getKeys4(bytes32 sourceTable) internal view returns (bytes32[] memory keys4) { + function getKeys4(bytes32 _tableId, bytes32 sourceTable) internal view returns (bytes32[] memory keys4) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -709,7 +766,11 @@ library KeysInTable { } /** Get keys4 (using the specified store) */ - function getKeys4(IStore _store, bytes32 sourceTable) internal view returns (bytes32[] memory keys4) { + function getKeys4( + IStore _store, + bytes32 _tableId, + bytes32 sourceTable + ) internal view returns (bytes32[] memory keys4) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -718,7 +779,7 @@ library KeysInTable { } /** Set keys4 */ - function setKeys4(bytes32 sourceTable, bytes32[] memory keys4) internal { + function setKeys4(bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys4) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -726,7 +787,7 @@ library KeysInTable { } /** Set keys4 (using the specified store) */ - function setKeys4(IStore _store, bytes32 sourceTable, bytes32[] memory keys4) internal { + function setKeys4(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys4) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -734,7 +795,7 @@ library KeysInTable { } /** Get the length of keys4 */ - function lengthKeys4(bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys4(bytes32 _tableId, bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -745,7 +806,7 @@ 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, bytes32 _tableId, bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -759,7 +820,7 @@ 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(bytes32 _tableId, bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -780,7 +841,12 @@ 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, + bytes32 _tableId, + bytes32 sourceTable, + uint256 _index + ) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -798,7 +864,7 @@ library KeysInTable { } /** Push an element to keys4 */ - function pushKeys4(bytes32 sourceTable, bytes32 _element) internal { + function pushKeys4(bytes32 _tableId, bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -806,7 +872,7 @@ library KeysInTable { } /** Push an element to keys4 (using the specified store) */ - function pushKeys4(IStore _store, bytes32 sourceTable, bytes32 _element) internal { + function pushKeys4(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -814,7 +880,7 @@ library KeysInTable { } /** Pop an element from keys4 */ - function popKeys4(bytes32 sourceTable) internal { + function popKeys4(bytes32 _tableId, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -822,7 +888,7 @@ library KeysInTable { } /** Pop an element from keys4 (using the specified store) */ - function popKeys4(IStore _store, bytes32 sourceTable) internal { + function popKeys4(IStore _store, bytes32 _tableId, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -833,7 +899,7 @@ 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(bytes32 _tableId, bytes32 sourceTable, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -846,7 +912,13 @@ 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, + bytes32 _tableId, + bytes32 sourceTable, + uint256 _index, + bytes32 _element + ) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -856,7 +928,7 @@ library KeysInTable { } /** Get the full data */ - function get(bytes32 sourceTable) internal view returns (KeysInTableData memory _table) { + function get(bytes32 _tableId, bytes32 sourceTable) internal view returns (KeysInTableData memory _table) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -865,7 +937,11 @@ 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, + bytes32 _tableId, + bytes32 sourceTable + ) internal view returns (KeysInTableData memory _table) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -875,6 +951,7 @@ library KeysInTable { /** Set the full data using individual values */ function set( + bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys0, bytes32[] memory keys1, @@ -893,6 +970,7 @@ library KeysInTable { /** Set the full data using individual values (using the specified store) */ function set( IStore _store, + bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys0, bytes32[] memory keys1, @@ -909,13 +987,13 @@ library KeysInTable { } /** Set the full data using the data struct */ - function set(bytes32 sourceTable, KeysInTableData memory _table) internal { - set(sourceTable, _table.keys0, _table.keys1, _table.keys2, _table.keys3, _table.keys4); + function set(bytes32 _tableId, bytes32 sourceTable, KeysInTableData memory _table) internal { + set(_tableId, sourceTable, _table.keys0, _table.keys1, _table.keys2, _table.keys3, _table.keys4); } /** Set the full data using the data struct (using the specified store) */ - function set(IStore _store, bytes32 sourceTable, KeysInTableData memory _table) internal { - set(_store, sourceTable, _table.keys0, _table.keys1, _table.keys2, _table.keys3, _table.keys4); + function set(IStore _store, bytes32 _tableId, bytes32 sourceTable, KeysInTableData memory _table) internal { + set(_store, _tableId, sourceTable, _table.keys0, _table.keys1, _table.keys2, _table.keys3, _table.keys4); } /** @@ -1002,7 +1080,7 @@ library KeysInTable { } /* Delete all data for given keys */ - function deleteRecord(bytes32 sourceTable) internal { + function deleteRecord(bytes32 _tableId, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -1010,7 +1088,7 @@ library KeysInTable { } /* Delete all data for given keys (using the specified store) */ - function deleteRecord(IStore _store, bytes32 sourceTable) internal { + function deleteRecord(IStore _store, bytes32 _tableId, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; diff --git a/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol b/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol index c83423843e..a18fcc0470 100644 --- a/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol +++ b/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol @@ -17,9 +17,6 @@ import { EncodeArray } from "@latticexyz/store/src/tightcoder/EncodeArray.sol"; import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCounter.sol"; -bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("UsedKeysIndex"))); -bytes32 constant UsedKeysIndexTableId = _tableId; - library UsedKeysIndex { /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { @@ -54,17 +51,17 @@ library UsedKeysIndex { } /** Register the table's key schema, value schema, key names and value names */ - function register() internal { + function register(bytes32 _tableId) internal { StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table's key schema, value schema, key names and value names (using the specified store) */ - function register(IStore _store) internal { + function register(IStore _store, bytes32 _tableId) internal { _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get has */ - function getHas(bytes32 sourceTable, bytes32 keysHash) internal view returns (bool has) { + function getHas(bytes32 _tableId, bytes32 sourceTable, bytes32 keysHash) internal view returns (bool has) { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; @@ -74,7 +71,12 @@ 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, + bytes32 _tableId, + bytes32 sourceTable, + bytes32 keysHash + ) internal view returns (bool has) { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; @@ -84,7 +86,7 @@ library UsedKeysIndex { } /** Set has */ - function setHas(bytes32 sourceTable, bytes32 keysHash, bool has) internal { + function setHas(bytes32 _tableId, bytes32 sourceTable, bytes32 keysHash, bool has) internal { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; @@ -93,7 +95,7 @@ library UsedKeysIndex { } /** Set has (using the specified store) */ - function setHas(IStore _store, bytes32 sourceTable, bytes32 keysHash, bool has) internal { + function setHas(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32 keysHash, bool has) internal { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; @@ -102,7 +104,7 @@ library UsedKeysIndex { } /** Get index */ - function getIndex(bytes32 sourceTable, bytes32 keysHash) internal view returns (uint40 index) { + function getIndex(bytes32 _tableId, bytes32 sourceTable, bytes32 keysHash) internal view returns (uint40 index) { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; @@ -112,7 +114,12 @@ 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, + bytes32 _tableId, + bytes32 sourceTable, + bytes32 keysHash + ) internal view returns (uint40 index) { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; @@ -122,7 +129,7 @@ library UsedKeysIndex { } /** Set index */ - function setIndex(bytes32 sourceTable, bytes32 keysHash, uint40 index) internal { + function setIndex(bytes32 _tableId, bytes32 sourceTable, bytes32 keysHash, uint40 index) internal { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; @@ -131,7 +138,7 @@ library UsedKeysIndex { } /** Set index (using the specified store) */ - function setIndex(IStore _store, bytes32 sourceTable, bytes32 keysHash, uint40 index) internal { + function setIndex(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32 keysHash, uint40 index) internal { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; @@ -140,7 +147,7 @@ library UsedKeysIndex { } /** Get the full data */ - function get(bytes32 sourceTable, bytes32 keysHash) internal view returns (bool has, uint40 index) { + function get(bytes32 _tableId, bytes32 sourceTable, bytes32 keysHash) internal view returns (bool has, uint40 index) { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; @@ -150,7 +157,12 @@ 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, + bytes32 _tableId, + bytes32 sourceTable, + bytes32 keysHash + ) internal view returns (bool has, uint40 index) { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; @@ -160,7 +172,7 @@ library UsedKeysIndex { } /** Set the full data using individual values */ - function set(bytes32 sourceTable, bytes32 keysHash, bool has, uint40 index) internal { + function set(bytes32 _tableId, bytes32 sourceTable, bytes32 keysHash, bool has, uint40 index) internal { bytes memory _data = encode(has, index); bytes32[] memory _keyTuple = new bytes32[](2); @@ -171,7 +183,14 @@ library UsedKeysIndex { } /** 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, + bytes32 _tableId, + bytes32 sourceTable, + bytes32 keysHash, + bool has, + uint40 index + ) internal { bytes memory _data = encode(has, index); bytes32[] memory _keyTuple = new bytes32[](2); @@ -203,7 +222,7 @@ library UsedKeysIndex { } /* Delete all data for given keys */ - function deleteRecord(bytes32 sourceTable, bytes32 keysHash) internal { + function deleteRecord(bytes32 _tableId, bytes32 sourceTable, bytes32 keysHash) internal { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; @@ -212,7 +231,7 @@ library UsedKeysIndex { } /* Delete all data for given keys (using the specified store) */ - function deleteRecord(IStore _store, bytes32 sourceTable, bytes32 keysHash) internal { + function deleteRecord(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32 keysHash) internal { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; diff --git a/packages/world/src/modules/keyswithvalue/KeysWithValueModule.sol b/packages/world/src/modules/keyswithvalue/KeysWithValueModule.sol index 1cd17800d2..dad35bd968 100644 --- a/packages/world/src/modules/keyswithvalue/KeysWithValueModule.sol +++ b/packages/world/src/modules/keyswithvalue/KeysWithValueModule.sol @@ -9,6 +9,7 @@ import { IModule } from "../../interfaces/IModule.sol"; import { WorldContextConsumer } from "../../WorldContext.sol"; import { ResourceSelector } from "../../ResourceSelector.sol"; +import { revertWithBytes } from "../../revertWithBytes.sol"; import { MODULE_NAMESPACE } from "./constants.sol"; import { KeysWithValueHook } from "./KeysWithValueHook.sol"; @@ -23,43 +24,55 @@ import { getTargetTableSelector } from "../utils/getTargetTableSelector.sol"; * * Note: if a table with composite keys is used, only the first key is indexed * - * Note: this module currently expects to be `delegatecalled` via World.installRootModule. - * Support for installing it via `World.installModule` depends on `World.callFrom` being implemented. + * Note: this module currently only supports `installRoot` (via `World.installRootModule`). + * TODO: add support for `install` (via `World.installModule`) by using `callFrom` with the `msgSender()` */ contract KeysWithValueModule is IModule, WorldContextConsumer { using ResourceSelector for bytes32; // The KeysWithValueHook is deployed once and infers the target table id // from the source table id (passed as argument to the hook methods) - KeysWithValueHook immutable hook = new KeysWithValueHook(); + KeysWithValueHook private immutable hook = new KeysWithValueHook(); function getName() public pure returns (bytes16) { return bytes16("index"); } - function install(bytes memory args) public override { + function installRoot(bytes memory args) public { // Extract source table id from args bytes32 sourceTableId = abi.decode(args, (bytes32)); bytes32 targetTableSelector = getTargetTableSelector(MODULE_NAMESPACE, sourceTableId); + IBaseWorld world = IBaseWorld(_world()); + // Register the target table - KeysWithValue.register(IBaseWorld(_world()), targetTableSelector); + KeysWithValue.register(world, targetTableSelector); // Grant the hook access to the target table - IBaseWorld(_world()).grantAccess(targetTableSelector, address(hook)); + world.grantAccess(targetTableSelector, address(hook)); // Register a hook that is called when a value is set in the source table - StoreSwitch.registerStoreHook( - sourceTableId, - hook, - StoreHookLib.encodeBitmap({ - onBeforeSetRecord: true, - onAfterSetRecord: false, - onBeforeSetField: true, - onAfterSetField: true, - onBeforeDeleteRecord: true, - onAfterDeleteRecord: false - }) + (bool success, bytes memory returnData) = address(world).delegatecall( + abi.encodeCall( + world.registerStoreHook, + ( + sourceTableId, + hook, + StoreHookLib.encodeBitmap({ + onBeforeSetRecord: true, + onAfterSetRecord: false, + onBeforeSetField: true, + onAfterSetField: true, + onBeforeDeleteRecord: true, + onAfterDeleteRecord: false + }) + ) + ) ); + if (!success) revertWithBytes(returnData); + } + + function install(bytes memory) public pure { + revert NonRootInstallNotSupported(); } } diff --git a/packages/world/src/modules/std-delegations/StandardDelegationsModule.sol b/packages/world/src/modules/std-delegations/StandardDelegationsModule.sol index 97d31ef2ec..74057574dc 100644 --- a/packages/world/src/modules/std-delegations/StandardDelegationsModule.sol +++ b/packages/world/src/modules/std-delegations/StandardDelegationsModule.sol @@ -6,6 +6,7 @@ import { IModule } from "../../interfaces/IModule.sol"; import { WorldContextConsumer } from "../../WorldContext.sol"; import { ResourceSelector } from "../../ResourceSelector.sol"; +import { revertWithBytes } from "../../revertWithBytes.sol"; import { CallboundDelegationControl } from "./CallboundDelegationControl.sol"; import { TimeboundDelegationControl } from "./TimeboundDelegationControl.sol"; @@ -25,15 +26,26 @@ contract StandardDelegationsModule is IModule, WorldContextConsumer { return MODULE_NAME; } - function install(bytes memory) public { + function installRoot(bytes memory) public { IBaseWorld world = IBaseWorld(_world()); // Register tables - CallboundDelegations.register(world); - TimeboundDelegations.register(world); + CallboundDelegations.register(); + TimeboundDelegations.register(); // Register systems - world.registerSystem(CALLBOUND_DELEGATION, callboundDelegationControl, true); - world.registerSystem(TIMEBOUND_DELEGATION, timeboundDelegationControl, true); + (bool success, bytes memory returnData) = address(world).delegatecall( + abi.encodeCall(world.registerSystem, (CALLBOUND_DELEGATION, callboundDelegationControl, true)) + ); + if (!success) revertWithBytes(returnData); + + (success, returnData) = address(world).delegatecall( + abi.encodeCall(world.registerSystem, (TIMEBOUND_DELEGATION, timeboundDelegationControl, true)) + ); + if (!success) revertWithBytes(returnData); + } + + function install(bytes memory) public pure { + revert NonRootInstallNotSupported(); } } diff --git a/packages/world/src/modules/uniqueentity/UniqueEntityModule.sol b/packages/world/src/modules/uniqueentity/UniqueEntityModule.sol index cfff61f6bc..18d87c7dfe 100644 --- a/packages/world/src/modules/uniqueentity/UniqueEntityModule.sol +++ b/packages/world/src/modules/uniqueentity/UniqueEntityModule.sol @@ -19,12 +19,16 @@ import { NAMESPACE, MODULE_NAME, SYSTEM_NAME, TABLE_NAME } from "./constants.sol contract UniqueEntityModule is IModule, WorldContextConsumer { // Since the UniqueEntitySystem only exists once per World and writes to // known tables, we can deploy it once and register it in multiple Worlds. - UniqueEntitySystem immutable uniqueEntitySystem = new UniqueEntitySystem(); + UniqueEntitySystem private immutable uniqueEntitySystem = new UniqueEntitySystem(); function getName() public pure returns (bytes16) { return MODULE_NAME; } + function installRoot(bytes memory args) public { + install(args); + } + function install(bytes memory) public { IBaseWorld world = IBaseWorld(_world()); diff --git a/packages/world/test/KeysInTableModule.t.sol b/packages/world/test/KeysInTableModule.t.sol index 1968d14a00..3446b8e238 100644 --- a/packages/world/test/KeysInTableModule.t.sol +++ b/packages/world/test/KeysInTableModule.t.sol @@ -20,30 +20,30 @@ import { hasKey } from "../src/modules/keysintable/hasKey.sol"; contract KeysInTableModuleTest is Test, GasReporter { using ResourceSelector for bytes32; - IBaseWorld world; - KeysInTableModule keysInTableModule = new KeysInTableModule(); // Modules can be deployed once and installed multiple times - - bytes16 namespace = ROOT_NAMESPACE; - bytes16 name = bytes16("source"); - bytes16 singletonName = bytes16("singleton"); - bytes16 compositeName = bytes16("composite"); - bytes32 key1 = keccak256("test"); - bytes32[] keyTuple1; - bytes32 key2 = keccak256("test2"); - bytes32[] keyTuple2; - bytes32 key3 = keccak256("test3"); - bytes32[] keyTuple3; - - Schema tableValueSchema; - Schema tableKeySchema; - Schema singletonKeySchema; - Schema compositeKeySchema; - bytes32 tableId = ResourceSelector.from(namespace, name); - bytes32 singletonTableId = ResourceSelector.from(namespace, singletonName); - bytes32 compositeTableId = ResourceSelector.from(namespace, compositeName); - - uint256 val1 = 123; - uint256 val2 = 42; + IBaseWorld private world; + KeysInTableModule private keysInTableModule = new KeysInTableModule(); // Modules can be deployed once and installed multiple times + + bytes16 private namespace = ROOT_NAMESPACE; + bytes16 private name = bytes16("source"); + bytes16 private singletonName = bytes16("singleton"); + bytes16 private compositeName = bytes16("composite"); + bytes32 private key1 = keccak256("test"); + bytes32[] private keyTuple1; + bytes32 private key2 = keccak256("test2"); + bytes32[] private keyTuple2; + bytes32 private key3 = keccak256("test3"); + bytes32[] private keyTuple3; + + Schema private tableValueSchema; + Schema private tableKeySchema; + Schema private singletonKeySchema; + Schema private compositeKeySchema; + bytes32 private tableId = ResourceSelector.from(namespace, name); + bytes32 private singletonTableId = ResourceSelector.from(namespace, singletonName); + bytes32 private compositeTableId = ResourceSelector.from(namespace, compositeName); + + uint256 private val1 = 123; + uint256 private val2 = 42; function setUp() public { tableValueSchema = SchemaEncodeHelper.encode(SchemaType.UINT256); diff --git a/packages/world/test/World.t.sol b/packages/world/test/World.t.sol index 1be65a4342..bfe8ee79c5 100644 --- a/packages/world/test/World.t.sol +++ b/packages/world/test/World.t.sol @@ -304,8 +304,8 @@ contract WorldTest is Test, GasReporter { _expectAccessDenied(address(0x01), namespace, ""); world.registerTable(otherTableSelector, defaultKeySchema, valueSchema, keyNames, fieldNames); - // Expect the World to be allowed to call registerTable - vm.prank(address(world)); + // Expect the World to not be allowed to call registerTable via an external call + _expectAccessDenied(address(world), namespace, ""); world.registerTable(otherTableSelector, defaultKeySchema, valueSchema, keyNames, fieldNames); } @@ -362,8 +362,8 @@ contract WorldTest is Test, GasReporter { _expectAccessDenied(address(0x01), "", ""); world.registerSystem(ResourceSelector.from("", "rootSystem"), yetAnotherSystem, true); - // Expect the registration to succeed when coming from the World - vm.prank(address(world)); + // Expect the registration to fail when coming from the World (since the World address doesn't have access) + _expectAccessDenied(address(world), "", ""); world.registerSystem(ResourceSelector.from("", "rootSystem"), yetAnotherSystem, true); } @@ -1047,8 +1047,8 @@ contract WorldTest is Test, GasReporter { _expectAccessDenied(address(0x01), "", ""); world.registerRootFunctionSelector(resourceSelector, worldFunc, sysFunc); - // Expect the World to be able to register a root function selector - vm.prank(address(world)); + // Expect the World to not be able to register a root function selector when calling the function externally + _expectAccessDenied(address(world), "", ""); world.registerRootFunctionSelector(resourceSelector, "smth", "smth"); startGasReport("Register a root function selector"); From 826b08bf31dde31a6e6c6aed9b381471e3dee655 Mon Sep 17 00:00:00 2001 From: alvrs Date: Tue, 12 Sep 2023 17:40:57 +0100 Subject: [PATCH 2/4] artifacts and gas-report --- .../abi/CoreModule.sol/CoreModule.abi.json | 23 ++++ .../CoreModule.sol/CoreModule.abi.json.d.ts | 23 ++++ .../world/abi/IModule.sol/IModule.abi.json | 23 ++++ .../abi/IModule.sol/IModule.abi.json.d.ts | 23 ++++ .../KeysInTableModule.abi.json | 25 +++- .../KeysInTableModule.abi.json.d.ts | 25 +++- .../KeysWithValueModule.abi.json | 54 ++++----- .../KeysWithValueModule.abi.json.d.ts | 54 ++++----- .../StandardDelegationsModule.abi.json | 108 ++++++++++++++++++ .../StandardDelegationsModule.abi.json.d.ts | 108 ++++++++++++++++++ .../UniqueEntityModule.abi.json | 23 ++++ .../UniqueEntityModule.abi.json.d.ts | 23 ++++ packages/world/gas-report.json | 58 +++++----- 13 files changed, 471 insertions(+), 99 deletions(-) diff --git a/packages/world/abi/CoreModule.sol/CoreModule.abi.json b/packages/world/abi/CoreModule.sol/CoreModule.abi.json index 43437a8fc5..472cd505dc 100644 --- a/packages/world/abi/CoreModule.sol/CoreModule.abi.json +++ b/packages/world/abi/CoreModule.sol/CoreModule.abi.json @@ -1,4 +1,9 @@ [ + { + "inputs": [], + "name": "NonRootInstallNotSupported", + "type": "error" + }, { "inputs": [ { @@ -21,6 +26,11 @@ "name": "RequiredModuleNotFound", "type": "error" }, + { + "inputs": [], + "name": "RootInstallModeNotSupported", + "type": "error" + }, { "inputs": [ { @@ -145,6 +155,19 @@ ], "name": "install", "outputs": [], + "stateMutability": "pure", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes", + "name": "", + "type": "bytes" + } + ], + "name": "installRoot", + "outputs": [], "stateMutability": "nonpayable", "type": "function" } diff --git a/packages/world/abi/CoreModule.sol/CoreModule.abi.json.d.ts b/packages/world/abi/CoreModule.sol/CoreModule.abi.json.d.ts index 60ff77131c..1f2c1eb490 100644 --- a/packages/world/abi/CoreModule.sol/CoreModule.abi.json.d.ts +++ b/packages/world/abi/CoreModule.sol/CoreModule.abi.json.d.ts @@ -1,4 +1,9 @@ declare const abi: [ + { + inputs: []; + name: "NonRootInstallNotSupported"; + type: "error"; + }, { inputs: [ { @@ -21,6 +26,11 @@ declare const abi: [ name: "RequiredModuleNotFound"; type: "error"; }, + { + inputs: []; + name: "RootInstallModeNotSupported"; + type: "error"; + }, { inputs: [ { @@ -145,6 +155,19 @@ declare const abi: [ ]; name: "install"; outputs: []; + stateMutability: "pure"; + type: "function"; + }, + { + inputs: [ + { + internalType: "bytes"; + name: ""; + type: "bytes"; + } + ]; + name: "installRoot"; + outputs: []; stateMutability: "nonpayable"; type: "function"; } diff --git a/packages/world/abi/IModule.sol/IModule.abi.json b/packages/world/abi/IModule.sol/IModule.abi.json index 0296b60f47..e4683fbb5e 100644 --- a/packages/world/abi/IModule.sol/IModule.abi.json +++ b/packages/world/abi/IModule.sol/IModule.abi.json @@ -1,4 +1,9 @@ [ + { + "inputs": [], + "name": "NonRootInstallNotSupported", + "type": "error" + }, { "inputs": [ { @@ -10,6 +15,11 @@ "name": "RequiredModuleNotFound", "type": "error" }, + { + "inputs": [], + "name": "RootInstallModeNotSupported", + "type": "error" + }, { "inputs": [], "name": "getName", @@ -35,5 +45,18 @@ "outputs": [], "stateMutability": "nonpayable", "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes", + "name": "args", + "type": "bytes" + } + ], + "name": "installRoot", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" } ] \ No newline at end of file diff --git a/packages/world/abi/IModule.sol/IModule.abi.json.d.ts b/packages/world/abi/IModule.sol/IModule.abi.json.d.ts index d9577d5f19..522a06fb65 100644 --- a/packages/world/abi/IModule.sol/IModule.abi.json.d.ts +++ b/packages/world/abi/IModule.sol/IModule.abi.json.d.ts @@ -1,4 +1,9 @@ declare const abi: [ + { + inputs: []; + name: "NonRootInstallNotSupported"; + type: "error"; + }, { inputs: [ { @@ -10,6 +15,11 @@ declare const abi: [ name: "RequiredModuleNotFound"; type: "error"; }, + { + inputs: []; + name: "RootInstallModeNotSupported"; + type: "error"; + }, { inputs: []; name: "getName"; @@ -35,6 +45,19 @@ declare const abi: [ outputs: []; stateMutability: "nonpayable"; type: "function"; + }, + { + inputs: [ + { + internalType: "bytes"; + name: "args"; + type: "bytes"; + } + ]; + name: "installRoot"; + outputs: []; + stateMutability: "nonpayable"; + type: "function"; } ]; export default abi; diff --git a/packages/world/abi/KeysInTableModule.sol/KeysInTableModule.abi.json b/packages/world/abi/KeysInTableModule.sol/KeysInTableModule.abi.json index 64373ae108..54e901cb10 100644 --- a/packages/world/abi/KeysInTableModule.sol/KeysInTableModule.abi.json +++ b/packages/world/abi/KeysInTableModule.sol/KeysInTableModule.abi.json @@ -1,4 +1,9 @@ [ + { + "inputs": [], + "name": "NonRootInstallNotSupported", + "type": "error" + }, { "inputs": [ { @@ -10,6 +15,11 @@ "name": "RequiredModuleNotFound", "type": "error" }, + { + "inputs": [], + "name": "RootInstallModeNotSupported", + "type": "error" + }, { "inputs": [ { @@ -43,12 +53,25 @@ "inputs": [ { "internalType": "bytes", - "name": "args", + "name": "", "type": "bytes" } ], "name": "install", "outputs": [], + "stateMutability": "pure", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes", + "name": "args", + "type": "bytes" + } + ], + "name": "installRoot", + "outputs": [], "stateMutability": "nonpayable", "type": "function" } diff --git a/packages/world/abi/KeysInTableModule.sol/KeysInTableModule.abi.json.d.ts b/packages/world/abi/KeysInTableModule.sol/KeysInTableModule.abi.json.d.ts index 7ceee8b586..96427e2f6b 100644 --- a/packages/world/abi/KeysInTableModule.sol/KeysInTableModule.abi.json.d.ts +++ b/packages/world/abi/KeysInTableModule.sol/KeysInTableModule.abi.json.d.ts @@ -1,4 +1,9 @@ declare const abi: [ + { + inputs: []; + name: "NonRootInstallNotSupported"; + type: "error"; + }, { inputs: [ { @@ -10,6 +15,11 @@ declare const abi: [ name: "RequiredModuleNotFound"; type: "error"; }, + { + inputs: []; + name: "RootInstallModeNotSupported"; + type: "error"; + }, { inputs: [ { @@ -43,12 +53,25 @@ declare const abi: [ inputs: [ { internalType: "bytes"; - name: "args"; + name: ""; type: "bytes"; } ]; name: "install"; outputs: []; + stateMutability: "pure"; + type: "function"; + }, + { + inputs: [ + { + internalType: "bytes"; + name: "args"; + type: "bytes"; + } + ]; + name: "installRoot"; + outputs: []; stateMutability: "nonpayable"; type: "function"; } diff --git a/packages/world/abi/KeysWithValueModule.sol/KeysWithValueModule.abi.json b/packages/world/abi/KeysWithValueModule.sol/KeysWithValueModule.abi.json index aeaee69069..54e901cb10 100644 --- a/packages/world/abi/KeysWithValueModule.sol/KeysWithValueModule.abi.json +++ b/packages/world/abi/KeysWithValueModule.sol/KeysWithValueModule.abi.json @@ -1,13 +1,7 @@ [ { - "inputs": [ - { - "internalType": "uint256", - "name": "length", - "type": "uint256" - } - ], - "name": "PackedCounter_InvalidLength", + "inputs": [], + "name": "NonRootInstallNotSupported", "type": "error" }, { @@ -21,46 +15,25 @@ "name": "RequiredModuleNotFound", "type": "error" }, - { - "inputs": [ - { - "internalType": "uint256", - "name": "length", - "type": "uint256" - } - ], - "name": "SchemaLib_InvalidLength", - "type": "error" - }, { "inputs": [], - "name": "SchemaLib_StaticTypeAfterDynamicType", + "name": "RootInstallModeNotSupported", "type": "error" }, { "inputs": [ - { - "internalType": "bytes", - "name": "data", - "type": "bytes" - }, - { - "internalType": "uint256", - "name": "start", - "type": "uint256" - }, { "internalType": "uint256", - "name": "end", + "name": "length", "type": "uint256" } ], - "name": "Slice_OutOfBounds", + "name": "SchemaLib_InvalidLength", "type": "error" }, { "inputs": [], - "name": "StoreCore_NotDynamicField", + "name": "SchemaLib_StaticTypeAfterDynamicType", "type": "error" }, { @@ -80,12 +53,25 @@ "inputs": [ { "internalType": "bytes", - "name": "args", + "name": "", "type": "bytes" } ], "name": "install", "outputs": [], + "stateMutability": "pure", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes", + "name": "args", + "type": "bytes" + } + ], + "name": "installRoot", + "outputs": [], "stateMutability": "nonpayable", "type": "function" } diff --git a/packages/world/abi/KeysWithValueModule.sol/KeysWithValueModule.abi.json.d.ts b/packages/world/abi/KeysWithValueModule.sol/KeysWithValueModule.abi.json.d.ts index 4b124c1e24..96427e2f6b 100644 --- a/packages/world/abi/KeysWithValueModule.sol/KeysWithValueModule.abi.json.d.ts +++ b/packages/world/abi/KeysWithValueModule.sol/KeysWithValueModule.abi.json.d.ts @@ -1,13 +1,7 @@ declare const abi: [ { - inputs: [ - { - internalType: "uint256"; - name: "length"; - type: "uint256"; - } - ]; - name: "PackedCounter_InvalidLength"; + inputs: []; + name: "NonRootInstallNotSupported"; type: "error"; }, { @@ -21,46 +15,25 @@ declare const abi: [ name: "RequiredModuleNotFound"; type: "error"; }, - { - inputs: [ - { - internalType: "uint256"; - name: "length"; - type: "uint256"; - } - ]; - name: "SchemaLib_InvalidLength"; - type: "error"; - }, { inputs: []; - name: "SchemaLib_StaticTypeAfterDynamicType"; + name: "RootInstallModeNotSupported"; type: "error"; }, { inputs: [ - { - internalType: "bytes"; - name: "data"; - type: "bytes"; - }, - { - internalType: "uint256"; - name: "start"; - type: "uint256"; - }, { internalType: "uint256"; - name: "end"; + name: "length"; type: "uint256"; } ]; - name: "Slice_OutOfBounds"; + name: "SchemaLib_InvalidLength"; type: "error"; }, { inputs: []; - name: "StoreCore_NotDynamicField"; + name: "SchemaLib_StaticTypeAfterDynamicType"; type: "error"; }, { @@ -80,12 +53,25 @@ declare const abi: [ inputs: [ { internalType: "bytes"; - name: "args"; + name: ""; type: "bytes"; } ]; name: "install"; outputs: []; + stateMutability: "pure"; + type: "function"; + }, + { + inputs: [ + { + internalType: "bytes"; + name: "args"; + type: "bytes"; + } + ]; + name: "installRoot"; + outputs: []; stateMutability: "nonpayable"; type: "function"; } diff --git a/packages/world/abi/StandardDelegationsModule.sol/StandardDelegationsModule.abi.json b/packages/world/abi/StandardDelegationsModule.sol/StandardDelegationsModule.abi.json index 646de1964b..1235206c8a 100644 --- a/packages/world/abi/StandardDelegationsModule.sol/StandardDelegationsModule.abi.json +++ b/packages/world/abi/StandardDelegationsModule.sol/StandardDelegationsModule.abi.json @@ -1,4 +1,9 @@ [ + { + "inputs": [], + "name": "NonRootInstallNotSupported", + "type": "error" + }, { "inputs": [ { @@ -10,6 +15,11 @@ "name": "RequiredModuleNotFound", "type": "error" }, + { + "inputs": [], + "name": "RootInstallModeNotSupported", + "type": "error" + }, { "inputs": [ { @@ -26,6 +36,91 @@ "name": "SchemaLib_StaticTypeAfterDynamicType", "type": "error" }, + { + "inputs": [ + { + "internalType": "bytes", + "name": "data", + "type": "bytes" + }, + { + "internalType": "uint256", + "name": "start", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "end", + "type": "uint256" + } + ], + "name": "Slice_OutOfBounds", + "type": "error" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "expected", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "received", + "type": "uint256" + } + ], + "name": "StoreCore_InvalidDataLength", + "type": "error" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "expected", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "received", + "type": "uint256" + } + ], + "name": "StoreCore_InvalidFieldNamesLength", + "type": "error" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "expected", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "received", + "type": "uint256" + } + ], + "name": "StoreCore_InvalidKeyNamesLength", + "type": "error" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "tableId", + "type": "bytes32" + }, + { + "internalType": "string", + "name": "tableIdString", + "type": "string" + } + ], + "name": "StoreCore_TableAlreadyExists", + "type": "error" + }, { "inputs": [], "name": "getName", @@ -49,6 +144,19 @@ ], "name": "install", "outputs": [], + "stateMutability": "pure", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes", + "name": "", + "type": "bytes" + } + ], + "name": "installRoot", + "outputs": [], "stateMutability": "nonpayable", "type": "function" } diff --git a/packages/world/abi/StandardDelegationsModule.sol/StandardDelegationsModule.abi.json.d.ts b/packages/world/abi/StandardDelegationsModule.sol/StandardDelegationsModule.abi.json.d.ts index 3c113d1eed..1b9972cc81 100644 --- a/packages/world/abi/StandardDelegationsModule.sol/StandardDelegationsModule.abi.json.d.ts +++ b/packages/world/abi/StandardDelegationsModule.sol/StandardDelegationsModule.abi.json.d.ts @@ -1,4 +1,9 @@ declare const abi: [ + { + inputs: []; + name: "NonRootInstallNotSupported"; + type: "error"; + }, { inputs: [ { @@ -10,6 +15,11 @@ declare const abi: [ name: "RequiredModuleNotFound"; type: "error"; }, + { + inputs: []; + name: "RootInstallModeNotSupported"; + type: "error"; + }, { inputs: [ { @@ -26,6 +36,91 @@ declare const abi: [ name: "SchemaLib_StaticTypeAfterDynamicType"; type: "error"; }, + { + inputs: [ + { + internalType: "bytes"; + name: "data"; + type: "bytes"; + }, + { + internalType: "uint256"; + name: "start"; + type: "uint256"; + }, + { + internalType: "uint256"; + name: "end"; + type: "uint256"; + } + ]; + name: "Slice_OutOfBounds"; + type: "error"; + }, + { + inputs: [ + { + internalType: "uint256"; + name: "expected"; + type: "uint256"; + }, + { + internalType: "uint256"; + name: "received"; + type: "uint256"; + } + ]; + name: "StoreCore_InvalidDataLength"; + type: "error"; + }, + { + inputs: [ + { + internalType: "uint256"; + name: "expected"; + type: "uint256"; + }, + { + internalType: "uint256"; + name: "received"; + type: "uint256"; + } + ]; + name: "StoreCore_InvalidFieldNamesLength"; + type: "error"; + }, + { + inputs: [ + { + internalType: "uint256"; + name: "expected"; + type: "uint256"; + }, + { + internalType: "uint256"; + name: "received"; + type: "uint256"; + } + ]; + name: "StoreCore_InvalidKeyNamesLength"; + type: "error"; + }, + { + inputs: [ + { + internalType: "bytes32"; + name: "tableId"; + type: "bytes32"; + }, + { + internalType: "string"; + name: "tableIdString"; + type: "string"; + } + ]; + name: "StoreCore_TableAlreadyExists"; + type: "error"; + }, { inputs: []; name: "getName"; @@ -49,6 +144,19 @@ declare const abi: [ ]; name: "install"; outputs: []; + stateMutability: "pure"; + type: "function"; + }, + { + inputs: [ + { + internalType: "bytes"; + name: ""; + type: "bytes"; + } + ]; + name: "installRoot"; + outputs: []; stateMutability: "nonpayable"; type: "function"; } diff --git a/packages/world/abi/UniqueEntityModule.sol/UniqueEntityModule.abi.json b/packages/world/abi/UniqueEntityModule.sol/UniqueEntityModule.abi.json index 646de1964b..90b64315e3 100644 --- a/packages/world/abi/UniqueEntityModule.sol/UniqueEntityModule.abi.json +++ b/packages/world/abi/UniqueEntityModule.sol/UniqueEntityModule.abi.json @@ -1,4 +1,9 @@ [ + { + "inputs": [], + "name": "NonRootInstallNotSupported", + "type": "error" + }, { "inputs": [ { @@ -10,6 +15,11 @@ "name": "RequiredModuleNotFound", "type": "error" }, + { + "inputs": [], + "name": "RootInstallModeNotSupported", + "type": "error" + }, { "inputs": [ { @@ -51,5 +61,18 @@ "outputs": [], "stateMutability": "nonpayable", "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes", + "name": "args", + "type": "bytes" + } + ], + "name": "installRoot", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" } ] \ No newline at end of file diff --git a/packages/world/abi/UniqueEntityModule.sol/UniqueEntityModule.abi.json.d.ts b/packages/world/abi/UniqueEntityModule.sol/UniqueEntityModule.abi.json.d.ts index 3c113d1eed..7965ef95be 100644 --- a/packages/world/abi/UniqueEntityModule.sol/UniqueEntityModule.abi.json.d.ts +++ b/packages/world/abi/UniqueEntityModule.sol/UniqueEntityModule.abi.json.d.ts @@ -1,4 +1,9 @@ declare const abi: [ + { + inputs: []; + name: "NonRootInstallNotSupported"; + type: "error"; + }, { inputs: [ { @@ -10,6 +15,11 @@ declare const abi: [ name: "RequiredModuleNotFound"; type: "error"; }, + { + inputs: []; + name: "RootInstallModeNotSupported"; + type: "error"; + }, { inputs: [ { @@ -51,6 +61,19 @@ declare const abi: [ outputs: []; stateMutability: "nonpayable"; type: "function"; + }, + { + inputs: [ + { + internalType: "bytes"; + name: "args"; + type: "bytes"; + } + ]; + name: "installRoot"; + outputs: []; + stateMutability: "nonpayable"; + type: "function"; } ]; export default abi; diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json index 0029f237db..ef589540e4 100644 --- a/packages/world/gas-report.json +++ b/packages/world/gas-report.json @@ -39,67 +39,67 @@ "file": "test/KeysInTableModule.t.sol", "test": "testInstallComposite", "name": "install keys in table module", - "gasUsed": 1413349 + "gasUsed": 1536430 }, { "file": "test/KeysInTableModule.t.sol", "test": "testInstallGas", "name": "install keys in table module", - "gasUsed": 1413349 + "gasUsed": 1536430 }, { "file": "test/KeysInTableModule.t.sol", "test": "testInstallGas", "name": "set a record on a table with keysInTableModule installed", - "gasUsed": 182589 + "gasUsed": 182582 }, { "file": "test/KeysInTableModule.t.sol", "test": "testInstallSingleton", "name": "install keys in table module", - "gasUsed": 1413349 + "gasUsed": 1536430 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookCompositeGas", "name": "install keys in table module", - "gasUsed": 1413349 + "gasUsed": 1536430 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookCompositeGas", "name": "change a composite record on a table with keysInTableModule installed", - "gasUsed": 26174 + "gasUsed": 26181 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookCompositeGas", "name": "delete a composite record on a table with keysInTableModule installed", - "gasUsed": 251091 + "gasUsed": 251130 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookGas", "name": "install keys in table module", - "gasUsed": 1413349 + "gasUsed": 1536430 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookGas", "name": "change a record on a table with keysInTableModule installed", - "gasUsed": 24894 + "gasUsed": 24901 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookGas", "name": "delete a record on a table with keysInTableModule installed", - "gasUsed": 129380 + "gasUsed": 129413 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testGetKeysWithValueGas", "name": "install keys with value module", - "gasUsed": 651540 + "gasUsed": 677067 }, { "file": "test/KeysWithValueModule.t.sol", @@ -117,7 +117,7 @@ "file": "test/KeysWithValueModule.t.sol", "test": "testInstall", "name": "install keys with value module", - "gasUsed": 651540 + "gasUsed": 677067 }, { "file": "test/KeysWithValueModule.t.sol", @@ -129,7 +129,7 @@ "file": "test/KeysWithValueModule.t.sol", "test": "testSetAndDeleteRecordHook", "name": "install keys with value module", - "gasUsed": 651540 + "gasUsed": 677067 }, { "file": "test/KeysWithValueModule.t.sol", @@ -147,7 +147,7 @@ "file": "test/KeysWithValueModule.t.sol", "test": "testSetField", "name": "install keys with value module", - "gasUsed": 651540 + "gasUsed": 677067 }, { "file": "test/KeysWithValueModule.t.sol", @@ -165,31 +165,31 @@ "file": "test/query.t.sol", "test": "testCombinedHasHasValueNotQuery", "name": "CombinedHasHasValueNotQuery", - "gasUsed": 165699 + "gasUsed": 165462 }, { "file": "test/query.t.sol", "test": "testCombinedHasHasValueQuery", "name": "CombinedHasHasValueQuery", - "gasUsed": 75996 + "gasUsed": 75917 }, { "file": "test/query.t.sol", "test": "testCombinedHasNotQuery", "name": "CombinedHasNotQuery", - "gasUsed": 229855 + "gasUsed": 229431 }, { "file": "test/query.t.sol", "test": "testCombinedHasQuery", "name": "CombinedHasQuery", - "gasUsed": 151588 + "gasUsed": 151359 }, { "file": "test/query.t.sol", "test": "testCombinedHasValueNotQuery", "name": "CombinedHasValueNotQuery", - "gasUsed": 143470 + "gasUsed": 143233 }, { "file": "test/query.t.sol", @@ -201,19 +201,19 @@ "file": "test/query.t.sol", "test": "testHasQuery", "name": "HasQuery", - "gasUsed": 34883 + "gasUsed": 34833 }, { "file": "test/query.t.sol", "test": "testHasQuery1000Keys", "name": "HasQuery with 1000 keys", - "gasUsed": 9272645 + "gasUsed": 9243653 }, { "file": "test/query.t.sol", "test": "testHasQuery100Keys", "name": "HasQuery with 100 keys", - "gasUsed": 861378 + "gasUsed": 858486 }, { "file": "test/query.t.sol", @@ -225,7 +225,7 @@ "file": "test/query.t.sol", "test": "testNotValueQuery", "name": "NotValueQuery", - "gasUsed": 69590 + "gasUsed": 69511 }, { "file": "test/StandardDelegationsModule.t.sol", @@ -255,7 +255,7 @@ "file": "test/UniqueEntityModule.t.sol", "test": "testInstall", "name": "install unique entity module", - "gasUsed": 727309 + "gasUsed": 727195 }, { "file": "test/UniqueEntityModule.t.sol", @@ -267,7 +267,7 @@ "file": "test/UniqueEntityModule.t.sol", "test": "testInstallRoot", "name": "installRoot unique entity module", - "gasUsed": 706087 + "gasUsed": 712496 }, { "file": "test/UniqueEntityModule.t.sol", @@ -309,19 +309,19 @@ "file": "test/World.t.sol", "test": "testRegisterFallbackSystem", "name": "Register a fallback system", - "gasUsed": 70620 + "gasUsed": 70563 }, { "file": "test/World.t.sol", "test": "testRegisterFallbackSystem", "name": "Register a root fallback system", - "gasUsed": 63860 + "gasUsed": 63803 }, { "file": "test/World.t.sol", "test": "testRegisterFunctionSelector", "name": "Register a function selector", - "gasUsed": 91214 + "gasUsed": 91157 }, { "file": "test/World.t.sol", @@ -333,7 +333,7 @@ "file": "test/World.t.sol", "test": "testRegisterRootFunctionSelector", "name": "Register a root function selector", - "gasUsed": 79771 + "gasUsed": 85721 }, { "file": "test/World.t.sol", From 6820bf84061de56e95553daf29272da00e51dcaa Mon Sep 17 00:00:00 2001 From: alvrs Date: Tue, 12 Sep 2023 18:36:55 +0100 Subject: [PATCH 3/4] add changeset --- .changeset/mighty-eels-type.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 .changeset/mighty-eels-type.md diff --git a/.changeset/mighty-eels-type.md b/.changeset/mighty-eels-type.md new file mode 100644 index 0000000000..dd807d4bae --- /dev/null +++ b/.changeset/mighty-eels-type.md @@ -0,0 +1,16 @@ +--- +"@latticexyz/world": major +--- + +- The access control library no longer allows calls by the `World` contract to itself to bypass the ownership check. + This is a breaking change for root modules that relied on this mechanism to register root tables, systems or function selectors. + To upgrade, root modules must use `delegatecall` instead of a regular `call` to install root tables, systems or function selectors. + + ```diff + - world.registerSystem(rootSystemId, rootSystemAddress); + + address(world).delegatecall(abi.encodeCall(world.registerSystem, (rootSystemId, rootSystemAddress))); + ``` + +- An `installRoot` method was added to the `IModule` interface. + This method is now called when installing a root module via `world.installRootModule`. + When installing non-root modules via `world.installModule`, the module's `install` function continues to be called. From 51b7b05b7a3cc5f36ec9be9f6e758951c4582002 Mon Sep 17 00:00:00 2001 From: alvrs Date: Tue, 12 Sep 2023 21:50:47 +0100 Subject: [PATCH 4/4] revert change of namespacing KeysInTable module tables --- packages/world/gas-report.json | 38 +-- packages/world/mud.config.ts | 2 - packages/world/src/Tables.sol | 4 +- .../modules/keysintable/KeysInTableHook.sol | 57 ++-- .../modules/keysintable/KeysInTableModule.sol | 45 +++- .../src/modules/keysintable/constants.sol | 4 - .../modules/keysintable/getKeysInTable.sol | 25 +- .../world/src/modules/keysintable/hasKey.sol | 5 +- .../keysintable/tables/KeysInTable.sol | 244 ++++++------------ .../keysintable/tables/UsedKeysIndex.sol | 57 ++-- 10 files changed, 203 insertions(+), 278 deletions(-) diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json index ef589540e4..980f15a975 100644 --- a/packages/world/gas-report.json +++ b/packages/world/gas-report.json @@ -39,61 +39,61 @@ "file": "test/KeysInTableModule.t.sol", "test": "testInstallComposite", "name": "install keys in table module", - "gasUsed": 1536430 + "gasUsed": 1433865 }, { "file": "test/KeysInTableModule.t.sol", "test": "testInstallGas", "name": "install keys in table module", - "gasUsed": 1536430 + "gasUsed": 1433865 }, { "file": "test/KeysInTableModule.t.sol", "test": "testInstallGas", "name": "set a record on a table with keysInTableModule installed", - "gasUsed": 182582 + "gasUsed": 182589 }, { "file": "test/KeysInTableModule.t.sol", "test": "testInstallSingleton", "name": "install keys in table module", - "gasUsed": 1536430 + "gasUsed": 1433865 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookCompositeGas", "name": "install keys in table module", - "gasUsed": 1536430 + "gasUsed": 1433865 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookCompositeGas", "name": "change a composite record on a table with keysInTableModule installed", - "gasUsed": 26181 + "gasUsed": 26174 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookCompositeGas", "name": "delete a composite record on a table with keysInTableModule installed", - "gasUsed": 251130 + "gasUsed": 251091 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookGas", "name": "install keys in table module", - "gasUsed": 1536430 + "gasUsed": 1433865 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookGas", "name": "change a record on a table with keysInTableModule installed", - "gasUsed": 24901 + "gasUsed": 24894 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookGas", "name": "delete a record on a table with keysInTableModule installed", - "gasUsed": 129413 + "gasUsed": 129380 }, { "file": "test/KeysWithValueModule.t.sol", @@ -165,31 +165,31 @@ "file": "test/query.t.sol", "test": "testCombinedHasHasValueNotQuery", "name": "CombinedHasHasValueNotQuery", - "gasUsed": 165462 + "gasUsed": 165699 }, { "file": "test/query.t.sol", "test": "testCombinedHasHasValueQuery", "name": "CombinedHasHasValueQuery", - "gasUsed": 75917 + "gasUsed": 75996 }, { "file": "test/query.t.sol", "test": "testCombinedHasNotQuery", "name": "CombinedHasNotQuery", - "gasUsed": 229431 + "gasUsed": 229855 }, { "file": "test/query.t.sol", "test": "testCombinedHasQuery", "name": "CombinedHasQuery", - "gasUsed": 151359 + "gasUsed": 151588 }, { "file": "test/query.t.sol", "test": "testCombinedHasValueNotQuery", "name": "CombinedHasValueNotQuery", - "gasUsed": 143233 + "gasUsed": 143470 }, { "file": "test/query.t.sol", @@ -201,19 +201,19 @@ "file": "test/query.t.sol", "test": "testHasQuery", "name": "HasQuery", - "gasUsed": 34833 + "gasUsed": 34883 }, { "file": "test/query.t.sol", "test": "testHasQuery1000Keys", "name": "HasQuery with 1000 keys", - "gasUsed": 9243653 + "gasUsed": 9272645 }, { "file": "test/query.t.sol", "test": "testHasQuery100Keys", "name": "HasQuery with 100 keys", - "gasUsed": 858486 + "gasUsed": 861378 }, { "file": "test/query.t.sol", @@ -225,7 +225,7 @@ "file": "test/query.t.sol", "test": "testNotValueQuery", "name": "NotValueQuery", - "gasUsed": 69511 + "gasUsed": 69590 }, { "file": "test/StandardDelegationsModule.t.sol", diff --git a/packages/world/mud.config.ts b/packages/world/mud.config.ts index 208e6df813..b23b1e0919 100644 --- a/packages/world/mud.config.ts +++ b/packages/world/mud.config.ts @@ -132,7 +132,6 @@ export default mudConfig({ keys3: "bytes32[]", keys4: "bytes32[]", }, - tableIdArgument: true, }, UsedKeysIndex: { directory: "modules/keysintable/tables", @@ -142,7 +141,6 @@ export default mudConfig({ }, schema: { has: "bool", index: "uint40" }, dataStruct: false, - tableIdArgument: true, }, UniqueEntity: { directory: "modules/uniqueentity/tables", diff --git a/packages/world/src/Tables.sol b/packages/world/src/Tables.sol index 3185ae02bc..27d8009070 100644 --- a/packages/world/src/Tables.sol +++ b/packages/world/src/Tables.sol @@ -14,8 +14,8 @@ import { SystemHooks, SystemHooksTableId } from "./modules/core/tables/SystemHoo import { ResourceType, ResourceTypeTableId } from "./modules/core/tables/ResourceType.sol"; import { FunctionSelectors, FunctionSelectorsTableId } from "./modules/core/tables/FunctionSelectors.sol"; import { KeysWithValue } from "./modules/keyswithvalue/tables/KeysWithValue.sol"; -import { KeysInTable, KeysInTableData } from "./modules/keysintable/tables/KeysInTable.sol"; -import { UsedKeysIndex } from "./modules/keysintable/tables/UsedKeysIndex.sol"; +import { KeysInTable, KeysInTableData, KeysInTableTableId } from "./modules/keysintable/tables/KeysInTable.sol"; +import { UsedKeysIndex, UsedKeysIndexTableId } from "./modules/keysintable/tables/UsedKeysIndex.sol"; import { UniqueEntity } from "./modules/uniqueentity/tables/UniqueEntity.sol"; import { CallboundDelegations, CallboundDelegationsTableId } from "./modules/std-delegations/tables/CallboundDelegations.sol"; import { TimeboundDelegations, TimeboundDelegationsTableId } from "./modules/std-delegations/tables/TimeboundDelegations.sol"; diff --git a/packages/world/src/modules/keysintable/KeysInTableHook.sol b/packages/world/src/modules/keysintable/KeysInTableHook.sol index 3d770693f1..7d55c25b75 100644 --- a/packages/world/src/modules/keysintable/KeysInTableHook.sol +++ b/packages/world/src/modules/keysintable/KeysInTableHook.sol @@ -6,7 +6,6 @@ import { Schema } from "@latticexyz/store/src/Schema.sol"; import { KeysInTable } from "./tables/KeysInTable.sol"; import { UsedKeysIndex } from "./tables/UsedKeysIndex.sol"; -import { KeysInTableTableId, UsedKeysIndexTableId } from "./constants.sol"; /** * Note: if a table with composite keys is used, only the first key is indexed @@ -16,20 +15,20 @@ contract KeysInTableHook is IStoreHook { bytes32 keysHash = keccak256(abi.encode(key)); // If the key has not yet been set in the table... - if (!UsedKeysIndex.getHas(UsedKeysIndexTableId, tableId, keysHash)) { - uint40 length = uint40(KeysInTable.lengthKeys0(KeysInTableTableId, tableId)); + if (!UsedKeysIndex.getHas(tableId, keysHash)) { + uint40 length = uint40(KeysInTable.lengthKeys0(tableId)); // Push the key to the list of keys in this table if (key.length > 0) { - KeysInTable.pushKeys0(KeysInTableTableId, tableId, key[0]); + KeysInTable.pushKeys0(tableId, key[0]); if (key.length > 1) { - KeysInTable.pushKeys1(KeysInTableTableId, tableId, key[1]); + KeysInTable.pushKeys1(tableId, key[1]); if (key.length > 2) { - KeysInTable.pushKeys2(KeysInTableTableId, tableId, key[2]); + KeysInTable.pushKeys2(tableId, key[2]); if (key.length > 3) { - KeysInTable.pushKeys3(KeysInTableTableId, tableId, key[3]); + KeysInTable.pushKeys3(tableId, key[3]); if (key.length > 4) { - KeysInTable.pushKeys4(KeysInTableTableId, tableId, key[4]); + KeysInTable.pushKeys4(tableId, key[4]); } } } @@ -37,7 +36,7 @@ contract KeysInTableHook is IStoreHook { } // Update the index to avoid duplicating this key in the array - UsedKeysIndex.set(UsedKeysIndexTableId, tableId, keysHash, true, length); + UsedKeysIndex.set(tableId, keysHash, true, length); } } @@ -59,60 +58,60 @@ contract KeysInTableHook is IStoreHook { function onBeforeDeleteRecord(bytes32 tableId, bytes32[] memory key, Schema) public { bytes32 keysHash = keccak256(abi.encode(key)); - (bool has, uint40 index) = UsedKeysIndex.get(UsedKeysIndexTableId, tableId, keysHash); + (bool has, uint40 index) = UsedKeysIndex.get(tableId, keysHash); // If the key was part of the table... if (has) { // Delete the index as the key is not in the table - UsedKeysIndex.deleteRecord(UsedKeysIndexTableId, tableId, keysHash); + UsedKeysIndex.deleteRecord(tableId, keysHash); - uint40 length = uint40(KeysInTable.lengthKeys0(KeysInTableTableId, tableId)); + uint40 length = uint40(KeysInTable.lengthKeys0(tableId)); if (length == 1) { // Delete the list of keys in this table - KeysInTable.deleteRecord(KeysInTableTableId, tableId); + KeysInTable.deleteRecord(tableId); } else { if (key.length > 0) { bytes32[] memory lastKeyTuple = new bytes32[](key.length); - bytes32 lastKey = KeysInTable.getItemKeys0(KeysInTableTableId, tableId, length - 1); + bytes32 lastKey = KeysInTable.getItemKeys0(tableId, length - 1); lastKeyTuple[0] = lastKey; // Remove the key from the list of keys in this table - KeysInTable.updateKeys0(KeysInTableTableId, tableId, index, lastKey); - KeysInTable.popKeys0(KeysInTableTableId, tableId); + KeysInTable.updateKeys0(tableId, index, lastKey); + KeysInTable.popKeys0(tableId); if (key.length > 1) { - lastKey = KeysInTable.getItemKeys1(KeysInTableTableId, tableId, length - 1); + lastKey = KeysInTable.getItemKeys1(tableId, length - 1); lastKeyTuple[1] = lastKey; // Remove the key from the list of keys in this table - KeysInTable.updateKeys1(KeysInTableTableId, tableId, index, lastKey); - KeysInTable.popKeys1(KeysInTableTableId, tableId); + KeysInTable.updateKeys1(tableId, index, lastKey); + KeysInTable.popKeys1(tableId); if (key.length > 2) { - lastKey = KeysInTable.getItemKeys2(KeysInTableTableId, tableId, length - 1); + lastKey = KeysInTable.getItemKeys2(tableId, length - 1); lastKeyTuple[2] = lastKey; // Swap and pop the key from the list of keys in this table - KeysInTable.updateKeys2(KeysInTableTableId, tableId, index, lastKey); - KeysInTable.popKeys2(KeysInTableTableId, tableId); + KeysInTable.updateKeys2(tableId, index, lastKey); + KeysInTable.popKeys2(tableId); if (key.length > 3) { - lastKey = KeysInTable.getItemKeys3(KeysInTableTableId, tableId, length - 1); + lastKey = KeysInTable.getItemKeys3(tableId, length - 1); lastKeyTuple[3] = lastKey; // Remove the key from the list of keys in this table - KeysInTable.updateKeys3(KeysInTableTableId, tableId, index, lastKey); - KeysInTable.popKeys3(KeysInTableTableId, tableId); + KeysInTable.updateKeys3(tableId, index, lastKey); + KeysInTable.popKeys3(tableId); if (key.length > 4) { - lastKey = KeysInTable.getItemKeys4(KeysInTableTableId, tableId, length - 1); + lastKey = KeysInTable.getItemKeys4(tableId, length - 1); lastKeyTuple[4] = lastKey; // Remove the key from the list of keys in this table - KeysInTable.updateKeys4(KeysInTableTableId, tableId, index, lastKey); - KeysInTable.popKeys4(KeysInTableTableId, tableId); + KeysInTable.updateKeys4(tableId, index, lastKey); + KeysInTable.popKeys4(tableId); } } } @@ -120,7 +119,7 @@ contract KeysInTableHook is IStoreHook { // Update the index of lastKey after swapping it with the deleted key bytes32 lastKeyHash = keccak256(abi.encode(lastKeyTuple)); - UsedKeysIndex.setIndex(UsedKeysIndexTableId, 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 8ea6712b7f..b01cf1ea49 100644 --- a/packages/world/src/modules/keysintable/KeysInTableModule.sol +++ b/packages/world/src/modules/keysintable/KeysInTableModule.sol @@ -14,9 +14,8 @@ import { ResourceSelector } from "../../ResourceSelector.sol"; import { revertWithBytes } from "../../revertWithBytes.sol"; import { KeysInTableHook } from "./KeysInTableHook.sol"; -import { KeysInTable } from "./tables/KeysInTable.sol"; -import { UsedKeysIndex } from "./tables/UsedKeysIndex.sol"; -import { KeysInTableTableId, UsedKeysIndexTableId } from "./constants.sol"; +import { KeysInTable, KeysInTableTableId } from "./tables/KeysInTable.sol"; +import { UsedKeysIndex, UsedKeysIndexTableId } from "./tables/UsedKeysIndex.sol"; /** * This module deploys a hook that is called when a value is set in the `sourceTableId` @@ -51,12 +50,44 @@ contract KeysInTableModule is IModule, WorldContextConsumer { if (ResourceType.get(KeysInTableTableId) == Resource.NONE) { // Register the tables - KeysInTable.register(world, KeysInTableTableId); - UsedKeysIndex.register(world, UsedKeysIndexTableId); + (success, returnData) = address(world).delegatecall( + abi.encodeCall( + world.registerTable, + ( + KeysInTableTableId, + KeysInTable.getKeySchema(), + KeysInTable.getValueSchema(), + KeysInTable.getKeyNames(), + KeysInTable.getFieldNames() + ) + ) + ); + if (!success) revertWithBytes(returnData); + + (success, returnData) = address(world).delegatecall( + abi.encodeCall( + world.registerTable, + ( + UsedKeysIndexTableId, + UsedKeysIndex.getKeySchema(), + UsedKeysIndex.getValueSchema(), + UsedKeysIndex.getKeyNames(), + UsedKeysIndex.getFieldNames() + ) + ) + ); + if (!success) revertWithBytes(returnData); // Grant the hook access to the tables - world.grantAccess(KeysInTableTableId, address(hook)); - world.grantAccess(UsedKeysIndexTableId, address(hook)); + (success, returnData) = address(world).delegatecall( + abi.encodeCall(world.grantAccess, (KeysInTableTableId, address(hook))) + ); + if (!success) revertWithBytes(returnData); + + (success, returnData) = address(world).delegatecall( + abi.encodeCall(world.grantAccess, (UsedKeysIndexTableId, address(hook))) + ); + if (!success) revertWithBytes(returnData); } // Register a hook that is called when a value is set in the source table diff --git a/packages/world/src/modules/keysintable/constants.sol b/packages/world/src/modules/keysintable/constants.sol index f1cbc12f40..e39fa1032a 100644 --- a/packages/world/src/modules/keysintable/constants.sol +++ b/packages/world/src/modules/keysintable/constants.sol @@ -5,7 +5,3 @@ pragma solidity >=0.8.0; // can be used for an identifier of the source table namespace to avoid // collisions between tables with the same name in different namespaces bytes8 constant MODULE_NAMESPACE = "keystab"; - -// TODO: once the config supports multiple namespaces, we don't have to manually construct the table id here -bytes32 constant KeysInTableTableId = bytes32(abi.encodePacked(bytes16(MODULE_NAMESPACE), bytes16("KeysInTable"))); -bytes32 constant UsedKeysIndexTableId = bytes32(abi.encodePacked(bytes16(MODULE_NAMESPACE), bytes16("UsedKeysIndex"))); diff --git a/packages/world/src/modules/keysintable/getKeysInTable.sol b/packages/world/src/modules/keysintable/getKeysInTable.sol index beb58c2c27..9cdfb1abe0 100644 --- a/packages/world/src/modules/keysintable/getKeysInTable.sol +++ b/packages/world/src/modules/keysintable/getKeysInTable.sol @@ -6,7 +6,6 @@ import { Schema } from "@latticexyz/store/src/Schema.sol"; import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol"; import { KeysInTable } from "./tables/KeysInTable.sol"; -import { KeysInTableTableId } from "./constants.sol"; /** * Get a list of keys in the given table. @@ -22,22 +21,22 @@ function getKeysInTable(bytes32 tableId) view returns (bytes32[][] memory keyTup Schema schema = StoreSwitch.getKeySchema(tableId); uint256 numFields = schema.numFields(); - uint256 length = KeysInTable.lengthKeys0(KeysInTableTableId, 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 schema if (numFields > 0) { - keyTuples[i][0] = KeysInTable.getItemKeys0(KeysInTableTableId, tableId, i); + keyTuples[i][0] = KeysInTable.getItemKeys0(tableId, i); if (numFields > 1) { - keyTuples[i][1] = KeysInTable.getItemKeys1(KeysInTableTableId, tableId, i); + keyTuples[i][1] = KeysInTable.getItemKeys1(tableId, i); if (numFields > 2) { - keyTuples[i][2] = KeysInTable.getItemKeys2(KeysInTableTableId, tableId, i); + keyTuples[i][2] = KeysInTable.getItemKeys2(tableId, i); if (numFields > 3) { - keyTuples[i][3] = KeysInTable.getItemKeys3(KeysInTableTableId, tableId, i); + keyTuples[i][3] = KeysInTable.getItemKeys3(tableId, i); if (numFields > 4) { - keyTuples[i][4] = KeysInTable.getItemKeys4(KeysInTableTableId, tableId, i); + keyTuples[i][4] = KeysInTable.getItemKeys4(tableId, i); } } } @@ -57,22 +56,22 @@ function getKeysInTable(IStore store, bytes32 tableId) view returns (bytes32[][] Schema schema = store.getKeySchema(tableId); uint256 numFields = schema.numFields(); - uint256 length = KeysInTable.lengthKeys0(store, KeysInTableTableId, 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 schema if (numFields > 0) { - keyTuples[i][0] = KeysInTable.getItemKeys0(store, KeysInTableTableId, tableId, i); + keyTuples[i][0] = KeysInTable.getItemKeys0(store, tableId, i); if (numFields > 1) { - keyTuples[i][1] = KeysInTable.getItemKeys1(store, KeysInTableTableId, tableId, i); + keyTuples[i][1] = KeysInTable.getItemKeys1(store, tableId, i); if (numFields > 2) { - keyTuples[i][2] = KeysInTable.getItemKeys2(store, KeysInTableTableId, tableId, i); + keyTuples[i][2] = KeysInTable.getItemKeys2(store, tableId, i); if (numFields > 3) { - keyTuples[i][3] = KeysInTable.getItemKeys3(store, KeysInTableTableId, tableId, i); + keyTuples[i][3] = KeysInTable.getItemKeys3(store, tableId, i); if (numFields > 4) { - keyTuples[i][4] = KeysInTable.getItemKeys4(store, KeysInTableTableId, 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 c716bd0bd6..d085a63e6c 100644 --- a/packages/world/src/modules/keysintable/hasKey.sol +++ b/packages/world/src/modules/keysintable/hasKey.sol @@ -4,7 +4,6 @@ pragma solidity >=0.8.0; import { IStore } from "@latticexyz/store/src/IStore.sol"; import { UsedKeysIndex } from "./tables/UsedKeysIndex.sol"; -import { UsedKeysIndexTableId } from "./constants.sol"; /** * Get whether the key is in the given table. @@ -15,7 +14,7 @@ import { UsedKeysIndexTableId } from "./constants.sol"; function hasKey(bytes32 tableId, bytes32[] memory key) view returns (bool) { bytes32 keysHash = keccak256(abi.encode(key)); - return UsedKeysIndex.getHas(UsedKeysIndexTableId, tableId, keysHash); + return UsedKeysIndex.getHas(tableId, keysHash); } /** @@ -24,5 +23,5 @@ function hasKey(bytes32 tableId, bytes32[] memory key) view returns (bool) { function hasKey(IStore store, bytes32 tableId, bytes32[] memory key) view returns (bool) { bytes32 keysHash = keccak256(abi.encode(key)); - return UsedKeysIndex.getHas(store, UsedKeysIndexTableId, 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 f919d8e848..c0a6832e7e 100644 --- a/packages/world/src/modules/keysintable/tables/KeysInTable.sol +++ b/packages/world/src/modules/keysintable/tables/KeysInTable.sol @@ -17,6 +17,9 @@ import { EncodeArray } from "@latticexyz/store/src/tightcoder/EncodeArray.sol"; import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCounter.sol"; +bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("KeysInTable"))); +bytes32 constant KeysInTableTableId = _tableId; + struct KeysInTableData { bytes32[] keys0; bytes32[] keys1; @@ -63,17 +66,17 @@ library KeysInTable { } /** Register the table's key schema, value schema, key names and value names */ - function register(bytes32 _tableId) internal { + function register() internal { StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table's key schema, value schema, key names and value names (using the specified store) */ - function register(IStore _store, bytes32 _tableId) internal { + function register(IStore _store) internal { _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get keys0 */ - function getKeys0(bytes32 _tableId, bytes32 sourceTable) internal view returns (bytes32[] memory keys0) { + function getKeys0(bytes32 sourceTable) internal view returns (bytes32[] memory keys0) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -82,11 +85,7 @@ library KeysInTable { } /** Get keys0 (using the specified store) */ - function getKeys0( - IStore _store, - bytes32 _tableId, - bytes32 sourceTable - ) internal view returns (bytes32[] memory keys0) { + function getKeys0(IStore _store, bytes32 sourceTable) internal view returns (bytes32[] memory keys0) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -95,7 +94,7 @@ library KeysInTable { } /** Set keys0 */ - function setKeys0(bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys0) internal { + function setKeys0(bytes32 sourceTable, bytes32[] memory keys0) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -103,7 +102,7 @@ library KeysInTable { } /** Set keys0 (using the specified store) */ - function setKeys0(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys0) internal { + function setKeys0(IStore _store, bytes32 sourceTable, bytes32[] memory keys0) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -111,7 +110,7 @@ library KeysInTable { } /** Get the length of keys0 */ - function lengthKeys0(bytes32 _tableId, bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys0(bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -122,7 +121,7 @@ library KeysInTable { } /** Get the length of keys0 (using the specified store) */ - function lengthKeys0(IStore _store, bytes32 _tableId, bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys0(IStore _store, bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -136,7 +135,7 @@ library KeysInTable { * Get an item of keys0 * (unchecked, returns invalid data if index overflows) */ - function getItemKeys0(bytes32 _tableId, bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + function getItemKeys0(bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -157,12 +156,7 @@ library KeysInTable { * Get an item of keys0 (using the specified store) * (unchecked, returns invalid data if index overflows) */ - function getItemKeys0( - IStore _store, - bytes32 _tableId, - bytes32 sourceTable, - uint256 _index - ) internal view returns (bytes32) { + function getItemKeys0(IStore _store, bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -180,7 +174,7 @@ library KeysInTable { } /** Push an element to keys0 */ - function pushKeys0(bytes32 _tableId, bytes32 sourceTable, bytes32 _element) internal { + function pushKeys0(bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -188,7 +182,7 @@ library KeysInTable { } /** Push an element to keys0 (using the specified store) */ - function pushKeys0(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32 _element) internal { + function pushKeys0(IStore _store, bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -196,7 +190,7 @@ library KeysInTable { } /** Pop an element from keys0 */ - function popKeys0(bytes32 _tableId, bytes32 sourceTable) internal { + function popKeys0(bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -204,7 +198,7 @@ library KeysInTable { } /** Pop an element from keys0 (using the specified store) */ - function popKeys0(IStore _store, bytes32 _tableId, bytes32 sourceTable) internal { + function popKeys0(IStore _store, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -215,7 +209,7 @@ 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 _tableId, bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + function updateKeys0(bytes32 sourceTable, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -228,13 +222,7 @@ 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 _tableId, - bytes32 sourceTable, - uint256 _index, - bytes32 _element - ) internal { + function updateKeys0(IStore _store, bytes32 sourceTable, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -244,7 +232,7 @@ library KeysInTable { } /** Get keys1 */ - function getKeys1(bytes32 _tableId, bytes32 sourceTable) internal view returns (bytes32[] memory keys1) { + function getKeys1(bytes32 sourceTable) internal view returns (bytes32[] memory keys1) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -253,11 +241,7 @@ library KeysInTable { } /** Get keys1 (using the specified store) */ - function getKeys1( - IStore _store, - bytes32 _tableId, - bytes32 sourceTable - ) internal view returns (bytes32[] memory keys1) { + function getKeys1(IStore _store, bytes32 sourceTable) internal view returns (bytes32[] memory keys1) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -266,7 +250,7 @@ library KeysInTable { } /** Set keys1 */ - function setKeys1(bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys1) internal { + function setKeys1(bytes32 sourceTable, bytes32[] memory keys1) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -274,7 +258,7 @@ library KeysInTable { } /** Set keys1 (using the specified store) */ - function setKeys1(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys1) internal { + function setKeys1(IStore _store, bytes32 sourceTable, bytes32[] memory keys1) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -282,7 +266,7 @@ library KeysInTable { } /** Get the length of keys1 */ - function lengthKeys1(bytes32 _tableId, bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys1(bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -293,7 +277,7 @@ library KeysInTable { } /** Get the length of keys1 (using the specified store) */ - function lengthKeys1(IStore _store, bytes32 _tableId, bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys1(IStore _store, bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -307,7 +291,7 @@ library KeysInTable { * Get an item of keys1 * (unchecked, returns invalid data if index overflows) */ - function getItemKeys1(bytes32 _tableId, bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + function getItemKeys1(bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -328,12 +312,7 @@ library KeysInTable { * Get an item of keys1 (using the specified store) * (unchecked, returns invalid data if index overflows) */ - function getItemKeys1( - IStore _store, - bytes32 _tableId, - bytes32 sourceTable, - uint256 _index - ) internal view returns (bytes32) { + function getItemKeys1(IStore _store, bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -351,7 +330,7 @@ library KeysInTable { } /** Push an element to keys1 */ - function pushKeys1(bytes32 _tableId, bytes32 sourceTable, bytes32 _element) internal { + function pushKeys1(bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -359,7 +338,7 @@ library KeysInTable { } /** Push an element to keys1 (using the specified store) */ - function pushKeys1(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32 _element) internal { + function pushKeys1(IStore _store, bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -367,7 +346,7 @@ library KeysInTable { } /** Pop an element from keys1 */ - function popKeys1(bytes32 _tableId, bytes32 sourceTable) internal { + function popKeys1(bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -375,7 +354,7 @@ library KeysInTable { } /** Pop an element from keys1 (using the specified store) */ - function popKeys1(IStore _store, bytes32 _tableId, bytes32 sourceTable) internal { + function popKeys1(IStore _store, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -386,7 +365,7 @@ 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 _tableId, bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + function updateKeys1(bytes32 sourceTable, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -399,13 +378,7 @@ 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 _tableId, - bytes32 sourceTable, - uint256 _index, - bytes32 _element - ) internal { + function updateKeys1(IStore _store, bytes32 sourceTable, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -415,7 +388,7 @@ library KeysInTable { } /** Get keys2 */ - function getKeys2(bytes32 _tableId, bytes32 sourceTable) internal view returns (bytes32[] memory keys2) { + function getKeys2(bytes32 sourceTable) internal view returns (bytes32[] memory keys2) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -424,11 +397,7 @@ library KeysInTable { } /** Get keys2 (using the specified store) */ - function getKeys2( - IStore _store, - bytes32 _tableId, - bytes32 sourceTable - ) internal view returns (bytes32[] memory keys2) { + function getKeys2(IStore _store, bytes32 sourceTable) internal view returns (bytes32[] memory keys2) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -437,7 +406,7 @@ library KeysInTable { } /** Set keys2 */ - function setKeys2(bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys2) internal { + function setKeys2(bytes32 sourceTable, bytes32[] memory keys2) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -445,7 +414,7 @@ library KeysInTable { } /** Set keys2 (using the specified store) */ - function setKeys2(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys2) internal { + function setKeys2(IStore _store, bytes32 sourceTable, bytes32[] memory keys2) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -453,7 +422,7 @@ library KeysInTable { } /** Get the length of keys2 */ - function lengthKeys2(bytes32 _tableId, bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys2(bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -464,7 +433,7 @@ library KeysInTable { } /** Get the length of keys2 (using the specified store) */ - function lengthKeys2(IStore _store, bytes32 _tableId, bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys2(IStore _store, bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -478,7 +447,7 @@ library KeysInTable { * Get an item of keys2 * (unchecked, returns invalid data if index overflows) */ - function getItemKeys2(bytes32 _tableId, bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + function getItemKeys2(bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -499,12 +468,7 @@ library KeysInTable { * Get an item of keys2 (using the specified store) * (unchecked, returns invalid data if index overflows) */ - function getItemKeys2( - IStore _store, - bytes32 _tableId, - bytes32 sourceTable, - uint256 _index - ) internal view returns (bytes32) { + function getItemKeys2(IStore _store, bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -522,7 +486,7 @@ library KeysInTable { } /** Push an element to keys2 */ - function pushKeys2(bytes32 _tableId, bytes32 sourceTable, bytes32 _element) internal { + function pushKeys2(bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -530,7 +494,7 @@ library KeysInTable { } /** Push an element to keys2 (using the specified store) */ - function pushKeys2(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32 _element) internal { + function pushKeys2(IStore _store, bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -538,7 +502,7 @@ library KeysInTable { } /** Pop an element from keys2 */ - function popKeys2(bytes32 _tableId, bytes32 sourceTable) internal { + function popKeys2(bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -546,7 +510,7 @@ library KeysInTable { } /** Pop an element from keys2 (using the specified store) */ - function popKeys2(IStore _store, bytes32 _tableId, bytes32 sourceTable) internal { + function popKeys2(IStore _store, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -557,7 +521,7 @@ 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 _tableId, bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + function updateKeys2(bytes32 sourceTable, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -570,13 +534,7 @@ 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 _tableId, - bytes32 sourceTable, - uint256 _index, - bytes32 _element - ) internal { + function updateKeys2(IStore _store, bytes32 sourceTable, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -586,7 +544,7 @@ library KeysInTable { } /** Get keys3 */ - function getKeys3(bytes32 _tableId, bytes32 sourceTable) internal view returns (bytes32[] memory keys3) { + function getKeys3(bytes32 sourceTable) internal view returns (bytes32[] memory keys3) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -595,11 +553,7 @@ library KeysInTable { } /** Get keys3 (using the specified store) */ - function getKeys3( - IStore _store, - bytes32 _tableId, - bytes32 sourceTable - ) internal view returns (bytes32[] memory keys3) { + function getKeys3(IStore _store, bytes32 sourceTable) internal view returns (bytes32[] memory keys3) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -608,7 +562,7 @@ library KeysInTable { } /** Set keys3 */ - function setKeys3(bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys3) internal { + function setKeys3(bytes32 sourceTable, bytes32[] memory keys3) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -616,7 +570,7 @@ library KeysInTable { } /** Set keys3 (using the specified store) */ - function setKeys3(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys3) internal { + function setKeys3(IStore _store, bytes32 sourceTable, bytes32[] memory keys3) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -624,7 +578,7 @@ library KeysInTable { } /** Get the length of keys3 */ - function lengthKeys3(bytes32 _tableId, bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys3(bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -635,7 +589,7 @@ library KeysInTable { } /** Get the length of keys3 (using the specified store) */ - function lengthKeys3(IStore _store, bytes32 _tableId, bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys3(IStore _store, bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -649,7 +603,7 @@ library KeysInTable { * Get an item of keys3 * (unchecked, returns invalid data if index overflows) */ - function getItemKeys3(bytes32 _tableId, bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + function getItemKeys3(bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -670,12 +624,7 @@ library KeysInTable { * Get an item of keys3 (using the specified store) * (unchecked, returns invalid data if index overflows) */ - function getItemKeys3( - IStore _store, - bytes32 _tableId, - bytes32 sourceTable, - uint256 _index - ) internal view returns (bytes32) { + function getItemKeys3(IStore _store, bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -693,7 +642,7 @@ library KeysInTable { } /** Push an element to keys3 */ - function pushKeys3(bytes32 _tableId, bytes32 sourceTable, bytes32 _element) internal { + function pushKeys3(bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -701,7 +650,7 @@ library KeysInTable { } /** Push an element to keys3 (using the specified store) */ - function pushKeys3(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32 _element) internal { + function pushKeys3(IStore _store, bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -709,7 +658,7 @@ library KeysInTable { } /** Pop an element from keys3 */ - function popKeys3(bytes32 _tableId, bytes32 sourceTable) internal { + function popKeys3(bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -717,7 +666,7 @@ library KeysInTable { } /** Pop an element from keys3 (using the specified store) */ - function popKeys3(IStore _store, bytes32 _tableId, bytes32 sourceTable) internal { + function popKeys3(IStore _store, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -728,7 +677,7 @@ 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 _tableId, bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + function updateKeys3(bytes32 sourceTable, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -741,13 +690,7 @@ 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 _tableId, - bytes32 sourceTable, - uint256 _index, - bytes32 _element - ) internal { + function updateKeys3(IStore _store, bytes32 sourceTable, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -757,7 +700,7 @@ library KeysInTable { } /** Get keys4 */ - function getKeys4(bytes32 _tableId, bytes32 sourceTable) internal view returns (bytes32[] memory keys4) { + function getKeys4(bytes32 sourceTable) internal view returns (bytes32[] memory keys4) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -766,11 +709,7 @@ library KeysInTable { } /** Get keys4 (using the specified store) */ - function getKeys4( - IStore _store, - bytes32 _tableId, - bytes32 sourceTable - ) internal view returns (bytes32[] memory keys4) { + function getKeys4(IStore _store, bytes32 sourceTable) internal view returns (bytes32[] memory keys4) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -779,7 +718,7 @@ library KeysInTable { } /** Set keys4 */ - function setKeys4(bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys4) internal { + function setKeys4(bytes32 sourceTable, bytes32[] memory keys4) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -787,7 +726,7 @@ library KeysInTable { } /** Set keys4 (using the specified store) */ - function setKeys4(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys4) internal { + function setKeys4(IStore _store, bytes32 sourceTable, bytes32[] memory keys4) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -795,7 +734,7 @@ library KeysInTable { } /** Get the length of keys4 */ - function lengthKeys4(bytes32 _tableId, bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys4(bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -806,7 +745,7 @@ library KeysInTable { } /** Get the length of keys4 (using the specified store) */ - function lengthKeys4(IStore _store, bytes32 _tableId, bytes32 sourceTable) internal view returns (uint256) { + function lengthKeys4(IStore _store, bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -820,7 +759,7 @@ library KeysInTable { * Get an item of keys4 * (unchecked, returns invalid data if index overflows) */ - function getItemKeys4(bytes32 _tableId, bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + function getItemKeys4(bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -841,12 +780,7 @@ library KeysInTable { * Get an item of keys4 (using the specified store) * (unchecked, returns invalid data if index overflows) */ - function getItemKeys4( - IStore _store, - bytes32 _tableId, - bytes32 sourceTable, - uint256 _index - ) internal view returns (bytes32) { + function getItemKeys4(IStore _store, bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -864,7 +798,7 @@ library KeysInTable { } /** Push an element to keys4 */ - function pushKeys4(bytes32 _tableId, bytes32 sourceTable, bytes32 _element) internal { + function pushKeys4(bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -872,7 +806,7 @@ library KeysInTable { } /** Push an element to keys4 (using the specified store) */ - function pushKeys4(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32 _element) internal { + function pushKeys4(IStore _store, bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -880,7 +814,7 @@ library KeysInTable { } /** Pop an element from keys4 */ - function popKeys4(bytes32 _tableId, bytes32 sourceTable) internal { + function popKeys4(bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -888,7 +822,7 @@ library KeysInTable { } /** Pop an element from keys4 (using the specified store) */ - function popKeys4(IStore _store, bytes32 _tableId, bytes32 sourceTable) internal { + function popKeys4(IStore _store, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -899,7 +833,7 @@ 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 _tableId, bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + function updateKeys4(bytes32 sourceTable, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -912,13 +846,7 @@ 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 _tableId, - bytes32 sourceTable, - uint256 _index, - bytes32 _element - ) internal { + function updateKeys4(IStore _store, bytes32 sourceTable, uint256 _index, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -928,7 +856,7 @@ library KeysInTable { } /** Get the full data */ - function get(bytes32 _tableId, bytes32 sourceTable) internal view returns (KeysInTableData memory _table) { + function get(bytes32 sourceTable) internal view returns (KeysInTableData memory _table) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -937,11 +865,7 @@ library KeysInTable { } /** Get the full data (using the specified store) */ - function get( - IStore _store, - bytes32 _tableId, - bytes32 sourceTable - ) internal view returns (KeysInTableData memory _table) { + function get(IStore _store, bytes32 sourceTable) internal view returns (KeysInTableData memory _table) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -951,7 +875,6 @@ library KeysInTable { /** Set the full data using individual values */ function set( - bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys0, bytes32[] memory keys1, @@ -970,7 +893,6 @@ library KeysInTable { /** Set the full data using individual values (using the specified store) */ function set( IStore _store, - bytes32 _tableId, bytes32 sourceTable, bytes32[] memory keys0, bytes32[] memory keys1, @@ -987,13 +909,13 @@ library KeysInTable { } /** Set the full data using the data struct */ - function set(bytes32 _tableId, bytes32 sourceTable, KeysInTableData memory _table) internal { - set(_tableId, sourceTable, _table.keys0, _table.keys1, _table.keys2, _table.keys3, _table.keys4); + function set(bytes32 sourceTable, KeysInTableData memory _table) internal { + set(sourceTable, _table.keys0, _table.keys1, _table.keys2, _table.keys3, _table.keys4); } /** Set the full data using the data struct (using the specified store) */ - function set(IStore _store, bytes32 _tableId, bytes32 sourceTable, KeysInTableData memory _table) internal { - set(_store, _tableId, sourceTable, _table.keys0, _table.keys1, _table.keys2, _table.keys3, _table.keys4); + function set(IStore _store, bytes32 sourceTable, KeysInTableData memory _table) internal { + set(_store, sourceTable, _table.keys0, _table.keys1, _table.keys2, _table.keys3, _table.keys4); } /** @@ -1080,7 +1002,7 @@ library KeysInTable { } /* Delete all data for given keys */ - function deleteRecord(bytes32 _tableId, bytes32 sourceTable) internal { + function deleteRecord(bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; @@ -1088,7 +1010,7 @@ library KeysInTable { } /* Delete all data for given keys (using the specified store) */ - function deleteRecord(IStore _store, bytes32 _tableId, bytes32 sourceTable) internal { + function deleteRecord(IStore _store, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; diff --git a/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol b/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol index a18fcc0470..c83423843e 100644 --- a/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol +++ b/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol @@ -17,6 +17,9 @@ import { EncodeArray } from "@latticexyz/store/src/tightcoder/EncodeArray.sol"; import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCounter.sol"; +bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("UsedKeysIndex"))); +bytes32 constant UsedKeysIndexTableId = _tableId; + library UsedKeysIndex { /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { @@ -51,17 +54,17 @@ library UsedKeysIndex { } /** Register the table's key schema, value schema, key names and value names */ - function register(bytes32 _tableId) internal { + function register() internal { StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Register the table's key schema, value schema, key names and value names (using the specified store) */ - function register(IStore _store, bytes32 _tableId) internal { + function register(IStore _store) internal { _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get has */ - function getHas(bytes32 _tableId, bytes32 sourceTable, bytes32 keysHash) internal view returns (bool has) { + function getHas(bytes32 sourceTable, bytes32 keysHash) internal view returns (bool has) { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; @@ -71,12 +74,7 @@ library UsedKeysIndex { } /** Get has (using the specified store) */ - function getHas( - IStore _store, - bytes32 _tableId, - bytes32 sourceTable, - bytes32 keysHash - ) internal view returns (bool has) { + function getHas(IStore _store, bytes32 sourceTable, bytes32 keysHash) internal view returns (bool has) { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; @@ -86,7 +84,7 @@ library UsedKeysIndex { } /** Set has */ - function setHas(bytes32 _tableId, bytes32 sourceTable, bytes32 keysHash, bool has) internal { + function setHas(bytes32 sourceTable, bytes32 keysHash, bool has) internal { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; @@ -95,7 +93,7 @@ library UsedKeysIndex { } /** Set has (using the specified store) */ - function setHas(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32 keysHash, bool has) internal { + function setHas(IStore _store, bytes32 sourceTable, bytes32 keysHash, bool has) internal { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; @@ -104,7 +102,7 @@ library UsedKeysIndex { } /** Get index */ - function getIndex(bytes32 _tableId, bytes32 sourceTable, bytes32 keysHash) internal view returns (uint40 index) { + function getIndex(bytes32 sourceTable, bytes32 keysHash) internal view returns (uint40 index) { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; @@ -114,12 +112,7 @@ library UsedKeysIndex { } /** Get index (using the specified store) */ - function getIndex( - IStore _store, - bytes32 _tableId, - bytes32 sourceTable, - bytes32 keysHash - ) internal view returns (uint40 index) { + function getIndex(IStore _store, bytes32 sourceTable, bytes32 keysHash) internal view returns (uint40 index) { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; @@ -129,7 +122,7 @@ library UsedKeysIndex { } /** Set index */ - function setIndex(bytes32 _tableId, bytes32 sourceTable, bytes32 keysHash, uint40 index) internal { + function setIndex(bytes32 sourceTable, bytes32 keysHash, uint40 index) internal { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; @@ -138,7 +131,7 @@ library UsedKeysIndex { } /** Set index (using the specified store) */ - function setIndex(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32 keysHash, uint40 index) internal { + function setIndex(IStore _store, bytes32 sourceTable, bytes32 keysHash, uint40 index) internal { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; @@ -147,7 +140,7 @@ library UsedKeysIndex { } /** Get the full data */ - function get(bytes32 _tableId, bytes32 sourceTable, bytes32 keysHash) internal view returns (bool has, uint40 index) { + function get(bytes32 sourceTable, bytes32 keysHash) internal view returns (bool has, uint40 index) { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; @@ -157,12 +150,7 @@ library UsedKeysIndex { } /** Get the full data (using the specified store) */ - function get( - IStore _store, - bytes32 _tableId, - bytes32 sourceTable, - bytes32 keysHash - ) internal view returns (bool has, uint40 index) { + function get(IStore _store, bytes32 sourceTable, bytes32 keysHash) internal view returns (bool has, uint40 index) { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; @@ -172,7 +160,7 @@ library UsedKeysIndex { } /** Set the full data using individual values */ - function set(bytes32 _tableId, bytes32 sourceTable, bytes32 keysHash, bool has, uint40 index) internal { + function set(bytes32 sourceTable, bytes32 keysHash, bool has, uint40 index) internal { bytes memory _data = encode(has, index); bytes32[] memory _keyTuple = new bytes32[](2); @@ -183,14 +171,7 @@ library UsedKeysIndex { } /** Set the full data using individual values (using the specified store) */ - function set( - IStore _store, - bytes32 _tableId, - bytes32 sourceTable, - bytes32 keysHash, - bool has, - uint40 index - ) internal { + function set(IStore _store, bytes32 sourceTable, bytes32 keysHash, bool has, uint40 index) internal { bytes memory _data = encode(has, index); bytes32[] memory _keyTuple = new bytes32[](2); @@ -222,7 +203,7 @@ library UsedKeysIndex { } /* Delete all data for given keys */ - function deleteRecord(bytes32 _tableId, bytes32 sourceTable, bytes32 keysHash) internal { + function deleteRecord(bytes32 sourceTable, bytes32 keysHash) internal { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; @@ -231,7 +212,7 @@ library UsedKeysIndex { } /* Delete all data for given keys (using the specified store) */ - function deleteRecord(IStore _store, bytes32 _tableId, bytes32 sourceTable, bytes32 keysHash) internal { + function deleteRecord(IStore _store, bytes32 sourceTable, bytes32 keysHash) internal { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash;