From 2c920de7b04bddde0280b1699a9b047182aa712c Mon Sep 17 00:00:00 2001 From: yonada Date: Fri, 1 Mar 2024 12:12:12 +0000 Subject: [PATCH] refactor(store): store core imports store events (#2356) --- .changeset/famous-feet-suffer.md | 5 ++ docs/pages/store/reference/store-core.mdx | 89 ------------------- .../contracts/test/ChatNamespaced.t.sol | 4 +- packages/store/src/StoreCore.sol | 70 ++------------- 4 files changed, 15 insertions(+), 153 deletions(-) create mode 100644 .changeset/famous-feet-suffer.md diff --git a/.changeset/famous-feet-suffer.md b/.changeset/famous-feet-suffer.md new file mode 100644 index 0000000000..e42174b4d9 --- /dev/null +++ b/.changeset/famous-feet-suffer.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/store": patch +--- + +Refactored `StoreCore` to import `IStoreEvents` instead of defining the events twice. diff --git a/docs/pages/store/reference/store-core.mdx b/docs/pages/store/reference/store-core.mdx index 253ec361fc..f5888d234c 100644 --- a/docs/pages/store/reference/store-core.mdx +++ b/docs/pages/store/reference/store-core.mdx @@ -762,95 +762,6 @@ function getDynamicFieldSlice( | -------- | ------- | ------------------------------------ | | `` | `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) diff --git a/examples/minimal/packages/contracts/test/ChatNamespaced.t.sol b/examples/minimal/packages/contracts/test/ChatNamespaced.t.sol index 85f7adfbbe..fe27846c43 100644 --- a/examples/minimal/packages/contracts/test/ChatNamespaced.t.sol +++ b/examples/minimal/packages/contracts/test/ChatNamespaced.t.sol @@ -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"; @@ -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), diff --git a/packages/store/src/StoreCore.sol b/packages/store/src/StoreCore.sol index 4b49743ba9..f8f26a1a6b 100644 --- a/packages/store/src/StoreCore.sol +++ b/packages/store/src/StoreCore.sol @@ -17,6 +17,7 @@ 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 @@ -24,61 +25,6 @@ import { RESOURCE_TABLE, RESOURCE_OFFCHAIN_TABLE } from "./storeResourceTypes.so * @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. @@ -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; } @@ -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); @@ -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; } @@ -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 }); @@ -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; } @@ -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); @@ -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,