Skip to content

Commit 41cced2

Browse files
committed
Merge pull request #5267
34318d7 RPC-test based on invalidateblock for mempool coinbase spends (Gavin Andresen) 7fd6219 Make CTxMemPool::remove more effecient by avoiding recursion (Matt Corallo) b7b4318 Make CTxMemPool::check more thourough by using CheckInputs (Matt Corallo) 723d12c Remove txn which are invalidated by coinbase maturity during reorg (Matt Corallo) 868d041 Remove coinbase-dependant transactions during reorg. (Matt Corallo)
2 parents 7c001bb + 34318d7 commit 41cced2

File tree

5 files changed

+171
-15
lines changed

5 files changed

+171
-15
lines changed

qa/pull-tester/rpc-tests.sh

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ if [ "x${ENABLE_BITCOIND}${ENABLE_UTILS}${ENABLE_WALLET}" = "x111" ]; then
2525
${BUILDDIR}/qa/rpc-tests/rest.py --srcdir "${BUILDDIR}/src"
2626
${BUILDDIR}/qa/rpc-tests/mempool_spendcoinbase.py --srcdir "${BUILDDIR}/src"
2727
${BUILDDIR}/qa/rpc-tests/httpbasics.py --srcdir "${BUILDDIR}/src"
28+
${BUILDDIR}/qa/rpc-tests/mempool_coinbase_spends.py --srcdir "${BUILDDIR}/src"
2829
#${BUILDDIR}/qa/rpc-tests/forknotify.py --srcdir "${BUILDDIR}/src"
2930
else
3031
echo "No rpc tests to run. Wallet, utils, and bitcoind must all be enabled"
+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env python2
2+
# Copyright (c) 2014 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#
7+
# Test re-org scenarios with a mempool that contains transactions
8+
# that spend (directly or indirectly) coinbase transactions.
9+
#
10+
11+
from test_framework import BitcoinTestFramework
12+
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
13+
from util import *
14+
import os
15+
import shutil
16+
17+
# Create one-input, one-output, no-fee transaction:
18+
class MempoolCoinbaseTest(BitcoinTestFramework):
19+
20+
alert_filename = None # Set by setup_network
21+
22+
def setup_network(self):
23+
args = ["-checkmempool", "-debug=mempool"]
24+
self.nodes = []
25+
self.nodes.append(start_node(0, self.options.tmpdir, args))
26+
self.nodes.append(start_node(1, self.options.tmpdir, args))
27+
connect_nodes(self.nodes[1], 0)
28+
self.is_network_split = False
29+
self.sync_all
30+
31+
def create_tx(self, from_txid, to_address, amount):
32+
inputs = [{ "txid" : from_txid, "vout" : 0}]
33+
outputs = { to_address : amount }
34+
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
35+
signresult = self.nodes[0].signrawtransaction(rawtx)
36+
assert_equal(signresult["complete"], True)
37+
return signresult["hex"]
38+
39+
def run_test(self):
40+
start_count = self.nodes[0].getblockcount()
41+
42+
# Mine three blocks. After this, nodes[0] blocks
43+
# 101, 102, and 103 are spend-able.
44+
new_blocks = self.nodes[1].setgenerate(True, 4)
45+
self.sync_all()
46+
47+
node0_address = self.nodes[0].getnewaddress()
48+
node1_address = self.nodes[1].getnewaddress()
49+
50+
# Three scenarios for re-orging coinbase spends in the memory pool:
51+
# 1. Direct coinbase spend : spend_101
52+
# 2. Indirect (coinbase spend in chain, child in mempool) : spend_102 and spend_102_1
53+
# 3. Indirect (coinbase and child both in chain) : spend_103 and spend_103_1
54+
# Use invalidatblock to make all of the above coinbase spends invalid (immature coinbase),
55+
# and make sure the mempool code behaves correctly.
56+
b = [ self.nodes[0].getblockhash(n) for n in range(102, 105) ]
57+
coinbase_txids = [ self.nodes[0].getblock(h)['tx'][0] for h in b ]
58+
spend_101_raw = self.create_tx(coinbase_txids[0], node1_address, 50)
59+
spend_102_raw = self.create_tx(coinbase_txids[1], node0_address, 50)
60+
spend_103_raw = self.create_tx(coinbase_txids[2], node0_address, 50)
61+
62+
# Broadcast and mine spend_102 and 103:
63+
spend_102_id = self.nodes[0].sendrawtransaction(spend_102_raw)
64+
spend_103_id = self.nodes[0].sendrawtransaction(spend_103_raw)
65+
self.nodes[0].setgenerate(True, 1)
66+
67+
# Create 102_1 and 103_1:
68+
spend_102_1_raw = self.create_tx(spend_102_id, node1_address, 50)
69+
spend_103_1_raw = self.create_tx(spend_103_id, node1_address, 50)
70+
71+
# Broadcast and mine 103_1:
72+
spend_103_1_id = self.nodes[0].sendrawtransaction(spend_103_1_raw)
73+
self.nodes[0].setgenerate(True, 1)
74+
75+
# ... now put spend_101 and spend_102_1 in memory pools:
76+
spend_101_id = self.nodes[0].sendrawtransaction(spend_101_raw)
77+
spend_102_1_id = self.nodes[0].sendrawtransaction(spend_102_1_raw)
78+
79+
self.sync_all()
80+
81+
assert_equal(set(self.nodes[0].getrawmempool()), set([ spend_101_id, spend_102_1_id ]))
82+
83+
# Use invalidateblock to re-org back and make all those coinbase spends
84+
# immature/invalid:
85+
for node in self.nodes:
86+
node.invalidateblock(new_blocks[0])
87+
88+
self.sync_all()
89+
90+
# mempool should be empty.
91+
assert_equal(set(self.nodes[0].getrawmempool()), set())
92+
93+
if __name__ == '__main__':
94+
MempoolCoinbaseTest().main()

