diff --git a/Connection.js b/Connection.js index 319a723..1545093 100644 --- a/Connection.js +++ b/Connection.js @@ -1,19 +1,18 @@ var imports = require('soop').imports(); -var config = imports.config || require('./config'); var log = imports.log || require('./util/log'); -var network = imports.network || require('./networks')[config.network]; var MAX_RECEIVE_BUFFER = 10000000; var PROTOCOL_VERSION = 70000; -var Binary = imports.Binary || require('binary'); var Put = imports.Put || require('bufferput'); var Buffers = imports.Buffers || require('buffers'); require('./Buffers.monkey').patch(Buffers); -var Block = require('./Block'); -var Transaction = require('./Transaction'); +var bitcoreDefaults = imports.config || require('./config'); +var networks = imports.networks || require('./networks'); +var Block = imports.Block || require('./Block'); +var Transaction = imports.Transaction || require('./Transaction'); var util = imports.util || require('./util/util'); var Parser = imports.Parser || require('./util/BinaryParser'); var buffertools = imports.buffertools || require('buffertools'); @@ -22,12 +21,14 @@ var nonce = util.generateNonce(); var BIP0031_VERSION = 60000; -function Connection(socket, peer) { +function Connection(socket, peer, config) { Connection.super(this, arguments); + this.config = config || bitcoreDefaults; + this.network = networks[this.config.network] || networks.testnet; this.socket = socket; this.peer = peer; - // A connection is considered "active" once we have received verack + // A connection is considered 'active' once we have received verack this.active = false; // The version incoming packages are interpreted as this.recvVer = 0; @@ -131,7 +132,7 @@ Connection.prototype.handleMessage = function(message) { this.recvVer = Math.min(message.version, PROTOCOL_VERSION); } else { // We won't start expecting a checksum until after we've received - // the "verack" message. + // the 'verack' message. this.once('verack', (function () { this.recvVer = message.version; }).bind(this)); @@ -145,7 +146,7 @@ Connection.prototype.handleMessage = function(message) { break; case 'ping': - if ("object" === typeof message.nonce) { + if ('object' === typeof message.nonce) { this.sendPong(message.nonce); } break; @@ -285,7 +286,7 @@ Connection.prototype.sendBlock = function (block, txs) { Connection.prototype.sendMessage = function (command, payload) { try { - var magic = network.magic; + var magic = this.network.magic; var commandBuf = new Buffer(command, 'ascii'); if (commandBuf.length > 12) throw 'Command name too long'; @@ -308,13 +309,13 @@ Connection.prototype.sendMessage = function (command, payload) { var buffer = message.buffer(); log.debug('['+this.peer+'] '+ - "Sending message "+command+" ("+payload.length+" bytes)"); + 'Sending message '+command+' ('+payload.length+' bytes)'); this.socket.write(buffer); } catch (err) { // TODO: We should catch this error one level higher in order to better // determine how to react to it. For now though, ignoring it will do. - log.err("Error while sending message to peer "+this.peer+": "+ + log.err('Error while sending message to peer '+this.peer+': '+ (err.stack ? err.stack : err.toString())); } }; @@ -323,7 +324,7 @@ Connection.prototype.handleData = function (data) { this.buffers.push(data); if (this.buffers.length > MAX_RECEIVE_BUFFER) { - log.err("Peer "+this.peer+" exceeded maxreceivebuffer, disconnecting."+ + log.err('Peer '+this.peer+' exceeded maxreceivebuffer, disconnecting.'+ (err.stack ? err.stack : err.toString())); this.socket.destroy(); return; @@ -336,7 +337,7 @@ Connection.prototype.processData = function () { // If there are less than 20 bytes there can't be a message yet. if (this.buffers.length < 20) return; - var magic = network.magic; + var magic = this.network.magic; var i = 0; for (;;) { if (this.buffers.get(i ) === magic[0] && @@ -371,13 +372,13 @@ Connection.prototype.processData = function () { if (this.buffers.length < endPos) return; - var command = this.buffers.slice(4, 16).toString('ascii').replace(/\0+$/,""); + var command = this.buffers.slice(4, 16).toString('ascii').replace(/\0+$/,''); var payload = this.buffers.slice(startPos, endPos); var checksum = (this.recvVer >= 209) ? this.buffers.slice(20, 24) : null; log.debug('['+this.peer+'] ' + - "Received message " + command + - " (" + payloadLen + " bytes)"); + 'Received message ' + command + + ' (' + payloadLen + ' bytes)'); if (checksum !== null) { var checksumConfirm = doubleSha256(payload).slice(0, 4); diff --git a/PeerManager.js b/PeerManager.js index d822986..76daa5c 100644 --- a/PeerManager.js +++ b/PeerManager.js @@ -1,21 +1,18 @@ -var imports = require('soop').imports(); -var config = imports.config || require('./config'); -var log = imports.log || require('./util/log'); -var network = imports.network || require('./networks')[config.network]; +var imports = require('soop').imports(); +var log = imports.log || require('./util/log'); +var bitcoreDefaults = imports.config || require('./config'); +var Connection = imports.Connection || require ('./Connection'); -var Connection = imports.Connection || - require('soop').load('./Connection', {config: config, network: network}) || - require ('./Connection'); - -var Peer = imports.Peer || require('./Peer'); +var Peer = imports.Peer || require('./Peer'); GetAdjustedTime = imports.GetAdjustedTime || function () { // TODO: Implement actual adjustment return Math.floor(new Date().getTime() / 1000); }; -function PeerManager() { +function PeerManager(config) { + this.config = config || bitcoreDefaults; this.active = false; this.timer = null; @@ -96,7 +93,7 @@ PeerManager.prototype.connectTo = function(peer) { }; PeerManager.prototype.addConnection = function(socketConn, peer) { - var conn = new Connection(socketConn, peer); + var conn = new Connection(socketConn, peer, this.config); this.connections.push(conn); this.emit('connection', conn); diff --git a/examples/PeerManager.js b/examples/PeerManager.js index 0d0bdba..2cccac9 100644 --- a/examples/PeerManager.js +++ b/examples/PeerManager.js @@ -3,11 +3,8 @@ var run = function() { // Replace '../bitcore' with 'bitcore' if you use this code elsewhere. var bitcore = require('../bitcore'); - var networks = bitcore.networks; var Peer = bitcore.Peer; - var PeerManager = require('soop').load('../PeerManager', { - network: networks.testnet - }); + var PeerManager = bitcore.PeerManager; var handleBlock = function(info) { console.log('** Block Received **'); diff --git a/examples/SendTx.js b/examples/SendTx.js index 3664660..5dddd80 100644 --- a/examples/SendTx.js +++ b/examples/SendTx.js @@ -9,9 +9,7 @@ var run = function() { var Address = bitcore.Address; var Script = bitcore.Script; var coinUtil = bitcore.util; - var PeerManager = require('soop').load('../PeerManager', { - network: networks.testnet - }); + var PeerManager = require('../PeerManager'); var createTx = function() { var TXIN = 'd05f35e0bbc495f6dcab03e599c8f5e32a07cdb4bc76964de201d06a2a7d8265'; diff --git a/examples/SimpleP2Pmonitor.js b/examples/SimpleP2Pmonitor.js index 1791b9b..838e578 100644 --- a/examples/SimpleP2Pmonitor.js +++ b/examples/SimpleP2Pmonitor.js @@ -1,6 +1,6 @@ /** * This is a simple script that will display network messages. - * It users the Peer / Connection classes * directly instead of + * It users the Peer / Connection classes directly instead of * relying on PeerManager. */