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.

txn_doublespend.py 6.4KB

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 proper accounting with a double-spend conflict
  7. #
  8. from test_framework.test_framework import BitcoinTestFramework
  9. from test_framework.util import *
  10. from decimal import Decimal
  11. import os
  12. import shutil
  13. class TxnMallTest(BitcoinTestFramework):
  14. def add_options(self, parser):
  15. parser.add_option("--mineblock", dest="mine_block", default=False, action="store_true",
  16. help="Test double-spend of 1-confirmed transaction")
  17. def setup_network(self):
  18. # Start with split network:
  19. return super(TxnMallTest, self).setup_network(True)
  20. def run_test(self):
  21. # All nodes should start with 1,250 BTC:
  22. starting_balance = 1250
  23. for i in range(4):
  24. assert_equal(self.nodes[i].getbalance(), starting_balance)
  25. self.nodes[i].getnewaddress("") # bug workaround, coins generated assigned to first getnewaddress!
  26. # Assign coins to foo and bar accounts:
  27. node0_address_foo = self.nodes[0].getnewaddress("foo")
  28. fund_foo_txid = self.nodes[0].sendfrom("", node0_address_foo, 1219)
  29. fund_foo_tx = self.nodes[0].gettransaction(fund_foo_txid)
  30. node0_address_bar = self.nodes[0].getnewaddress("bar")
  31. fund_bar_txid = self.nodes[0].sendfrom("", node0_address_bar, 29)
  32. fund_bar_tx = self.nodes[0].gettransaction(fund_bar_txid)
  33. assert_equal(self.nodes[0].getbalance(""),
  34. starting_balance - 1219 - 29 + fund_foo_tx["fee"] + fund_bar_tx["fee"])
  35. # Coins are sent to node1_address
  36. node1_address = self.nodes[1].getnewaddress("from0")
  37. # First: use raw transaction API to send 1240 BTC to node1_address,
  38. # but don't broadcast:
  39. doublespend_fee = Decimal('-.02')
  40. rawtx_input_0 = {}
  41. rawtx_input_0["txid"] = fund_foo_txid
  42. rawtx_input_0["vout"] = find_output(self.nodes[0], fund_foo_txid, 1219)
  43. rawtx_input_1 = {}
  44. rawtx_input_1["txid"] = fund_bar_txid
  45. rawtx_input_1["vout"] = find_output(self.nodes[0], fund_bar_txid, 29)
  46. inputs = [rawtx_input_0, rawtx_input_1]
  47. change_address = self.nodes[0].getnewaddress()
  48. outputs = {}
  49. outputs[node1_address] = 1240
  50. outputs[change_address] = 1248 - 1240 + doublespend_fee
  51. rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
  52. doublespend = self.nodes[0].signrawtransaction(rawtx)
  53. assert_equal(doublespend["complete"], True)
  54. # Create two spends using 1 50 BTC coin each
  55. txid1 = self.nodes[0].sendfrom("foo", node1_address, 40, 0)
  56. txid2 = self.nodes[0].sendfrom("bar", node1_address, 20, 0)
  57. # Have node0 mine a block:
  58. if (self.options.mine_block):
  59. self.nodes[0].generate(1)
  60. sync_blocks(self.nodes[0:2])
  61. tx1 = self.nodes[0].gettransaction(txid1)
  62. tx2 = self.nodes[0].gettransaction(txid2)
  63. # Node0's balance should be starting balance, plus 50BTC for another
  64. # matured block, minus 40, minus 20, and minus transaction fees:
  65. expected = starting_balance + fund_foo_tx["fee"] + fund_bar_tx["fee"]
  66. if self.options.mine_block: expected += 50
  67. expected += tx1["amount"] + tx1["fee"]
  68. expected += tx2["amount"] + tx2["fee"]
  69. assert_equal(self.nodes[0].getbalance(), expected)
  70. # foo and bar accounts should be debited:
  71. assert_equal(self.nodes[0].getbalance("foo", 0), 1219+tx1["amount"]+tx1["fee"])
  72. assert_equal(self.nodes[0].getbalance("bar", 0), 29+tx2["amount"]+tx2["fee"])
  73. if self.options.mine_block:
  74. assert_equal(tx1["confirmations"], 1)
  75. assert_equal(tx2["confirmations"], 1)
  76. # Node1's "from0" balance should be both transaction amounts:
  77. assert_equal(self.nodes[1].getbalance("from0"), -(tx1["amount"]+tx2["amount"]))
  78. else:
  79. assert_equal(tx1["confirmations"], 0)
  80. assert_equal(tx2["confirmations"], 0)
  81. # Now give doublespend and its parents to miner:
  82. self.nodes[2].sendrawtransaction(fund_foo_tx["hex"])
  83. self.nodes[2].sendrawtransaction(fund_bar_tx["hex"])
  84. doublespend_txid = self.nodes[2].sendrawtransaction(doublespend["hex"])
  85. # ... mine a block...
  86. self.nodes[2].generate(1)
  87. # Reconnect the split network, and sync chain:
  88. connect_nodes(self.nodes[1], 2)
  89. self.nodes[2].generate(1) # Mine another block to make sure we sync
  90. sync_blocks(self.nodes)
  91. assert_equal(self.nodes[0].gettransaction(doublespend_txid)["confirmations"], 2)
  92. # Re-fetch transaction info:
  93. tx1 = self.nodes[0].gettransaction(txid1)
  94. tx2 = self.nodes[0].gettransaction(txid2)
  95. # Both transactions should be conflicted
  96. assert_equal(tx1["confirmations"], -2)
  97. assert_equal(tx2["confirmations"], -2)
  98. # Node0's total balance should be starting balance, plus 100BTC for
  99. # two more matured blocks, minus 1240 for the double-spend, plus fees (which are
  100. # negative):
  101. expected = starting_balance + 100 - 1240 + fund_foo_tx["fee"] + fund_bar_tx["fee"] + doublespend_fee
  102. assert_equal(self.nodes[0].getbalance(), expected)
  103. assert_equal(self.nodes[0].getbalance("*"), expected)
  104. # Final "" balance is starting_balance - amount moved to accounts - doublespend + subsidies +
  105. # fees (which are negative)
  106. assert_equal(self.nodes[0].getbalance("foo"), 1219)
  107. assert_equal(self.nodes[0].getbalance("bar"), 29)
  108. assert_equal(self.nodes[0].getbalance(""), starting_balance
  109. -1219
  110. - 29
  111. -1240
  112. + 100
  113. + fund_foo_tx["fee"]
  114. + fund_bar_tx["fee"]
  115. + doublespend_fee)
  116. # Node1's "from0" account balance should be just the doublespend:
  117. assert_equal(self.nodes[1].getbalance("from0"), 1240)
  118. if __name__ == '__main__':
  119. TxnMallTest().main()