coin_daemon_control.py 4.5 KB

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