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.

wallet.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python2
  2. # Copyright (c) 2014 The Bitcoin Core developers
  3. # Distributed under the MIT/X11 software license, see the accompanying
  4. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #
  6. # Exercise the wallet. Ported from wallet.sh.
  7. # Does the following:
  8. # a) creates 3 nodes, with an empty chain (no blocks).
  9. # b) node0 mines a block
  10. # c) node1 mines 101 blocks, so now nodes 0 and 1 have 50btc, node2 has none.
  11. # d) node0 sends 21 btc to node2, in two transactions (11 btc, then 10 btc).
  12. # e) node0 mines a block, collects the fee on the second transaction
  13. # f) node1 mines 100 blocks, to mature node0's just-mined block
  14. # g) check that node0 has 100-21, node2 has 21
  15. # h) node0 should now have 2 unspent outputs; send these to node2 via raw tx broadcast by node1
  16. # i) have node1 mine a block
  17. # j) check balances - node0 should have 0, node2 should have 100
  18. #
  19. from test_framework import BitcoinTestFramework
  20. from util import *
  21. class WalletTest (BitcoinTestFramework):
  22. def setup_chain(self):
  23. print("Initializing test directory "+self.options.tmpdir)
  24. initialize_chain_clean(self.options.tmpdir, 3)
  25. def setup_network(self, split=False):
  26. self.nodes = start_nodes(3, self.options.tmpdir)
  27. connect_nodes_bi(self.nodes,0,1)
  28. connect_nodes_bi(self.nodes,1,2)
  29. connect_nodes_bi(self.nodes,0,2)
  30. self.is_network_split=False
  31. self.sync_all()
  32. def run_test (self):
  33. print "Mining blocks..."
  34. self.nodes[0].setgenerate(True, 1)
  35. self.sync_all()
  36. self.nodes[1].setgenerate(True, 101)
  37. self.sync_all()
  38. assert_equal(self.nodes[0].getbalance(), 50)
  39. assert_equal(self.nodes[1].getbalance(), 50)
  40. assert_equal(self.nodes[2].getbalance(), 0)
  41. # Send 21 BTC from 0 to 2 using sendtoaddress call.
  42. # Second transaction will be child of first, and will require a fee
  43. self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11)
  44. self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 10)
  45. # Have node0 mine a block, thus he will collect his own fee.
  46. self.nodes[0].setgenerate(True, 1)
  47. self.sync_all()
  48. # Have node1 generate 100 blocks (so node0 can recover the fee)
  49. self.nodes[1].setgenerate(True, 100)
  50. self.sync_all()
  51. # node0 should end up with 100 btc in block rewards plus fees, but
  52. # minus the 21 plus fees sent to node2
  53. assert_equal(self.nodes[0].getbalance(), 100-21)
  54. assert_equal(self.nodes[2].getbalance(), 21)
  55. # Node0 should have two unspent outputs.
  56. # Create a couple of transactions to send them to node2, submit them through
  57. # node1, and make sure both node0 and node2 pick them up properly:
  58. node0utxos = self.nodes[0].listunspent(1)
  59. assert_equal(len(node0utxos), 2)
  60. # create both transactions
  61. txns_to_send = []
  62. for utxo in node0utxos:
  63. inputs = []
  64. outputs = {}
  65. inputs.append({ "txid" : utxo["txid"], "vout" : utxo["vout"]})
  66. outputs[self.nodes[2].getnewaddress("from1")] = utxo["amount"]
  67. raw_tx = self.nodes[0].createrawtransaction(inputs, outputs)
  68. txns_to_send.append(self.nodes[0].signrawtransaction(raw_tx))
  69. # Have node 1 (miner) send the transactions
  70. self.nodes[1].sendrawtransaction(txns_to_send[0]["hex"], True)
  71. self.nodes[1].sendrawtransaction(txns_to_send[1]["hex"], True)
  72. # Have node1 mine a block to confirm transactions:
  73. self.nodes[1].setgenerate(True, 1)
  74. self.sync_all()
  75. assert_equal(self.nodes[0].getbalance(), 0)
  76. assert_equal(self.nodes[2].getbalance(), 100)
  77. assert_equal(self.nodes[2].getbalance("from1"), 100-21)
  78. if __name__ == '__main__':
  79. WalletTest ().main ()