Skip to content
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

QuickJSPlugin: read external data #33

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion warp-contracts-plugin-quickjs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "warp-contracts-plugin-quickjs",
"version": "1.1.12-external.0",
"version": "1.1.13",
"license": "MIT",
"main": "./build/cjs/index.js",
"module": "./build/esm/index.js",
Expand Down
7 changes: 2 additions & 5 deletions warp-contracts-plugin-quickjs/src/QuickJsHandlerApi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { QuickJSContext, QuickJSHandle, QuickJSRuntime, QuickJSWASMModule } from 'quickjs-emscripten';
import { QuickJSContext, QuickJSHandle, QuickJSRuntime } from 'quickjs-emscripten';
import { AoInteractionResult, InteractionResult, LoggerFactory, QuickJsPluginMessage, Tag } from 'warp-contracts';
import { errorEvalAndDispose } from './utils';

Expand All @@ -19,7 +19,7 @@ export class QuickJsHandlerApi<State> {
if (state) {
this.initState(state);
}
return await this.runContractFunction(message, env);
return this.runContractFunction(message, env);
}

initState(state: State): void {
Expand All @@ -43,9 +43,6 @@ export class QuickJsHandlerApi<State> {
errorEvalAndDispose('interaction', this.logger, this.vm, evalInteractionResult.error);
} else {
const result: AoInteractionResult<Result> = this.disposeResult(evalInteractionResult);
if (this.isSourceAsync) {
this.vm.runtime.executePendingJobs();
}
const state = this.currentState() as State;
return {
Memory: null,
Expand Down
39 changes: 30 additions & 9 deletions warp-contracts-plugin-quickjs/src/eval/QuickJsEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { HandlerBasedContract, LoggerFactory } from 'warp-contracts';
import { PNG } from 'pngjs';
import seedrandom from 'seedrandom';
import { SignedDataPackage } from '@redstone-finance/protocol';
import { timeout } from '../utils';

const TIMEOUT_ASYNC_OPERATIONS = 10000;

export class QuickJsEvaluator {
private readonly logger = LoggerFactory.INST.create('QuickJsEvaluator');
Expand Down Expand Up @@ -64,9 +67,13 @@ export class QuickJsEvaluator {
evalExternal() {
const readExternalHandle = this.vm.newFunction('readExternal', (processIdHandle, actionHandle) => {
const promise = this.vm.newPromise();
this.readExternal(processIdHandle, actionHandle).then((result) => {
promise.resolve(this.vm.newString(JSON.stringify(result) || ''));
});
this.readExternal(processIdHandle, actionHandle)
.then((result) => {
promise.resolve(this.vm.newString(result) || '');
})
.catch((error) => {
promise.reject(this.vm.newString(error?.message) || `External read threw an error.`);
});
promise.settled.then(this.vm.runtime.executePendingJobs);
return promise.handle;
});
Expand All @@ -78,12 +85,26 @@ export class QuickJsEvaluator {
const processId = this.vm.getString(processIdHandle);
const action = this.vm.getString(actionHandle);
const { dryrun } = await import('@permaweb/aoconnect');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why dynamic import?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

already discussed

const readRes = await dryrun({
process: processId,
tags: [{ name: 'Action', value: action }],
data: '1234'
});
return JSON.parse(readRes.Messages[0].Data);
try {
const result = await Promise.race<{
Copy link
Contributor

@ppedziwiatr ppedziwiatr Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the issue with race is that the 'timeout' promise won't be removed even if the 'raced' promise is settled.
https://stackoverflow.com/a/39852372

It is already handled by a timeout implementation in warp sdk - https://github.com/warp-contracts/warp/blob/main/src/utils/utils.ts#L52

Output: any;
Messages: any[];
Spawns: any[];
Error?: any;
}>([
dryrun({
process: processId,
tags: [{ name: 'Action', value: action }],
data: '1234'
}),
timeout(TIMEOUT_ASYNC_OPERATIONS, 'Dryrun operation timed out after 10 seconds')
]);
return result.Messages[0].Data;
} catch (error) {
const errorMessage = (error as Error).message;
this.logger.error(errorMessage);
throw new Error(errorMessage);
}
}

evalRedStone() {
Expand Down
21 changes: 11 additions & 10 deletions warp-contracts-plugin-quickjs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ export const DELIMITER = '|||';
const MEMORY_LIMIT = 1024 * 640;
const MAX_STACK_SIZE = 1024 * 320;
const INTERRUPT_CYCLES = 1024;
const MEMORY_INITIAL_PAGE_SIZE = 64 * 1024;
const MEMORY_MAXIMUM_PAGE_SIZE = 2048;

export class QuickJsPlugin<State> implements WarpPlugin<QuickJsPluginInput, Promise<QuickJsHandlerApi<State>>> {
private readonly logger = LoggerFactory.INST.create('QuickJsPlugin');
Expand All @@ -37,21 +35,21 @@ export class QuickJsPlugin<State> implements WarpPlugin<QuickJsPluginInput, Prom
constructor(private readonly quickJsOptions: QuickJsOptions) {}

async process(input: QuickJsPluginInput): Promise<QuickJsHandlerApi<State>> {
({ QuickJS: this.QuickJS, runtime: this.runtime, vm: this.vm } = await this.configureWasmModule(input.binaryType));
const isSourceAsync = input.contractSource.search('async') > -1;
({ QuickJS: this.QuickJS, runtime: this.runtime, vm: this.vm } = await this.configureWasmModule(isSourceAsync));
this.setRuntimeOptions();

const quickJsEvaluator = new QuickJsEvaluator(this.vm);
const isSourceAsync = input.contractSource.search('async') > -1;
const processDecorator = isSourceAsync ? asyncDecorateProcessFn : decorateProcessFn;
quickJsEvaluator.evalSeedRandom();
quickJsEvaluator.evalGlobalsCode(globals);
quickJsEvaluator.evalHandleFnCode(processDecorator, input.contractSource);
quickJsEvaluator.evalLogging();
quickJsEvaluator.evalPngJS();
quickJsEvaluator.evalRedStone();
quickJsEvaluator.evalExternal();
quickJsEvaluator.dummyPromiseEval();

if (isSourceAsync) {
quickJsEvaluator.evalExternal();
quickJsEvaluator.dummyPromiseEval();
}
return new QuickJsHandlerApi(this.vm, this.runtime, isSourceAsync);
}

Expand All @@ -64,7 +62,7 @@ export class QuickJsPlugin<State> implements WarpPlugin<QuickJsPluginInput, Prom
);
}

async configureWasmModule(binaryType: QuickJsBinaryType): Promise<WasmModuleConfig> {
async configureWasmModule(isSourceAsync: boolean): Promise<WasmModuleConfig> {
try {
const initialWasmMemory = new WebAssembly.Memory({
initial: 256, //*65536
Expand All @@ -79,7 +77,10 @@ export class QuickJsPlugin<State> implements WarpPlugin<QuickJsPluginInput, Prom
const runtime = QuickJS.newRuntime();

const vm = runtime.newContext({
intrinsics: vmIntrinsics
intrinsics: {
...vmIntrinsics,
...(isSourceAsync && { Promise: true })
}
});

return {
Expand Down
12 changes: 10 additions & 2 deletions warp-contracts-plugin-quickjs/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const errorEvalAndDispose = (
vm: QuickJSContext,
evalError: QuickJSHandle
) => {
const error = vm.dump(evalError);
const error = vm.dump(evalError) || vm.getString(evalError);
evalError.dispose();
logger.error(`${evalType} eval failed: ${JSON.stringify(error)}`);

Expand All @@ -23,7 +23,7 @@ export const vmIntrinsics = {
...DefaultIntrinsics,
Date: false,
Proxy: false,
Promise: true,
Promise: false,
MapSet: false,
BigFloat: false,
BigInt: true,
Expand All @@ -50,3 +50,11 @@ export const splitBuffer = (buffer: Buffer, delimiter: string) => {
splitted.push(buffer.slice(start));
return splitted;
};

export const timeout = (ms: number, message: string): Promise<never> => {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error(message));
}, ms);
});
};
83 changes: 83 additions & 0 deletions warp-contracts-plugin-quickjs/tools/quickjs-promise.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { newQuickJSWASMModule, newVariant, RELEASE_SYNC } from 'quickjs-emscripten';
import { dryrun } from '@permaweb/aoconnect';

const mem = new WebAssembly.Memory({
initial: 256, //*65536
maximum: 2048 //*65536
});
const variant = newVariant(RELEASE_SYNC, {
wasmMemory: mem
});
const QuickJS = await newQuickJSWASMModule(variant);

const vm = QuickJS.newContext();

const timeout = (ms, message) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error(message));
}, ms);
});
};

const readExternalHandle = vm.newFunction('readExternal', () => {
const promise = vm.newPromise();
readExternal()
.then((result) => {
promise.resolve(vm.newString(result) || '');
})
.catch((error) => {
promise.reject(vm.newString(error.message) || '');
});
promise.settled.then(vm.runtime.executePendingJobs);
return promise.handle;
});
readExternalHandle.consume((handle) => vm.setProp(vm.global, 'readExternal', handle));

async function readExternal() {
const result = await Promise.race([
fakedryrun({
process: 'iWM-odlyQHopPECpyz465p7ED8lm5d3hyyWtijKhie4',
tags: [{ name: 'Action', value: 'Read-Hollow' }],
data: '1234'
}),
timeout(1000, 'Operation timed out after 10 seconds')
]);
return result.Messages[0].Data;
}
function fakedryrun(config) {
return new Promise((resolve) => {
setTimeout(() => {
resolve({
Messages: [{ Data: JSON.stringify({ ap: { Price: '100', Quantity: '49' } }) }]
});
}, 20000);
});
}

const result = vm.evalCode(`(async () => {
const content = await readExternal()
return content;
})()`);
const promiseHandle = vm.unwrapResult(result);
const resolvedResult = await vm.resolvePromise(promiseHandle);
promiseHandle.dispose();

if (resolvedResult.error) {
// const errorMessage = vm.getString(resolvedResult.error);
const error = vm.dump(resolvedResult) || vm.getString(resolvedResult.error);
resolvedResult.error.dispose();
console.log(`eval failed: ${JSON.stringify(error)}`);

throw new EvalError(`eval failed.`, {
name: error.name,
evalMessage: error.message,
stack: error.stack
});
} else {
const resultValue = resolvedResult.value;
const stringValue = vm.getString(resultValue);
const result = stringValue === 'undefined' ? undefined : JSON.parse(vm.getString(resultValue));
resultValue.dispose();
console.log('Result:', result);
}