daemon.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. 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', 18003003, '0.18.3.3-release')
  19. networks = ('mainnet','testnet')
  20. exec_fn = 'monerod'
  21. testnet_dir = 'stagenet'
  22. new_console_mswin = True
  23. rpc_ports = _nw(18081, 38081, None) # testnet is stagenet
  24. cfg_file = 'bitmonero.conf'
  25. datadirs = {
  26. 'linux': [gc.home_dir,'.bitmonero'],
  27. 'win32': ['/','c','ProgramData','bitmonero']
  28. }
  29. def init_datadir(self):
  30. self.logdir = super().init_datadir()
  31. return os.path.join(
  32. self.logdir,
  33. self.testnet_dir if self.network == 'testnet' else '' )
  34. def get_p2p_port(self):
  35. return self.rpc_port - 1
  36. def init_subclass(self):
  37. from .rpc import MoneroRPCClient
  38. self.rpc = MoneroRPCClient(
  39. cfg = self.cfg,
  40. proto = self.proto,
  41. host = 'localhost',
  42. port = self.rpc_port,
  43. user = None,
  44. passwd = None,
  45. test_connection = False,
  46. daemon = self )
  47. self.shared_args = list_gen(
  48. ['--no-zmq'],
  49. [f'--p2p-bind-port={self.p2p_port}', self.p2p_port],
  50. [f'--rpc-bind-port={self.rpc_port}'],
  51. ['--stagenet', self.network == 'testnet'],
  52. )
  53. self.coind_args = list_gen(
  54. ['--hide-my-port'],
  55. ['--no-igd'],
  56. [f'--data-dir={self.datadir}', self.non_dfl_datadir],
  57. [f'--pidfile={self.pidfile}', self.platform == 'linux'],
  58. ['--detach', not (self.opt.no_daemonize or self.platform=='win32')],
  59. ['--offline', not self.opt.online],
  60. )
  61. @property
  62. def stop_cmd(self):
  63. if self.platform == 'win32':
  64. return ['kill','-Wf',self.pid]
  65. elif contains_any( self.start_cmd, ['--restricted-rpc','--public-node'] ):
  66. return ['kill',self.pid]
  67. else:
  68. return [self.exec_fn] + self.shared_args + ['exit']
  69. class MoneroWalletDaemon(RPCDaemon):
  70. master_daemon = 'monero_daemon'
  71. rpc_type = 'Monero wallet'
  72. exec_fn = 'monero-wallet-rpc'
  73. coin = 'XMR'
  74. new_console_mswin = True
  75. networks = ('mainnet','testnet')
  76. rpc_ports = _nw(13131, 13141, None) # testnet is non-standard
  77. _reset_ok = ('debug','wait','pids','force_kill')
  78. test_suite_datadir = os.path.join('test','daemons','xmrtest','wallet_rpc')
  79. def start(self,*args,**kwargs):
  80. try: # NB: required due to bug in v18.3.1: PID file not deleted on shutdown
  81. os.unlink(self.pidfile)
  82. except FileNotFoundError:
  83. pass
  84. super().start(*args,**kwargs)
  85. def __init__(
  86. self,
  87. cfg,
  88. proto,
  89. wallet_dir = None,
  90. test_suite = False,
  91. user = None,
  92. passwd = None,
  93. monerod_addr = None,
  94. proxy = None,
  95. port_shift = None,
  96. datadir = None,
  97. trust_monerod = False,
  98. test_monerod = False,
  99. opts = None,
  100. flags = None ):
  101. self.proto = proto
  102. self.test_suite = test_suite
  103. super().__init__(cfg,opts=opts,flags=flags)
  104. self.network = proto.network
  105. self.wallet_dir = wallet_dir or (self.test_suite_datadir if test_suite else None)
  106. self.rpc_port = (
  107. self.cfg.wallet_rpc_port or
  108. getattr(self.rpc_ports,self.network) + (11 if test_suite else 0) )
  109. if port_shift:
  110. self.rpc_port += port_shift
  111. id_str = f'{self.exec_fn}-{self.bind_port}'
  112. self.datadir = datadir or (self.test_suite_datadir if test_suite else self.exec_fn + '.d')
  113. self.pidfile = os.path.join(self.datadir,id_str+'.pid')
  114. self.logfile = os.path.join(self.datadir,id_str+'.log')
  115. self.proxy = proxy
  116. self.monerod_addr = monerod_addr
  117. self.monerod_port = (
  118. None if monerod_addr else
  119. CoinDaemon(
  120. cfg = self.cfg,
  121. proto = proto,
  122. test_suite = test_suite).rpc_port
  123. )
  124. if test_monerod and self.monerod_port:
  125. import socket
  126. try:
  127. socket.create_connection(('localhost',self.monerod_port),timeout=1).close()
  128. except:
  129. die( 'SocketError', f'Unable to connect to Monero daemon at localhost:{self.monerod_port}' )
  130. self.user = user or self.cfg.wallet_rpc_user or self.cfg.monero_wallet_rpc_user
  131. self.passwd = passwd or self.cfg.wallet_rpc_password or self.cfg.monero_wallet_rpc_password
  132. assert self.user
  133. if not self.passwd:
  134. die(1,
  135. 'You must set your Monero wallet RPC password.\n' +
  136. 'This can be done on the command line with the --wallet-rpc-password option\n' +
  137. "(insecure, not recommended), or by setting 'monero_wallet_rpc_password' in\n" +
  138. "the MMGen config file." )
  139. self.daemon_args = list_gen(
  140. ['--trusted-daemon', trust_monerod],
  141. ['--untrusted-daemon', not trust_monerod],
  142. [f'--rpc-bind-port={self.rpc_port}'],
  143. [f'--wallet-dir={self.wallet_dir}'],
  144. [f'--log-file={self.logfile}'],
  145. [f'--rpc-login={self.user}:{self.passwd}'],
  146. [f'--daemon-address={self.monerod_addr}', self.monerod_addr],
  147. [f'--daemon-port={self.monerod_port}', not self.monerod_addr],
  148. [f'--proxy={self.proxy}', self.proxy],
  149. [f'--pidfile={self.pidfile}', self.platform == 'linux'],
  150. ['--detach', not (self.opt.no_daemonize or self.platform=='win32')],
  151. ['--stagenet', self.network == 'testnet'],
  152. ['--allow-mismatched-daemon-version', test_suite],
  153. )
  154. from .rpc import MoneroWalletRPCClient
  155. self.rpc = MoneroWalletRPCClient(
  156. cfg = self.cfg,
  157. daemon = self,
  158. test_connection = False )