@@ -2,7 +2,7 @@ import log, { trace } from 'loglevel';
2
2
import PeerId from 'peer-id' ;
3
3
import { AquamarineInterpreter } from './aqua/interpreter' ;
4
4
import { AquaCallHandler } from './AquaHandler' ;
5
- import { InterpreterOutcome } from './commonTypes' ;
5
+ import { InterpreterOutcome , PeerIdB58 } from './commonTypes' ;
6
6
import { FluenceConnection } from './FluenceConnection' ;
7
7
import { Particle , genUUID , logParticle } from './particle' ;
8
8
@@ -27,6 +27,7 @@ export class RequestFlow {
27
27
readonly handler = new AquaCallHandler ( ) ;
28
28
29
29
ttl : number = DEFAULT_TTL ;
30
+ relayPeerId ?: PeerIdB58 ;
30
31
31
32
static createExternal ( particle : Particle ) : RequestFlow {
32
33
const res = new RequestFlow ( true , particle . id , particle . script ) ;
@@ -56,7 +57,7 @@ export class RequestFlow {
56
57
this . onErrorHandlers . push ( handler ) ;
57
58
}
58
59
59
- async execute ( interpreter : AquamarineInterpreter , connection : FluenceConnection ) {
60
+ async execute ( interpreter : AquamarineInterpreter , connection : FluenceConnection , relayPeerId ?: PeerIdB58 ) {
60
61
if ( this . hasExpired ( ) ) {
61
62
return ;
62
63
}
@@ -76,14 +77,32 @@ export class RequestFlow {
76
77
) ;
77
78
}
78
79
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 ;
84
81
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
+ ) ;
86
99
}
100
+
101
+ if ( ! connection ) {
102
+ throw new Error ( 'Cannot send particle: non connected' ) ;
103
+ }
104
+
105
+ this . sendIntoConnection ( connection ) ;
87
106
}
88
107
89
108
async initState ( peerId : PeerId ) : Promise < void > {
0 commit comments