您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

rawtransactions.py 10KB

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