spvnode. readme.
This commit is contained in:
parent
d87e301986
commit
187137262e
23
bin/spvnode
Executable file
23
bin/spvnode
Executable file
@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
var bcoin = require('bcoin');
|
||||
var utils = bcoin.utils;
|
||||
var assert = utils.assert;
|
||||
|
||||
var node = bcoin.spvnode({
|
||||
debug: true,
|
||||
passphrase: 'node',
|
||||
preload: process.argv.indexOf('--preload') !== -1,
|
||||
useCheckpoints: process.argv.indexOf('--checkpoints') !== -1
|
||||
});
|
||||
|
||||
node.on('error', function(err) {
|
||||
utils.debug(err.message);
|
||||
});
|
||||
|
||||
node.open(function(err) {
|
||||
if (err)
|
||||
throw err;
|
||||
|
||||
node.startSync();
|
||||
});
|
||||
@ -172,11 +172,9 @@ Chain.prototype._init = function _init() {
|
||||
|
||||
utils.debug('Chain is loading.');
|
||||
|
||||
self._preload(function(err, start) {
|
||||
if (err) {
|
||||
utils.debug('Preloading chain failed.');
|
||||
utils.debug('Reason: %s', err.message);
|
||||
}
|
||||
self._preload(function(err) {
|
||||
if (err)
|
||||
return self.emit('error', err);
|
||||
|
||||
self.db.open(function(err) {
|
||||
if (err)
|
||||
@ -239,6 +237,7 @@ Chain.prototype._preload = function _preload(callback) {
|
||||
var url = 'https://headers.electrum.org/blockchain_headers';
|
||||
var buf, height, stream;
|
||||
var request = require('./http/request');
|
||||
var locker = new bcoin.locker();
|
||||
|
||||
if (!this.options.preload)
|
||||
return callback();
|
||||
@ -267,31 +266,24 @@ Chain.prototype._preload = function _preload(callback) {
|
||||
}
|
||||
|
||||
function save(entry) {
|
||||
if (save.locked)
|
||||
return save.queue.push(entry);
|
||||
|
||||
save.locked = true;
|
||||
var unlock = locker.lock(save, [entry]);
|
||||
if (!unlock)
|
||||
return;
|
||||
|
||||
self.db.save(entry, null, true, function(err) {
|
||||
if (err)
|
||||
return callback(err, 0);
|
||||
|
||||
save.locked = false;
|
||||
|
||||
if (save.queue.length === 0) {
|
||||
if (save.ended)
|
||||
return callback(null, height + 1);
|
||||
return;
|
||||
if (err) {
|
||||
stream.destroy();
|
||||
locker.destroy();
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
save(save.queue.shift());
|
||||
if (locker.jobs.length === 0 && save.ended)
|
||||
return callback();
|
||||
|
||||
unlock();
|
||||
});
|
||||
}
|
||||
|
||||
save.locked = false;
|
||||
save.queue = [];
|
||||
save.ended = false;
|
||||
|
||||
this.db.getChainHeight(function(err, chainHeight) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
@ -314,8 +306,8 @@ Chain.prototype._preload = function _preload(callback) {
|
||||
var start = Math.max(0, height - 2);
|
||||
self.reset(start, function(e) {
|
||||
if (e)
|
||||
return callback(e, 0);
|
||||
return callback(err, start + 1);
|
||||
return callback(e);
|
||||
return callback(err);
|
||||
});
|
||||
});
|
||||
|
||||
@ -348,7 +340,7 @@ Chain.prototype._preload = function _preload(callback) {
|
||||
try {
|
||||
data = parseHeader(data);
|
||||
} catch (e) {
|
||||
return callback(e, Math.max(0, height - 2));
|
||||
return callback(e);
|
||||
}
|
||||
|
||||
data.height = height;
|
||||
@ -356,7 +348,7 @@ Chain.prototype._preload = function _preload(callback) {
|
||||
// Make sure the genesis block is correct.
|
||||
if (data.height === 0 && data.hash !== network.genesis.hash) {
|
||||
stream.destroy();
|
||||
return callback(new Error('Bad genesis block.'), 0);
|
||||
return callback(new Error('Bad genesis block.'));
|
||||
}
|
||||
|
||||
// Do some paranoid checks.
|
||||
@ -365,8 +357,8 @@ Chain.prototype._preload = function _preload(callback) {
|
||||
stream.destroy();
|
||||
return self.reset(start, function(err) {
|
||||
if (err)
|
||||
return callback(err, 0);
|
||||
return callback(new Error('Corrupt headers.'), start + 1);
|
||||
return callback(err);
|
||||
return callback(new Error('Corrupt headers.'));
|
||||
});
|
||||
}
|
||||
|
||||
@ -380,8 +372,8 @@ Chain.prototype._preload = function _preload(callback) {
|
||||
stream.destroy();
|
||||
return self.reset(start, function(err) {
|
||||
if (err)
|
||||
return callback(err, 0);
|
||||
return callback(new Error('Bad headers.'), start + 1);
|
||||
return callback(err);
|
||||
return callback(new Error('Bad headers.'));
|
||||
});
|
||||
}
|
||||
|
||||
@ -403,8 +395,8 @@ Chain.prototype._preload = function _preload(callback) {
|
||||
|
||||
stream.on('end', function() {
|
||||
save.ended = true;
|
||||
if (!save.locked && save.queue.length === 0)
|
||||
return callback(null, height + 1);
|
||||
if (!locker.busy && locker.jobs.length === 0)
|
||||
return callback();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
@ -32,12 +32,10 @@ utils.inherits(Fullnode, bcoin.node);
|
||||
|
||||
Fullnode.prototype._init = function _init() {
|
||||
var self = this;
|
||||
var pending = 5;
|
||||
var options;
|
||||
|
||||
this.chain = new bcoin.chain(this, {
|
||||
preload: false,
|
||||
fsync: false,
|
||||
spv: false,
|
||||
prune: this.options.prune,
|
||||
useCheckpoints: this.options.useCheckpoints
|
||||
@ -139,11 +137,9 @@ Fullnode.prototype._init = function _init() {
|
||||
if (err)
|
||||
return self.emit('error', err);
|
||||
|
||||
if (!--pending) {
|
||||
self.loaded = true;
|
||||
self.emit('open');
|
||||
utils.debug('Node is loaded.');
|
||||
}
|
||||
self.loaded = true;
|
||||
self.emit('open');
|
||||
utils.debug('Node is loaded.');
|
||||
}
|
||||
|
||||
options = {
|
||||
@ -151,28 +147,31 @@ Fullnode.prototype._init = function _init() {
|
||||
passphrase: this.options.passphrase
|
||||
};
|
||||
|
||||
// Create or load the primary wallet.
|
||||
this.walletdb.open(function(err) {
|
||||
if (err)
|
||||
return self.emit('error', err);
|
||||
utils.serial([
|
||||
this.chain.open.bind(this.chain),
|
||||
this.mempool.open.bind(this.mempool),
|
||||
this.miner.open.bind(this.miner),
|
||||
this.pool.open.bind(this.pool),
|
||||
function(next) {
|
||||
self.walletdb.open(function(err) {
|
||||
if (err)
|
||||
return next(err);
|
||||
|
||||
self.createWallet(options, function(err, wallet) {
|
||||
if (err)
|
||||
return self.emit('error', err);
|
||||
self.createWallet(options, function(err, wallet) {
|
||||
if (err)
|
||||
return next(err);
|
||||
|
||||
// Set the miner payout address if the
|
||||
// programmer didn't pass one in.
|
||||
if (!self.miner.address)
|
||||
self.miner.address = wallet.getAddress();
|
||||
// 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.open(load);
|
||||
this.mempool.open(load);
|
||||
this.pool.open(load);
|
||||
this.http.open(load);
|
||||
load();
|
||||
});
|
||||
});
|
||||
},
|
||||
this.http.open.bind(this.http)
|
||||
], load);
|
||||
};
|
||||
|
||||
Fullnode.prototype.broadcast = function broadcast(item, callback) {
|
||||
@ -204,11 +203,12 @@ Fullnode.prototype.open = function open(callback) {
|
||||
|
||||
Fullnode.prototype.close =
|
||||
Fullnode.prototype.destroy = function destroy(callback) {
|
||||
utils.parallel([
|
||||
this.pool.close.bind(this.pool),
|
||||
utils.serial([
|
||||
this.http.close.bind(this.http),
|
||||
this.mempool.close.bind(this.mempool),
|
||||
this.walletdb.close.bind(this.walletdb),
|
||||
this.pool.close.bind(this.pool),
|
||||
this.miner.close.bind(this.miner),
|
||||
this.mempool.close.bind(this.mempool),
|
||||
this.chain.close.bind(this.chain)
|
||||
], callback);
|
||||
};
|
||||
|
||||
@ -91,6 +91,11 @@ Locker.prototype.lock = function lock(func, args, force) {
|
||||
};
|
||||
};
|
||||
|
||||
Locker.prototype.destroy = function destroy() {
|
||||
this.purgePending();
|
||||
this.jobs.length = 0;
|
||||
};
|
||||
|
||||
Locker.prototype.purgePending = function purgePending() {
|
||||
var self = this;
|
||||
var total = this.pending.length;
|
||||
|
||||
@ -52,6 +52,15 @@ function Miner(node, options) {
|
||||
|
||||
utils.inherits(Miner, EventEmitter);
|
||||
|
||||
Miner.prototype.open = function open(callback) {
|
||||
return utils.nextTick(callback);
|
||||
};
|
||||
|
||||
Miner.prototype.close =
|
||||
Miner.prototype.destroy = function destroy(callback) {
|
||||
return utils.nextTick(callback);
|
||||
};
|
||||
|
||||
Miner.prototype._init = function _init() {
|
||||
var self = this;
|
||||
|
||||
|
||||
@ -21,11 +21,7 @@ function SPVNode(options) {
|
||||
|
||||
bcoin.node.call(this, options);
|
||||
|
||||
this.pool = null;
|
||||
this.chain = null;
|
||||
this.walletdb = null;
|
||||
|
||||
this.loading = false;
|
||||
this.loaded = false;
|
||||
|
||||
SPVNode.global = this;
|
||||
|
||||
@ -38,21 +34,29 @@ SPVNode.prototype._init = function _init() {
|
||||
var self = this;
|
||||
var options;
|
||||
|
||||
this.loading = true;
|
||||
|
||||
this.chain = new bcoin.chain(this, {
|
||||
preload: this.options.preload,
|
||||
spv: true,
|
||||
preload: true,
|
||||
fsync: false
|
||||
useCheckpoints: this.options.useCheckpoints
|
||||
});
|
||||
|
||||
// Pool needs access to the chain.
|
||||
this.pool = new bcoin.pool(this, {
|
||||
witness: this.network.witness,
|
||||
spv: true
|
||||
});
|
||||
|
||||
// WalletDB needs access to the network type.
|
||||
this.walletdb = new bcoin.walletdb(this);
|
||||
|
||||
this.http = new bcoin.http.server(this, {
|
||||
key: this.options.sslKey,
|
||||
cert: this.options.sslCert,
|
||||
port: this.options.httpPort || 8080,
|
||||
host: '0.0.0.0'
|
||||
});
|
||||
|
||||
// Bind to errors
|
||||
this.pool.on('error', function(err) {
|
||||
self.emit('error', err);
|
||||
});
|
||||
@ -61,32 +65,110 @@ SPVNode.prototype._init = function _init() {
|
||||
self.emit('error', err);
|
||||
});
|
||||
|
||||
this.http.on('error', function(err) {
|
||||
self.emit('error', err);
|
||||
});
|
||||
|
||||
this.walletdb.on('error', function(err) {
|
||||
self.emit('error', err);
|
||||
});
|
||||
|
||||
this.pool.on('tx', function(tx) {
|
||||
this.on('tx', function(tx) {
|
||||
self.walletdb.addTX(tx, function(err) {
|
||||
if (err)
|
||||
self.emit('error', err);
|
||||
});
|
||||
});
|
||||
|
||||
// Emit events for valid blocks and TXs.
|
||||
this.chain.on('block', function(block) {
|
||||
self.emit('block', block);
|
||||
block.txs.forEach(function(tx) {
|
||||
self.emit('tx', tx, block);
|
||||
});
|
||||
});
|
||||
|
||||
this.pool.on('tx', function(tx) {
|
||||
self.emit('tx', tx);
|
||||
});
|
||||
|
||||
this.chain.on('remove entry', function(entry) {
|
||||
self.walletdb.removeBlockSPV(entry, function(err) {
|
||||
if (err)
|
||||
self.emit('error', err);
|
||||
});
|
||||
});
|
||||
|
||||
function load(err) {
|
||||
if (err)
|
||||
return self.emit('error', err);
|
||||
|
||||
self.loaded = true;
|
||||
self.emit('open');
|
||||
utils.debug('Node is loaded.');
|
||||
}
|
||||
|
||||
options = {
|
||||
id: 'primary',
|
||||
passphrase: this.options.passphrase
|
||||
};
|
||||
|
||||
this.createWallet(options, function(err) {
|
||||
if (err)
|
||||
throw err;
|
||||
// Create or load the primary wallet.
|
||||
utils.serial([
|
||||
this.chain.open.bind(this.chain),
|
||||
this.pool.open.bind(this.pool),
|
||||
function (next) {
|
||||
self.walletdb.open(function(err) {
|
||||
if (err)
|
||||
return next(err);
|
||||
|
||||
self.loading = false;
|
||||
self.emit('load');
|
||||
self.pool.startSync();
|
||||
self.createWallet(options, function(err, wallet) {
|
||||
if (err)
|
||||
return next(err);
|
||||
|
||||
utils.debug('Node is loaded and syncing.');
|
||||
});
|
||||
next();
|
||||
});
|
||||
});
|
||||
},
|
||||
this.http.open.bind(this.http)
|
||||
], load);
|
||||
};
|
||||
|
||||
SPVNode.prototype.broadcast = function broadcast(item, callback) {
|
||||
return this.pool.broadcast(item, callback);
|
||||
};
|
||||
|
||||
SPVNode.prototype.sendTX = function sendTX(item, callback) {
|
||||
return this.pool.sendTX(item, callback);
|
||||
};
|
||||
|
||||
SPVNode.prototype.sendBlock = function sendBlock(item, callback) {
|
||||
return this.pool.sendBlock(item, callback);
|
||||
};
|
||||
|
||||
SPVNode.prototype.startSync = function startSync() {
|
||||
return this.pool.startSync();
|
||||
};
|
||||
|
||||
SPVNode.prototype.stopSync = function stopSync() {
|
||||
return this.pool.stopSync();
|
||||
};
|
||||
|
||||
SPVNode.prototype.open = function open(callback) {
|
||||
if (this.loaded)
|
||||
return utils.nextTick(callback);
|
||||
|
||||
this.once('open', callback);
|
||||
};
|
||||
|
||||
SPVNode.prototype.close =
|
||||
SPVNode.prototype.destroy = function destroy(callback) {
|
||||
utils.parallel([
|
||||
this.http.close.bind(this.http),
|
||||
this.pool.close.bind(this.pool),
|
||||
this.walletdb.close.bind(this.walletdb),
|
||||
this.chain.close.bind(this.chain)
|
||||
], callback);
|
||||
};
|
||||
|
||||
SPVNode.prototype.createWallet = function createWallet(options, callback) {
|
||||
|
||||
@ -1810,12 +1810,14 @@ utils.parallel = function parallel(stack, callback) {
|
||||
utils.serial = function serial(stack, callback) {
|
||||
var i = 0;
|
||||
(function next(err) {
|
||||
if (i++ >= stack.length)
|
||||
var cb = stack[i++];
|
||||
|
||||
if (!cb)
|
||||
return callback(err);
|
||||
|
||||
if (stack[i].length >= 2) {
|
||||
if (cb.length >= 2) {
|
||||
try {
|
||||
return stack[i](err, next);
|
||||
return cb(err, next);
|
||||
} catch (e) {
|
||||
return next(e);
|
||||
}
|
||||
@ -1825,7 +1827,7 @@ utils.serial = function serial(stack, callback) {
|
||||
return utils.nextTick(next.bind(null, err));
|
||||
|
||||
try {
|
||||
return stack[i](next);
|
||||
return cb(next);
|
||||
} catch (e) {
|
||||
return next(e);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user