Skip to content

Commit

Permalink
test: cover mempool fee too high and too low
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Nov 29, 2022
1 parent 0b71f5f commit efe3467
Showing 1 changed file with 81 additions and 2 deletions.
83 changes: 81 additions & 2 deletions test/mempool-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const wallet = new MemWallet({ network });

let cachedTX = null;

function dummyInput(addr, hash) {
function dummyInput(addr, hash, value = 70000) {
const coin = new Coin();
coin.height = 0;
coin.value = 0;
Expand All @@ -73,7 +73,7 @@ function dummyInput(addr, hash) {

const fund = new MTX();
fund.addCoin(coin);
fund.addOutput(addr, 70000);
fund.addOutput(addr, value);

const [tx, view] = fund.commit();

Expand Down Expand Up @@ -465,6 +465,85 @@ describe('Mempool', function() {
}
});

it('should reject absurd fee', async () => {
const wallet = new MemWallet({ network });
const addr = wallet.getAddress();
const funds = 10000e6;

const mtx = new MTX();
mtx.addCoin(
dummyInput(
addr,
random.randomBytes(32),
funds
)
);
mtx.addOutput(wallet.getAddress(), 0); // temp
wallet.sign(mtx);

const vsize = mtx.getVirtualSize();
const minFee = (vsize / 1000) * network.minRelay;
const absurdFee = minFee * 10000;

// Revise with exactly absurd fee
mtx.outputs[0].value = funds - absurdFee - 1;
mtx.inputs[0].witness.items.length = 0;
wallet.sign(mtx);
const tx1 = mtx.toTX();

await assert.rejects(
mempool.addTX(tx1),
{message: /absurdly-high-fee/}
);

// Revise again with just under absurd fee
mtx.outputs[0].value = funds - absurdFee;
mtx.inputs[0].witness.items.length = 0;
wallet.sign(mtx);
const tx2 = mtx.toTX();

await mempool.addTX(tx2);
});

it('should reject too-low fee', async () => {
const wallet = new MemWallet({ network });
const addr = wallet.getAddress();
const funds = 10000e6;

const mtx = new MTX();
mtx.addCoin(
dummyInput(
addr,
random.randomBytes(32),
funds
)
);
mtx.addOutput(wallet.getAddress(), 0); // temp
wallet.sign(mtx);

const vsize = mtx.getVirtualSize();
const minFee = (vsize / 1000) * network.minRelay;

// Revise with just under minFee
mtx.outputs[0].value = funds - minFee + 1;
mtx.inputs[0].witness.items.length = 0;
wallet.sign(mtx);
const tx1 = mtx.toTX();

await assert.rejects(
mempool.addTX(tx1),
{message: /insufficient priority/}
);

// Revise again with exactly minFee
mtx.outputs[0].value = funds - minFee;
mtx.inputs[0].witness.items.length = 0;
wallet.sign(mtx);
const tx2 = mtx.toTX();

await mempool.addTX(tx2);
});

it('should destroy mempool', async () => {
await mempool.close();
await chain.close();
Expand Down

0 comments on commit efe3467

Please sign in to comment.