Dash Core  0.12.2.1
P2P Digital Currency
txn_doublespend.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 a double-spend conflict
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  node0_address_foo = self.nodes[0].getnewaddress("foo")
32  fund_foo_txid = self.nodes[0].sendfrom("", node0_address_foo, 12190)
33  fund_foo_tx = self.nodes[0].gettransaction(fund_foo_txid)
34 
35  node0_address_bar = self.nodes[0].getnewaddress("bar")
36  fund_bar_txid = self.nodes[0].sendfrom("", node0_address_bar, 290)
37  fund_bar_tx = self.nodes[0].gettransaction(fund_bar_txid)
38 
39  assert_equal(self.nodes[0].getbalance(""),
40  starting_balance - 12190 - 290 + fund_foo_tx["fee"] + fund_bar_tx["fee"])
41 
42  # Coins are sent to node1_address
43  node1_address = self.nodes[1].getnewaddress("from0")
44 
45  # First: use raw transaction API to send 12400 DASH to node1_address,
46  # but don't broadcast:
47  doublespend_fee = Decimal('-.02')
48  rawtx_input_0 = {}
49  rawtx_input_0["txid"] = fund_foo_txid
50  rawtx_input_0["vout"] = find_output(self.nodes[0], fund_foo_txid, 12190)
51  rawtx_input_1 = {}
52  rawtx_input_1["txid"] = fund_bar_txid
53  rawtx_input_1["vout"] = find_output(self.nodes[0], fund_bar_txid, 290)
54  inputs = [rawtx_input_0, rawtx_input_1]
55  change_address = self.nodes[0].getnewaddress()
56  outputs = {}
57  outputs[node1_address] = 12400
58  outputs[change_address] = 12480 - 12400 + doublespend_fee
59  rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
60  doublespend = self.nodes[0].signrawtransaction(rawtx)
61  assert_equal(doublespend["complete"], True)
62 
63  # Create two spends using 1 500 DASH coin each
64  txid1 = self.nodes[0].sendfrom("foo", node1_address, 400, 0)
65  txid2 = self.nodes[0].sendfrom("bar", node1_address, 200, 0)
66 
67  # Have node0 mine a block:
68  if (self.options.mine_block):
69  self.nodes[0].generate(1)
70  sync_blocks(self.nodes[0:2])
71 
72  tx1 = self.nodes[0].gettransaction(txid1)
73  tx2 = self.nodes[0].gettransaction(txid2)
74 
75  # Node0's balance should be starting balance, plus 500DASH for another
76  # matured block, minus 400, minus 200, and minus transaction fees:
77  expected = starting_balance + fund_foo_tx["fee"] + fund_bar_tx["fee"]
78  if self.options.mine_block: expected += 500
79  expected += tx1["amount"] + tx1["fee"]
80  expected += tx2["amount"] + tx2["fee"]
81  assert_equal(self.nodes[0].getbalance(), expected)
82 
83  # foo and bar accounts should be debited:
84  assert_equal(self.nodes[0].getbalance("foo", 0), 12190+tx1["amount"]+tx1["fee"])
85  assert_equal(self.nodes[0].getbalance("bar", 0), 290+tx2["amount"]+tx2["fee"])
86 
87  if self.options.mine_block:
88  assert_equal(tx1["confirmations"], 1)
89  assert_equal(tx2["confirmations"], 1)
90  # Node1's "from0" balance should be both transaction amounts:
91  assert_equal(self.nodes[1].getbalance("from0"), -(tx1["amount"]+tx2["amount"]))
92  else:
93  assert_equal(tx1["confirmations"], 0)
94  assert_equal(tx2["confirmations"], 0)
95 
96  # Now give doublespend and its parents to miner:
97  self.nodes[2].sendrawtransaction(fund_foo_tx["hex"])
98  self.nodes[2].sendrawtransaction(fund_bar_tx["hex"])
99  doublespend_txid = self.nodes[2].sendrawtransaction(doublespend["hex"])
100  # ... mine a block...
101  self.nodes[2].generate(1)
102 
103  # Reconnect the split network, and sync chain:
104  connect_nodes(self.nodes[1], 2)
105  self.nodes[2].generate(1) # Mine another block to make sure we sync
106  sync_blocks(self.nodes)
107  assert_equal(self.nodes[0].gettransaction(doublespend_txid)["confirmations"], 2)
108 
109  # Re-fetch transaction info:
110  tx1 = self.nodes[0].gettransaction(txid1)
111  tx2 = self.nodes[0].gettransaction(txid2)
112 
113  # Both transactions should be conflicted
114  assert_equal(tx1["confirmations"], -2)
115  assert_equal(tx2["confirmations"], -2)
116 
117  # Node0's total balance should be starting balance, plus 1000DASH for
118  # two more matured blocks, minus 12400 for the double-spend, plus fees (which are
119  # negative):
120  expected = starting_balance + 1000 - 12400 + fund_foo_tx["fee"] + fund_bar_tx["fee"] + doublespend_fee
121  assert_equal(self.nodes[0].getbalance(), expected)
122  assert_equal(self.nodes[0].getbalance("*"), expected)
123 
124  # Final "" balance is starting_balance - amount moved to accounts - doublespend + subsidies +
125  # fees (which are negative)
126  assert_equal(self.nodes[0].getbalance("foo"), 12190)
127  assert_equal(self.nodes[0].getbalance("bar"), 290)
128  assert_equal(self.nodes[0].getbalance(""), starting_balance
129  -12190
130  - 290
131  -12400
132  + 1000
133  + fund_foo_tx["fee"]
134  + fund_bar_tx["fee"]
135  + doublespend_fee)
136 
137  # Node1's "from0" account balance should be just the doublespend:
138  assert_equal(self.nodes[1].getbalance("from0"), 12400)
139 
140 if __name__ == '__main__':
141  TxnMallTest().main()
142 
UniValue getbalance(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:792
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
def find_output(node, txid, amount)
Definition: util.py:355
UniValue signrawtransaction(const UniValue &params, bool fHelp)
UniValue createrawtransaction(const UniValue &params, bool fHelp)
def add_options(self, parser)
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 assert_equal(thing1, thing2)
Definition: util.py:461
UniValue gettransaction(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:1821