coin-daemon-info.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/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('Coin','Network','Status','Port','Chain','Latest Block','Daemon','Version','Datadir')
  63. for coin,rpc in rpcs.items():
  64. info = ('Down','-','-','-','-','-','-') if rpc is False else (
  65. 'Up',
  66. rpc.port,
  67. rpc.chain,
  68. f'{rpc.blockcount:<8} [{make_timestr(rpc.cur_date)}]',
  69. rpc.daemon.coind_name,
  70. rpc.daemon_version_str,
  71. rpc.daemon.datadir
  72. )
  73. yield fs.format( coin.upper(), cfgs[coin].network, *info )
  74. base_cfg._util.stdout_or_pager('\n'.join(gen_output()))
  75. all_coins = ('btc', 'ltc', 'eth')
  76. coins = sys.argv[1:]
  77. if coins and all(coin in all_coins for coin in coins):
  78. asyncio.run(main(coins))
  79. else:
  80. print(f'You must supply one or more of the following coins on the command line:\n {all_coins}')
  81. sys.exit(1)