halving-calculator.py 2.8 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. from decimal import Decimal
  15. from mmgen.common import *
  16. 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. from mmgen.protocol import init_proto_from_opts
  48. proto = init_proto_from_opts(need_amt=True)
  49. from mmgen.rpc import rpc_init
  50. c = await rpc_init(proto)
  51. tip = await c.call('getblockcount')
  52. assert tip > 1, 'block tip must be > 1'
  53. remaining = proto.halving_interval - tip % proto.halving_interval
  54. sample_size = int(opt.sample_size) if opt.sample_size else min(tip-1,max(remaining,144))
  55. # aiohttp backend will perform these two calls concurrently:
  56. cur,old = await c.gathered_call('getblockstats',((tip,),(tip - sample_size,)))
  57. clock_time = int(time.time())
  58. time_diff_warning(clock_time - cur['time'])
  59. bdr = (cur['time'] - old['time']) / sample_size
  60. t_rem = remaining * int(bdr)
  61. sub = cur['subsidy'] * proto.coin_amt.satoshi
  62. print(
  63. f'Current block: {tip}\n'
  64. f'Next halving block: {tip + remaining}\n'
  65. f'Blocks until halving: {remaining}\n'
  66. f'Current block subsidy: {str(sub).rstrip("0")} {proto.coin}\n'
  67. f'Current block discovery rate (over last {sample_size} blocks): {bdr/60:0.1f} minutes\n'
  68. f'Current clock time (UTC): {date(clock_time)}\n'
  69. f'Est. halving date (UTC): {date(cur["time"] + t_rem)}\n'
  70. f'Est. time until halving: {dhms(cur["time"] + t_rem - clock_time)}\n'
  71. )
  72. async_run(main())