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
* in the wallet db (for resending).
* Resend all pending transactions.
* @returns {Promise}
*/
WalletDB.prototype.getPendingKeys = co(function* getPendingKeys() {
var layout = layouts.txdb;
var dummy = new Buffer(0);
var keys = [];
var iter, item;
WalletDB.prototype.resend = co(function* resend() {
var iter, item, wid;
iter = this.db.iterator({
gte: layout.prefix(0x00000000, dummy),
lte: layout.prefix(0xffffffff, dummy)
gte: layout.w(0x00000000),
lte: layout.w(0xffffffff)
});
for (;;) {
@ -1431,61 +1427,47 @@ WalletDB.prototype.getPendingKeys = co(function* getPendingKeys() {
if (!item)
break;
if (item.key[5] === 0x70)
keys.push(item.key);
try {
wid = layout.ww(item.key);
yield this.resendPending(wid);
} catch (e) {
yield iter.end();
throw e;
}
}
return keys;
});
/**
* Get keys of all pending transactions
* in the wallet db (for resending).
* Resend all pending transactions for a specific wallet.
* @private
* @param {WalletID} wid
* @returns {Promise}
*/
WalletDB.prototype.getPendingTX = co(function* getPendingTX() {
WalletDB.prototype.resendPending = co(function* resendPending(wid) {
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 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)
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++) {
key = keys[i];
hash = layout.pp(key);
key = layout.prefix(wid, layout.t(hash));
data = yield this.db.get(key);
if (!data)
@ -1501,8 +1483,10 @@ WalletDB.prototype.resend = co(function* resend() {
txs = common.sortTX(txs);
for (i = 0; i < txs.length; i++)
yield this.send(txs[i]);
for (i = 0; i < txs.length; i++) {
tx = txs[i];
yield this.send(tx);
}
});
/**