Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mempool: remove block even if mempool is empty #916

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions lib/mempool/mempool.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,6 @@ class Mempool extends EventEmitter {
*/

async _removeBlock(block, txs) {
if (this.map.size === 0) {
this.tip = block.prevBlock;
return;
}

let total = 0;

for (let i = 1; i < txs.length; i++) {
Expand Down
53 changes: 53 additions & 0 deletions test/mempool-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const assert = require('bsert');
const random = require('bcrypto/lib/random');
const common = require('../lib/blockchain/common');
const Block = require('../lib/primitives/block');
const ChainEntry = require('../lib/blockchain/chainentry');
const MempoolEntry = require('../lib/mempool/mempoolentry');
const Mempool = require('../lib/mempool/mempool');
const AddrIndexer = require('../lib/mempool/addrindexer');
Expand Down Expand Up @@ -783,6 +784,58 @@ describe('Mempool', function() {
chaincoins.addTX(tx);
wallet.addTX(tx);
});

it('should insert unconfirmed txs from removed block', async () => {
await mempool.reset();
// Mempool is empty
assert.strictEqual(mempool.map.size, 0);

// Create 1 TX
const coin1 = chaincoins.getCoins()[0];
const addr = wallet.createReceive().getAddress();
const mtx1 = new MTX();
mtx1.addCoin(coin1);
mtx1.addOutput(addr, 90000);
chaincoins.sign(mtx1);
const tx1 = mtx1.toTX();
chaincoins.addTX(tx1);
wallet.addTX(tx1);

// Create 1 block (no need to actually add it to chain)
const block1 = await getMockBlock(chain, [tx1]);
const entry1 = await ChainEntry.fromBlock(block1, chain.tip);

// Unconfirm block into mempool
await mempool._removeBlock(entry1, block1.txs);

// Mempool should contain newly unconfirmed TX
assert(mempool.hasEntry(tx1.hash()));

// Mempool is NOT empty
assert.strictEqual(mempool.map.size, 1);

// Create second TX
const coin2 = chaincoins.getCoins()[0];
const mtx2 = new MTX();
mtx2.addCoin(coin2);
mtx2.addOutput(addr, 90000);
chaincoins.sign(mtx2);
const tx2 = mtx2.toTX();
chaincoins.addTX(tx2);
wallet.addTX(tx2);

// Create 1 block (no need to actually add it to chain)
const block2 = await getMockBlock(chain, [tx2]);
const entry2 = await ChainEntry.fromBlock(block2, chain.tip);

// Unconfirm block into mempool
await mempool._removeBlock(entry2, block2.txs);

// Mempool should contain both TXs
assert(mempool.hasEntry(tx2.hash()));
assert(mempool.hasEntry(tx1.hash()));
assert.strictEqual(mempool.map.size, 2);
});
});

describe('AddrIndexer', function () {
Expand Down