reset chain after spv preload.
This commit is contained in:
parent
648e46a252
commit
2037060c35
@ -224,7 +224,7 @@ Fullnode.prototype._init = function _init() {
|
|||||||
var i;
|
var i;
|
||||||
self.walletdb.getUnconfirmed(function(err, txs) {
|
self.walletdb.getUnconfirmed(function(err, txs) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return next(err);
|
||||||
|
|
||||||
if (txs.length > 0)
|
if (txs.length > 0)
|
||||||
bcoin.debug('Rebroadcasting %d transactions.', txs.length);
|
bcoin.debug('Rebroadcasting %d transactions.', txs.length);
|
||||||
|
|||||||
@ -1309,7 +1309,7 @@ Pool.prototype.updateWatch = function updateWatch() {
|
|||||||
|
|
||||||
this._pendingWatch = true;
|
this._pendingWatch = true;
|
||||||
|
|
||||||
utils.nextTick(function() {
|
setTimeout(function() {
|
||||||
self._pendingWatch = false;
|
self._pendingWatch = false;
|
||||||
|
|
||||||
if (self.peers.load)
|
if (self.peers.load)
|
||||||
@ -1317,7 +1317,7 @@ Pool.prototype.updateWatch = function updateWatch() {
|
|||||||
|
|
||||||
for (i = 0; i < self.peers.regular.length; i++)
|
for (i = 0; i < self.peers.regular.length; i++)
|
||||||
self.peers.regular[i].updateWatch();
|
self.peers.regular[i].updateWatch();
|
||||||
});
|
}, 50);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1329,73 +1329,6 @@ Pool.prototype.watchAddress = function watchAddress(address) {
|
|||||||
this.watch(bcoin.address.getHash(address), 'hex');
|
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
|
* Queue a `getdata` request to be sent. Checks existence
|
||||||
* in the chain before requesting.
|
* in the chain before requesting.
|
||||||
|
|||||||
@ -164,7 +164,7 @@ SPVNode.prototype._init = function _init() {
|
|||||||
var i;
|
var i;
|
||||||
self.walletdb.getUnconfirmed(function(err, txs) {
|
self.walletdb.getUnconfirmed(function(err, txs) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return next(err);
|
||||||
|
|
||||||
if (txs.length > 0)
|
if (txs.length > 0)
|
||||||
bcoin.debug('Rebroadcasting %d transactions.', txs.length);
|
bcoin.debug('Rebroadcasting %d transactions.', txs.length);
|
||||||
@ -179,7 +179,7 @@ SPVNode.prototype._init = function _init() {
|
|||||||
var i;
|
var i;
|
||||||
self.walletdb.getAddresses(function(err, hashes) {
|
self.walletdb.getAddresses(function(err, hashes) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return next(err);
|
||||||
|
|
||||||
if (hashes.length > 0)
|
if (hashes.length > 0)
|
||||||
bcoin.debug('Adding %d addresses to filter.', hashes.length);
|
bcoin.debug('Adding %d addresses to filter.', hashes.length);
|
||||||
@ -190,6 +190,24 @@ SPVNode.prototype._init = function _init() {
|
|||||||
next();
|
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)
|
this.http.open.bind(this.http)
|
||||||
], load);
|
], load);
|
||||||
};
|
};
|
||||||
@ -276,8 +294,8 @@ SPVNode.prototype.destroy = function destroy(callback) {
|
|||||||
this.wallet.destroy();
|
this.wallet.destroy();
|
||||||
utils.parallel([
|
utils.parallel([
|
||||||
this.http.close.bind(this.http),
|
this.http.close.bind(this.http),
|
||||||
this.pool.close.bind(this.pool),
|
|
||||||
this.walletdb.close.bind(this.walletdb),
|
this.walletdb.close.bind(this.walletdb),
|
||||||
|
this.pool.close.bind(this.pool),
|
||||||
this.chain.close.bind(this.chain)
|
this.chain.close.bind(this.chain)
|
||||||
], callback);
|
], callback);
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user