main_halving_calculator.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2021 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify it under
  7. # the terms of the GNU General Public License as published by the Free Software
  8. # Foundation, either version 3 of the License, or (at your option) any later
  9. # version.
  10. #
  11. # This program is distributed in the hope that it will be useful, but WITHOUT
  12. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  14. # details.
  15. #
  16. # You should have received a copy of the GNU General Public License along with
  17. # this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. mmnode-halving-calculator: Estimate date(s) of future block subsidy halving(s)
  20. """
  21. import time
  22. from decimal import Decimal
  23. from mmgen.common import *
  24. bdr_proj = 9.95
  25. opts.init({
  26. 'sets': [('mined',True,'list',True)],
  27. 'text': {
  28. 'desc': 'Estimate date(s) of future block subsidy halving(s)',
  29. 'usage':'[opts]',
  30. 'options': f"""
  31. -h, --help Print this help message
  32. --, --longhelp Print help message for long options (common options)
  33. -l, --list List historical and projected halvings
  34. -m, --mined Same as above, plus list coins mined
  35. -r, --bdr-proj=I Block discovery interval for projected halvings (default:
  36. {bdr_proj:.5f} min)
  37. -s, --sample-size=N Block range to calculate block discovery interval for next
  38. halving estimate (default: dynamically calculated)
  39. """ }
  40. })
  41. if opt.bdr_proj:
  42. bdr_proj = float(opt.bdr_proj)
  43. def date(t):
  44. return '{}-{:02}-{:02} {:02}:{:02}:{:02}'.format(*time.gmtime(t)[:6])
  45. def dhms(t):
  46. t,neg = (-t,'-') if t < 0 else (t,' ')
  47. return f'{neg}{t//60//60//24} days, {t//60//60%24:02}:{t//60%60:02}:{t%60:02} h/m/s'
  48. def time_diff_warning(t_diff):
  49. if abs(t_diff) > 60*60:
  50. print('Warning: block tip time is {} {} clock time!'.format(
  51. dhms(abs(t_diff)),
  52. ('behind','ahead of')[t_diff<0]))
  53. async def main():
  54. from mmgen.protocol import init_proto_from_opts
  55. proto = init_proto_from_opts(need_amt=True)
  56. from mmgen.rpc import rpc_init
  57. c = await rpc_init(proto)
  58. tip = await c.call('getblockcount')
  59. assert tip > 1, 'block tip must be > 1'
  60. remaining = proto.halving_interval - tip % proto.halving_interval
  61. sample_size = int(opt.sample_size) if opt.sample_size else min(tip-1,max(remaining,144))
  62. cur,old = await c.gathered_call('getblockstats',((tip,),(tip - sample_size,)))
  63. clock_time = int(time.time())
  64. time_diff_warning(clock_time - cur['time'])
  65. bdr = (cur['time'] - old['time']) / sample_size
  66. t_rem = remaining * int(bdr)
  67. t_next = cur['time'] + t_rem
  68. if proto.name == 'BitcoinCash':
  69. sub = proto.coin_amt(str(cur['subsidy']))
  70. else:
  71. sub = cur['subsidy'] * proto.coin_amt.satoshi
  72. def print_current_stats():
  73. print(
  74. f'Current block: {tip:>7}\n'
  75. f'Next halving block: {tip + remaining:>7}\n'
  76. f'Halving interval: {proto.halving_interval:>7}\n'
  77. f'Blocks since last halving: {proto.halving_interval - remaining:>7}\n'
  78. f'Blocks until next halving: {remaining:>7}\n\n'
  79. f'Current block subsidy: {str(sub).rstrip("0")} {proto.coin}\n'
  80. f'Current block discovery interval (over last {sample_size} blocks): {bdr/60:0.2f} min\n\n'
  81. f'Current clock time (UTC): {date(clock_time)}\n'
  82. f'Est. halving date (UTC): {date(t_next)}\n'
  83. f'Est. time until halving: {dhms(cur["time"] + t_rem - clock_time)}'
  84. )
  85. async def print_halvings():
  86. halving_blocknums = [i*proto.halving_interval for i in range(proto.max_halvings+1)][1:]
  87. hist_halvings = await c.gathered_call('getblockstats',([(n,) for n in halving_blocknums if n <= tip]))
  88. halving_secs = bdr_proj * 60 * proto.halving_interval
  89. nhist = len(hist_halvings)
  90. nSubsidy = int(proto.start_subsidy / proto.coin_amt.satoshi)
  91. block0_hash = await c.call('getblockhash',0)
  92. block0_date = (await c.call('getblock',block0_hash))['time']
  93. def gen_data():
  94. total_mined = 0
  95. date = block0_date
  96. for n,blk in enumerate(halving_blocknums):
  97. mined = (nSubsidy >> n) * proto.halving_interval
  98. if n == 0:
  99. mined -= nSubsidy # subtract unspendable genesis block subsidy
  100. total_mined += mined
  101. sub = nSubsidy >> n+1 if n+1 < proto.max_halvings else 0
  102. bdi = (
  103. (hist_halvings[n]['time'] - date) / (proto.halving_interval * 60) if n < nhist
  104. else bdr/60 if n == nhist
  105. else bdr_proj
  106. )
  107. date = (
  108. hist_halvings[n]['time'] if n < nhist
  109. else t_next + int((n - nhist) * halving_secs)
  110. )
  111. yield ( n, sub, blk, mined, total_mined, bdi, date )
  112. if sub == 0:
  113. break
  114. fs = (
  115. ' {a:<7} {b:>8} {c:19}{d:2} {e:10} {f}',
  116. ' {a:<7} {b:>8} {c:19}{d:2} {e:10} {f:17} {g:17} {h}'
  117. )[bool(opt.mined)]
  118. print(
  119. f'Historical/Estimated/Projected Halvings ({proto.coin}):\n\n'
  120. + f' Sample size for next halving estimate (E): {sample_size} blocks\n'
  121. + f' Block discovery interval for projected halvings (P): {bdr_proj:.5f} minutes\n\n'
  122. + fs.format(
  123. a = 'HALVING',
  124. b = 'BLOCK',
  125. c = 'DATE',
  126. d = '',
  127. e = f'BDI (mins)',
  128. f = f'SUBSIDY ({proto.coin})',
  129. g = f'MINED ({proto.coin})',
  130. h = f'TOTAL MINED ({proto.coin})'
  131. )
  132. + '\n'
  133. + fs.format(
  134. a = '-' * 7,
  135. b = '-' * 8,
  136. c = '-' * 19,
  137. d = '-' * 2,
  138. e = '-' * 10,
  139. f = '-' * 13,
  140. g = '-' * 17,
  141. h = '-' * 17
  142. )
  143. + '\n'
  144. + '\n'.join(fs.format(
  145. a = n + 1,
  146. b = blk,
  147. c = date(t),
  148. d = ' P' if n > nhist else '' if n < nhist else ' E',
  149. e = f'{bdr:8.5f}',
  150. f = proto.coin_amt(sub,from_unit='satoshi').fmt(fs='2.8'),
  151. g = proto.coin_amt(mined,from_unit='satoshi').fmt(fs='8.8'),
  152. h = proto.coin_amt(total_mined,from_unit='satoshi').fmt(fs='8.8')
  153. ) for n,sub,blk,mined,total_mined,bdr,t in gen_data())
  154. )
  155. if opt.list:
  156. await print_halvings()
  157. else:
  158. print_current_stats()
  159. async_run(main())