From ebe37222f561fa6d5be05b80271a42198ee37cb9 Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Thu, 27 Apr 2017 15:52:46 -0400 Subject: [PATCH] wip on timestamp test. --- lib/services/db/index.js | 50 ++++++++++++++------------------ lib/services/timestamp/index.js | 20 ++++++++----- lib/services/wallet-api/index.js | 9 ++---- regtest/wallet.js | 5 ++-- 4 files changed, 39 insertions(+), 45 deletions(-) diff --git a/lib/services/db/index.js b/lib/services/db/index.js index 59eba422..16120197 100644 --- a/lib/services/db/index.js +++ b/lib/services/db/index.js @@ -163,6 +163,10 @@ DB.prototype.start = function(callback) { self._sync.on('synced', function() { self.syncing = false; + self.node.services.bitcoind.on('tip', function(height) { + log.info('New tip at height: ' + height + ' hash: ' + self.node.services.bitcoind.tiphash); + self._sync.sync(); + }); log.info('Initial sync complete'); }); @@ -171,34 +175,21 @@ DB.prototype.start = function(callback) { }); self.node.once('ready', function() { - self._sync.initialSync(); - self.node.services.bitcoind.on('tip', function(height) { - log.info('New tip at height: ' + height + ' hash: ' + self.node.services.bitcoind.tiphash); - self._sync.sync(); - }); - - }); - - async.series([ - function(next) { - self._checkVersion(next); - }, - function(next) { - self._setVersion(next); - } - ], function(err) { - if (err) { - return callback(err); - } - self.loadTip(function(err) { - if (err) { - return callback(err); + function finish(err) { + if(err) { + throw err; } - - self.loadConcurrentTip(callback); - }); + self._sync.initialSync(); + } + self.loadTip(self.loadConcurrentTip.bind(self, finish)); }); + + //TODO remove! + setImmediate(function() { + self._checkVersion(self._setVersion.bind(self, callback)); + }); + }; DB.prototype.stop = function(callback) { @@ -227,6 +218,8 @@ DB.prototype.loadTip = function(callback) { if(err && err instanceof levelup.errors.NotFoundError) { self.tip = self.genesis; self.tip.__height = 0; + // we need to wait for all the services to become ready, + // then we can proceed with connecting blocks here self.connectBlock(self.genesis, function(err) { if(err) { return callback(err); @@ -276,11 +269,12 @@ DB.prototype.loadConcurrentTip = function(callback) { var self = this; self.store.get(self.dbPrefix + 'concurrentTip', self.dbOptions, function(err, tipData) { - if(err && err instanceof levelup.errors.NotFoundError) { + + if (err instanceof levelup.errors.NotFoundError) { self.concurrentTip = self.genesis; self.concurrentTip.__height = 0; - return; - } else if(err) { + return callback(); + } else if (err) { return callback(err); } diff --git a/lib/services/timestamp/index.js b/lib/services/timestamp/index.js index ed25284f..e5f75b92 100644 --- a/lib/services/timestamp/index.js +++ b/lib/services/timestamp/index.js @@ -2,6 +2,7 @@ var Encoding = require('./encoding'); var BaseService = require('../../service'); var inherits = require('util').inherits; +var MAINNET_BITCOIN_GENESIS_TIME = 1296688602; function TimestampService(options) { BaseService.call(this, options); @@ -44,12 +45,8 @@ TimestampService.prototype.blockHandler = function(block, connectBlock, callback var operations = []; function getLastTimestamp(next) { - if(!block.header.prevHash) { - // Genesis block - return next(null, 0); - } else if(block.__height === 1) { - // TODO fix bug where genesis block doesn't get indexed - return next(null, 0); + if(block.__height === 0) { + return next(null, (block.header.timestamp - 1)); } self.getTimestamp(block.header.prevHash.reverse().toString('hex'), next); @@ -109,9 +106,16 @@ TimestampService.prototype.getTimestamp = function(hash, callback) { TimestampService.prototype.getBlockHeights = function(timestamps, callback) { var self = this; timestamps.sort(); + //due to a bug in the encoding routines, thers is a minimum timestamp that + //can be searched for, which is 1296688602 (the bitcoin mainnet genesis block + timestamps = timestamps.map(function(timestamp) { + return timestamp >= MAINNET_BITCOIN_GENESIS_TIME ? timestamp : MAINNET_BITCOIN_GENESIS_TIME; + }); + var start = self.encoding.encodeTimestampBlockKey(timestamps[0]); + var end = self.encoding.encodeTimestampBlockKey(timestamps[1]); var stream = self.store.createReadStream({ - gte: self.encoding.encodeTimestampBlockKey(timestamps[0]), - lte: self.encoding.encodeTimestampBlockKey(timestamps[1]) + gte: start, + lte: end }); var hashes = []; diff --git a/lib/services/wallet-api/index.js b/lib/services/wallet-api/index.js index f11c7806..354cf466 100644 --- a/lib/services/wallet-api/index.js +++ b/lib/services/wallet-api/index.js @@ -20,11 +20,6 @@ var Readable = require('stream').Readable; var WalletService = function(options) { BaseService.call(this, options); - //TODO: what to do if we overflow jobs for which we never reported on - //jobs are agnostic to the amount of work in each job. Additionally, - //each job in the cache is already running immediately after setting - //the job. This means that a large job can be started before smaller - //jobs and this can lead to slower processing of all jobs over time. this._MAX_QUEUE = 20; this._jobs = LRU({ max: this._MAX_QUEUE, @@ -616,6 +611,7 @@ WalletService.prototype._endpointGetTransactions = function() { from: req.query.from, to: req.query.to }; + self._getTransactions(walletId, options, function(err, transactions) { if(err) { @@ -1120,7 +1116,7 @@ WalletService.prototype._storeBalance = function(walletId, balance, callback) { WalletService.prototype._processStartEndOptions = function(req, callback) { var self = this; - if (req.query.start && req.query.end) { + if (req.query.start >= 0 && req.query.end >= 0) { var heights = []; self.node.services.timestamp.getBlockHeights([ @@ -1129,7 +1125,6 @@ WalletService.prototype._processStartEndOptions = function(req, callback) { ], function(err, hashTuple) { - if(err) { return callback(err); } diff --git a/regtest/wallet.js b/regtest/wallet.js index 3d967ab2..b46144a7 100644 --- a/regtest/wallet.js +++ b/regtest/wallet.js @@ -17,7 +17,7 @@ var fs = require('fs'); var http = require('http'); var crypto = require('crypto'); -var debug = false; +var debug = true; var bitcoreDataDir = '/tmp/bitcore'; var bitcoinDataDir = '/tmp/bitcoin'; @@ -181,7 +181,8 @@ describe('Wallet Operations', function() { }); it('should get a list of transactions', function(done) { - var httpOpts = getHttpOpts({ path: '/wallet-api/wallets/' + walletId + '/transactions' }); + var end = Date.now() + 86400000; + var httpOpts = getHttpOpts({ path: '/wallet-api/wallets/' + walletId + '/transactions?start=0&end=' + end }); queryBitcoreNode(httpOpts, function(err, res) { if(err) { return done(err);