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) {
height = -1;
hash = new Array(65).join('0');
} else {
height = tipData.readUInt32BE(32);
hash = tipData.slice(0, 32).toString('hex');
self[tip] = {
height: height,
hash: hash,
'__height': height
};
return next();
}
self[tip] = {
hash: hash,
height: height,
'__height': height //to be consistent with real blocks
};
height = tipData.readUInt32BE(32);
hash = tipData.slice(0, 32).toString('hex');
next();
self.bitcoind.getBlock(hash, function(err, block) {
if(err) {
return next(err);
}
block.__height = height;
self[tip] = block;
next();
});
});
}, callback);

View File

@ -132,13 +132,9 @@ describe('DB Operations', function() {
req.on('end', function() {
var body = JSON.parse(data);
if (debug) {
console.log('request', body);
}
//console.log('request', body);
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.end();
});

View File

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

View File

@ -8,7 +8,7 @@ var path = require('path');
var utils = require('./utils');
var crypto = require('crypto');
var debug = true;
var debug = false;
var bitcoreDataDir = '/tmp/bitcore';
var bitcoinDataDir = '/tmp/bitcoin';
@ -24,7 +24,6 @@ var rpcConfig = {
var bitcoin = {
args: {
datadir: bitcoinDataDir,
txindex: 1,
listen: 0,
regtest: 1,
server: 1,
@ -129,7 +128,7 @@ describe('Wallet Operations', function() {
it('should register wallet', function(done) {
utils.registerWallet(self.opts, function(err, res) {
utils.registerWallet.call(utils, self.opts, function(err, res) {
if (err) {
return done(err);
@ -144,23 +143,24 @@ describe('Wallet Operations', function() {
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) {
//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) {
return done(err);
}
utils.waitForBitcoreNode(self.opts, function(err) {
utils.waitForBitcoreNode.call(utils, self.opts, function(err) {
if(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) {
// async.series([
// utils.startBitcoind.bind(utils, self.opts),
// utils.waitForBitcoinReady.bind(utils, self.opts),
// utils.unlockWallet.bind(utils, self.opts),
// utils.setupInitialTxs.bind(utils, self.opts),
// utils.sendTxs.bind(utils, self.opts),
// utils.startBitcoreNode.bind(utils, self.opts),
// utils.waitForBitcoreNode.bind(utils, self.opts),
// utils.registerWallet.bind(utils, self.opts),
// utils.uploadWallet.bind(utils, self.opts)
// ], done);
// });
before(function(done) {
async.series([
utils.startBitcoind.bind(utils, self.opts),
utils.waitForBitcoinReady.bind(utils, self.opts),
utils.unlockWallet.bind(utils, self.opts),
utils.setupInitialTxs.bind(utils, self.opts),
utils.sendTxs.bind(utils, self.opts),
utils.startBitcoreNode.bind(utils, self.opts),
utils.waitForBitcoreNode.bind(utils, self.opts),
utils.registerWallet.bind(utils, self.opts),
utils.uploadWallet.bind(utils, self.opts)
], 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(
// self.opts,
// { path: '/wallet-api/wallets/' + self.opts.walletId + '/balance' });
var httpOpts = utils.getHttpOpts.call(
utils,
self.opts,
{ path: '/wallet-api/wallets/' + self.opts.walletId + '/balance' });
// utils.queryBitcoreNode(httpOpts, function(err, res) {
// if(err) {
// return done(err);
// }
// var results = JSON.parse(res);
// results.satoshis.should.equal(self.opts.satoshisReceived);
// done();
// });
utils.queryBitcoreNode.call(utils, httpOpts, function(err, res) {
if(err) {
return done(err);
}
var results = JSON.parse(res);
results.satoshis.should.equal(self.opts.satoshisReceived);
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(
// self.opts,
// { path: '/wallet-api/wallets/' + opts.walletId + '/utxos' });
var httpOpts = utils.getHttpOpts.call(
utils,
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) {
// return done(err);
// }
if(err) {
return done(err);
}
// var results = JSON.parse(res);
// var balance = 0;
var results = JSON.parse(res);
var balance = 0;
// results.utxos.forEach(function(utxo) {
// balance += utxo.satoshis;
// });
results.utxos.forEach(function(utxo) {
balance += utxo.satoshis;
});
// results.height.should.equal(self.opts.blockHeight);
// balance.should.equal(self.opts.satoshisReceived);
// done();
// });
// });
results.height.should.equal(self.opts.blockHeight);
balance.should.equal(self.opts.satoshisReceived);
done();
});
});
// it('should get the list of jobs', function(done) {
// var httpOpts = utils.getHttpOpts(self.opts, { path: '/wallet-api/jobs' });
// utils.queryBitcoreNode(httpOpts, function(err, res) {
// if(err) {
// return done(err);
// }
// var results = JSON.parse(res);
// results.jobCount.should.equal(1);
// done();
// });
// });
it('should get the list of jobs', function(done) {
var httpOpts = utils.getHttpOpts.call(utils, self.opts, { path: '/wallet-api/jobs' });
utils.queryBitcoreNode.call(utils, httpOpts, function(err, res) {
if(err) {
return done(err);
}
var results = JSON.parse(res);
results.jobCount.should.equal(1);
done();
});
});
// it('should remove all wallets', function(done) {
// var httpOpts = utils.getHttpOpts(self.opts, { path: '/wallet-api/wallets', method: 'DELETE' });
// utils.queryBitcoreNode(httpOpts, function(err, res) {
// if(err) {
// return done(err);
// }
// var results = JSON.parse(res);
// results.numberRemoved.should.equal(152);
// done();
// });
// });
//});
it('should remove all wallets', function(done) {
var httpOpts = utils.getHttpOpts.call(utils, self.opts, { path: '/wallet-api/wallets', method: 'DELETE' });
utils.queryBitcoreNode.call(utils, httpOpts, function(err, res) {
if(err) {
return done(err);
}
var results = JSON.parse(res);
results.numberRemoved.should.equal(152);
done();
});
});
});
});