Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit a69f782

Browse files
authored
fix: do not abort dht operation on error responses (#3001)
Response streams from DHT operations contain lots of message types, one of which is 'Error', but they are emitted for all sorts of reasons, failing to dial peers, etc and valid responses can arrive after several errors. The change here is to ignore error messages sent by the remote node as they do not indicate that the request has failed. Fixes #2991
1 parent 6ce5976 commit a69f782

File tree

6 files changed

+20
-39
lines changed

6 files changed

+20
-39
lines changed

packages/ipfs-http-client/src/dht/find-peer.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const CID = require('cids')
55
const multiaddr = require('multiaddr')
66
const configure = require('../lib/configure')
77
const toUrlSearchParams = require('../lib/to-url-search-params')
8+
const { FinalPeer } = require('./response-types')
89

910
module.exports = configure(api => {
1011
return async function findPeer (peerId, options = {}) {
@@ -18,11 +19,7 @@ module.exports = configure(api => {
1819
})
1920

2021
for await (const data of res.ndjson()) {
21-
if (data.Type === 3) {
22-
throw new Error(data.Extra)
23-
}
24-
25-
if (data.Type === 2 && data.Responses) {
22+
if (data.Type === FinalPeer && data.Responses) {
2623
const { ID, Addrs } = data.Responses[0]
2724
return {
2825
id: ID,

packages/ipfs-http-client/src/dht/find-provs.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const CID = require('cids')
44
const multiaddr = require('multiaddr')
55
const configure = require('../lib/configure')
66
const toUrlSearchParams = require('../lib/to-url-search-params')
7+
const { Provider } = require('./response-types')
78

89
module.exports = configure(api => {
910
return async function * findProvs (cid, options = {}) {
@@ -17,16 +18,7 @@ module.exports = configure(api => {
1718
})
1819

1920
for await (const message of res.ndjson()) {
20-
// 3 = QueryError
21-
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L18
22-
// https://github.com/libp2p/go-libp2p-kad-dht/blob/master/routing.go#L525-L526
23-
if (message.Type === 3) {
24-
throw new Error(message.Extra)
25-
}
26-
27-
// 4 = Provider
28-
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L20
29-
if (message.Type === 4 && message.Responses) {
21+
if (message.Type === Provider && message.Responses) {
3022
for (const { ID, Addrs } of message.Responses) {
3123
yield {
3224
id: ID,

packages/ipfs-http-client/src/dht/get.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { Buffer } = require('buffer')
44
const encodeBufferURIComponent = require('../lib/encode-buffer-uri-component')
55
const configure = require('../lib/configure')
66
const toUrlSearchParams = require('../lib/to-url-search-params')
7+
const { Value } = require('./response-types')
78

89
module.exports = configure(api => {
910
return async function get (key, options = {}) {
@@ -21,16 +22,7 @@ module.exports = configure(api => {
2122
})
2223

2324
for await (const message of res.ndjson()) {
24-
// 3 = QueryError
25-
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L18
26-
// https://github.com/ipfs/go-ipfs/blob/eb11f569b064b960d1aba4b5b8ca155a3bd2cb21/core/commands/dht.go#L472-L473
27-
if (message.Type === 3) {
28-
throw new Error(message.Extra)
29-
}
30-
31-
// 5 = Value
32-
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L21
33-
if (message.Type === 5) {
25+
if (message.Type === Value) {
3426
return message.Extra
3527
}
3628
}

packages/ipfs-http-client/src/dht/provide.js

-7
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ module.exports = configure(api => {
2020
})
2121

2222
for await (let message of res.ndjson()) {
23-
// 3 = QueryError
24-
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L18
25-
// https://github.com/ipfs/go-ipfs/blob/eb11f569b064b960d1aba4b5b8ca155a3bd2cb21/core/commands/dht.go#L283-L284
26-
if (message.Type === 3) {
27-
throw new Error(message.Extra)
28-
}
29-
3023
message = toCamel(message)
3124
message.id = new CID(message.id)
3225
if (message.responses) {

packages/ipfs-http-client/src/dht/put.js

-7
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ module.exports = configure(api => {
2121
})
2222

2323
for await (let message of res.ndjson()) {
24-
// 3 = QueryError
25-
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L18
26-
// https://github.com/ipfs/go-ipfs/blob/eb11f569b064b960d1aba4b5b8ca155a3bd2cb21/core/commands/dht.go#L472-L473
27-
if (message.Type === 3) {
28-
throw new Error(message.Extra)
29-
}
30-
3124
message = toCamel(message)
3225
message.id = new CID(message.id)
3326
if (message.responses) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict'
2+
3+
// Response types are defined here:
4+
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L15-L24
5+
module.exports = {
6+
SendingQuery: 0,
7+
PeerResponse: 1,
8+
FinalPeer: 2,
9+
QueryError: 3,
10+
Provider: 4,
11+
Value: 5,
12+
AddingPeer: 6,
13+
DialingPeer: 7
14+
}

0 commit comments

Comments
 (0)