Skip to content

Commit

Permalink
wallet: don't add coinbase txs to the pending list.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Oct 23, 2023
1 parent bb7da60 commit 1b78312
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/wallet/txdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,7 @@ class TXDB {
* database. Disconnect inputs.
* @private
* @param {TXRecord} wtx
* @returns {Promise<Details?>}
* @returns {Promise<Details>}
*/

async erase(wtx, block) {
Expand Down Expand Up @@ -1495,7 +1495,7 @@ class TXDB {
* @private
* @param {Hash} hash
* @param {Number} height
* @returns {Promise}
* @returns {Promise<Details?>}
*/

async unconfirm(hash, height) {
Expand All @@ -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<Details>}
*/

async disconnect(wtx, block) {
Expand Down Expand Up @@ -2947,7 +2952,7 @@ class TXDB {
/**
* Get transaction.
* @param {Hash} hash
* @returns {Promise} - Returns {@link TX}.
* @returns {Promise<TXRecord>}
*/

async getTX(hash) {
Expand Down
45 changes: 45 additions & 0 deletions test/wallet-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

const pendingBefore = await wallet.getPending();
assert.strictEqual(pendingBefore.length, 0);

assert(cbTX.isCoinbase());
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();

Expand Down

0 comments on commit 1b78312

Please sign in to comment.