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.9KB

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