Skip to content

Commit 727a798

Browse files
committed
Merge #9516: Bug-fix: listsinceblock: use fork point as reference for blocks in reorg'd chains
7ba0a00 Testing: listsinceblock should not use orphan block height. (Karl-Johan Alm) ee5c1ce Bug-fix: listsinceblock: use closest common ancestor when a block hash was provided for a chain that was not the main chain. (Karl-Johan Alm)
2 parents 5cf3c60 + 7ba0a00 commit 727a798

File tree

3 files changed

+95
-4
lines changed

3 files changed

+95
-4
lines changed

qa/pull-tester/rpc-tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
'import-rescan.py',
154154
'bumpfee.py',
155155
'rpcnamedargs.py',
156+
'listsinceblock.py',
156157
]
157158
if ENABLE_ZMQ:
158159
testScripts.append('zmq_test.py')

qa/rpc-tests/listsinceblock.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2017 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+
from test_framework.test_framework import BitcoinTestFramework
7+
from test_framework.util import assert_equal
8+
9+
class ListSinceBlockTest (BitcoinTestFramework):
10+
11+
def __init__(self):
12+
super().__init__()
13+
self.setup_clean_chain = True
14+
self.num_nodes = 4
15+
16+
def run_test (self):
17+
'''
18+
`listsinceblock` did not behave correctly when handed a block that was
19+
no longer in the main chain:
20+
21+
ab0
22+
/ \
23+
aa1 [tx0] bb1
24+
| |
25+
aa2 bb2
26+
| |
27+
aa3 bb3
28+
|
29+
bb4
30+
31+
Consider a client that has only seen block `aa3` above. It asks the node
32+
to `listsinceblock aa3`. But at some point prior the main chain switched
33+
to the bb chain.
34+
35+
Previously: listsinceblock would find height=4 for block aa3 and compare
36+
this to height=5 for the tip of the chain (bb4). It would then return
37+
results restricted to bb3-bb4.
38+
39+
Now: listsinceblock finds the fork at ab0 and returns results in the
40+
range bb1-bb4.
41+
42+
This test only checks that [tx0] is present.
43+
'''
44+
45+
assert_equal(self.is_network_split, False)
46+
self.nodes[2].generate(101)
47+
self.sync_all()
48+
49+
assert_equal(self.nodes[0].getbalance(), 0)
50+
assert_equal(self.nodes[1].getbalance(), 0)
51+
assert_equal(self.nodes[2].getbalance(), 50)
52+
assert_equal(self.nodes[3].getbalance(), 0)
53+
54+
# Split network into two
55+
self.split_network()
56+
assert_equal(self.is_network_split, True)
57+
58+
# send to nodes[0] from nodes[2]
59+
senttx = self.nodes[2].sendtoaddress(self.nodes[0].getnewaddress(), 1)
60+
61+
# generate on both sides
62+
lastblockhash = self.nodes[1].generate(6)[5]
63+
self.nodes[2].generate(7)
64+
print('lastblockhash=%s' % (lastblockhash))
65+
66+
self.sync_all()
67+
68+
self.join_network()
69+
70+
# listsinceblock(lastblockhash) should now include tx, as seen from nodes[0]
71+
lsbres = self.nodes[0].listsinceblock(lastblockhash)
72+
found = False
73+
for tx in lsbres['transactions']:
74+
if tx['txid'] == senttx:
75+
found = True
76+
break
77+
assert_equal(found, True)
78+
79+
if __name__ == '__main__':
80+
ListSinceBlockTest().main()

src/wallet/rpcwallet.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,7 @@ UniValue listsinceblock(const JSONRPCRequest& request)
16811681

16821682
LOCK2(cs_main, pwalletMain->cs_wallet);
16831683

1684-
CBlockIndex *pindex = NULL;
1684+
const CBlockIndex *pindex = NULL;
16851685
int target_confirms = 1;
16861686
isminefilter filter = ISMINE_SPENDABLE;
16871687

@@ -1692,7 +1692,16 @@ UniValue listsinceblock(const JSONRPCRequest& request)
16921692
blockId.SetHex(request.params[0].get_str());
16931693
BlockMap::iterator it = mapBlockIndex.find(blockId);
16941694
if (it != mapBlockIndex.end())
1695+
{
16951696
pindex = it->second;
1697+
if (chainActive[pindex->nHeight] != pindex)
1698+
{
1699+
// the block being asked for is a part of a deactivated chain;
1700+
// we don't want to depend on its perceived height in the block
1701+
// chain, we want to instead use the last common ancestor
1702+
pindex = chainActive.FindFork(pindex);
1703+
}
1704+
}
16961705
}
16971706

16981707
if (request.params.size() > 1)
@@ -1703,9 +1712,10 @@ UniValue listsinceblock(const JSONRPCRequest& request)
17031712
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter");
17041713
}
17051714

1706-
if(request.params.size() > 2)
1707-
if(request.params[2].get_bool())
1708-
filter = filter | ISMINE_WATCH_ONLY;
1715+
if (request.params.size() > 2 && request.params[2].get_bool())
1716+
{
1717+
filter = filter | ISMINE_WATCH_ONLY;
1718+
}
17091719

17101720
int depth = pindex ? (1 + chainActive.Height() - pindex->nHeight) : -1;
17111721

0 commit comments

Comments
 (0)