]>
Commit | Line | Data |
---|---|---|
4ca5def6 | 1 | #!/usr/bin/env python |
bba2216f JS |
2 | # Copyright (c) 2014 The Bitcoin Core developers |
3 | # Distributed under the MIT software license, see the accompanying | |
bc909a7a | 4 | # file COPYING or https://www.opensource.org/licenses/mit-license.php . |
bba2216f JS |
5 | |
6 | # | |
7 | # Test re-org scenarios with a mempool that contains transactions | |
8 | # that spend (directly or indirectly) coinbase transactions. | |
9 | # | |
10 | ||
4ca5def6 | 11 | import sys; assert sys.version_info < (3,), ur"This script does not run under Python 3. Please use Python 2.7.x." |
12 | ||
3e875b1b | 13 | from test_framework.test_framework import BitcoinTestFramework |
aff0bf7f DH |
14 | from test_framework.authproxy import JSONRPCException |
15 | from test_framework.util import assert_equal, initialize_chain_clean, \ | |
16 | start_nodes, connect_nodes_bi | |
17 | ||
18 | from decimal import Decimal | |
bba2216f JS |
19 | |
20 | # Create one-input, one-output, no-fee transaction: | |
21 | class RawTransactionsTest(BitcoinTestFramework): | |
231072fe | 22 | |
bba2216f JS |
23 | def setup_chain(self): |
24 | print("Initializing test directory "+self.options.tmpdir) | |
25 | initialize_chain_clean(self.options.tmpdir, 3) | |
26 | ||
27 | def setup_network(self, split=False): | |
28 | self.nodes = start_nodes(3, self.options.tmpdir) | |
29 | ||
30 | #connect to a local machine for debugging | |
3985a40d | 31 | #url = "http://bitcoinrpc:DP6DvqZtqXarpeNWyN3LZTFchCCyCUuHwNF7E8pX99x1@%s:%d" % ('127.0.0.1', 18232) |
bba2216f JS |
32 | #proxy = AuthServiceProxy(url) |
33 | #proxy.url = url # store URL on proxy for info | |
34 | #self.nodes.append(proxy) | |
231072fe | 35 | |
bba2216f JS |
36 | connect_nodes_bi(self.nodes,0,1) |
37 | connect_nodes_bi(self.nodes,1,2) | |
38 | connect_nodes_bi(self.nodes,0,2) | |
231072fe | 39 | |
bba2216f JS |
40 | self.is_network_split=False |
41 | self.sync_all() | |
231072fe | 42 | |
bba2216f | 43 | def run_test(self): |
231072fe | 44 | |
bba2216f JS |
45 | #prepare some coins for multiple *rawtransaction commands |
46 | self.nodes[2].generate(1) | |
abc1c352 | 47 | self.sync_all() |
bba2216f JS |
48 | self.nodes[0].generate(101) |
49 | self.sync_all() | |
50 | self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(),1.5); | |
51 | self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(),1.0); | |
52 | self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(),5.0); | |
53 | self.sync_all() | |
54 | self.nodes[0].generate(5) | |
55 | self.sync_all() | |
56 | ||
57 | ######################################### | |
58 | # sendrawtransaction with missing input # | |
59 | ######################################### | |
60 | inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1}] #won't exists | |
61 | outputs = { self.nodes[0].getnewaddress() : 4.998 } | |
62 | rawtx = self.nodes[2].createrawtransaction(inputs, outputs) | |
63 | rawtx = self.nodes[2].signrawtransaction(rawtx) | |
231072fe | 64 | |
bba2216f JS |
65 | errorString = "" |
66 | try: | |
67 | rawtx = self.nodes[2].sendrawtransaction(rawtx['hex']) | |
68 | except JSONRPCException,e: | |
69 | errorString = e.error['message'] | |
231072fe JS |
70 | |
71 | assert_equal("Missing inputs" in errorString, True); | |
72 | ||
73 | ######################### | |
74 | # RAW TX MULTISIG TESTS # | |
75 | ######################### | |
76 | # 2of2 test | |
77 | addr1 = self.nodes[2].getnewaddress() | |
78 | addr2 = self.nodes[2].getnewaddress() | |
79 | ||
80 | addr1Obj = self.nodes[2].validateaddress(addr1) | |
81 | addr2Obj = self.nodes[2].validateaddress(addr2) | |
82 | ||
83 | mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey']]) | |
84 | mSigObjValid = self.nodes[2].validateaddress(mSigObj) | |
85 | ||
86 | #use balance deltas instead of absolute values | |
87 | bal = self.nodes[2].getbalance() | |
88 | ||
89 | # send 1.2 BTC to msig adr | |
90 | txId = self.nodes[0].sendtoaddress(mSigObj, 1.2); | |
91 | self.sync_all() | |
92 | self.nodes[0].generate(1) | |
93 | self.sync_all() | |
94 | assert_equal(self.nodes[2].getbalance(), bal+Decimal('1.20000000')) #node2 has both keys of the 2of2 ms addr., tx should affect the balance | |
95 | ||
96 | ||
97 | ||
98 | ||
99 | # 2of3 test from different nodes | |
100 | bal = self.nodes[2].getbalance() | |
101 | addr1 = self.nodes[1].getnewaddress() | |
102 | addr2 = self.nodes[2].getnewaddress() | |
103 | addr3 = self.nodes[2].getnewaddress() | |
104 | ||
105 | addr1Obj = self.nodes[1].validateaddress(addr1) | |
106 | addr2Obj = self.nodes[2].validateaddress(addr2) | |
107 | addr3Obj = self.nodes[2].validateaddress(addr3) | |
108 | ||
109 | mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey'], addr3Obj['pubkey']]) | |
110 | mSigObjValid = self.nodes[2].validateaddress(mSigObj) | |
811b36ba | 111 | assert_equal(mSigObjValid['isvalid'], True) |
231072fe JS |
112 | |
113 | txId = self.nodes[0].sendtoaddress(mSigObj, 2.2); | |
114 | decTx = self.nodes[0].gettransaction(txId) | |
115 | rawTx = self.nodes[0].decoderawtransaction(decTx['hex']) | |
116 | sPK = rawTx['vout'][0]['scriptPubKey']['hex'] | |
811b36ba | 117 | [sPK] # hush pyflakes |
231072fe JS |
118 | self.sync_all() |
119 | self.nodes[0].generate(1) | |
120 | self.sync_all() | |
121 | ||
ff0f3054 DH |
122 | # THIS IS A INCOMPLETE FEATURE |
123 | # NODE2 HAS TWO OF THREE KEY AND THE FUNDS SHOULD BE SPENDABLE AND COUNT AT BALANCE CALCULATION | |
124 | assert_equal(self.nodes[2].getbalance(), bal) # for now, assume the funds of a 2of3 multisig tx are not marked as spendable | |
231072fe JS |
125 | |
126 | txDetails = self.nodes[0].gettransaction(txId, True) | |
127 | rawTx = self.nodes[0].decoderawtransaction(txDetails['hex']) | |
128 | vout = False | |
129 | for outpoint in rawTx['vout']: | |
130 | if outpoint['value'] == Decimal('2.20000000'): | |
131 | vout = outpoint | |
132 | break; | |
133 | ||
134 | bal = self.nodes[0].getbalance() | |
135 | inputs = [{ "txid" : txId, "vout" : vout['n'], "scriptPubKey" : vout['scriptPubKey']['hex']}] | |
85c9ecb8 | 136 | outputs = { self.nodes[0].getnewaddress() : 2.199 } |
231072fe JS |
137 | rawTx = self.nodes[2].createrawtransaction(inputs, outputs) |
138 | rawTxPartialSigned = self.nodes[1].signrawtransaction(rawTx, inputs) | |
ff0f3054 DH |
139 | assert_equal(rawTxPartialSigned['complete'], False) # node1 only has one key, can't comp. sign the tx |
140 | ||
231072fe | 141 | rawTxSigned = self.nodes[2].signrawtransaction(rawTx, inputs) |
ff0f3054 | 142 | assert_equal(rawTxSigned['complete'], True) # node2 can sign the tx compl., own two of three keys |
231072fe JS |
143 | self.nodes[2].sendrawtransaction(rawTxSigned['hex']) |
144 | rawTx = self.nodes[0].decoderawtransaction(rawTxSigned['hex']) | |
145 | self.sync_all() | |
146 | self.nodes[0].generate(1) | |
147 | self.sync_all() | |
85c9ecb8 | 148 | assert_equal(self.nodes[0].getbalance(), bal+Decimal('10.00000000')+Decimal('2.19900000')) #block reward + tx |
bba2216f | 149 | |
216e94fb JS |
150 | inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : 1000}] |
151 | outputs = { self.nodes[0].getnewaddress() : 1 } | |
152 | rawtx = self.nodes[0].createrawtransaction(inputs, outputs) | |
153 | decrawtx= self.nodes[0].decoderawtransaction(rawtx) | |
154 | assert_equal(decrawtx['vin'][0]['sequence'], 1000) | |
155 | ||
bba2216f JS |
156 | if __name__ == '__main__': |
157 | RawTransactionsTest().main() |