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.

rawtransactions.py 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 the rawtranscation RPCs.
  6. Test the following RPCs:
  7. - createrawtransaction
  8. - signrawtransaction
  9. - sendrawtransaction
  10. - decoderawtransaction
  11. - getrawtransaction
  12. """
  13. from test_framework.test_framework import BitcoinTestFramework
  14. from test_framework.util import *
  15. # Create one-input, one-output, no-fee transaction:
  16. class RawTransactionsTest(BitcoinTestFramework):
  17. def __init__(self):
  18. super().__init__()
  19. self.setup_clean_chain = True
  20. self.num_nodes = 3
  21. def setup_network(self, split=False):
  22. self.nodes = start_nodes(self.num_nodes, self.options.tmpdir)
  23. #connect to a local machine for debugging
  24. #url = "http://bitcoinrpc:DP6DvqZtqXarpeNWyN3LZTFchCCyCUuHwNF7E8pX99x1@%s:%d" % ('127.0.0.1', 18332)
  25. #proxy = AuthServiceProxy(url)
  26. #proxy.url = url # store URL on proxy for info
  27. #self.nodes.append(proxy)
  28. connect_nodes_bi(self.nodes,0,1)
  29. connect_nodes_bi(self.nodes,1,2)
  30. connect_nodes_bi(self.nodes,0,2)
  31. self.is_network_split=False
  32. self.sync_all()
  33. def run_test(self):
  34. #prepare some coins for multiple *rawtransaction commands
  35. self.nodes[2].generate(1)
  36. self.sync_all()
  37. self.nodes[0].generate(101)
  38. self.sync_all()
  39. self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(),1.5)
  40. self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(),1.0)
  41. self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(),5.0)
  42. self.sync_all()
  43. self.nodes[0].generate(5)
  44. self.sync_all()
  45. #########################################
  46. # sendrawtransaction with missing input #
  47. #########################################
  48. inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1}] #won't exists
  49. outputs = { self.nodes[0].getnewaddress() : 4.998 }
  50. rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
  51. rawtx = self.nodes[2].signrawtransaction(rawtx)
  52. # This will raise an exception since there are missing inputs
  53. assert_raises_jsonrpc(-25, "Missing inputs", self.nodes[2].sendrawtransaction, rawtx['hex'])
  54. #########################
  55. # RAW TX MULTISIG TESTS #
  56. #########################
  57. # 2of2 test
  58. addr1 = self.nodes[2].getnewaddress()
  59. addr2 = self.nodes[2].getnewaddress()
  60. addr1Obj = self.nodes[2].validateaddress(addr1)
  61. addr2Obj = self.nodes[2].validateaddress(addr2)
  62. mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])
  63. mSigObjValid = self.nodes[2].validateaddress(mSigObj)
  64. #use balance deltas instead of absolute values
  65. bal = self.nodes[2].getbalance()
  66. # send 1.2 BTC to msig adr
  67. txId = self.nodes[0].sendtoaddress(mSigObj, 1.2)
  68. self.sync_all()
  69. self.nodes[0].generate(1)
  70. self.sync_all()
  71. assert_equal(self.nodes[2].getbalance(), bal+Decimal('1.20000000')) #node2 has both keys of the 2of2 ms addr., tx should affect the balance
  72. # 2of3 test from different nodes
  73. bal = self.nodes[2].getbalance()
  74. addr1 = self.nodes[1].getnewaddress()
  75. addr2 = self.nodes[2].getnewaddress()
  76. addr3 = self.nodes[2].getnewaddress()
  77. addr1Obj = self.nodes[1].validateaddress(addr1)
  78. addr2Obj = self.nodes[2].validateaddress(addr2)
  79. addr3Obj = self.nodes[2].validateaddress(addr3)
  80. mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey'], addr3Obj['pubkey']])
  81. mSigObjValid = self.nodes[2].validateaddress(mSigObj)
  82. txId = self.nodes[0].sendtoaddress(mSigObj, 2.2)
  83. decTx = self.nodes[0].gettransaction(txId)
  84. rawTx = self.nodes[0].decoderawtransaction(decTx['hex'])
  85. sPK = rawTx['vout'][0]['scriptPubKey']['hex']
  86. self.sync_all()
  87. self.nodes[0].generate(1)
  88. self.sync_all()
  89. #THIS IS A INCOMPLETE FEATURE
  90. #NODE2 HAS TWO OF THREE KEY AND THE FUNDS SHOULD BE SPENDABLE AND COUNT AT BALANCE CALCULATION
  91. assert_equal(self.nodes[2].getbalance(), bal) #for now, assume the funds of a 2of3 multisig tx are not marked as spendable
  92. txDetails = self.nodes[0].gettransaction(txId, True)
  93. rawTx = self.nodes[0].decoderawtransaction(txDetails['hex'])
  94. vout = False
  95. for outpoint in rawTx['vout']:
  96. if outpoint['value'] == Decimal('2.20000000'):
  97. vout = outpoint
  98. break
  99. bal = self.nodes[0].getbalance()
  100. inputs = [{ "txid" : txId, "vout" : vout['n'], "scriptPubKey" : vout['scriptPubKey']['hex']}]
  101. outputs = { self.nodes[0].getnewaddress() : 2.19 }
  102. rawTx = self.nodes[2].createrawtransaction(inputs, outputs)
  103. rawTxPartialSigned = self.nodes[1].signrawtransaction(rawTx, inputs)
  104. assert_equal(rawTxPartialSigned['complete'], False) #node1 only has one key, can't comp. sign the tx
  105. rawTxSigned = self.nodes[2].signrawtransaction(rawTx, inputs)
  106. assert_equal(rawTxSigned['complete'], True) #node2 can sign the tx compl., own two of three keys
  107. self.nodes[2].sendrawtransaction(rawTxSigned['hex'])
  108. rawTx = self.nodes[0].decoderawtransaction(rawTxSigned['hex'])
  109. self.sync_all()
  110. self.nodes[0].generate(1)
  111. self.sync_all()
  112. assert_equal(self.nodes[0].getbalance(), bal+Decimal('50.00000000')+Decimal('2.19000000')) #block reward + tx
  113. # getrawtransaction tests
  114. # 1. valid parameters - only supply txid
  115. txHash = rawTx["hash"]
  116. assert_equal(self.nodes[0].getrawtransaction(txHash), rawTxSigned['hex'])
  117. # 2. valid parameters - supply txid and 0 for non-verbose
  118. assert_equal(self.nodes[0].getrawtransaction(txHash, 0), rawTxSigned['hex'])
  119. # 3. valid parameters - supply txid and False for non-verbose
  120. assert_equal(self.nodes[0].getrawtransaction(txHash, False), rawTxSigned['hex'])
  121. # 4. valid parameters - supply txid and 1 for verbose.
  122. # We only check the "hex" field of the output so we don't need to update this test every time the output format changes.
  123. assert_equal(self.nodes[0].getrawtransaction(txHash, 1)["hex"], rawTxSigned['hex'])
  124. # 5. valid parameters - supply txid and True for non-verbose
  125. assert_equal(self.nodes[0].getrawtransaction(txHash, True)["hex"], rawTxSigned['hex'])
  126. # 6. invalid parameters - supply txid and string "Flase"
  127. assert_raises_jsonrpc(-3,"Invalid type", self.nodes[0].getrawtransaction, txHash, "Flase")
  128. # 7. invalid parameters - supply txid and empty array
  129. assert_raises_jsonrpc(-3,"Invalid type", self.nodes[0].getrawtransaction, txHash, [])
  130. # 8. invalid parameters - supply txid and empty dict
  131. assert_raises_jsonrpc(-3,"Invalid type", self.nodes[0].getrawtransaction, txHash, {})
  132. inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : 1000}]
  133. outputs = { self.nodes[0].getnewaddress() : 1 }
  134. rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
  135. decrawtx= self.nodes[0].decoderawtransaction(rawtx)
  136. assert_equal(decrawtx['vin'][0]['sequence'], 1000)
  137. # 9. invalid parameters - sequence number out of range
  138. inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : -1}]
  139. outputs = { self.nodes[0].getnewaddress() : 1 }
  140. assert_raises_jsonrpc(-8, 'Invalid parameter, sequence number is out of range', self.nodes[0].createrawtransaction, inputs, outputs)
  141. # 10. invalid parameters - sequence number out of range
  142. inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : 4294967296}]
  143. outputs = { self.nodes[0].getnewaddress() : 1 }
  144. assert_raises_jsonrpc(-8, 'Invalid parameter, sequence number is out of range', self.nodes[0].createrawtransaction, inputs, outputs)
  145. inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : 4294967294}]
  146. outputs = { self.nodes[0].getnewaddress() : 1 }
  147. rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
  148. decrawtx= self.nodes[0].decoderawtransaction(rawtx)
  149. assert_equal(decrawtx['vin'][0]['sequence'], 4294967294)
  150. if __name__ == '__main__':
  151. RawTransactionsTest().main()