feat: quickjs plugin

This commit is contained in:
Asia
2024-02-12 15:40:36 +01:00
committed by ppedziwiatr
parent 024ed9b35c
commit d36daf78c8
5 changed files with 52 additions and 3 deletions

View File

@@ -646,6 +646,7 @@ export class HandlerBasedContract<State> implements Contract<State> {
const benchmark = Benchmark.measure();
if (!this.isRoot()) {
// przekazać nasz gotowiec
cachedState = this.interactionState().getLessOrEqual(this.txId(), upToSortKey) as SortKeyCacheResult<
EvalStateResult<State>
>;

View File

@@ -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];

View File

@@ -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<HandlerApi<unknow
swGlobal: swGlobal,
contractDefinition
});
} else if (warp.hasPlugin('quickjs')) {
const quickJsPlugin = warp.loadPlugin<QuickJsPluginInput, HandlerApi<State>>('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<State> {
export type HandlerResult<State, Result> = {
result: Result;
state: State;
event: InteractionCompleteEvent;
event?: InteractionCompleteEvent;
gasUsed?: number;
};
export type InteractionResult<State, Result> = HandlerResult<State, Result> & {
type WarpInteractionResult<State, Result> = HandlerResult<State, Result> & {
type: InteractionResultType;
errorMessage?: string;
error?: unknown;
@@ -298,6 +307,25 @@ export type InteractionResult<State, Result> = HandlerResult<State, Result> & {
originalErrorMessages?: Record<string, string>;
};
export type AoInteractionResult<Result> = {
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<State, Result> = XOR<WarpInteractionResult<State, Result>, AoInteractionResult<Result>>;
export type InteractionType = 'view' | 'write';
export type ContractInteraction<Input> = {
@@ -330,3 +358,18 @@ export interface VM2PluginInput {
logger: WarpLogger;
contractDefinition: ContractDefinition<unknown>;
}
export interface QuickJsPluginInput {
contractSource: string;
evaluationOptions: EvaluationOptions;
swGlobal: SmartWeaveGlobal;
contractDefinition: ContractDefinition<unknown>;
wasmMemory?: WebAssembly.Memory;
}
export interface QuickJsOptions {
memoryLimit?: number;
maxStackSize?: number;
interruptCycles?: number;
timeout?: number;
}

View File

@@ -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';

View File

@@ -0,0 +1,2 @@
type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };
export type XOR<T, U> = T | U extends object ? (Without<T, U> & U) | (Without<U, T> & T) : T | U;