-
Notifications
You must be signed in to change notification settings - Fork 9
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { QuickJSContext, QuickJSHandle, QuickJSRuntime, QuickJSWASMModule } from 'quickjs-emscripten'; | ||
import {AoInteractionResult, InteractionResult, LoggerFactory, QuickJsPluginMessage, Tag} from 'warp-contracts'; | ||
import { QuickJSContext, QuickJSHandle, QuickJSRuntime } from 'quickjs-emscripten'; | ||
import { AoInteractionResult, InteractionResult, LoggerFactory, QuickJsPluginMessage, Tag } from 'warp-contracts'; | ||
import { errorEvalAndDispose } from './utils'; | ||
|
||
export class QuickJsHandlerApi<State> { | ||
|
@@ -8,10 +8,14 @@ export class QuickJsHandlerApi<State> { | |
constructor( | ||
private readonly vm: QuickJSContext, | ||
private readonly runtime: QuickJSRuntime, | ||
private readonly quickJS: QuickJSWASMModule, | ||
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. I guess the QuickJSWASMModule import can also now be removed? |
||
private readonly isSourceAsync: boolean | ||
) {} | ||
|
||
async handle<Result>(message: QuickJsPluginMessage, env: ProcessEnv, state?: State): Promise<InteractionResult<State, Result>> { | ||
async handle<Result>( | ||
message: QuickJsPluginMessage, | ||
env: ProcessEnv, | ||
state?: State | ||
): Promise<InteractionResult<State, Result>> { | ||
if (state) { | ||
this.initState(state); | ||
} | ||
|
@@ -27,9 +31,14 @@ export class QuickJsHandlerApi<State> { | |
} | ||
} | ||
|
||
private async runContractFunction<Result>(message: QuickJsPluginMessage, env: ProcessEnv): InteractionResult<State, Result> { | ||
private async runContractFunction<Result>( | ||
message: QuickJsPluginMessage, | ||
env: ProcessEnv | ||
): InteractionResult<State, Result> { | ||
try { | ||
const evalInteractionResult = this.vm.evalCode(`__handleDecorator(${JSON.stringify(message)}, ${JSON.stringify(env)})`); | ||
const evalInteractionResult = this.isSourceAsync | ||
? await this.evalInteractionAsync(message, env) | ||
: this.evalInteractionSync(message, env); | ||
if (evalInteractionResult.error) { | ||
errorEvalAndDispose('interaction', this.logger, this.vm, evalInteractionResult.error); | ||
} else { | ||
|
@@ -69,6 +78,20 @@ export class QuickJsHandlerApi<State> { | |
} | ||
} | ||
|
||
private async evalInteractionAsync(message: QuickJsPluginMessage, env: ProcessEnv) { | ||
const result = this.vm.evalCode(`(async () => { | ||
return await __handleDecorator(${JSON.stringify(message)}, ${JSON.stringify(env)}) | ||
})()`); | ||
const promiseHandle = this.vm.unwrapResult(result); | ||
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. how are erros how handled? e.g. what if dry-run timeouts? |
||
const evalInteractionResult = await this.vm.resolvePromise(promiseHandle); | ||
promiseHandle.dispose(); | ||
return evalInteractionResult; | ||
} | ||
|
||
private evalInteractionSync(message: QuickJsPluginMessage, env: ProcessEnv) { | ||
return this.vm.evalCode(`__handleDecorator(${JSON.stringify(message)}, ${JSON.stringify(env)})`); | ||
} | ||
|
||
currentBinaryState(state: State): Buffer { | ||
const currentState = state || this.currentState(); | ||
return Buffer.from(JSON.stringify(currentState)); | ||
|
@@ -99,19 +122,18 @@ export class QuickJsHandlerApi<State> { | |
resultValue.dispose(); | ||
return result; | ||
} | ||
|
||
} | ||
|
||
// https://cookbook_ao.g8way.io/concepts/processes.html | ||
export type ProcessEnv = { | ||
Process: { | ||
Id: string, | ||
Owner: string, | ||
Tags: { name: string, value: string }[] | ||
}, | ||
Id: string; | ||
Owner: string; | ||
Tags: { name: string; value: string }[]; | ||
}; | ||
Module: { | ||
Id: string, | ||
Owner: string, | ||
Tags: { name: string, value: string }[] | ||
} | ||
} | ||
Id: string; | ||
Owner: string; | ||
Tags: { name: string; value: string }[]; | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import { QuickJSContext } from 'quickjs-emscripten'; | ||
import { LoggerFactory } from 'warp-contracts'; | ||
import { QuickJSContext, QuickJSHandle } from 'quickjs-emscripten'; | ||
import { HandlerBasedContract, LoggerFactory } from 'warp-contracts'; | ||
import { PNG } from 'pngjs'; | ||
import seedrandom from 'seedrandom'; | ||
import { SignedDataPackage } from "@redstone-finance/protocol" | ||
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'); | ||
|
@@ -39,7 +42,7 @@ export class QuickJsEvaluator { | |
const randomHandle = this.vm.newFunction('random', (...args) => { | ||
const nativeArgs = args.map(this.vm.dump); | ||
const message = nativeArgs[0]; | ||
const uniqueValue = nativeArgs.length > 1 ? "" + nativeArgs[1] : '' | ||
const uniqueValue = nativeArgs.length > 1 ? '' + nativeArgs[1] : ''; | ||
const rng = seedrandom(message.Signature + uniqueValue); | ||
return this.vm.newNumber(rng()); | ||
}); | ||
|
@@ -50,6 +53,65 @@ export class QuickJsEvaluator { | |
randomHandle.dispose(); | ||
} | ||
|
||
dummyPromiseEval() { | ||
const dummyPromiseEval = this.vm.newFunction('dummyPromise', () => { | ||
const promise = this.vm.newPromise(); | ||
promise.resolve(this.vm.newString('')); | ||
promise.settled.then(this.vm.runtime.executePendingJobs); | ||
return promise.handle; | ||
}); | ||
this.vm.setProp(this.vm.global, 'dummyPromise', dummyPromiseEval); | ||
dummyPromiseEval.dispose(); | ||
} | ||
|
||
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(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; | ||
}); | ||
this.vm.setProp(this.vm.global, 'readExternal', readExternalHandle); | ||
readExternalHandle.dispose(); | ||
} | ||
|
||
async readExternal(processIdHandle: QuickJSHandle, actionHandle: QuickJSHandle) { | ||
const processId = this.vm.getString(processIdHandle); | ||
const action = this.vm.getString(actionHandle); | ||
const { dryrun } = await import('@permaweb/aoconnect'); | ||
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. why dynamic import? 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. already discussed |
||
const { timeoutId, timeoutPromise } = timeout( | ||
TIMEOUT_ASYNC_OPERATIONS, | ||
'Dryrun operation timed out after 10 seconds' | ||
); | ||
try { | ||
const result = await Promise.race<{ | ||
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. the issue with race is that the 'timeout' promise won't be removed even if the 'raced' promise is settled. 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' | ||
}), | ||
timeoutPromise | ||
]); | ||
if (timeoutId) clearTimeout(timeoutId); | ||
return result.Messages[0].Data; | ||
} catch (error) { | ||
const errorMessage = (error as Error).message; | ||
this.logger.error(errorMessage); | ||
throw new Error(errorMessage); | ||
} | ||
} | ||
|
||
evalRedStone() { | ||
const recoverSignerAddressHandle = this.vm.newFunction('recoverSignerAddress', (...args) => { | ||
const nativeArgs = args.map(this.vm.dump); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?