From 4c7fd3eb29e3d3954f2f1f36ace474a436082651 Mon Sep 17 00:00:00 2001 From: alvarius Date: Fri, 15 Sep 2023 18:32:27 +0100 Subject: [PATCH] refactor(world): remove workaround in mud config (#1501) Co-authored-by: Kevin Ingersoll --- .changeset/lovely-buses-boil.md | 5 ++ packages/world/gas-report.json | 26 +++---- packages/world/mud.config.ts | 5 -- packages/world/src/Tables.sol | 2 +- packages/world/src/World.sol | 2 +- .../world/src/tables/InstalledModules.sol | 77 +------------------ packages/world/test/Factories.t.sol | 6 +- 7 files changed, 27 insertions(+), 96 deletions(-) create mode 100644 .changeset/lovely-buses-boil.md diff --git a/.changeset/lovely-buses-boil.md b/.changeset/lovely-buses-boil.md new file mode 100644 index 0000000000..497c52749c --- /dev/null +++ b/.changeset/lovely-buses-boil.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/world": patch +--- + +Remove a workaround for the internal `InstalledModules` table that is not needed anymore. diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json index 4157bcf039..30962ed0d7 100644 --- a/packages/world/gas-report.json +++ b/packages/world/gas-report.json @@ -39,13 +39,13 @@ "file": "test/KeysInTableModule.t.sol", "test": "testInstallComposite", "name": "install keys in table module", - "gasUsed": 1519669 + "gasUsed": 1519335 }, { "file": "test/KeysInTableModule.t.sol", "test": "testInstallGas", "name": "install keys in table module", - "gasUsed": 1519669 + "gasUsed": 1519335 }, { "file": "test/KeysInTableModule.t.sol", @@ -57,13 +57,13 @@ "file": "test/KeysInTableModule.t.sol", "test": "testInstallSingleton", "name": "install keys in table module", - "gasUsed": 1519669 + "gasUsed": 1519335 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookCompositeGas", "name": "install keys in table module", - "gasUsed": 1519669 + "gasUsed": 1519335 }, { "file": "test/KeysInTableModule.t.sol", @@ -81,7 +81,7 @@ "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookGas", "name": "install keys in table module", - "gasUsed": 1519669 + "gasUsed": 1519335 }, { "file": "test/KeysInTableModule.t.sol", @@ -99,7 +99,7 @@ "file": "test/KeysWithValueModule.t.sol", "test": "testGetKeysWithValueGas", "name": "install keys with value module", - "gasUsed": 728673 + "gasUsed": 728346 }, { "file": "test/KeysWithValueModule.t.sol", @@ -117,7 +117,7 @@ "file": "test/KeysWithValueModule.t.sol", "test": "testInstall", "name": "install keys with value module", - "gasUsed": 728673 + "gasUsed": 728346 }, { "file": "test/KeysWithValueModule.t.sol", @@ -129,7 +129,7 @@ "file": "test/KeysWithValueModule.t.sol", "test": "testSetAndDeleteRecordHook", "name": "install keys with value module", - "gasUsed": 728673 + "gasUsed": 728346 }, { "file": "test/KeysWithValueModule.t.sol", @@ -147,7 +147,7 @@ "file": "test/KeysWithValueModule.t.sol", "test": "testSetField", "name": "install keys with value module", - "gasUsed": 728673 + "gasUsed": 728346 }, { "file": "test/KeysWithValueModule.t.sol", @@ -255,7 +255,7 @@ "file": "test/UniqueEntityModule.t.sol", "test": "testInstall", "name": "install unique entity module", - "gasUsed": 787953 + "gasUsed": 787645 }, { "file": "test/UniqueEntityModule.t.sol", @@ -267,7 +267,7 @@ "file": "test/UniqueEntityModule.t.sol", "test": "testInstallRoot", "name": "installRoot unique entity module", - "gasUsed": 771494 + "gasUsed": 771167 }, { "file": "test/UniqueEntityModule.t.sol", @@ -327,7 +327,7 @@ "file": "test/World.t.sol", "test": "testRegisterNamespace", "name": "Register a new namespace", - "gasUsed": 146371 + "gasUsed": 146378 }, { "file": "test/World.t.sol", @@ -339,7 +339,7 @@ "file": "test/World.t.sol", "test": "testRegisterTable", "name": "Register a new table in the namespace", - "gasUsed": 685337 + "gasUsed": 685344 }, { "file": "test/World.t.sol", diff --git a/packages/world/mud.config.ts b/packages/world/mud.config.ts index a043d644dd..97563a1cdf 100644 --- a/packages/world/mud.config.ts +++ b/packages/world/mud.config.ts @@ -36,11 +36,6 @@ export default mudConfig({ valueSchema: { moduleAddress: "address", }, - // TODO: this is a workaround to use `getRecord` instead of `getField` in the autogen library, - // to allow using the table before it is registered. This is because `getRecord` passes the valueSchema - // to store, while `getField` loads it from storage. Remove this once we have support for passing the - // valueSchema in `getField` too. (See https://github.com/latticexyz/mud/issues/444) - dataStruct: true, }, Delegations: { keySchema: { diff --git a/packages/world/src/Tables.sol b/packages/world/src/Tables.sol index 27d8009070..021a7371bf 100644 --- a/packages/world/src/Tables.sol +++ b/packages/world/src/Tables.sol @@ -5,7 +5,7 @@ pragma solidity >=0.8.0; import { NamespaceOwner, NamespaceOwnerTableId } from "./tables/NamespaceOwner.sol"; import { ResourceAccess, ResourceAccessTableId } from "./tables/ResourceAccess.sol"; -import { InstalledModules, InstalledModulesData, InstalledModulesTableId } from "./tables/InstalledModules.sol"; +import { InstalledModules, InstalledModulesTableId } from "./tables/InstalledModules.sol"; import { Delegations, DelegationsTableId } from "./tables/Delegations.sol"; import { Balances, BalancesTableId } from "./modules/core/tables/Balances.sol"; import { Systems, SystemsTableId } from "./modules/core/tables/Systems.sol"; diff --git a/packages/world/src/World.sol b/packages/world/src/World.sol index 3653102813..1c80c16249 100644 --- a/packages/world/src/World.sol +++ b/packages/world/src/World.sol @@ -52,7 +52,7 @@ contract World is StoreRead, IStoreData, IWorldKernel { } // The World can only be initialized once - if (InstalledModules.getModuleAddress(CORE_MODULE_NAME, keccak256("")) != address(0)) { + if (InstalledModules.get(CORE_MODULE_NAME, keccak256("")) != address(0)) { revert WorldAlreadyInitialized(); } diff --git a/packages/world/src/tables/InstalledModules.sol b/packages/world/src/tables/InstalledModules.sol index 9a03d507fa..a0bbbc61b7 100644 --- a/packages/world/src/tables/InstalledModules.sol +++ b/packages/world/src/tables/InstalledModules.sol @@ -21,10 +21,6 @@ import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCou bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("InstalledModules"))); bytes32 constant InstalledModulesTableId = _tableId; -struct InstalledModulesData { - address moduleAddress; -} - library InstalledModules { /** Get the table values' field layout */ function getFieldLayout() internal pure returns (FieldLayout) { @@ -82,7 +78,7 @@ library InstalledModules { } /** Get moduleAddress */ - function getModuleAddress(bytes16 moduleName, bytes32 argumentsHash) internal view returns (address moduleAddress) { + function get(bytes16 moduleName, bytes32 argumentsHash) internal view returns (address moduleAddress) { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; @@ -92,11 +88,7 @@ library InstalledModules { } /** Get moduleAddress (using the specified store) */ - function getModuleAddress( - IStore _store, - bytes16 moduleName, - bytes32 argumentsHash - ) internal view returns (address moduleAddress) { + function get(IStore _store, bytes16 moduleName, bytes32 argumentsHash) internal view returns (address moduleAddress) { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; @@ -106,7 +98,7 @@ library InstalledModules { } /** Set moduleAddress */ - function setModuleAddress(bytes16 moduleName, bytes32 argumentsHash, address moduleAddress) internal { + function set(bytes16 moduleName, bytes32 argumentsHash, address moduleAddress) internal { bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; @@ -115,73 +107,12 @@ library InstalledModules { } /** Set moduleAddress (using the specified store) */ - function setModuleAddress(IStore _store, bytes16 moduleName, bytes32 argumentsHash, address moduleAddress) internal { - bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = bytes32(moduleName); - _keyTuple[1] = argumentsHash; - - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((moduleAddress)), getFieldLayout()); - } - - /** Get the full data */ - function get(bytes16 moduleName, bytes32 argumentsHash) internal view returns (InstalledModulesData memory _table) { - bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = bytes32(moduleName); - _keyTuple[1] = argumentsHash; - - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getFieldLayout()); - return decode(_blob); - } - - /** Get the full data (using the specified store) */ - function get( - IStore _store, - bytes16 moduleName, - bytes32 argumentsHash - ) internal view returns (InstalledModulesData memory _table) { - bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = bytes32(moduleName); - _keyTuple[1] = argumentsHash; - - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getFieldLayout()); - return decode(_blob); - } - - /** Set the full data using individual values */ - function set(bytes16 moduleName, bytes32 argumentsHash, address moduleAddress) internal { - bytes memory _data = encode(moduleAddress); - - bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = bytes32(moduleName); - _keyTuple[1] = argumentsHash; - - StoreSwitch.setRecord(_tableId, _keyTuple, _data, getFieldLayout()); - } - - /** Set the full data using individual values (using the specified store) */ function set(IStore _store, bytes16 moduleName, bytes32 argumentsHash, address moduleAddress) internal { - bytes memory _data = encode(moduleAddress); - bytes32[] memory _keyTuple = new bytes32[](2); _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - _store.setRecord(_tableId, _keyTuple, _data, getFieldLayout()); - } - - /** Set the full data using the data struct */ - function set(bytes16 moduleName, bytes32 argumentsHash, InstalledModulesData memory _table) internal { - set(moduleName, argumentsHash, _table.moduleAddress); - } - - /** Set the full data using the data struct (using the specified store) */ - function set(IStore _store, bytes16 moduleName, bytes32 argumentsHash, InstalledModulesData memory _table) internal { - set(_store, moduleName, argumentsHash, _table.moduleAddress); - } - - /** Decode the tightly packed blob using this table's field layout */ - function decode(bytes memory _blob) internal pure returns (InstalledModulesData memory _table) { - _table.moduleAddress = (address(Bytes.slice20(_blob, 0))); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((moduleAddress)), getFieldLayout()); } /** Tightly pack full data using this table's field layout */ diff --git a/packages/world/test/Factories.t.sol b/packages/world/test/Factories.t.sol index 0609c16938..9cc72f6a3d 100644 --- a/packages/world/test/Factories.t.sol +++ b/packages/world/test/Factories.t.sol @@ -9,7 +9,7 @@ import { CoreModule } from "../src/modules/core/CoreModule.sol"; import { Create2Factory } from "../src/factories/Create2Factory.sol"; import { WorldFactory } from "../src/factories/WorldFactory.sol"; import { IWorldFactory } from "../src/factories/IWorldFactory.sol"; -import { InstalledModules, InstalledModulesData } from "../src/tables/InstalledModules.sol"; +import { InstalledModules } from "../src/tables/InstalledModules.sol"; import { NamespaceOwner } from "../src/tables/NamespaceOwner.sol"; import { ROOT_NAMESPACE } from "../src/constants.sol"; @@ -70,10 +70,10 @@ contract FactoriesTest is Test { StoreSwitch.setStoreAddress(calculatedAddress); // Retrieve CoreModule address from InstalledModule table - InstalledModulesData memory installedModule = InstalledModules.get(bytes16("core.m"), keccak256(new bytes(0))); + address installedModule = InstalledModules.get(bytes16("core.m"), keccak256(new bytes(0))); // Confirm correct Core is installed - assertEq(installedModule.moduleAddress, address(coreModule)); + assertEq(installedModule, address(coreModule)); // Confirm worldCount (which is salt) has incremented assertEq(uint256(worldFactory.worldCount()), uint256(1));