Pass updated RunParameters to AquaVM (#256)

This commit is contained in:
Pavel 2022-04-25 17:36:42 +03:00 committed by GitHub
parent 3f510e1581
commit f175b31ffd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 12 deletions

View File

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { CallResultsArray, InterpreterResult, CallRequest } from './types'; import { CallResultsArray, InterpreterResult, CallRequest, RunParameters } from './types';
const decoder = new TextDecoder(); const decoder = new TextDecoder();
const encoder = new TextEncoder(); const encoder = new TextEncoder();
@ -30,8 +30,7 @@ const encoder = new TextEncoder();
* @returns AVM call arguments as serialized JSON string * @returns AVM call arguments as serialized JSON string
*/ */
export function serializeAvmArgs( export function serializeAvmArgs(
initPeerId: string, runParams: RunParameters,
currentPeerId: string,
air: string, air: string,
prevData: Uint8Array, prevData: Uint8Array,
data: 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 encoded = encoder.encode(JSON.stringify(callResultsToPass));
const avmArg = JSON.stringify([ const avmArg = JSON.stringify([
@ -57,7 +51,12 @@ export function serializeAvmArgs(
air, air,
Array.from(prevData), Array.from(prevData),
Array.from(data), Array.from(data),
paramsToPass, {
init_peer_id: runParams.initPeerId,
current_peer_id: runParams.currentPeerId,
timestamp: runParams.timestamp,
ttl: runParams.ttl,
},
Array.from(encoded), Array.from(encoded),
]); ]);
@ -149,15 +148,14 @@ type CallToAvm = ((args: string) => Promise<string>) | ((args: string) => string
*/ */
export async function callAvm( export async function callAvm(
fn: CallToAvm, fn: CallToAvm,
initPeerId: string, runParams: RunParameters,
currentPeerId: string,
air: string, air: string,
prevData: Uint8Array, prevData: Uint8Array,
data: Uint8Array, data: Uint8Array,
callResults: CallResultsArray, callResults: CallResultsArray,
): Promise<InterpreterResult> { ): Promise<InterpreterResult> {
try { try {
const avmArg = serializeAvmArgs(initPeerId, currentPeerId, air, prevData, data, callResults); const avmArg = serializeAvmArgs(runParams, air, prevData, data, callResults);
const rawResult = await fn(avmArg); const rawResult = await fn(avmArg);
return deserializeAvmResult(rawResult); return deserializeAvmResult(rawResult);
} catch (e) { } catch (e) {

View File

@ -16,6 +16,32 @@
export type LogLevel = 'info' | 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'off'; 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. * Represents an executed host function result.
*/ */