Skip to content

Commit

Permalink
refactor(world): remove workaround in mud config (#1501)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Ingersoll <kingersoll@gmail.com>
  • Loading branch information
alvrs and holic committed Sep 15, 2023
1 parent e0193e5 commit 4c7fd3e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 96 deletions.
5 changes: 5 additions & 0 deletions .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.
26 changes: 13 additions & 13 deletions packages/world/gas-report.json
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
5 changes: 0 additions & 5 deletions packages/world/mud.config.ts
Expand Up @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion packages/world/src/Tables.sol
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion packages/world/src/World.sol
Expand Up @@ -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();
}

Expand Down
77 changes: 4 additions & 73 deletions packages/world/src/tables/InstalledModules.sol
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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 */
Expand Down
6 changes: 3 additions & 3 deletions packages/world/test/Factories.t.sol
Expand Up @@ -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";

Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 4c7fd3e

Please sign in to comment.