-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathStoreCore.sol
1221 lines (1122 loc) · 53 KB
/
StoreCore.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;
import { STORE_VERSION } from "./version.sol";
import { Bytes } from "./Bytes.sol";
import { Storage } from "./Storage.sol";
import { Memory } from "./Memory.sol";
import { FieldLayout, FieldLayoutLib } from "./FieldLayout.sol";
import { Schema, SchemaLib } from "./Schema.sol";
import { EncodedLengths } from "./EncodedLengths.sol";
import { Slice, SliceLib } from "./Slice.sol";
import { Tables, ResourceIds, StoreHooks } from "./codegen/index.sol";
import { IStoreErrors } from "./IStoreErrors.sol";
import { IStoreHook } from "./IStoreHook.sol";
import { StoreSwitch } from "./StoreSwitch.sol";
import { Hook, HookLib } from "./Hook.sol";
import { BEFORE_SET_RECORD, AFTER_SET_RECORD, BEFORE_SPLICE_STATIC_DATA, AFTER_SPLICE_STATIC_DATA, BEFORE_SPLICE_DYNAMIC_DATA, AFTER_SPLICE_DYNAMIC_DATA, BEFORE_DELETE_RECORD, AFTER_DELETE_RECORD } from "./storeHookTypes.sol";
import { ResourceId, ResourceIdLib } from "./ResourceId.sol";
import { RESOURCE_TABLE, RESOURCE_OFFCHAIN_TABLE } from "./storeResourceTypes.sol";
import { IStoreEvents } from "./IStoreEvents.sol";
/**
* @title StoreCore Library
* @author MUD (https://mud.dev) by Lattice (https://lattice.xyz)
* @notice This library includes implementations for all IStore methods and events related to the store actions.
*/
library StoreCore {
/**
* @notice Initialize the store address in StoreSwitch.
* @dev Consumers must call this function in their constructor.
* StoreSwitch uses the storeAddress to decide where to write data to.
* If StoreSwitch is called in the context of a Store contract (storeAddress == address(this)),
* StoreSwitch uses internal methods to write data instead of external calls.
*/
function initialize() internal {
StoreSwitch.setStoreAddress(address(this));
}
/**
* @notice Register Store protocol's internal tables in the store.
* @dev Consumers must call this function in their constructor before setting
* any table data to allow indexers to decode table events.
*/
function registerInternalTables() internal {
// Because `registerTable` writes to both `Tables` and `ResourceIds`, we can't use it
// directly here without creating a race condition, where we'd write to one or the other
// before they exist (depending on the order of registration).
//
// Instead, we'll register them manually, writing everything to the `Tables` table first,
// then the `ResourceIds` table. The logic here ought to be kept in sync with the internals
// of the `registerTable` function below.
if (ResourceIds._getExists(Tables._tableId)) {
revert IStoreErrors.Store_TableAlreadyExists(Tables._tableId, string(abi.encodePacked(Tables._tableId)));
}
if (ResourceIds._getExists(ResourceIds._tableId)) {
revert IStoreErrors.Store_TableAlreadyExists(
ResourceIds._tableId,
string(abi.encodePacked(ResourceIds._tableId))
);
}
Tables._set(
Tables._tableId,
Tables._fieldLayout,
Tables._keySchema,
Tables._valueSchema,
abi.encode(Tables.getKeyNames()),
abi.encode(Tables.getFieldNames())
);
Tables._set(
ResourceIds._tableId,
ResourceIds._fieldLayout,
ResourceIds._keySchema,
ResourceIds._valueSchema,
abi.encode(ResourceIds.getKeyNames()),
abi.encode(ResourceIds.getFieldNames())
);
ResourceIds._setExists(Tables._tableId, true);
ResourceIds._setExists(ResourceIds._tableId, true);
// Now we can register the rest of the core tables as regular tables.
StoreHooks.register();
}
/************************************************************************
*
* SCHEMA
*
************************************************************************/
/**
* @notice Get the field layout for the given table ID.
* @param tableId The ID of the table for which to get the field layout.
* @return The field layout for the given table ID.
*/
function getFieldLayout(ResourceId tableId) internal view returns (FieldLayout) {
// Explicit check for the Tables table to solve the bootstraping issue
// of the Tables table not having a field layout before it is registered
// since the field layout is stored in the Tables table.
if (ResourceId.unwrap(tableId) == ResourceId.unwrap(Tables._tableId)) {
return Tables._fieldLayout;
}
return
FieldLayout.wrap(
Storage.loadField({
storagePointer: StoreCoreInternal._getStaticDataLocation(Tables._tableId, ResourceId.unwrap(tableId)),
length: 32,
offset: 0
})
);
}
/**
* @notice Get the key schema for the given table ID.
* @dev Reverts if the table ID is not registered.
* @param tableId The ID of the table for which to get the key schema.
* @return keySchema The key schema for the given table ID.
*/
function getKeySchema(ResourceId tableId) internal view returns (Schema keySchema) {
keySchema = Tables._getKeySchema(tableId);
// key schemas can be empty for singleton tables, so we can't depend on key schema for table check
if (!ResourceIds._getExists(tableId)) {
revert IStoreErrors.Store_TableNotFound(tableId, string(abi.encodePacked(tableId)));
}
}
/**
* @notice Get the value schema for the given table ID.
* @dev Reverts if the table ID is not registered.
* @param tableId The ID of the table for which to get the value schema.
* @return valueSchema The value schema for the given table ID.
*/
function getValueSchema(ResourceId tableId) internal view returns (Schema valueSchema) {
valueSchema = Tables._getValueSchema(tableId);
if (valueSchema.isEmpty()) {
revert IStoreErrors.Store_TableNotFound(tableId, string(abi.encodePacked(tableId)));
}
}
/**
* @notice Register a new table with the given configuration.
* @dev This method reverts if
* - The table ID is not of type RESOURCE_TABLE or RESOURCE_OFFCHAIN_TABLE.
* - The field layout is invalid.
* - The key schema is invalid.
* - The value schema is invalid.
* - The number of key names does not match the number of key schema types.
* - The number of field names does not match the number of field layout fields.
* @param tableId The ID of the table to register.
* @param fieldLayout The field layout of the table.
* @param keySchema The key schema of the table.
* @param valueSchema The value schema of the table.
* @param keyNames The names of the keys in the table.
* @param fieldNames The names of the fields in the table.
*/
function registerTable(
ResourceId tableId,
FieldLayout fieldLayout,
Schema keySchema,
Schema valueSchema,
string[] memory keyNames,
string[] memory fieldNames
) internal {
// Verify the table ID is of type RESOURCE_TABLE or RESOURCE_OFFCHAIN_TABLE
if (tableId.getType() != RESOURCE_TABLE && tableId.getType() != RESOURCE_OFFCHAIN_TABLE) {
revert IStoreErrors.Store_InvalidResourceType(RESOURCE_TABLE, tableId, string(abi.encodePacked(tableId)));
}
// Verify the field layout is valid
fieldLayout.validate();
// Verify the schema is valid
keySchema.validate({ allowEmpty: true });
valueSchema.validate({ allowEmpty: false });
// Verify the number of key names matches the number of key schema types
if (keyNames.length != keySchema.numFields()) {
revert IStoreErrors.Store_InvalidKeyNamesLength(keySchema.numFields(), keyNames.length);
}
// Verify the number of value names
if (fieldNames.length != fieldLayout.numFields()) {
revert IStoreErrors.Store_InvalidFieldNamesLength(fieldLayout.numFields(), fieldNames.length);
}
// Verify the number of value schema types
if (valueSchema.numFields() != fieldLayout.numFields()) {
revert IStoreErrors.Store_InvalidValueSchemaLength(fieldLayout.numFields(), valueSchema.numFields());
}
if (valueSchema.numStaticFields() != fieldLayout.numStaticFields()) {
revert IStoreErrors.Store_InvalidValueSchemaStaticLength(
fieldLayout.numStaticFields(),
valueSchema.numStaticFields()
);
}
if (valueSchema.numDynamicFields() != fieldLayout.numDynamicFields()) {
revert IStoreErrors.Store_InvalidValueSchemaDynamicLength(
fieldLayout.numDynamicFields(),
valueSchema.numDynamicFields()
);
}
// Verify that static field lengths are consistent between Schema and FieldLayout
for (uint256 i; i < fieldLayout.numStaticFields(); i++) {
if (fieldLayout.atIndex(i) != valueSchema.atIndex(i).getStaticByteLength()) {
revert IStoreErrors.Store_InvalidStaticDataLength(
fieldLayout.atIndex(i),
valueSchema.atIndex(i).getStaticByteLength()
);
}
}
// Verify that there is no table or offchain table with the same name
ResourceId onchainTableId = ResourceIdLib.encode(RESOURCE_TABLE, tableId.getResourceName());
ResourceId offchainTableId = ResourceIdLib.encode(RESOURCE_OFFCHAIN_TABLE, tableId.getResourceName());
if (ResourceIds._getExists(onchainTableId) || ResourceIds._getExists(offchainTableId)) {
revert IStoreErrors.Store_TableAlreadyExists(tableId, string(abi.encodePacked(tableId)));
}
// Register the table metadata
Tables._set(tableId, fieldLayout, keySchema, valueSchema, abi.encode(keyNames), abi.encode(fieldNames));
// Register the table ID
ResourceIds._setExists(tableId, true);
}
/************************************************************************
*
* REGISTER HOOKS
*
************************************************************************/
/**
* @notice Register hooks to be called when a record or field is set or deleted.
* @dev This method reverts for all resource IDs other than tables.
* Hooks are not supported for offchain tables.
* @param tableId The ID of the table to register the hook for.
* @param hookAddress The address of the hook contract to register.
* @param enabledHooksBitmap The bitmap of enabled hooks.
*/
function registerStoreHook(ResourceId tableId, IStoreHook hookAddress, uint8 enabledHooksBitmap) internal {
// Hooks are only supported for tables, not for offchain tables
if (tableId.getType() != RESOURCE_TABLE) {
revert IStoreErrors.Store_InvalidResourceType(RESOURCE_TABLE, tableId, string(abi.encodePacked(tableId)));
}
// Require the table to exist
if (!ResourceIds._getExists(tableId)) {
revert IStoreErrors.Store_TableNotFound(tableId, string(abi.encodePacked(tableId)));
}
StoreHooks._push(tableId, Hook.unwrap(HookLib.encode(address(hookAddress), enabledHooksBitmap)));
}
/**
* @notice Unregister a hook from the given table ID.
* @param tableId The ID of the table to unregister the hook from.
* @param hookAddress The address of the hook to unregister.
*/
function unregisterStoreHook(ResourceId tableId, IStoreHook hookAddress) internal {
HookLib.filterListByAddress(StoreHooks._tableId, tableId, address(hookAddress));
}
/************************************************************************
*
* SET DATA
*
************************************************************************/
/**
* @notice Set a full record for the given table ID and key tuple.
* @dev Calling this method emits a Store_SetRecord event.
* This method internally calls another overload of setRecord by fetching the field layout for the given table ID.
* If the field layout is available to the caller, it is recommended to use the other overload to avoid an additional storage read.
* @param tableId The ID of the table to set the record for.
* @param keyTuple An array representing the composite key for the record.
* @param staticData The static data of the record.
* @param encodedLengths The encoded lengths of the dynamic data of the record.
* @param dynamicData The dynamic data of the record.
*/
function setRecord(
ResourceId tableId,
bytes32[] memory keyTuple,
bytes memory staticData,
EncodedLengths encodedLengths,
bytes memory dynamicData
) internal {
setRecord(tableId, keyTuple, staticData, encodedLengths, dynamicData, getFieldLayout(tableId));
}
/**
* @notice Set a full data record for the given table ID, key tuple, and field layout.
* @dev For onchain tables, the method emits a `Store_SetRecord` event, updates the data in storage,
* calls `onBeforeSetRecord` hooks before actually modifying the state, and calls `onAfterSetRecord`
* hooks after modifying the state. For offchain tables, the method returns early after emitting the
* event without calling hooks or modifying the state.
* @param tableId The ID of the table to set the record for.
* @param keyTuple An array representing the composite key for the record.
* @param staticData The static data of the record.
* @param encodedLengths The encoded lengths of the dynamic data of the record.
* @param dynamicData The dynamic data of the record.
* @param fieldLayout The field layout for the record.
*/
function setRecord(
ResourceId tableId,
bytes32[] memory keyTuple,
bytes memory staticData,
EncodedLengths encodedLengths,
bytes memory dynamicData,
FieldLayout fieldLayout
) internal {
// Early return if the table is an offchain table
if (tableId.getType() == RESOURCE_OFFCHAIN_TABLE) {
// Emit event to notify indexers
emit IStoreEvents.Store_SetRecord(tableId, keyTuple, staticData, encodedLengths, dynamicData);
return;
}
// Call onBeforeSetRecord hooks (before actually modifying the state, so observers have access to the previous state if needed)
bytes21[] memory hooks = StoreHooks._get(tableId);
for (uint256 i; i < hooks.length; i++) {
Hook hook = Hook.wrap(hooks[i]);
if (hook.isEnabled(BEFORE_SET_RECORD)) {
IStoreHook(hook.getAddress()).onBeforeSetRecord(
tableId,
keyTuple,
staticData,
encodedLengths,
dynamicData,
fieldLayout
);
}
}
// Emit event to notify indexers
emit IStoreEvents.Store_SetRecord(tableId, keyTuple, staticData, encodedLengths, dynamicData);
// Store the static data at the static data location
uint256 staticDataLocation = StoreCoreInternal._getStaticDataLocation(tableId, keyTuple);
uint256 memoryPointer = Memory.dataPointer(staticData);
Storage.store({
storagePointer: staticDataLocation,
offset: 0,
length: staticData.length,
memoryPointer: memoryPointer
});
// Set the dynamic data if there are dynamic fields
if (fieldLayout.numDynamicFields() > 0) {
// Store the dynamic data length at the dynamic data length location
uint256 dynamicDataLengthLocation = StoreCoreInternal._getDynamicDataLengthLocation(tableId, keyTuple);
Storage.store({ storagePointer: dynamicDataLengthLocation, data: encodedLengths.unwrap() });
// Move the memory pointer to the start of the dynamic data
memoryPointer = Memory.dataPointer(dynamicData);
// For every dynamic element, slice off the dynamic data and store it at the dynamic location
uint256 dynamicDataLocation;
uint256 dynamicDataLength;
for (uint8 i; i < fieldLayout.numDynamicFields(); ) {
dynamicDataLocation = StoreCoreInternal._getDynamicDataLocation(tableId, keyTuple, i);
dynamicDataLength = encodedLengths.atIndex(i);
Storage.store({
storagePointer: dynamicDataLocation,
offset: 0,
length: dynamicDataLength,
memoryPointer: memoryPointer
});
memoryPointer += dynamicDataLength; // move the memory pointer to the start of the next dynamic data
unchecked {
i++;
}
}
}
// Call onAfterSetRecord hooks (after modifying the state)
for (uint256 i; i < hooks.length; i++) {
Hook hook = Hook.wrap(hooks[i]);
if (hook.isEnabled(AFTER_SET_RECORD)) {
IStoreHook(hook.getAddress()).onAfterSetRecord(
tableId,
keyTuple,
staticData,
encodedLengths,
dynamicData,
fieldLayout
);
}
}
}
/**
* @notice Splice the static data for the given table ID and key tuple.
* @dev This method emits a `Store_SpliceStaticData` event, updates the data in storage, and calls
* `onBeforeSpliceStaticData` and `onAfterSpliceStaticData` hooks.
* For offchain tables, it returns early after emitting the event.
* @param tableId The ID of the table to splice the static data for.
* @param keyTuple An array representing the composite key for the record.
* @param start The start position in bytes for the splice operation.
* @param data The data to write to the static data of the record at the start byte.
*/
function spliceStaticData(ResourceId tableId, bytes32[] memory keyTuple, uint48 start, bytes memory data) internal {
// Early return if the table is an offchain table
if (tableId.getType() == RESOURCE_OFFCHAIN_TABLE) {
// Emit event to notify offchain indexers
emit IStoreEvents.Store_SpliceStaticData({ tableId: tableId, keyTuple: keyTuple, start: start, data: data });
return;
}
uint256 location = StoreCoreInternal._getStaticDataLocation(tableId, keyTuple);
// Call onBeforeSpliceStaticData hooks (before actually modifying the state, so observers have access to the previous state if needed)
bytes21[] memory hooks = StoreHooks._get(tableId);
for (uint256 i; i < hooks.length; i++) {
Hook hook = Hook.wrap(hooks[i]);
if (hook.isEnabled(BEFORE_SPLICE_STATIC_DATA)) {
IStoreHook(hook.getAddress()).onBeforeSpliceStaticData({
tableId: tableId,
keyTuple: keyTuple,
start: start,
data: data
});
}
}
// Emit event to notify offchain indexers
emit IStoreEvents.Store_SpliceStaticData({ tableId: tableId, keyTuple: keyTuple, start: start, data: data });
// Store the provided value in storage
Storage.store({ storagePointer: location, offset: start, data: data });
// Call onAfterSpliceStaticData hooks
for (uint256 i; i < hooks.length; i++) {
Hook hook = Hook.wrap(hooks[i]);
if (hook.isEnabled(AFTER_SPLICE_STATIC_DATA)) {
IStoreHook(hook.getAddress()).onAfterSpliceStaticData({
tableId: tableId,
keyTuple: keyTuple,
start: start,
data: data
});
}
}
}
/**
* @notice Splice the dynamic data for the given table ID, key tuple, and dynamic field index.
* @dev This method emits a `Store_SpliceDynamicData` event, updates the data in storage, and calls
* `onBeforeSpliceDynamicData` and `onAfterSpliceDynamicData` hooks.
* For offchain tables, it returns early after emitting the event.
* @param tableId The ID of the table to splice the dynamic data for.
* @param keyTuple An array representing the composite key for the record.
* @param dynamicFieldIndex The index of the dynamic field to splice. (Dynamic field index = field index - number of static fields)
* @param startWithinField The start position within the field for the splice operation.
* @param deleteCount The number of bytes to delete in the splice operation.
* @param data The data to insert into the dynamic data of the record at the start byte.
*/
function spliceDynamicData(
ResourceId tableId,
bytes32[] memory keyTuple,
uint8 dynamicFieldIndex,
uint40 startWithinField,
uint40 deleteCount,
bytes memory data
) internal {
StoreCoreInternal._spliceDynamicData({
tableId: tableId,
keyTuple: keyTuple,
dynamicFieldIndex: dynamicFieldIndex,
startWithinField: startWithinField,
deleteCount: deleteCount,
data: data,
previousEncodedLengths: StoreCoreInternal._loadEncodedDynamicDataLength(tableId, keyTuple)
});
}
/**
* @notice Set data for a field at the given index in a table with the given tableId, key tuple, and value field layout.
* @dev This method internally calls another overload of setField by fetching the field layout for the given table ID.
* If the field layout is available to the caller, it is recommended to use the other overload to avoid an additional storage read.
* This function emits a `Store_SpliceStaticData` or `Store_SpliceDynamicData` event and calls the corresponding hooks.
* For offchain tables, it returns early after emitting the event.
* @param tableId The ID of the table to set the field for.
* @param keyTuple An array representing the key for the record.
* @param fieldIndex The index of the field to set.
* @param data The data to set for the field.
*/
function setField(ResourceId tableId, bytes32[] memory keyTuple, uint8 fieldIndex, bytes memory data) internal {
setField(tableId, keyTuple, fieldIndex, data, getFieldLayout(tableId));
}
/**
* @notice Set data for a field at the given index in a table with the given tableId, key tuple, and value field layout.
* @dev This method internally calls to `setStaticField` or `setDynamicField` based on the field index and layout.
* Calling `setStaticField` or `setDynamicField` directly is recommended if the caller is aware of the field layout.
* This function emits a `Store_SpliceStaticData` or `Store_SpliceDynamicData` event, updates the data in storage,
* and calls the corresponding hooks.
* For offchain tables, it returns early after emitting the event.
* @param tableId The ID of the table to set the field for.
* @param keyTuple An array representing the composite key for the record.
* @param fieldIndex The index of the field to set.
* @param data The data to set for the field.
* @param fieldLayout The field layout for the record.
*/
function setField(
ResourceId tableId,
bytes32[] memory keyTuple,
uint8 fieldIndex,
bytes memory data,
FieldLayout fieldLayout
) internal {
if (fieldIndex < fieldLayout.numStaticFields()) {
setStaticField(tableId, keyTuple, fieldIndex, data, fieldLayout);
} else {
setDynamicField(tableId, keyTuple, fieldIndex - uint8(fieldLayout.numStaticFields()), data);
}
}
/**
* @notice Set a static field for the given table ID, key tuple, field index, and field layout.
* @dev This method emits a `Store_SpliceStaticData` event, updates the data in storage and calls the
* `onBeforeSpliceStaticData` and `onAfterSpliceStaticData` hooks.
* For offchain tables, it returns early after emitting the event.
* @param tableId The ID of the table to set the static field for.
* @param keyTuple An array representing the key for the record.
* @param fieldIndex The index of the field to set.
* @param data The data to set for the static field.
* @param fieldLayout The field layout for the record.
*/
function setStaticField(
ResourceId tableId,
bytes32[] memory keyTuple,
uint8 fieldIndex,
bytes memory data,
FieldLayout fieldLayout
) internal {
spliceStaticData({
tableId: tableId,
keyTuple: keyTuple,
start: uint48(StoreCoreInternal._getStaticDataOffset(fieldLayout, fieldIndex)),
data: data
});
}
/**
* @notice Set a dynamic field for the given table ID, key tuple, and dynamic field index.
* @dev This method emits a `Store_SpliceDynamicData` event, updates the data in storage and calls the
* `onBeforeSpliceDynamicaData` and `onAfterSpliceDynamicData` hooks.
* For offchain tables, it returns early after emitting the event.
* @param tableId The ID of the table to set the dynamic field for.
* @param keyTuple An array representing the composite key for the record.
* @param dynamicFieldIndex The index of the dynamic field to set. (Dynamic field index = field index - number of static fields).
* @param data The data to set for the dynamic field.
*/
function setDynamicField(
ResourceId tableId,
bytes32[] memory keyTuple,
uint8 dynamicFieldIndex,
bytes memory data
) internal {
// Load the previous length of the field to set from storage to compute how much data to delete
EncodedLengths previousEncodedLengths = StoreCoreInternal._loadEncodedDynamicDataLength(tableId, keyTuple);
uint40 previousFieldLength = uint40(previousEncodedLengths.atIndex(dynamicFieldIndex));
StoreCoreInternal._spliceDynamicData({
tableId: tableId,
keyTuple: keyTuple,
dynamicFieldIndex: dynamicFieldIndex,
startWithinField: 0,
deleteCount: previousFieldLength,
data: data,
previousEncodedLengths: previousEncodedLengths
});
}
/**
* @notice Delete a record for the given table ID and key tuple.
* @dev This method internally calls another overload of deleteRecord by fetching the field layout for the given table ID.
* This method deletes static data and sets the dynamic data length to 0, but does not
* actually modify the dynamic data. It emits a `Store_DeleteRecord` event and emits the
* `onBeforeDeleteRecord` and `onAfterDeleteRecord` hooks.
* For offchain tables, it returns early after emitting the event.
* @param tableId The ID of the table to delete the record from.
* @param keyTuple An array representing the composite key for the record.
*/
function deleteRecord(ResourceId tableId, bytes32[] memory keyTuple) internal {
deleteRecord(tableId, keyTuple, getFieldLayout(tableId));
}
/**
* @notice Delete a record for the given table ID and key tuple.
* @dev This method deletes static data and sets the dynamic data length to 0, but does not
* actually modify the dynamic data. It emits a `Store_DeleteRecord` event and emits the
* `onBeforeDeleteRecord` and `onAfterDeleteRecord` hooks.
* For offchain tables, it returns early after emitting the event.
* @param tableId The ID of the table to delete the record from.
* @param keyTuple An array representing the composite key for the record.
* @param fieldLayout The field layout for the record.
*/
function deleteRecord(ResourceId tableId, bytes32[] memory keyTuple, FieldLayout fieldLayout) internal {
// Early return if the table is an offchain table
if (tableId.getType() == RESOURCE_OFFCHAIN_TABLE) {
// Emit event to notify indexers
emit IStoreEvents.Store_DeleteRecord(tableId, keyTuple);
return;
}
// Call onBeforeDeleteRecord hooks (before actually modifying the state, so observers have access to the previous state if needed)
bytes21[] memory hooks = StoreHooks._get(tableId);
for (uint256 i; i < hooks.length; i++) {
Hook hook = Hook.wrap(hooks[i]);
if (hook.isEnabled(BEFORE_DELETE_RECORD)) {
IStoreHook(hook.getAddress()).onBeforeDeleteRecord(tableId, keyTuple, fieldLayout);
}
}
// Emit event to notify indexers
emit IStoreEvents.Store_DeleteRecord(tableId, keyTuple);
// Delete static data
uint256 staticDataLocation = StoreCoreInternal._getStaticDataLocation(tableId, keyTuple);
Storage.store({ storagePointer: staticDataLocation, offset: 0, data: new bytes(fieldLayout.staticDataLength()) });
// If there are dynamic fields, set the dynamic data length to 0.
// We don't need to delete the dynamic data because it will be overwritten when a new record is set.
if (fieldLayout.numDynamicFields() > 0) {
uint256 dynamicDataLengthLocation = StoreCoreInternal._getDynamicDataLengthLocation(tableId, keyTuple);
Storage.zero({ storagePointer: dynamicDataLengthLocation, length: 32 });
}
// Call onAfterDeleteRecord hooks
for (uint256 i; i < hooks.length; i++) {
Hook hook = Hook.wrap(hooks[i]);
if (hook.isEnabled(AFTER_DELETE_RECORD)) {
IStoreHook(hook.getAddress()).onAfterDeleteRecord(tableId, keyTuple, fieldLayout);
}
}
}
/**
* @notice Push data to a field at the dynamic field index in a table with the given table ID and key tuple.
* @dev This method emits a `Store_SpliceDynamicData` event, updates the data in storage and calls the
* `onBeforeSpliceDynamicData` and `onAfterSpliceDynamicData` hooks.
* For offchain tables, it returns early after emitting the event.
* @param tableId The ID of the table to push data to the dynamic field.
* @param keyTuple An array representing the composite key for the record.
* @param dynamicFieldIndex The index of the dynamic field to push data to.
* @param dataToPush The data to push to the dynamic field.
*/
function pushToDynamicField(
ResourceId tableId,
bytes32[] memory keyTuple,
uint8 dynamicFieldIndex,
bytes memory dataToPush
) internal {
// Load the previous length of the field to set from storage to compute where to start to push
EncodedLengths previousEncodedLengths = StoreCoreInternal._loadEncodedDynamicDataLength(tableId, keyTuple);
uint40 previousFieldLength = uint40(previousEncodedLengths.atIndex(dynamicFieldIndex));
// Splice the dynamic data
StoreCoreInternal._spliceDynamicData({
tableId: tableId,
keyTuple: keyTuple,
dynamicFieldIndex: dynamicFieldIndex,
startWithinField: uint40(previousFieldLength),
deleteCount: 0,
data: dataToPush,
previousEncodedLengths: previousEncodedLengths
});
}
/**
* @notice Pop data from a field at the dynamic field index in a table with the given table ID and key tuple.
* @dev This method emits a `Store_SpliceDynamicData` event, updates the data in storage and calls the
* `onBeforeSpliceDynamicData` and `onAfterSpliceDynamicData` hooks.
* For offchain tables, it returns early after emitting the event.
* @param tableId The ID of the table to pop data from the dynamic field.
* @param keyTuple An array representing the composite key for the record.
* @param dynamicFieldIndex The index of the dynamic field to pop data from.
* @param byteLengthToPop The byte length to pop from the dynamic field.
*/
function popFromDynamicField(
ResourceId tableId,
bytes32[] memory keyTuple,
uint8 dynamicFieldIndex,
uint256 byteLengthToPop
) internal {
// Load the previous length of the field to set from storage to compute where to start to push
EncodedLengths previousEncodedLengths = StoreCoreInternal._loadEncodedDynamicDataLength(tableId, keyTuple);
uint40 previousFieldLength = uint40(previousEncodedLengths.atIndex(dynamicFieldIndex));
// Splice the dynamic data
StoreCoreInternal._spliceDynamicData({
tableId: tableId,
keyTuple: keyTuple,
dynamicFieldIndex: dynamicFieldIndex,
startWithinField: uint40(previousFieldLength - byteLengthToPop),
deleteCount: uint40(byteLengthToPop),
data: new bytes(0),
previousEncodedLengths: previousEncodedLengths
});
}
/************************************************************************
*
* GET DATA
*
************************************************************************/
/**
* @notice Get the full record (all fields, static and dynamic data) for the given table ID and key tuple.
* @dev This function internally calls another overload of `getRecord`, loading the field layout from storage.
* If the field layout is available to the caller, it is recommended to use the other overload to avoid an additional storage read.
* @param tableId The ID of the table to get the record from.
* @param keyTuple An array representing the composite key for the record.
* @return staticData The static data of the record.
* @return encodedLengths The encoded lengths of the dynamic data of the record.
* @return dynamicData The dynamic data of the record.
*/
function getRecord(
ResourceId tableId,
bytes32[] memory keyTuple
) internal view returns (bytes memory staticData, EncodedLengths encodedLengths, bytes memory dynamicData) {
return getRecord(tableId, keyTuple, getFieldLayout(tableId));
}
/**
* @notice Get the full record (all fields, static and dynamic data) for the given table ID and key tuple, with the given field layout.
* @param tableId The ID of the table to get the record from.
* @param keyTuple An array representing the composite key for the record.
* @param fieldLayout The field layout for the record.
* @return staticData The static data of the record.
* @return encodedLengths The encoded lengths of the dynamic data of the record.
* @return dynamicData The dynamic data of the record.
*/
function getRecord(
ResourceId tableId,
bytes32[] memory keyTuple,
FieldLayout fieldLayout
) internal view returns (bytes memory staticData, EncodedLengths encodedLengths, bytes memory dynamicData) {
// Get the static data length
uint256 staticLength = fieldLayout.staticDataLength();
// Load the static data from storage
staticData = StoreCoreInternal._getStaticData(tableId, keyTuple, staticLength);
// Load the dynamic data if there are dynamic fields
uint256 numDynamicFields = fieldLayout.numDynamicFields();
if (numDynamicFields > 0) {
// Load the encoded dynamic data length
encodedLengths = StoreCoreInternal._loadEncodedDynamicDataLength(tableId, keyTuple);
// Append dynamic data
dynamicData = new bytes(encodedLengths.total());
uint256 memoryPointer = Memory.dataPointer(dynamicData);
for (uint8 i; i < numDynamicFields; i++) {
uint256 dynamicDataLocation = StoreCoreInternal._getDynamicDataLocation(tableId, keyTuple, i);
uint256 length = encodedLengths.atIndex(i);
Storage.load({ storagePointer: dynamicDataLocation, offset: 0, length: length, memoryPointer: memoryPointer });
// Advance memoryPointer by the length of this dynamic field
memoryPointer += length;
}
}
}
/**
* @notice Get a single field from the given table ID and key tuple.
* @dev This function internally calls another overload of `getField`, loading the field layout from storage.
* @param tableId The ID of the table to get the field from.
* @param keyTuple An array representing the composite key for the record.
* @param fieldIndex The index of the field to get.
* @return The data of the field.
*/
function getField(
ResourceId tableId,
bytes32[] memory keyTuple,
uint8 fieldIndex
) internal view returns (bytes memory) {
return getField(tableId, keyTuple, fieldIndex, getFieldLayout(tableId));
}
/**
* @notice Get a single field from the given table ID and key tuple, with the given field layout.
* @param tableId The ID of the table to get the field from.
* @param keyTuple An array representing the composite key for the record.
* @param fieldIndex The index of the field to get.
* @param fieldLayout The field layout for the record.
* @return The data of the field.
*/
function getField(
ResourceId tableId,
bytes32[] memory keyTuple,
uint8 fieldIndex,
FieldLayout fieldLayout
) internal view returns (bytes memory) {
if (fieldIndex < fieldLayout.numStaticFields()) {
return StoreCoreInternal._getStaticFieldBytes(tableId, keyTuple, fieldIndex, fieldLayout);
} else {
return getDynamicField(tableId, keyTuple, fieldIndex - uint8(fieldLayout.numStaticFields()));
}
}
/**
* @notice Get a single static field from the given table ID and key tuple, with the given value field layout.
* @dev The field value is left-aligned in the returned bytes32, the rest of the word is not zeroed out.
* Consumers are expected to truncate the returned value as needed.
* @param tableId The ID of the table to get the static field from.
* @param keyTuple An array representing the composite key for the record.
* @param fieldIndex The index of the field to get.
* @param fieldLayout The field layout for the record.
* @return The data of the static field.
*/
function getStaticField(
ResourceId tableId,
bytes32[] memory keyTuple,
uint8 fieldIndex,
FieldLayout fieldLayout
) internal view returns (bytes32) {
// Get the length, storage location and offset of the static field
// and load the data from storage
return
Storage.loadField({
storagePointer: StoreCoreInternal._getStaticDataLocation(tableId, keyTuple),
length: fieldLayout.atIndex(fieldIndex),
offset: StoreCoreInternal._getStaticDataOffset(fieldLayout, fieldIndex)
});
}
/**
* @notice Get a single dynamic field from the given table ID and key tuple.
* @param tableId The ID of the table to get the dynamic field from.
* @param keyTuple An array representing the composite key for the record.
* @param dynamicFieldIndex The index of the dynamic field to get, relative to the start of the dynamic fields.
* (Dynamic field index = field index - number of static fields)
* @return The data of the dynamic field.
*/
function getDynamicField(
ResourceId tableId,
bytes32[] memory keyTuple,
uint8 dynamicFieldIndex
) internal view returns (bytes memory) {
// Get the storage location of the dynamic field
// and load the data from storage
return
Storage.load({
storagePointer: StoreCoreInternal._getDynamicDataLocation(tableId, keyTuple, dynamicFieldIndex),
offset: 0,
length: StoreCoreInternal._loadEncodedDynamicDataLength(tableId, keyTuple).atIndex(dynamicFieldIndex)
});
}
/**
* @notice Get the byte length of a single field from the given table ID and key tuple.
* @dev This function internally calls another overload of `getFieldLength`, loading the field layout from storage.
* If the field layout is available to the caller, it is recommended to use the other overload to avoid an additional storage read.
* @param tableId The ID of the table to get the field length from.
* @param keyTuple An array representing the composite key for the record.
* @param fieldIndex The index of the field to get the length for.
* @return The byte length of the field.
*/
function getFieldLength(
ResourceId tableId,
bytes32[] memory keyTuple,
uint8 fieldIndex
) internal view returns (uint256) {
return getFieldLength(tableId, keyTuple, fieldIndex, getFieldLayout(tableId));
}
/**
* @notice Get the byte length of a single field from the given table ID and key tuple.
* @param tableId The ID of the table to get the field length from.
* @param keyTuple An array representing the composite key for the record.
* @param fieldIndex The index of the field to get the length for.
* @param fieldLayout The field layout for the record.
* @return The byte length of the field.
*/
function getFieldLength(
ResourceId tableId,
bytes32[] memory keyTuple,
uint8 fieldIndex,
FieldLayout fieldLayout
) internal view returns (uint256) {
uint8 numStaticFields = uint8(fieldLayout.numStaticFields());
if (fieldIndex < numStaticFields) {
return fieldLayout.atIndex(fieldIndex);
} else {
return getDynamicFieldLength(tableId, keyTuple, fieldIndex - numStaticFields);
}
}
/**
* @notice Get the byte length of a single dynamic field from the given table ID and key tuple.
* @param tableId The ID of the table to get the dynamic field length from.
* @param keyTuple An array representing the composite key for the record.
* @param dynamicFieldIndex The index of the dynamic field to get the length for, relative to the start of the dynamic fields.
* (Dynamic field index = field index - number of static fields)
* @return The byte length of the dynamic field.
*/
function getDynamicFieldLength(
ResourceId tableId,
bytes32[] memory keyTuple,
uint8 dynamicFieldIndex
) internal view returns (uint256) {
return StoreCoreInternal._loadEncodedDynamicDataLength(tableId, keyTuple).atIndex(dynamicFieldIndex);
}
/**
* @notice Get a byte slice (including start, excluding end) of a single dynamic field from the given table ID and key tuple.
* @param tableId The ID of the table to get the dynamic field slice from.
* @param keyTuple An array representing the composite key for the record.
* @param dynamicFieldIndex The index of the dynamic field to get the slice from, relative to the start of the dynamic fields.
* (Dynamic field index = field index - number of static fields)
* @param start The start index within the dynamic field for the slice operation (inclusive).
* @param end The end index within the dynamic field for the slice operation (exclusive).
* @return The byte slice of the dynamic field.
*/
function getDynamicFieldSlice(
ResourceId tableId,
bytes32[] memory keyTuple,
uint8 dynamicFieldIndex,
uint256 start,
uint256 end
) internal view returns (bytes memory) {
// Verify the slice bounds are valid
if (start > end) {
revert IStoreErrors.Store_InvalidBounds(start, end);
}
// Verify the accessed data is within the bounds of the dynamic field.
// This is necessary because we don't delete the dynamic data when a record is deleted,
// but only decrease its length.
EncodedLengths encodedLengths = StoreCoreInternal._loadEncodedDynamicDataLength(tableId, keyTuple);
uint256 fieldLength = encodedLengths.atIndex(dynamicFieldIndex);
if (start >= fieldLength || end > fieldLength) {
revert IStoreErrors.Store_IndexOutOfBounds(fieldLength, start >= fieldLength ? start : end - 1);
}
// Get the length and storage location of the dynamic field
uint256 location = StoreCoreInternal._getDynamicDataLocation(tableId, keyTuple, dynamicFieldIndex);
unchecked {
return Storage.load({ storagePointer: location, offset: start, length: end - start });
}
}
}
/**
* @title StoreCoreInternal
* @author MUD (https://mud.dev) by Lattice (https://lattice.xyz)
* @dev This library contains internal functions used by StoreCore.
* They are not intended to be used directly by consumers of StoreCore.
*/
library StoreCoreInternal {
bytes32 internal constant SLOT = keccak256("mud.store");
bytes32 internal constant DYNAMIC_DATA_SLOT = keccak256("mud.store.dynamicData");
bytes32 internal constant DYNAMIC_DATA_LENGTH_SLOT = keccak256("mud.store.dynamicDataLength");
/************************************************************************
*
* SET DATA
*
************************************************************************/
/**
* @notice Splice dynamic data in the store.
* @dev This function checks various conditions to ensure the operation is valid.
* It emits a `Store_SpliceDynamicData` event, calls `onBeforeSpliceDynamicData` hooks before actually modifying the storage,
* and calls `onAfterSpliceDynamicData` hooks after modifying the storage.
* It reverts with `Store_InvalidResourceType` if the table ID is not a table.
* (Splicing dynamic data is not supported for offchain tables, as it requires reading the previous encoded lengths from storage.)
* It reverts with `Store_InvalidSplice` if the splice total length of the field is changed but the splice is not at the end of the field.
* It reverts with `Store_IndexOutOfBounds` if the start index is larger than the previous length of the field.
* @param tableId The ID of the table to splice dynamic data.
* @param keyTuple An array representing the composite key for the record.
* @param dynamicFieldIndex The index of the dynamic field to splice data, relative to the start of the dynamic fields.
* (Dynamic field index = field index - number of static fields)
* @param startWithinField The start index within the field for the splice operation.
* @param deleteCount The number of bytes to delete in the splice operation.
* @param data The data to insert into the dynamic data of the record at the start byte.
* @param previousEncodedLengths The previous encoded lengths of the dynamic data of the record.
*/
function _spliceDynamicData(
ResourceId tableId,
bytes32[] memory keyTuple,
uint8 dynamicFieldIndex,
uint40 startWithinField,
uint40 deleteCount,
bytes memory data,
EncodedLengths previousEncodedLengths
) internal {
// Splicing dynamic data is not supported for offchain tables, because it
// requires reading the previous encoded lengths from storage
if (tableId.getType() != RESOURCE_TABLE) {
revert IStoreErrors.Store_InvalidResourceType(RESOURCE_TABLE, tableId, string(abi.encodePacked(tableId)));
}
uint256 previousFieldLength = previousEncodedLengths.atIndex(dynamicFieldIndex);
uint256 updatedFieldLength = previousFieldLength - deleteCount + data.length;
// If the total length of the field is changed, the data has to be appended/removed at the end of the field.
// Otherwise offchain indexers would shift the data after inserted data, while onchain the data is truncated at the end.