Checking if the particle is sent not to the relay which the client is connected to. If not an error is thrown

This commit is contained in:
Pavel Murygin 2021-03-04 13:48:01 +03:00 committed by Pavel
parent 89d42476a2
commit 88d534e4a5
4 changed files with 30 additions and 11 deletions

View File

@ -13,7 +13,7 @@ export interface FluenceClient {
/** /**
* { string } Gets the base58 representation of the current peer id. Read only * { string } Gets the base58 representation of the current peer id. Read only
*/ */
readonly relayPeerId: PeerIdB58; readonly relayPeerId: PeerIdB58 | undefined;
/** /**
* { string } Gets the base58 representation of the connected relay's peer id. Read only * { string } Gets the base58 representation of the connected relay's peer id. Read only

View File

@ -17,7 +17,7 @@ export interface FluenceClient {
/** /**
* { string } Gets the base58 representation of the current peer id. Read only * { string } Gets the base58 representation of the current peer id. Read only
*/ */
readonly relayPeerId: PeerIdB58; readonly relayPeerId: PeerIdB58 | undefined;
/** /**
* { string } Gets the base58 representation of the connected relay's peer id. Read only * { string } Gets the base58 representation of the connected relay's peer id. Read only

View File

@ -133,7 +133,7 @@ export class ClientImpl implements FluenceClient {
private async processRequest(request: RequestFlow): Promise<void> { private async processRequest(request: RequestFlow): Promise<void> {
try { try {
this.currentRequestId = request.id; this.currentRequestId = request.id;
request.execute(this.interpreter, this.connection); request.execute(this.interpreter, this.connection, this.relayPeerId);
} catch (err) { } catch (err) {
log.error('particle processing failed: ' + err); log.error('particle processing failed: ' + err);
} finally { } finally {

View File

@ -2,7 +2,7 @@ import log, { trace } from 'loglevel';
import PeerId from 'peer-id'; import PeerId from 'peer-id';
import { AquamarineInterpreter } from './aqua/interpreter'; import { AquamarineInterpreter } from './aqua/interpreter';
import { AquaCallHandler } from './AquaHandler'; import { AquaCallHandler } from './AquaHandler';
import { InterpreterOutcome } from './commonTypes'; import { InterpreterOutcome, PeerIdB58 } from './commonTypes';
import { FluenceConnection } from './FluenceConnection'; import { FluenceConnection } from './FluenceConnection';
import { Particle, genUUID, logParticle } from './particle'; import { Particle, genUUID, logParticle } from './particle';
@ -27,6 +27,7 @@ export class RequestFlow {
readonly handler = new AquaCallHandler(); readonly handler = new AquaCallHandler();
ttl: number = DEFAULT_TTL; ttl: number = DEFAULT_TTL;
relayPeerId?: PeerIdB58;
static createExternal(particle: Particle): RequestFlow { static createExternal(particle: Particle): RequestFlow {
const res = new RequestFlow(true, particle.id, particle.script); const res = new RequestFlow(true, particle.id, particle.script);
@ -56,7 +57,7 @@ export class RequestFlow {
this.onErrorHandlers.push(handler); this.onErrorHandlers.push(handler);
} }
async execute(interpreter: AquamarineInterpreter, connection: FluenceConnection) { async execute(interpreter: AquamarineInterpreter, connection: FluenceConnection, relayPeerId?: PeerIdB58) {
if (this.hasExpired()) { if (this.hasExpired()) {
return; return;
} }
@ -76,14 +77,32 @@ export class RequestFlow {
); );
} }
// do nothing if there is no `next_peer_pks` or if client isn't connected to the network const nextPeers = interpreterOutcome.next_peer_pks;
if (interpreterOutcome.next_peer_pks.length > 0) {
if (!connection) {
log.error('Cannot send particle: non connected');
}
this.sendIntoConnection(connection); // do nothing if there are no peers to send particle further
if (nextPeers.length === 0) {
return;
} }
// 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 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)',
);
}
if (!connection) {
throw new Error('Cannot send particle: non connected');
}
this.sendIntoConnection(connection);
} }
async initState(peerId: PeerId): Promise<void> { async initState(peerId: PeerId): Promise<void> {