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

Commit

Permalink
fix: circuit dialing
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dryajov authored and daviddias committed Dec 14, 2017
1 parent 232f61f commit 77a4f61
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 37 deletions.
5 changes: 1 addition & 4 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# 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.
machine:
node:
version: stable
Expand All @@ -6,12 +7,8 @@ dependencies:
pre:
- google-chrome --version
- curl -L -o google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
- 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
- sudo dpkg -i google-chrome.deb || true
- sudo dpkg -i libnss3*.deb || true
- sudo apt-get update
- sudo apt-get install -f || true
- sudo dpkg -i libnss3*.deb
- sudo apt-get install -f
- sudo apt-get install --only-upgrade lsb-base
- sudo dpkg -i google-chrome.deb
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@
"async": "^2.6.0",
"debug": "^3.1.0",
"interface-connection": "~0.3.2",
"ip-address": "^5.8.8",
"ip-address": "^5.8.9",
"libp2p-circuit": "~0.1.4",
"libp2p-identify": "~0.6.1",
"lodash.includes": "^4.3.0",
"multiaddr": "^3.0.1",
"multistream-select": "~0.14.1",
"once": "^1.4.0",
"peer-id": "~0.10.2",
"peer-info": "~0.11.1",
"peer-id": "~0.10.3",
"peer-info": "~0.11.3",
"pull-stream": "^3.6.1"
},
"contributors": [
Expand Down
49 changes: 20 additions & 29 deletions src/dial.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,36 @@ function dial (swarm) {
}

function attemptDial (pi, cb) {
const tKeys = swarm.availableTransports(pi)

if (tKeys.length === 0) {
if (!swarm.hasTransports()) {
return cb(new Error('No transports registered, dial not possible'))
}

const tKeys = swarm.availableTransports(pi)

let circuitTried = false
nextTransport(tKeys.shift())

function nextTransport (key) {
if (!key) {
return dialCircuit((err, circuit) => {
if (err) {
return cb(new Error('Could not dial in any of the transports or relays'))
}
let transport = key
if (!transport) {
if (circuitTried) {
return cb(new Error(`Circuit already tried!`))
}

cb(null, circuit)
})
if (!swarm.transports[Circuit.tag]) {
return cb(new Error(`Circuit not enabled!`))
}

log(`Falling back to dialing over circuit`)
pi.multiaddrs.add(`/p2p-circuit/ipfs/${pi.id.toB58String()}`)
circuitTried = true
transport = Circuit.tag
}

log(`dialing transport ${key}`)
swarm.transport.dial(key, pi, (err, conn) => {
log(`dialing transport ${transport}`)
swarm.transport.dial(transport, pi, (err, conn) => {
if (err) {
log(err)
return nextTransport(tKeys.shift())
}

Expand Down Expand Up @@ -131,23 +139,6 @@ function dial (swarm) {
}
}

function dialCircuit (cb) {
log(`Falling back to dialing over circuit`)
pi.multiaddrs.add(`/p2p-circuit/ipfs/${pi.id.toB58String()}`)
if (!swarm.transports[Circuit.tag]) {
return cb(new Error(`Circuit not enabled!`))
}

swarm.transport.dial(Circuit.tag, pi, (err, conn) => {
if (err) {
log(err)
return cb(err)
}

cb(null, conn)
})
}

function attemptMuxerUpgrade (conn, cb) {
const muxers = Object.keys(swarm.muxers)
if (muxers.length === 0) {
Expand Down
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ function Swarm (peerInfo, peerBook) {
this.transport = transport(this)
this.connection = connection(this)

this.hasTransports = () => {
const transports = Object.keys(this.transports).filter((t) => t !== 'Circuit')
return transports && transports.length > 0
}

this.availableTransports = (pi) => {
const myAddrs = pi.multiaddrs.toArray()
const myTransports = Object.keys(this.transports)
Expand All @@ -67,7 +72,7 @@ function Swarm (peerInfo, peerBook) {
return myTransports.filter((ts) => this.transports[ts].filter(myAddrs).length > 0)
// push Circuit to be the last proto to be dialed
.sort((a) => {
return a === 'Circuit' ? -1 : 0
return a === 'Circuit' ? 1 : 0
})
}

Expand Down
12 changes: 12 additions & 0 deletions test/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ describe(`circuit`, function () {
})
})

it(`should dial circuit ony once`, function (done) {
peerA.multiaddrs.clear()
peerA.multiaddrs.add(`/dns4/wrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star`)
swarmA.dial(peerC, (err, conn) => {
expect(err).to.exist()
expect(err).to.match(/Circuit already tried!/)
expect(conn).to.not.exist()
expect(dialSpyA.callCount).to.be.eql(1)
done()
})
})

it(`should dial circuit last`, function (done) {
peerC.multiaddrs.clear()
peerC.multiaddrs.add(`/p2p-circuit/ipfs/ABCD`)
Expand Down

0 comments on commit 77a4f61

Please sign in to comment.