coin_daemon_control.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, a command-line 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. test.include.coin_daemon_control: Start and stop daemons for the MMGen test suite
  12. """
  13. import sys
  14. from pathlib import PurePath
  15. sys.path[0] = str(PurePath(*PurePath(__file__).parts[:-3]))
  16. from mmgen.cfg import Config,gc
  17. from mmgen.util import msg,die,oneshot_warning,async_run
  18. from mmgen.protocol import init_proto
  19. from mmgen.daemon import CoinDaemon
  20. xmr_wallet_network_ids = {
  21. 'xmrw': 'mainnet',
  22. 'xmrw_tn': 'testnet'
  23. }
  24. action = gc.prog_name.split('-')[0]
  25. opts_data = {
  26. 'sets': [('debug',True,'verbose',True)],
  27. 'text': {
  28. 'desc': f'{action.capitalize()} coin or wallet daemons for the MMGen test suite',
  29. 'usage':'[opts] <network IDs>',
  30. 'options': """
  31. -h, --help Print this help message
  32. --, --longhelp Print help message for long options (common options)
  33. -D, --debug Produce debugging output (implies --verbose)
  34. -d, --datadir= Override the default datadir
  35. -i, --daemon-ids Print all known daemon IDs
  36. -m, --mainnet-only Perform operations for mainnet daemons only
  37. -n, --no-daemonize Don't fork daemon to background
  38. -p, --port-shift= Shift the RPC port by this number
  39. -s, --get-state Get the state of the daemon(s) and exit
  40. -t, --testing Testing mode. Print commands but don't execute them
  41. -q, --quiet Produce quieter output
  42. -u, --usermode Run the daemon in user (non test-suite) mode
  43. -v, --verbose Produce more verbose output
  44. -V, --print-version Print version strings from exec’ed daemons (not RPC)
  45. -W, --no-wait Don't wait for daemons to change state before exiting
  46. """,
  47. 'notes': """
  48. Valid network IDs: {nid}, {xmrw_nid}, all, no_xmr
  49. """
  50. },
  51. 'code': {
  52. 'options': lambda s: s.format(a=action.capitalize(),pn=gc.prog_name),
  53. 'notes': lambda s,help_notes: s.format(
  54. nid = help_notes('coin_daemon_network_ids'),
  55. xmrw_nid = ', '.join(xmr_wallet_network_ids),
  56. )
  57. }
  58. }
  59. class warn_missing_exec(oneshot_warning):
  60. color = 'nocolor'
  61. message = 'daemon executable {!r} not found on this system!'
  62. def run(network_id=None,proto=None,daemon_id=None,missing_exec_ok=False):
  63. if network_id in xmr_wallet_network_ids:
  64. from mmgen.proto.xmr.daemon import MoneroWalletDaemon
  65. d = MoneroWalletDaemon(
  66. cfg = cfg,
  67. proto = init_proto( cfg, coin='XMR', network=xmr_wallet_network_ids[network_id] ),
  68. user = 'test',
  69. passwd = 'test passwd',
  70. test_suite = True,
  71. monerod_addr = None,
  72. trust_monerod = True,
  73. test_monerod = False,
  74. opts = ['no_daemonize'] if cfg.no_daemonize else None )
  75. else:
  76. d = CoinDaemon(
  77. cfg,
  78. network_id = network_id,
  79. proto = proto,
  80. test_suite = not cfg.usermode,
  81. opts = ['no_daemonize'] if cfg.no_daemonize else None,
  82. port_shift = int(cfg.port_shift or 0),
  83. datadir = cfg.datadir,
  84. daemon_id = daemon_id )
  85. if cfg.mainnet_only and d.network != 'mainnet':
  86. return
  87. d.debug = d.debug or cfg.debug
  88. d.wait = not cfg.no_wait
  89. if missing_exec_ok:
  90. try:
  91. d.get_exec_version_str()
  92. except Exception as e:
  93. if not cfg.quiet:
  94. msg(str(e))
  95. warn_missing_exec( div=d.exec_fn, fmt_args=(d.exec_fn,) )
  96. return
  97. if cfg.print_version:
  98. msg('{:16} {}'.format( d.exec_fn+':', d.get_exec_version_str() ))
  99. elif cfg.get_state:
  100. print(d.state_msg())
  101. elif cfg.testing:
  102. for cmd in d.start_cmds if action == 'start' else [d.stop_cmd]:
  103. print(' '.join(cmd))
  104. else:
  105. if action == 'stop' and hasattr(d,'rpc'):
  106. async_run(d.rpc.stop_daemon(quiet=cfg.quiet))
  107. else:
  108. d.cmd(action,quiet=cfg.quiet)
  109. def main():
  110. if cfg.daemon_ids:
  111. print('\n'.join(CoinDaemon.all_daemon_ids()))
  112. elif 'all' in cfg._args or 'no_xmr' in cfg._args:
  113. if len(cfg._args) != 1:
  114. die(1,"'all' or 'no_xmr' must be the sole argument")
  115. for coin in CoinDaemon.coins:
  116. if coin == 'XMR' and cfg._args[0] == 'no_xmr':
  117. continue
  118. for daemon_id in CoinDaemon.get_daemon_ids(cfg,coin):
  119. for network in CoinDaemon.get_daemon(cfg,coin,daemon_id).networks:
  120. run(
  121. proto = init_proto( cfg, coin=coin, network=network ),
  122. daemon_id = daemon_id,
  123. missing_exec_ok = True )
  124. else:
  125. ids = cfg._args
  126. network_ids = CoinDaemon.get_network_ids(cfg)
  127. if not ids:
  128. cfg._opts.usage()
  129. for i in ids:
  130. if i not in network_ids + list(xmr_wallet_network_ids):
  131. die(1,f'{i!r}: invalid network ID')
  132. for network_id in ids:
  133. run(network_id=network_id.lower())
  134. cfg = Config(opts_data=opts_data)