Skip to content

Commit

Permalink
mtx: remove CoinSelector.MAX_FEE
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Nov 29, 2022
1 parent e07ba54 commit 0b71f5f
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 24 deletions.
20 changes: 4 additions & 16 deletions lib/primitives/mtx.js
Original file line number Diff line number Diff line change
Expand Up @@ -1662,13 +1662,10 @@ class CoinSelector {
// This is mostly here for testing.
// i.e. A fee rounded to the nearest
// kb is easier to predict ahead of time.
if (this.round) {
const fee = policy.getRoundFee(size, this.rate);
return Math.min(fee, CoinSelector.MAX_FEE);
}
if (this.round)
return policy.getRoundFee(size, this.rate);

const fee = policy.getMinFee(size, this.rate);
return Math.min(fee, CoinSelector.MAX_FEE);
return policy.getMinFee(size, this.rate);
}

/**
Expand Down Expand Up @@ -1803,7 +1800,7 @@ class CoinSelector {
*/

selectHard() {
this.fee = Math.min(this.hardFee, CoinSelector.MAX_FEE);
this.fee = this.hardFee;
this.fund();
}
}
Expand All @@ -1826,15 +1823,6 @@ CoinSelector.FEE_RATE = 10000;

CoinSelector.MIN_FEE = 10000;

/**
* Maximum fee to allow
* after coin selection.
* @const {Amount}
* @default
*/

CoinSelector.MAX_FEE = consensus.COIN / 10;

/**
* Funding Error
* An error thrown from the coin selector.
Expand Down
7 changes: 1 addition & 6 deletions lib/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -1186,8 +1186,6 @@ class Wallet extends EventEmitter {
maxFee: options.maxFee,
estimate: prev => this.estimateSize(prev)
});

assert(mtx.getFee() <= MTX.Selector.MAX_FEE, 'TX exceeds MAX_FEE.');
}

/**
Expand Down Expand Up @@ -4018,10 +4016,7 @@ class Wallet extends EventEmitter {

const oldFee = tx.getFee(view);

let fee = tx.getMinFee(null, rate);

if (fee > MTX.Selector.MAX_FEE)
fee = MTX.Selector.MAX_FEE;
const fee = tx.getMinFee(null, rate);

if (oldFee >= fee)
throw new Error('Fee is not increasing.');
Expand Down
2 changes: 0 additions & 2 deletions test/util/memwallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -1669,8 +1669,6 @@ class MemWallet {
async _create(mtx, options) {
await this.fund(mtx, options);

assert(mtx.getFee() <= MTX.Selector.MAX_FEE, 'TX exceeds MAX_FEE.');

mtx.sortMembers();

if (options && options.locktime != null)
Expand Down
106 changes: 106 additions & 0 deletions test/wallet-coinselection-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
'use strict';

const assert = require('bsert');
const {
MTX,
Network,
WalletDB
} = require('..');

// Use main instead of regtest because (deprecated)
// CoinSelector.MAX_FEE was network agnostic
const network = Network.get('main');

async function fundWallet(wallet, amounts) {
assert(Array.isArray(amounts));

const mtx = new MTX();
const addr = await wallet.receiveAddress();
for (const amt of amounts) {
mtx.addOutput(addr, amt);
}

const height = wallet.wdb.height + 1;
const hash = Buffer.alloc(32);
hash.writeUInt16BE(height);
const dummyBlock = {
hash,
height,
time: Date.now()
};
await wallet.wdb.addBlock(dummyBlock, [mtx.toTX()]);
}

describe('Wallet Coin Selection', function () {
describe('Fees', function () {
const wdb = new WalletDB({network});
let wallet;

before(async () => {
await wdb.open();
wdb.height = network.txStart + 1;
wdb.state.height = wdb.height;
wallet = wdb.primary;
});

after(async () => {
await wdb.close();
});

it('should fund wallet', async () => {
await fundWallet(wallet, [100e6, 10e6, 1e6, 100000, 10000]);
const bal = await wallet.getBalance();
assert.strictEqual(bal.confirmed, 111110000);
});

it('should pay default fee rate for small tx', async () => {
const address = await wallet.receiveAddress();
const mtx = new MTX();
mtx.addOutput(address, 5e6);
await wallet.fund(mtx);
await wallet.sign(mtx);

assert.strictEqual(mtx.inputs.length, 1);
assert.strictEqual(mtx.outputs.length, 2);

const rate = mtx.getRate();
const fee = mtx.getFee();

assert.strictEqual(rate, network.feeRate);
assert(rate < network.maxFeeRate);
assert(fee > network.minRelay);
});

it('should pay default fee rate for maximum policy weight TX', async () => {
const address = await wallet.receiveAddress();
const mtx = new MTX();
for (let i = 0; i < 3120; i++) {
mtx.addOutput(address, 500);
}
// Add nulldata output to add precise amount of extra weight
mtx.addOutput(
{
version: 31,
hash: Buffer.alloc(38)
},
0
);
await wallet.fund(mtx);
await wallet.sign(mtx);

// This is as close as we can get to
// policy.MAX_TX_WEIGHT (400000) using standard wallet
assert.strictEqual(mtx.getWeight(), 399997);
assert.strictEqual(mtx.inputs.length, 1);

const rate = mtx.getRate();
const fee = mtx.getFee();

assert.strictEqual(fee, 10e6); // 10 HNS

assert.strictEqual(rate, network.feeRate);
assert(rate < network.maxFeeRate);
assert(fee > network.minRelay);
});
});
});

0 comments on commit 0b71f5f

Please sign in to comment.