|
| 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() |
0 commit comments