Skip to content
This repository was archived by the owner on Aug 23, 2019. It is now read-only.

Commit 77a4f61

Browse files
dryajovdaviddias
authored andcommitted
fix: circuit dialing
* feat: fix circuit dialing * chore: upgrade deps * chore: update circle ci config * chore: adding missing dev dependency * fix: removing unused dependency * test: adding tests * fix: remove unused dep
1 parent 232f61f commit 77a4f61

File tree

5 files changed

+42
-37
lines changed

5 files changed

+42
-37
lines changed

circle.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
12
machine:
23
node:
34
version: stable
@@ -6,12 +7,8 @@ dependencies:
67
pre:
78
- google-chrome --version
89
- curl -L -o google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
9-
- for v in $(curl http://archive.ubuntu.com/ubuntu/pool/main/n/nss/ | grep "href=" | grep "libnss3.*deb\"" -o | grep -o "libnss3.*deb" | grep "3.28" | grep "14.04"); do curl -L -o $v http://archive.ubuntu.com/ubuntu/pool/main/n/nss/$v; done && rm libnss3-tools*_i386.deb libnss3-dev*_i386.deb
1010
- sudo dpkg -i google-chrome.deb || true
11-
- sudo dpkg -i libnss3*.deb || true
1211
- sudo apt-get update
13-
- sudo apt-get install -f || true
14-
- sudo dpkg -i libnss3*.deb
1512
- sudo apt-get install -f
1613
- sudo apt-get install --only-upgrade lsb-base
1714
- sudo dpkg -i google-chrome.deb

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@
5858
"async": "^2.6.0",
5959
"debug": "^3.1.0",
6060
"interface-connection": "~0.3.2",
61-
"ip-address": "^5.8.8",
61+
"ip-address": "^5.8.9",
6262
"libp2p-circuit": "~0.1.4",
6363
"libp2p-identify": "~0.6.1",
6464
"lodash.includes": "^4.3.0",
6565
"multiaddr": "^3.0.1",
6666
"multistream-select": "~0.14.1",
6767
"once": "^1.4.0",
68-
"peer-id": "~0.10.2",
69-
"peer-info": "~0.11.1",
68+
"peer-id": "~0.10.3",
69+
"peer-info": "~0.11.3",
7070
"pull-stream": "^3.6.1"
7171
},
7272
"contributors": [

src/dial.js

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -81,28 +81,36 @@ function dial (swarm) {
8181
}
8282

8383
function attemptDial (pi, cb) {
84-
const tKeys = swarm.availableTransports(pi)
85-
86-
if (tKeys.length === 0) {
84+
if (!swarm.hasTransports()) {
8785
return cb(new Error('No transports registered, dial not possible'))
8886
}
8987

88+
const tKeys = swarm.availableTransports(pi)
89+
90+
let circuitTried = false
9091
nextTransport(tKeys.shift())
9192

9293
function nextTransport (key) {
93-
if (!key) {
94-
return dialCircuit((err, circuit) => {
95-
if (err) {
96-
return cb(new Error('Could not dial in any of the transports or relays'))
97-
}
94+
let transport = key
95+
if (!transport) {
96+
if (circuitTried) {
97+
return cb(new Error(`Circuit already tried!`))
98+
}
9899

99-
cb(null, circuit)
100-
})
100+
if (!swarm.transports[Circuit.tag]) {
101+
return cb(new Error(`Circuit not enabled!`))
102+
}
103+
104+
log(`Falling back to dialing over circuit`)
105+
pi.multiaddrs.add(`/p2p-circuit/ipfs/${pi.id.toB58String()}`)
106+
circuitTried = true
107+
transport = Circuit.tag
101108
}
102109

103-
log(`dialing transport ${key}`)
104-
swarm.transport.dial(key, pi, (err, conn) => {
110+
log(`dialing transport ${transport}`)
111+
swarm.transport.dial(transport, pi, (err, conn) => {
105112
if (err) {
113+
log(err)
106114
return nextTransport(tKeys.shift())
107115
}
108116

@@ -131,23 +139,6 @@ function dial (swarm) {
131139
}
132140
}
133141

134-
function dialCircuit (cb) {
135-
log(`Falling back to dialing over circuit`)
136-
pi.multiaddrs.add(`/p2p-circuit/ipfs/${pi.id.toB58String()}`)
137-
if (!swarm.transports[Circuit.tag]) {
138-
return cb(new Error(`Circuit not enabled!`))
139-
}
140-
141-
swarm.transport.dial(Circuit.tag, pi, (err, conn) => {
142-
if (err) {
143-
log(err)
144-
return cb(err)
145-
}
146-
147-
cb(null, conn)
148-
})
149-
}
150-
151142
function attemptMuxerUpgrade (conn, cb) {
152143
const muxers = Object.keys(swarm.muxers)
153144
if (muxers.length === 0) {

src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ function Swarm (peerInfo, peerBook) {
5959
this.transport = transport(this)
6060
this.connection = connection(this)
6161

62+
this.hasTransports = () => {
63+
const transports = Object.keys(this.transports).filter((t) => t !== 'Circuit')
64+
return transports && transports.length > 0
65+
}
66+
6267
this.availableTransports = (pi) => {
6368
const myAddrs = pi.multiaddrs.toArray()
6469
const myTransports = Object.keys(this.transports)
@@ -67,7 +72,7 @@ function Swarm (peerInfo, peerBook) {
6772
return myTransports.filter((ts) => this.transports[ts].filter(myAddrs).length > 0)
6873
// push Circuit to be the last proto to be dialed
6974
.sort((a) => {
70-
return a === 'Circuit' ? -1 : 0
75+
return a === 'Circuit' ? 1 : 0
7176
})
7277
}
7378

test/circuit.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ describe(`circuit`, function () {
8989
})
9090
})
9191

92+
it(`should dial circuit ony once`, function (done) {
93+
peerA.multiaddrs.clear()
94+
peerA.multiaddrs.add(`/dns4/wrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star`)
95+
swarmA.dial(peerC, (err, conn) => {
96+
expect(err).to.exist()
97+
expect(err).to.match(/Circuit already tried!/)
98+
expect(conn).to.not.exist()
99+
expect(dialSpyA.callCount).to.be.eql(1)
100+
done()
101+
})
102+
})
103+
92104
it(`should dial circuit last`, function (done) {
93105
peerC.multiaddrs.clear()
94106
peerC.multiaddrs.add(`/p2p-circuit/ipfs/ABCD`)

0 commit comments

Comments
 (0)