Dash Core  0.12.2.1
P2P Digital Currency
txn_clone.py
Go to the documentation of this file.
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 #
7 # Test proper accounting with an equivalent malleability clone
8 #
9 
10 from test_framework.test_framework import BitcoinTestFramework
11 from test_framework.util import *
12 
14 
15  def add_options(self, parser):
16  parser.add_option("--mineblock", dest="mine_block", default=False, action="store_true",
17  help="Test double-spend of 1-confirmed transaction")
18 
19  def setup_network(self):
20  # Start with split network:
21  return super(TxnMallTest, self).setup_network(True)
22 
23  def run_test(self):
24  # All nodes should start with 12,500 DASH:
25  starting_balance = 12500
26  for i in range(4):
27  assert_equal(self.nodes[i].getbalance(), starting_balance)
28  self.nodes[i].getnewaddress("") # bug workaround, coins generated assigned to first getnewaddress!
29 
30  # Assign coins to foo and bar accounts:
31  self.nodes[0].settxfee(.001)
32 
33  node0_address_foo = self.nodes[0].getnewaddress("foo")
34  fund_foo_txid = self.nodes[0].sendfrom("", node0_address_foo, 12190)
35  fund_foo_tx = self.nodes[0].gettransaction(fund_foo_txid)
36 
37  node0_address_bar = self.nodes[0].getnewaddress("bar")
38  fund_bar_txid = self.nodes[0].sendfrom("", node0_address_bar, 290)
39  fund_bar_tx = self.nodes[0].gettransaction(fund_bar_txid)
40 
41  assert_equal(self.nodes[0].getbalance(""),
42  starting_balance - 12190 - 290 + fund_foo_tx["fee"] + fund_bar_tx["fee"])
43 
44  # Coins are sent to node1_address
45  node1_address = self.nodes[1].getnewaddress("from0")
46 
47  # Send tx1, and another transaction tx2 that won't be cloned
48  txid1 = self.nodes[0].sendfrom("foo", node1_address, 400, 0)
49  txid2 = self.nodes[0].sendfrom("bar", node1_address, 200, 0)
50 
51  # Construct a clone of tx1, to be malleated
52  rawtx1 = self.nodes[0].getrawtransaction(txid1,1)
53  clone_inputs = [{"txid":rawtx1["vin"][0]["txid"],"vout":rawtx1["vin"][0]["vout"]}]
54  clone_outputs = {rawtx1["vout"][0]["scriptPubKey"]["addresses"][0]:rawtx1["vout"][0]["value"],
55  rawtx1["vout"][1]["scriptPubKey"]["addresses"][0]:rawtx1["vout"][1]["value"]}
56  clone_raw = self.nodes[0].createrawtransaction(clone_inputs, clone_outputs)
57 
58  # 3 hex manipulations on the clone are required
59 
60  # manipulation 1. sequence is at version+#inputs+input+sigstub
61  posseq = 2*(4+1+36+1)
62  seqbe = '%08x' % rawtx1["vin"][0]["sequence"]
63  clone_raw = clone_raw[:posseq] + seqbe[6:8] + seqbe[4:6] + seqbe[2:4] + seqbe[0:2] + clone_raw[posseq + 8:]
64 
65  # manipulation 2. createrawtransaction randomizes the order of its outputs, so swap them if necessary.
66  # output 0 is at version+#inputs+input+sigstub+sequence+#outputs
67  # 400 DASH serialized is 00902f5009000000
68  pos0 = 2*(4+1+36+1+4+1)
69  hex400 = "00902f5009000000"
70  output_len = 16 + 2 + 2 * int("0x" + clone_raw[pos0 + 16 : pos0 + 16 + 2], 0)
71  if (rawtx1["vout"][0]["value"] == 400 and clone_raw[pos0 : pos0 + 16] != hex400 or
72  rawtx1["vout"][0]["value"] != 400 and clone_raw[pos0 : pos0 + 16] == hex400):
73  output0 = clone_raw[pos0 : pos0 + output_len]
74  output1 = clone_raw[pos0 + output_len : pos0 + 2 * output_len]
75  clone_raw = clone_raw[:pos0] + output1 + output0 + clone_raw[pos0 + 2 * output_len:]
76 
77  # manipulation 3. locktime is after outputs
78  poslt = pos0 + 2 * output_len
79  ltbe = '%08x' % rawtx1["locktime"]
80  clone_raw = clone_raw[:poslt] + ltbe[6:8] + ltbe[4:6] + ltbe[2:4] + ltbe[0:2] + clone_raw[poslt + 8:]
81 
82  # Use a different signature hash type to sign. This creates an equivalent but malleated clone.
83  # Don't send the clone anywhere yet
84  tx1_clone = self.nodes[0].signrawtransaction(clone_raw, None, None, "ALL|ANYONECANPAY")
85  assert_equal(tx1_clone["complete"], True)
86 
87  # Have node0 mine a block, if requested:
88  if (self.options.mine_block):
89  self.nodes[0].generate(1)
90  sync_blocks(self.nodes[0:2])
91 
92  tx1 = self.nodes[0].gettransaction(txid1)
93  tx2 = self.nodes[0].gettransaction(txid2)
94 
95  # Node0's balance should be starting balance, plus 500DASH for another
96  # matured block, minus tx1 and tx2 amounts, and minus transaction fees:
97  expected = starting_balance + fund_foo_tx["fee"] + fund_bar_tx["fee"]
98  if self.options.mine_block: expected += 500
99  expected += tx1["amount"] + tx1["fee"]
100  expected += tx2["amount"] + tx2["fee"]
101  assert_equal(self.nodes[0].getbalance(), expected)
102 
103  # foo and bar accounts should be debited:
104  assert_equal(self.nodes[0].getbalance("foo", 0), 12190 + tx1["amount"] + tx1["fee"])
105  assert_equal(self.nodes[0].getbalance("bar", 0), 290 + tx2["amount"] + tx2["fee"])
106 
107  if self.options.mine_block:
108  assert_equal(tx1["confirmations"], 1)
109  assert_equal(tx2["confirmations"], 1)
110  # Node1's "from0" balance should be both transaction amounts:
111  assert_equal(self.nodes[1].getbalance("from0"), -(tx1["amount"] + tx2["amount"]))
112  else:
113  assert_equal(tx1["confirmations"], 0)
114  assert_equal(tx2["confirmations"], 0)
115 
116  # Send clone and its parent to miner
117  self.nodes[2].sendrawtransaction(fund_foo_tx["hex"])
118  txid1_clone = self.nodes[2].sendrawtransaction(tx1_clone["hex"])
119  # ... mine a block...
120  self.nodes[2].generate(1)
121 
122  # Reconnect the split network, and sync chain:
123  connect_nodes(self.nodes[1], 2)
124  self.nodes[2].sendrawtransaction(fund_bar_tx["hex"])
125  self.nodes[2].sendrawtransaction(tx2["hex"])
126  self.nodes[2].generate(1) # Mine another block to make sure we sync
127  sync_blocks(self.nodes)
128 
129  # Re-fetch transaction info:
130  tx1 = self.nodes[0].gettransaction(txid1)
131  tx1_clone = self.nodes[0].gettransaction(txid1_clone)
132  tx2 = self.nodes[0].gettransaction(txid2)
133 
134  # Verify expected confirmations
135  assert_equal(tx1["confirmations"], -2)
136  assert_equal(tx1_clone["confirmations"], 2)
137  assert_equal(tx2["confirmations"], 1)
138 
139  # Check node0's total balance; should be same as before the clone, + 1000 DASH for 2 matured,
140  # less possible orphaned matured subsidy
141  expected += 1000
142  if (self.options.mine_block):
143  expected -= 500
144  assert_equal(self.nodes[0].getbalance(), expected)
145  assert_equal(self.nodes[0].getbalance("*", 0), expected)
146 
147  # Check node0's individual account balances.
148  # "foo" should have been debited by the equivalent clone of tx1
149  assert_equal(self.nodes[0].getbalance("foo"), 12190 + tx1["amount"] + tx1["fee"])
150  # "bar" should have been debited by (possibly unconfirmed) tx2
151  assert_equal(self.nodes[0].getbalance("bar", 0), 290 + tx2["amount"] + tx2["fee"])
152  # "" should have starting balance, less funding txes, plus subsidies
153  assert_equal(self.nodes[0].getbalance("", 0), starting_balance
154  - 12190
155  + fund_foo_tx["fee"]
156  - 290
157  + fund_bar_tx["fee"]
158  + 1000)
159 
160  # Node1's "from0" account balance
161  assert_equal(self.nodes[1].getbalance("from0", 0), -(tx1["amount"] + tx2["amount"]))
162 
163 if __name__ == '__main__':
164  TxnMallTest().main()
165 
def run_test(self)
Definition: txn_clone.py:23
UniValue settxfee(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:2349
UniValue getbalance(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:792
UniValue getrawtransaction(const UniValue &params, bool fHelp)
UniValue sendfrom(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:958
def connect_nodes(from_connection, node_num)
Definition: util.py:343
UniValue getnewaddress(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:113
UniValue signrawtransaction(const UniValue &params, bool fHelp)
UniValue createrawtransaction(const UniValue &params, bool fHelp)
UniValue generate(const UniValue &params, bool fHelp)
Definition: mining.cpp:122
UniValue sendrawtransaction(const UniValue &params, bool fHelp)
def sync_blocks(rpc_connections, wait=1)
Definition: util.py:117
def add_options(self, parser)
Definition: txn_clone.py:15
def assert_equal(thing1, thing2)
Definition: util.py:461
UniValue gettransaction(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:1821
def setup_network(self)
Definition: txn_clone.py:19