Skip to content

Commit

Permalink
refactor(cli,world-module-metadata): install metadata module once (#3037
Browse files Browse the repository at this point in the history
)
  • Loading branch information
holic authored Aug 15, 2024
1 parent 6a66f57 commit 5dafc63
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
11 changes: 5 additions & 6 deletions packages/cli/src/deploy/ensureModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,15 @@ export async function ensureModules({
args: [moduleAddress, mod.installData],
});
} catch (error) {
if (mod.optional) {
debug(
`optional module ${mod.name} install failed, skipping\n ${error instanceof BaseError ? error.shortMessage : error}`,
);
return;
}
if (error instanceof BaseError && error.message.includes("Module_AlreadyInstalled")) {
debug(`module ${mod.name} already installed`);
return;
}
if (mod.optional) {
debug(`optional module ${mod.name} install failed, skipping`);
debug(error);
return;
}
throw error;
}
},
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/deploy/ensureResourceLabels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function ensureResourceLabels({
return [];
}

debug(`setting ${resources.length} resource labels`);
debug("setting", resources.length, "resource labels");
return (
await Promise.all(
resourcesToSet.map(async ({ resourceId, label }) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/world-module-metadata/gas-report.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"file": "test/MetadataModule.t.sol",
"test": "testInstall",
"name": "install metadata module",
"gasUsed": 1106562
"gasUsed": 1109853
},
{
"file": "test/MetadataModule.t.sol",
Expand Down
6 changes: 5 additions & 1 deletion packages/world-module-metadata/src/MetadataModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ contract MetadataModule is Module {
revert Module_RootInstallNotSupported();
}

function install(bytes memory) public {
function install(bytes memory args) public {
// naive check to ensure this is only installed once
// TODO: update this + deployer to be idempotent
requireNotInstalled(__self, args);

IBaseWorld world = IBaseWorld(_world());

ResourceId namespace = ResourceTag._tableId.getNamespaceId();
Expand Down
14 changes: 11 additions & 3 deletions packages/world-module-metadata/test/MetadataModule.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol";
import { createWorld } from "@latticexyz/world/test/createWorld.sol";
import { ResourceId, WorldResourceIdLib, WorldResourceIdInstance } from "@latticexyz/world/src/WorldResourceId.sol";
import { IWorldErrors } from "@latticexyz/world/src/IWorldErrors.sol";
import { IModuleErrors } from "@latticexyz/world/src/IModuleErrors.sol";
import { NamespaceOwner } from "@latticexyz/world/src/codegen/tables/NamespaceOwner.sol";

import { MetadataModule } from "../src/MetadataModule.sol";
Expand All @@ -19,6 +20,7 @@ contract MetadataModuleTest is Test, GasReporter {

IWorld world;
MetadataModule metadataModule = new MetadataModule();
ResourceId namespace = ResourceTag._tableId.getNamespaceId();

function setUp() public {
world = IWorld(address(createWorld()));
Expand All @@ -30,16 +32,22 @@ contract MetadataModuleTest is Test, GasReporter {
world.installModule(metadataModule, new bytes(0));
endGasReport();

ResourceId namespace = ResourceTag._tableId.getNamespaceId();
assertEq(NamespaceOwner.get(namespace), address(this));

// Installing again will revert because metadata namespace isn't owned by the module, so the module is unable to write to it.
vm.expectRevert(IModuleErrors.Module_AlreadyInstalled.selector);
world.installModule(metadataModule, new bytes(0));
}

function testInstallExistingNamespace() public {
world.registerNamespace(namespace);

// Installing will revert because metadata namespace isn't owned by the module, so the module is unable to write to it.
vm.expectRevert(
abi.encodeWithSelector(IWorldErrors.World_AccessDenied.selector, namespace.toString(), address(metadataModule))
);
world.installModule(metadataModule, new bytes(0));

// Transferring the namespace to the module and installing should be a no-op/idempotent and the module will return namespace ownership.
// Transferring the namespace to the module and installing will return namespace ownership.
world.transferOwnership(namespace, address(metadataModule));
world.installModule(metadataModule, new bytes(0));
assertEq(NamespaceOwner.get(namespace), address(this));
Expand Down

0 comments on commit 5dafc63

Please sign in to comment.