mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-04-25 01:42:13 +00:00
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:
parent
89d42476a2
commit
88d534e4a5
@ -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
|
||||
|
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -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,14 +77,32 @@ 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) {
|
||||
if (!connection) {
|
||||
log.error('Cannot send particle: non connected');
|
||||
}
|
||||
const nextPeers = interpreterOutcome.next_peer_pks;
|
||||
|
||||
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> {
|
||||
|
Loading…
x
Reference in New Issue
Block a user