Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit 388042b

Browse files
authored
chore: update interfaces to new version (#327)
Removes dialer, lets connection manager manage connections.
1 parent 526e65e commit 388042b

File tree

16 files changed

+140
-186
lines changed

16 files changed

+140
-186
lines changed

.aegir.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
/** @type {import('aegir').PartialOptions} */
33
export default {
44
build: {
5-
bundlesizeMax: '300KB'
5+
bundlesizeMax: '160KB'
66
}
77
}

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,14 @@
132132
"release": "aegir release"
133133
},
134134
"dependencies": {
135-
"@libp2p/crypto": "^0.22.10",
136-
"@libp2p/interfaces": "^1.3.21",
137-
"@libp2p/logger": "^1.1.3",
138-
"@libp2p/peer-id": "^1.1.9",
139-
"@libp2p/record": "^1.0.3",
135+
"@libp2p/crypto": "^0.22.11",
136+
"@libp2p/interfaces": "^1.3.29",
137+
"@libp2p/logger": "^1.1.4",
138+
"@libp2p/peer-id": "^1.1.10",
139+
"@libp2p/record": "^1.0.4",
140140
"@libp2p/topology": "^1.1.7",
141141
"@multiformats/multiaddr": "^10.1.5",
142+
"abortable-iterator": "^4.0.2",
142143
"any-signal": "^3.0.0",
143144
"datastore-core": "^7.0.0",
144145
"err-code": "^3.0.1",
@@ -168,9 +169,9 @@
168169
"varint": "^6.0.0"
169170
},
170171
"devDependencies": {
171-
"@libp2p/interface-compliance-tests": "^1.1.21",
172+
"@libp2p/interface-compliance-tests": "^1.1.31",
172173
"@libp2p/peer-id-factory": "^1.0.9",
173-
"@libp2p/peer-store": "^1.0.8",
174+
"@libp2p/peer-store": "^1.0.11",
174175
"@types/lodash.random": "^3.2.6",
175176
"@types/lodash.range": "^3.2.6",
176177
"@types/node": "^16.11.26",

src/content-fetching/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ export class ContentFetching implements Initializable {
163163
const msg = new Message(MESSAGE_TYPE.PUT_VALUE, key, 0)
164164
msg.record = Libp2pRecord.deserialize(record)
165165

166+
this.log('send put to %p', event.peer.id)
166167
for await (const putEvent of this.network.sendRequest(event.peer.id, msg, options)) {
167168
events.push(putEvent)
168169

src/kad-dht.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT, In
196196
}
197197

198198
async onPeerConnect (peerData: PeerInfo) {
199-
this.log('peer %p connected', peerData.id)
199+
this.log('peer %p connected with protocols %s', peerData.id, peerData.protocols)
200200

201201
if (this.lan) {
202202
peerData = removePublicAddresses(peerData)
@@ -242,7 +242,6 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT, In
242242
} else {
243243
this.log('enabling server mode')
244244
this.clientMode = false
245-
246245
await this.components.getRegistrar().handle(this.protocol, this.rpc.onIncomingStream.bind(this.rpc))
247246
}
248247
}

src/network.ts

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import type { Logger } from '@libp2p/logger'
1818
import type { Duplex } from 'it-stream-types'
1919
import type { PeerInfo } from '@libp2p/interfaces/peer-info'
2020
import { Components, Initializable } from '@libp2p/interfaces/components'
21+
import type { Stream } from '@libp2p/interfaces/connection'
22+
import { abortableDuplex } from 'abortable-iterator'
2123

2224
export interface NetworkInit {
2325
protocol: string
@@ -87,15 +89,17 @@ export class Network extends EventEmitter<NetworkEvents> implements Startable, I
8789
}
8890

8991
this.log('sending %s to %p', msg.type, to)
92+
yield dialingPeerEvent({ peer: to })
93+
yield sendingQueryEvent({ to, type: msg.type })
9094

91-
try {
92-
yield dialingPeerEvent({ peer: to })
93-
94-
const { stream } = await this.components.getDialer().dialProtocol(to, this.protocol, options)
95+
let stream: Stream | undefined
9596

96-
yield sendingQueryEvent({ to, type: msg.type })
97+
try {
98+
const connection = await this.components.getConnectionManager().openConnection(to, options)
99+
const streamData = await connection.newStream(this.protocol)
100+
stream = streamData.stream
97101

98-
const response = await this._writeReadMessage(stream, msg.serialize())
102+
const response = await this._writeReadMessage(stream, msg.serialize(), options)
99103

100104
yield peerResponseEvent({
101105
from: to,
@@ -106,6 +110,10 @@ export class Network extends EventEmitter<NetworkEvents> implements Startable, I
106110
})
107111
} catch (err: any) {
108112
yield queryErrorEvent({ from: to, error: err })
113+
} finally {
114+
if (stream != null) {
115+
stream.close()
116+
}
109117
}
110118
}
111119

@@ -118,26 +126,36 @@ export class Network extends EventEmitter<NetworkEvents> implements Startable, I
118126
}
119127

120128
this.log('sending %s to %p', msg.type, to)
121-
122129
yield dialingPeerEvent({ peer: to })
123-
124-
const { stream } = await this.components.getDialer().dialProtocol(to, this.protocol, options)
125-
126130
yield sendingQueryEvent({ to, type: msg.type })
127131

132+
let stream: Stream | undefined
133+
128134
try {
129-
await this._writeMessage(stream, msg.serialize())
135+
const connection = await this.components.getConnectionManager().openConnection(to, options)
136+
const data = await connection.newStream(this.protocol)
137+
stream = data.stream
138+
139+
await this._writeMessage(stream, msg.serialize(), options)
130140

131141
yield peerResponseEvent({ from: to, messageType: msg.type })
132142
} catch (err: any) {
133143
yield queryErrorEvent({ from: to, error: err })
144+
} finally {
145+
if (stream != null) {
146+
stream.close()
147+
}
134148
}
135149
}
136150

