From 5c45966d4e715eff100a58f56b3c9248e2a7215a Mon Sep 17 00:00:00 2001 From: dk1a Date: Tue, 28 Mar 2023 20:00:37 +0300 Subject: [PATCH] refactor: rename startIndex to startByteIndex --- packages/store/src/IStore.sol | 2 +- packages/store/src/StoreCore.sol | 22 ++++++++++---------- packages/store/src/StoreSwitch.sol | 6 +++--- packages/store/test/StoreCore.t.sol | 6 +++--- packages/world/src/World.sol | 19 +++++++++++------ packages/world/src/interfaces/IWorldCore.sol | 4 ++-- 6 files changed, 33 insertions(+), 26 deletions(-) diff --git a/packages/store/src/IStore.sol b/packages/store/src/IStore.sol index ee3b113452..a05a25577e 100644 --- a/packages/store/src/IStore.sol +++ b/packages/store/src/IStore.sol @@ -33,7 +33,7 @@ interface IStore { uint256 table, bytes32[] calldata key, uint8 schemaIndex, - uint256 startIndex, + uint256 startByteIndex, bytes calldata dataToSet ) external; diff --git a/packages/store/src/StoreCore.sol b/packages/store/src/StoreCore.sol index fc11750d38..bbabfe823d 100644 --- a/packages/store/src/StoreCore.sol +++ b/packages/store/src/StoreCore.sol @@ -305,7 +305,7 @@ library StoreCore { uint256 tableId, bytes32[] memory key, uint8 schemaIndex, - uint256 startIndex, + uint256 startByteIndex, bytes memory dataToSet ) internal { Schema schema = getSchema(tableId); @@ -315,8 +315,8 @@ library StoreCore { } // index must be checked because it could be arbitrarily large // (but dataToSet.length can be unchecked - it won't overflow into another slot due to gas costs and hashed slots) - if (startIndex > type(uint16).max) { - revert StoreCore_DataIndexOverflow(type(uint16).max, startIndex); + if (startByteIndex > type(uint16).max) { + revert StoreCore_DataIndexOverflow(type(uint16).max, startByteIndex); } // TODO add setItem-specific event and hook to avoid the storage read? (https://github.com/latticexyz/mud/issues/444) @@ -324,9 +324,9 @@ library StoreCore { { bytes memory oldData = StoreCoreInternal._getDynamicField(tableId, key, schemaIndex, schema); fullData = abi.encodePacked( - SliceLib.getSubslice(oldData, 0, startIndex).toBytes(), + SliceLib.getSubslice(oldData, 0, startByteIndex).toBytes(), dataToSet, - SliceLib.getSubslice(oldData, startIndex + dataToSet.length, oldData.length).toBytes() + SliceLib.getSubslice(oldData, startByteIndex + dataToSet.length, oldData.length).toBytes() ); } @@ -340,7 +340,7 @@ library StoreCore { hook.onBeforeSetField(tableId, key, schemaIndex, fullData); } - StoreCoreInternal._setDynamicFieldItem(tableId, key, schema, schemaIndex, startIndex, dataToSet); + StoreCoreInternal._setDynamicFieldItem(tableId, key, schema, schemaIndex, startByteIndex, dataToSet); // Call onAfterSetField hooks (after modifying the state) for (uint256 i; i < hooks.length; i++) { @@ -547,13 +547,13 @@ library StoreCoreInternal { bytes32[] memory key, Schema schema, uint8 schemaIndex, - uint256 startIndex, + uint256 startByteIndex, bytes memory dataToSet ) internal { uint8 dynamicSchemaIndex = schemaIndex - schema.numStaticFields(); // Set `dataToSet` at the given index - _setPartialDynamicData(tableId, key, dynamicSchemaIndex, startIndex, dataToSet); + _setPartialDynamicData(tableId, key, dynamicSchemaIndex, startByteIndex, dataToSet); } /************************************************************************ @@ -704,14 +704,14 @@ library StoreCoreInternal { uint256 tableId, bytes32[] memory key, uint8 dynamicSchemaIndex, - uint256 startIndex, + uint256 startByteIndex, bytes memory partialData ) internal { uint256 dynamicDataLocation = _getDynamicDataLocation(tableId, key, dynamicSchemaIndex); // start index is in bytes, whereas storage slots are in 32-byte words - dynamicDataLocation += startIndex / 32; + dynamicDataLocation += startByteIndex / 32; // partial storage slot offset (there is no inherent offset, as each dynamic field starts at its own storage slot) - uint256 offset = startIndex % 32; + uint256 offset = startByteIndex % 32; Storage.store({ storagePointer: dynamicDataLocation, offset: offset, data: partialData }); } } diff --git a/packages/store/src/StoreSwitch.sol b/packages/store/src/StoreSwitch.sol index de7f626659..7945a3f4e3 100644 --- a/packages/store/src/StoreSwitch.sol +++ b/packages/store/src/StoreSwitch.sol @@ -94,13 +94,13 @@ library StoreSwitch { uint256 table, bytes32[] memory key, uint8 fieldIndex, - uint256 startIndex, + uint256 startByteIndex, bytes memory dataToSet ) internal { if (isDelegateCall()) { - StoreCore.updateInField(table, key, fieldIndex, startIndex, dataToSet); + StoreCore.updateInField(table, key, fieldIndex, startByteIndex, dataToSet); } else { - IStore(msg.sender).updateInField(table, key, fieldIndex, startIndex, dataToSet); + IStore(msg.sender).updateInField(table, key, fieldIndex, startByteIndex, dataToSet); } } diff --git a/packages/store/test/StoreCore.t.sol b/packages/store/test/StoreCore.t.sol index 752866b9fa..02ddd6354a 100644 --- a/packages/store/test/StoreCore.t.sol +++ b/packages/store/test/StoreCore.t.sol @@ -51,10 +51,10 @@ contract StoreCoreTest is Test, StoreView { uint256 table, bytes32[] calldata key, uint8 schemaIndex, - uint256 startIndex, + uint256 startByteIndex, bytes calldata dataToSet ) public override { - StoreCore.updateInField(table, key, schemaIndex, startIndex, dataToSet); + StoreCore.updateInField(table, key, schemaIndex, startByteIndex, dataToSet); } // Expose an external deleteRecord function for testing purposes of indexers (see testHooks) @@ -774,7 +774,7 @@ contract StoreCoreTest is Test, StoreView { assertEq(bytes32(StoreCore.getField(table, key, 0)), firstDataBytes); assertEq(StoreCore.getField(table, key, 1), newSecondDataBytes); - // startIndex must not overflow + // startByteIndex must not overflow vm.expectRevert( abi.encodeWithSelector(StoreCore.StoreCore_DataIndexOverflow.selector, type(uint16).max, type(uint32).max) ); diff --git a/packages/world/src/World.sol b/packages/world/src/World.sol index aa9a4153c5..66fa557eaa 100644 --- a/packages/world/src/World.sol +++ b/packages/world/src/World.sol @@ -164,7 +164,7 @@ contract World is Store, IWorldCore, IErrors { } /** - * Update data at `startIndex` of a field in the table at the given namespace and file. + * Update data at `startByteIndex` of a field in the table at the given namespace and file. * Requires the caller to have access to the namespace or file. */ function updateInField( @@ -172,14 +172,14 @@ contract World is Store, IWorldCore, IErrors { bytes16 file, bytes32[] calldata key, uint8 schemaIndex, - uint256 startIndex, + uint256 startByteIndex, bytes calldata dataToSet ) public virtual { // Require access to namespace or file bytes32 resourceSelector = AccessControl.requireAccess(namespace, file, msg.sender); // Update data in the field - StoreCore.updateInField(resourceSelector.toTableId(), key, schemaIndex, startIndex, dataToSet); + StoreCore.updateInField(resourceSelector.toTableId(), key, schemaIndex, startByteIndex, dataToSet); } /** @@ -314,7 +314,7 @@ contract World is Store, IWorldCore, IErrors { } /** - * Update data at `startIndex` of a field in the table at the given tableId. + * Update data at `startByteIndex` of a field in the table at the given tableId. * This overload exists to conform with the `IStore` interface. * The tableId is converted to a resourceSelector, and access is checked based on the namespace or file. */ @@ -322,11 +322,18 @@ contract World is Store, IWorldCore, IErrors { uint256 tableId, bytes32[] calldata key, uint8 schemaIndex, - uint256 startIndex, + uint256 startByteIndex, bytes calldata dataToSet ) public virtual { bytes32 resourceSelector = ResourceSelector.from(tableId); - updateInField(resourceSelector.getNamespace(), resourceSelector.getFile(), key, schemaIndex, startIndex, dataToSet); + updateInField( + resourceSelector.getNamespace(), + resourceSelector.getFile(), + key, + schemaIndex, + startByteIndex, + dataToSet + ); } /** diff --git a/packages/world/src/interfaces/IWorldCore.sol b/packages/world/src/interfaces/IWorldCore.sol index e01a0a12ab..0e49f38256 100644 --- a/packages/world/src/interfaces/IWorldCore.sol +++ b/packages/world/src/interfaces/IWorldCore.sol @@ -84,7 +84,7 @@ interface IWorldCore { ) external; /** - * Update data at `startIndex` of a field in the table at the given namespace and file. + * Update data at `startByteIndex` of a field in the table at the given namespace and file. * Requires the caller to have access to the namespace or file. */ function updateInField( @@ -92,7 +92,7 @@ interface IWorldCore { bytes16 file, bytes32[] calldata key, uint8 schemaIndex, - uint256 startIndex, + uint256 startByteIndex, bytes calldata dataToSet ) external;