Load tip must retrieve blocks from bitcoind at startup.

This commit is contained in:
Chris Kleeschulte 2017-05-12 14:47:28 -04:00
parent 835ab3b617
commit f2eaa1ae83
4 changed files with 112 additions and 108 deletions

View File

@ -304,18 +304,28 @@ DB.prototype.loadTips = function(callback) {
if (!tipData) { if (!tipData) {
height = -1; height = -1;
hash = new Array(65).join('0'); hash = new Array(65).join('0');
} else { self[tip] = {
height = tipData.readUInt32BE(32); height: height,
hash = tipData.slice(0, 32).toString('hex'); hash: hash,
'__height': height
};
return next();
} }
self[tip] = { height = tipData.readUInt32BE(32);
hash: hash, hash = tipData.slice(0, 32).toString('hex');
height: height,
'__height': height //to be consistent with real blocks
};
next(); self.bitcoind.getBlock(hash, function(err, block) {
if(err) {
return next(err);
}
block.__height = height;
self[tip] = block;
next();
});
}); });
}, callback); }, callback);

View File

@ -132,13 +132,9 @@ describe('DB Operations', function() {
req.on('end', function() { req.on('end', function() {
var body = JSON.parse(data); var body = JSON.parse(data);
if (debug) { //console.log('request', body);
console.log('request', body);
}
var response = JSON.stringify({ result: responses[responseCount++] }); var response = JSON.stringify({ result: responses[responseCount++] });
if (debug) { //console.log('response', response, 'id: ', body.id);
console.log('response', response, 'id: ', body.id);
}
res.write(response); res.write(response);
res.end(); res.end();
}); });

View File

@ -25,7 +25,7 @@ utils.toArgs = function(opts) {
}; };
utils.waitForService = function(task, callback) { utils.waitForService = function(task, callback) {
var retryOpts = { times: 20, interval: 10000 }; var retryOpts = { times: 20, interval: 1000 };
async.retry(retryOpts, task, callback); async.retry(retryOpts, task, callback);
}; };
@ -35,8 +35,9 @@ utils.queryBitcoreNode = function(httpOpts, callback) {
if (res.statusCode !== 200 && res.statusCode !== 201) { if (res.statusCode !== 200 && res.statusCode !== 201) {
if (error) { if (error) {
return callback(); return;
} }
return callback(res.statusCode);
} }
var resError; var resError;
@ -52,7 +53,7 @@ utils.queryBitcoreNode = function(httpOpts, callback) {
res.on('end', function() { res.on('end', function() {
if (error) { if (error) {
return callback(error); return;
} }
if (httpOpts.errorFilter) { if (httpOpts.errorFilter) {
return callback(httpOpts.errorFilter(resError, resData)); return callback(httpOpts.errorFilter(resError, resData));
@ -64,7 +65,7 @@ utils.queryBitcoreNode = function(httpOpts, callback) {
request.on('error', function(e) { request.on('error', function(e) {
error = e; error = e;
callback(); callback(error);
}); });
request.write(httpOpts.body || ''); request.write(httpOpts.body || '');
@ -99,14 +100,8 @@ utils.waitForBitcoreNode = function(opts, callback) {
}; };
var httpOpts = self.getHttpOpts(opts, { path: '/wallet-api/info', errorFilter: errorFilter }); var httpOpts = self.getHttpOpts(opts, { path: '/wallet-api/info', errorFilter: errorFilter });
var rounds = 10;
async.whilst( self.waitForService(self.queryBitcoreNode.bind(self, httpOpts), callback);
function() {
console.log('called rounds', rounds);
return rounds--;
},
self.queryBitcoreNode.bind(self, httpOpts), callback);
//self.waitForService(self.queryBitcoreNode.bind(self, httpOpts), callback);
}; };
utils.waitForBitcoinReady = function(opts, callback) { utils.waitForBitcoinReady = function(opts, callback) {
@ -339,6 +334,7 @@ utils.uploadWallet = function(opts, callback) {
}; };
utils.getListOfTxs = function(opts, callback) { utils.getListOfTxs = function(opts, callback) {
var self = this; var self = this;
var end = Date.now() + 86400000; var end = Date.now() + 86400000;
var httpOpts = self.getHttpOpts(opts, { var httpOpts = self.getHttpOpts(opts, {

View File

@ -8,7 +8,7 @@ var path = require('path');
var utils = require('./utils'); var utils = require('./utils');
var crypto = require('crypto'); var crypto = require('crypto');
var debug = true; var debug = false;
var bitcoreDataDir = '/tmp/bitcore'; var bitcoreDataDir = '/tmp/bitcore';
var bitcoinDataDir = '/tmp/bitcoin'; var bitcoinDataDir = '/tmp/bitcoin';
@ -24,7 +24,6 @@ var rpcConfig = {
var bitcoin = { var bitcoin = {
args: { args: {
datadir: bitcoinDataDir, datadir: bitcoinDataDir,
txindex: 1,
listen: 0, listen: 0,
regtest: 1, regtest: 1,
server: 1, server: 1,
@ -129,7 +128,7 @@ describe('Wallet Operations', function() {
it('should register wallet', function(done) { it('should register wallet', function(done) {
utils.registerWallet(self.opts, function(err, res) { utils.registerWallet.call(utils, self.opts, function(err, res) {
if (err) { if (err) {
return done(err); return done(err);
@ -144,23 +143,24 @@ describe('Wallet Operations', function() {
it('should upload a wallet', function(done) { it('should upload a wallet', function(done) {
utils.uploadWallet(self.opts, done); utils.uploadWallet.call(utils, self.opts, done);
}); });
it('should get a list of transactions', function(done) { it('should get a list of transactions', function(done) {
//the wallet should be fully uploaded and indexed by the time this happens //the wallet should be fully uploaded and indexed by the time this happens
utils.sendTxs(self.opts, function(err) { utils.sendTxs.call(utils, self.opts, function(err) {
if(err) { if(err) {
return done(err); return done(err);
} }
utils.waitForBitcoreNode(self.opts, function(err) { utils.waitForBitcoreNode.call(utils, self.opts, function(err) {
if(err) { if(err) {
return done(err); return done(err);
} }
utils.getListOfTxs(self.opts, done); utils.getListOfTxs.call(utils, self.opts, done);
}); });
}); });
@ -168,98 +168,100 @@ describe('Wallet Operations', function() {
}); });
//describe('Load addresses after syncing the blockchain', function() { describe('Load addresses after syncing the blockchain', function() {
// var self = this; var self = this;
// self.opts = Object.assign({}, opts); self.opts = Object.assign({}, opts);
// after(utils.cleanup.bind(utils, self.opts)); after(utils.cleanup.bind(utils, self.opts));
// before(function(done) { before(function(done) {
// async.series([ async.series([
// utils.startBitcoind.bind(utils, self.opts), utils.startBitcoind.bind(utils, self.opts),
// utils.waitForBitcoinReady.bind(utils, self.opts), utils.waitForBitcoinReady.bind(utils, self.opts),
// utils.unlockWallet.bind(utils, self.opts), utils.unlockWallet.bind(utils, self.opts),
// utils.setupInitialTxs.bind(utils, self.opts), utils.setupInitialTxs.bind(utils, self.opts),
// utils.sendTxs.bind(utils, self.opts), utils.sendTxs.bind(utils, self.opts),
// utils.startBitcoreNode.bind(utils, self.opts), utils.startBitcoreNode.bind(utils, self.opts),
// utils.waitForBitcoreNode.bind(utils, self.opts), utils.waitForBitcoreNode.bind(utils, self.opts),
// utils.registerWallet.bind(utils, self.opts), utils.registerWallet.bind(utils, self.opts),
// utils.uploadWallet.bind(utils, self.opts) utils.uploadWallet.bind(utils, self.opts)
// ], done); ], done);
// }); });
// it('should get list of transactions', function(done) { it('should get list of transactions', function(done) {
// utils.getListOfTxs(self.opts, done); utils.getListOfTxs.call(utils, self.opts, done);
// }); });
// it('should get the balance of a wallet', function(done) { it('should get the balance of a wallet', function(done) {
// var httpOpts = utils.getHttpOpts( var httpOpts = utils.getHttpOpts.call(
// self.opts, utils,
// { path: '/wallet-api/wallets/' + self.opts.walletId + '/balance' }); self.opts,
{ path: '/wallet-api/wallets/' + self.opts.walletId + '/balance' });
// utils.queryBitcoreNode(httpOpts, function(err, res) { utils.queryBitcoreNode.call(utils, httpOpts, function(err, res) {
// if(err) { if(err) {
// return done(err); return done(err);
// } }
// var results = JSON.parse(res); var results = JSON.parse(res);
// results.satoshis.should.equal(self.opts.satoshisReceived); results.satoshis.should.equal(self.opts.satoshisReceived);
// done(); done();
// }); });
// }); });
// it('should get the set of utxos for the wallet', function(done) { it('should get the set of utxos for the wallet', function(done) {
// var httpOpts = utils.getHttpOpts( var httpOpts = utils.getHttpOpts.call(
// self.opts, utils,
// { path: '/wallet-api/wallets/' + opts.walletId + '/utxos' }); self.opts,
{ path: '/wallet-api/wallets/' + opts.walletId + '/utxos' });
// utils.queryBitcoreNode(httpOpts, function(err, res) { utils.queryBitcoreNode.call(utils, httpOpts, function(err, res) {
// if(err) { if(err) {
// return done(err); return done(err);
// } }
// var results = JSON.parse(res); var results = JSON.parse(res);
// var balance = 0; var balance = 0;
// results.utxos.forEach(function(utxo) { results.utxos.forEach(function(utxo) {
// balance += utxo.satoshis; balance += utxo.satoshis;
// }); });
// results.height.should.equal(self.opts.blockHeight); results.height.should.equal(self.opts.blockHeight);
// balance.should.equal(self.opts.satoshisReceived); balance.should.equal(self.opts.satoshisReceived);
// done(); done();
// }); });
// }); });
// it('should get the list of jobs', function(done) { it('should get the list of jobs', function(done) {
// var httpOpts = utils.getHttpOpts(self.opts, { path: '/wallet-api/jobs' }); var httpOpts = utils.getHttpOpts.call(utils, self.opts, { path: '/wallet-api/jobs' });
// utils.queryBitcoreNode(httpOpts, function(err, res) { utils.queryBitcoreNode.call(utils, httpOpts, function(err, res) {
// if(err) { if(err) {
// return done(err); return done(err);
// } }
// var results = JSON.parse(res); var results = JSON.parse(res);
// results.jobCount.should.equal(1); results.jobCount.should.equal(1);
// done(); done();
// }); });
// }); });
// it('should remove all wallets', function(done) { it('should remove all wallets', function(done) {
// var httpOpts = utils.getHttpOpts(self.opts, { path: '/wallet-api/wallets', method: 'DELETE' }); var httpOpts = utils.getHttpOpts.call(utils, self.opts, { path: '/wallet-api/wallets', method: 'DELETE' });
// utils.queryBitcoreNode(httpOpts, function(err, res) { utils.queryBitcoreNode.call(utils, httpOpts, function(err, res) {
// if(err) { if(err) {
// return done(err); return done(err);
// } }
// var results = JSON.parse(res); var results = JSON.parse(res);
// results.numberRemoved.should.equal(152); results.numberRemoved.should.equal(152);
// done(); done();
// }); });
// }); });
//}); });
}); });