Skip to content

Commit

Permalink
add stubs for unimplemented functions
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Sep 18, 2023
1 parent d8e28e2 commit 65d0f63
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 147 deletions.
2 changes: 2 additions & 0 deletions packages/store/src/IStoreHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
64 changes: 64 additions & 0 deletions packages/store/src/StoreHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
16 changes: 8 additions & 8 deletions packages/store/test/EchoSubscriber.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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))
);
Expand All @@ -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))
);
Expand All @@ -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)));
}

Expand All @@ -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)));
}

Expand All @@ -62,7 +62,7 @@ contract EchoSubscriber is StoreHook {
uint40 deleteCount,
bytes memory data,
PackedCounter encodedLengths
) public {
) public override {
emit HookCalled(
abi.encodeCall(
this.onBeforeSpliceDynamicData,
Expand All @@ -79,7 +79,7 @@ contract EchoSubscriber is StoreHook {
uint40 deleteCount,
bytes memory data,
PackedCounter encodedLengths
) public {
) public override {
emit HookCalled(
abi.encodeCall(
this.onAfterSpliceDynamicData,
Expand All @@ -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)));
}
}
41 changes: 8 additions & 33 deletions packages/store/test/MirrorSubscriber.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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);
}
Expand All @@ -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
}
}
24 changes: 12 additions & 12 deletions packages/store/test/RevertSubscriber.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ contract RevertSubscriber is StoreHook {
PackedCounter,
bytes memory,
FieldLayout
) public pure {
) public pure override {
revert("onBeforeSetRecord");
}

Expand All @@ -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");
}
}
6 changes: 3 additions & 3 deletions packages/store/test/setDynamicDataLengthAtIndex.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ 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
uint256 dynamicDataLengthSlot = StoreCoreInternal._getDynamicDataLengthLocation(tableId, keyTuple);
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() });
Expand Down
Loading

0 comments on commit 65d0f63

Please sign in to comment.