Skip to content

Commit 1c7e09f

Browse files
committed
Merge pull request #5369
b2d0162 Test resurrecting memory pool transactions during chain re-org (Gavin Andresen) 3dd8ed7 Delay writing block indexes in invalidate/reconsider (Pieter Wuille) 798faec Add 'invalidateblock' and 'reconsiderblock' RPC commands. (Pieter Wuille)
2 parents 90f7aa7 + b2d0162 commit 1c7e09f

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

qa/pull-tester/rpc-tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ fi
1818
if [ "x${ENABLE_BITCOIND}${ENABLE_UTILS}${ENABLE_WALLET}" = "x111" ]; then
1919
${BUILDDIR}/qa/rpc-tests/wallet.py --srcdir "${BUILDDIR}/src"
2020
${BUILDDIR}/qa/rpc-tests/listtransactions.py --srcdir "${BUILDDIR}/src"
21+
${BUILDDIR}/qa/rpc-tests/mempool_resurrect_test.py --srcdir "${BUILDDIR}/src"
2122
${BUILDDIR}/qa/rpc-tests/txn_doublespend.py --srcdir "${BUILDDIR}/src"
2223
${BUILDDIR}/qa/rpc-tests/txn_doublespend.py --mineblock --srcdir "${BUILDDIR}/src"
2324
${BUILDDIR}/qa/rpc-tests/getchaintips.py --srcdir "${BUILDDIR}/src"
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env python
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 resurrection of mined transactions when
8+
# the blockchain is re-organized.
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+
def setup_network(self):
21+
# Just need one node for this test
22+
args = ["-checkmempool", "-debug=mempool"]
23+
self.nodes = []
24+
self.nodes.append(start_node(0, self.options.tmpdir, args))
25+
self.is_network_split = False
26+
27+
def create_tx(self, from_txid, to_address, amount):
28+
inputs = [{ "txid" : from_txid, "vout" : 0}]
29+
outputs = { to_address : amount }
30+
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
31+
signresult = self.nodes[0].signrawtransaction(rawtx)
32+
assert_equal(signresult["complete"], True)
33+
return signresult["hex"]
34+
35+
def run_test(self):
36+
node0_address = self.nodes[0].getnewaddress()
37+
38+
# Spend block 1/2/3's coinbase transactions
39+
# Mine a block.
40+
# Create three more transactions, spending the spends
41+
# Mine another block.
42+
# ... make sure all the transactions are confirmed
43+
# Invalidate both blocks
44+
# ... make sure all the transactions are put back in the mempool
45+
# Mine a new block
46+
# ... make sure all the transactions are confirmed again.
47+
48+
b = [ self.nodes[0].getblockhash(n) for n in range(1, 4) ]
49+
coinbase_txids = [ self.nodes[0].getblock(h)['tx'][0] for h in b ]
50+
spends1_raw = [ self.create_tx(txid, node0_address, 50) for txid in coinbase_txids ]
51+
spends1_id = [ self.nodes[0].sendrawtransaction(tx) for tx in spends1_raw ]
52+
53+
blocks = []
54+
blocks.extend(self.nodes[0].setgenerate(True, 1))
55+
56+
spends2_raw = [ self.create_tx(txid, node0_address, 49.99) for txid in spends1_id ]
57+
spends2_id = [ self.nodes[0].sendrawtransaction(tx) for tx in spends2_raw ]
58+
59+
blocks.extend(self.nodes[0].setgenerate(True, 1))
60+
61+
# mempool should be empty, all txns confirmed
62+
assert_equal(set(self.nodes[0].getrawmempool()), set())
63+
for txid in spends1_id+spends2_id:
64+
tx = self.nodes[0].gettransaction(txid)
65+
assert(tx["confirmations"] > 0)
66+
67+
# Use invalidateblock to re-org back; all transactions should
68+
# end up unconfirmed and back in the mempool
69+
for node in self.nodes:
70+
node.invalidateblock(blocks[0])
71+
72+
# mempool should be empty, all txns confirmed
73+
assert_equal(set(self.nodes[0].getrawmempool()), set(spends1_id+spends2_id))
74+
for txid in spends1_id+spends2_id:
75+
tx = self.nodes[0].gettransaction(txid)
76+
assert(tx["confirmations"] == 0)
77+
78+
# Generate another block, they should all get mined
79+
self.nodes[0].setgenerate(True, 1)
80+
# mempool should be empty, all txns confirmed
81+
assert_equal(set(self.nodes[0].getrawmempool()), set())
82+
for txid in spends1_id+spends2_id:
83+
tx = self.nodes[0].gettransaction(txid)
84+
assert(tx["confirmations"] > 0)
85+
86+
87+
if __name__ == '__main__':
88+
MempoolCoinbaseTest().main()

src/rpcserver.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ static const CRPCCommand vRPCCommands[] =
269269
{ "blockchain", "gettxout", &gettxout, true, false, false },
270270
{ "blockchain", "gettxoutsetinfo", &gettxoutsetinfo, true, false, false },
271271
{ "blockchain", "verifychain", &verifychain, true, false, false },
272+
{ "blockchain", "invalidateblock", &invalidateblock, true, true, false },
273+
{ "blockchain", "reconsiderblock", &reconsiderblock, true, true, false },
272274

273275
/* Mining */
274276
{ "mining", "getblocktemplate", &getblocktemplate, true, false, false },

0 commit comments

Comments
 (0)