From 67a1f919611266f4417909d6ec84f37373729e4f Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 8 Jun 2021 11:08:07 +0300 Subject: [PATCH] Fix issue with avm logs not being displayed (#54) --- package-lock.json | 6 +-- package.json | 2 +- src/__test__/integration/builtins.spec.ts | 2 +- src/__test__/integration/client.spec.ts | 5 +- src/__test__/integration/legacy.api.spec.ts | 4 +- src/internal/ClientImpl.ts | 60 ++++++++++++++++++--- src/internal/FluenceConnection.ts | 16 +++--- src/internal/RequestFlowBuilder.ts | 11 ++-- src/internal/particle.ts | 4 +- 9 files changed, 77 insertions(+), 33 deletions(-) diff --git a/package-lock.json b/package-lock.json index cf957b50..3ffe5147 100644 --- a/package-lock.json +++ b/package-lock.json @@ -435,9 +435,9 @@ } }, "@fluencelabs/avm": { - "version": "0.9.12", - "resolved": "https://registry.npmjs.org/@fluencelabs/avm/-/avm-0.9.12.tgz", - "integrity": "sha512-NCRGJafLa1Zs98IIOIALrPUoOEjesITsB9of2/r+IDgGKunf0NjI2/tqU5pK+37ye/qro5W+FJsHyseDh7zSDQ==", + "version": "0.10.2", + "resolved": "https://registry.npmjs.org/@fluencelabs/avm/-/avm-0.10.2.tgz", + "integrity": "sha512-8FlCx3eTY5Xq5PdE1Fa1rrjrw8oNIdNSnjAxl1MYKL4IEurtRmzQbvFa7s06uX8nU0CRh7UTf9B5xqGOHWGBpA==", "requires": { "base64-js": "1.5.1" }, diff --git a/package.json b/package.json index 8b345032..7891fcd8 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "author": "Fluence Labs", "license": "Apache-2.0", "dependencies": { - "@fluencelabs/avm": "0.9.12", + "@fluencelabs/avm": "0.10.2", "async": "3.2.0", "base64-js": "1.3.1", "bs58": "4.0.1", diff --git a/src/__test__/integration/builtins.spec.ts b/src/__test__/integration/builtins.spec.ts index 3d086293..ec32026f 100644 --- a/src/__test__/integration/builtins.spec.ts +++ b/src/__test__/integration/builtins.spec.ts @@ -85,7 +85,7 @@ describe('Builtins usage suite', () => { let promise = createService(client, 'test_broken_blueprint'); await expect(promise).rejects.toMatchObject({ - error: expect.stringContaining("Blueprint 'test_broken_blueprint' wasn't found"), + msg: expect.stringContaining("Blueprint 'test_broken_blueprint' wasn't found"), instruction: expect.stringContaining('blueprint_id'), }); }); diff --git a/src/__test__/integration/client.spec.ts b/src/__test__/integration/client.spec.ts index 871bf63e..5210120b 100644 --- a/src/__test__/integration/client.spec.ts +++ b/src/__test__/integration/client.spec.ts @@ -2,7 +2,6 @@ import { checkConnection, createClient, FluenceClient } from '../../FluenceClien import Multiaddr from 'multiaddr'; import { nodes } from '../connection'; import { RequestFlowBuilder } from '../../internal/RequestFlowBuilder'; -import { error } from 'loglevel'; let client: FluenceClient; @@ -203,7 +202,7 @@ describe('Typescript usage suite', () => { // assert await expect(promise).rejects.toMatchObject({ - error: expect.stringContaining("Service with id 'incorrect' not found"), + msg: expect.stringContaining("Service with id 'incorrect' not found"), instruction: expect.stringContaining('incorrect'), }); }); @@ -241,7 +240,7 @@ describe('Typescript usage suite', () => { // assert await expect(res).rejects.toMatchObject({ - error: "Local service error: ret_code is 1024, error message is '\"The handler did not set any result. Make sure you are calling the right peer and the handler has been registered. Original request data was: serviceId='peer' fnName='identify' args=''\"'", + msg: "Local service error: ret_code is 1024, error message is '\"The handler did not set any result. Make sure you are calling the right peer and the handler has been registered. Original request data was: serviceId='peer' fnName='identify' args=''\"'", instruction: 'call %init_peer_id% ("peer" "identify") [] res', }); }); diff --git a/src/__test__/integration/legacy.api.spec.ts b/src/__test__/integration/legacy.api.spec.ts index fd3eaa60..d5b42375 100644 --- a/src/__test__/integration/legacy.api.spec.ts +++ b/src/__test__/integration/legacy.api.spec.ts @@ -46,7 +46,7 @@ describe('Legacy api suite', () => { }); await expect(promise).rejects.toMatchObject({ - error: expect.stringContaining("Service with id 'incorrect' not found"), + msg: expect.stringContaining("Service with id 'incorrect' not found"), instruction: expect.stringContaining('incorrect'), }); }); @@ -78,7 +78,7 @@ describe('Legacy api suite', () => { const promise = sendParticleAsFetch<[string]>(client, new Particle(script), 'fn', 'service'); await expect(promise).rejects.toMatchObject({ - error: expect.stringContaining("Service with id 'incorrect' not found"), + msg: expect.stringContaining("Service with id 'incorrect' not found"), instruction: expect.stringContaining('incorrect'), }); }); diff --git a/src/internal/ClientImpl.ts b/src/internal/ClientImpl.ts index de24f83f..55e88294 100644 --- a/src/internal/ClientImpl.ts +++ b/src/internal/ClientImpl.ts @@ -25,9 +25,60 @@ import { CallServiceHandler } from './CallServiceHandler'; import { loadRelayFn, loadVariablesService } from './RequestFlowBuilder'; import { logParticle, Particle } from './particle'; import log from 'loglevel'; -import { AirInterpreter, ParticleHandler, SecurityTetraplet, CallServiceResult } from '@fluencelabs/avm'; +import { + AirInterpreter, + ParticleHandler, + SecurityTetraplet, + CallServiceResult, + LogLevel as AvmLogLevel, +} from '@fluencelabs/avm'; import makeDefaultClientHandler from './defaultClientHandler'; +const createClient = (handler, peerId): Promise => { + let logLevel: AvmLogLevel = 'off'; + switch (log.getLevel()) { + case 0: // 'TRACE' + logLevel = 'trace'; + break; + case 1: // 'DEBUG' + logLevel = 'debug'; + break; + case 2: // 'INFO' + logLevel = 'info'; + break; + case 3: // 'WARN' + logLevel = 'warn'; + break; + case 4: // 'ERROR' + logLevel = 'error'; + break; + case 5: // 'SILENT' + logLevel = 'off'; + break; + } + const logFn = (level: AvmLogLevel, msg: string) => { + switch (level) { + case 'error': + log.error(msg); + break; + + case 'warn': + log.warn(msg); + break; + + case 'info': + log.info(msg); + break; + + case 'debug': + case 'trace': + log.log(msg); + break; + } + }; + return AirInterpreter.create(handler, peerId, logLevel, logFn); +}; + export class ClientImpl implements FluenceClient { readonly selfPeerIdFull: PeerId; @@ -68,12 +119,7 @@ export class ClientImpl implements FluenceClient { } async initAirInterpreter(): Promise { - this.interpreter = await AirInterpreter.create( - this.interpreterCallback.bind(this), - this.selfPeerId, - 'trace', - log.log, - ); + this.interpreter = await createClient(this.interpreterCallback.bind(this), this.selfPeerId); } async connect(multiaddr: string | Multiaddr, options?: FluenceConnectionOptions): Promise { diff --git a/src/internal/FluenceConnection.ts b/src/internal/FluenceConnection.ts index b0f25ddd..1994321f 100644 --- a/src/internal/FluenceConnection.ts +++ b/src/internal/FluenceConnection.ts @@ -20,7 +20,7 @@ import Peer from 'libp2p'; import { decode, encode } from 'it-length-prefixed'; import pipe from 'it-pipe'; import * as log from 'loglevel'; -import { parseParticle, Particle, toPayload } from './particle'; +import { logParticle, parseParticle, Particle, toPayload } from './particle'; import { NOISE } from 'libp2p-noise'; import PeerId from 'peer-id'; import Multiaddr from 'multiaddr'; @@ -114,7 +114,7 @@ export class FluenceConnection { if (this.status === Status.Initializing) { await this.node.start(); - log.trace(`dialing to the node with client's address: ` + this.node.peerId.toB58String()); + log.debug(`dialing to the node with client's address: ` + this.node.peerId.toB58String()); try { await this.node.dial(this.address); @@ -127,15 +127,13 @@ export class FluenceConnection { } } - let _this = this; - this.node.handle([PROTOCOL_NAME], async ({ connection, stream }) => { - pipe(stream.source, decode(), async function (source: AsyncIterable) { + pipe(stream.source, decode(), async (source: AsyncIterable) => { for await (const msg of source) { try { - let particle = parseParticle(msg); - log.trace('Particle is received:', JSON.stringify(particle, undefined, 2)); - _this.handleParticle(particle); + const particle = parseParticle(msg); + logParticle(log.debug, 'Particle is received:', particle); + this.handleParticle(particle); } catch (e) { log.error('error on handling a new incoming message: ' + e); } @@ -165,7 +163,7 @@ export class FluenceConnection { let action = toPayload(particle); let particleStr = JSON.stringify(action); - log.debug('send particle: \n' + JSON.stringify(action, undefined, 2)); + logParticle(log.debug, 'send particle: \n', particle); // create outgoing substream const conn = (await this.node.dialProtocol(this.address, PROTOCOL_NAME)) as { diff --git a/src/internal/RequestFlowBuilder.ts b/src/internal/RequestFlowBuilder.ts index 286e5827..9b949d5b 100644 --- a/src/internal/RequestFlowBuilder.ts +++ b/src/internal/RequestFlowBuilder.ts @@ -195,15 +195,14 @@ export class RequestFlowBuilder { this.configHandler((h, request) => { h.onEvent(xorHandleService, xorHandleFn, (args) => { - let msg; - try { - msg = JSON.parse(args[0]); - } catch (e) { - msg = e; + if (args[0] === undefined) { + log.error( + 'Request flow error handler recieved unexpected argument, value of %last_error% is undefined', + ); } try { - request.raiseError(msg); + request.raiseError(args[0]); } catch (e) { log.error('Error handling script executed with error', e); } diff --git a/src/internal/particle.ts b/src/internal/particle.ts index aa2ae698..279ad73a 100644 --- a/src/internal/particle.ts +++ b/src/internal/particle.ts @@ -32,7 +32,9 @@ export interface Particle { } export const logParticle = (fn: Function, message: string, particle: Particle) => { - fn(message, particle); + const toLog = { ...particle }; + delete toLog.data; + fn(message, toLog); }; /**