diff --git a/packages/world/src/modules/keysintable/KeysInTableModule.sol b/packages/world/src/modules/keysintable/KeysInTableModule.sol index 74abc6ad84..a80e7de6c2 100644 --- a/packages/world/src/modules/keysintable/KeysInTableModule.sol +++ b/packages/world/src/modules/keysintable/KeysInTableModule.sol @@ -47,8 +47,8 @@ contract KeysInTableModule is IModule, WorldContext { UsedKeysIndex.register(world); // Grant the hook access to the tables - world.grantAccess(KeysInTableTableId.getNamespace(), KeysInTableTableId.getName(), address(hook)); - world.grantAccess(UsedKeysIndexTableId.getNamespace(), UsedKeysIndexTableId.getName(), address(hook)); + world.grantAccess(KeysInTableTableId, address(hook)); + world.grantAccess(UsedKeysIndexTableId, address(hook)); } // Register a hook that is called when a value is set in the source table diff --git a/packages/world/src/modules/keyswithvalue/KeysWithValueModule.sol b/packages/world/src/modules/keyswithvalue/KeysWithValueModule.sol index 43c8ca34cb..575ae3238f 100644 --- a/packages/world/src/modules/keyswithvalue/KeysWithValueModule.sol +++ b/packages/world/src/modules/keyswithvalue/KeysWithValueModule.sol @@ -45,7 +45,7 @@ contract KeysWithValueModule is IModule, WorldContext { KeysWithValue.register(IBaseWorld(_world()), targetTableSelector); // Grant the hook access to the target table - IBaseWorld(_world()).grantAccess(targetTableSelector.getNamespace(), targetTableSelector.getName(), address(hook)); + IBaseWorld(_world()).grantAccess(targetTableSelector, address(hook)); // Register a hook that is called when a value is set in the source table StoreSwitch.registerStoreHook(sourceTableId, hook); diff --git a/packages/world/test/AccessControl.t.sol b/packages/world/test/AccessControl.t.sol index fcafb4f67d..39092d375c 100644 --- a/packages/world/test/AccessControl.t.sol +++ b/packages/world/test/AccessControl.t.sol @@ -21,35 +21,35 @@ contract AccessControlTest is Test, StoreReadWithStubs { NamespaceOwner.register(); NamespaceOwner.set(namespace, address(this)); - ResourceAccess.set(ResourceSelector.from(namespace, 0), address(this), true); + ResourceAccess.set(ResourceSelector.from(namespace), address(this), true); } function testAccessControl() public { // Check that the caller has no access to the namespace or name - assertFalse(AccessControl.hasAccess(namespace, name, caller)); + assertFalse(AccessControl.hasAccess(ResourceSelector.from(namespace, name), caller)); // Grant access to the namespace ResourceAccess.set(ResourceSelector.from(namespace, 0), caller, true); // Check that the caller has access to the namespace or name - assertTrue(AccessControl.hasAccess(namespace, name, caller)); + assertTrue(AccessControl.hasAccess(ResourceSelector.from(namespace, name), caller)); // Revoke access to the namespace ResourceAccess.set(ResourceSelector.from(namespace, 0), caller, false); // Check that the caller has no access to the namespace or name - assertFalse(AccessControl.hasAccess(namespace, name, caller)); + assertFalse(AccessControl.hasAccess(ResourceSelector.from(namespace, name), caller)); // Grant access to the name ResourceAccess.set(ResourceSelector.from(namespace, name), caller, true); // Check that the caller has access to the name - assertTrue(AccessControl.hasAccess(namespace, name, caller)); + assertTrue(AccessControl.hasAccess(ResourceSelector.from(namespace, name), caller)); // Revoke access to the name ResourceAccess.set(ResourceSelector.from(namespace, name), caller, false); // Check that the caller has no access to the namespace or name - assertFalse(AccessControl.hasAccess(namespace, name, caller)); + assertFalse(AccessControl.hasAccess(ResourceSelector.from(namespace, name), caller)); } } diff --git a/packages/world/test/KeysInTableModule.t.sol b/packages/world/test/KeysInTableModule.t.sol index a1547985c1..1968d14a00 100644 --- a/packages/world/test/KeysInTableModule.t.sol +++ b/packages/world/test/KeysInTableModule.t.sol @@ -38,9 +38,9 @@ contract KeysInTableModuleTest is Test, GasReporter { Schema tableKeySchema; Schema singletonKeySchema; Schema compositeKeySchema; - bytes32 tableId; - bytes32 singletonTableId; - bytes32 compositeTableId; + bytes32 tableId = ResourceSelector.from(namespace, name); + bytes32 singletonTableId = ResourceSelector.from(namespace, singletonName); + bytes32 compositeTableId = ResourceSelector.from(namespace, compositeName); uint256 val1 = 123; uint256 val2 = 42; @@ -65,23 +65,9 @@ contract KeysInTableModuleTest is Test, GasReporter { function _installKeysInTableModule() internal { // Register source table - tableId = world.registerTable(namespace, name, tableKeySchema, tableValueSchema, new string[](1), new string[](1)); - singletonTableId = world.registerTable( - namespace, - singletonName, - singletonKeySchema, - tableValueSchema, - new string[](0), - new string[](1) - ); - compositeTableId = world.registerTable( - namespace, - compositeName, - compositeKeySchema, - tableValueSchema, - new string[](3), - new string[](1) - ); + world.registerTable(tableId, tableKeySchema, tableValueSchema, new string[](1), new string[](1)); + world.registerTable(singletonTableId, singletonKeySchema, tableValueSchema, new string[](0), new string[](1)); + world.registerTable(compositeTableId, compositeKeySchema, tableValueSchema, new string[](3), new string[](1)); // Install the index module // TODO: add support for installing this via installModule @@ -100,7 +86,7 @@ contract KeysInTableModuleTest is Test, GasReporter { bytes32[] memory keyTuple = new bytes32[](0); - world.setRecord(namespace, singletonName, keyTuple, abi.encodePacked(val1), tableValueSchema); + world.setRecord(singletonTableId, keyTuple, abi.encodePacked(val1), tableValueSchema); // Get the list of keys in this target table bytes32[][] memory keysInTable = getKeysInTable(world, singletonTableId); @@ -117,7 +103,7 @@ contract KeysInTableModuleTest is Test, GasReporter { keyTuple[1] = "two"; keyTuple[2] = "three"; - world.setRecord(namespace, compositeName, keyTuple, abi.encodePacked(val1), tableValueSchema); + world.setRecord(compositeTableId, keyTuple, abi.encodePacked(val1), tableValueSchema); // Get the list of keys in this target table bytes32[][] memory keysInTable = getKeysInTable(world, compositeTableId); @@ -142,7 +128,7 @@ contract KeysInTableModuleTest is Test, GasReporter { _installKeysInTableModule(); // Set a value in the source table startGasReport("set a record on a table with keysInTableModule installed"); - world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value), tableValueSchema); + world.setRecord(tableId, keyTuple1, abi.encodePacked(value), tableValueSchema); endGasReport(); // Get the list of keys in this target table @@ -161,7 +147,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Set a value in the source table startGasReport("set a record on a table with keysInTableModule installed (first)"); - world.setRecord(namespace, name, keyTuple, abi.encodePacked(value1), tableValueSchema); + world.setRecord(tableId, keyTuple, abi.encodePacked(value1), tableValueSchema); endGasReport(); // Get the list of keys in the first target table @@ -173,14 +159,8 @@ contract KeysInTableModuleTest is Test, GasReporter { // Install the hook on the second table bytes16 sourceFile2 = bytes16("source2"); - bytes32 sourceTableId2 = world.registerTable( - namespace, - sourceFile2, - tableValueSchema, - tableKeySchema, - new string[](1), - new string[](1) - ); + bytes32 sourceTableId2 = ResourceSelector.from(namespace, sourceFile2); + world.registerTable(sourceTableId2, tableValueSchema, tableKeySchema, new string[](1), new string[](1)); world.installRootModule(keysInTableModule, abi.encode(sourceTableId2)); keyTuple = new bytes32[](1); @@ -188,7 +168,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Set a value in the source table startGasReport("set a record on a table with keysInTableModule installed (second)"); - world.setRecord(namespace, sourceFile2, keyTuple, abi.encodePacked(value2), tableValueSchema); + world.setRecord(sourceTableId2, keyTuple, abi.encodePacked(value2), tableValueSchema); endGasReport(); // Get the list of keys in the second target table @@ -208,7 +188,7 @@ contract KeysInTableModuleTest is Test, GasReporter { _installKeysInTableModule(); // Set a value in the source table - world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value1), tableValueSchema); + world.setRecord(tableId, keyTuple1, abi.encodePacked(value1), tableValueSchema); // Get the list of keys in the target table bytes32[][] memory keysInTable = getKeysInTable(world, tableId); @@ -218,7 +198,7 @@ contract KeysInTableModuleTest is Test, GasReporter { assertEq(keysInTable[0][0], key1, "2"); // Set another key with the same value - world.setRecord(namespace, name, keyTuple2, abi.encodePacked(value1), tableValueSchema); + world.setRecord(tableId, keyTuple2, abi.encodePacked(value1), tableValueSchema); // Get the list of keys in the target table keysInTable = getKeysInTable(world, tableId); @@ -230,7 +210,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Change the value of the first key startGasReport("change a record on a table with keysInTableModule installed"); - world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value2), tableValueSchema); + world.setRecord(tableId, keyTuple1, abi.encodePacked(value2), tableValueSchema); endGasReport(); // Get the list of keys in the target table @@ -243,7 +223,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Delete the first key startGasReport("delete a record on a table with keysInTableModule installed"); - world.deleteRecord(namespace, name, keyTuple1, tableValueSchema); + world.deleteRecord(tableId, keyTuple1, tableValueSchema); endGasReport(); // Get the list of keys in the target table @@ -273,7 +253,7 @@ contract KeysInTableModuleTest is Test, GasReporter { keyTupleB[2] = "charlie"; // Set a value in the source table - world.setRecord(namespace, compositeName, keyTupleA, abi.encodePacked(value1), tableValueSchema); + world.setRecord(compositeTableId, keyTupleA, abi.encodePacked(value1), tableValueSchema); // Get the list of keys in the target table bytes32[][] memory keysInTable = getKeysInTable(world, compositeTableId); @@ -285,7 +265,7 @@ contract KeysInTableModuleTest is Test, GasReporter { } // Set another key with the same value - world.setRecord(namespace, compositeName, keyTupleB, abi.encodePacked(value1), tableValueSchema); + world.setRecord(compositeTableId, keyTupleB, abi.encodePacked(value1), tableValueSchema); // Get the list of keys in the target table keysInTable = getKeysInTable(world, compositeTableId); @@ -301,7 +281,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Change the value of the first key startGasReport("change a composite record on a table with keysInTableModule installed"); - world.setRecord(namespace, compositeName, keyTupleA, abi.encodePacked(value2), tableValueSchema); + world.setRecord(compositeTableId, keyTupleA, abi.encodePacked(value2), tableValueSchema); endGasReport(); // Get the list of keys in the target table @@ -318,7 +298,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Delete the first key startGasReport("delete a composite record on a table with keysInTableModule installed"); - world.deleteRecord(namespace, compositeName, keyTupleA, tableValueSchema); + world.deleteRecord(compositeTableId, keyTupleA, tableValueSchema); endGasReport(); // Get the list of keys in the target table @@ -336,7 +316,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Set a value in the source table startGasReport("set a field on a table with keysInTableModule installed"); - world.setField(namespace, name, keyTuple1, 0, abi.encodePacked(value1), tableValueSchema); + world.setField(tableId, keyTuple1, 0, abi.encodePacked(value1), tableValueSchema); endGasReport(); // Get the list of keys in the target table @@ -348,7 +328,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Change the value using setField startGasReport("change a field on a table with keysInTableModule installed"); - world.setField(namespace, name, keyTuple1, 0, abi.encodePacked(value2), tableValueSchema); + world.setField(tableId, keyTuple1, 0, abi.encodePacked(value2), tableValueSchema); endGasReport(); // Get the list of keys in the target table @@ -363,7 +343,7 @@ contract KeysInTableModuleTest is Test, GasReporter { _installKeysInTableModule(); // Set a value in the source table - world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value1), tableValueSchema); + world.setRecord(tableId, keyTuple1, abi.encodePacked(value1), tableValueSchema); startGasReport("Get list of keys in a given table"); bytes32[][] memory keysInTable = getKeysInTable(world, tableId); @@ -374,7 +354,7 @@ contract KeysInTableModuleTest is Test, GasReporter { assertEq(keysInTable[0][0], key1); // Set another key with a different value - world.setRecord(namespace, name, keyTuple2, abi.encodePacked(value2), tableValueSchema); + world.setRecord(tableId, keyTuple2, abi.encodePacked(value2), tableValueSchema); // Get the list of keys in the target table keysInTable = getKeysInTable(world, tableId); @@ -389,14 +369,14 @@ contract KeysInTableModuleTest is Test, GasReporter { _installKeysInTableModule(); // Add 3 values - world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value), tableValueSchema); - world.setRecord(namespace, name, keyTuple2, abi.encodePacked(value), tableValueSchema); - world.setRecord(namespace, name, keyTuple3, abi.encodePacked(value), tableValueSchema); + world.setRecord(tableId, keyTuple1, abi.encodePacked(value), tableValueSchema); + world.setRecord(tableId, keyTuple2, abi.encodePacked(value), tableValueSchema); + world.setRecord(tableId, keyTuple3, abi.encodePacked(value), tableValueSchema); // Remove 2, starting from the middle // This tests that KeysInTable correctly tracks swaps indexes - world.deleteRecord(namespace, name, keyTuple2, tableValueSchema); - world.deleteRecord(namespace, name, keyTuple3, tableValueSchema); + world.deleteRecord(tableId, keyTuple2, tableValueSchema); + world.deleteRecord(tableId, keyTuple3, tableValueSchema); // Get the list of keys in the target table bytes32[][] memory keysInTable = getKeysInTable(world, tableId); diff --git a/packages/world/test/KeysWithValueModule.t.sol b/packages/world/test/KeysWithValueModule.t.sol index 3f55d0b8d7..4e27ae8cc7 100644 --- a/packages/world/test/KeysWithValueModule.t.sol +++ b/packages/world/test/KeysWithValueModule.t.sol @@ -52,14 +52,7 @@ contract KeysWithValueModuleTest is Test, GasReporter { function _installKeysWithValueModule() internal { // Register source table - sourceTableId = world.registerTable( - namespace, - sourceName, - sourceTableSchema, - sourceTableKeySchema, - new string[](1), - new string[](1) - ); + world.registerTable(sourceTableId, sourceTableSchema, sourceTableKeySchema, new string[](1), new string[](1)); // Install the index module // TODO: add support for installing this via installModule @@ -75,7 +68,7 @@ contract KeysWithValueModuleTest is Test, GasReporter { uint256 value = 1; startGasReport("set a record on a table with KeysWithValueModule installed"); - world.setRecord(namespace, sourceName, keyTuple1, abi.encodePacked(value), sourceTableSchema); + world.setRecord(sourceTableId, keyTuple1, abi.encodePacked(value), sourceTableSchema); endGasReport(); // Get the list of entities with this value from the target table @@ -92,7 +85,7 @@ contract KeysWithValueModuleTest is Test, GasReporter { // Set a value in the source table uint256 value1 = 1; - world.setRecord(namespace, sourceName, keyTuple1, abi.encodePacked(value1), sourceTableSchema); + world.setRecord(sourceTableId, keyTuple1, abi.encodePacked(value1), sourceTableSchema); // Get the list of entities with value1 from the target table bytes32[] memory keysWithValue = KeysWithValue.get(world, targetTableId, keccak256(abi.encode(value1))); @@ -102,7 +95,7 @@ contract KeysWithValueModuleTest is Test, GasReporter { assertEq(keysWithValue[0], key1, "2"); // Set a another key with the same value - world.setRecord(namespace, sourceName, keyTuple2, abi.encodePacked(value1), sourceTableSchema); + world.setRecord(sourceTableId, keyTuple2, abi.encodePacked(value1), sourceTableSchema); // Get the list of entities with value2 from the target table keysWithValue = KeysWithValue.get(world, targetTableId, keccak256(abi.encode(value1))); @@ -116,7 +109,7 @@ contract KeysWithValueModuleTest is Test, GasReporter { uint256 value2 = 2; startGasReport("change a record on a table with KeysWithValueModule installed"); - world.setRecord(namespace, sourceName, keyTuple1, abi.encodePacked(value2), sourceTableSchema); + world.setRecord(sourceTableId, keyTuple1, abi.encodePacked(value2), sourceTableSchema); endGasReport(); // Get the list of entities with value1 from the target table @@ -135,7 +128,7 @@ contract KeysWithValueModuleTest is Test, GasReporter { // Delete the first key startGasReport("delete a record on a table with KeysWithValueModule installed"); - world.deleteRecord(namespace, sourceName, keyTuple1, sourceTableSchema); + world.deleteRecord(sourceTableId, keyTuple1, sourceTableSchema); endGasReport(); // Get the list of entities with value2 from the target table @@ -152,7 +145,7 @@ contract KeysWithValueModuleTest is Test, GasReporter { uint256 value1 = 1; startGasReport("set a field on a table with KeysWithValueModule installed"); - world.setField(namespace, sourceName, keyTuple1, 0, abi.encodePacked(value1), sourceTableSchema); + world.setField(sourceTableId, keyTuple1, 0, abi.encodePacked(value1), sourceTableSchema); endGasReport(); // Get the list of entities with value1 from the target table @@ -166,7 +159,7 @@ contract KeysWithValueModuleTest is Test, GasReporter { // Change the value using setField startGasReport("change a field on a table with KeysWithValueModule installed"); - world.setField(namespace, sourceName, keyTuple1, 0, abi.encodePacked(value2), sourceTableSchema); + world.setField(sourceTableId, keyTuple1, 0, abi.encodePacked(value2), sourceTableSchema); endGasReport(); // Get the list of entities with value1 from the target table @@ -207,7 +200,7 @@ contract KeysWithValueModuleTest is Test, GasReporter { _installKeysWithValueModule(); // Set a value in the source table - world.setRecord(namespace, sourceName, keyTuple1, abi.encodePacked(value), sourceTableSchema); + world.setRecord(sourceTableId, keyTuple1, abi.encodePacked(value), sourceTableSchema); startGasReport("Get list of keys with a given value"); bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value)); @@ -218,7 +211,7 @@ contract KeysWithValueModuleTest is Test, GasReporter { assertEq(keysWithValue[0], key1); // Set a another key with the same value - world.setRecord(namespace, sourceName, keyTuple2, abi.encodePacked(value), sourceTableSchema); + world.setRecord(sourceTableId, keyTuple2, abi.encodePacked(value), sourceTableSchema); // Get the list of keys with value from the target table keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value)); diff --git a/packages/world/test/Utils.t.sol b/packages/world/test/Utils.t.sol index fa6cf1e012..55d066d32c 100644 --- a/packages/world/test/Utils.t.sol +++ b/packages/world/test/Utils.t.sol @@ -29,9 +29,12 @@ contract UtilsTest is Test { function _registerAndGetNamespace(bytes16 namespace) internal returns (bytes16 returnedNamespace) { UtilsTestSystem testSystem = new UtilsTestSystem(); bytes16 name = "testSystem"; - world.registerSystem(namespace, name, testSystem, true); + world.registerSystem(ResourceSelector.from(namespace, name), testSystem, true); - bytes memory data = world.call(namespace, name, abi.encodeWithSelector(UtilsTestSystem.systemNamespace.selector)); + bytes memory data = world.call( + ResourceSelector.from(namespace, name), + abi.encodeWithSelector(UtilsTestSystem.systemNamespace.selector) + ); returnedNamespace = abi.decode(data, (bytes16)); } diff --git a/packages/world/test/World.t.sol b/packages/world/test/World.t.sol index 75498e8eeb..bec358ae48 100644 --- a/packages/world/test/World.t.sol +++ b/packages/world/test/World.t.sol @@ -84,7 +84,7 @@ contract WorldTestSystem is System { if (StoreSwitch.getStoreAddress() == address(this)) { StoreCore.setRecord(tableId, key, abi.encodePacked(data), valueSchema); } else { - IBaseWorld(msg.sender).setRecord(namespace, name, key, abi.encodePacked(data), valueSchema); + IBaseWorld(msg.sender).setRecord(tableId, key, abi.encodePacked(data), valueSchema); } } @@ -221,10 +221,11 @@ contract WorldTest is Test, GasReporter { function testStoreAddress() public { // Register a system and use it to get storeAddress WorldTestSystem system = new WorldTestSystem(); - world.registerSystem("namespace", "testSystem", system, false); + bytes32 resourceSelector = ResourceSelector.from("namespace", "testSystem"); + + world.registerSystem(resourceSelector, system, false); bytes memory result = world.call( - "namespace", - "testSystem", + resourceSelector, abi.encodeWithSelector(WorldTestSystem.getStoreAddress.selector) ); @@ -253,7 +254,7 @@ contract WorldTest is Test, GasReporter { Schema valueSchema = SchemaEncodeHelper.encode(SchemaType.BOOL, SchemaType.UINT256, SchemaType.STRING); bytes16 namespace = "testNamespace"; bytes16 tableName = "testTable"; - + bytes32 tableSelector = ResourceSelector.from(namespace, tableName); string[] memory keyNames = new string[](1); keyNames[0] = "key1"; string[] memory valueNames = new string[](3); @@ -262,14 +263,7 @@ contract WorldTest is Test, GasReporter { valueNames[2] = "value3"; startGasReport("Register a new table in the namespace"); - bytes32 tableSelector = world.registerTable( - namespace, - tableName, - defaultKeySchema, - valueSchema, - keyNames, - valueNames - ); + world.registerTable(tableSelector, defaultKeySchema, valueSchema, keyNames, valueNames); endGasReport(); // Expect the namespace to be created and owned by the caller @@ -287,24 +281,26 @@ contract WorldTest is Test, GasReporter { // Expect an error when registering an existing table vm.expectRevert(abi.encodeWithSelector(IWorldErrors.ResourceExists.selector, tableSelector.toString())); - world.registerTable(namespace, tableName, defaultKeySchema, valueSchema, keyNames, valueNames); + world.registerTable(tableSelector, defaultKeySchema, valueSchema, keyNames, valueNames); // Expect an error when registering a table in a namespace that is not owned by the caller + bytes32 otherTableSelector = ResourceSelector.from(namespace, "otherTable"); _expectAccessDenied(address(0x01), namespace, ""); - world.registerTable(namespace, "otherTable", defaultKeySchema, valueSchema, keyNames, valueNames); + world.registerTable(otherTableSelector, defaultKeySchema, valueSchema, keyNames, valueNames); // Expect the World to be allowed to call registerTable vm.prank(address(world)); - world.registerTable(namespace, "otherTable", defaultKeySchema, valueSchema, keyNames, valueNames); + world.registerTable(otherTableSelector, defaultKeySchema, valueSchema, keyNames, valueNames); } function testRegisterSystem() public { System system = new System(); bytes16 namespace = ""; bytes16 name = "testSystem"; + bytes32 resourceSelector = ResourceSelector.from(namespace, name); // !gasrepot Register a new system - bytes32 resourceSelector = world.registerSystem(namespace, name, system, false); + world.registerSystem(resourceSelector, system, false); // Expect the system to be registered (address registeredAddress, bool publicAccess) = Systems.get(world, resourceSelector); @@ -328,61 +324,49 @@ contract WorldTest is Test, GasReporter { // Expect the namespace to be created if it doesn't exist yet assertEq(NamespaceOwner.get(world, "newNamespace"), address(0)); - world.registerSystem("newNamespace", "testSystem", new System(), false); + world.registerSystem(ResourceSelector.from("newNamespace", "testSystem"), new System(), false); assertEq(NamespaceOwner.get(world, "newNamespace"), address(this)); // Expect an error when registering an existing system vm.expectRevert(abi.encodeWithSelector(IWorldErrors.SystemExists.selector, address(system))); - world.registerSystem("", "newSystem", system, true); + world.registerSystem(ResourceSelector.from("", "newSystem"), system, true); // Expect an error when registering a system at an existing resource selector System newSystem = new System(); // Expect an error when registering a system at an existing resource selector vm.expectRevert(abi.encodeWithSelector(IWorldErrors.ResourceExists.selector, resourceSelector.toString())); - resourceSelector = world.registerSystem("", "testSystem", newSystem, true); + world.registerSystem(ResourceSelector.from("", "testSystem"), newSystem, true); // Expect an error when registering a system in a namespace is not owned by the caller System yetAnotherSystem = new System(); _expectAccessDenied(address(0x01), "", ""); - world.registerSystem("", "rootSystem", yetAnotherSystem, true); + world.registerSystem(ResourceSelector.from("", "rootSystem"), yetAnotherSystem, true); // Expect the registration to succeed when coming from the World vm.prank(address(world)); - world.registerSystem("", "rootSystem", yetAnotherSystem, true); + world.registerSystem(ResourceSelector.from("", "rootSystem"), yetAnotherSystem, true); } function testDuplicateSelectors() public { // Register a new table - bytes32 resourceSelector = world.registerTable( - "namespace", - "name", - defaultKeySchema, - Bool.getValueSchema(), - new string[](1), - new string[](1) - ); + bytes32 resourceSelector = ResourceSelector.from("namespace", "name"); + world.registerTable(resourceSelector, defaultKeySchema, Bool.getValueSchema(), new string[](1), new string[](1)); // Deploy a new system System system = new System(); // Expect an error when trying to register a system at the same selector vm.expectRevert(abi.encodeWithSelector(IWorldErrors.ResourceExists.selector, resourceSelector.toString())); - world.registerSystem("namespace", "name", system, false); + world.registerSystem(resourceSelector, system, false); // Register a new system - resourceSelector = world.registerSystem("namespace2", "name", new System(), false); + bytes32 resourceSelector2 = ResourceSelector.from("namespace2", "name"); + world.registerSystem(resourceSelector, new System(), false); // Expect an error when trying to register a table at the same selector vm.expectRevert(abi.encodeWithSelector(IWorldErrors.ResourceExists.selector, resourceSelector.toString())); - world.registerTable( - "namespace2", - "name", - defaultKeySchema, - Bool.getValueSchema(), - new string[](1), - new string[](1) - ); + world.registerTable(resourceSelector2, defaultKeySchema, Bool.getValueSchema(), new string[](1), new string[](1)); } function testGrantAccess() public { @@ -394,15 +378,9 @@ contract WorldTest is Test, GasReporter { } function testSetRecord() public { + bytes32 tableId = ResourceSelector.from("testSetRecord", "testTable"); // Register a new table - bytes32 tableId = world.registerTable( - "testSetRecord", - "testTable", - defaultKeySchema, - Bool.getValueSchema(), - new string[](1), - new string[](1) - ); + world.registerTable(tableId, defaultKeySchema, Bool.getValueSchema(), new string[](1), new string[](1)); startGasReport("Write data to the table"); Bool.set(world, tableId, true); @@ -423,20 +401,14 @@ contract WorldTest is Test, GasReporter { function testSetField() public { bytes16 namespace = "testSetField"; bytes16 name = "testTable"; + bytes32 tableId = ResourceSelector.from(namespace, name); Schema valueSchema = Bool.getValueSchema(); // Register a new table - bytes32 tableId = world.registerTable( - namespace, - name, - defaultKeySchema, - valueSchema, - new string[](1), - new string[](1) - ); + world.registerTable(tableId, defaultKeySchema, valueSchema, new string[](1), new string[](1)); startGasReport("Write data to a table field"); - world.setField(namespace, name, singletonKey, 0, abi.encodePacked(true), valueSchema); + world.setField(tableId, singletonKey, 0, abi.encodePacked(true), valueSchema); endGasReport(); // Expect the data to be written @@ -450,7 +422,7 @@ contract WorldTest is Test, GasReporter { // Expect an error when trying to write from an address that doesn't have access when calling via the namespace _expectAccessDenied(address(0x01), "testSetField", "testTable"); - world.setField("testSetField", "testTable", singletonKey, 0, abi.encodePacked(true), valueSchema); + world.setField(tableId, singletonKey, 0, abi.encodePacked(true), valueSchema); // Expect an error when trying to write from an address that doesn't have access when calling via the tableId _expectAccessDenied(address(0x01), "testSetField", "testTable"); @@ -458,23 +430,17 @@ contract WorldTest is Test, GasReporter { // Expect the World to have access vm.prank(address(world)); - world.setField("testSetField", "testTable", singletonKey, 0, abi.encodePacked(true), valueSchema); + world.setField(tableId, singletonKey, 0, abi.encodePacked(true), valueSchema); } function testPushToField() public { bytes16 namespace = "testPushToField"; bytes16 name = "testTable"; + bytes32 tableId = ResourceSelector.from(namespace, name); Schema valueSchema = AddressArray.getValueSchema(); // Register a new table - bytes32 tableId = world.registerTable( - namespace, - name, - defaultKeySchema, - valueSchema, - new string[](1), - new string[](1) - ); + world.registerTable(tableId, defaultKeySchema, valueSchema, new string[](1), new string[](1)); // Create data address[] memory dataToPush = new address[](3); @@ -484,14 +450,14 @@ contract WorldTest is Test, GasReporter { bytes memory encodedData = EncodeArray.encode(dataToPush); startGasReport("Push data to the table"); - world.pushToField(namespace, name, keyTuple, 0, encodedData, valueSchema); + world.pushToField(tableId, keyTuple, 0, encodedData, valueSchema); endGasReport(); // Expect the data to be written assertEq(AddressArray.get(world, tableId, key), dataToPush); // Delete the data - world.deleteRecord(namespace, name, keyTuple, valueSchema); + world.deleteRecord(tableId, keyTuple, valueSchema); // Push data to the table via direct access world.pushToField(tableId, keyTuple, 0, encodedData, valueSchema); @@ -499,47 +465,37 @@ contract WorldTest is Test, GasReporter { // Expect the data to be written assertEq(AddressArray.get(world, tableId, key), dataToPush); - // Expect an error when trying to write from an address that doesn't have access (via namespace/name) - _expectAccessDenied(address(0x01), namespace, name); - world.pushToField(namespace, name, keyTuple, 0, encodedData, valueSchema); - - // Expect an error when trying to write from an address that doesn't have access (via tableId) + // Expect an error when trying to write from an address that doesn't have access _expectAccessDenied(address(0x01), namespace, name); world.pushToField(tableId, keyTuple, 0, encodedData, valueSchema); // Expect the World to have access vm.prank(address(world)); - world.pushToField(namespace, name, keyTuple, 0, encodedData, valueSchema); + world.pushToField(tableId, keyTuple, 0, encodedData, valueSchema); } function testDeleteRecord() public { bytes16 namespace = "testDeleteRecord"; bytes16 name = "testTable"; + bytes32 tableId = ResourceSelector.from(namespace, name); Schema valueSchema = Bool.getValueSchema(); // Register a new table - bytes32 tableId = world.registerTable( - namespace, - name, - defaultKeySchema, - valueSchema, - new string[](1), - new string[](1) - ); + world.registerTable(tableId, defaultKeySchema, valueSchema, new string[](1), new string[](1)); // Write data to the table via the namespace and expect it to be written - world.setRecord(namespace, name, singletonKey, abi.encodePacked(true), valueSchema); + world.setRecord(tableId, singletonKey, abi.encodePacked(true), valueSchema); assertTrue(Bool.get(world, tableId)); startGasReport("Delete record"); - world.deleteRecord(namespace, name, singletonKey, valueSchema); + world.deleteRecord(tableId, singletonKey, valueSchema); endGasReport(); // expect it to be deleted assertFalse(Bool.get(world, tableId)); // Write data to the table via the namespace and expect it to be written - world.setRecord("testDeleteRecord", "testTable", singletonKey, abi.encodePacked(true), valueSchema); + world.setRecord(tableId, singletonKey, abi.encodePacked(true), valueSchema); assertTrue(Bool.get(world, tableId)); // Delete the record via the tableId and expect it to be deleted @@ -547,41 +503,33 @@ contract WorldTest is Test, GasReporter { assertFalse(Bool.get(world, tableId)); // Write data to the table via the namespace and expect it to be written - world.setRecord("testDeleteRecord", "testTable", singletonKey, abi.encodePacked(true), valueSchema); + world.setRecord(tableId, singletonKey, abi.encodePacked(true), valueSchema); assertTrue(Bool.get(world, tableId)); - // Expect an error when trying to delete from an address that doesn't have access when calling via the namespace - _expectAccessDenied(address(0x01), "testDeleteRecord", "testTable"); - world.deleteRecord("testDeleteRecord", "testTable", singletonKey, valueSchema); - - // Expect an error when trying to delete from an address that doesn't have access when calling via the tableId + // Expect an error when trying to delete from an address that doesn't have access _expectAccessDenied(address(0x02), "testDeleteRecord", "testTable"); world.deleteRecord(tableId, singletonKey, valueSchema); // Expect the World to have access vm.prank(address(world)); - world.deleteRecord("testDeleteRecord", "testTable", singletonKey, valueSchema); + world.deleteRecord(tableId, singletonKey, valueSchema); } function testCall() public { // Register a new system WorldTestSystem system = new WorldTestSystem(); - world.registerSystem("namespace", "testSystem", system, false); + bytes32 resourceSelector = ResourceSelector.from("namespace", "testSystem"); + world.registerSystem(resourceSelector, system, false); // Call a system function without arguments via the World - bytes memory result = world.call( - "namespace", - "testSystem", - abi.encodeWithSelector(WorldTestSystem.msgSender.selector) - ); + bytes memory result = world.call(resourceSelector, abi.encodeWithSelector(WorldTestSystem.msgSender.selector)); // Expect the system to have received the caller's address assertEq(address(uint160(uint256(bytes32(result)))), address(this)); // Call a system function with arguments via the World result = world.call( - "namespace", - "testSystem", + resourceSelector, abi.encodeWithSelector(WorldTestSystem.echo.selector, bytes32(uint256(0x123))) ); @@ -597,23 +545,24 @@ contract WorldTest is Test, GasReporter { // Expect an error when trying to call a private system from an address that doesn't have access _expectAccessDenied(address(0x01), "namespace", "testSystem"); - world.call("namespace", "testSystem", abi.encodeWithSelector(WorldTestSystem.msgSender.selector)); + world.call(resourceSelector, abi.encodeWithSelector(WorldTestSystem.msgSender.selector)); // Expect the World to have access vm.prank(address(world)); - world.call("namespace", "testSystem", abi.encodeWithSelector(WorldTestSystem.msgSender.selector)); + world.call(resourceSelector, abi.encodeWithSelector(WorldTestSystem.msgSender.selector)); // Expect errors from the system to be forwarded vm.expectRevert(abi.encodeWithSelector(WorldTestSystem.WorldTestSystemError.selector, "test error")); - world.call("namespace", "testSystem", abi.encodeWithSelector(WorldTestSystem.err.selector, "test error")); + world.call(resourceSelector, abi.encodeWithSelector(WorldTestSystem.err.selector, "test error")); // Register another system in the same namespace WorldTestSystem subSystem = new WorldTestSystem(); - world.registerSystem("namespace", "testSubSystem", subSystem, false); + bytes32 subsystemResourceSelector = ResourceSelector.from("namespace", "testSubSystem"); + world.registerSystem(subsystemResourceSelector, subSystem, false); // Call the subsystem via the World (with access to the base route) returnedAddress = abi.decode( - world.call("namespace", "testSubSystem", abi.encodeWithSelector(WorldTestSystem.msgSender.selector)), + world.call(subsystemResourceSelector, abi.encodeWithSelector(WorldTestSystem.msgSender.selector)), (address) ); assertEq(returnedAddress, address(this)); @@ -621,8 +570,7 @@ contract WorldTest is Test, GasReporter { // Call the subsystem via delegatecall from the system // (Note: just for testing purposes, in reality systems can call subsystems directly instead of via two indirections like here) bytes memory nestedReturndata = world.call( - "namespace", - "testSystem", + resourceSelector, abi.encodeWithSelector( WorldTestSystem.delegateCallSubSystem.selector, // Function in system address(subSystem), // Address of subsystem @@ -636,20 +584,14 @@ contract WorldTest is Test, GasReporter { function testRegisterTableHook() public { Schema valueSchema = Bool.getValueSchema(); + bytes32 tableId = ResourceSelector.from("", "testTable"); // Register a new table - bytes32 tableId = world.registerTable( - "", - "testTable", - defaultKeySchema, - valueSchema, - new string[](1), - new string[](1) - ); + world.registerTable(tableId, defaultKeySchema, valueSchema, new string[](1), new string[](1)); // Register a new hook IStoreHook tableHook = new WorldTestTableHook(); - world.registerTableHook("", "testTable", tableHook); + world.registerStoreHook(tableId, tableHook); // Prepare data to write to the table bytes memory value = abi.encodePacked(true); @@ -664,13 +606,15 @@ contract WorldTest is Test, GasReporter { } function testRegisterSystemHook() public { + bytes32 tableId = ResourceSelector.from("namespace", "testTable"); + // Register a new system WorldTestSystem system = new WorldTestSystem(); - world.registerSystem("namespace", "testSystem", system, false); + world.registerSystem(tableId, system, false); // Register a new hook ISystemHook systemHook = new WorldTestSystemHook(); - world.registerSystemHook("namespace", "testSystem", systemHook); + world.registerSystemHook(tableId, systemHook); bytes memory funcSelectorAndArgs = abi.encodeWithSelector(bytes4(keccak256("fallbackselector"))); @@ -685,28 +629,22 @@ contract WorldTest is Test, GasReporter { emit SystemHookCalled(abi.encode("after", address(this), address(system), funcSelectorAndArgs)); // Call a system fallback function without arguments via the World - world.call("namespace", "testSystem", funcSelectorAndArgs); + world.call(tableId, funcSelectorAndArgs); } function testWriteRootSystem() public { + bytes32 tableId = ResourceSelector.from("namespace", "testTable"); // Register a new table - bytes32 tableId = world.registerTable( - "namespace", - "testTable", - defaultKeySchema, - Bool.getValueSchema(), - new string[](1), - new string[](1) - ); + world.registerTable(tableId, defaultKeySchema, Bool.getValueSchema(), new string[](1), new string[](1)); // Register a new system + bytes32 rootSystemId = ResourceSelector.from("", "testSystem"); WorldTestSystem system = new WorldTestSystem(); - world.registerSystem("", "testSystem", system, false); + world.registerSystem(rootSystemId, system, false); // Call a system function that writes data to the World world.call( - "", - "testSystem", + rootSystemId, abi.encodeWithSelector(WorldTestSystem.writeData.selector, bytes16("namespace"), bytes16("testTable"), true) ); @@ -715,24 +653,17 @@ contract WorldTest is Test, GasReporter { } function testWriteAutonomousSystem() public { + bytes32 tableId = ResourceSelector.from("namespace", "testTable"); // Register a new table - bytes32 tableId = world.registerTable( - "namespace", - "testTable", - defaultKeySchema, - Bool.getValueSchema(), - new string[](1), - new string[](1) - ); + world.registerTable(tableId, defaultKeySchema, Bool.getValueSchema(), new string[](1), new string[](1)); // Register a new system WorldTestSystem system = new WorldTestSystem(); - world.registerSystem("namespace", "testSystem", system, false); + world.registerSystem(tableId, system, false); // Call a system function that writes data to the World world.call( - "namespace", - "testSystem", + tableId, abi.encodeWithSelector(WorldTestSystem.writeData.selector, bytes16("namespace"), bytes16("testTable"), true) ); @@ -741,37 +672,40 @@ contract WorldTest is Test, GasReporter { } function testDelegatecallRootSystem() public { + bytes32 resourceSelector = ResourceSelector.from("", "testSystem"); // Register a new root system WorldTestSystem system = new WorldTestSystem(); - world.registerSystem("", "testSystem", system, false); + world.registerSystem(resourceSelector, system, false); // Call the root sysyem vm.expectEmit(true, true, true, true); emit WorldTestSystemLog("delegatecall"); - world.call("", "testSystem", abi.encodeWithSelector(WorldTestSystem.emitCallType.selector)); + world.call(resourceSelector, abi.encodeWithSelector(WorldTestSystem.emitCallType.selector)); } function testCallAutonomousSystem() public { + bytes32 resourceSelector = ResourceSelector.from("namespace", "testSystem"); // Register a new non-root system WorldTestSystem system = new WorldTestSystem(); - world.registerSystem("namespace", "testSystem", system, false); + world.registerSystem(resourceSelector, system, false); // Call the sysyem vm.expectEmit(true, true, true, true); emit WorldTestSystemLog("call"); - world.call("namespace", "testSystem", abi.encodeWithSelector(WorldTestSystem.emitCallType.selector)); + world.call(resourceSelector, abi.encodeWithSelector(WorldTestSystem.emitCallType.selector)); } function testRegisterFunctionSelector() public { bytes16 namespace = "testNamespace"; bytes16 name = "testSystem"; + bytes32 resourceSelector = ResourceSelector.from(namespace, name); // Register a new system WorldTestSystem system = new WorldTestSystem(); - world.registerSystem(namespace, name, system, true); + world.registerSystem(resourceSelector, system, true); startGasReport("Register a function selector"); - bytes4 functionSelector = world.registerFunctionSelector(namespace, name, "msgSender", "()"); + bytes4 functionSelector = world.registerFunctionSelector(resourceSelector, "msgSender", "()"); endGasReport(); string memory expectedWorldFunctionSignature = "testNamespace_testSystem_msgSender()"; @@ -785,7 +719,7 @@ contract WorldTest is Test, GasReporter { assertEq(abi.decode(data, (address)), address(this), "wrong address returned"); // Register a function selector to the error function - functionSelector = world.registerFunctionSelector(namespace, name, "err", "(string)"); + functionSelector = world.registerFunctionSelector(resourceSelector, "err", "(string)"); // Expect errors to be passed through vm.expectRevert(abi.encodeWithSelector(WorldTestSystem.WorldTestSystemError.selector, "test error")); @@ -795,24 +729,25 @@ contract WorldTest is Test, GasReporter { function testRegisterRootFunctionSelector() public { bytes16 namespace = "testNamespace"; bytes16 name = "testSystem"; + bytes32 resourceSelector = ResourceSelector.from(namespace, name); // Register a new system WorldTestSystem system = new WorldTestSystem(); - world.registerSystem(namespace, name, system, true); + world.registerSystem(resourceSelector, system, true); bytes4 worldFunc = bytes4(abi.encodeWithSignature("testSelector()")); bytes4 sysFunc = WorldTestSystem.msgSender.selector; // Expect an error when trying to register a root function selector from an account without access _expectAccessDenied(address(0x01), "", ""); - world.registerRootFunctionSelector(namespace, name, worldFunc, sysFunc); + world.registerRootFunctionSelector(resourceSelector, worldFunc, sysFunc); // Expect the World to be able to register a root function selector vm.prank(address(world)); - world.registerRootFunctionSelector(namespace, name, "smth", "smth"); + world.registerRootFunctionSelector(resourceSelector, "smth", "smth"); startGasReport("Register a root function selector"); - bytes4 functionSelector = world.registerRootFunctionSelector(namespace, name, worldFunc, sysFunc); + bytes4 functionSelector = world.registerRootFunctionSelector(resourceSelector, worldFunc, sysFunc); endGasReport(); assertEq(functionSelector, worldFunc, "wrong function selector returned"); @@ -825,8 +760,7 @@ contract WorldTest is Test, GasReporter { // Register a function selector to the error function functionSelector = world.registerRootFunctionSelector( - namespace, - name, + resourceSelector, WorldTestSystem.err.selector, WorldTestSystem.err.selector ); @@ -839,13 +773,14 @@ contract WorldTest is Test, GasReporter { function testRegisterFallbackSystem() public { bytes16 namespace = "testNamespace"; bytes16 name = "testSystem"; + bytes32 resourceSelector = ResourceSelector.from(namespace, name); // Register a new system WorldTestSystem system = new WorldTestSystem(); - world.registerSystem(namespace, name, system, true); + world.registerSystem(resourceSelector, system, true); startGasReport("Register a fallback system"); - bytes4 funcSelector1 = world.registerFunctionSelector(namespace, name, "", ""); + bytes4 funcSelector1 = world.registerFunctionSelector(resourceSelector, "", ""); endGasReport(); // Call the system's fallback function @@ -857,7 +792,7 @@ contract WorldTest is Test, GasReporter { bytes4 worldFunc = bytes4(abi.encodeWithSignature("testSelector()")); startGasReport("Register a root fallback system"); - bytes4 funcSelector2 = world.registerRootFunctionSelector(namespace, name, worldFunc, 0); + bytes4 funcSelector2 = world.registerRootFunctionSelector(resourceSelector, worldFunc, 0); endGasReport(); assertEq(funcSelector2, worldFunc, "wrong function selector returned"); @@ -895,10 +830,11 @@ contract WorldTest is Test, GasReporter { WorldTestSystem system = new WorldTestSystem(); bytes16 namespace = "noroot"; bytes16 name = "testSystem"; - world.registerSystem(namespace, name, system, true); + bytes32 resourceSelector = ResourceSelector.from(namespace, name); + + world.registerSystem(resourceSelector, system, true); world.registerRootFunctionSelector( - namespace, - name, + resourceSelector, WorldTestSystem.receiveEther.selector, WorldTestSystem.receiveEther.selector ); @@ -927,10 +863,10 @@ contract WorldTest is Test, GasReporter { WorldTestSystem system = new WorldTestSystem(); bytes16 namespace = "noroot"; bytes16 name = "testSystem"; - world.registerSystem(namespace, name, system, true); + bytes32 resourceSelector = ResourceSelector.from(namespace, name); + world.registerSystem(resourceSelector, system, true); world.registerRootFunctionSelector( - namespace, - name, + resourceSelector, WorldTestSystem.msgSender.selector, WorldTestSystem.msgSender.selector ); @@ -959,10 +895,10 @@ contract WorldTest is Test, GasReporter { WorldTestSystem system = new WorldTestSystem(); bytes16 namespace = "noroot"; bytes16 name = "testSystem"; - world.registerSystem(namespace, name, system, true); + bytes32 resourceSelector = ResourceSelector.from(namespace, name); + world.registerSystem(resourceSelector, system, true); world.registerRootFunctionSelector( - namespace, - name, + resourceSelector, bytes4(abi.encodeWithSignature("systemFallback()")), bytes4("") ); @@ -989,10 +925,10 @@ contract WorldTest is Test, GasReporter { PayableFallbackSystem system = new PayableFallbackSystem(); bytes16 namespace = "noroot"; bytes16 name = "testSystem"; - world.registerSystem(namespace, name, system, true); + bytes32 resourceSelector = ResourceSelector.from(namespace, name); + world.registerSystem(resourceSelector, system, true); world.registerRootFunctionSelector( - namespace, - name, + resourceSelector, bytes4(abi.encodeWithSignature("systemFallback()")), bytes4("") ); @@ -1019,10 +955,10 @@ contract WorldTest is Test, GasReporter { WorldTestSystem system = new WorldTestSystem(); bytes16 namespace = ""; bytes16 name = "testSystem"; - world.registerSystem(namespace, name, system, true); + bytes32 resourceSelector = ResourceSelector.from(namespace, name); + world.registerSystem(resourceSelector, system, true); world.registerRootFunctionSelector( - namespace, - name, + resourceSelector, WorldTestSystem.receiveEther.selector, WorldTestSystem.receiveEther.selector ); diff --git a/packages/world/test/WorldDynamicUpdate.t.sol b/packages/world/test/WorldDynamicUpdate.t.sol index 809d704923..62f9a71016 100644 --- a/packages/world/test/WorldDynamicUpdate.t.sol +++ b/packages/world/test/WorldDynamicUpdate.t.sol @@ -38,6 +38,8 @@ contract UpdateInFieldTest is Test, GasReporter { bytes32[] internal keyTuple; bytes32[] internal singletonKey; + bytes16 namespace; + bytes16 name; bytes32 internal tableId; address[] internal initData; bytes internal encodedData; @@ -54,11 +56,12 @@ contract UpdateInFieldTest is Test, GasReporter { // Initialize the data in setUp so that slots aren't warm in tests (to test cold update) - bytes16 namespace = "DynamicUpdTest"; - bytes16 name = "testTable"; + namespace = "DynamicUpdTest"; + name = "testTable"; + tableId = ResourceSelector.from(namespace, name); // Register a new table - tableId = world.registerTable(namespace, name, defaultKeySchema, valueSchema, new string[](1), new string[](1)); + world.registerTable(tableId, defaultKeySchema, valueSchema, new string[](1), new string[](1)); // Create data initData = new address[](3); @@ -67,24 +70,16 @@ contract UpdateInFieldTest is Test, GasReporter { initData[2] = address(bytes20(keccak256("another address"))); encodedData = EncodeArray.encode(initData); - world.setField(namespace, name, keyTuple, 0, encodedData, valueSchema); + world.setField(tableId, keyTuple, 0, encodedData, valueSchema); } // Expect an error when trying to write from an address that doesn't have access - function _expectAccessDenied(address caller, bytes16 namespace, bytes16 name) internal { - vm.prank(caller); - vm.expectRevert( - abi.encodeWithSelector( - IWorldErrors.AccessDenied.selector, - ResourceSelector.from(namespace, name).toString(), - caller - ) - ); + function _expectAccessDenied(address _caller, bytes32 _tableId) internal { + vm.prank(_caller); + vm.expectRevert(abi.encodeWithSelector(IWorldErrors.AccessDenied.selector, _tableId.toString(), _caller)); } function testPopFromField() public { - bytes16 namespace = "DynamicUpdTest"; - bytes16 name = "testTable"; Schema valueSchema = AddressArray.getValueSchema(); // Expect the data to be written @@ -94,7 +89,7 @@ contract UpdateInFieldTest is Test, GasReporter { uint256 byteLengthToPop = 20; startGasReport("pop 1 address (cold)"); - world.popFromField(namespace, name, keyTuple, 0, byteLengthToPop, valueSchema); + world.popFromField(tableId, keyTuple, 0, byteLengthToPop, valueSchema); endGasReport(); // Expect the data to be updated @@ -108,7 +103,7 @@ contract UpdateInFieldTest is Test, GasReporter { byteLengthToPop = 20; startGasReport("pop 1 address (warm)"); - world.popFromField(namespace, name, keyTuple, 0, byteLengthToPop, valueSchema); + world.popFromField(tableId, keyTuple, 0, byteLengthToPop, valueSchema); endGasReport(); // Expect the data to be updated @@ -119,7 +114,7 @@ contract UpdateInFieldTest is Test, GasReporter { } // Reset data - world.setField(namespace, name, keyTuple, 0, encodedData, valueSchema); + world.setField(tableId, keyTuple, 0, encodedData, valueSchema); // Pop 2 items via direct access byteLengthToPop = 20 * 2; world.popFromField(tableId, keyTuple, 0, byteLengthToPop, valueSchema); @@ -131,21 +126,19 @@ contract UpdateInFieldTest is Test, GasReporter { } // Expect an error when trying to write from an address that doesn't have access (via namespace/name) - _expectAccessDenied(address(0x01), namespace, name); - world.popFromField(namespace, name, keyTuple, 0, 20, valueSchema); + _expectAccessDenied(address(0x01), tableId); + world.popFromField(tableId, keyTuple, 0, 20, valueSchema); // Expect an error when trying to write from an address that doesn't have access (via tableId) - _expectAccessDenied(address(0x01), namespace, name); + _expectAccessDenied(address(0x01), tableId); world.popFromField(tableId, keyTuple, 0, 20, valueSchema); // Expect the World to have access vm.prank(address(world)); - world.popFromField(namespace, name, keyTuple, 0, 20, valueSchema); + world.popFromField(tableId, keyTuple, 0, 20, valueSchema); } function testUpdateInField() public { - bytes16 namespace = "DynamicUpdTest"; - bytes16 name = "testTable"; Schema valueSchema = AddressArray.getValueSchema(); // Expect the data to be written @@ -156,11 +149,11 @@ contract UpdateInFieldTest is Test, GasReporter { dataForUpdate[0] = address(bytes20(keccak256("address for update"))); startGasReport("updateInField 1 item (cold)"); - world.updateInField(namespace, name, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), valueSchema); + world.updateInField(tableId, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), valueSchema); endGasReport(); startGasReport("updateInField 1 item (warm)"); - world.updateInField(namespace, name, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), valueSchema); + world.updateInField(tableId, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), valueSchema); endGasReport(); // Expect the data to be updated @@ -175,15 +168,15 @@ contract UpdateInFieldTest is Test, GasReporter { assertEq(AddressArray.get(world, tableId, key), initData); // Expect an error when trying to write from an address that doesn't have access (via namespace/name) - _expectAccessDenied(address(0x01), namespace, name); - world.updateInField(namespace, name, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), valueSchema); + _expectAccessDenied(address(0x01), tableId); + world.updateInField(tableId, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), valueSchema); // Expect an error when trying to write from an address that doesn't have access (via tableId) - _expectAccessDenied(address(0x01), namespace, name); + _expectAccessDenied(address(0x01), tableId); world.updateInField(tableId, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), valueSchema); // Expect the World to have access vm.prank(address(world)); - world.updateInField(namespace, name, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), valueSchema); + world.updateInField(tableId, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), valueSchema); } } diff --git a/packages/world/test/query.t.sol b/packages/world/test/query.t.sol index 6a40efae56..7f061dcd1f 100644 --- a/packages/world/test/query.t.sol +++ b/packages/world/test/query.t.sol @@ -62,9 +62,9 @@ contract QueryTest is Test, GasReporter { function _installKeysInTableModule() internal { // Register source table - table1 = world.registerTable(namespace, name1, tableKeySchema, tableValueSchema, new string[](1), new string[](1)); - table2 = world.registerTable(namespace, name2, tableKeySchema, tableValueSchema, new string[](1), new string[](1)); - table3 = world.registerTable(namespace, name3, tableKeySchema, tableValueSchema, new string[](1), new string[](1)); + world.registerTable(table1, tableKeySchema, tableValueSchema, new string[](1), new string[](1)); + world.registerTable(table2, tableKeySchema, tableValueSchema, new string[](1), new string[](1)); + world.registerTable(table3, tableKeySchema, tableValueSchema, new string[](1), new string[](1)); // Install the index module // TODO: add support for installing this via installModule @@ -86,9 +86,9 @@ contract QueryTest is Test, GasReporter { function testHasQuery() public { _installKeysInTableModule(); - world.setRecord(namespace, name1, key1, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name1, key2, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name2, key1, abi.encode(0), tableValueSchema); + world.setRecord(table1, key1, abi.encode(1), tableValueSchema); + world.setRecord(table1, key2, abi.encode(1), tableValueSchema); + world.setRecord(table2, key1, abi.encode(0), tableValueSchema); // Query should return all keys in table1 QueryFragment[] memory fragments = new QueryFragment[](1); @@ -107,9 +107,9 @@ contract QueryTest is Test, GasReporter { _installKeysInTableModule(); _installKeysWithValueModule(); - world.setRecord(namespace, name1, key1, abi.encode(2), tableValueSchema); - world.setRecord(namespace, name1, key2, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name1, key3, abi.encode(1), tableValueSchema); + world.setRecord(table1, key1, abi.encode(2), tableValueSchema); + world.setRecord(table1, key2, abi.encode(1), tableValueSchema); + world.setRecord(table1, key3, abi.encode(1), tableValueSchema); // Query should return all keys in table1 with value 1 QueryFragment[] memory fragments = new QueryFragment[](1); fragments[0] = QueryFragment(QueryType.HasValue, table1, abi.encode(1)); @@ -125,12 +125,12 @@ contract QueryTest is Test, GasReporter { function testCombinedHasQuery() public { _installKeysInTableModule(); - world.setRecord(namespace, name1, key1, abi.encode(2), tableValueSchema); - world.setRecord(namespace, name1, key2, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name1, key3, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name2, key2, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name2, key3, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name3, key1, abi.encode(1), tableValueSchema); + world.setRecord(table1, key1, abi.encode(2), tableValueSchema); + world.setRecord(table1, key2, abi.encode(1), tableValueSchema); + world.setRecord(table1, key3, abi.encode(1), tableValueSchema); + world.setRecord(table2, key2, abi.encode(1), tableValueSchema); + world.setRecord(table2, key3, abi.encode(1), tableValueSchema); + world.setRecord(table3, key1, abi.encode(1), tableValueSchema); // Query should return all entities that have table1 and table2 QueryFragment[] memory fragments = new QueryFragment[](2); @@ -149,12 +149,12 @@ contract QueryTest is Test, GasReporter { _installKeysInTableModule(); _installKeysWithValueModule(); - world.setRecord(namespace, name1, key1, abi.encode(2), tableValueSchema); - world.setRecord(namespace, name1, key2, abi.encode(2), tableValueSchema); - world.setRecord(namespace, name1, key3, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name2, key2, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name2, key3, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name3, key1, abi.encode(1), tableValueSchema); + world.setRecord(table1, key1, abi.encode(2), tableValueSchema); + world.setRecord(table1, key2, abi.encode(2), tableValueSchema); + world.setRecord(table1, key3, abi.encode(1), tableValueSchema); + world.setRecord(table2, key2, abi.encode(1), tableValueSchema); + world.setRecord(table2, key3, abi.encode(1), tableValueSchema); + world.setRecord(table3, key1, abi.encode(1), tableValueSchema); // Query should return all entities that have table1 and table2 QueryFragment[] memory fragments = new QueryFragment[](2); @@ -172,13 +172,13 @@ contract QueryTest is Test, GasReporter { _installKeysInTableModule(); _installKeysWithValueModule(); - world.setRecord(namespace, name1, key1, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name1, key2, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name1, key3, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name2, key1, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name2, key2, abi.encode(2), tableValueSchema); - world.setRecord(namespace, name2, key3, abi.encode(2), tableValueSchema); - world.setRecord(namespace, name2, key4, abi.encode(2), tableValueSchema); + world.setRecord(table1, key1, abi.encode(1), tableValueSchema); + world.setRecord(table1, key2, abi.encode(1), tableValueSchema); + world.setRecord(table1, key3, abi.encode(1), tableValueSchema); + world.setRecord(table2, key1, abi.encode(1), tableValueSchema); + world.setRecord(table2, key2, abi.encode(2), tableValueSchema); + world.setRecord(table2, key3, abi.encode(2), tableValueSchema); + world.setRecord(table2, key4, abi.encode(2), tableValueSchema); // Query should return all entities that have table1 and table2 QueryFragment[] memory fragments = new QueryFragment[](2); @@ -196,13 +196,13 @@ contract QueryTest is Test, GasReporter { function testCombinedHasNotQuery() public { _installKeysInTableModule(); - world.setRecord(namespace, name1, key1, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name1, key2, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name1, key3, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name2, key1, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name2, key2, abi.encode(2), tableValueSchema); - world.setRecord(namespace, name2, key3, abi.encode(2), tableValueSchema); - world.setRecord(namespace, name2, key4, abi.encode(2), tableValueSchema); + world.setRecord(table1, key1, abi.encode(1), tableValueSchema); + world.setRecord(table1, key2, abi.encode(1), tableValueSchema); + world.setRecord(table1, key3, abi.encode(1), tableValueSchema); + world.setRecord(table2, key1, abi.encode(1), tableValueSchema); + world.setRecord(table2, key2, abi.encode(2), tableValueSchema); + world.setRecord(table2, key3, abi.encode(2), tableValueSchema); + world.setRecord(table2, key4, abi.encode(2), tableValueSchema); // Query should return all entities that have table1 and table2 QueryFragment[] memory fragments = new QueryFragment[](2); @@ -220,13 +220,13 @@ contract QueryTest is Test, GasReporter { _installKeysInTableModule(); _installKeysWithValueModule(); - world.setRecord(namespace, name1, key1, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name1, key2, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name1, key3, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name2, key1, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name2, key2, abi.encode(2), tableValueSchema); - world.setRecord(namespace, name2, key3, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name2, key4, abi.encode(1), tableValueSchema); + world.setRecord(table1, key1, abi.encode(1), tableValueSchema); + world.setRecord(table1, key2, abi.encode(1), tableValueSchema); + world.setRecord(table1, key3, abi.encode(1), tableValueSchema); + world.setRecord(table2, key1, abi.encode(1), tableValueSchema); + world.setRecord(table2, key2, abi.encode(2), tableValueSchema); + world.setRecord(table2, key3, abi.encode(1), tableValueSchema); + world.setRecord(table2, key4, abi.encode(1), tableValueSchema); // Query should return all entities that have table1 and table2 QueryFragment[] memory fragments = new QueryFragment[](2); @@ -244,16 +244,16 @@ contract QueryTest is Test, GasReporter { _installKeysInTableModule(); _installKeysWithValueModule(); - world.setRecord(namespace, name1, key1, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name1, key2, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name1, key3, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name2, key1, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name2, key2, abi.encode(2), tableValueSchema); - world.setRecord(namespace, name2, key3, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name2, key4, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name3, key2, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name3, key3, abi.encode(1), tableValueSchema); - world.setRecord(namespace, name3, key4, abi.encode(1), tableValueSchema); + world.setRecord(table1, key1, abi.encode(1), tableValueSchema); + world.setRecord(table1, key2, abi.encode(1), tableValueSchema); + world.setRecord(table1, key3, abi.encode(1), tableValueSchema); + world.setRecord(table2, key1, abi.encode(1), tableValueSchema); + world.setRecord(table2, key2, abi.encode(2), tableValueSchema); + world.setRecord(table2, key3, abi.encode(1), tableValueSchema); + world.setRecord(table2, key4, abi.encode(1), tableValueSchema); + world.setRecord(table3, key2, abi.encode(1), tableValueSchema); + world.setRecord(table3, key3, abi.encode(1), tableValueSchema); + world.setRecord(table3, key4, abi.encode(1), tableValueSchema); // Query should return all entities that have table2 and not table1 QueryFragment[] memory fragments = new QueryFragment[](3); @@ -272,9 +272,9 @@ contract QueryTest is Test, GasReporter { _installKeysInTableModule(); _installKeysWithValueModule(); - world.setRecord(namespace, name1, key1, abi.encode(4), tableValueSchema); - world.setRecord(namespace, name1, key2, abi.encode(5), tableValueSchema); - world.setRecord(namespace, name1, key3, abi.encode(6), tableValueSchema); + world.setRecord(table1, key1, abi.encode(4), tableValueSchema); + world.setRecord(table1, key2, abi.encode(5), tableValueSchema); + world.setRecord(table1, key3, abi.encode(6), tableValueSchema); // Query should return all entities with table1 except value 6 QueryFragment[] memory fragments = new QueryFragment[](2); @@ -295,9 +295,9 @@ contract QueryTest is Test, GasReporter { for (uint256 i; i < 100; i++) { bytes32[] memory key = new bytes32[](1); key[0] = bytes32(i); - world.setRecord(namespace, name1, key, abi.encode(1), tableValueSchema); + world.setRecord(table1, key, abi.encode(1), tableValueSchema); } - world.setRecord(namespace, name2, key1, abi.encode(0), tableValueSchema); + world.setRecord(table2, key1, abi.encode(0), tableValueSchema); // Query should return all keys in table1 QueryFragment[] memory fragments = new QueryFragment[](1); @@ -316,9 +316,9 @@ contract QueryTest is Test, GasReporter { for (uint256 i; i < 1000; i++) { bytes32[] memory key = new bytes32[](1); key[0] = bytes32(i); - world.setRecord(namespace, name1, key, abi.encode(1), tableValueSchema); + world.setRecord(table1, key, abi.encode(1), tableValueSchema); } - world.setRecord(namespace, name2, key1, abi.encode(0), tableValueSchema); + world.setRecord(table2, key1, abi.encode(0), tableValueSchema); // Query should return all keys in table1 QueryFragment[] memory fragments = new QueryFragment[](1);