make PeerManager and Connection instances configurable
This commit is contained in:
parent
ec918558fe
commit
d5153850ad
@ -1,19 +1,18 @@
|
|||||||
var imports = require('soop').imports();
|
var imports = require('soop').imports();
|
||||||
|
|
||||||
var config = imports.config || require('./config');
|
|
||||||
var log = imports.log || require('./util/log');
|
var log = imports.log || require('./util/log');
|
||||||
var network = imports.network || require('./networks')[config.network];
|
|
||||||
|
|
||||||
var MAX_RECEIVE_BUFFER = 10000000;
|
var MAX_RECEIVE_BUFFER = 10000000;
|
||||||
var PROTOCOL_VERSION = 70000;
|
var PROTOCOL_VERSION = 70000;
|
||||||
|
|
||||||
var Binary = imports.Binary || require('binary');
|
|
||||||
var Put = imports.Put || require('bufferput');
|
var Put = imports.Put || require('bufferput');
|
||||||
var Buffers = imports.Buffers || require('buffers');
|
var Buffers = imports.Buffers || require('buffers');
|
||||||
require('./Buffers.monkey').patch(Buffers);
|
require('./Buffers.monkey').patch(Buffers);
|
||||||
|
|
||||||
var Block = require('./Block');
|
var bitcoreDefaults = imports.config || require('./config');
|
||||||
var Transaction = require('./Transaction');
|
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 util = imports.util || require('./util/util');
|
||||||
var Parser = imports.Parser || require('./util/BinaryParser');
|
var Parser = imports.Parser || require('./util/BinaryParser');
|
||||||
var buffertools = imports.buffertools || require('buffertools');
|
var buffertools = imports.buffertools || require('buffertools');
|
||||||
@ -22,12 +21,14 @@ var nonce = util.generateNonce();
|
|||||||
|
|
||||||
var BIP0031_VERSION = 60000;
|
var BIP0031_VERSION = 60000;
|
||||||
|
|
||||||
function Connection(socket, peer) {
|
function Connection(socket, peer, config) {
|
||||||
Connection.super(this, arguments);
|
Connection.super(this, arguments);
|
||||||
|
this.config = config || bitcoreDefaults;
|
||||||
|
this.network = networks[this.config.network] || networks.testnet;
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
this.peer = peer;
|
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;
|
this.active = false;
|
||||||
// The version incoming packages are interpreted as
|
// The version incoming packages are interpreted as
|
||||||
this.recvVer = 0;
|
this.recvVer = 0;
|
||||||
@ -131,7 +132,7 @@ Connection.prototype.handleMessage = function(message) {
|
|||||||
this.recvVer = Math.min(message.version, PROTOCOL_VERSION);
|
this.recvVer = Math.min(message.version, PROTOCOL_VERSION);
|
||||||
} else {
|
} else {
|
||||||
// We won't start expecting a checksum until after we've received
|
// We won't start expecting a checksum until after we've received
|
||||||
// the "verack" message.
|
// the 'verack' message.
|
||||||
this.once('verack', (function () {
|
this.once('verack', (function () {
|
||||||
this.recvVer = message.version;
|
this.recvVer = message.version;
|
||||||
}).bind(this));
|
}).bind(this));
|
||||||
@ -145,7 +146,7 @@ Connection.prototype.handleMessage = function(message) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'ping':
|
case 'ping':
|
||||||
if ("object" === typeof message.nonce) {
|
if ('object' === typeof message.nonce) {
|
||||||
this.sendPong(message.nonce);
|
this.sendPong(message.nonce);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -285,7 +286,7 @@ Connection.prototype.sendBlock = function (block, txs) {
|
|||||||
|
|
||||||
Connection.prototype.sendMessage = function (command, payload) {
|
Connection.prototype.sendMessage = function (command, payload) {
|
||||||
try {
|
try {
|
||||||
var magic = network.magic;
|
var magic = this.network.magic;
|
||||||
var commandBuf = new Buffer(command, 'ascii');
|
var commandBuf = new Buffer(command, 'ascii');
|
||||||
if (commandBuf.length > 12) throw 'Command name too long';
|
if (commandBuf.length > 12) throw 'Command name too long';
|
||||||
|
|
||||||
@ -308,13 +309,13 @@ Connection.prototype.sendMessage = function (command, payload) {
|
|||||||
var buffer = message.buffer();
|
var buffer = message.buffer();
|
||||||
|
|
||||||
log.debug('['+this.peer+'] '+
|
log.debug('['+this.peer+'] '+
|
||||||
"Sending message "+command+" ("+payload.length+" bytes)");
|
'Sending message '+command+' ('+payload.length+' bytes)');
|
||||||
|
|
||||||
this.socket.write(buffer);
|
this.socket.write(buffer);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// TODO: We should catch this error one level higher in order to better
|
// 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.
|
// 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()));
|
(err.stack ? err.stack : err.toString()));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -323,7 +324,7 @@ Connection.prototype.handleData = function (data) {
|
|||||||
this.buffers.push(data);
|
this.buffers.push(data);
|
||||||
|
|
||||||
if (this.buffers.length > MAX_RECEIVE_BUFFER) {
|
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()));
|
(err.stack ? err.stack : err.toString()));
|
||||||
this.socket.destroy();
|
this.socket.destroy();
|
||||||
return;
|
return;
|
||||||
@ -336,7 +337,7 @@ Connection.prototype.processData = function () {
|
|||||||
// If there are less than 20 bytes there can't be a message yet.
|
// If there are less than 20 bytes there can't be a message yet.
|
||||||
if (this.buffers.length < 20) return;
|
if (this.buffers.length < 20) return;
|
||||||
|
|
||||||
var magic = network.magic;
|
var magic = this.network.magic;
|
||||||
var i = 0;
|
var i = 0;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (this.buffers.get(i ) === magic[0] &&
|
if (this.buffers.get(i ) === magic[0] &&
|
||||||
@ -371,13 +372,13 @@ Connection.prototype.processData = function () {
|
|||||||
|
|
||||||
if (this.buffers.length < endPos) return;
|
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 payload = this.buffers.slice(startPos, endPos);
|
||||||
var checksum = (this.recvVer >= 209) ? this.buffers.slice(20, 24) : null;
|
var checksum = (this.recvVer >= 209) ? this.buffers.slice(20, 24) : null;
|
||||||
|
|
||||||
log.debug('['+this.peer+'] ' +
|
log.debug('['+this.peer+'] ' +
|
||||||
"Received message " + command +
|
'Received message ' + command +
|
||||||
" (" + payloadLen + " bytes)");
|
' (' + payloadLen + ' bytes)');
|
||||||
|
|
||||||
if (checksum !== null) {
|
if (checksum !== null) {
|
||||||
var checksumConfirm = doubleSha256(payload).slice(0, 4);
|
var checksumConfirm = doubleSha256(payload).slice(0, 4);
|
||||||
|
|||||||
@ -1,21 +1,18 @@
|
|||||||
|
|
||||||
var imports = require('soop').imports();
|
var imports = require('soop').imports();
|
||||||
var config = imports.config || require('./config');
|
var log = imports.log || require('./util/log');
|
||||||
var log = imports.log || require('./util/log');
|
var bitcoreDefaults = imports.config || require('./config');
|
||||||
var network = imports.network || require('./networks')[config.network];
|
var Connection = imports.Connection || require ('./Connection');
|
||||||
|
|
||||||
var Connection = imports.Connection ||
|
var Peer = imports.Peer || require('./Peer');
|
||||||
require('soop').load('./Connection', {config: config, network: network}) ||
|
|
||||||
require ('./Connection');
|
|
||||||
|
|
||||||
var Peer = imports.Peer || require('./Peer');
|
|
||||||
|
|
||||||
GetAdjustedTime = imports.GetAdjustedTime || function () {
|
GetAdjustedTime = imports.GetAdjustedTime || function () {
|
||||||
// TODO: Implement actual adjustment
|
// TODO: Implement actual adjustment
|
||||||
return Math.floor(new Date().getTime() / 1000);
|
return Math.floor(new Date().getTime() / 1000);
|
||||||
};
|
};
|
||||||
|
|
||||||
function PeerManager() {
|
function PeerManager(config) {
|
||||||
|
this.config = config || bitcoreDefaults;
|
||||||
this.active = false;
|
this.active = false;
|
||||||
this.timer = null;
|
this.timer = null;
|
||||||
|
|
||||||
@ -96,7 +93,7 @@ PeerManager.prototype.connectTo = function(peer) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
PeerManager.prototype.addConnection = function(socketConn, 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.connections.push(conn);
|
||||||
this.emit('connection', conn);
|
this.emit('connection', conn);
|
||||||
|
|
||||||
|
|||||||
@ -3,11 +3,8 @@
|
|||||||
var run = function() {
|
var run = function() {
|
||||||
// Replace '../bitcore' with 'bitcore' if you use this code elsewhere.
|
// Replace '../bitcore' with 'bitcore' if you use this code elsewhere.
|
||||||
var bitcore = require('../bitcore');
|
var bitcore = require('../bitcore');
|
||||||
var networks = bitcore.networks;
|
|
||||||
var Peer = bitcore.Peer;
|
var Peer = bitcore.Peer;
|
||||||
var PeerManager = require('soop').load('../PeerManager', {
|
var PeerManager = bitcore.PeerManager;
|
||||||
network: networks.testnet
|
|
||||||
});
|
|
||||||
|
|
||||||
var handleBlock = function(info) {
|
var handleBlock = function(info) {
|
||||||
console.log('** Block Received **');
|
console.log('** Block Received **');
|
||||||
|
|||||||
@ -9,9 +9,7 @@ var run = function() {
|
|||||||
var Address = bitcore.Address;
|
var Address = bitcore.Address;
|
||||||
var Script = bitcore.Script;
|
var Script = bitcore.Script;
|
||||||
var coinUtil = bitcore.util;
|
var coinUtil = bitcore.util;
|
||||||
var PeerManager = require('soop').load('../PeerManager', {
|
var PeerManager = require('../PeerManager');
|
||||||
network: networks.testnet
|
|
||||||
});
|
|
||||||
|
|
||||||
var createTx = function() {
|
var createTx = function() {
|
||||||
var TXIN = 'd05f35e0bbc495f6dcab03e599c8f5e32a07cdb4bc76964de201d06a2a7d8265';
|
var TXIN = 'd05f35e0bbc495f6dcab03e599c8f5e32a07cdb4bc76964de201d06a2a7d8265';
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* This is a simple script that will display network messages.
|
* 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.
|
* relying on PeerManager.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user