Skip to content

Commit 88d534e

Browse files
committed
Checking if the particle is sent not to the relay which the client is connected to. If not an error is thrown
1 parent 89d4247 commit 88d534e

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

src/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface FluenceClient {
1313
/**
1414
* { string } Gets the base58 representation of the current peer id. Read only
1515
*/
16-
readonly relayPeerId: PeerIdB58;
16+
readonly relayPeerId: PeerIdB58 | undefined;
1717

1818
/**
1919
* { string } Gets the base58 representation of the connected relay's peer id. Read only

src/api.unstable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface FluenceClient {
1717
/**
1818
* { string } Gets the base58 representation of the current peer id. Read only
1919
*/
20-
readonly relayPeerId: PeerIdB58;
20+
readonly relayPeerId: PeerIdB58 | undefined;
2121

2222
/**
2323
* { string } Gets the base58 representation of the connected relay's peer id. Read only

src/internal/ClientImpl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export class ClientImpl implements FluenceClient {
133133
private async processRequest(request: RequestFlow): Promise<void> {
134134
try {
135135
this.currentRequestId = request.id;
136-
request.execute(this.interpreter, this.connection);
136+
request.execute(this.interpreter, this.connection, this.relayPeerId);
137137
} catch (err) {
138138
log.error('particle processing failed: ' + err);
139139
} finally {

src/internal/RequestFlow.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import log, { trace } from 'loglevel';
22
import PeerId from 'peer-id';
33
import { AquamarineInterpreter } from './aqua/interpreter';
44
import { AquaCallHandler } from './AquaHandler';
5-
import { InterpreterOutcome } from './commonTypes';
5+
import { InterpreterOutcome, PeerIdB58 } from './commonTypes';
66
import { FluenceConnection } from './FluenceConnection';
77
import { Particle, genUUID, logParticle } from './particle';
88

@@ -27,6 +27,7 @@ export class RequestFlow {
2727
readonly handler = new AquaCallHandler();
2828

2929
ttl: number = DEFAULT_TTL;
30+
relayPeerId?: PeerIdB58;
3031

3132
static createExternal(particle: Particle): RequestFlow {
3233
const res = new RequestFlow(true, particle.id, particle.script);
@@ -56,7 +57,7 @@ export class RequestFlow {
5657
this.onErrorHandlers.push(handler);
5758
}
5859

59-
async execute(interpreter: AquamarineInterpreter, connection: FluenceConnection) {
60+
async execute(interpreter: AquamarineInterpreter, connection: FluenceConnection, relayPeerId?: PeerIdB58) {
6061
if (this.hasExpired()) {
6162
return;
6263
}
@@ -76,14 +77,32 @@ export class RequestFlow {
7677
);
7778
}
7879

79-
// do nothing if there is no `next_peer_pks` or if client isn't connected to the network
80-
if (interpreterOutcome.next_peer_pks.length > 0) {
81-
if (!connection) {
82-
log.error('Cannot send particle: non connected');
83-
}
80+
const nextPeers = interpreterOutcome.next_peer_pks;
8481

85-
this.sendIntoConnection(connection);
82+
// do nothing if there are no peers to send particle further
83+
if (nextPeers.length === 0) {
84+
return;
85+
}
86+
87+
// we only expect a single possible peer id to send particle further
88+
if (nextPeers.length > 1) {
89+
throw new Error(
90+
'Particle is expected to be sent to only the single peer (relay which client is connected to)',
91+
);
92+
}
93+
94+
// this peer id must be the relay, the client is connected to
95+
if (!relayPeerId || nextPeers[0] !== relayPeerId) {
96+
throw new Error(
97+
'Particle is expected to be sent to only the single peer (relay which client is connected to)',
98+
);
8699
}
100+
101+
if (!connection) {
102+
throw new Error('Cannot send particle: non connected');
103+
}
104+
105+
this.sendIntoConnection(connection);
87106
}
88107

89108
async initState(peerId: PeerId): Promise<void> {

0 commit comments

Comments
 (0)