From 245662c9b619261f848cc319d1853ba01af7e3e4 Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Fri, 4 Jun 2021 15:12:27 +0300 Subject: [PATCH 1/5] get rid of log.trace --- src/internal/FluenceConnection.ts | 16 +++++++--------- src/internal/particle.ts | 4 +++- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/internal/FluenceConnection.ts b/src/internal/FluenceConnection.ts index b0f25ddd9..de262b3b9 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.info(`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.info, '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/particle.ts b/src/internal/particle.ts index aa2ae6987..279ad73a0 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); }; /** From 5929359c265560b9cd8b9b4e600cf493cb8b19cc Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Fri, 4 Jun 2021 17:49:11 +0300 Subject: [PATCH 2/5] update avm package --- package-lock.json | 6 +++--- package.json | 2 +- src/internal/ClientImpl.ts | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index cf957b507..243c2f5d0 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.1", + "resolved": "https://registry.npmjs.org/@fluencelabs/avm/-/avm-0.10.1.tgz", + "integrity": "sha512-1wSRwdQ0HuHwTg5jZFKp/ubdYJ8VBAQcglgArS1z2Kdof9sGEXBlkoJVcQL0teJiXUzMWuFk1n6Z1sM3FcyPsw==", "requires": { "base64-js": "1.5.1" }, diff --git a/package.json b/package.json index 8b345032a..f79c2ffce 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.1", "async": "3.2.0", "base64-js": "1.3.1", "bs58": "4.0.1", diff --git a/src/internal/ClientImpl.ts b/src/internal/ClientImpl.ts index de24f83fc..46097b291 100644 --- a/src/internal/ClientImpl.ts +++ b/src/internal/ClientImpl.ts @@ -72,7 +72,7 @@ export class ClientImpl implements FluenceClient { this.interpreterCallback.bind(this), this.selfPeerId, 'trace', - log.log, + (level, msg) => console.log(`level: ${level}, msg: ${msg}`), ); } From 388067c555f43cbe8d79f99144210fbbfe286e77 Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Mon, 7 Jun 2021 14:46:47 +0300 Subject: [PATCH 3/5] Correctly translating log levels --- package-lock.json | 6 ++-- package.json | 2 +- src/internal/ClientImpl.ts | 60 +++++++++++++++++++++++++++++++++----- 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index 243c2f5d0..3ffe51476 100644 --- a/package-lock.json +++ b/package-lock.json @@ -435,9 +435,9 @@ } }, "@fluencelabs/avm": { - "version": "0.10.1", - "resolved": "https://registry.npmjs.org/@fluencelabs/avm/-/avm-0.10.1.tgz", - "integrity": "sha512-1wSRwdQ0HuHwTg5jZFKp/ubdYJ8VBAQcglgArS1z2Kdof9sGEXBlkoJVcQL0teJiXUzMWuFk1n6Z1sM3FcyPsw==", + "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 f79c2ffce..7891fcd81 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "author": "Fluence Labs", "license": "Apache-2.0", "dependencies": { - "@fluencelabs/avm": "0.10.1", + "@fluencelabs/avm": "0.10.2", "async": "3.2.0", "base64-js": "1.3.1", "bs58": "4.0.1", diff --git a/src/internal/ClientImpl.ts b/src/internal/ClientImpl.ts index 46097b291..55e882946 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', - (level, msg) => console.log(`level: ${level}, msg: ${msg}`), - ); + this.interpreter = await createClient(this.interpreterCallback.bind(this), this.selfPeerId); } async connect(multiaddr: string | Multiaddr, options?: FluenceConnectionOptions): Promise { From 83325bae72111f3febc551b7a72d89d80fa18e14 Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Mon, 7 Jun 2021 23:10:42 +0300 Subject: [PATCH 4/5] fix tests --- src/__test__/integration/builtins.spec.ts | 2 +- src/__test__/integration/client.spec.ts | 5 ++--- src/__test__/integration/legacy.api.spec.ts | 4 ++-- src/internal/RequestFlowBuilder.ts | 9 +-------- 4 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/__test__/integration/builtins.spec.ts b/src/__test__/integration/builtins.spec.ts index 3d086293d..ec32026fa 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 871bf63ec..5210120bf 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 fd3eaa603..d5b423752 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/RequestFlowBuilder.ts b/src/internal/RequestFlowBuilder.ts index 286e58273..c5950571e 100644 --- a/src/internal/RequestFlowBuilder.ts +++ b/src/internal/RequestFlowBuilder.ts @@ -195,15 +195,8 @@ export class RequestFlowBuilder { this.configHandler((h, request) => { h.onEvent(xorHandleService, xorHandleFn, (args) => { - let msg; try { - msg = JSON.parse(args[0]); - } catch (e) { - msg = e; - } - - try { - request.raiseError(msg); + request.raiseError(args[0]); } catch (e) { log.error('Error handling script executed with error', e); } From 300641e4f396419456f67030306d4cca08fe781a Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Tue, 8 Jun 2021 11:01:35 +0300 Subject: [PATCH 5/5] fix comments --- src/internal/FluenceConnection.ts | 4 ++-- src/internal/RequestFlowBuilder.ts | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/internal/FluenceConnection.ts b/src/internal/FluenceConnection.ts index de262b3b9..1994321f1 100644 --- a/src/internal/FluenceConnection.ts +++ b/src/internal/FluenceConnection.ts @@ -114,7 +114,7 @@ export class FluenceConnection { if (this.status === Status.Initializing) { await this.node.start(); - log.info(`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); @@ -132,7 +132,7 @@ export class FluenceConnection { for await (const msg of source) { try { const particle = parseParticle(msg); - logParticle(log.info, 'Particle is received:', particle); + logParticle(log.debug, 'Particle is received:', particle); this.handleParticle(particle); } catch (e) { log.error('error on handling a new incoming message: ' + e); diff --git a/src/internal/RequestFlowBuilder.ts b/src/internal/RequestFlowBuilder.ts index c5950571e..9b949d5b9 100644 --- a/src/internal/RequestFlowBuilder.ts +++ b/src/internal/RequestFlowBuilder.ts @@ -195,6 +195,12 @@ export class RequestFlowBuilder { this.configHandler((h, request) => { h.onEvent(xorHandleService, xorHandleFn, (args) => { + if (args[0] === undefined) { + log.error( + 'Request flow error handler recieved unexpected argument, value of %last_error% is undefined', + ); + } + try { request.raiseError(args[0]); } catch (e) {