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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 re-org scenarios with a mempool that contains transactions
  7. # that spend (directly or indirectly) coinbase transactions.
  8. #
  9. from test_framework.test_framework import BitcoinTestFramework
  10. from test_framework.util import *
  11. from pprint import pprint
  12. from time import sleep
  13. # Create one-input, one-output, no-fee transaction:
  14. class RawTransactionsTest(BitcoinTestFramework):
  15. def setup_chain(self):
  16. print("Initializing test directory "+self.options.tmpdir)
  17. initialize_chain_clean(self.options.tmpdir, 3)
  18. def setup_network(self, split=False):
  19. self.nodes = start_nodes(3, self.options.tmpdir)
  20. #connect to a local machine for debugging
  21. #url = "http://bitcoinrpc:DP6DvqZtqXarpeNWyN3LZTFchCCyCUuHwNF7E8pX99x1@%s:%d" % ('127.0.0.1', 18332)
  22. #proxy = AuthServiceProxy(url)
  23. #proxy.url = url # store URL on proxy for info
  24. #self.nodes.append(proxy)
  25. connect_nodes_bi(self.nodes,0,1)
  26. connect_nodes_bi(self.nodes,1,2)
  27. connect_nodes_bi(self.nodes,0,2)
  28. self.is_network_split=False
  29. self.sync_all()
  30. def run_test(self):
  31. #prepare some coins for multiple *rawtransaction commands
  32. self.nodes[2].generate(1)
  33. self.sync_all()
  34. self.nodes[0].generate(101)
  35. self.sync_all()
  36. self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(),1.5);
  37. self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(),1.0);
  38. self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(),5.0);
  39. self.sync_all()
  40. self.nodes[0].generate(5)
  41. self.sync_all()
  42. #########################################
  43. # sendrawtransaction with missing input #
  44. #########################################
  45. inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1}] #won't exists
  46. outputs = { self.nodes[0].getnewaddress() : 4.998 }
  47. rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
  48. rawtx = self.nodes[2].signrawtransaction(rawtx)
  49. errorString = ""
  50. try:
  51. rawtx = self.nodes[2].sendrawtransaction(rawtx['hex'])
  52. except JSONRPCException,e:
  53. errorString = e.error['message']
  54. assert_equal("Missing inputs" in errorString, True);
  55. #########################
  56. # RAW TX MULTISIG TESTS #
  57. #########################
  58. # 2of2 test
  59. addr1 = self.nodes[2].getnewaddress()
  60. addr2 = self.nodes[2].getnewaddress()
  61. addr1Obj = self.nodes[2].validateaddress(addr1)
  62. addr2Obj = self.nodes[2].validateaddress(addr2)
  63. mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])
  64. mSigObjValid = self.nodes[2].validateaddress(mSigObj)
  65. #use balance deltas instead of absolute values
  66. bal = self.nodes[2].getbalance()
  67. # send 1.2 BTC to msig adr
  68. txId = self.nodes[0].sendtoaddress(mSigObj, 1.2);
  69. self.sync_all()
  70. self.nodes[0].generate(1)
  71. self.sync_all()
  72. assert_equal(self.nodes[2].getbalance(), bal+Decimal('1.20000000')) #node2 has both keys of the 2of2 ms addr., tx should affect the balance
  73. # 2of3 test from different nodes
  74. bal = self.nodes[2].getbalance()
  75. addr1 = self.nodes[1].getnewaddress()
  76. addr2 = self.nodes[2].getnewaddress()
  77. addr3 = self.nodes[2].getnewaddress()
  78. addr1Obj = self.nodes[1].validateaddress(addr1)
  79. addr2Obj = self.nodes[2].validateaddress(addr2)
  80. addr3Obj = self.nodes[2].validateaddress(addr3)
  81. mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey'], addr3Obj['pubkey']])
  82. mSigObjValid = self.nodes[2].validateaddress(mSigObj)
  83. txId = self.nodes[0].sendtoaddress(mSigObj, 2.2);
  84. decTx = self.nodes[0].gettransaction(txId)
  85. rawTx = self.nodes[0].decoderawtransaction(decTx['hex'])
  86. sPK = rawTx['vout'][0]['scriptPubKey']['hex']
  87. self.sync_all()
  88. self.nodes[0].generate(1)
  89. self.sync_all()
  90. #THIS IS A INCOMPLETE FEATURE
  91. #NODE2 HAS TWO OF THREE KEY AND THE FUNDS SHOULD BE SPENDABLE AND COUNT AT BALANCE CALCULATION
  92. assert_equal(self.nodes[2].getbalance(), bal) #for now, assume the funds of a 2of3 multisig tx are not marked as spendable
  93. txDetails = self.nodes[0].gettransaction(txId, True)
  94. rawTx = self.nodes[0].decoderawtransaction(txDetails['hex'])
  95. vout = False
  96. for outpoint in rawTx['vout']:
  97. if outpoint['value'] == Decimal('2.20000000'):
  98. vout = outpoint
  99. break;
  100. bal = self.nodes[0].getbalance()
  101. inputs = [{ "txid" : txId, "vout" : vout['n'], "scriptPubKey" : vout['scriptPubKey']['hex']}]
  102. outputs = { self.nodes[0].getnewaddress() : 2.19 }
  103. rawTx = self.nodes[2].createrawtransaction(inputs, outputs)
  104. rawTxPartialSigned = self.nodes[1].signrawtransaction(rawTx, inputs)
  105. assert_equal(rawTxPartialSigned['complete'], False) #node1 only has one key, can't comp. sign the tx
  106. rawTxSigned = self.nodes[2].signrawtransaction(rawTx, inputs)
  107. assert_equal(rawTxSigned['complete'], True) #node2 can sign the tx compl., own two of three keys
  108. self.nodes[2].sendrawtransaction(rawTxSigned['hex'])
  109. rawTx = self.nodes[0].decoderawtransaction(rawTxSigned['hex'])
  110. self.sync_all()
  111. self.nodes[0].generate(1)
  112. self.sync_all()
  113. assert_equal(self.nodes[0].getbalance(), bal+Decimal('50.00000000')+Decimal('2.19000000')) #block reward + tx
  114. if __name__ == '__main__':
  115. RawTransactionsTest().main()