Skip to content

Commit

Permalink
more refactor to change function interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Aug 16, 2023
1 parent 0dc4a2b commit 127bf5d
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 340 deletions.
4 changes: 2 additions & 2 deletions packages/world/src/modules/keysintable/KeysInTableModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions packages/world/test/AccessControl.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
80 changes: 30 additions & 50 deletions packages/world/test/KeysInTableModule.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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
Expand All @@ -173,22 +159,16 @@ 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);
keyTuple[0] = keyB;

// 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
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
27 changes: 10 additions & 17 deletions packages/world/test/KeysWithValueModule.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)));
Expand All @@ -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)));
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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));
Expand All @@ -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));
Expand Down
Loading

0 comments on commit 127bf5d

Please sign in to comment.