ts_regtest.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
  4. # Copyright (C)2013-2022 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-node-tools
  9. # https://gitlab.com/mmgen/mmgen-node-tools
  10. """
  11. ts_regtest.py: Regtest tests for the test.py test suite
  12. """
  13. import os
  14. from mmgen.globalvars import g
  15. from mmgen.opts import opt
  16. from mmgen.util import die,gmsg
  17. from mmgen.protocol import init_proto
  18. from ..include.common import *
  19. from .common import *
  20. from .ts_base import *
  21. args1 = ['--bob']
  22. args2 = ['--bob','--rpc-backend=http']
  23. def gen_addrs(proto,network,keys):
  24. from mmgen.tool.api import tool_api
  25. tool = tool_api()
  26. tool.init_coin(proto.coin,'regtest')
  27. tool.addrtype = proto.mmtypes[-1]
  28. return [tool.privhex2addr('{:064x}'.format(key)) for key in keys]
  29. class TestSuiteRegtest(TestSuiteBase):
  30. 'various operations via regtest mode'
  31. networks = ('btc','ltc','bch')
  32. passthru_opts = ('coin',)
  33. extra_spawn_args = ['--regtest=1']
  34. tmpdir_nums = [1]
  35. color = True
  36. deterministic = False
  37. cmd_group_in = (
  38. ('setup', 'regtest mode setup'),
  39. ('subgroup.halving_calculator', []),
  40. ('subgroup.fund_addrbal', []),
  41. ('subgroup.addrbal', ['fund_addrbal']),
  42. ('stop', 'stopping regtest daemon'),
  43. )
  44. cmd_subgroups = {
  45. 'halving_calculator': (
  46. "'mmnode-halving-calculator' script",
  47. ('halving_calculator1', "halving calculator (--help)"),
  48. ('halving_calculator2', "halving calculator"),
  49. ('halving_calculator3', "halving calculator (--list)"),
  50. ('halving_calculator4', "halving calculator (--mined)"),
  51. ('halving_calculator5', "halving calculator (--mined --bdr-proj=5)"),
  52. ('halving_calculator6', "halving calculator (--mined --sample-size=20)"),
  53. ),
  54. 'fund_addrbal': (
  55. "funding addresses for 'addrbal' subgroup",
  56. ('sendto1', 'sending funds to address #1 (1)'),
  57. ('sendto2', 'sending funds to address #1 (2)'),
  58. ('sendto3', 'sending funds to address #2'),
  59. ),
  60. 'addrbal': (
  61. "'mmnode-addrbal' script",
  62. ('addrbal_single', 'getting address balance (single address)'),
  63. ('addrbal_multiple', 'getting address balances (multiple addresses)'),
  64. ('addrbal_multiple_tabular1', 'getting address balances (multiple addresses, tabular output)'),
  65. ('addrbal_multiple_tabular2', 'getting address balances (multiple addresses, tabular, show first block)'),
  66. ('addrbal_nobal1', 'getting address balances (no balance)'),
  67. ('addrbal_nobal2', 'getting address balances (no balances)'),
  68. ('addrbal_nobal3', 'getting address balances (one null balance)'),
  69. ('addrbal_nobal3_tabular1', 'getting address balances (one null balance, tabular output)'),
  70. ('addrbal_nobal3_tabular2', 'getting address balances (one null balance, tabular, show first block)'),
  71. )
  72. }
  73. def __init__(self,trunner,cfgs,spawn):
  74. TestSuiteBase.__init__(self,trunner,cfgs,spawn)
  75. if trunner == None:
  76. return
  77. if self.proto.testnet:
  78. die(2,'--testnet and --regtest options incompatible with regtest test suite')
  79. self.proto = init_proto(self.proto.coin,network='regtest',need_amt=True)
  80. self.addrs = gen_addrs(self.proto,'regtest',[1,2,3,4,5])
  81. os.environ['MMGEN_TEST_SUITE_REGTEST'] = '1'
  82. def __del__(self):
  83. os.environ['MMGEN_TEST_SUITE_REGTEST'] = ''
  84. def setup(self):
  85. stop_test_daemons(self.proto.network_id,force=True,remove_datadir=True)
  86. from shutil import rmtree
  87. try: rmtree(joinpath(self.tr.data_dir,'regtest'))
  88. except: pass
  89. t = self.spawn('mmgen-regtest',['-n','setup'])
  90. for s in ('Starting','Creating','Creating','Creating','Mined','Setup complete'):
  91. t.expect(s)
  92. return t
  93. def halving_calculator(self,add_args,expect_list):
  94. t = self.spawn('mmnode-halving-calculator',args1+add_args)
  95. t.match_expect_list(expect_list)
  96. return t
  97. def halving_calculator1(self):
  98. return self.halving_calculator(['--help'],['USAGE:'])
  99. def halving_calculator2(self):
  100. return self.halving_calculator([],['Current block: 393',f'Current block subsidy: 12.5 {g.coin}'])
  101. def halving_calculator3(self):
  102. return self.halving_calculator(['--list'],['33 4950','0'])
  103. def halving_calculator4(self):
  104. return self.halving_calculator(['--mined'],['0 0.0000015 14949.9999835'])
  105. def halving_calculator5(self):
  106. return self.halving_calculator(['--mined','--bdr-proj=5'],['5.00000 0 0.0000015 14949.9999835'])
  107. def halving_calculator6(self):
  108. return self.halving_calculator(['--mined','--sample-size=20'],['33 4950','0 0.0000015 14949.9999835'])
  109. def sendto(self,addr,amt):
  110. return self.spawn('mmgen-regtest',['send',addr,amt])
  111. def sendto1(self): return self.sendto(self.addrs[0],'0.123')
  112. def sendto2(self): return self.sendto(self.addrs[0],'0.234')
  113. def sendto3(self): return self.sendto(self.addrs[1],'0.345')
  114. def addrbal(self,args,expect_list):
  115. t = self.spawn('mmnode-addrbal',args)
  116. t.match_expect_list(expect_list)
  117. return t
  118. def addrbal_single(self):
  119. return self.addrbal(
  120. args2 + [self.addrs[0]],
  121. [
  122. f'Balance: 0.357 {g.coin}',
  123. '2 unspent outputs in 2 blocks',
  124. '394','0.123',
  125. '395','0.234'
  126. ])
  127. def addrbal_multiple(self):
  128. return self.addrbal(
  129. args2 + [self.addrs[1],self.addrs[0]],
  130. [
  131. '396','0.345',
  132. '394','0.123',
  133. '395','0.234'
  134. ])
  135. def addrbal_multiple_tabular1(self):
  136. return self.addrbal(
  137. args2 + ['--tabular',self.addrs[1],self.addrs[0]],
  138. [
  139. self.addrs[1] + ' 1 396','0.345',
  140. self.addrs[0] + ' 2 395','0.357'
  141. ])
  142. def addrbal_multiple_tabular2(self):
  143. return self.addrbal(
  144. args2 + ['--tabular','--first-block',self.addrs[1],self.addrs[0]],
  145. [
  146. self.addrs[1] + ' 1 396','396','0.345',
  147. self.addrs[0] + ' 2 394','395','0.357'
  148. ])
  149. def addrbal_nobal1(self):
  150. return self.addrbal(
  151. args2 + [self.addrs[2]], ['Address has no balance'] )
  152. def addrbal_nobal2(self):
  153. return self.addrbal(
  154. args2 + [self.addrs[2],self.addrs[3]], ['Addresses have no balances'] )
  155. def addrbal_nobal3(self):
  156. return self.addrbal(
  157. args2 + [self.addrs[4],self.addrs[0],self.addrs[3]],
  158. [
  159. 'No balance',
  160. '2 unspent outputs in 2 blocks',
  161. '394','0.123','395','0.234',
  162. 'No balance'
  163. ])
  164. def addrbal_nobal3_tabular1(self):
  165. return self.addrbal(
  166. args2 + ['--tabular',self.addrs[4],self.addrs[0],self.addrs[3]],
  167. [
  168. self.addrs[4] + ' - - -',
  169. self.addrs[0] + ' 2 395','0.357',
  170. self.addrs[3] + ' - - -',
  171. ])
  172. def addrbal_nobal3_tabular2(self):
  173. return self.addrbal(
  174. args2 + ['--tabular','--first-block',self.addrs[4],self.addrs[0],self.addrs[3]],
  175. [
  176. self.addrs[4] + ' - - - -',
  177. self.addrs[0] + ' 2 394','395','0.357',
  178. self.addrs[3] + ' - - - -',
  179. ])
  180. def stop(self):
  181. if opt.no_daemon_stop:
  182. self.spawn('',msg_only=True)
  183. msg_r('(leaving daemon running by user request)')
  184. return 'ok'
  185. else:
  186. return self.spawn('mmgen-regtest',['stop'])