Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(store,world): add splice hooks, expose spliceStaticData, spliceDynamicData #1531

Merged
merged 16 commits into from
Sep 19, 2023
Merged
97 changes: 97 additions & 0 deletions .changeset/few-papayas-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
---
"@latticexyz/store": major
"@latticexyz/world": major
---

- The `IStoreHook` interface was changed to replace `onBeforeSetField` and `onAfterSetField` with `onBeforeSpliceStaticData`, `onAfterSpliceStaticData`, `onBeforeSpliceDynamicData` and `onAfterSpliceDynamicData`.

This new interface matches the new `StoreSpliceStaticData` and `StoreSpliceDynamicData` events, and avoids having to read the entire field from storage when only a subset of the field was updated
(e.g. when pushing elements to a field).

```diff
interface IStoreHook {
- function onBeforeSetField(
- bytes32 tableId,
- bytes32[] memory keyTuple,
- uint8 fieldIndex,
- bytes memory data,
- FieldLayout fieldLayout
- ) external;

- function onAfterSetField(
- bytes32 tableId,
- bytes32[] memory keyTuple,
- uint8 fieldIndex,
- bytes memory data,
- FieldLayout fieldLayout
- ) external;

+ function onBeforeSpliceStaticData(
+ bytes32 tableId,
+ bytes32[] memory keyTuple,
+ uint48 start,
+ uint40 deleteCount,
+ bytes memory data
+ ) external;

+ function onAfterSpliceStaticData(
+ bytes32 tableId,
+ bytes32[] memory keyTuple,
+ uint48 start,
+ uint40 deleteCount,
+ bytes memory data
+ ) external;

+ function onBeforeSpliceDynamicData(
+ bytes32 tableId,
+ bytes32[] memory keyTuple,
+ uint8 dynamicFieldIndex,
+ uint40 startWithinField,
+ uint40 deleteCount,
+ bytes memory data,
+ PackedCounter encodedLengths
+ ) external;

+ function onAfterSpliceDynamicData(
+ bytes32 tableId,
+ bytes32[] memory keyTuple,
+ uint8 dynamicFieldIndex,
+ uint40 startWithinField,
+ uint40 deleteCount,
+ bytes memory data,
+ PackedCounter encodedLengths
+ ) external;
}
```

- All `calldata` parameters on the `IStoreHook` interface were changed to `memory`, since the functions are called with `memory` from the `World`.

- `IStore` exposes two new functions: `spliceStaticData` and `spliceDynamicData`.

These functions provide lower level access to the operations happening under the hood in `setField`, `pushToField`, `popFromField` and `updateInField` and simplify handling
the new splice hooks.

`StoreCore`'s internal logic was simplified to use the `spliceStaticData` and `spliceDynamicData` functions instead of duplicating similar logic in different functions.

```solidity
interface IStore {
// Splice data in the static part of the record
function spliceStaticData(
bytes32 tableId,
bytes32[] calldata keyTuple,
uint48 start,
uint40 deleteCount,
bytes calldata data
) external;

// Splice data in the dynamic part of the record
function spliceDynamicData(
bytes32 tableId,
bytes32[] calldata keyTuple,
uint8 dynamicFieldIndex,
uint40 startWithinField,
uint40 deleteCount,
bytes calldata data
) external;
}
```
Loading