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.
7 Test the following RPCs:
11 - decoderawtransaction
15 from test_framework
.test_framework
import BitcoinTestFramework
16 from test_framework
.util
import *
18 # Create one-input, one-output, no-fee transaction:
19 class RawTransactionsTest(BitcoinTestFramework
):
23 self
.setup_clean_chain
= True
26 def setup_network(self
, split
=False):
27 super().setup_network()
28 connect_nodes_bi(self
.nodes
,0,2)
32 #prepare some coins for multiple *rawtransaction commands
33 self
.nodes
[2].generate(1)
35 self
.nodes
[0].generate(101)
37 self
.nodes
[0].sendtoaddress(self
.nodes
[2].getnewaddress(),1.5)
38 self
.nodes
[0].sendtoaddress(self
.nodes
[2].getnewaddress(),1.0)
39 self
.nodes
[0].sendtoaddress(self
.nodes
[2].getnewaddress(),5.0)
41 self
.nodes
[0].generate(5)
44 #########################################
45 # sendrawtransaction with missing input #
46 #########################################
47 inputs
= [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1}] #won't exists
48 outputs
= { self
.nodes
[0].getnewaddress() : 4.998 }
49 rawtx
= self
.nodes
[2].createrawtransaction(inputs
, outputs
)
50 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'])
55 #########################
56 # RAW TX MULTISIG TESTS #
57 #########################
59 addr1
= self
.nodes
[2].getnewaddress()
60 addr2
= self
.nodes
[2].getnewaddress()
62 addr1Obj
= self
.nodes
[2].validateaddress(addr1
)
63 addr2Obj
= self
.nodes
[2].validateaddress(addr2
)
65 mSigObj
= self
.nodes
[2].addmultisigaddress(2, [addr1Obj
['pubkey'], addr2Obj
['pubkey']])
66 mSigObjValid
= self
.nodes
[2].validateaddress(mSigObj
)
68 #use balance deltas instead of absolute values
69 bal
= self
.nodes
[2].getbalance()
71 # send 1.2 BTC to msig adr
72 txId
= self
.nodes
[0].sendtoaddress(mSigObj
, 1.2)
74 self
.nodes
[0].generate(1)
76 assert_equal(self
.nodes
[2].getbalance(), bal
+Decimal('1.20000000')) #node2 has both keys of the 2of2 ms addr., tx should affect the balance
79 # 2of3 test from different nodes
80 bal
= self
.nodes
[2].getbalance()
81 addr1
= self
.nodes
[1].getnewaddress()
82 addr2
= self
.nodes
[2].getnewaddress()
83 addr3
= self
.nodes
[2].getnewaddress()
85 addr1Obj
= self
.nodes
[1].validateaddress(addr1
)
86 addr2Obj
= self
.nodes
[2].validateaddress(addr2
)
87 addr3Obj
= self
.nodes
[2].validateaddress(addr3
)
89 mSigObj
= self
.nodes
[2].addmultisigaddress(2, [addr1Obj
['pubkey'], addr2Obj
['pubkey'], addr3Obj
['pubkey']])
90 mSigObjValid
= self
.nodes
[2].validateaddress(mSigObj
)
92 txId
= self
.nodes
[0].sendtoaddress(mSigObj
, 2.2)
93 decTx
= self
.nodes
[0].gettransaction(txId
)
94 rawTx
= self
.nodes
[0].decoderawtransaction(decTx
['hex'])
96 self
.nodes
[0].generate(1)
99 #THIS IS A INCOMPLETE FEATURE
100 #NODE2 HAS TWO OF THREE KEY AND THE FUNDS SHOULD BE SPENDABLE AND COUNT AT BALANCE CALCULATION
101 assert_equal(self
.nodes
[2].getbalance(), bal
) #for now, assume the funds of a 2of3 multisig tx are not marked as spendable
103 txDetails
= self
.nodes
[0].gettransaction(txId
, True)
104 rawTx
= self
.nodes
[0].decoderawtransaction(txDetails
['hex'])
106 for outpoint
in rawTx
['vout']:
107 if outpoint
['value'] == Decimal('2.20000000'):
111 bal
= self
.nodes
[0].getbalance()
112 inputs
= [{ "txid" : txId
, "vout" : vout
['n'], "scriptPubKey" : vout
['scriptPubKey']['hex']}]
113 outputs
= { self
.nodes
[0].getnewaddress() : 2.19 }
114 rawTx
= self
.nodes
[2].createrawtransaction(inputs
, outputs
)
115 rawTxPartialSigned
= self
.nodes
[1].signrawtransaction(rawTx
, inputs
)
116 assert_equal(rawTxPartialSigned
['complete'], False) #node1 only has one key, can't comp. sign the tx
118 rawTxSigned
= self
.nodes
[2].signrawtransaction(rawTx
, inputs
)
119 assert_equal(rawTxSigned
['complete'], True) #node2 can sign the tx compl., own two of three keys
120 self
.nodes
[2].sendrawtransaction(rawTxSigned
['hex'])
121 rawTx
= self
.nodes
[0].decoderawtransaction(rawTxSigned
['hex'])
123 self
.nodes
[0].generate(1)
125 assert_equal(self
.nodes
[0].getbalance(), bal
+Decimal('50.00000000')+Decimal('2.19000000')) #block reward + tx
127 # getrawtransaction tests
128 # 1. valid parameters - only supply txid
129 txHash
= rawTx
["hash"]
130 assert_equal(self
.nodes
[0].getrawtransaction(txHash
), rawTxSigned
['hex'])
132 # 2. valid parameters - supply txid and 0 for non-verbose
133 assert_equal(self
.nodes
[0].getrawtransaction(txHash
, 0), rawTxSigned
['hex'])
135 # 3. valid parameters - supply txid and False for non-verbose
136 assert_equal(self
.nodes
[0].getrawtransaction(txHash
, False), rawTxSigned
['hex'])
138 # 4. valid parameters - supply txid and 1 for verbose.
139 # We only check the "hex" field of the output so we don't need to update this test every time the output format changes.
140 assert_equal(self
.nodes
[0].getrawtransaction(txHash
, 1)["hex"], rawTxSigned
['hex'])
142 # 5. valid parameters - supply txid and True for non-verbose
143 assert_equal(self
.nodes
[0].getrawtransaction(txHash
, True)["hex"], rawTxSigned
['hex'])
145 # 6. invalid parameters - supply txid and string "Flase"
146 assert_raises_jsonrpc(-3,"Invalid type", self
.nodes
[0].getrawtransaction
, txHash
, "Flase")
148 # 7. invalid parameters - supply txid and empty array
149 assert_raises_jsonrpc(-3,"Invalid type", self
.nodes
[0].getrawtransaction
, txHash
, [])
151 # 8. invalid parameters - supply txid and empty dict
152 assert_raises_jsonrpc(-3,"Invalid type", self
.nodes
[0].getrawtransaction
, txHash
, {})
154 inputs
= [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : 1000}]
155 outputs
= { self
.nodes
[0].getnewaddress() : 1 }
156 rawtx
= self
.nodes
[0].createrawtransaction(inputs
, outputs
)
157 decrawtx
= self
.nodes
[0].decoderawtransaction(rawtx
)
158 assert_equal(decrawtx
['vin'][0]['sequence'], 1000)
160 # 9. invalid parameters - sequence number out of range
161 inputs
= [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : -1}]
162 outputs
= { self
.nodes
[0].getnewaddress() : 1 }
163 assert_raises_jsonrpc(-8, 'Invalid parameter, sequence number is out of range', self
.nodes
[0].createrawtransaction
, inputs
, outputs
)
165 # 10. invalid parameters - sequence number out of range
166 inputs
= [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : 4294967296}]
167 outputs
= { self
.nodes
[0].getnewaddress() : 1 }
168 assert_raises_jsonrpc(-8, 'Invalid parameter, sequence number is out of range', self
.nodes
[0].createrawtransaction
, inputs
, outputs
)
170 inputs
= [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : 4294967294}]
171 outputs
= { self
.nodes
[0].getnewaddress() : 1 }
172 rawtx
= self
.nodes
[0].createrawtransaction(inputs
, outputs
)
173 decrawtx
= self
.nodes
[0].decoderawtransaction(rawtx
)
174 assert_equal(decrawtx
['vin'][0]['sequence'], 4294967294)
176 if __name__
== '__main__':
177 RawTransactionsTest().main()