From 7ca831657b9128c5d3e5a53f06d92bff3c9e726a Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Tue, 20 Jun 2017 15:05:46 -0400 Subject: [PATCH] wip --- bitcore-node.json.connect | 22 +++++++++++++++ lib/node.js | 40 ++++++++++++++++++--------- lib/services/p2p/index.js | 57 +++++++++++++++++++++++++++++++-------- package.json | 1 + regtest/p2p.js | 2 -- 5 files changed, 96 insertions(+), 26 deletions(-) create mode 100644 bitcore-node.json.connect diff --git a/bitcore-node.json.connect b/bitcore-node.json.connect new file mode 100644 index 00000000..925c217e --- /dev/null +++ b/bitcore-node.json.connect @@ -0,0 +1,22 @@ +{ + "network": "regtest", + "port": 3001, + "datadir": "/Users/chrisk", + "services": [ + "p2p", + "db", + "transaction", + "timestamp", + "address", + "mempool", + "wallet-api", + "web" + ], + "servicesConfig": { + "p2p": { + "connect": [ + { "rpchost": "127.0.0.1", "rpcport": 58332, "rpcuser": "bitcoin", "rpcpassword": "local321", "zmqpubrawtx": "tcp://127.0.0.1:28332" } + ] + } + } +} diff --git a/lib/node.js b/lib/node.js index 729725ba..8dac6387 100644 --- a/lib/node.js +++ b/lib/node.js @@ -19,6 +19,15 @@ function Node(config) { } this._init(config); + + if (!_.isUndefined(config.formatLogs)) { + this.log.formatting = config.formatLogs ? true : false; + } + + if (config.services) { + $.checkArgument(Array.isArray(config.services)); + this._unloadedServices = config.services; + } } util.inherits(Node, EventEmitter); @@ -28,19 +37,11 @@ Node.prototype._init = function(config) { this.errors = errors; this.log = log; - if (!_.isUndefined(config.formatLogs)) { - this.log.formatting = config.formatLogs ? true : false; - } - this.datadir = config.datadir; this.network = null; this.services = {}; this._unloadedServices = []; - if (config.services) { - $.checkArgument(Array.isArray(config.services)); - this._unloadedServices = config.services; - } this.port = config.port; this.https = config.https; this.httpsOptions = config.httpsOptions; @@ -88,9 +89,7 @@ Node.prototype.getAllPublishEvents = function() { return events; }; -Node.prototype.getServiceOrder = function() { - - var services = this._unloadedServices; +Node.prototype._getServiceOrder = function(services) { var names = []; var servicesByName = {}; @@ -187,9 +186,24 @@ Node.prototype._logTitle = function() { }; +Node.prototype._startBcoinNode = function() { + bcoin.set(this.getNetworkName()) +}; + + +Node.prototype._detectRemoteNode = function(services) { + if (!_.isArray(services) || services.indexOf('p2p') === -1) { + this._startBcoinNode(); + } + return services; +}; + Node.prototype.start = function(callback) { var self = this; - var servicesOrder = this.getServiceOrder(); + + var services = this._detectRemoteNode(this._unloadedServices); + + var servicesOrder = this._getServiceOrder(services); self._logTitle(); @@ -221,7 +235,7 @@ Node.prototype.stop = function(callback) { log.info('Beginning shutdown'); var self = this; - var services = this.getServiceOrder().reverse(); + var services = this._getServiceOrder(this._unloadedServices).reverse(); this.stopping = true; this.emit('stopping'); diff --git a/lib/services/p2p/index.js b/lib/services/p2p/index.js index 7bb0add5..50fe7856 100644 --- a/lib/services/p2p/index.js +++ b/lib/services/p2p/index.js @@ -7,6 +7,9 @@ var index = require('../../'); var log = index.log; var BaseService = require('../../service'); var assert = require('assert'); +var utils = require('../utils'); +var async = require('async'); +var bcoin = require('bcoin'); var P2P = function(options) { @@ -16,13 +19,34 @@ var P2P = function(options) { BaseService.call(this, options); this.options = options; + + this._initPubSub(); + this._initP2P(); + +}; + +util.inherits(P2P, BaseService); + +P2P.dependencies = []; + +P2P.prototype._initRemoteNode = function(callback) { + // if no connect params exist for p2p, then spawn bcoin as a fallback + if (!this.options.connect) { + return this._spawnBcoin(callback); + } + // remote p2p nodes may not be ready just yet. + async.retry({ times: 10, interval: 1000 }, utils.tryTcpConnection, callback); +}; + +P2P.prototype._spawnBcoin = function(callback) { + bcoin.set(this.node.getNetworkName()); + l +}; + +P2P.prototype._initP2P = function() { this._maxPeers = this.options.maxPeers || 60; this._minPeers = this.options.minPeers || 1; this._configPeers = this.options.peers; - this.subscriptions = {}; - this.subscriptions.block = []; - this.subscriptions.headers = []; - this.subscriptions.transaction = []; this.messages = new p2p.Messages({ network: this.node.network }); this._peerHeights = []; this._peers = []; @@ -30,17 +54,28 @@ var P2P = function(options) { this._mempoolFilter = []; }; -util.inherits(P2P, BaseService); - -P2P.dependencies = []; +P2P.prototype._initPubSub = function() { + this.subscriptions = {}; + this.subscriptions.block = []; + this.subscriptions.headers = []; + this.subscriptions.transaction = []; +}; P2P.prototype.start = function(callback) { var self = this; - self._initCache(); - self._initPool(); - this._setupListeners(); - callback(); + self._initRemoteNode(function(err) { + + if (err) { + return callback(err); + } + + self._initCache(); + self._initPool(); + this._setupListeners(); + callback(); + + }); }; diff --git a/package.json b/package.json index df393255..e07b524e 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ ], "dependencies": { "async": "^1.3.0", + "bcoin", "^1.0.0-beta.12", "bitcoind-rpc": "^0.6.0", "bitcore-lib": "^0.14", "body-parser": "^1.13.3", diff --git a/regtest/p2p.js b/regtest/p2p.js index 9d783dba..6f98899d 100644 --- a/regtest/p2p.js +++ b/regtest/p2p.js @@ -10,10 +10,8 @@ var crypto = require('crypto'); var zmq = require('zmq'); var bitcore = require('bitcore-lib'); var Transaction = bitcore.Transaction; -var PrivateKey = bitcore.PrivateKey; var Block = bitcore.Block; var BlockHeader = bitcore.BlockHeader; -var Unit = bitcore.Unit; var constants = require('../lib/constants'); var debug = true; var extraDebug = true;