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.

merkle_blocks.py 3.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2014-2016 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 gettxoutproof and verifytxoutproof RPCs."""
  6. from test_framework.test_framework import BitcoinTestFramework
  7. from test_framework.util import *
  8. class MerkleBlockTest(BitcoinTestFramework):
  9. def __init__(self):
  10. super().__init__()
  11. self.setup_clean_chain = True
  12. self.num_nodes = 4
  13. def setup_network(self):
  14. self.nodes = []
  15. # Nodes 0/1 are "wallet" nodes
  16. self.nodes.append(start_node(0, self.options.tmpdir))
  17. self.nodes.append(start_node(1, self.options.tmpdir))
  18. # Nodes 2/3 are used for testing
  19. self.nodes.append(start_node(2, self.options.tmpdir))
  20. self.nodes.append(start_node(3, self.options.tmpdir, ["-txindex"]))
  21. connect_nodes(self.nodes[0], 1)
  22. connect_nodes(self.nodes[0], 2)
  23. connect_nodes(self.nodes[0], 3)
  24. self.is_network_split = False
  25. self.sync_all()
  26. def run_test(self):
  27. self.log.info("Mining blocks...")
  28. self.nodes[0].generate(105)
  29. self.sync_all()
  30. chain_height = self.nodes[1].getblockcount()
  31. assert_equal(chain_height, 105)
  32. assert_equal(self.nodes[1].getbalance(), 0)
  33. assert_equal(self.nodes[2].getbalance(), 0)
  34. node0utxos = self.nodes[0].listunspent(1)
  35. tx1 = self.nodes[0].createrawtransaction([node0utxos.pop()], {self.nodes[1].getnewaddress(): 49.99})
  36. txid1 = self.nodes[0].sendrawtransaction(self.nodes[0].signrawtransaction(tx1)["hex"])
  37. tx2 = self.nodes[0].createrawtransaction([node0utxos.pop()], {self.nodes[1].getnewaddress(): 49.99})
  38. txid2 = self.nodes[0].sendrawtransaction(self.nodes[0].signrawtransaction(tx2)["hex"])
  39. assert_raises(JSONRPCException, self.nodes[0].gettxoutproof, [txid1])
  40. self.nodes[0].generate(1)
  41. blockhash = self.nodes[0].getblockhash(chain_height + 1)
  42. self.sync_all()
  43. txlist = []
  44. blocktxn = self.nodes[0].getblock(blockhash, True)["tx"]
  45. txlist.append(blocktxn[1])
  46. txlist.append(blocktxn[2])
  47. assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1])), [txid1])
  48. assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1, txid2])), txlist)
  49. assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1, txid2], blockhash)), txlist)
  50. txin_spent = self.nodes[1].listunspent(1).pop()
  51. tx3 = self.nodes[1].createrawtransaction([txin_spent], {self.nodes[0].getnewaddress(): 49.98})
  52. self.nodes[0].sendrawtransaction(self.nodes[1].signrawtransaction(tx3)["hex"])
  53. self.nodes[0].generate(1)
  54. self.sync_all()
  55. txid_spent = txin_spent["txid"]
  56. txid_unspent = txid1 if txin_spent["txid"] != txid1 else txid2
  57. # We can't find the block from a fully-spent tx
  58. assert_raises(JSONRPCException, self.nodes[2].gettxoutproof, [txid_spent])
  59. # ...but we can if we specify the block
  60. assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid_spent], blockhash)), [txid_spent])
  61. # ...or if the first tx is not fully-spent
  62. assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid_unspent])), [txid_unspent])
  63. try:
  64. assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1, txid2])), txlist)
  65. except JSONRPCException:
  66. assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid2, txid1])), txlist)
  67. # ...or if we have a -txindex
  68. assert_equal(self.nodes[2].verifytxoutproof(self.nodes[3].gettxoutproof([txid_spent])), [txid_spent])
  69. if __name__ == '__main__':
  70. MerkleBlockTest().main()