From c844cf1c4bb56673a2ffe1f6cc0d808bc43eae9e Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 10 Mar 2016 02:40:33 -0800 Subject: [PATCH] move nodes around. --- lib/bcoin.js | 4 +- lib/bcoin/fullnode.js | 322 ++++++++++++++++++++++++++++++++++++++++++ lib/bcoin/node.js | 313 ++++------------------------------------ lib/bcoin/node2.js | 57 -------- 4 files changed, 348 insertions(+), 348 deletions(-) create mode 100644 lib/bcoin/fullnode.js delete mode 100644 lib/bcoin/node2.js diff --git a/lib/bcoin.js b/lib/bcoin.js index c450d2fd..4f053328 100644 --- a/lib/bcoin.js +++ b/lib/bcoin.js @@ -92,9 +92,9 @@ bcoin.merkleblock = require('./bcoin/merkleblock'); bcoin.headers = require('./bcoin/headers'); bcoin.ramdisk = require('./bcoin/ramdisk'); bcoin.blockdb = require('./bcoin/blockdb'); -bcoin.node = require('./bcoin/node2'); +bcoin.node = require('./bcoin/node'); bcoin.spvnode = require('./bcoin/spvnode'); -bcoin.fullnode = require('./bcoin/node'); +bcoin.fullnode = require('./bcoin/fullnode'); bcoin.chainblock = require('./bcoin/chainblock'); bcoin.chaindb = require('./bcoin/chaindb'); bcoin.chain = require('./bcoin/chain'); diff --git a/lib/bcoin/fullnode.js b/lib/bcoin/fullnode.js new file mode 100644 index 00000000..4b17b342 --- /dev/null +++ b/lib/bcoin/fullnode.js @@ -0,0 +1,322 @@ +/** + * fullnode.js - full node for bcoin + * Copyright (c) 2014-2015, Fedor Indutny (MIT License) + * https://github.com/indutny/bcoin + */ + +var EventEmitter = require('events').EventEmitter; +var bcoin = require('../bcoin'); +var bn = require('bn.js'); +var constants = bcoin.protocol.constants; +var network = bcoin.protocol.network; +var utils = bcoin.utils; +var assert = utils.assert; +var fs = bcoin.fs; + +/** + * Fullnode + */ + +function Fullnode(options) { + if (!(this instanceof Fullnode)) + return new Fullnode(options); + + EventEmitter.call(this); + + if (!options) + options = {}; + + bcoin.node.call(this, options); + + this.loading = false; + + Fullnode.global = this; + + this._init(); +} + +utils.inherits(Fullnode, bcoin.node); + +Fullnode.prototype._init = function _init() { + var self = this; + var pending = 3; + + this.loading = true; + + // BlockDB technically needs access to the + // chain, but that's only once it's being + // used for tx retrieval. + this.blockdb = new bcoin.blockdb(this, { + cache: false, + fsync: false + }); + + // Mempool needs access to blockdb. + this.mempool = new bcoin.mempool(this, { + rbf: false + }); + + // Chain needs access to blockdb. + this.chain = new bcoin.chain(this, { + preload: false, + fsync: false + }); + + // Pool needs access to the chain. + this.pool = new bcoin.pool(this, { + witness: this.network.type === 'segnet', + spv: false + }); + + // Miner needs access to the mempool. + this.miner = new bcoin.miner(this, { + address: this.options.payoutAddress, + coinbaseFlags: this.options.coinbaseFlags + }); + + // WalletDB needs access to the network type. + this.walletdb = new bcoin.walletdb(this); + + // HTTP needs access to the mempool + // and blockdb. + this.http = new bcoin.http.server(this, { + key: this.options.sslKey, + cert: this.options.sslCert + }); + + // Bind to errors + this.mempool.on('error', function(err) { + self.emit('error', err); + }); + + this.pool.on('error', function(err) { + self.emit('error', err); + }); + + this.chain.on('error', function(err) { + self.emit('error', err); + }); + + this.http.on('error', function(err) { + self.emit('error', err); + }); + + this.walletdb.on('error', function(err) { + self.emit('error', err); + }); + + if (0) + this.on('tx', function(tx) { + self.walletdb.addTX(tx, function(err) { + if (err) + self.emit('error', err); + }); + }); + + // Emit events for valid blocks and TXs. + if (0) + this.chain.on('block', function(block) { + self.emit('block', block); + block.txs.forEach(function(tx) { + self.emit('tx', tx, block); + }); + }); + + this.mempool.on('tx', function(tx) { + self.emit('tx', tx); + }); + + // Update the mempool. + if (0) + this.chain.on('add block', function(block) { + self.mempool.addBlock(block); + }); + + if (0) + this.chain.on('remove block', function(block) { + self.mempool.removeBlock(block); + self.walletdb.removeBlock(block); + }); + + function load() { + if (!--pending) { + self.loading = false; + self.emit('load'); + self.pool.startSync(); + utils.debug('Node is loaded and syncing.'); + } + } + + // Create or load the primary wallet. + this.createWallet({ id: 'primary', passphrase: 'node' }, function(err, wallet) { + if (err) + throw err; + + // Set the miner payout address if the + // programmer didn't pass one in. + if (!self.miner.address) + self.miner.address = wallet.getAddress(); + + load(); + }); + + this.chain.once('load', function() { + load(); + }); + + this.http.listen(this.options.httpPort || 8080, '0.0.0.0', function(err) { + if (err) + throw err; + + load(); + }); +}; + +Fullnode.prototype.createWallet = function createWallet(options, callback) { + var self = this; + callback = utils.ensure(callback); + this.walletdb.create(options, function(err, wallet) { + if (err) + return callback(err); + + assert(wallet); + + utils.debug('Loaded wallet with id=%s address=%s', + wallet.id, wallet.getAddress()); + + self.pool.addWallet(wallet, function(err) { + if (err) + return callback(err); + + return callback(null, wallet); + }); + }); +}; + +Fullnode.prototype.scanWallet = function scanWallet(wallet, callback) { + wallet.scan(this.getTXByAddress.bind(this), callback); +}; + +Fullnode.prototype.getBlock = function getBlock(hash, callback) { + this.blockdb.getBlock(hash, callback); +}; + +Fullnode.prototype.getFullBlock = function getFullBlock(hash, callback) { + this.blockdb.getFullBlock(hash, callback); +}; + +Fullnode.prototype.getCoin = function getCoin(hash, index, callback) { + var self = this; + var coin; + + callback = utils.asyncify(callback); + + coin = this.mempool.getCoin(hash, index); + if (coin) + return callback(null, coin); + + if (this.mempool.isSpent(hash, index)) + return callback(null, null); + + this.blockdb.getCoin(hash, index, function(err, coin) { + if (err) + return callback(err); + + if (!coin) + return callback(); + + return callback(null, coin); + }); +}; + +Fullnode.prototype.getCoinByAddress = function getCoinByAddress(addresses, callback) { + var self = this; + var mempool; + + callback = utils.asyncify(callback); + + mempool = this.mempool.getCoinsByAddress(addresses); + + this.blockdb.getCoinsByAddress(addresses, function(err, coins) { + if (err) + return callback(err); + + return callback(null, mempool.concat(coins.filter(function(coin) { + if (self.mempool.isSpent(coin.hash, coin.index)) + return false; + return true; + }))); + }); +}; + +Fullnode.prototype.getTX = function getTX(hash, callback) { + var self = this; + var tx; + + callback = utils.asyncify(callback); + + tx = this.mempool.getTX(hash); + if (tx) + return callback(null, tx); + + this.blockdb.getTX(hash, function(err, tx) { + if (err) + return callback(err); + + if (!tx) + return callback(); + + return callback(null, tx); + }); +}; + +Fullnode.prototype.isSpent = function isSpent(hash, index, callback) { + var self = this; + + callback = utils.asyncify(callback); + + if (this.mempool.isSpent(hash, index)) + return callback(null, true); + + this.blockdb.isSpent(hash, index, callback); +}; + +Fullnode.prototype.getTXByAddress = function getTXByAddress(addresses, callback) { + var self = this; + var mempool; + + callback = utils.asyncify(callback); + + mempool = this.mempool.getTXByAddress(addresses); + + this.blockdb.getTXByAddress(addresses, function(err, txs) { + if (err) + return callback(err); + + return callback(null, mempool.concat(txs)); + }); +}; + +Fullnode.prototype.fillCoin = function fillCoin(tx, callback) { + callback = utils.asyncify(callback); + + if (this.mempool.fillCoin(tx)) + return callback(); + + this.blockdb.fillCoin(tx, callback); +}; + +Fullnode.prototype.fillTX = function fillTX(tx, callback) { + callback = utils.asyncify(callback); + + if (this.mempool.fillTX(tx)) + return callback(); + + this.blockdb.fillTX(tx, callback); +}; + +/** + * Expose + */ + +module.exports = Fullnode; diff --git a/lib/bcoin/node.js b/lib/bcoin/node.js index 4b17b342..34afb643 100644 --- a/lib/bcoin/node.js +++ b/lib/bcoin/node.js @@ -1,5 +1,5 @@ /** - * fullnode.js - full node for bcoin + * node.js - node object for bcoin * Copyright (c) 2014-2015, Fedor Indutny (MIT License) * https://github.com/indutny/bcoin */ @@ -14,309 +14,44 @@ var assert = utils.assert; var fs = bcoin.fs; /** - * Fullnode + * Node */ -function Fullnode(options) { - if (!(this instanceof Fullnode)) - return new Fullnode(options); +function Node(options) { + if (!(this instanceof Node)) + return new Node(options); EventEmitter.call(this); if (!options) options = {}; - bcoin.node.call(this, options); + this.options = options; - this.loading = false; + if (this.options.debug != null) + bcoin.debug = this.options.debug; - Fullnode.global = this; + if (this.options.debugFile != null) + bcoin.debugFile = this.options.debugFile; - this._init(); + if (this.options.network) + network.set(this.options.network); + + this.network = network; + this.blockdb = null; + this.mempool = null; + this.pool = null; + this.chain = null; + this.miner = null; + this.walletdb = null; + + Node.global = this; } -utils.inherits(Fullnode, bcoin.node); - -Fullnode.prototype._init = function _init() { - var self = this; - var pending = 3; - - this.loading = true; - - // BlockDB technically needs access to the - // chain, but that's only once it's being - // used for tx retrieval. - this.blockdb = new bcoin.blockdb(this, { - cache: false, - fsync: false - }); - - // Mempool needs access to blockdb. - this.mempool = new bcoin.mempool(this, { - rbf: false - }); - - // Chain needs access to blockdb. - this.chain = new bcoin.chain(this, { - preload: false, - fsync: false - }); - - // Pool needs access to the chain. - this.pool = new bcoin.pool(this, { - witness: this.network.type === 'segnet', - spv: false - }); - - // Miner needs access to the mempool. - this.miner = new bcoin.miner(this, { - address: this.options.payoutAddress, - coinbaseFlags: this.options.coinbaseFlags - }); - - // WalletDB needs access to the network type. - this.walletdb = new bcoin.walletdb(this); - - // HTTP needs access to the mempool - // and blockdb. - this.http = new bcoin.http.server(this, { - key: this.options.sslKey, - cert: this.options.sslCert - }); - - // Bind to errors - this.mempool.on('error', function(err) { - self.emit('error', err); - }); - - this.pool.on('error', function(err) { - self.emit('error', err); - }); - - this.chain.on('error', function(err) { - self.emit('error', err); - }); - - this.http.on('error', function(err) { - self.emit('error', err); - }); - - this.walletdb.on('error', function(err) { - self.emit('error', err); - }); - - if (0) - this.on('tx', function(tx) { - self.walletdb.addTX(tx, function(err) { - if (err) - self.emit('error', err); - }); - }); - - // Emit events for valid blocks and TXs. - if (0) - this.chain.on('block', function(block) { - self.emit('block', block); - block.txs.forEach(function(tx) { - self.emit('tx', tx, block); - }); - }); - - this.mempool.on('tx', function(tx) { - self.emit('tx', tx); - }); - - // Update the mempool. - if (0) - this.chain.on('add block', function(block) { - self.mempool.addBlock(block); - }); - - if (0) - this.chain.on('remove block', function(block) { - self.mempool.removeBlock(block); - self.walletdb.removeBlock(block); - }); - - function load() { - if (!--pending) { - self.loading = false; - self.emit('load'); - self.pool.startSync(); - utils.debug('Node is loaded and syncing.'); - } - } - - // Create or load the primary wallet. - this.createWallet({ id: 'primary', passphrase: 'node' }, function(err, wallet) { - if (err) - throw err; - - // Set the miner payout address if the - // programmer didn't pass one in. - if (!self.miner.address) - self.miner.address = wallet.getAddress(); - - load(); - }); - - this.chain.once('load', function() { - load(); - }); - - this.http.listen(this.options.httpPort || 8080, '0.0.0.0', function(err) { - if (err) - throw err; - - load(); - }); -}; - -Fullnode.prototype.createWallet = function createWallet(options, callback) { - var self = this; - callback = utils.ensure(callback); - this.walletdb.create(options, function(err, wallet) { - if (err) - return callback(err); - - assert(wallet); - - utils.debug('Loaded wallet with id=%s address=%s', - wallet.id, wallet.getAddress()); - - self.pool.addWallet(wallet, function(err) { - if (err) - return callback(err); - - return callback(null, wallet); - }); - }); -}; - -Fullnode.prototype.scanWallet = function scanWallet(wallet, callback) { - wallet.scan(this.getTXByAddress.bind(this), callback); -}; - -Fullnode.prototype.getBlock = function getBlock(hash, callback) { - this.blockdb.getBlock(hash, callback); -}; - -Fullnode.prototype.getFullBlock = function getFullBlock(hash, callback) { - this.blockdb.getFullBlock(hash, callback); -}; - -Fullnode.prototype.getCoin = function getCoin(hash, index, callback) { - var self = this; - var coin; - - callback = utils.asyncify(callback); - - coin = this.mempool.getCoin(hash, index); - if (coin) - return callback(null, coin); - - if (this.mempool.isSpent(hash, index)) - return callback(null, null); - - this.blockdb.getCoin(hash, index, function(err, coin) { - if (err) - return callback(err); - - if (!coin) - return callback(); - - return callback(null, coin); - }); -}; - -Fullnode.prototype.getCoinByAddress = function getCoinByAddress(addresses, callback) { - var self = this; - var mempool; - - callback = utils.asyncify(callback); - - mempool = this.mempool.getCoinsByAddress(addresses); - - this.blockdb.getCoinsByAddress(addresses, function(err, coins) { - if (err) - return callback(err); - - return callback(null, mempool.concat(coins.filter(function(coin) { - if (self.mempool.isSpent(coin.hash, coin.index)) - return false; - return true; - }))); - }); -}; - -Fullnode.prototype.getTX = function getTX(hash, callback) { - var self = this; - var tx; - - callback = utils.asyncify(callback); - - tx = this.mempool.getTX(hash); - if (tx) - return callback(null, tx); - - this.blockdb.getTX(hash, function(err, tx) { - if (err) - return callback(err); - - if (!tx) - return callback(); - - return callback(null, tx); - }); -}; - -Fullnode.prototype.isSpent = function isSpent(hash, index, callback) { - var self = this; - - callback = utils.asyncify(callback); - - if (this.mempool.isSpent(hash, index)) - return callback(null, true); - - this.blockdb.isSpent(hash, index, callback); -}; - -Fullnode.prototype.getTXByAddress = function getTXByAddress(addresses, callback) { - var self = this; - var mempool; - - callback = utils.asyncify(callback); - - mempool = this.mempool.getTXByAddress(addresses); - - this.blockdb.getTXByAddress(addresses, function(err, txs) { - if (err) - return callback(err); - - return callback(null, mempool.concat(txs)); - }); -}; - -Fullnode.prototype.fillCoin = function fillCoin(tx, callback) { - callback = utils.asyncify(callback); - - if (this.mempool.fillCoin(tx)) - return callback(); - - this.blockdb.fillCoin(tx, callback); -}; - -Fullnode.prototype.fillTX = function fillTX(tx, callback) { - callback = utils.asyncify(callback); - - if (this.mempool.fillTX(tx)) - return callback(); - - this.blockdb.fillTX(tx, callback); -}; +utils.inherits(Node, EventEmitter); /** * Expose */ -module.exports = Fullnode; +module.exports = Node; diff --git a/lib/bcoin/node2.js b/lib/bcoin/node2.js deleted file mode 100644 index 34afb643..00000000 --- a/lib/bcoin/node2.js +++ /dev/null @@ -1,57 +0,0 @@ -/** - * node.js - node object for bcoin - * Copyright (c) 2014-2015, Fedor Indutny (MIT License) - * https://github.com/indutny/bcoin - */ - -var EventEmitter = require('events').EventEmitter; -var bcoin = require('../bcoin'); -var bn = require('bn.js'); -var constants = bcoin.protocol.constants; -var network = bcoin.protocol.network; -var utils = bcoin.utils; -var assert = utils.assert; -var fs = bcoin.fs; - -/** - * Node - */ - -function Node(options) { - if (!(this instanceof Node)) - return new Node(options); - - EventEmitter.call(this); - - if (!options) - options = {}; - - this.options = options; - - if (this.options.debug != null) - bcoin.debug = this.options.debug; - - if (this.options.debugFile != null) - bcoin.debugFile = this.options.debugFile; - - if (this.options.network) - network.set(this.options.network); - - this.network = network; - this.blockdb = null; - this.mempool = null; - this.pool = null; - this.chain = null; - this.miner = null; - this.walletdb = null; - - Node.global = this; -} - -utils.inherits(Node, EventEmitter); - -/** - * Expose - */ - -module.exports = Node;