-
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
Open
asiaziola
wants to merge
3
commits into
main
Choose a base branch
from
asiaziola/read-external
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'); | ||
|
@@ -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; | ||
}); | ||
|
@@ -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'); | ||
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<{ | ||
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' | ||
}), | ||
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() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
why dynamic import?
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.
already discussed