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

refactor(store): store core imports store events #2356

Merged
merged 3 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/famous-feet-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/store": patch
---

Refactored `StoreCore` to import `IStoreEvents` instead of defining the events twice.
89 changes: 0 additions & 89 deletions docs/pages/store/reference/store-core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -762,95 +762,6 @@ function getDynamicFieldSlice(
| -------- | ------- | ------------------------------------ |
| `<none>` | `bytes` | The byte slice of the dynamic field. |

### Events

#### Store_SetRecord

Emitted when a new record is set in the store.

```solidity
event Store_SetRecord(
ResourceId indexed tableId,
bytes32[] keyTuple,
bytes staticData,
PackedCounter encodedLengths,
bytes dynamicData
);
```

**Parameters**

| Name | Type | Description |
| ---------------- | --------------- | ------------------------------------------------------- |
| `tableId` | `ResourceId` | The ID of the table where the record is set. |
| `keyTuple` | `bytes32[]` | An array representing the composite key for the record. |
| `staticData` | `bytes` | The static data of the record. |
| `encodedLengths` | `PackedCounter` | The encoded lengths of the dynamic data of the record. |
| `dynamicData` | `bytes` | The dynamic data of the record. |

#### Store_SpliceStaticData

Emitted when static data in the store is spliced.

_In static data, data is always overwritten starting at the start position,
so the total length of the data remains the same and no data is shifted._

```solidity
event Store_SpliceStaticData(ResourceId indexed tableId, bytes32[] keyTuple, uint48 start, bytes data);
```

**Parameters**

| Name | Type | Description |
| ---------- | ------------ | --------------------------------------------------------------------- |
| `tableId` | `ResourceId` | The ID of the table where the data is spliced. |
| `keyTuple` | `bytes32[]` | An array representing the key for the record. |
| `start` | `uint48` | The start position in bytes for the splice operation. |
| `data` | `bytes` | The data to write to the static data of the record at the start byte. |

#### Store_SpliceDynamicData

Emitted when dynamic data in the store is spliced.

```solidity
event Store_SpliceDynamicData(
ResourceId indexed tableId,
bytes32[] keyTuple,
uint8 dynamicFieldIndex,
uint48 start,
uint40 deleteCount,
PackedCounter encodedLengths,
bytes data
);
```

**Parameters**

| Name | Type | Description |
| ------------------- | --------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `tableId` | `ResourceId` | The ID of the table where the data is spliced. |
| `keyTuple` | `bytes32[]` | An array representing the composite key for the record. |
| `dynamicFieldIndex` | `uint8` | 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) |
| `start` | `uint48` | The start position in bytes for the splice operation. |
| `deleteCount` | `uint40` | The number of bytes to delete in the splice operation. |
| `encodedLengths` | `PackedCounter` | The encoded lengths of the dynamic data of the record. |
| `data` | `bytes` | The data to insert into the dynamic data of the record at the start byte. |

#### Store_DeleteRecord

Emitted when a record is deleted from the store.

```solidity
event Store_DeleteRecord(ResourceId indexed tableId, bytes32[] keyTuple);
```

**Parameters**

| Name | Type | Description |
| ---------- | ------------ | ------------------------------------------------------- |
| `tableId` | `ResourceId` | The ID of the table where the record is deleted. |
| `keyTuple` | `bytes32[]` | An array representing the composite key for the record. |

## StoreCoreInternal

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/StoreCore.sol)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity >=0.8.24;
import "forge-std/Test.sol";
import { MudTest } from "@latticexyz/world/test/MudTest.t.sol";
import { getKeysWithValue } from "@latticexyz/world-modules/src/modules/keyswithvalue/getKeysWithValue.sol";
import { StoreCore } from "@latticexyz/store/src/StoreCore.sol";
import { IStoreEvents } from "@latticexyz/store/src/IStoreEvents.sol";

