-
Notifications
You must be signed in to change notification settings - Fork 0
Add operation hook #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -938,6 +938,32 @@ export default sbp('sbp/selectors/register', { | |
| targetState._volatile.pendingKeyRequests.filter((pkr) => pkr?.name !== signingKey.name) | ||
| ) | ||
| }, | ||
| 'chelonia/private/operationHook': function ( | ||
| this: CheloniaContext, | ||
| contractID: string, | ||
| contractName: string, | ||
| message: SPMessage, | ||
| state: ChelContractState, | ||
| atomicIndex?: number | ||
| ) { | ||
| if (this.config.skipActionProcessing) return | ||
| const manifestHash = this.config.contracts.manifests[contractName] | ||
| if (manifestHash) { | ||
|
Comment on lines
+950
to
+951
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This hook will only run on the latest version of the contract, is that intentional? EDIT: I think we should make this behave like contract methods, which are version controlled. We don't want the hook to only run for the latest version, but for all versions, and for it to be consistent for those specific versions. See how the contract methods are implemented for details. |
||
| const hook = `${manifestHash}/${contractName}/hook/${message.opType()}` | ||
| // Check if a hook is defined | ||
| if (sbp('sbp/selectors/fn', hook)) { | ||
| // And call it | ||
| try { | ||
| sbp(hook, { contractID, message, state, atomicIndex }) | ||
| } catch (e) { | ||
| console.error( | ||
| `[chelonia/private/operationHook] Error at operation hook for ${contractID}`, | ||
| e | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| 'chelonia/private/in/processMessage': async function ( | ||
| this: CheloniaContext, | ||
| message: SPMessage, | ||
|
|
@@ -1005,6 +1031,7 @@ export default sbp('sbp/selectors/register', { | |
| throw new Error('Inside OP_ATOMIC: no matching signing key was defined') | ||
| } | ||
| await (opFns[u[0]] as (x: unknown) => Promise<void>)(u[1]) | ||
| sbp('chelonia/private/operationHook', contractID, contractName, message, state, i) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this something that should also be done for if (config[`preOp_${opT}`]) {
processOp = config[`preOp_${opT}`]!(message, state, i) !== false && processOp
}
if (processOp) {
await (opFns[u[0]] as (x: unknown) => Promise<void>)(u[1])
sbp('chelonia/private/operationHook', contractID, contractName, message, state, i)
config.postOp?.(message, state, i)
config[`postOp_${opT}`]?.(message, state, i) // hack to fix syntax highlighting `
}
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you mean to ask whether we should also call postOp/preOp here inside of the OP_ATOMIC, no, I think that's excessive. |
||
| } catch (e_) { | ||
| const e = e_ as Error | ||
| if (e && typeof e === 'object') { | ||
|
|
@@ -1718,26 +1745,26 @@ export default sbp('sbp/selectors/register', { | |
| }, | ||
| [SPMessage.OP_PROTOCOL_UPGRADE]: notImplemented | ||
| } | ||
| const rootState = sbp(this.config.stateSelector) as ChelRootState | ||
| // Having rootState.contracts[contractID] is not enough to determine we | ||
| // have previously synced this contract, as reference counts are also | ||
| // stored there. Hence, we check for the presence of 'type' | ||
| if (!contractName) { | ||
| contractName = | ||
| has(rootState.contracts, contractID) && | ||
| rootState.contracts[contractID] && | ||
| has(rootState.contracts[contractID], 'type') | ||
| ? rootState.contracts[contractID].type | ||
| : opT === SPMessage.OP_CONTRACT | ||
| ? (opV as SPOpContract).type | ||
| : '' | ||
| } | ||
| if (!contractName) { | ||
| throw new Error( | ||
| `Unable to determine the name for a contract and refusing to load it (contract ID was ${contractID} and its manifest hash was ${manifestHash})` | ||
| ) | ||
| } | ||
| if (!this.config.skipActionProcessing && !this.manifestToContract[manifestHash]) { | ||
| const rootState = sbp(this.config.stateSelector) as ChelRootState | ||
| // Having rootState.contracts[contractID] is not enough to determine we | ||
| // have previously synced this contract, as reference counts are also | ||
| // stored there. Hence, we check for the presence of 'type' | ||
| if (!contractName) { | ||
| contractName = | ||
| has(rootState.contracts, contractID) && | ||
| rootState.contracts[contractID] && | ||
| has(rootState.contracts[contractID], 'type') | ||
| ? rootState.contracts[contractID].type | ||
| : opT === SPMessage.OP_CONTRACT | ||
| ? (opV as SPOpContract).type | ||
| : '' | ||
| } | ||
| if (!contractName) { | ||
| throw new Error( | ||
| `Unable to determine the name for a contract and refusing to load it (contract ID was ${contractID} and its manifest hash was ${manifestHash})` | ||
| ) | ||
| } | ||
| await sbp('chelonia/private/loadManifest', contractName, manifestHash) | ||
| } | ||
| let processOp = true | ||
|
|
@@ -1782,6 +1809,7 @@ export default sbp('sbp/selectors/register', { | |
| } | ||
| if (processOp) { | ||
| await (opFns[opT] as (op: unknown) => Promise<void>)(opV) | ||
| sbp('chelonia/private/operationHook', contractID, contractName, message, state) | ||
| config.postOp?.(message, state) | ||
| config[`postOp_${opT}`]?.(message, state) // hack to fix syntax highlighting ` | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.