From e0ffd144092f706d16a9c2b2cde4f12132488b28 Mon Sep 17 00:00:00 2001 From: Asia Date: Mon, 12 Feb 2024 15:40:36 +0100 Subject: [PATCH] feat: quickjs plugin --- src/contract/Contract.ts | 1 + src/contract/HandlerBasedContract.ts | 1 + src/core/WarpPlugin.ts | 3 +- .../modules/impl/HandlerExecutorFactory.ts | 47 ++++++++++++++++++- src/index.ts | 2 + src/utils/types/mutually-exclusive.ts | 2 + 6 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 src/utils/types/mutually-exclusive.ts diff --git a/src/contract/Contract.ts b/src/contract/Contract.ts index e33d6105..3eaca4f0 100644 --- a/src/contract/Contract.ts +++ b/src/contract/Contract.ts @@ -132,6 +132,7 @@ export interface Contract { sortKey: string, interactions: GQLNodeInterface[], signal?: AbortSignal + // przekazać stan (SortKeyCachedResult) ): Promise>>; /** diff --git a/src/contract/HandlerBasedContract.ts b/src/contract/HandlerBasedContract.ts index 556360b6..d826c5b2 100644 --- a/src/contract/HandlerBasedContract.ts +++ b/src/contract/HandlerBasedContract.ts @@ -640,6 +640,7 @@ export class HandlerBasedContract implements Contract { const benchmark = Benchmark.measure(); if (!this.isRoot()) { + // przekazać nasz gotowiec cachedState = this.interactionState().getLessOrEqual(this.txId(), upToSortKey) as SortKeyCacheResult< EvalStateResult >; diff --git a/src/core/WarpPlugin.ts b/src/core/WarpPlugin.ts index 40eead82..3e332b02 100644 --- a/src/core/WarpPlugin.ts +++ b/src/core/WarpPlugin.ts @@ -10,7 +10,8 @@ export const knownWarpPlugins = [ 'deploy', 'contract-blacklist', 'vm2', - 'vrf' + 'vrf', + 'quickjs' ] as const; type WarpPluginPartialType = `smartweave-extension-${string}`; export type WarpKnownPluginType = (typeof knownWarpPlugins)[number]; diff --git a/src/core/modules/impl/HandlerExecutorFactory.ts b/src/core/modules/impl/HandlerExecutorFactory.ts index 1b3904d4..f884601c 100644 --- a/src/core/modules/impl/HandlerExecutorFactory.ts +++ b/src/core/modules/impl/HandlerExecutorFactory.ts @@ -16,6 +16,7 @@ import { isBrowser } from '../../../utils/utils'; import { Buffer } from 'warp-isomorphic'; import { InteractionState } from '../../../contract/states/InteractionState'; import { WarpLogger } from '../../../logging/WarpLogger'; +import { XOR } from 'utils/types/mutually-exclusive'; // 'require' to fix esbuild adding same lib in both cjs and esm format // https://github.com/evanw/esbuild/issues/1950 @@ -208,6 +209,14 @@ export class HandlerExecutorFactory implements ExecutorFactory>('quickjs'); + return await quickJsPlugin.process({ + contractSource: contractDefinition.src, + evaluationOptions, + swGlobal: swGlobal, + contractDefinition + }); } else { const contractFunction = new Function(normalizedSource); const handler = isBrowser() @@ -286,11 +295,11 @@ export interface HandlerApi { export type HandlerResult = { result: Result; state: State; - event: InteractionCompleteEvent; + event?: InteractionCompleteEvent; gasUsed?: number; }; -export type InteractionResult = HandlerResult & { +type WarpInteractionResult = HandlerResult & { type: InteractionResultType; errorMessage?: string; error?: unknown; @@ -298,6 +307,25 @@ export type InteractionResult = HandlerResult & { originalErrorMessages?: Record; }; +export type AoInteractionResult = { + Memory: string; + Error: string; + Messages: { + target: string; + data: string; + anchor: string; + tags: { name: string; value: string }[]; + }[]; + Spawns: { + data: string; + anchor: string; + tags: { name: string; value: string }[]; + }[]; + Output: Result; +}; + +export type InteractionResult = XOR, AoInteractionResult>; + export type InteractionType = 'view' | 'write'; export type ContractInteraction = { @@ -330,3 +358,18 @@ export interface VM2PluginInput { logger: WarpLogger; contractDefinition: ContractDefinition; } + +export interface QuickJsPluginInput { + contractSource: string; + evaluationOptions: EvaluationOptions; + swGlobal: SmartWeaveGlobal; + contractDefinition: ContractDefinition; + wasmMemory?: WebAssembly.Memory; +} + +export interface QuickJsOptions { + memoryLimit?: number; + maxStackSize?: number; + interruptCycles?: number; + timeout?: number; +} diff --git a/src/index.ts b/src/index.ts index 522a81cd..fff92734 100644 --- a/src/index.ts +++ b/src/index.ts @@ -53,6 +53,8 @@ export * from './contract/Signature'; export * from './contract/EvaluationOptionsEvaluator'; export * from './contract/deploy/Source'; export * from './contract/deploy/CreateContract'; +export * from './contract/states/ContractInteractionState'; +export * from './contract/states/InteractionState'; export * from './legacy/gqlResult'; export * from './legacy/smartweave-global'; diff --git a/src/utils/types/mutually-exclusive.ts b/src/utils/types/mutually-exclusive.ts new file mode 100644 index 00000000..a314dbdc --- /dev/null +++ b/src/utils/types/mutually-exclusive.ts @@ -0,0 +1,2 @@ +type Without = { [P in Exclude]?: never }; +export type XOR = T | U extends object ? (Without & U) | (Without & T) : T | U;