daemon.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. proto.xmr.daemon: Monero base protocol daemon classes
  12. """
  13. import os
  14. from ...cfg import gc
  15. from ...util import list_gen,die,contains_any
  16. from ...daemon import CoinDaemon,RPCDaemon,_nw,_dd
  17. class monero_daemon(CoinDaemon):
  18. daemon_data = _dd('Monero', 18002002, '0.18.2.2-release')
  19. networks = ('mainnet','testnet')
  20. exec_fn = 'monerod'
  21. testnet_dir = 'stagenet'
  22. new_console_mswin = True
  23. host = 'localhost' # FIXME
  24. rpc_ports = _nw(18081, 38081, None) # testnet is stagenet
  25. cfg_file = 'bitmonero.conf'
  26. datadirs = {
  27. 'linux': [gc.home_dir,'.bitmonero'],
  28. 'win': ['/','c','ProgramData','bitmonero']
  29. }
  30. def init_datadir(self):
  31. self.logdir = super().init_datadir()
  32. return os.path.join(
  33. self.logdir,
  34. self.testnet_dir if self.network == 'testnet' else '' )
  35. def get_p2p_port(self):
  36. return self.rpc_port - 1
  37. def init_subclass(self):
  38. from .rpc import MoneroRPCClient
  39. self.rpc = MoneroRPCClient(
  40. cfg = self.cfg,
  41. proto = self.proto,
  42. host = self.host,
  43. port = self.rpc_port,
  44. user = None,
  45. passwd = None,
  46. test_connection = False,
  47. daemon = self )
  48. self.shared_args = list_gen(
  49. [f'--no-zmq'],
  50. [f'--p2p-bind-port={self.p2p_port}', self.p2p_port],
  51. [f'--rpc-bind-port={self.rpc_port}'],
  52. ['--stagenet', self.network == 'testnet'],
  53. )
  54. self.coind_args = list_gen(
  55. ['--hide-my-port'],
  56. ['--no-igd'],
  57. [f'--data-dir={self.datadir}', self.non_dfl_datadir],
  58. [f'--pidfile={self.pidfile}', self.platform == 'linux'],
  59. ['--detach', not (self.opt.no_daemonize or self.platform=='win')],
  60. ['--offline', not self.opt.online],
  61. )
  62. @property
  63. def stop_cmd(self):
  64. if self.platform == 'win':
  65. return ['kill','-Wf',self.pid]
  66. elif contains_any( self.start_cmd, ['--restricted-rpc','--public-node'] ):
  67. return ['kill',self.pid]
  68. else:
  69. return [self.exec_fn] + self.shared_args + ['exit']
  70. class MoneroWalletDaemon(RPCDaemon):
  71. master_daemon = 'monero_daemon'
  72. rpc_type = 'Monero wallet'
  73. exec_fn = 'monero-wallet-rpc'
  74. coin = 'XMR'
  75. new_console_mswin = True
  76. networks = ('mainnet','testnet')
  77. rpc_ports = _nw(13131, 13141, None) # testnet is non-standard
  78. _reset_ok = ('debug','wait','pids','force_kill')
  79. def __init__(
  80. self,
  81. cfg,
  82. proto,
  83. wallet_dir,
  84. test_suite = False,
  85. host = None,
  86. user = None,
  87. passwd = None,
  88. daemon_addr = None,
  89. proxy = None,
  90. port_shift = None,
  91. datadir = None,
  92. trust_daemon = False ):
  93. self.proto = proto
  94. self.test_suite = test_suite
  95. super().__init__(cfg)
  96. self.network = proto.network
  97. self.wallet_dir = wallet_dir
  98. self.rpc_port = getattr(self.rpc_ports,self.network) + (11 if test_suite else 0)
  99. if port_shift:
  100. self.rpc_port += port_shift
  101. id_str = f'{self.exec_fn}-{self.bind_port}'
  102. self.datadir = os.path.join((datadir or self.exec_fn),('','test_suite')[test_suite])
  103. self.pidfile = os.path.join(self.datadir,id_str+'.pid')
  104. self.logfile = os.path.join(self.datadir,id_str+'.log')
  105. self.proxy = proxy
  106. self.daemon_addr = daemon_addr
  107. self.daemon_port = (
  108. None if daemon_addr else
  109. CoinDaemon(
  110. cfg = self.cfg,
  111. proto = proto,
  112. test_suite = test_suite).rpc_port
  113. )
  114. self.host = host or self.cfg.wallet_rpc_host or self.cfg.monero_wallet_rpc_host
  115. self.user = user or self.cfg.wallet_rpc_user or self.cfg.monero_wallet_rpc_user
  116. self.passwd = passwd or self.cfg.wallet_rpc_password or self.cfg.monero_wallet_rpc_password
  117. assert self.host
  118. assert self.user
  119. if not self.passwd:
  120. die(1,
  121. 'You must set your Monero wallet RPC password.\n' +
  122. 'This can be done on the command line with the --wallet-rpc-password option\n' +
  123. "(insecure, not recommended), or by setting 'monero_wallet_rpc_password' in\n" +
  124. "the MMGen config file." )
  125. self.daemon_args = list_gen(
  126. ['--trusted-daemon', trust_daemon],
  127. ['--untrusted-daemon', not trust_daemon],
  128. [f'--rpc-bind-port={self.rpc_port}'],
  129. [f'--wallet-dir={self.wallet_dir}'],
  130. [f'--log-file={self.logfile}'],
  131. [f'--rpc-login={self.user}:{self.passwd}'],
  132. [f'--daemon-address={self.daemon_addr}', self.daemon_addr],
  133. [f'--daemon-port={self.daemon_port}', not self.daemon_addr],
  134. [f'--proxy={self.proxy}', self.proxy],
  135. [f'--pidfile={self.pidfile}', self.platform == 'linux'],
  136. ['--detach', not (self.opt.no_daemonize or self.platform=='win')],
  137. ['--stagenet', self.network == 'testnet'],
  138. ['--allow-mismatched-daemon-version', test_suite],
  139. )
  140. from .rpc import MoneroWalletRPCClient
  141. self.rpc = MoneroWalletRPCClient(
  142. cfg = self.cfg,
  143. daemon = self,
  144. test_connection = False )