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