Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/__test__/integration/builtins.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
});
});
Expand Down
5 changes: 2 additions & 3 deletions src/__test__/integration/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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'),
});
});
Expand Down Expand Up @@ -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',
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/__test__/integration/legacy.api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
});
});
Expand Down Expand Up @@ -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'),
});
});
Expand Down
60 changes: 53 additions & 7 deletions src/internal/ClientImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AirInterpreter> => {
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;

Expand Down Expand Up @@ -68,12 +119,7 @@ export class ClientImpl implements FluenceClient {
}

async initAirInterpreter(): Promise<void> {
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<void> {
Expand Down
16 changes: 7 additions & 9 deletions src/internal/FluenceConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand All @@ -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<string>) {
pipe(stream.source, decode(), async (source: AsyncIterable<string>) => {
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);
}
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 5 additions & 6 deletions src/internal/RequestFlowBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 3 additions & 1 deletion src/internal/particle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

/**
Expand Down