Skip to content

Commit

Permalink
Make the user pass in a WebRTC implementation (for #7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Kernfeld committed Feb 5, 2016
1 parent 6c44c08 commit 0eff7d4
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 22 deletions.
1 change: 1 addition & 0 deletions bin/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var argv = require('minimist')(process.argv.slice(2))

var bridge = new Bridge({
network: argv.testnet ? Networks.testnet : Networks.livenet,
wrtc: require('wrtc'),
localPeer: argv.local
})

Expand Down
1 change: 1 addition & 0 deletions bin/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var Node = require('../lib/node.js')

var node = new Node({
network: Networks.livenet,
wrtc: require('wrtc'),
path: 'data',
acceptWeb: true
})
Expand Down
2 changes: 1 addition & 1 deletion lib/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var Bridge = module.exports = function (opts) {

webSeeds.forEach(function (uri) {
debug('bridge connecting to web seed', uri)
var client = new PeerhubClient(uri, function () {
var client = new PeerhubClient(uri, { wrtc: opts.wrtc }, function () {
debug('bridge connected to web seed', uri)
self.webSeeds.push(client)
self.emit('seedconnect', client)
Expand Down
1 change: 1 addition & 0 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var Node = module.exports = function (opts, cb) {

this.peers = opts.peers || new PeerGroup({
network: this.network,
wrtc: opts.wrtc,
acceptWeb: opts.accept || opts.acceptWeb,
acceptTcp: opts.accept || opts.acceptTcp,
getTip: function () { return self.chain.getTip() },
Expand Down
18 changes: 4 additions & 14 deletions lib/peerGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ var u = require('./utils.js')

var webSeeds = require('./constants.js').webSeeds

var supportsWebRTC = false
if (process.browser) {
var RTCPC = window.mozRTCPeerConnection || window.webkitRTCPeerConnection || window.RTCPeerConnection
supportsWebRTC = !!RTCPC
} else {
try {
require('wrtc')
supportsWebRTC = true
} catch (e) {}
}

// HACK: suppress warnings from Buffer#get()
Buffer.prototype.get = function get (offset) {
return this.readUInt8(offset)
Expand All @@ -35,6 +24,7 @@ var commands = Object.keys((new Builder()).commandsMap)
var PeerGroup = module.exports = function (opts) {
opts = opts || {}
this.network = Networks.get(opts.network) || Networks.defaultNetwork
this.wrtc = opts.wrtc
this.acceptTcp = opts.acceptTcp || false
this.acceptWeb = opts.acceptWeb || false
this.tcpCount = opts.tcpCount != null ? opts.tcpCount : 5
Expand Down Expand Up @@ -173,10 +163,10 @@ PeerGroup.prototype.connect = function (opts, cb) {
})
}

if (supportsWebRTC) {
if (self.wrtc) {
tasks.push(function (cb) {
async.each(webSeeds, function (uri, cb) {
var client = new PeerhubClient(uri, function () {
var client = new PeerhubClient(uri, { wrtc: self.wrtc }, function () {
self.webSeeds.push(client)
self.emit('seedconnect', client)
self._connectToWebPeers()
Expand Down Expand Up @@ -271,7 +261,7 @@ PeerGroup.prototype._onWebPeerConnect = function (conn, incoming) {
}

PeerGroup.prototype.acceptWebPeers = function () {
if (!supportsWebRTC) throw new Error('WebRTC is not supported')
if (!this.wrtc) throw new Error('WebRTC is not supported')
this.webSeeds.forEach(this._acceptFromPeerhub.bind(this))
this.acceptWeb = true
}
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@
"buffertools": "browserify-buffertools",
"bitcore-p2p": "bitcore-p2p-browserify"
},
"optionalDependencies": {
"wrtc": "0.0.58"
},
"repository": {
"type": "git",
"url": "https://github.com/mappum/webcoin"
Expand Down
7 changes: 4 additions & 3 deletions test/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var BlockStore = require('../lib/blockStore.js')
var Blockchain = require('../lib/blockchain.js')
var u = require('../lib/utils.js')
var constants = require('../lib/constants.js')
var common = require('./common.js')

try {
var leveldown = require('leveldown')
Expand Down Expand Up @@ -49,7 +50,7 @@ test('creating blockchain instances', function (t) {

t.test('create blockchain with instantiated BlockStore', function (t) {
t.doesNotThrow(function () {
var peers = new PeerGroup()
var peers = new PeerGroup({wrtc: common.wrtc})
var store = new BlockStore({ path: storePath })
var chain = new Blockchain({ peerGroup: peers, store: store })
endStore(chain.store, t)
Expand All @@ -58,7 +59,7 @@ test('creating blockchain instances', function (t) {

t.test('create blockchain with path instead of BlockStore', function (t) {
t.doesNotThrow(function () {
var peers = new PeerGroup()
var peers = new PeerGroup({wrtc: common.wrtc})
var chain = new Blockchain({ peerGroup: peers, path: storePath })
endStore(chain.store, t)
})
Expand Down Expand Up @@ -397,7 +398,7 @@ test('blockchain queries', function (t) {
})

test('blockchain sync', function (t) {
var peers = new PeerGroup()
var peers = new PeerGroup({wrtc: common.wrtc})
peers.on('error', function (err) { console.error(err) })
peers.connect()

Expand Down
2 changes: 2 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Feel free to substitute in an implementation of your preference.
module.exports.wrtc = require('wrtc')
2 changes: 2 additions & 0 deletions test/node.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var test = require('tape')
var Networks = require('bitcore-lib').Networks
var Node = require('../lib/node.js')
var common = require('./common.js')

test('Node constructor', function (t) {
var HEIGHT = 1000
Expand All @@ -10,6 +11,7 @@ test('Node constructor', function (t) {
t.plan(1)
node = new Node({
network: Networks.testnet,
wrtc: common.wrtc,
path: 'data/' + process.pid,
to: HEIGHT
})
Expand Down
3 changes: 2 additions & 1 deletion test/wallet.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
var test = require('tape')
var Networks = require('bitcore-lib').Networks
var Node = require('../lib/node.js')
// var Wallet = require('../lib/wallet.js')
var common = require('./common.js')

test('Node wallet creation', function (t) {
var node

t.test('setup', function (t) {
node = new Node({
network: Networks.testnet,
wrtc: common.wrtc,
path: 'data/' + process.pid,
to: 1000
})
Expand Down

0 comments on commit 0eff7d4

Please sign in to comment.