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
*/
readonly relayPeerId: PeerIdB58;
readonly relayPeerId: PeerIdB58 | undefined;
/**
* { 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
*/
readonly relayPeerId: PeerIdB58;
readonly relayPeerId: PeerIdB58 | undefined;
/**
* { 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> {
try {
this.currentRequestId = request.id;
request.execute(this.interpreter, this.connection);
request.execute(this.interpreter, this.connection, this.relayPeerId);
} catch (err) {
log.error('particle processing failed: ' + err);
} finally {

View File

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