From 6156dec1fa0e192c4ad4ddd0b61808e44db60123 Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Mon, 26 Apr 2021 20:21:06 +0300 Subject: [PATCH 1/6] Replace the error when particle is being sent to some peer other than the relay with a warning with detailed information --- src/internal/RequestFlow.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/internal/RequestFlow.ts b/src/internal/RequestFlow.ts index f77f218da..85a03c81a 100644 --- a/src/internal/RequestFlow.ts +++ b/src/internal/RequestFlow.ts @@ -66,6 +66,7 @@ export class RequestFlow { const interpreterOutcome = this.runInterpreter(interpreter); log.debug('inner interpreter outcome:', { + particleId: this.getParticle()?.id, ret_code: interpreterOutcome.ret_code, error_message: interpreterOutcome.error_message, next_peer_pks: interpreterOutcome.next_peer_pks, @@ -86,16 +87,12 @@ export class RequestFlow { // we only expect a single possible peer id to send particle further if (nextPeers.length > 1) { - throw new Error( - 'Particle is expected to be sent to only the single peer (relay which client is connected to)', - ); + this.warnIfNotSinglePeerAndRelay(nextPeers); } // this peer id must be the relay, the client is connected to if (!relayPeerId || nextPeers[0] !== relayPeerId) { - throw new Error( - 'Particle is expected to be sent to only the single peer (relay which client is connected to)', - ); + this.warnIfNotSinglePeerAndRelay(nextPeers); } if (!connection) { @@ -105,6 +102,14 @@ export class RequestFlow { this.sendIntoConnection(connection); } + private warnIfNotSinglePeerAndRelay(nextPeers: PeerIdB58[]) { + log.warn('Particle is expected to be sent to only the single peer (relay which client is connected to', { + particleId: this.getParticle()?.id, + nextPeers: nextPeers, + currentRelayId: this.relayPeerId, + }); + } + async initState(peerId: PeerId): Promise { const id = this.id; let currentTime = Date.now(); From ca45904cf6e22890d52a5ab123c8e1fd3a993d4c Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Mon, 26 Apr 2021 21:48:44 +0300 Subject: [PATCH 2/6] Throwing custom error in case of version incompatibility --- src/internal/FluenceConnection.ts | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/internal/FluenceConnection.ts b/src/internal/FluenceConnection.ts index 7b85d618d..2ef3d78d9 100644 --- a/src/internal/FluenceConnection.ts +++ b/src/internal/FluenceConnection.ts @@ -54,6 +54,12 @@ export interface FluenceConnectionOptions { dialTimeout?: number; } +export class VersionIncompatibleError extends Error { + constructor() { + super('Version of JS SDK is incompatible with '); + } +} + export class FluenceConnection { private readonly selfPeerId: PeerId; private node: Peer; @@ -89,7 +95,7 @@ export class FluenceConnection { private async createPeer(options?: FluenceConnectionOptions) { const peerInfo = this.selfPeerId; - const transportKey = Websockets.prototype[Symbol.toStringTag] + const transportKey = Websockets.prototype[Symbol.toStringTag]; this.node = await Peer.create({ peerId: peerInfo, modules: { @@ -100,9 +106,9 @@ export class FluenceConnection { config: { transport: { [transportKey]: { - filter: allow_all - } - } + filter: allow_all, + }, + }, }, dialer: { timeout: options?.dialTimeout, @@ -116,7 +122,14 @@ export class FluenceConnection { log.trace(`dialing to the node with client's address: ` + this.node.peerId.toB58String()); - await this.node.dial(this.address); + try { + await this.node.dial(this.address); + } catch (e) { + if (e.name === 'AggregateError' && e._errors[0].code === 'ERR_ENCRYPTION_FAILED') { + console.log(e); + throw new VersionIncompatibleError(); + } + } let _this = this; From 824778574498c67823596ee28083931b0354eae2 Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Tue, 27 Apr 2021 12:07:40 +0300 Subject: [PATCH 3/6] fixes --- src/internal/FluenceConnection.ts | 3 +-- src/internal/RequestFlow.ts | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/internal/FluenceConnection.ts b/src/internal/FluenceConnection.ts index 2ef3d78d9..35efc260d 100644 --- a/src/internal/FluenceConnection.ts +++ b/src/internal/FluenceConnection.ts @@ -56,7 +56,7 @@ export interface FluenceConnectionOptions { export class VersionIncompatibleError extends Error { constructor() { - super('Version of JS SDK is incompatible with '); + super('Current version of JS SDK is incompatible with the network. Please update the JS SDK'); } } @@ -126,7 +126,6 @@ export class FluenceConnection { await this.node.dial(this.address); } catch (e) { if (e.name === 'AggregateError' && e._errors[0].code === 'ERR_ENCRYPTION_FAILED') { - console.log(e); throw new VersionIncompatibleError(); } } diff --git a/src/internal/RequestFlow.ts b/src/internal/RequestFlow.ts index 85a03c81a..b3a006d55 100644 --- a/src/internal/RequestFlow.ts +++ b/src/internal/RequestFlow.ts @@ -87,12 +87,12 @@ export class RequestFlow { // we only expect a single possible peer id to send particle further if (nextPeers.length > 1) { - this.warnIfNotSinglePeerAndRelay(nextPeers); + this.throwIfNotSinglePeerAndRelay(nextPeers); } // this peer id must be the relay, the client is connected to if (!relayPeerId || nextPeers[0] !== relayPeerId) { - this.warnIfNotSinglePeerAndRelay(nextPeers); + this.throwIfNotSinglePeerAndRelay(nextPeers); } if (!connection) { @@ -102,12 +102,14 @@ export class RequestFlow { this.sendIntoConnection(connection); } - private warnIfNotSinglePeerAndRelay(nextPeers: PeerIdB58[]) { - log.warn('Particle is expected to be sent to only the single peer (relay which client is connected to', { - particleId: this.getParticle()?.id, - nextPeers: nextPeers, - currentRelayId: this.relayPeerId, - }); + private throwIfNotSinglePeerAndRelay(nextPeers: PeerIdB58[]) { + throw new Error( + `Particle is expected to be sent to only the single peer (relay which client is connected to). +particle id: ${this.getParticle()?.id} +next peers: ${nextPeers.join(' ')} +relay peer id: ${this.relayPeerId} +`, + ); } async initState(peerId: PeerId): Promise { From 91af80759288b1629638155b2f94095ca2d2937c Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Tue, 27 Apr 2021 12:36:03 +0300 Subject: [PATCH 4/6] add a test --- src/__test__/integration/client.spec.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/__test__/integration/client.spec.ts b/src/__test__/integration/client.spec.ts index 12b5f7ea4..7359f2fa1 100644 --- a/src/__test__/integration/client.spec.ts +++ b/src/__test__/integration/client.spec.ts @@ -246,6 +246,27 @@ describe('Typescript usage suite', () => { instruction: 'call %init_peer_id% ("peer" "identify") [] res', }); }); + + it('Should throw correct error when the client tries to send a particle not to the relay', async () => { + // arrange + client = await createClient(); + + // act + const [req, promise] = new RequestFlowBuilder() + .withRawScript( + ` + (call "incorrect_peer_id" ("any" "service") []) + `, + ) + .buildWithErrorHandling(); + + await client.initiateFlow(req); + + // assert + await expect(promise).rejects.toMatch( + 'Particle is expected to be sent to only the single peer (relay which client is connected to)', + ); + }); }); async function callIdentifyOnInitPeerId(client: FluenceClient): Promise { From 07d9d243a84b31821f6ce0f591e8424245070f9a Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Tue, 27 Apr 2021 12:59:03 +0300 Subject: [PATCH 5/6] fix test --- src/internal/ClientImpl.ts | 4 ++-- src/internal/RequestFlow.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/internal/ClientImpl.ts b/src/internal/ClientImpl.ts index 3bd6241dd..4154e6c42 100644 --- a/src/internal/ClientImpl.ts +++ b/src/internal/ClientImpl.ts @@ -112,7 +112,7 @@ export class ClientImpl implements FluenceClient { request.handler.combineWith(this.aquaCallHandler); this.requests.set(request.id, request); - await this.processRequest(request); + this.processRequest(request); } async executeIncomingParticle(particle: Particle) { @@ -130,7 +130,7 @@ export class ClientImpl implements FluenceClient { await this.processRequest(request); } - private async processRequest(request: RequestFlow): Promise { + private processRequest(request: RequestFlow) { try { this.currentRequestId = request.id; request.execute(this.interpreter, this.connection, this.relayPeerId); diff --git a/src/internal/RequestFlow.ts b/src/internal/RequestFlow.ts index b3a006d55..122c67797 100644 --- a/src/internal/RequestFlow.ts +++ b/src/internal/RequestFlow.ts @@ -96,14 +96,14 @@ export class RequestFlow { } if (!connection) { - throw new Error('Cannot send particle: non connected'); + this.raiseError('Cannot send particle: non connected'); } this.sendIntoConnection(connection); } private throwIfNotSinglePeerAndRelay(nextPeers: PeerIdB58[]) { - throw new Error( + this.raiseError( `Particle is expected to be sent to only the single peer (relay which client is connected to). particle id: ${this.getParticle()?.id} next peers: ${nextPeers.join(' ')} From 4faebcba44d478a51fbad80de7f0e85297cd55c2 Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Tue, 27 Apr 2021 16:57:57 +0300 Subject: [PATCH 6/6] fix pr comments --- src/__test__/integration/client.spec.ts | 6 +----- src/internal/FluenceConnection.ts | 2 +- src/internal/RequestFlow.ts | 6 +++--- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/__test__/integration/client.spec.ts b/src/__test__/integration/client.spec.ts index 7359f2fa1..970aff3be 100644 --- a/src/__test__/integration/client.spec.ts +++ b/src/__test__/integration/client.spec.ts @@ -253,11 +253,7 @@ describe('Typescript usage suite', () => { // act const [req, promise] = new RequestFlowBuilder() - .withRawScript( - ` - (call "incorrect_peer_id" ("any" "service") []) - `, - ) + .withRawScript('(call "incorrect_peer_id" ("any" "service") [])') .buildWithErrorHandling(); await client.initiateFlow(req); diff --git a/src/internal/FluenceConnection.ts b/src/internal/FluenceConnection.ts index 35efc260d..10586e35d 100644 --- a/src/internal/FluenceConnection.ts +++ b/src/internal/FluenceConnection.ts @@ -56,7 +56,7 @@ export interface FluenceConnectionOptions { export class VersionIncompatibleError extends Error { constructor() { - super('Current version of JS SDK is incompatible with the network. Please update the JS SDK'); + super('Current version of JS SDK is incompatible with the connected Fluence node. Please update JS SDK'); } } diff --git a/src/internal/RequestFlow.ts b/src/internal/RequestFlow.ts index 122c67797..440a5f52b 100644 --- a/src/internal/RequestFlow.ts +++ b/src/internal/RequestFlow.ts @@ -87,12 +87,12 @@ export class RequestFlow { // we only expect a single possible peer id to send particle further if (nextPeers.length > 1) { - this.throwIfNotSinglePeerAndRelay(nextPeers); + this.throwIncorrectNextPeerPks(nextPeers); } // this peer id must be the relay, the client is connected to if (!relayPeerId || nextPeers[0] !== relayPeerId) { - this.throwIfNotSinglePeerAndRelay(nextPeers); + this.throwIncorrectNextPeerPks(nextPeers); } if (!connection) { @@ -102,7 +102,7 @@ export class RequestFlow { this.sendIntoConnection(connection); } - private throwIfNotSinglePeerAndRelay(nextPeers: PeerIdB58[]) { + private throwIncorrectNextPeerPks(nextPeers: PeerIdB58[]) { this.raiseError( `Particle is expected to be sent to only the single peer (relay which client is connected to). particle id: ${this.getParticle()?.id}