@@ -20,7 +20,7 @@ import type { PeerId } from '@libp2p/interface/peer-id';
2020import { createLibp2p , Libp2p } from 'libp2p' ;
2121
2222import { noise } from '@chainsafe/libp2p-noise' ;
23- import { mplex } from '@libp2p/mplex ' ;
23+ import { yamux } from '@chainsafe/libp2p-yamux ' ;
2424import { webSockets } from '@libp2p/websockets' ;
2525import { all } from '@libp2p/websockets/filters' ;
2626import { multiaddr } from '@multiformats/multiaddr' ;
@@ -36,7 +36,8 @@ import { throwIfHasNoPeerId } from '../util/libp2pUtils.js';
3636import { IConnection } from './interfaces.js' ;
3737import { IParticle } from '../particle/interfaces.js' ;
3838import { Particle , serializeToString } from '../particle/Particle.js' ;
39- import { IStartable } from '../util/commonTypes.js' ;
39+ import { identifyService } from 'libp2p/identify' ;
40+ import { pingService } from 'libp2p/ping' ;
4041
4142const log = logger ( 'connection' ) ;
4243
@@ -77,7 +78,7 @@ export interface RelayConnectionConfig {
7778/**
7879 * Implementation for JS peers which connects to Fluence through relay node
7980 */
80- export class RelayConnection implements IStartable , IConnection {
81+ export class RelayConnection implements IConnection {
8182 private relayAddress : Multiaddr ;
8283 private lib2p2Peer : Libp2p | null = null ;
8384
@@ -110,14 +111,20 @@ export class RelayConnection implements IStartable, IConnection {
110111 filter : all ,
111112 } ) ,
112113 ] ,
113- streamMuxers : [ mplex ( ) ] ,
114+ streamMuxers : [ yamux ( ) ] ,
114115 connectionEncryption : [ noise ( ) ] ,
115116 connectionManager : {
116117 dialTimeout : this . config . dialTimeoutMs ,
117118 } ,
118119 connectionGater : {
119120 // By default, this function forbids connections to private peers. For example multiaddr with ip 127.0.0.1 isn't allowed
120121 denyDialMultiaddr : ( ) => Promise . resolve ( false )
122+ } ,
123+ services : {
124+ identify : identifyService ( {
125+ runOnConnectionOpen : false ,
126+ } ) ,
127+ ping : pingService ( )
121128 }
122129 } ) ;
123130
@@ -158,23 +165,25 @@ export class RelayConnection implements IStartable, IConnection {
158165 const sink = this._connection.streams[0].sink;
159166 */
160167
168+ log . trace ( 'sending particle...' ) ;
161169 const stream = await this . lib2p2Peer . dialProtocol ( this . relayAddress , PROTOCOL_NAME ) ;
170+ log . trace ( 'created stream with id ' , stream . id ) ;
162171 const sink = stream . sink ;
163172
164- pipe (
173+ await pipe (
165174 [ fromString ( serializeToString ( particle ) ) ] ,
166- // @ts -ignore
167175 encode ( ) ,
168176 sink ,
169177 ) ;
178+ log . trace ( 'data written to sink' ) ;
170179 }
171180
172181 private async connect ( ) {
173182 if ( this . lib2p2Peer === null ) {
174183 throw new Error ( 'Relay connection is not started' ) ;
175184 }
176185
177- this . lib2p2Peer . handle (
186+ await this . lib2p2Peer . handle (
178187 [ PROTOCOL_NAME ] ,
179188 async ( { connection, stream } ) => {
180189 pipe (
@@ -188,6 +197,7 @@ export class RelayConnection implements IStartable, IConnection {
188197 for await ( const msg of source ) {
189198 try {
190199 const particle = Particle . fromString ( msg ) ;
200+ log . trace ( 'got particle from stream with id %s and particle id %s' , stream . id , particle . id ) ;
191201 this . particleSource . next ( particle ) ;
192202 } catch ( e ) {
193203 log . error ( 'error on handling a new incoming message: %j' , e ) ;
0 commit comments