halving-calculator.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
  4. # Copyright (C)2013-2023 The MMGen Project <mmgen@tuta.io>
  5. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen
  9. # https://gitlab.com/mmgen/mmgen
  10. """
  11. examples.halving-calculator.py: Demonstrate use of the MMGen asyncio/aiohttp JSON-RPC interface
  12. """
  13. import time
  14. import mmgen.opts as opts
  15. from mmgen.opts import opt
  16. from mmgen.util import async_run
  17. opts.init({
  18. 'text': {
  19. 'desc': 'Estimate date of next block subsidy halving',
  20. 'usage':'[opts]',
  21. 'options': """
  22. -h, --help Print this help message
  23. --, --longhelp Print help message for long options (common options)
  24. -s, --sample-size=N Specify sample block range for block discovery time
  25. estimate
  26. """,
  27. 'notes': """
  28. Requires a running coin daemon
  29. Specify coin with --coin=btc (default)/--coin=bch/--coin=ltc
  30. If necessary, invoke with --rpc-host/--rpc-port/--rpc-user/--rpc-password
  31. Specify aiohttp backend with --rpc-backend=aiohttp (Linux only)
  32. A more full-featured version of this program can be found in the
  33. mmgen-node-tools repository.
  34. """
  35. }
  36. })
  37. def date(t):
  38. return '{}-{:02}-{:02} {:02}:{:02}:{:02}'.format(*time.gmtime(t)[:6])
  39. def dhms(t):
  40. t,neg = (-t,'-') if t < 0 else (t,' ')
  41. return f'{neg}{t//60//60//24} days, {t//60//60%24:02}:{t//60%60:02}:{t%60:02} h/m/s'
  42. def time_diff_warning(t_diff):
  43. if abs(t_diff) > 60*60:
  44. print('Warning: block tip time is {} {} clock time!'.format(
  45. dhms(abs(t_diff)),
  46. ('behind','ahead of')[t_diff<0]))
  47. async def main():
  48. from mmgen.protocol import init_proto_from_opts
  49. proto = init_proto_from_opts(need_amt=True)
  50. from mmgen.rpc import rpc_init
  51. c = await rpc_init(proto)
  52. tip = await c.call('getblockcount')
  53. assert tip > 1, 'block tip must be > 1'
  54. remaining = proto.halving_interval - tip % proto.halving_interval
  55. sample_size = int(opt.sample_size) if opt.sample_size else min(tip-1,max(remaining,144))
  56. # aiohttp backend will perform these two calls concurrently:
  57. cur,old = await c.gathered_call('getblockstats',((tip,),(tip - sample_size,)))
  58. clock_time = int(time.time())
  59. time_diff_warning(clock_time - cur['time'])
  60. bdr = (cur['time'] - old['time']) / sample_size
  61. t_rem = remaining * int(bdr)
  62. sub = cur['subsidy'] * proto.coin_amt.satoshi
  63. print(
  64. f'Current block: {tip}\n'
  65. f'Next halving block: {tip + remaining}\n'
  66. f'Blocks until halving: {remaining}\n'
  67. f'Current block subsidy: {str(sub).rstrip("0")} {proto.coin}\n'
  68. f'Current block discovery rate (over last {sample_size} blocks): {bdr/60:0.1f} minutes\n'
  69. f'Current clock time (UTC): {date(clock_time)}\n'
  70. f'Est. halving date (UTC): {date(cur["time"] + t_rem)}\n'
  71. f'Est. time until halving: {dhms(cur["time"] + t_rem - clock_time)}\n'
  72. )
  73. async_run(main())