-
Notifications
You must be signed in to change notification settings - Fork 174
/
KeysInTableModule.t.sol
446 lines (357 loc) · 16 KB
/
KeysInTableModule.t.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;
import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { FieldLayout, FieldLayoutLib } from "@latticexyz/store/src/FieldLayout.sol";
import { FieldLayoutEncodeHelper } from "@latticexyz/store/test/FieldLayoutEncodeHelper.sol";
import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol";
import { PackedCounter } from "@latticexyz/store/src/PackedCounter.sol";
import { SchemaEncodeHelper } from "@latticexyz/store/test/SchemaEncodeHelper.sol";
import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol";
import { World } from "@latticexyz/world/src/World.sol";
import { IModule } from "@latticexyz/world/src/IModule.sol";
import { IModuleErrors } from "@latticexyz/world/src/IModule.sol";
import { IBaseWorld } from "@latticexyz/world/src/codegen/interfaces/IBaseWorld.sol";
import { ResourceId, WorldResourceIdLib, WorldResourceIdInstance } from "@latticexyz/world/src/WorldResourceId.sol";
import { ROOT_NAMESPACE } from "@latticexyz/world/src/constants.sol";
import { RESOURCE_TABLE } from "@latticexyz/world/src/worldResourceTypes.sol";
import { createWorld } from "@latticexyz/world/test/createWorld.sol";
import { KeysInTableModule } from "../src/modules/keysintable/KeysInTableModule.sol";
import { getKeysInTable } from "../src/modules/keysintable/getKeysInTable.sol";
import { hasKey } from "../src/modules/keysintable/hasKey.sol";
contract KeysInTableModuleTest is Test, GasReporter {
using WorldResourceIdInstance for ResourceId;
IBaseWorld private world;
KeysInTableModule private keysInTableModule = new KeysInTableModule(); // Modules can be deployed once and installed multiple times
bytes14 private namespace = ROOT_NAMESPACE;
bytes16 private name = bytes16("source");
bytes16 private singletonName = bytes16("singleton");
bytes16 private compositeName = bytes16("composite");
bytes32 private key1 = keccak256("test");
bytes32[] private keyTuple1;
bytes32 private key2 = keccak256("test2");
bytes32[] private keyTuple2;
bytes32 private key3 = keccak256("test3");
bytes32[] private keyTuple3;
FieldLayout private tableFieldLayout;
Schema private tableValueSchema;
Schema private tableKeySchema;
Schema private singletonKeySchema;
Schema private compositeKeySchema;
ResourceId private tableId = WorldResourceIdLib.encode({ typeId: RESOURCE_TABLE, namespace: namespace, name: name });
ResourceId private singletonTableId =
WorldResourceIdLib.encode({ typeId: RESOURCE_TABLE, namespace: namespace, name: singletonName });
ResourceId private compositeTableId =
WorldResourceIdLib.encode({ typeId: RESOURCE_TABLE, namespace: namespace, name: compositeName });
uint256 private val1 = 123;
uint256 private val2 = 42;
function setUp() public {
tableFieldLayout = FieldLayoutEncodeHelper.encode(32, 0);
tableValueSchema = SchemaEncodeHelper.encode(SchemaType.UINT256);
tableKeySchema = SchemaEncodeHelper.encode(SchemaType.BYTES32);
compositeKeySchema = SchemaEncodeHelper.encode(SchemaType.BYTES32, SchemaType.BYTES32, SchemaType.BYTES32);
singletonKeySchema = SchemaLib.encode(new SchemaType[](0));
world = createWorld();
keyTuple1 = new bytes32[](1);
keyTuple1[0] = key1;
keyTuple2 = new bytes32[](1);
keyTuple2[0] = key2;
keyTuple3 = new bytes32[](1);
keyTuple3[0] = key3;
}
function _installKeysInTableModule() internal {
// Register source table
world.registerTable(tableId, tableFieldLayout, tableKeySchema, tableValueSchema, new string[](1), new string[](1));
world.registerTable(
singletonTableId,
tableFieldLayout,
singletonKeySchema,
tableValueSchema,
new string[](0),
new string[](1)
);
world.registerTable(
compositeTableId,
tableFieldLayout,
compositeKeySchema,
tableValueSchema,
new string[](3),
new string[](1)
);
// Install the index module
// TODO: add support for installing this via installModule
// -> requires `callFrom` for the module to be able to register a hook in the name of the original caller
startGasReport("install keys in table module");
world.installRootModule(keysInTableModule, abi.encode(tableId));
endGasReport();
world.installRootModule(keysInTableModule, abi.encode(singletonTableId));
world.installRootModule(keysInTableModule, abi.encode(compositeTableId));
}
// This test is expected to fail because `getKeySchema()` on StoreCore reverts on singleton tables
// TODO: we need to be able to determine whether a table is singleton vs. nonexistent
function testInstallSingleton() public {
_installKeysInTableModule();
bytes32[] memory keyTuple = new bytes32[](0);
world.setRecord(singletonTableId, keyTuple, abi.encodePacked(val1), PackedCounter.wrap(bytes32(0)), new bytes(0));
// Get the list of keys in this target table
bytes32[][] memory keysInTable = getKeysInTable(world, singletonTableId);
// Assert that the list is correct
assertEq(keysInTable.length, 0);
}
function testInstallComposite() public {
_installKeysInTableModule();
bytes32[] memory keyTuple = new bytes32[](3);
keyTuple[0] = "one";
keyTuple[1] = "two";
keyTuple[2] = "three";
world.setRecord(compositeTableId, keyTuple, abi.encodePacked(val1), PackedCounter.wrap(bytes32(0)), new bytes(0));
// Get the list of keys in this target table
bytes32[][] memory keysInTable = getKeysInTable(world, compositeTableId);
// Assert that the list is correct
assertEq(keysInTable.length, 1);
assertEq(keysInTable[0].length, keyTuple.length);
for (uint256 i; i < keyTuple.length; i++) {
assertEq(keysInTable[0][i], keyTuple[i]);
}
// Assert that the key tuple is in the source table
assertTrue(hasKey(world, compositeTableId, keyTuple));
}
function testInstallGas() public {
// call fuzzed test manually to get gas report
testInstall(val1);
}
function testInstall(uint256 value) public {
_installKeysInTableModule();
// Set a value in the source table
startGasReport("set a record on a table with keysInTableModule installed");
world.setRecord(tableId, keyTuple1, abi.encodePacked(value), PackedCounter.wrap(bytes32(0)), new bytes(0));
endGasReport();
// Get the list of keys in this target table
bytes32[][] memory keysInTable = getKeysInTable(world, tableId);
// Assert that the list is correct
assertEq(keysInTable.length, 1);
assertEq(keysInTable[0][0], key1);
}
function testInstallTwice() public {
world.installRootModule(keysInTableModule, abi.encode(tableId));
vm.expectRevert(IModuleErrors.Module_AlreadyInstalled.selector);
world.installRootModule(keysInTableModule, abi.encode(tableId));
}
function testInstallMultiple(bytes32 keyA, bytes32 keyB, uint256 value1, uint256 value2) public {
_installKeysInTableModule();
bytes32[] memory keyTuple = new bytes32[](1);
keyTuple[0] = keyA;
// Set a value in the source table
startGasReport("set a record on a table with keysInTableModule installed (first)");
world.setRecord(tableId, keyTuple, abi.encodePacked(value1), PackedCounter.wrap(bytes32(0)), new bytes(0));
endGasReport();
// Get the list of keys in the first target table
bytes32[][] memory keysInTable = getKeysInTable(world, tableId);
// Assert that the list is correct
assertEq(keysInTable.length, 1);
assertEq(keysInTable[0][0], keyA);
// Install the hook on the second table
bytes16 sourceFile2 = bytes16("source2");
ResourceId sourceTableId2 = WorldResourceIdLib.encode({
typeId: RESOURCE_TABLE,
namespace: namespace,
name: sourceFile2
});
world.registerTable(
sourceTableId2,
tableFieldLayout,
tableKeySchema,
tableValueSchema,
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(sourceTableId2, keyTuple, abi.encodePacked(value2), PackedCounter.wrap(bytes32(0)), new bytes(0));
endGasReport();
// Get the list of keys in the second target table
keysInTable = getKeysInTable(world, sourceTableId2);
// Assert that the list is correct
assertEq(keysInTable.length, 1);
assertEq(keysInTable[0][0], keyB);
}
function testSetAndDeleteRecordHookGas() public {
// call fuzzed test manually to get gas report
testSetAndDeleteRecordHook(val1, val2);
}
function testSetAndDeleteRecordHook(uint256 value1, uint256 value2) public {
_installKeysInTableModule();
// Set a value in the source table
world.setRecord(tableId, keyTuple1, abi.encodePacked(value1), PackedCounter.wrap(bytes32(0)), new bytes(0));
// Get the list of keys in the target table
bytes32[][] memory keysInTable = getKeysInTable(world, tableId);
// Assert that the list is correct
assertEq(keysInTable.length, 1, "1");
assertEq(keysInTable[0][0], key1, "2");
// Set another key with the same value
world.setRecord(tableId, keyTuple2, abi.encodePacked(value1), PackedCounter.wrap(bytes32(0)), new bytes(0));
// Get the list of keys in the target table
keysInTable = getKeysInTable(world, tableId);
// Assert that the list is correct
assertEq(keysInTable.length, 2);
assertEq(keysInTable[0][0], key1, "3");
assertEq(keysInTable[1][0], key2, "4");
// Change the value of the first key
startGasReport("change a record on a table with keysInTableModule installed");
world.setRecord(tableId, keyTuple1, abi.encodePacked(value2), PackedCounter.wrap(bytes32(0)), new bytes(0));
endGasReport();
// Get the list of keys in the target table
keysInTable = getKeysInTable(world, tableId);
// Assert that the list is correct
assertEq(keysInTable.length, 2, "5");
assertEq(keysInTable[0][0], key1, "6");
assertEq(keysInTable[1][0], key2, "7");
// Delete the first key
startGasReport("delete a record on a table with keysInTableModule installed");
world.deleteRecord(tableId, keyTuple1);
endGasReport();
// Get the list of keys in the target table
keysInTable = getKeysInTable(world, tableId);
// Assert that the list is correct
assertEq(keysInTable.length, 1, "8");
assertEq(keysInTable[0][0], key2, "9");
}
function testSetAndDeleteRecordHookCompositeGas() public {
// call fuzzed test manually to get gas report
testSetAndDeleteRecordHookComposite(val1, val2);
}
function testSetAndDeleteRecordHookComposite(uint256 value1, uint256 value2) public {
_installKeysInTableModule();
bytes32[] memory keyTupleA = new bytes32[](3);
keyTupleA[0] = "one";
keyTupleA[1] = "two";
keyTupleA[2] = "three";
bytes32[] memory keyTupleB = new bytes32[](3);
keyTupleB[0] = "alpha";
keyTupleB[1] = "beta";
keyTupleB[2] = "charlie";
// Set a value in the source table
world.setRecord(
compositeTableId,
keyTupleA,
abi.encodePacked(value1),
PackedCounter.wrap(bytes32(0)),
new bytes(0)
);
// Get the list of keys in the target table
bytes32[][] memory keysInTable = getKeysInTable(world, compositeTableId);
// Assert that the list is correct
assertEq(keysInTable.length, 1);
for (uint256 i; i < keyTupleA.length; i++) {
assertEq(keysInTable[0][i], keyTupleA[i]);
}
// Set another key with the same value
world.setRecord(
compositeTableId,
keyTupleB,
abi.encodePacked(value1),
PackedCounter.wrap(bytes32(0)),
new bytes(0)
);
// Get the list of keys in the target table
keysInTable = getKeysInTable(world, compositeTableId);
// Assert that the list is correct
assertEq(keysInTable.length, 2);
for (uint256 i; i < keyTupleA.length; i++) {
assertEq(keysInTable[0][i], keyTupleA[i]);
}
for (uint256 i; i < keyTupleA.length; i++) {
assertEq(keysInTable[1][i], keyTupleB[i]);
}
// Change the value of the first key
startGasReport("change a composite record on a table with keysInTableModule installed");
world.setRecord(
compositeTableId,
keyTupleA,
abi.encodePacked(value2),
PackedCounter.wrap(bytes32(0)),
new bytes(0)
);
endGasReport();
// Get the list of keys in the target table
keysInTable = getKeysInTable(world, compositeTableId);
// Assert that the list is correct
assertEq(keysInTable.length, 2);
for (uint256 i; i < keyTupleA.length; i++) {
assertEq(keysInTable[0][i], keyTupleA[i]);
}
for (uint256 i; i < keyTupleA.length; i++) {
assertEq(keysInTable[1][i], keyTupleB[i]);
}
// Delete the first key
startGasReport("delete a composite record on a table with keysInTableModule installed");
world.deleteRecord(compositeTableId, keyTupleA);
endGasReport();
// Get the list of keys in the target table
keysInTable = getKeysInTable(world, compositeTableId);
// Assert that the list is correct
assertEq(keysInTable.length, 1);
// for (uint256 i; i < keyTupleB.length; i++) {
// assertEq(keysInTable[0][i], keyTupleB[i]);
// }
}
function testSetField(uint256 value1, uint256 value2) public {
_installKeysInTableModule();
// Set a value in the source table
startGasReport("set a field on a table with keysInTableModule installed");
world.setField(tableId, keyTuple1, 0, abi.encodePacked(value1), tableFieldLayout);
endGasReport();
// Get the list of keys in the target table
bytes32[][] memory keysInTable = getKeysInTable(world, tableId);
// Assert that the list is correct
assertEq(keysInTable.length, 1);
assertEq(keysInTable[0][0], key1);
// Change the value using setField
startGasReport("change a field on a table with keysInTableModule installed");
world.setField(tableId, keyTuple1, 0, abi.encodePacked(value2), tableFieldLayout);
endGasReport();
// Get the list of keys in the target table
keysInTable = getKeysInTable(world, tableId);
// Assert that the list is correct
assertEq(keysInTable.length, 1);
assertEq(keysInTable[0][0], key1);
}
function testGetKeysInTable(uint256 value1, uint256 value2) public {
_installKeysInTableModule();
// Set a value in the source table
world.setRecord(tableId, keyTuple1, abi.encodePacked(value1), PackedCounter.wrap(bytes32(0)), new bytes(0));
startGasReport("Get list of keys in a given table");
bytes32[][] memory keysInTable = getKeysInTable(world, tableId);
endGasReport();
// Assert that the list is correct
assertEq(keysInTable.length, 1);
assertEq(keysInTable[0][0], key1);
// Set another key with a different value
world.setRecord(tableId, keyTuple2, abi.encodePacked(value2), PackedCounter.wrap(bytes32(0)), new bytes(0));
// Get the list of keys in the target table
keysInTable = getKeysInTable(world, tableId);
// Assert that the list is correct
assertEq(keysInTable.length, 2);
assertEq(keysInTable[0][0], key1);
assertEq(keysInTable[1][0], key2);
}
function testDeleteFromMiddle(uint256 value) public {
_installKeysInTableModule();
// Add 3 values
world.setRecord(tableId, keyTuple1, abi.encodePacked(value), PackedCounter.wrap(bytes32(0)), new bytes(0));
world.setRecord(tableId, keyTuple2, abi.encodePacked(value), PackedCounter.wrap(bytes32(0)), new bytes(0));
world.setRecord(tableId, keyTuple3, abi.encodePacked(value), PackedCounter.wrap(bytes32(0)), new bytes(0));
// Remove 2, starting from the middle
// This tests that KeysInTable correctly tracks swaps indexes
world.deleteRecord(tableId, keyTuple2);
world.deleteRecord(tableId, keyTuple3);
// Get the list of keys in the target table
bytes32[][] memory keysInTable = getKeysInTable(world, tableId);
// Assert that the list is correct
assertEq(keysInTable.length, 1);
assertEq(keysInTable[0][0], key1);
}
}