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.

smartfees.py 4.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python2
  2. # Copyright (c) 2014 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 fee estimation code
  7. #
  8. from test_framework import BitcoinTestFramework
  9. from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
  10. from util import *
  11. class EstimateFeeTest(BitcoinTestFramework):
  12. def setup_network(self):
  13. self.nodes = []
  14. self.nodes.append(start_node(0, self.options.tmpdir,
  15. ["-debug=mempool", "-debug=estimatefee"]))
  16. # Node1 mines small-but-not-tiny blocks, and allows free transactions.
  17. # NOTE: the CreateNewBlock code starts counting block size at 1,000 bytes,
  18. # so blockmaxsize of 2,000 is really just 1,000 bytes (room enough for
  19. # 6 or 7 transactions)
  20. self.nodes.append(start_node(1, self.options.tmpdir,
  21. ["-blockprioritysize=1500", "-blockmaxsize=2000",
  22. "-debug=mempool", "-debug=estimatefee"]))
  23. connect_nodes(self.nodes[1], 0)
  24. # Node2 is a stingy miner, that
  25. # produces very small blocks (room for only 3 or so transactions)
  26. node2args = [ "-blockprioritysize=0", "-blockmaxsize=1500",
  27. "-debug=mempool", "-debug=estimatefee"]
  28. self.nodes.append(start_node(2, self.options.tmpdir, node2args))
  29. connect_nodes(self.nodes[2], 0)
  30. self.is_network_split = False
  31. self.sync_all()
  32. def run_test(self):
  33. # Prime the memory pool with pairs of transactions
  34. # (high-priority, random fee and zero-priority, random fee)
  35. min_fee = Decimal("0.001")
  36. fees_per_kb = [];
  37. for i in range(12):
  38. (txid, txhex, fee) = random_zeropri_transaction(self.nodes, Decimal("1.1"),
  39. min_fee, min_fee, 20)
  40. tx_kbytes = (len(txhex)/2)/1000.0
  41. fees_per_kb.append(float(fee)/tx_kbytes)
  42. # Mine blocks with node2 until the memory pool clears:
  43. count_start = self.nodes[2].getblockcount()
  44. while len(self.nodes[2].getrawmempool()) > 0:
  45. self.nodes[2].setgenerate(True, 1)
  46. self.sync_all()
  47. all_estimates = [ self.nodes[0].estimatefee(i) for i in range(1,20) ]
  48. print("Fee estimates, super-stingy miner: "+str([str(e) for e in all_estimates]))
  49. # Estimates should be within the bounds of what transactions fees actually were:
  50. delta = 1.0e-6 # account for rounding error
  51. for e in filter(lambda x: x >= 0, all_estimates):
  52. if float(e)+delta < min(fees_per_kb) or float(e)-delta > max(fees_per_kb):
  53. raise AssertionError("Estimated fee (%f) out of range (%f,%f)"%(float(e), min_fee_kb, max_fee_kb))
  54. # Generate transactions while mining 30 more blocks, this time with node1:
  55. for i in range(30):
  56. for j in range(random.randrange(6-4,6+4)):
  57. (txid, txhex, fee) = random_transaction(self.nodes, Decimal("1.1"),
  58. Decimal("0.0"), min_fee, 20)
  59. tx_kbytes = (len(txhex)/2)/1000.0
  60. fees_per_kb.append(float(fee)/tx_kbytes)
  61. self.nodes[1].setgenerate(True, 1)
  62. self.sync_all()
  63. all_estimates = [ self.nodes[0].estimatefee(i) for i in range(1,20) ]
  64. print("Fee estimates, more generous miner: "+str([ str(e) for e in all_estimates]))
  65. for e in filter(lambda x: x >= 0, all_estimates):
  66. if float(e)+delta < min(fees_per_kb) or float(e)-delta > max(fees_per_kb):
  67. raise AssertionError("Estimated fee (%f) out of range (%f,%f)"%(float(e), min_fee_kb, max_fee_kb))
  68. # Finish by mining a normal-sized block:
  69. while len(self.nodes[0].getrawmempool()) > 0:
  70. self.nodes[0].setgenerate(True, 1)
  71. self.sync_all()
  72. final_estimates = [ self.nodes[0].estimatefee(i) for i in range(1,20) ]
  73. print("Final fee estimates: "+str([ str(e) for e in final_estimates]))
  74. if __name__ == '__main__':
  75. EstimateFeeTest().main()