coin-daemon-info.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2024 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-wallet
  9. # https://gitlab.com/mmgen/mmgen-wallet
  10. """
  11. examples/coin-daemon-info.py:
  12. Get info about multiple running coin daemons.
  13. Demonstrates use of the MMGen Config API.
  14. """
  15. # Instructions
  16. #
  17. # Testing mode:
  18. #
  19. # 1) From the MMGen repository root, start the mainnet test suite daemons as follows
  20. # (note that Geth is the default testing daemon for ETH):
  21. #
  22. # test/start-coin-daemons.py btc ltc eth
  23. #
  24. # 2) Then run the script as follows:
  25. #
  26. # PYTHONPATH=. MMGEN_TEST_SUITE=1 examples/coin-daemon-info.py btc ltc eth
  27. #
  28. # Live mode:
  29. #
  30. # 1) Start up one or more of bitcoind, litecoind or geth with the standard mainnet ports
  31. # and datadirs. For geth, the options ‘--http --http.api=eth,web3’ are required.
  32. #
  33. # 2) Then run the script as follows:
  34. #
  35. # PYTHONPATH=. examples/coin-daemon-info.py btc ltc eth
  36. import sys, os, asyncio
  37. from mmgen.exception import SocketError
  38. from mmgen.cfg import Config
  39. from mmgen.rpc import rpc_init
  40. from mmgen.util import make_timestr
  41. async def get_rpc(cfg):
  42. try:
  43. return await rpc_init(cfg, ignore_wallet=True)
  44. except SocketError:
  45. return False
  46. async def main(coins):
  47. rpcs = {}
  48. cfgs = {}
  49. test_suite = os.getenv('MMGEN_TEST_SUITE')
  50. base_cfg = Config({'pager':True})
  51. for coin in coins:
  52. cfg_in = {
  53. 'coin': coin,
  54. 'test_suite': test_suite,
  55. }
  56. if coin == 'eth' and not test_suite:
  57. cfg_in.update({'daemon_id': 'geth'})
  58. cfgs[coin] = Config(cfg_in)
  59. rpcs[coin] = await get_rpc(cfgs[coin])
  60. def gen_output():
  61. fs = '{:4} {:7} {:6} {:<5} {:<8} {:30} {:13} {:23} {}'
  62. yield fs.format(
  63. 'Coin',
  64. 'Network',
  65. 'Status',
  66. 'Port',
  67. 'Chain',
  68. 'Latest Block',
  69. 'Daemon',
  70. 'Version',
  71. 'Datadir')
  72. for coin, rpc in rpcs.items():
  73. info = ('Down', '-', '-', '-', '-', '-', '-') if rpc is False else (
  74. 'Up',
  75. rpc.port,
  76. rpc.chain,
  77. f'{rpc.blockcount:<8} [{make_timestr(rpc.cur_date)}]',
  78. rpc.daemon.coind_name,
  79. rpc.daemon_version_str,
  80. rpc.daemon.datadir
  81. )
  82. yield fs.format(coin.upper(), cfgs[coin].network, *info)
  83. base_cfg._util.stdout_or_pager('\n'.join(gen_output()))
  84. all_coins = ('btc', 'ltc', 'eth')
  85. coins = sys.argv[1:]
  86. if coins and all(coin in all_coins for coin in coins):
  87. asyncio.run(main(coins))
  88. else:
  89. print(f'You must supply one or more of the following coins on the command line:\n {all_coins}')
  90. sys.exit(1)