src/main.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1892,10 +1892,10 @@ bool static DisconnectTip(CValidationState &state) {
18921892
// ignore validation errors in resurrected transactions
18931893
list<CTransaction> removed;
18941894
CValidationState stateDummy;
1895-
if (!tx.IsCoinBase())
1896-
if (!AcceptToMemoryPool(mempool, stateDummy, tx, false, NULL))
1897-
mempool.remove(tx, removed, true);
1895+
if (tx.IsCoinBase() || !AcceptToMemoryPool(mempool, stateDummy, tx, false, NULL))
1896+
mempool.remove(tx, removed, true);
18981897
}
1898+
mempool.removeCoinbaseSpends(pcoinsTip, pindexDelete->nHeight);
18991899
mempool.check(pcoinsTip);
19001900
// Update chainActive and related variables.
19011901
UpdateTip(pindexDelete->pprev);

src/txmempool.cpp

+72-12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "txmempool.h"
77

88
#include "clientversion.h"
9+
#include "main.h"
910
#include "streams.h"
1011
#include "util.h"
1112
#include "utilmoneystr.h"
@@ -426,33 +427,64 @@ bool CTxMemPool::addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry)
426427
}
427428

428429

429-
void CTxMemPool::remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive)
430+
void CTxMemPool::remove(const CTransaction &origTx, std::list<CTransaction>& removed, bool fRecursive)
430431
{
431432
// Remove transaction from memory pool
432433
{
433434
LOCK(cs);
434-
uint256 hash = tx.GetHash();
435-
if (fRecursive) {
436-
for (unsigned int i = 0; i < tx.vout.size(); i++) {
437-
std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(hash, i));
438-
if (it == mapNextTx.end())
439-
continue;
440-
remove(*it->second.ptx, removed, true);
441-
}
442-
}
443-
if (mapTx.count(hash))
435+
std::deque<uint256> txToRemove;
436+
txToRemove.push_back(origTx.GetHash());
437+
while (!txToRemove.empty())
444438
{
445-
removed.push_front(tx);
439+
uint256 hash = txToRemove.front();
440+
txToRemove.pop_front();
441+
if (!mapTx.count(hash))
442+
continue;
443+
const CTransaction& tx = mapTx[hash].GetTx();
444+
if (fRecursive) {
445+
for (unsigned int i = 0; i < tx.vout.size(); i++) {
446+
std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(hash, i));
447+
if (it == mapNextTx.end())
448+
continue;
449+
txToRemove.push_back(it->second.ptx->GetHash());
450+
}
451+
}
446452
BOOST_FOREACH(const CTxIn& txin, tx.vin)
447453
mapNextTx.erase(txin.prevout);
448454

455+
removed.push_back(tx);
449456
totalTxSize -= mapTx[hash].GetTxSize();
450457
mapTx.erase(hash);
451458
nTransactionsUpdated++;
452459
}
453460
}
454461
}
455462

