txdb: fix limit on value coin selector.
This commit is contained in:
parent
44589a974e
commit
7ed3a340d4
2 changed files with 27 additions and 1 deletions
|
|
@ -3821,7 +3821,7 @@ class TXDB {
|
|||
* @param {Object} [options]
|
||||
* @param {Number} [options.minValue=0]
|
||||
* @param {Number} [options.maxValue=MAX_MONEY]
|
||||
* @param {Number} [options.limit=-1]
|
||||
* @param {Number?} [options.limit]
|
||||
* @param {Boolean} [options.reverse=false]
|
||||
* @param {Boolean} [options.inclusive=true]
|
||||
* @returns {AsyncGenerator<Credit>}
|
||||
|
|
@ -3872,6 +3872,7 @@ class TXDB {
|
|||
iterOpts[lesser] = max;
|
||||
|
||||
const iter = this.bucket.iterator(iterOpts);
|
||||
let items = 0;
|
||||
|
||||
for await (const key of iter.keysAsync()) {
|
||||
const decoded = prefix.decode(key);
|
||||
|
|
@ -3881,6 +3882,7 @@ class TXDB {
|
|||
|
||||
assert(credit);
|
||||
yield credit;
|
||||
items++;
|
||||
}
|
||||
|
||||
// now process unconfirmed.
|
||||
|
|
@ -3897,6 +3899,13 @@ class TXDB {
|
|||
iterOpts[greater] = min;
|
||||
iterOpts[lesser] = max;
|
||||
|
||||
if (limit != null && limit > 0) {
|
||||
if (items >= limit)
|
||||
return;
|
||||
|
||||
iterOpts.limit = limit - items;
|
||||
}
|
||||
|
||||
const ucIter = this.bucket.iterator(iterOpts);
|
||||
|
||||
for await (const key of ucIter.keysAsync()) {
|
||||
|
|
|
|||
|
|
@ -102,6 +102,17 @@ describe('Wallet Coin Selection', function() {
|
|||
|
||||
let isSorted, getCredits;
|
||||
const sumCredits = credits => credits.reduce((acc, c) => acc + c.coin.value, 0);
|
||||
const checkWithLimits = async (credits, wallet, acct) => {
|
||||
for (let i = 1; i < credits.length; i++) {
|
||||
const creditsLimit = await getCredits(wallet, acct, {
|
||||
limit: i
|
||||
});
|
||||
assert.strictEqual(creditsLimit.length, i);
|
||||
assert(isSorted(creditsLimit), 'Credits not sorted.');
|
||||
assert.deepStrictEqual(creditsLimit, credits.slice(0, i));
|
||||
assert(sumCredits(creditsLimit) === sumCredits(credits.slice(0, i)));
|
||||
}
|
||||
};
|
||||
|
||||
before(() => {
|
||||
switch (indexType) {
|
||||
|
|
@ -631,15 +642,21 @@ describe('Wallet Coin Selection', function() {
|
|||
assert(isSorted(credits0), 'Credits not sorted.');
|
||||
assert(sumCredits(credits0) === sum0);
|
||||
|
||||
await checkWithLimits(credits0, wallet);
|
||||
|
||||
const credits1 = await getCredits(wallet, 1);
|
||||
assert.strictEqual(credits1.length, 4);
|
||||
assert(isSorted(credits1), 'Credits not sorted.');
|
||||
assert(sumCredits(credits1) === sum1);
|
||||
|
||||
await checkWithLimits(credits1, wallet, 1);
|
||||
|
||||
const both = await getCredits(wallet, -1);
|
||||
assert.strictEqual(both.length, 8);
|
||||
assert(isSorted(both), 'Credits not sorted.');
|
||||
assert(sumCredits(both) === sum0 + sum1);
|
||||
|
||||
await checkWithLimits(both, wallet, -1);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue