Skip to content

Commit

Permalink
fix(world): register store namespace during initialization (#1712)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Oct 6, 2023
1 parent 08d7c47 commit 430e6b2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/eight-deers-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@latticexyz/world": patch
---

Register the `store` namespace in the `CoreModule`.
Since namespaces are a World concept, registering the Store's internal tables does not automatically register the Store's namespace, so we do this manually during initialization in the `CoreModule`.
4 changes: 4 additions & 0 deletions packages/world/src/constants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { RESOURCE_SYSTEM, RESOURCE_NAMESPACE } from "./worldResourceTypes.sol";
bytes14 constant ROOT_NAMESPACE = "";
bytes16 constant ROOT_NAME = "";

ResourceId constant STORE_NAMESPACE_ID = ResourceId.wrap(
bytes32(abi.encodePacked(RESOURCE_NAMESPACE, bytes14("store"), ROOT_NAME))
);

ResourceId constant WORLD_NAMESPACE_ID = ResourceId.wrap(
bytes32(abi.encodePacked(RESOURCE_NAMESPACE, bytes14("world"), ROOT_NAME))
);
Expand Down
6 changes: 5 additions & 1 deletion packages/world/src/modules/core/CoreModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity >=0.8.21;

import { WorldContextProviderLib } from "../../WorldContext.sol";
import { ROOT_NAMESPACE, ROOT_NAMESPACE_ID, WORLD_NAMESPACE_ID } from "../../constants.sol";
import { ROOT_NAMESPACE, ROOT_NAMESPACE_ID, STORE_NAMESPACE_ID, WORLD_NAMESPACE_ID } from "../../constants.sol";
import { Module } from "../../Module.sol";

import { IBaseWorld } from "../../codegen/interfaces/IBaseWorld.sol";
Expand Down Expand Up @@ -94,6 +94,10 @@ contract CoreModule is Module {
NamespaceOwner._set(ROOT_NAMESPACE_ID, _msgSender());
ResourceAccess._set(ROOT_NAMESPACE_ID, _msgSender(), true);

ResourceIds._setExists(STORE_NAMESPACE_ID, true);
NamespaceOwner._set(STORE_NAMESPACE_ID, _msgSender());
ResourceAccess._set(STORE_NAMESPACE_ID, _msgSender(), true);

ResourceIds._setExists(WORLD_NAMESPACE_ID, true);
NamespaceOwner._set(WORLD_NAMESPACE_ID, _msgSender());
ResourceAccess._set(WORLD_NAMESPACE_ID, _msgSender(), true);
Expand Down
31 changes: 30 additions & 1 deletion packages/world/test/World.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { WORLD_VERSION } from "../src/version.sol";
import { World } from "../src/World.sol";
import { System } from "../src/System.sol";
import { ResourceId, WorldResourceIdLib, WorldResourceIdInstance } from "../src/WorldResourceId.sol";
import { ROOT_NAMESPACE, ROOT_NAME, ROOT_NAMESPACE_ID, UNLIMITED_DELEGATION } from "../src/constants.sol";
import { ROOT_NAMESPACE, ROOT_NAME, ROOT_NAMESPACE_ID, UNLIMITED_DELEGATION, WORLD_NAMESPACE_ID, STORE_NAMESPACE_ID } from "../src/constants.sol";
import { RESOURCE_TABLE, RESOURCE_SYSTEM, RESOURCE_NAMESPACE } from "../src/worldResourceTypes.sol";
import { WorldContextProviderLib, WORLD_CONTEXT_CONSUMER_INTERFACE_ID } from "../src/WorldContext.sol";
import { SystemHook } from "../src/SystemHook.sol";
Expand Down Expand Up @@ -344,6 +344,35 @@ contract WorldTest is Test, GasReporter {
world.registerNamespace(namespaceId);
}

function testRegisterCoreNamespacesRevert() public {
vm.expectRevert(
abi.encodeWithSelector(
IWorldErrors.World_ResourceAlreadyExists.selector,
ROOT_NAMESPACE_ID,
ROOT_NAMESPACE_ID.toString()
)
);
world.registerNamespace(ROOT_NAMESPACE_ID);

vm.expectRevert(
abi.encodeWithSelector(
IWorldErrors.World_ResourceAlreadyExists.selector,
WORLD_NAMESPACE_ID,
WORLD_NAMESPACE_ID.toString()
)
);
world.registerNamespace(WORLD_NAMESPACE_ID);

vm.expectRevert(
abi.encodeWithSelector(
IWorldErrors.World_ResourceAlreadyExists.selector,
STORE_NAMESPACE_ID,
STORE_NAMESPACE_ID.toString()
)
);
world.registerNamespace(STORE_NAMESPACE_ID);
}

function testRegisterNamespaceRevertInvalidType() public {
ResourceId invalidNamespaceId = WorldResourceIdLib.encode({
typeId: RESOURCE_TABLE,
Expand Down

0 comments on commit 430e6b2

Please sign in to comment.