Skip to content

Commit

Permalink
fix: add successful stream peer to protobook (#1341)
Browse files Browse the repository at this point in the history
* fix: add successful stream peer to protobook

If a protocol stream has been successfully negotiated and is to be
psased to the application, the peerstore should ensure that the peer is
registerd with that protocol.

* fix: upgrader test fix
  • Loading branch information
wemeetagain committed Aug 11, 2022
1 parent f439d9b commit 8880eef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/upgrader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg

muxedStream.stat.protocol = protocol

// If a protocol stream has been successfully negotiated and is to be passed to the application,
// the peerstore should ensure that the peer is registered with that protocol
this.components.getPeerStore().protoBook.add(remotePeer, [protocol]).catch(err => log.error(err))

connection.addStream(muxedStream)
this._onStream({ connection, stream: { ...muxedStream, ...stream }, protocol })
})
Expand Down Expand Up @@ -438,6 +442,10 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg

muxedStream.stat.protocol = protocol

// If a protocol stream has been successfully negotiated and is to be passed to the application,
// the peerstore should ensure that the peer is registered with that protocol
this.components.getPeerStore().protoBook.add(remotePeer, [protocol]).catch(err => log.error(err))

return {
...muxedStream,
...stream,
Expand Down
10 changes: 8 additions & 2 deletions test/upgrading/upgrader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import { TimeoutController } from 'timeout-abort-controller'
import delay from 'delay'
import drain from 'it-drain'
import { Uint8ArrayList } from 'uint8arraylist'
import { PersistentPeerStore } from '@libp2p/peer-store'
import { MemoryDatastore } from 'datastore-core'

const addrs = [
new Multiaddr('/ip4/127.0.0.1/tcp/0'),
Expand Down Expand Up @@ -57,7 +59,9 @@ describe('Upgrader', () => {
localComponents = new Components({
peerId: localPeer,
connectionGater: mockConnectionGater(),
registrar: mockRegistrar()
registrar: mockRegistrar(),
peerStore: new PersistentPeerStore(),
datastore: new MemoryDatastore()
})
localMuxerFactory = new Mplex()
localUpgrader = new DefaultUpgrader(localComponents, {
Expand All @@ -73,7 +77,9 @@ describe('Upgrader', () => {
remoteComponents = new Components({
peerId: remotePeer,
connectionGater: mockConnectionGater(),
registrar: mockRegistrar()
registrar: mockRegistrar(),
peerStore: new PersistentPeerStore(),
datastore: new MemoryDatastore()
})
remoteUpgrader = new DefaultUpgrader(remoteComponents, {
connectionEncryption: [
Expand Down

0 comments on commit 8880eef

Please sign in to comment.