walletdb: improve global resend.

This commit is contained in:
Christopher Jeffrey 2017-01-22 15:25:33 -08:00
parent 25899827fd
commit 1a2f29a360
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -1409,20 +1409,16 @@ WalletDB.prototype.decryptKeys = co(function* decryptKeys(wallet, key) {
}); });
/** /**
* Get keys of all pending transactions * Resend all pending transactions.
* in the wallet db (for resending).
* @returns {Promise} * @returns {Promise}
*/ */
WalletDB.prototype.getPendingKeys = co(function* getPendingKeys() { WalletDB.prototype.resend = co(function* resend() {
var layout = layouts.txdb; var iter, item, wid;
var dummy = new Buffer(0);
var keys = [];
var iter, item;
iter = this.db.iterator({ iter = this.db.iterator({
gte: layout.prefix(0x00000000, dummy), gte: layout.w(0x00000000),
lte: layout.prefix(0xffffffff, dummy) lte: layout.w(0xffffffff)
}); });
for (;;) { for (;;) {
@ -1431,61 +1427,47 @@ WalletDB.prototype.getPendingKeys = co(function* getPendingKeys() {
if (!item) if (!item)
break; break;
if (item.key[5] === 0x70) try {
keys.push(item.key); wid = layout.ww(item.key);
yield this.resendPending(wid);
} catch (e) {
yield iter.end();
throw e;
}
} }
return keys;
}); });
/** /**
* Get keys of all pending transactions * Resend all pending transactions for a specific wallet.
* in the wallet db (for resending). * @private
* @param {WalletID} wid
* @returns {Promise} * @returns {Promise}
*/ */
WalletDB.prototype.getPendingTX = co(function* getPendingTX() { WalletDB.prototype.resendPending = co(function* resendPending(wid) {
var layout = layouts.txdb; var layout = layouts.txdb;
var keys = yield this.getPendingKeys();
var uniq = {};
var result = [];
var i, key, wid, hash;
for (i = 0; i < keys.length; i++) {
key = keys[i];
wid = layout.pre(key);
hash = layout.pp(key);
if (uniq[hash])
continue;
uniq[hash] = true;
key = layout.prefix(wid, layout.t(hash));
result.push(key);
}
return result;
});
/**
* Resend all pending transactions.
* @returns {Promise}
*/
WalletDB.prototype.resend = co(function* resend() {
var keys = yield this.getPendingTX();
var txs = []; var txs = [];
var i, key, data, wtx; var i, key, keys, hash, data, wtx, tx;
keys = yield this.db.keys({
gte: layout.prefix(wid, layout.p(encoding.NULL_HASH)),
lte: layout.prefix(wid, layout.p(encoding.HIGH_HASH))
});
if (keys.length === 0) if (keys.length === 0)
return; return;
this.logger.info('Rebroadcasting %d WalletDB transactions.', keys.length); this.logger.info(
'Rebroadcasting %d transactions for %d.',
keys.length,
wid);
for (i = 0; i < keys.length; i++) { for (i = 0; i < keys.length; i++) {
key = keys[i]; key = keys[i];
hash = layout.pp(key);
key = layout.prefix(wid, layout.t(hash));
data = yield this.db.get(key); data = yield this.db.get(key);
if (!data) if (!data)
@ -1501,8 +1483,10 @@ WalletDB.prototype.resend = co(function* resend() {
txs = common.sortTX(txs); txs = common.sortTX(txs);
for (i = 0; i < txs.length; i++) for (i = 0; i < txs.length; i++) {
yield this.send(txs[i]); tx = txs[i];
yield this.send(tx);
}
}); });
/** /**