463+
void CTxMemPool::removeCoinbaseSpends(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight)
464+
{
465+
// Remove transactions spending a coinbase which are now immature
466+
LOCK(cs);
467+
list<CTransaction> transactionsToRemove;
468+
for (std::map<uint256, CTxMemPoolEntry>::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
469+
const CTransaction& tx = it->second.GetTx();
470+
BOOST_FOREACH(const CTxIn& txin, tx.vin) {
471+
std::map<uint256, CTxMemPoolEntry>::const_iterator it2 = mapTx.find(txin.prevout.hash);
472+
if (it2 != mapTx.end())
473+
continue;
474+
const CCoins *coins = pcoins->AccessCoins(txin.prevout.hash);
475+
if (fSanityCheck) assert(coins);
476+
if (!coins || (coins->IsCoinBase() && nMemPoolHeight - coins->nHeight < COINBASE_MATURITY)) {
477+
transactionsToRemove.push_back(tx);
478+
break;
479+
}
480+
}
481+
}
482+
BOOST_FOREACH(const CTransaction& tx, transactionsToRemove) {
483+
list<CTransaction> removed;
484+
remove(tx, removed, true);
485+
}
486+
}
487+
456488
void CTxMemPool::removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed)
457489
{
458490
// Remove transactions which depend on inputs of tx, recursively
@@ -513,17 +545,22 @@ void CTxMemPool::check(const CCoinsViewCache *pcoins) const
513545

514546
uint64_t checkTotal = 0;
515547

548+
CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(pcoins));
549+
516550
LOCK(cs);
551+
list<const CTxMemPoolEntry*> waitingOnDependants;
517552
for (std::map<uint256, CTxMemPoolEntry>::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
518553
unsigned int i = 0;
519554
checkTotal += it->second.GetTxSize();
520555
const CTransaction& tx = it->second.GetTx();
556+
bool fDependsWait = false;
521557
BOOST_FOREACH(const CTxIn &txin, tx.vin) {
522558
// Check that every mempool transaction's inputs refer to available coins, or other mempool tx's.
523559
std::map<uint256, CTxMemPoolEntry>::const_iterator it2 = mapTx.find(txin.prevout.hash);
524560
if (it2 != mapTx.end()) {
525561
const CTransaction& tx2 = it2->second.GetTx();
526562
assert(tx2.vout.size() > txin.prevout.n && !tx2.vout[txin.prevout.n].IsNull());
563+
fDependsWait = true;
527564
} else {
528565
const CCoins* coins = pcoins->AccessCoins(txin.prevout.hash);
529566
assert(coins && coins->IsAvailable(txin.prevout.n));
@@ -535,6 +572,29 @@ void CTxMemPool::check(const CCoinsViewCache *pcoins) const
535572
assert(it3->second.n == i);
536573
i++;
537574
}
575+
if (fDependsWait)
576+
waitingOnDependants.push_back(&it->second);
577+
else {
578+
CValidationState state; CTxUndo undo;
579+
assert(CheckInputs(tx, state, mempoolDuplicate, false, 0, false, NULL));
580+
UpdateCoins(tx, state, mempoolDuplicate, undo, 1000000);
581+
}
582+
}
583+
unsigned int stepsSinceLastRemove = 0;
584+
while (!waitingOnDependants.empty()) {
585+
const CTxMemPoolEntry* entry = waitingOnDependants.front();
586+
waitingOnDependants.pop_front();
587+
CValidationState state;
588+
if (!mempoolDuplicate.HaveInputs(entry->GetTx())) {
589+
waitingOnDependants.push_back(entry);
590+
stepsSinceLastRemove++;
591+
assert(stepsSinceLastRemove < waitingOnDependants.size());
592+
} else {
593+
assert(CheckInputs(entry->GetTx(), state, mempoolDuplicate, false, 0, false, NULL));
594+
CTxUndo undo;
595+
UpdateCoins(entry->GetTx(), state, mempoolDuplicate, undo, 1000000);
596+
stepsSinceLastRemove = 0;
597+
}
538598
}
539599
for (std::map<COutPoint, CInPoint>::const_iterator it = mapNextTx.begin(); it != mapNextTx.end(); it++) {
540600
uint256 hash = it->second.ptx->GetHash();

src/txmempool.h

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ class CTxMemPool
113113

114114
bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry);
115115
void remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive = false);
116+
void removeCoinbaseSpends(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight);
116117
void removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed);
117118
void removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
118119
std::list<CTransaction>& conflicts);

0 commit comments

Comments
 (0)