Skip to content

Commit

Permalink
wallet: check MTX for high and low fee errors along with policy limits
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Nov 29, 2022
1 parent 362d05d commit 48d93c4
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
10 changes: 10 additions & 0 deletions lib/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -3958,6 +3958,16 @@ class Wallet extends EventEmitter {
if (tx.getWeight() > policy.MAX_TX_WEIGHT)
throw new Error('TX exceeds policy weight.');

const minFee = policy.getMinFee(
mtx.getVirtualSize(),
this.network.minRelay
);
const fee = mtx.getFee();
if (fee < minFee)
throw new Error('Fee is below minimum relay limit.');
if (fee > minFee * 10000)
throw new Error('Fee exceeds absurd limit.');

const ancestors = await this.getPendingAncestors(tx);
if (ancestors.size + 1 > this.maxAncestors)
throw new Error('TX exceeds maximum unconfirmed ancestors.');
Expand Down
4 changes: 2 additions & 2 deletions test/wallet-auction-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ describe('Wallet Auction', function() {
actions.push(['NONE', addr, 10000]);
}

const batch = await wallet.sendBatch(actions, {hardFee: 10});
const batch = await wallet.sendBatch(actions, {hardFee: 1000});

assert.strictEqual(batch.outputs.length, 11);

Expand All @@ -597,7 +597,7 @@ describe('Wallet Auction', function() {

const newBal = await wallet.getBalance();

assert.strictEqual(oldBal.confirmed - newBal.confirmed, 100010);
assert.strictEqual(oldBal.confirmed - newBal.confirmed, 101000);
});

it('should verify expected name properties', async () => {
Expand Down
54 changes: 54 additions & 0 deletions test/wallet-coinselection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,59 @@ describe('Wallet Coin Selection', function () {
assert(rate < network.maxFeeRate);
assert(fee > network.minRelay);
});

it('should fail to pay absurd fee rate for small tx', async () => {
const address = await wallet.receiveAddress();
await assert.rejects(
wallet.send({
outputs: [{
address,
value: 5e6
}],
rate: 10001 * network.minRelay
}),
{message: 'Fee exceeds absurd limit.'}
);
});

it('should pay fee just under the absurd limit', async () => {
const address = await wallet.receiveAddress();
const tx = await wallet.send({
outputs: [{
address,
value: 5e6
}],
rate: 10000 * network.minRelay
});
const view = await wallet.getWalletCoinView(tx);
assert.strictEqual(tx.getRate(view), 10000 * network.minRelay);
});

it('should fail to pay too-low fee rate for small tx', async () => {
const address = await wallet.receiveAddress();
await assert.rejects(
wallet.send({
outputs: [{
address,
value: 5e6
}],
rate: network.minRelay - 1
}),
{message: 'Fee is below minimum relay limit.'}
);
});

it('should pay fee at the minimum relay limit', async () => {
const address = await wallet.receiveAddress();
const tx = await wallet.send({
outputs: [{
address,
value: 5e6
}],
rate: network.minRelay
});
const view = await wallet.getWalletCoinView(tx);
assert.strictEqual(tx.getRate(view), network.minRelay);
});
});
});

0 comments on commit 48d93c4

Please sign in to comment.