137151
/**
138152
* Write a message to the given stream
139153
*/
140-
async _writeMessage (stream: Duplex<Uint8Array>, msg: Uint8Array) {
154+
async _writeMessage (stream: Duplex<Uint8Array>, msg: Uint8Array, options: AbortOptions) {
155+
if (options.signal != null) {
156+
stream = abortableDuplex(stream, options.signal)
157+
}
158+
141159
await pipe(
142160
[msg],
143161
lp.encode(),
@@ -151,7 +169,11 @@ export class Network extends EventEmitter<NetworkEvents> implements Startable, I
151169
* If no response is received after the specified timeout
152170
* this will error out.
153171
*/
154-
async _writeReadMessage (stream: Duplex<Uint8Array>, msg: Uint8Array) {
172+
async _writeReadMessage (stream: Duplex<Uint8Array>, msg: Uint8Array, options: AbortOptions) {
173+
if (options.signal != null) {
174+
stream = abortableDuplex(stream, options.signal)
175+
}
176+
155177
const res = await pipe(
156178
[msg],
157179
lp.encode(),

src/routing-table/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,10 @@ export class RoutingTable implements Startable, Initializable {
128128
timeoutController = new TimeoutController(this.pingTimeout)
129129

130130
this.log('pinging old contact %p', oldContact.peer)
131-
const { stream } = await this.components.getDialer().dialProtocol(oldContact.peer, PROTOCOL_DHT, {
131+
const connection = await this.components.getConnectionManager().openConnection(oldContact.peer, {
132132
signal: timeoutController.signal
133133
})
134+
const { stream } = await connection.newStream(PROTOCOL_DHT)
134135
await stream.close()
135136
responded++
136137
} catch (err: any) {

src/rpc/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class RPC implements Initializable {
9898
source => (async function * () {
9999
for await (const msg of source) {
100100
// handle the message
101-
const desMessage = Message.deserialize(msg.slice())
101+
const desMessage = Message.deserialize(msg)
102102
self.log('incoming %s from %p', desMessage.type, peerId)
103103
const res = await self.handleMessage(peerId, desMessage)
104104

src/topology-listener.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class TopologyListener extends EventEmitter<TopologyListenerEvents> imple
5656
// register protocol with topology
5757
const topology = createTopology({
5858
onConnect: (peerId) => {
59-
this.log('observed peer %p with protocol %s', this.protocol, peerId)
59+
this.log('observed peer %p with protocol %s', peerId, this.protocol)
6060
this.dispatchEvent(new CustomEvent('peer', {
6161
detail: peerId
6262
}))

test/generate-peers/generate-peers.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import {
1111
} from '../../src/utils.js'
1212
import { Components } from '@libp2p/interfaces/components'
1313
import { stubInterface } from 'ts-sinon'
14-
import type { Dialer } from '@libp2p/interfaces/dialer'
1514
import path from 'path'
1615
import { fileURLToPath } from 'url'
16+
import type { ConnectionManager } from '@libp2p/interfaces/connection-manager'
1717

1818
const dirname = path.dirname(fileURLToPath(import.meta.url))
1919

@@ -54,7 +54,7 @@ describe.skip('generate peers', function () {
5454

5555
const components = new Components({
5656
peerId: id,
57-
dialer: stubInterface<Dialer>()
57+
connectionManager: stubInterface<ConnectionManager>()
5858
})
5959
const table = new RoutingTable({
6060
kBucketSize: 20,

test/kad-dht.spec.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -750,10 +750,14 @@ describe('KadDHT', () => {
750750
new Array(nDHTs).fill(0).map(async () => await tdht.spawn())
751751
)
752752

753+
const connected: Array<Promise<void>> = []
754+
753755
for (let i = 0; i < dhts.length - 1; i++) {
754-
await tdht.connect(dhts[i], dhts[(i + 1) % dhts.length])
756+
connected.push(tdht.connect(dhts[i], dhts[(i + 1) % dhts.length]))
755757
}
756758

759+
await Promise.all(connected)
760+
757761
const res = await all(filter(dhts[1].getClosestPeers(uint8ArrayFromString('foo')), event => event.name === 'FINAL_PEER'))
758762

759763
expect(res).to.not.be.empty()
@@ -785,16 +789,16 @@ describe('KadDHT', () => {
785789
tdht.spawn(),
786790
tdht.spawn()
787791
])
788-
const stub = sinon.stub(dhtA.components.getDialer(), 'dialProtocol').rejects(error)
789792

790793
await tdht.connect(dhtA, dhtB)
791794

795+
const stub = sinon.stub(dhtA.components.getConnectionManager(), 'openConnection').rejects(error)
796+
792797
const errors = await all(filter(dhtA.get(uint8ArrayFromString('/v/hello')), event => event.name === 'QUERY_ERROR'))
793798

794-
expect(errors).to.have.lengthOf(3)
799+
expect(errors).to.have.lengthOf(2)
795800
expect(errors).to.have.nested.property('[0].error.code', errCode)
796-
expect(errors).to.have.nested.property('[1].error.code', errCode)
797-
expect(errors).to.have.nested.property('[2].error.code', 'ERR_NOT_FOUND')
801+
expect(errors).to.have.nested.property('[1].error.code', 'ERR_NOT_FOUND')
798802

799803
stub.restore()
800804
})

0 commit comments

Comments
 (0)