Skip to content

Commit dc9b126

Browse files
authored
fix: iterate over connections instead of every peer in the peerstore (#450)
When starting up, only look at the current connections rather than trying to get the connections for every peer in the peerstore as this is much slower.
1 parent 72e4a63 commit dc9b126

File tree

5 files changed

+17
-26
lines changed

5 files changed

+17
-26
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@
187187
"@libp2p/mplex": "^1.0.2",
188188
"@libp2p/peer-id": "^1.1.8",
189189
"@libp2p/peer-id-factory": "^1.0.8",
190-
"@libp2p/peer-store": "^1.0.7",
191190
"@libp2p/tcp": "^1.0.6",
192191
"@nodeutils/defaults-deep": "^1.1.0",
193192
"@types/debug": "^4.1.5",
@@ -202,7 +201,7 @@
202201
"iso-random-stream": "^2.0.0",
203202
"it-all": "^1.0.5",
204203
"it-drain": "^1.0.4",
205-
"libp2p": "next",
204+
"libp2p": "^0.37.3",
206205
"lodash.difference": "^4.5.0",
207206
"lodash.flatten": "^4.4.0",
208207
"lodash.range": "^3.2.0",

src/network.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ export class Network {
6767
this._registrarId = await this._libp2p.registrar.register(this._protocols, topology)
6868

6969
// All existing connections are like new ones for us
70-
await this._libp2p.peerStore.forEach(peer => {
71-
this._libp2p.getConnections(peer.id).forEach(conn => this._onPeerConnect(conn.remotePeer))
70+
this._libp2p.getConnections().forEach(conn => {
71+
this._onPeerConnect(conn.remotePeer)
7272
})
7373
}
7474

@@ -93,11 +93,14 @@ export class Network {
9393
* @param {Stream} connection.stream - A duplex iterable stream
9494
* @param {Connection} connection.connection - A libp2p Connection
9595
*/
96-
async _onConnection ({ protocol, stream, connection }) {
97-
if (!this._running) { return }
98-
this._log('incoming new bitswap %s connection from %p', protocol, connection.remotePeer)
96+
_onConnection ({ protocol, stream, connection }) {
97+
if (!this._running) {
98+
return
99+
}
100+
101+
Promise.resolve().then(async () => {
102+
this._log('incoming new bitswap %s connection from %p', protocol, connection.remotePeer)
99103

100-
try {
101104
await pipe(
102105
stream,
103106
lp.decode(),
@@ -113,9 +116,10 @@ export class Network {
113116
}
114117
}
115118
)
116-
} catch (err) {
117-
this._log(err)
118-
}
119+
})
120+
.catch(err => {
121+
this._log(err)
122+
})
119123
}
120124

121125
/**

test/bitswap.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ describe('start/stop', () => {
3737
registrar: {
3838
register: () => {}
3939
},
40-
peerStore: {
41-
forEach: async () => {}
42-
}
40+
getConnections: () => []
4341
}
4442
// @ts-ignore not a full libp2p
4543
const bitswap = new Bitswap(libp2p, new MemoryBlockstore())

test/network/network.node.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,7 @@ describe('network', () => {
307307
registrar: {
308308
register: sinon.stub()
309309
},
310-
// @ts-expect-error incomplete implementation
311-
peerStore: {
312-
forEach: async () => {}
313-
},
310+
getConnections: () => [],
314311
dial: mockDial,
315312
handle: sinon.stub()
316313
}

test/utils/mocks.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import { PersistentPeerStore } from '@libp2p/peer-store'
21
import { MemoryBlockstore } from 'blockstore-core/memory'
32
import { EventEmitter } from 'events'
4-
import { MemoryDatastore } from 'datastore-core/memory'
53
import { Bitswap } from '../../src/bitswap.js'
64
import { Network } from '../../src/network.js'
75
import { Stats } from '../../src/stats/index.js'
86
import { peerIdFromBytes } from '@libp2p/peer-id'
9-
import { Components } from '@libp2p/interfaces/components'
107
import { createLibp2pNode } from './create-libp2p-node.js'
118
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
129
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
@@ -56,13 +53,9 @@ export const mockLibp2pNode = () => {
5653
swarm: {
5754
setMaxListeners () {}
5855
},
59-
peerStore: new PersistentPeerStore({
60-
addressFilter: async () => true
61-
})
56+
getConnections: () => []
6257
})
6358

64-
libp2p.peerStore.init(new Components({ peerId, datastore: new MemoryDatastore() }))
65-
6659
// @ts-expect-error not all libp2p fields are implemented
6760
return libp2p
6861
}

0 commit comments

Comments
 (0)