You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

listsinceblock.py 2.6KB

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