From 65d0f63189b89ec704e2653b98dcbd4a49af3f0f Mon Sep 17 00:00:00 2001 From: alvrs Date: Mon, 18 Sep 2023 15:17:17 +0100 Subject: [PATCH] add stubs for unimplemented functions --- packages/store/src/IStoreHook.sol | 2 + packages/store/src/StoreHook.sol | 64 ++++++++++++++++++ packages/store/test/EchoSubscriber.sol | 16 ++--- packages/store/test/MirrorSubscriber.sol | 41 +++--------- packages/store/test/RevertSubscriber.sol | 24 +++---- .../test/setDynamicDataLengthAtIndex.sol | 6 +- .../modules/keysintable/KeysInTableHook.sol | 47 +++---------- .../keyswithvalue/KeysWithValueHook.sol | 67 +++++-------------- 8 files changed, 120 insertions(+), 147 deletions(-) diff --git a/packages/store/src/IStoreHook.sol b/packages/store/src/IStoreHook.sol index 76892abdbc..a2be6d40f0 100644 --- a/packages/store/src/IStoreHook.sol +++ b/packages/store/src/IStoreHook.sol @@ -17,6 +17,8 @@ bytes4 constant STORE_HOOK_INTERFACE_ID = IStoreHook.onBeforeSetRecord.selector ERC165_INTERFACE_ID; interface IStoreHook is IERC165 { + error StoreHook_NotImplemented(); + function onBeforeSetRecord( bytes32 tableId, bytes32[] memory keyTuple, diff --git a/packages/store/src/StoreHook.sol b/packages/store/src/StoreHook.sol index 0fff2ce06e..ca854c06fa 100644 --- a/packages/store/src/StoreHook.sol +++ b/packages/store/src/StoreHook.sol @@ -3,10 +3,74 @@ pragma solidity >=0.8.0; import { IStoreHook, STORE_HOOK_INTERFACE_ID } from "./IStoreHook.sol"; import { ERC165_INTERFACE_ID } from "./IERC165.sol"; +import { PackedCounter } from "./PackedCounter.sol"; +import { FieldLayout } from "./FieldLayout.sol"; abstract contract StoreHook is IStoreHook { // ERC-165 supportsInterface (see https://eips.ethereum.org/EIPS/eip-165) function supportsInterface(bytes4 interfaceId) public pure virtual returns (bool) { return interfaceId == STORE_HOOK_INTERFACE_ID || interfaceId == ERC165_INTERFACE_ID; } + + function onBeforeSetRecord( + bytes32, + bytes32[] memory, + bytes memory, + PackedCounter, + bytes memory, + FieldLayout + ) public virtual { + revert StoreHook_NotImplemented(); + } + + function onAfterSetRecord( + bytes32, + bytes32[] memory, + bytes memory, + PackedCounter, + bytes memory, + FieldLayout + ) public virtual { + revert StoreHook_NotImplemented(); + } + + function onBeforeSpliceStaticData(bytes32, bytes32[] memory, uint48, uint40, bytes memory) public virtual { + revert StoreHook_NotImplemented(); + } + + function onAfterSpliceStaticData(bytes32, bytes32[] memory, uint48, uint40, bytes memory) public virtual { + revert StoreHook_NotImplemented(); + } + + function onBeforeSpliceDynamicData( + bytes32, + bytes32[] memory, + uint8, + uint40, + uint40, + bytes memory, + PackedCounter + ) public virtual { + revert StoreHook_NotImplemented(); + } + + function onAfterSpliceDynamicData( + bytes32, + bytes32[] memory, + uint8, + uint40, + uint40, + bytes memory, + PackedCounter + ) public virtual { + revert StoreHook_NotImplemented(); + } + + function onBeforeDeleteRecord(bytes32, bytes32[] memory, FieldLayout) public virtual { + revert StoreHook_NotImplemented(); + } + + function onAfterDeleteRecord(bytes32, bytes32[] memory, FieldLayout) public virtual { + revert StoreHook_NotImplemented(); + } } diff --git a/packages/store/test/EchoSubscriber.sol b/packages/store/test/EchoSubscriber.sol index 2b928c0268..acaf3b50ab 100644 --- a/packages/store/test/EchoSubscriber.sol +++ b/packages/store/test/EchoSubscriber.sol @@ -15,7 +15,7 @@ contract EchoSubscriber is StoreHook { PackedCounter encodedLengths, bytes memory dynamicData, FieldLayout fieldLayout - ) public { + ) public override { emit HookCalled( abi.encodeCall(this.onBeforeSetRecord, (tableId, keyTuple, staticData, encodedLengths, dynamicData, fieldLayout)) ); @@ -28,7 +28,7 @@ contract EchoSubscriber is StoreHook { PackedCounter encodedLengths, bytes memory dynamicData, FieldLayout fieldLayout - ) public { + ) public override { emit HookCalled( abi.encodeCall(this.onAfterSetRecord, (tableId, keyTuple, staticData, encodedLengths, dynamicData, fieldLayout)) ); @@ -40,7 +40,7 @@ contract EchoSubscriber is StoreHook { uint48 start, uint40 deleteCount, bytes memory data - ) public { + ) public override { emit HookCalled(abi.encodeCall(this.onBeforeSpliceStaticData, (tableId, keyTuple, start, deleteCount, data))); } @@ -50,7 +50,7 @@ contract EchoSubscriber is StoreHook { uint48 start, uint40 deleteCount, bytes memory data - ) public { + ) public override { emit HookCalled(abi.encodeCall(this.onAfterSpliceStaticData, (tableId, keyTuple, start, deleteCount, data))); } @@ -62,7 +62,7 @@ contract EchoSubscriber is StoreHook { uint40 deleteCount, bytes memory data, PackedCounter encodedLengths - ) public { + ) public override { emit HookCalled( abi.encodeCall( this.onBeforeSpliceDynamicData, @@ -79,7 +79,7 @@ contract EchoSubscriber is StoreHook { uint40 deleteCount, bytes memory data, PackedCounter encodedLengths - ) public { + ) public override { emit HookCalled( abi.encodeCall( this.onAfterSpliceDynamicData, @@ -88,11 +88,11 @@ contract EchoSubscriber is StoreHook { ); } - function onBeforeDeleteRecord(bytes32 tableId, bytes32[] memory keyTuple, FieldLayout fieldLayout) public { + function onBeforeDeleteRecord(bytes32 tableId, bytes32[] memory keyTuple, FieldLayout fieldLayout) public override { emit HookCalled(abi.encodeCall(this.onBeforeDeleteRecord, (tableId, keyTuple, fieldLayout))); } - function onAfterDeleteRecord(bytes32 tableId, bytes32[] memory keyTuple, FieldLayout fieldLayout) public { + function onAfterDeleteRecord(bytes32 tableId, bytes32[] memory keyTuple, FieldLayout fieldLayout) public override { emit HookCalled(abi.encodeCall(this.onAfterDeleteRecord, (tableId, keyTuple, fieldLayout))); } } diff --git a/packages/store/test/MirrorSubscriber.sol b/packages/store/test/MirrorSubscriber.sol index 3c16493b8b..844ebc8b68 100644 --- a/packages/store/test/MirrorSubscriber.sol +++ b/packages/store/test/MirrorSubscriber.sol @@ -11,7 +11,7 @@ import { Schema } from "../src/Schema.sol"; bytes32 constant indexerTableId = keccak256("indexer.tableId"); contract MirrorSubscriber is StoreHook { - bytes32 _tableId; + bytes32 public _tableId; constructor( bytes32 tableId, @@ -28,47 +28,26 @@ contract MirrorSubscriber is StoreHook { function onBeforeSetRecord( bytes32 tableId, bytes32[] memory keyTuple, - bytes calldata staticData, + bytes memory staticData, PackedCounter encodedLengths, - bytes calldata dynamicData, + bytes memory dynamicData, FieldLayout fieldLayout - ) public { + ) public override { if (tableId != _tableId) revert("invalid table"); StoreSwitch.setRecord(indexerTableId, keyTuple, staticData, encodedLengths, dynamicData, fieldLayout); } - function onAfterSetRecord( - bytes32 tableId, - bytes32[] memory keyTuple, - bytes calldata staticData, - PackedCounter encodedLengths, - bytes calldata dynamicData, - FieldLayout fieldLayout - ) public { - // NOOP - } - function onBeforeSpliceStaticData( bytes32 tableId, bytes32[] memory keyTuple, uint48 start, uint40 deleteCount, bytes memory data - ) public { + ) public override { if (tableId != _tableId) revert("invalid tableId"); StoreSwitch.spliceStaticData(indexerTableId, keyTuple, start, deleteCount, data); } - function onAfterSpliceStaticData( - bytes32 tableId, - bytes32[] memory keyTuple, - uint48 start, - uint40 deleteCount, - bytes memory data - ) public { - // NOOP - } - function onBeforeSpliceDynamicData( bytes32 tableId, bytes32[] memory keyTuple, @@ -77,7 +56,7 @@ contract MirrorSubscriber is StoreHook { uint40 deleteCount, bytes memory data, PackedCounter - ) public { + ) public override { if (tableId != _tableId) revert("invalid tableId"); StoreSwitch.spliceDynamicData(indexerTableId, keyTuple, dynamicFieldIndex, startWithinField, deleteCount, data); } @@ -90,17 +69,13 @@ contract MirrorSubscriber is StoreHook { uint40 deleteCount, bytes memory data, PackedCounter - ) public { + ) public override { if (tableId != _tableId) revert("invalid tableId"); StoreSwitch.spliceDynamicData(indexerTableId, keyTuple, dynamicFieldIndex, startWithinField, deleteCount, data); } - function onBeforeDeleteRecord(bytes32 tableId, bytes32[] memory keyTuple, FieldLayout fieldLayout) public { + function onBeforeDeleteRecord(bytes32 tableId, bytes32[] memory keyTuple, FieldLayout fieldLayout) public overide { if (tableId != tableId) revert("invalid tableId"); StoreSwitch.deleteRecord(indexerTableId, keyTuple, fieldLayout); } - - function onAfterDeleteRecord(bytes32 tableId, bytes32[] memory keyTuple, FieldLayout fieldLayout) public { - // NOOP - } } diff --git a/packages/store/test/RevertSubscriber.sol b/packages/store/test/RevertSubscriber.sol index 988e15bd14..7d706405d4 100644 --- a/packages/store/test/RevertSubscriber.sol +++ b/packages/store/test/RevertSubscriber.sol @@ -13,7 +13,7 @@ contract RevertSubscriber is StoreHook { PackedCounter, bytes memory, FieldLayout - ) public pure { + ) public pure override { revert("onBeforeSetRecord"); } @@ -24,47 +24,47 @@ contract RevertSubscriber is StoreHook { PackedCounter, bytes memory, FieldLayout - ) public pure { + ) public pure override { revert("onAfterSetRecord"); } - function onBeforeSpliceStaticData(bytes32, bytes32[] calldata, uint48, uint40, bytes calldata) public pure { + function onBeforeSpliceStaticData(bytes32, bytes32[] memory, uint48, uint40, bytes memory) public pure override { revert("onBeforeSpliceStaticData"); } - function onAfterSpliceStaticData(bytes32, bytes32[] calldata, uint48, uint40, bytes calldata) public pure { + function onAfterSpliceStaticData(bytes32, bytes32[] memory, uint48, uint40, bytes memory) public pure override { revert("onAfterSpliceStaticData"); } function onBeforeSpliceDynamicData( bytes32, - bytes32[] calldata, + bytes32[] memory, uint8, uint40, uint40, - bytes calldata, + bytes memory, PackedCounter - ) public pure { + ) public pure override { revert("onBeforeSpliceDynamicData"); } function onAfterSpliceDynamicData( bytes32, - bytes32[] calldata, + bytes32[] memory, uint8, uint40, uint40, - bytes calldata, + bytes memory, PackedCounter - ) public pure { + ) public pure override { revert("onAfterSpliceDynamicData"); } - function onBeforeDeleteRecord(bytes32, bytes32[] memory, FieldLayout) public pure { + function onBeforeDeleteRecord(bytes32, bytes32[] memory, FieldLayout) public pure override { revert("onBeforeDeleteRecord"); } - function onAfterDeleteRecord(bytes32, bytes32[] memory, FieldLayout) public pure { + function onAfterDeleteRecord(bytes32, bytes32[] memory, FieldLayout) public pure override { revert("onAfterDeleteRecord"); } } diff --git a/packages/store/test/setDynamicDataLengthAtIndex.sol b/packages/store/test/setDynamicDataLengthAtIndex.sol index f250974010..10bb2c9872 100644 --- a/packages/store/test/setDynamicDataLengthAtIndex.sol +++ b/packages/store/test/setDynamicDataLengthAtIndex.sol @@ -6,12 +6,12 @@ import { StoreCoreInternal } from "../src/StoreCore.sol"; import { Storage } from "../src/Storage.sol"; /** - * Tes helper function to set the length of the dynamic data (in bytes) for the given value field layout and index + * Test helper function to set the length of the dynamic data (in bytes) for the given value field layout and index */ function setDynamicDataLengthAtIndex( bytes32 tableId, bytes32[] memory keyTuple, - uint8 dynamicSchemaIndex, // fieldIndex - numStaticFields + uint8 dynamicFieldIndex, // fieldIndex - numStaticFields uint256 newLengthAtIndex ) { // Load dynamic data length from storage @@ -19,7 +19,7 @@ function setDynamicDataLengthAtIndex( PackedCounter encodedLengths = PackedCounter.wrap(Storage.load({ storagePointer: dynamicDataLengthSlot })); // Update the encoded lengths - encodedLengths = encodedLengths.setAtIndex(dynamicSchemaIndex, newLengthAtIndex); + encodedLengths = encodedLengths.setAtIndex(dynamicFieldIndex, newLengthAtIndex); // Set the new lengths Storage.store({ storagePointer: dynamicDataLengthSlot, data: encodedLengths.unwrap() }); diff --git a/packages/world/src/modules/keysintable/KeysInTableHook.sol b/packages/world/src/modules/keysintable/KeysInTableHook.sol index 1b54c852a8..69113cc5a8 100644 --- a/packages/world/src/modules/keysintable/KeysInTableHook.sol +++ b/packages/world/src/modules/keysintable/KeysInTableHook.sol @@ -48,60 +48,33 @@ contract KeysInTableHook is StoreHook { PackedCounter, bytes memory, FieldLayout - ) public { + ) public override { handleSet(tableId, keyTuple); } - function onAfterSetRecord( - bytes32 tableId, - bytes32[] memory keyTuple, - bytes memory, - PackedCounter, - bytes memory, - FieldLayout - ) public { - // NOOP - } - - function onBeforeSpliceStaticData(bytes32, bytes32[] calldata, uint48, uint40, bytes calldata) public pure { - // NOOP - } - function onAfterSpliceStaticData( bytes32 tableId, - bytes32[] calldata keyTuple, + bytes32[] memory keyTuple, uint48, uint40, - bytes calldata - ) public { + bytes memory + ) public override { handleSet(tableId, keyTuple); } - function onBeforeSpliceDynamicData( - bytes32, - bytes32[] calldata, - uint8, - uint40, - uint40, - bytes calldata, - PackedCounter - ) public pure { - // NOOP - } - function onAfterSpliceDynamicData( bytes32 tableId, - bytes32[] calldata keyTuple, + bytes32[] memory keyTuple, uint8, uint40, uint40, - bytes calldata, + bytes memory, PackedCounter - ) public { + ) public override { handleSet(tableId, keyTuple); } - function onBeforeDeleteRecord(bytes32 tableId, bytes32[] memory keyTuple, FieldLayout) public { + function onBeforeDeleteRecord(bytes32 tableId, bytes32[] memory keyTuple, FieldLayout) public override { bytes32 keysHash = keccak256(abi.encode(keyTuple)); (bool has, uint40 index) = UsedKeysIndex.get(tableId, keysHash); @@ -165,8 +138,4 @@ contract KeysInTableHook is StoreHook { } } } - - function onAfterDeleteRecord(bytes32 tableId, bytes32[] memory keyTuple, FieldLayout fieldLayout) public { - // NOOP - } } diff --git a/packages/world/src/modules/keyswithvalue/KeysWithValueHook.sol b/packages/world/src/modules/keyswithvalue/KeysWithValueHook.sol index 3f500725a3..f61308b2fc 100644 --- a/packages/world/src/modules/keyswithvalue/KeysWithValueHook.sol +++ b/packages/world/src/modules/keyswithvalue/KeysWithValueHook.sol @@ -38,7 +38,7 @@ contract KeysWithValueHook is StoreHook { PackedCounter encodedLengths, bytes memory dynamicData, FieldLayout fieldLayout - ) public { + ) public override { bytes32 targetTableId = getTargetTableSelector(MODULE_NAMESPACE, sourceTableId); // Get the previous value @@ -57,24 +57,13 @@ contract KeysWithValueHook is StoreHook { KeysWithValue.push(targetTableId, keccak256(data), keyTuple[0]); } - function onAfterSetRecord( - bytes32 sourceTableId, - bytes32[] memory keyTuple, - bytes memory staticData, - PackedCounter encodedLengths, - bytes memory dynamicData, - FieldLayout fieldLayout - ) public { - // NOOP - } - function onBeforeSpliceStaticData( bytes32 sourceTableId, - bytes32[] calldata keyTuple, + bytes32[] memory keyTuple, uint48, uint40, - bytes calldata - ) public { + bytes memory + ) public override { // Remove the key from the list of keys with the previous value FieldLayout fieldLayout = FieldLayout.wrap(Tables.getFieldLayout(sourceTableId)); bytes32 previousValue = keccak256(_world().getRecord(sourceTableId, keyTuple, fieldLayout)); @@ -84,11 +73,11 @@ contract KeysWithValueHook is StoreHook { function onAfterSpliceStaticData( bytes32 sourceTableId, - bytes32[] calldata keyTuple, + bytes32[] memory keyTuple, uint48, uint40, - bytes calldata - ) public { + bytes memory + ) public override { // Add the key to the list of keys with the new value FieldLayout fieldLayout = FieldLayout.wrap(Tables.getFieldLayout(sourceTableId)); bytes32 newValue = keccak256(_world().getRecord(sourceTableId, keyTuple, fieldLayout)); @@ -98,13 +87,13 @@ contract KeysWithValueHook is StoreHook { function onBeforeSpliceDynamicData( bytes32 sourceTableId, - bytes32[] calldata keyTuple, + bytes32[] memory keyTuple, uint8, uint40, uint40, - bytes calldata, + bytes memory, PackedCounter - ) public { + ) public override { // Remove the key from the list of keys with the previous value FieldLayout fieldLayout = FieldLayout.wrap(Tables.getFieldLayout(sourceTableId)); bytes32 previousValue = keccak256(_world().getRecord(sourceTableId, keyTuple, fieldLayout)); @@ -114,13 +103,13 @@ contract KeysWithValueHook is StoreHook { function onAfterSpliceDynamicData( bytes32 sourceTableId, - bytes32[] calldata keyTuple, + bytes32[] memory keyTuple, uint8, uint40, uint40, - bytes calldata, + bytes memory, PackedCounter - ) public { + ) public override { // Add the key to the list of keys with the new value FieldLayout fieldLayout = FieldLayout.wrap(Tables.getFieldLayout(sourceTableId)); bytes32 newValue = keccak256(_world().getRecord(sourceTableId, keyTuple, fieldLayout)); @@ -128,43 +117,17 @@ contract KeysWithValueHook is StoreHook { KeysWithValue.push(targetTableId, newValue, keyTuple[0]); } - function onBeforeSetField( + function onBeforeDeleteRecord( bytes32 sourceTableId, bytes32[] memory keyTuple, - uint8, - bytes memory, FieldLayout fieldLayout - ) public { + ) public override { // Remove the key from the list of keys with the previous value bytes32 previousValue = keccak256(_world().getRecord(sourceTableId, keyTuple, fieldLayout)); bytes32 targetTableId = getTargetTableSelector(MODULE_NAMESPACE, sourceTableId); _removeKeyFromList(targetTableId, keyTuple[0], previousValue); } - function onAfterSetField( - bytes32 sourceTableId, - bytes32[] memory keyTuple, - uint8, - bytes memory, - FieldLayout fieldLayout - ) public { - // Add the key to the list of keys with the new value - bytes32 newValue = keccak256(_world().getRecord(sourceTableId, keyTuple, fieldLayout)); - bytes32 targetTableId = getTargetTableSelector(MODULE_NAMESPACE, sourceTableId); - KeysWithValue.push(targetTableId, newValue, keyTuple[0]); - } - - function onBeforeDeleteRecord(bytes32 sourceTableId, bytes32[] memory keyTuple, FieldLayout fieldLayout) public { - // Remove the key from the list of keys with the previous value - bytes32 previousValue = keccak256(_world().getRecord(sourceTableId, keyTuple, fieldLayout)); - bytes32 targetTableId = getTargetTableSelector(MODULE_NAMESPACE, sourceTableId); - _removeKeyFromList(targetTableId, keyTuple[0], previousValue); - } - - function onAfterDeleteRecord(bytes32 sourceTableId, bytes32[] memory keyTuple, FieldLayout fieldLayout) public { - // NOOP - } - function _removeKeyFromList(bytes32 targetTableId, bytes32 key, bytes32 valueHash) internal { // Get the keys with the previous value excluding the current key bytes32[] memory keysWithPreviousValue = KeysWithValue.get(targetTableId, valueHash).filter(key);