reset chain after spv preload.

This commit is contained in:
Christopher Jeffrey 2016-06-02 16:45:05 -07:00
parent 648e46a252
commit 2037060c35
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 24 additions and 73 deletions

View File

@ -224,7 +224,7 @@ Fullnode.prototype._init = function _init() {
var i;
self.walletdb.getUnconfirmed(function(err, txs) {
if (err)
return callback(err);
return next(err);
if (txs.length > 0)
bcoin.debug('Rebroadcasting %d transactions.', txs.length);

View File

@ -1309,7 +1309,7 @@ Pool.prototype.updateWatch = function updateWatch() {
this._pendingWatch = true;
utils.nextTick(function() {
setTimeout(function() {
self._pendingWatch = false;
if (self.peers.load)
@ -1317,7 +1317,7 @@ Pool.prototype.updateWatch = function updateWatch() {
for (i = 0; i < self.peers.regular.length; i++)
self.peers.regular[i].updateWatch();
});
}, 50);
};
/**
@ -1329,73 +1329,6 @@ Pool.prototype.watchAddress = function watchAddress(address) {
this.watch(bcoin.address.getHash(address), 'hex');
};
/**
* Reset the chain to the wallet's last
* active transaction's timestamp/height (SPV-only).
* @param {Wallet} wallet
* @param {Function} callback
*/
Pool.prototype.resetWallet = function resetWallet(wallet, callback) {
var self = this;
assert(this.loaded, 'Pool is not loaded.');
callback = utils.asyncify(callback);
if (!this.options.spv)
return callback();
wallet.getLastTime(function(err, ts, height) {
if (err)
return callback(err);
// Always prefer height
if (height > 0) {
// Back one week
if (!height || height === -1)
height = self.chain.height - (7 * 24 * 6);
self.chain.reset(height, function(err) {
if (err) {
bcoin.error(err);
return callback(err);
}
bcoin.debug('Wallet height: %s.', height);
bcoin.debug(
'Reverted chain to height=%d (%s).',
self.chain.height,
utils.date(self.chain.tip.ts)
);
callback();
});
return;
}
if (!ts)
ts = utils.now() - 7 * 24 * 3600;
self.chain.resetTime(ts, function(err) {
if (err) {
bcoin.error(err);
return callback(err);
}
bcoin.debug('Wallet time is %s.', utils.date(ts));
bcoin.debug(
'Reverted chain to height=%d (%s).',
self.chain.height,
utils.date(self.chain.tip.ts)
);
callback();
});
});
};
/**
* Queue a `getdata` request to be sent. Checks existence
* in the chain before requesting.

View File

@ -164,7 +164,7 @@ SPVNode.prototype._init = function _init() {
var i;
self.walletdb.getUnconfirmed(function(err, txs) {
if (err)
return callback(err);
return next(err);
if (txs.length > 0)
bcoin.debug('Rebroadcasting %d transactions.', txs.length);
@ -179,7 +179,7 @@ SPVNode.prototype._init = function _init() {
var i;
self.walletdb.getAddresses(function(err, hashes) {
if (err)
return callback(err);
return next(err);
if (hashes.length > 0)
bcoin.debug('Adding %d addresses to filter.', hashes.length);
@ -190,6 +190,24 @@ SPVNode.prototype._init = function _init() {
next();
});
},
function(next) {
if (!self.chain.options.preload)
return next();
// If we preloaded, we want to reset
// the chain to our last height.
self.walletdb.getLastTime(function(err, ts, height) {
if (err)
return next(err);
if (height === -1)
return next();
bcoin.debug('Rewinding chain to height %s.', height);
self.chain.reset(height, next);
});
},
this.http.open.bind(this.http)
], load);
};
@ -276,8 +294,8 @@ SPVNode.prototype.destroy = function destroy(callback) {
this.wallet.destroy();
utils.parallel([
this.http.close.bind(this.http),
this.pool.close.bind(this.pool),
this.walletdb.close.bind(this.walletdb),
this.pool.close.bind(this.pool),
this.chain.close.bind(this.chain)
], callback);
};