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 4.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 set_test_params(self):
  10. self.num_nodes = 4
  11. self.setup_clean_chain = True
  12. # Nodes 0/1 are "wallet" nodes, Nodes 2/3 are used for testing
  13. self.extra_args = [[], [], [], ["-txindex"]]
  14. def setup_network(self):
  15. self.setup_nodes()
  16. connect_nodes(self.nodes[0], 1)
  17. connect_nodes(self.nodes[0], 2)
  18. connect_nodes(self.nodes[0], 3)
  19. self.sync_all()
  20. def run_test(self):
  21. self.log.info("Mining blocks...")
  22. self.nodes[0].generate(105)
  23. self.sync_all()
  24. chain_height = self.nodes[1].getblockcount()
  25. assert_equal(chain_height, 105)
  26. assert_equal(self.nodes[1].getbalance(), 0)
  27. assert_equal(self.nodes[2].getbalance(), 0)
  28. node0utxos = self.nodes[0].listunspent(1)
  29. tx1 = self.nodes[0].createrawtransaction([node0utxos.pop()], {self.nodes[1].getnewaddress(): 49.99})
  30. txid1 = self.nodes[0].sendrawtransaction(self.nodes[0].signrawtransaction(tx1)["hex"])
  31. tx2 = self.nodes[0].createrawtransaction([node0utxos.pop()], {self.nodes[1].getnewaddress(): 49.99})
  32. txid2 = self.nodes[0].sendrawtransaction(self.nodes[0].signrawtransaction(tx2)["hex"])
  33. # This will raise an exception because the transaction is not yet in a block
  34. assert_raises_rpc_error(-5, "Transaction not yet in block", self.nodes[0].gettxoutproof, [txid1])
  35. self.nodes[0].generate(1)
  36. blockhash = self.nodes[0].getblockhash(chain_height + 1)
  37. self.sync_all()
  38. txlist = []
  39. blocktxn = self.nodes[0].getblock(blockhash, True)["tx"]
  40. txlist.append(blocktxn[1])
  41. txlist.append(blocktxn[2])
  42. assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1])), [txid1])
  43. assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1, txid2])), txlist)
  44. assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1, txid2], blockhash)), txlist)
  45. txin_spent = self.nodes[1].listunspent(1).pop()
  46. tx3 = self.nodes[1].createrawtransaction([txin_spent], {self.nodes[0].getnewaddress(): 49.98})
  47. txid3 = self.nodes[0].sendrawtransaction(self.nodes[1].signrawtransaction(tx3)["hex"])
  48. self.nodes[0].generate(1)
  49. self.sync_all()
  50. txid_spent = txin_spent["txid"]
  51. txid_unspent = txid1 if txin_spent["txid"] != txid1 else txid2
  52. # We can't find the block from a fully-spent tx
  53. assert_raises_rpc_error(-5, "Transaction not yet in block", self.nodes[2].gettxoutproof, [txid_spent])
  54. # We can get the proof if we specify the block
  55. assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid_spent], blockhash)), [txid_spent])
  56. # We can't get the proof if we specify a non-existent block
  57. assert_raises_rpc_error(-5, "Block not found", self.nodes[2].gettxoutproof, [txid_spent], "00000000000000000000000000000000")
  58. # We can get the proof if the transaction is unspent
  59. assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid_unspent])), [txid_unspent])
  60. # We can get the proof if we provide a list of transactions and one of them is unspent. The ordering of the list should not matter.
  61. assert_equal(sorted(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1, txid2]))), sorted(txlist))
  62. assert_equal(sorted(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid2, txid1]))), sorted(txlist))
  63. # We can always get a proof if we have a -txindex
  64. assert_equal(self.nodes[2].verifytxoutproof(self.nodes[3].gettxoutproof([txid_spent])), [txid_spent])
  65. # We can't get a proof if we specify transactions from different blocks
  66. assert_raises_rpc_error(-5, "Not all transactions found in specified or retrieved block", self.nodes[2].gettxoutproof, [txid1, txid3])
  67. if __name__ == '__main__':
  68. MerkleBlockTest().main()