@@ -21,29 +21,22 @@ import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol";
2121import { WorldConsumer } from "../src/experimental/WorldConsumer.sol " ;
2222
2323contract MockWorldConsumer is WorldConsumer {
24- constructor (
25- IBaseWorld world ,
26- bytes14 namespace ,
27- bool registerNamespace
28- ) WorldConsumer (world, namespace, registerNamespace) {}
24+ bytes14 immutable namespace;
25+ constructor (IBaseWorld world , bytes14 _namespace ) WorldConsumer (world) {
26+ namespace = _namespace;
27+ }
2928
3029 function getStoreAddress () public view virtual returns (address ) {
3130 return StoreSwitch.getStoreAddress ();
3231 }
3332
34- function grantNamespaceAccess (address to ) external {
35- IBaseWorld (_world ()).grantAccess (namespaceId, to);
36- }
37-
38- function transferNamespaceOwnership (address to ) external {
39- IBaseWorld (_world ()).transferOwnership (namespaceId, to);
40- }
41-
4233 function callableByAnyone () external view {}
4334
4435 function onlyCallableByWorld () external view onlyWorld {}
4536
46- function onlyCallableByNamespace () external view onlyNamespace {}
37+ function onlyCallableByNamespace () external view onlyNamespace (namespace) {}
38+
39+ function onlyCallableByNamespaceOwner () external view onlyNamespaceOwner (namespace) {}
4740
4841 function payableFn () external payable returns (uint256 value ) {
4942 return _msgValue ();
@@ -53,33 +46,33 @@ contract MockWorldConsumer is WorldConsumer {
5346contract WorldConsumerTest is Test , GasReporter {
5447 using WorldResourceIdInstance for ResourceId;
5548
56- function testWorldConsumer () public {
57- IBaseWorld world = createWorld ();
58- bytes14 namespace = "myNamespace " ;
59- ResourceId namespaceId = WorldResourceIdLib.encodeNamespace (namespace);
49+ bytes14 constant namespace = "myNamespace " ;
50+ bytes16 constant systemName = "mySystem " ;
6051
61- MockWorldConsumer mock = new MockWorldConsumer (world, namespace, true );
62- assertEq (mock.getStoreAddress (), address (world));
63- assertEq (mock.namespace (), namespace);
64- assertEq (mock.namespaceId ().unwrap (), namespaceId.unwrap ());
52+ ResourceId systemId;
53+ ResourceId namespaceId;
54+
55+ IBaseWorld world;
56+ MockWorldConsumer mock;
6557
58+ function setUp () public {
59+ world = createWorld ();
6660 StoreSwitch.setStoreAddress (address (world));
6761
68- assertTrue (ResourceIds.getExists (namespaceId), "Namespace not registered " );
62+ namespaceId = WorldResourceIdLib.encodeNamespace (namespace);
63+ world.registerNamespace (namespaceId);
64+
65+ systemId = WorldResourceIdLib.encode (RESOURCE_SYSTEM, namespace, systemName);
66+
67+ mock = new MockWorldConsumer (world, namespace);
68+ }
69+
70+ function testWorldConsumer () public view {
71+ assertEq (mock.getStoreAddress (), address (world));
6972 }
7073
7174 // Test internal MUD access control
7275 function testAccessControl () public {
73- IBaseWorld world = createWorld ();
74- StoreSwitch.setStoreAddress (address (world));
75-
76- bytes16 systemName = "mySystem " ;
77- bytes14 namespace = "myNamespace " ;
78- ResourceId namespaceId = WorldResourceIdLib.encodeNamespace (namespace);
79- ResourceId systemId = WorldResourceIdLib.encode (RESOURCE_SYSTEM, namespace, systemName);
80- MockWorldConsumer mock = new MockWorldConsumer (world, namespace, true );
81- mock.transferNamespaceOwnership (address (this ));
82-
8376 // Register the mock as a system with PRIVATE access
8477 world.registerSystem (systemId, mock, false );
8578
@@ -96,15 +89,6 @@ contract WorldConsumerTest is Test, GasReporter {
9689 }
9790
9891 function testOnlyWorld () public {
99- IBaseWorld world = createWorld ();
100- StoreSwitch.setStoreAddress (address (world));
101-
102- bytes16 systemName = "mySystem " ;
103- bytes14 namespace = "myNamespace " ;
104- ResourceId systemId = WorldResourceIdLib.encode (RESOURCE_SYSTEM, namespace, systemName);
105- MockWorldConsumer mock = new MockWorldConsumer (world, namespace, true );
106- mock.transferNamespaceOwnership (address (this ));
107-
10892 // Register the mock as a system with PUBLIC access
10993 world.registerSystem (systemId, mock, true );
11094
@@ -119,16 +103,6 @@ contract WorldConsumerTest is Test, GasReporter {
119103 }
120104
121105 function testOnlyNamespace () public {
122- IBaseWorld world = createWorld ();
123- StoreSwitch.setStoreAddress (address (world));
124-
125- bytes16 systemName = "mySystem " ;
126- bytes14 namespace = "myNamespace " ;
127- ResourceId namespaceId = WorldResourceIdLib.encodeNamespace (namespace);
128- ResourceId systemId = WorldResourceIdLib.encode (RESOURCE_SYSTEM, namespace, systemName);
129- MockWorldConsumer mock = new MockWorldConsumer (world, namespace, true );
130- mock.transferNamespaceOwnership (address (this ));
131-
132106 // Register the mock as a system with PUBLIC access
133107 world.registerSystem (systemId, mock, true );
134108
@@ -150,16 +124,37 @@ contract WorldConsumerTest is Test, GasReporter {
150124 world.call (systemId, abi.encodeCall (mock.onlyCallableByNamespace, ()));
151125 }
152126
153- function testMsgValue () public {
154- IBaseWorld world = createWorld ();
155- StoreSwitch. setStoreAddress ( address (world) );
127+ function testOnlyNamespaceOwner () public {
128+ // Register the mock as a system with PUBLIC access
129+ world. registerSystem (systemId, mock, true );
156130
157- bytes16 systemName = "mySystem " ;
158- bytes14 namespace = "myNamespace " ;
159- ResourceId systemId = WorldResourceIdLib.encode (RESOURCE_SYSTEM, namespace, systemName);
160- MockWorldConsumer mock = new MockWorldConsumer (world, namespace, true );
161- mock.transferNamespaceOwnership (address (this ));
131+ address alice = address (0x1234 );
162132
133+ vm.prank (alice);
134+ vm.expectRevert (abi.encodeWithSelector (WorldConsumer.WorldConsumer_CallerIsNotWorld.selector , world, alice));
135+ mock.onlyCallableByNamespaceOwner ();
136+
137+ vm.prank (alice);
138+ vm.expectRevert (
139+ abi.encodeWithSelector (WorldConsumer.WorldConsumer_CallerIsNotNamespaceOwner.selector , world, namespace, alice)
140+ );
141+ world.call (systemId, abi.encodeCall (mock.onlyCallableByNamespaceOwner, ()));
142+
143+ // After granting access to namespace, it should not work
144+ world.grantAccess (namespaceId, alice);
145+ vm.prank (alice);
146+ vm.expectRevert (
147+ abi.encodeWithSelector (WorldConsumer.WorldConsumer_CallerIsNotNamespaceOwner.selector , world, namespace, alice)
148+ );
149+ world.call (systemId, abi.encodeCall (mock.onlyCallableByNamespaceOwner, ()));
150+
151+ // After transfering namespace ownership, it should work
152+ world.transferOwnership (namespaceId, alice);
153+ vm.prank (alice);
154+ world.call (systemId, abi.encodeCall (mock.onlyCallableByNamespaceOwner, ()));
155+ }
156+
157+ function testMsgValue () public {
163158 // Register the mock as a system with PUBLIC access
164159 world.registerSystem (systemId, mock, true );
165160
0 commit comments