From f175b31ffda792377886341a18daffbfe915bcf6 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 25 Apr 2022 17:36:42 +0300 Subject: [PATCH] Pass updated RunParameters to AquaVM (#256) --- avm/client/src/avmHelpers.ts | 22 ++++++++++------------ avm/client/src/types.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/avm/client/src/avmHelpers.ts b/avm/client/src/avmHelpers.ts index c2630c03..87070f9a 100644 --- a/avm/client/src/avmHelpers.ts +++ b/avm/client/src/avmHelpers.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { CallResultsArray, InterpreterResult, CallRequest } from './types'; +import { CallResultsArray, InterpreterResult, CallRequest, RunParameters } from './types'; const decoder = new TextDecoder(); const encoder = new TextEncoder(); @@ -30,8 +30,7 @@ const encoder = new TextEncoder(); * @returns AVM call arguments as serialized JSON string */ export function serializeAvmArgs( - initPeerId: string, - currentPeerId: string, + runParams: RunParameters, air: string, prevData: Uint8Array, data: Uint8Array, @@ -45,11 +44,6 @@ export function serializeAvmArgs( }; } - const paramsToPass = { - init_peer_id: initPeerId, - current_peer_id: currentPeerId, - }; - const encoded = encoder.encode(JSON.stringify(callResultsToPass)); const avmArg = JSON.stringify([ @@ -57,7 +51,12 @@ export function serializeAvmArgs( air, Array.from(prevData), Array.from(data), - paramsToPass, + { + init_peer_id: runParams.initPeerId, + current_peer_id: runParams.currentPeerId, + timestamp: runParams.timestamp, + ttl: runParams.ttl, + }, Array.from(encoded), ]); @@ -149,15 +148,14 @@ type CallToAvm = ((args: string) => Promise) | ((args: string) => string */ export async function callAvm( fn: CallToAvm, - initPeerId: string, - currentPeerId: string, + runParams: RunParameters, air: string, prevData: Uint8Array, data: Uint8Array, callResults: CallResultsArray, ): Promise { try { - const avmArg = serializeAvmArgs(initPeerId, currentPeerId, air, prevData, data, callResults); + const avmArg = serializeAvmArgs(runParams, air, prevData, data, callResults); const rawResult = await fn(avmArg); return deserializeAvmResult(rawResult); } catch (e) { diff --git a/avm/client/src/types.ts b/avm/client/src/types.ts index 1563f313..fbdec3d8 100644 --- a/avm/client/src/types.ts +++ b/avm/client/src/types.ts @@ -16,6 +16,32 @@ export type LogLevel = 'info' | 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'off'; +/** + * Parameters that a host side should pass to an interpreter and that necessary for execution. + */ +export interface RunParameters { + /** + * Peer id of a peer that start this particle. + */ + initPeerId: String; + + /** + * Peer id of a current peer. + */ + currentPeerId: String; + + /** + * Unix timestamp from a particle in milliseconds. + * It represents time when this particle was sent from the init peer id. + */ + timestamp: number; + + /** + * TTL set by init peer id in milliseconds. + */ + ttl: number; +} + /** * Represents an executed host function result. */