import { IWorld } from "../src/codegen/world/IWorld.sol";
import { MessageTable } from "../src/codegen/index.sol";
Expand All @@ -15,7 +15,7 @@ contract ChatNamespacedTest is MudTest {
bytes32[] memory keyTuple;
string memory value = "test";
vm.expectEmit(true, true, true, true);
emit StoreCore.Store_SetRecord(
emit IStoreEvents.Store_SetRecord(
MessageTable._tableId,
keyTuple,
new bytes(0),
Expand Down
70 changes: 8 additions & 62 deletions packages/store/src/StoreCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,68 +17,14 @@ 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 } 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 Emitted when a new record is set in the store.
* @param tableId The ID of the table where the record is set.
* @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.
*/
event Store_SetRecord(
ResourceId indexed tableId,
bytes32[] keyTuple,
bytes staticData,
PackedCounter encodedLengths,
bytes dynamicData
);

/**
* @notice Emitted when static data in the store is spliced.
* @dev In static data, data is always overwritten starting at the start position,
* so the total length of the data remains the same and no data is shifted.
* @param tableId The ID of the table where the data is spliced.
* @param keyTuple An array representing the 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.
*/
event Store_SpliceStaticData(ResourceId indexed tableId, bytes32[] keyTuple, uint48 start, bytes data);

/**
* @notice Emitted when dynamic data in the store is spliced.
* @param tableId The ID of the table where the data is spliced.
* @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 start The start position in bytes for the splice operation.
* @param deleteCount The number of bytes to delete in the splice operation.
* @param encodedLengths The encoded lengths of the dynamic data of the record.
* @param data The data to insert into the dynamic data of the record at the start byte.
*/
event Store_SpliceDynamicData(
ResourceId indexed tableId,
bytes32[] keyTuple,
uint8 dynamicFieldIndex,
uint48 start,
uint40 deleteCount,
PackedCounter encodedLengths,
bytes data
);

/**
* @notice Emitted when a record is deleted from the store.
* @param tableId The ID of the table where the record is deleted.
* @param keyTuple An array representing the composite key for the record.
*/
event Store_DeleteRecord(ResourceId indexed tableId, bytes32[] keyTuple);

/**
* @notice Initialize the store address in StoreSwitch.
* @dev Consumers must call this function in their constructor.
Expand Down Expand Up @@ -363,7 +309,7 @@ library StoreCore {
// Early return if the table is an offchain table
if (tableId.getType() == RESOURCE_OFFCHAIN_TABLE) {
// Emit event to notify indexers
emit Store_SetRecord(tableId, keyTuple, staticData, encodedLengths, dynamicData);
emit IStoreEvents.Store_SetRecord(tableId, keyTuple, staticData, encodedLengths, dynamicData);
return;
}

Expand All @@ -384,7 +330,7 @@ library StoreCore {
}

// Emit event to notify indexers
emit Store_SetRecord(tableId, keyTuple, staticData, encodedLengths, dynamicData);
emit IStoreEvents.Store_SetRecord(tableId, keyTuple, staticData, encodedLengths, dynamicData);

// Store the static data at the static data location
uint256 staticDataLocation = StoreCoreInternal._getStaticDataLocation(tableId, keyTuple);
Expand Down Expand Up @@ -454,7 +400,7 @@ library StoreCore {
// Early return if the table is an offchain table
if (tableId.getType() == RESOURCE_OFFCHAIN_TABLE) {
// Emit event to notify offchain indexers
emit StoreCore.Store_SpliceStaticData({ tableId: tableId, keyTuple: keyTuple, start: start, data: data });
emit IStoreEvents.Store_SpliceStaticData({ tableId: tableId, keyTuple: keyTuple, start: start, data: data });
return;
}

Expand All @@ -475,7 +421,7 @@ library StoreCore {
}

// Emit event to notify offchain indexers
emit StoreCore.Store_SpliceStaticData({ tableId: tableId, keyTuple: keyTuple, start: start, data: data });
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 });
Expand Down Expand Up @@ -652,7 +598,7 @@ library StoreCore {
// Early return if the table is an offchain table
if (tableId.getType() == RESOURCE_OFFCHAIN_TABLE) {
// Emit event to notify indexers
emit Store_DeleteRecord(tableId, keyTuple);
emit IStoreEvents.Store_DeleteRecord(tableId, keyTuple);
return;
}

Expand All @@ -666,7 +612,7 @@ library StoreCore {
}

// Emit event to notify indexers
emit Store_DeleteRecord(tableId, keyTuple);
emit IStoreEvents.Store_DeleteRecord(tableId, keyTuple);

// Delete static data
uint256 staticDataLocation = StoreCoreInternal._getStaticDataLocation(tableId, keyTuple);
Expand Down Expand Up @@ -1090,7 +1036,7 @@ library StoreCoreInternal {
}

// Emit event to notify offchain indexers
emit StoreCore.Store_SpliceDynamicData({
emit IStoreEvents.Store_SpliceDynamicData({
tableId: tableId,
keyTuple: keyTuple,
dynamicFieldIndex: dynamicFieldIndex,
Expand Down
Loading