-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store,world): more granularity for onchain hooks (#1399)
- Loading branch information
Showing
88 changed files
with
2,881 additions
and
431 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
--- | ||
"@latticexyz/store": major | ||
"@latticexyz/world": major | ||
--- | ||
|
||
- The `onSetRecord` hook is split into `onBeforeSetRecord` and `onAfterSetRecord` and the `onDeleteRecord` hook is split into `onBeforeDeleteRecord` and `onAfterDeleteRecord`. | ||
The purpose of this change is to allow more fine-grained control over the point in the lifecycle at which hooks are executed. | ||
|
||
The previous hooks were executed before modifying data, so they can be replaced with the respective `onBefore` hooks. | ||
|
||
```diff | ||
- function onSetRecord( | ||
+ function onBeforeSetRecord( | ||
bytes32 table, | ||
bytes32[] memory key, | ||
bytes memory data, | ||
Schema valueSchema | ||
) public; | ||
|
||
- function onDeleteRecord( | ||
+ function onBeforeDeleteRecord( | ||
bytes32 table, | ||
bytes32[] memory key, | ||
Schema valueSchema | ||
) public; | ||
``` | ||
|
||
- It is now possible to specify which methods of a hook contract should be called when registering a hook. The purpose of this change is to save gas by avoiding to call no-op hook methods. | ||
|
||
```diff | ||
function registerStoreHook( | ||
bytes32 tableId, | ||
- IStoreHook hookAddress | ||
+ IStoreHook hookAddress, | ||
+ uint8 enabledHooksBitmap | ||
) public; | ||
|
||
function registerSystemHook( | ||
bytes32 systemId, | ||
- ISystemHook hookAddress | ||
+ ISystemHook hookAddress, | ||
+ uint8 enabledHooksBitmap | ||
) public; | ||
``` | ||
|
||
There are `StoreHookLib` and `SystemHookLib` with helper functions to encode the bitmap of enabled hooks. | ||
|
||
```solidity | ||
import { StoreHookLib } from "@latticexyz/store/src/StoreHook.sol"; | ||
uint8 storeHookBitmap = StoreBookLib.encodeBitmap({ | ||
onBeforeSetRecord: true, | ||
onAfterSetRecord: true, | ||
onBeforeSetField: true, | ||
onAfterSetField: true, | ||
onBeforeDeleteRecord: true, | ||
onAfterDeleteRecord: true | ||
}); | ||
``` | ||
|
||
```solidity | ||
import { SystemHookLib } from "@latticexyz/world/src/SystemHook.sol"; | ||
uint8 systemHookBitmap = SystemHookLib.encodeBitmap({ | ||
onBeforeCallSystem: true, | ||
onAfterCallSystem: true | ||
}); | ||
``` | ||
|
||
- The `onSetRecord` hook call for `emitEphemeralRecord` has been removed to save gas and to more clearly distinguish ephemeral tables as offchain tables. |
183 changes: 183 additions & 0 deletions
183
packages/store/abi/EchoSubscriber.sol/EchoSubscriber.abi.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
[ | ||
{ | ||
"anonymous": false, | ||
"inputs": [ | ||
{ | ||
"indexed": false, | ||
"internalType": "bytes", | ||
"name": "", | ||
"type": "bytes" | ||
} | ||
], | ||
"name": "HookCalled", | ||
"type": "event" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "bytes32", | ||
"name": "table", | ||
"type": "bytes32" | ||
}, | ||
{ | ||
"internalType": "bytes32[]", | ||
"name": "key", | ||
"type": "bytes32[]" | ||
}, | ||
{ | ||
"internalType": "Schema", | ||
"name": "valueSchema", | ||
"type": "bytes32" | ||
} | ||
], | ||
"name": "onAfterDeleteRecord", | ||
"outputs": [], | ||
"stateMutability": "nonpayable", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "bytes32", | ||
"name": "table", | ||
"type": "bytes32" | ||
}, | ||
{ | ||
"internalType": "bytes32[]", | ||
"name": "key", | ||
"type": "bytes32[]" | ||
}, | ||
{ | ||
"internalType": "uint8", | ||
"name": "schemaIndex", | ||
"type": "uint8" | ||
}, | ||
{ | ||
"internalType": "bytes", | ||
"name": "data", | ||
"type": "bytes" | ||
}, | ||
{ | ||
"internalType": "Schema", | ||
"name": "valueSchem", | ||
"type": "bytes32" | ||
} | ||
], | ||
"name": "onAfterSetField", | ||
"outputs": [], | ||
"stateMutability": "nonpayable", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "bytes32", | ||
"name": "table", | ||
"type": "bytes32" | ||
}, | ||
{ | ||
"internalType": "bytes32[]", | ||
"name": "key", | ||
"type": "bytes32[]" | ||
}, | ||
{ | ||
"internalType": "bytes", | ||
"name": "data", | ||
"type": "bytes" | ||
}, | ||
{ | ||
"internalType": "Schema", | ||
"name": "valueSchema", | ||
"type": "bytes32" | ||
} | ||
], | ||
"name": "onAfterSetRecord", | ||
"outputs": [], | ||
"stateMutability": "nonpayable", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "bytes32", | ||
"name": "table", | ||
"type": "bytes32" | ||
}, | ||
{ | ||
"internalType": "bytes32[]", | ||
"name": "key", | ||
"type": "bytes32[]" | ||
}, | ||
{ | ||
"internalType": "Schema", | ||
"name": "valueSchema", | ||
"type": "bytes32" | ||
} | ||
], | ||
"name": "onBeforeDeleteRecord", | ||
"outputs": [], | ||
"stateMutability": "nonpayable", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "bytes32", | ||
"name": "table", | ||
"type": "bytes32" | ||
}, | ||
{ | ||
"internalType": "bytes32[]", | ||
"name": "key", | ||
"type": "bytes32[]" | ||
}, | ||
{ | ||
"internalType": "uint8", | ||
"name": "schemaIndex", | ||
"type": "uint8" | ||
}, | ||
{ | ||
"internalType": "bytes", | ||
"name": "data", | ||
"type": "bytes" | ||
}, | ||
{ | ||
"internalType": "Schema", | ||
"name": "valueSchema", | ||
"type": "bytes32" | ||
} | ||
], | ||
"name": "onBeforeSetField", | ||
"outputs": [], | ||
"stateMutability": "nonpayable", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "bytes32", | ||
"name": "table", | ||
"type": "bytes32" | ||
}, | ||
{ | ||
"internalType": "bytes32[]", | ||
"name": "key", | ||
"type": "bytes32[]" | ||
}, | ||
{ | ||
"internalType": "bytes", | ||
"name": "data", | ||
"type": "bytes" | ||
}, | ||
{ | ||
"internalType": "Schema", | ||
"name": "valueSchema", | ||
"type": "bytes32" | ||
} | ||
], | ||
"name": "onBeforeSetRecord", | ||
"outputs": [], | ||
"stateMutability": "nonpayable", | ||
"type": "function" | ||
} | ||
] |
Oops, something went wrong.