From b87632f97f44aecf583df06aed865bc4e087391a Mon Sep 17 00:00:00 2001 From: Cayman Date: Fri, 9 Sep 2022 06:27:34 -0400 Subject: [PATCH] fix: add yamux interop tests (#1290) Test stream compatibility with https://www.npmjs.com/package/@chainsafe/libp2p-yamux Co-authored-by: achingbrain --- package.json | 7 ++++--- src/upgrader.ts | 6 +++++- test/interop.ts | 9 ++++++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 1fcb950f..81e524a0 100644 --- a/package.json +++ b/package.json @@ -168,16 +168,17 @@ }, "devDependencies": { "@chainsafe/libp2p-noise": "^8.0.0", + "@chainsafe/libp2p-yamux": "^1.0.0", "@libp2p/bootstrap": "^2.0.0", - "@libp2p/daemon-client": "^2.0.4", - "@libp2p/daemon-server": "^2.0.4", + "@libp2p/daemon-client": "^3.0.0", + "@libp2p/daemon-server": "^3.0.0", "@libp2p/delegated-content-routing": "^2.0.1", "@libp2p/delegated-peer-routing": "^2.0.1", "@libp2p/floodsub": "^3.0.0", "@libp2p/interface-compliance-tests": "^3.0.1", "@libp2p/interface-connection-encrypter-compliance-tests": "^2.0.1", "@libp2p/interface-mocks": "^4.0.1", - "@libp2p/interop": "^2.0.0", + "@libp2p/interop": "^3.0.0", "@libp2p/kad-dht": "^3.0.4", "@libp2p/mdns": "^3.0.0", "@libp2p/mplex": "^5.2.1", diff --git a/src/upgrader.ts b/src/upgrader.ts index 23159d03..6b123047 100644 --- a/src/upgrader.ts +++ b/src/upgrader.ts @@ -374,6 +374,10 @@ export class DefaultUpgrader extends EventEmitter implements Upg return } + // after the handshake the returned stream can have early data so override + // the souce/sink + muxedStream.source = stream.source + muxedStream.sink = stream.sink muxedStream.stat.protocol = protocol // If a protocol stream has been successfully negotiated and is to be passed to the application, @@ -381,7 +385,7 @@ export class DefaultUpgrader extends EventEmitter implements Upg this.components.getPeerStore().protoBook.add(remotePeer, [protocol]).catch(err => log.error(err)) connection.addStream(muxedStream) - this._onStream({ connection, stream: { ...muxedStream, ...stream }, protocol }) + this._onStream({ connection, stream: muxedStream, protocol }) }) .catch(err => { log.error(err) diff --git a/test/interop.ts b/test/interop.ts index bf83312e..9768e73e 100644 --- a/test/interop.ts +++ b/test/interop.ts @@ -12,6 +12,7 @@ import { execa } from 'execa' import pDefer from 'p-defer' import { logger } from '@libp2p/logger' import { Mplex } from '@libp2p/mplex' +import { Yamux } from '@chainsafe/libp2p-yamux' import fs from 'fs' import { unmarshalPrivateKey } from '@libp2p/crypto/keys' import type { PeerId } from '@libp2p/interface-peer-id' @@ -93,10 +94,16 @@ async function createJsPeer (options: SpawnOptions): Promise { listen: ['/ip4/0.0.0.0/tcp/0'] }, transports: [new TCP()], - streamMuxers: [new Mplex()], + streamMuxers: [], connectionEncryption: [new Noise()] } + if (options.muxer === 'mplex') { + opts.streamMuxers?.push(new Mplex()) + } else { + opts.streamMuxers?.push(new Yamux()) + } + if (options.dht === true) { // go-libp2p-daemon only has the older single-table DHT instead of the dual // lan/wan version found in recent go-ipfs versions. unfortunately it's been