From 84ec94596c728df727282dae4d8420dff2a2b2a6 Mon Sep 17 00:00:00 2001 From: Nodari Chkuaselidze Date: Mon, 16 Oct 2023 13:25:18 +0400 Subject: [PATCH] wallet: don't add coinbase txs to the pending list. --- lib/wallet/txdb.js | 13 +++++++++---- test/wallet-test.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index 734ef7305..855186502 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -1304,7 +1304,7 @@ class TXDB { * database. Disconnect inputs. * @private * @param {TXRecord} wtx - * @returns {Promise} + * @returns {Promise
} */ async erase(wtx, block) { @@ -1495,7 +1495,7 @@ class TXDB { * @private * @param {Hash} hash * @param {Number} height - * @returns {Promise} + * @returns {Promise} */ async unconfirm(hash, height) { @@ -1522,13 +1522,18 @@ class TXDB { if (wtx.height === -1) return null; + const tx = wtx.tx; + + if (tx.isCoinbase()) + return this.removeRecursive(wtx); + return this.disconnect(wtx, wtx.getBlock()); } /** * Unconfirm a transaction. Necessary after a reorg. * @param {TXRecord} wtx - * @returns {Promise} + * @returns {Promise
} */ async disconnect(wtx, block) { @@ -2947,7 +2952,7 @@ class TXDB { /** * Get transaction. * @param {Hash} hash - * @returns {Promise} - Returns {@link TX}. + * @returns {Promise} */ async getTX(hash) { diff --git a/test/wallet-test.js b/test/wallet-test.js index 76b5d3c13..6dc2b00b6 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -382,6 +382,51 @@ describe('Wallet', function() { } }); + it('should remove unconfirmed coinbase tx', async () => { + const wallet = await wdb.create(); + + const block = nextBlock(wdb); + const normalTX = new MTX(); + + normalTX.addInput(dummyInput()); + normalTX.addOutput(await wallet.receiveAddress(), 5000); + + assert(!normalTX.isCoinbase()); + + const cbTX = new MTX(); + + cbTX.addInput({ + prevout: new Outpoint() + }); + + cbTX.addOutput(await wallet.receiveAddress(), 5000); + assert(cbTX.isCoinbase()); + + const pendingBefore = await wallet.getPending(); + assert.strictEqual(pendingBefore.length, 0); + + await wdb.addBlock(block, [cbTX.toTX(), normalTX.toTX()]); + + const balanceBefore = await wallet.getBalance(); + + assert.strictEqual(balanceBefore.confirmed, balanceBefore.unconfirmed); + assert.strictEqual(balanceBefore.confirmed, 10000); + assert.strictEqual(balanceBefore.tx, 2); + assert.strictEqual(balanceBefore.coin, 2); + + await wdb.removeBlock(block, [cbTX.toTX(), normalTX.toTX()]); + const pending = await wallet.getPending(); + + assert.strictEqual(pending.length, 1); + + const balance = await wallet.getBalance(); + + assert.strictEqual(balance.confirmed, 0); + assert.strictEqual(balance.unconfirmed, 5000); + assert.strictEqual(balance.tx, 1); + assert.strictEqual(balance.coin, 1); + }); + it('should handle double-spend (not our input)', async () => { const wallet = await wdb.create();