halving-calculator.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.util import async_run
  16. cfg = opts.init({
  17. 'text': {
  18. 'desc': 'Estimate date of next block subsidy halving',
  19. 'usage':'[opts]',
  20. 'options': """
  21. -h, --help Print this help message
  22. --, --longhelp Print help message for long options (common options)
  23. -s, --sample-size=N Specify sample block range for block discovery time
  24. estimate
  25. """,
  26. 'notes': """
  27. Requires a running coin daemon
  28. Specify coin with --coin=btc (default)/--coin=bch/--coin=ltc
  29. If necessary, invoke with --rpc-host/--rpc-port/--rpc-user/--rpc-password
  30. Specify aiohttp backend with --rpc-backend=aiohttp (Linux only)
  31. A more full-featured version of this program can be found in the
  32. mmgen-node-tools repository.
  33. """
  34. }
  35. })
  36. def date(t):
  37. return '{}-{:02}-{:02} {:02}:{:02}:{:02}'.format(*time.gmtime(t)[:6])
  38. def dhms(t):
  39. t,neg = (-t,'-') if t < 0 else (t,' ')
  40. return f'{neg}{t//60//60//24} days, {t//60//60%24:02}:{t//60%60:02}:{t%60:02} h/m/s'
  41. def time_diff_warning(t_diff):
  42. if abs(t_diff) > 60*60:
  43. print('Warning: block tip time is {} {} clock time!'.format(
  44. dhms(abs(t_diff)),
  45. ('behind','ahead of')[t_diff<0]))
  46. async def main():
  47. proto = cfg._proto
  48. from mmgen.rpc import rpc_init
  49. c = await rpc_init(cfg,proto)
  50. tip = await c.call('getblockcount')
  51. assert tip > 1, 'block tip must be > 1'
  52. remaining = proto.halving_interval - tip % proto.halving_interval
  53. sample_size = int(cfg.sample_size) if cfg.sample_size else min(tip-1,max(remaining,144))
  54. # aiohttp backend will perform these two calls concurrently:
  55. cur,old = await c.gathered_call('getblockstats',((tip,),(tip - sample_size,)))
  56. clock_time = int(time.time())
  57. time_diff_warning(clock_time - cur['time'])
  58. bdr = (cur['time'] - old['time']) / sample_size
  59. t_rem = remaining * int(bdr)
  60. sub = cur['subsidy'] * proto.coin_amt.satoshi
  61. print(
  62. f'Current block: {tip}\n'
  63. f'Next halving block: {tip + remaining}\n'
  64. f'Blocks until halving: {remaining}\n'
  65. f'Current block subsidy: {str(sub).rstrip("0")} {proto.coin}\n'
  66. f'Current block discovery rate (over last {sample_size} blocks): {bdr/60:0.1f} minutes\n'
  67. f'Current clock time (UTC): {date(clock_time)}\n'
  68. f'Est. halving date (UTC): {date(cur["time"] + t_rem)}\n'
  69. f'Est. time until halving: {dhms(cur["time"] + t_rem - clock_time)}\n'
  70. )
  71. async_run(main())