fcoin/lib/bcoin/node.js
Christopher Jeffrey 997f0acbb0 more refactoring.
2016-02-16 16:13:34 -08:00

192 lines
3.9 KiB
JavaScript

/**
* node.js - full node for bcoin
* Copyright (c) 2014-2015, Fedor Indutny (MIT License)
* https://github.com/indutny/bcoin
*/
var inherits = require('inherits');
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)
bcoin.debug = this.options.debug;
if (this.options.network)
network.set(this.options.network);
this.blockdb = null;
this.mempool = null;
this.pool = null;
this.chain = null;
Node.global = this;
this._init();
}
inherits(Node, EventEmitter);
Node.prototype._init = function _init() {
var self = this;
this.blockdb = new bcoin.blockdb(this.options.blockdb);
this.mempool = new bcoin.mempool(this, this.options.mempool);
if (!this.options.pool)
this.options.pool = {};
this.options.pool.spv = false;
this.options.pool.blockdb = this.blockdb;
this.options.pool.mempool = this.mempool;
this.pool = new bcoin.pool(this.options.pool);
this.chain = this.pool.chain;
this.mempool.on('error', function(err) {
self.emit('error', err);
});
this.pool.on('error', function(err) {
self.emit('error', err);
});
this.pool.startSync();
};
Node.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);
});
};
Node.prototype.getCoinByAddress = function getCoinsByAddress(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;
})));
});
};
Node.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);
});
};
Node.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);
};
Node.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));
});
};
Node.prototype.fillCoin = function fillCoin(tx, callback) {
callback = utils.asyncify(callback);
if (this.mempool.fillCoin(tx))
return callback();
this.blockdb.fillCoin(tx, callback);
};
Node.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 = Node;