diff --git a/test/mempool-test.js b/test/mempool-test.js index 419022119..2877ad2fa 100644 --- a/test/mempool-test.js +++ b/test/mempool-test.js @@ -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; @@ -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(); @@ -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();