regtest.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2021 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. regtest: Coin daemon regression test mode setup and operations for the MMGen suite
  20. """
  21. import os,time,shutil,json,re
  22. from subprocess import run,PIPE
  23. from .common import *
  24. from .protocol import init_proto
  25. from .rpc import rpc_init,json_encoder
  26. def create_data_dir(data_dir):
  27. try: os.stat(os.path.join(data_dir,'regtest'))
  28. except: pass
  29. else:
  30. if keypress_confirm(
  31. f'Delete your existing MMGen regtest setup at {data_dir!r} and create a new one?'):
  32. shutil.rmtree(data_dir)
  33. else:
  34. die()
  35. try: os.makedirs(data_dir)
  36. except: pass
  37. def cliargs_convert(args):
  38. def gen():
  39. for arg in args:
  40. if arg.lower() in ('true','false'):
  41. yield (True,False)[arg.lower() == 'false']
  42. elif len(str(arg)) < 20 and re.match(r'[0-9]+',arg):
  43. yield int(arg)
  44. else:
  45. yield arg
  46. return tuple(gen())
  47. class MMGenRegtest(MMGenObject):
  48. rpc_user = 'bobandalice'
  49. rpc_password = 'hodltothemoon'
  50. users = ('bob','alice','miner')
  51. coins = ('btc','bch','ltc')
  52. usr_cmds = ('setup','generate','send','start','stop', 'state', 'balances','mempool','cli','wallet_cli')
  53. def __init__(self,coin):
  54. self.coin = coin.lower()
  55. assert self.coin in self.coins, f'{coin!r}: invalid coin for regtest'
  56. from .daemon import CoinDaemon
  57. self.proto = init_proto(self.coin,regtest=True)
  58. self.d = CoinDaemon(self.coin+'_rt',test_suite=g.test_suite)
  59. async def generate(self,blocks=1,silent=False):
  60. blocks = int(blocks)
  61. async def have_generatetoaddress():
  62. ret = await self.rpc_call('help','generatetoaddress',wallet='miner')
  63. return not 'unknown command:' in ret
  64. async def get_miner_address():
  65. return await self.rpc_call('getnewaddress',wallet='miner')
  66. if self.d.state == 'stopped':
  67. die(1,'Regtest daemon is not running')
  68. self.d.wait_for_state('ready')
  69. if await have_generatetoaddress():
  70. cmd_args = ( 'generatetoaddress', blocks, await get_miner_address() )
  71. else:
  72. cmd_args = ( 'generate', blocks )
  73. out = await self.rpc_call(*cmd_args,wallet='miner')
  74. if len(out) != blocks:
  75. rdie(1,'Error generating blocks')
  76. gmsg(f'Mined {blocks} block{suf(blocks)}')
  77. async def setup(self):
  78. try: os.makedirs(self.d.datadir)
  79. except: pass
  80. if self.d.state != 'stopped':
  81. await self.rpc_call('stop')
  82. create_data_dir(self.d.datadir)
  83. gmsg(f'Starting {self.coin.upper()} regtest setup')
  84. self.d.start(silent=True)
  85. rpc = await rpc_init(self.proto,backend=None,daemon=self.d)
  86. for user in ('miner','bob','alice'):
  87. gmsg(f'Creating {capfirst(user)}’s wallet')
  88. await rpc.icall(
  89. 'createwallet',
  90. wallet_name = user,
  91. no_keys = user != 'miner',
  92. load_on_startup = False )
  93. await self.generate(432,silent=True)
  94. gmsg('Setup complete')
  95. if opt.setup_no_stop_daemon:
  96. msg('Leaving regtest daemon running')
  97. else:
  98. msg('Stopping regtest daemon')
  99. await self.rpc_call('stop')
  100. def init_daemon(self,reindex=False):
  101. if reindex:
  102. self.d.usr_coind_args.append('--reindex')
  103. async def start_daemon(self,reindex=False,silent=True):
  104. self.init_daemon(reindex=reindex)
  105. self.d.start(silent=silent)
  106. for user in ('miner','bob','alice'):
  107. msg(f'Loading {capfirst(user)}’s wallet')
  108. await self.rpc_call('loadwallet',user,start_daemon=False)
  109. async def rpc_call(self,*args,wallet=None,start_daemon=True):
  110. # g.prog_name == 'mmgen-regtest' test is used by .rpc to identify caller, so require this:
  111. assert g.prog_name == 'mmgen-regtest', 'only mmgen-regtest util is allowed to use this method'
  112. if start_daemon and self.d.state == 'stopped':
  113. await self.start_daemon()
  114. rpc = await rpc_init(self.proto,backend=None,daemon=self.d)
  115. return await rpc.call(*args,wallet=wallet)
  116. async def start(self):
  117. if self.d.state == 'stopped':
  118. await self.start_daemon(silent=False)
  119. else:
  120. msg(f'{g.coin} regtest daemon already started')
  121. async def stop(self):
  122. if self.d.state == 'stopped':
  123. msg(f'{g.coin} regtest daemon already stopped')
  124. else:
  125. msg(f'Stopping {g.coin} regtest daemon')
  126. await self.rpc_call('stop',start_daemon=False)
  127. def state(self):
  128. msg(self.d.state)
  129. async def balances(self):
  130. bal = {}
  131. users = ('bob','alice')
  132. for user in users:
  133. out = await self.rpc_call('listunspent',0,wallet=user)
  134. bal[user] = sum(e['amount'] for e in out)
  135. fs = '{:<16} {:18.8f}'
  136. for user in users:
  137. msg(fs.format(user.capitalize()+"'s balance:",bal[user]))
  138. msg(fs.format('Total balance:',sum(v for k,v in bal.items())))
  139. async def send(self,addr,amt):
  140. gmsg(f'Sending {amt} miner {self.d.coin} to address {addr}')
  141. cp = await self.rpc_call('sendtoaddress',addr,str(amt),wallet='miner')
  142. await self.generate(1)
  143. async def mempool(self):
  144. await self.cli('getrawmempool')
  145. async def cli(self,*args):
  146. ret = await self.rpc_call(*cliargs_convert(args))
  147. print(ret if type(ret) == str else json.dumps(ret,cls=json_encoder,indent=4))
  148. async def wallet_cli(self,wallet,*args):
  149. ret = await self.rpc_call(*cliargs_convert(args),wallet=wallet)
  150. print(ret if type(ret) == str else json.dumps(ret,cls=json_encoder,indent=4))
  151. async def cmd(self,args):
  152. ret = getattr(self,args[0])(*args[1:])
  153. return (await ret) if type(ret).__name__ == 'coroutine' else ret
  154. async def fork(self,coin): # currently disabled
  155. proto = init_proto(coin,False)
  156. if not [f for f in proto.forks if f[2] == proto.coin.lower() and f[3] == True]:
  157. die(1,f'Coin {proto.coin} is not a replayable fork of coin {coin}')
  158. gmsg(f'Creating fork from coin {coin} to coin {proto.coin}')
  159. source_rt = MMGenRegtest(coin)
  160. try:
  161. os.stat(source_rt.d.datadir)
  162. except:
  163. die(1,f'Source directory {source_rt.d.datadir!r} does not exist!')
  164. # stop the source daemon
  165. if source_rt.d.state != 'stopped':
  166. await source_rt.d.cli('stop')
  167. # stop our daemon
  168. if self.d.state != 'stopped':
  169. await self.rpc_call('stop')
  170. try: os.makedirs(self.d.datadir)
  171. except: pass
  172. create_data_dir(self.d.datadir)
  173. os.rmdir(self.d.datadir)
  174. shutil.copytree(source_data_dir,self.d.datadir,symlinks=True)
  175. await self.start_daemon(reindex=True)
  176. await self.rpc_call('stop')
  177. gmsg(f'Fork {proto.coin} successfully created')