ts_regtest.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 mmgen.proto.btc.regtest import MMGenRegtest
  19. from ..include.common import *
  20. from .common import *
  21. from .ts_base import *
  22. args1 = ['--bob']
  23. args2 = ['--bob','--rpc-backend=http']
  24. def gen_addrs(proto,network,keys):
  25. from mmgen.tool.api import tool_api
  26. tool = tool_api()
  27. tool.init_coin(proto.coin,'regtest')
  28. tool.addrtype = proto.mmtypes[-1]
  29. return [tool.privhex2addr('{:064x}'.format(key)) for key in keys]
  30. class TestSuiteRegtest(TestSuiteBase):
  31. 'various operations via regtest mode'
  32. networks = ('btc','ltc','bch')
  33. passthru_opts = ('coin',)
  34. extra_spawn_args = ['--regtest=1']
  35. tmpdir_nums = [1]
  36. color = True
  37. deterministic = False
  38. cmd_group_in = (
  39. ('setup', 'regtest mode setup'),
  40. ('subgroup.halving_calculator', []),
  41. ('subgroup.fund_addrbal', []),
  42. ('subgroup.addrbal', ['fund_addrbal']),
  43. ('subgroup.blocks_info', ['addrbal']),
  44. ('subgroup.feeview', []),
  45. ('stop', 'stopping regtest daemon'),
  46. )
  47. cmd_subgroups = {
  48. 'halving_calculator': (
  49. "'mmnode-halving-calculator' script",
  50. ('halving_calculator1', "halving calculator (--help)"),
  51. ('halving_calculator2', "halving calculator"),
  52. ('halving_calculator3', "halving calculator (--list)"),
  53. ('halving_calculator4', "halving calculator (--mined)"),
  54. ('halving_calculator5', "halving calculator (--mined --bdr-proj=5)"),
  55. ('halving_calculator6', "halving calculator (--mined --sample-size=20)"),
  56. ),
  57. 'fund_addrbal': (
  58. "funding addresses for 'addrbal' subgroup",
  59. ('sendto1', 'sending funds to address #1 (1)'),
  60. ('sendto2', 'sending funds to address #1 (2)'),
  61. ('sendto3', 'sending funds to address #2'),
  62. ),
  63. 'addrbal': (
  64. "'mmnode-addrbal' script",
  65. ('addrbal_single', 'getting address balance (single address)'),
  66. ('addrbal_multiple', 'getting address balances (multiple addresses)'),
  67. ('addrbal_multiple_tabular1', 'getting address balances (multiple addresses, tabular output)'),
  68. ('addrbal_multiple_tabular2', 'getting address balances (multiple addresses, tabular, show first block)'),
  69. ('addrbal_nobal1', 'getting address balances (no balance)'),
  70. ('addrbal_nobal2', 'getting address balances (no balances)'),
  71. ('addrbal_nobal3', 'getting address balances (one null balance)'),
  72. ('addrbal_nobal3_tabular1', 'getting address balances (one null balance, tabular output)'),
  73. ('addrbal_nobal3_tabular2', 'getting address balances (one null balance, tabular, show first block)'),
  74. ),
  75. 'blocks_info': (
  76. "'mmnode-blocks-info' script",
  77. ('blocks_info1', "blocks-info (--help)"),
  78. ('blocks_info2', "blocks-info (no args)"),
  79. ('blocks_info3', "blocks-info +100"),
  80. ('blocks_info4', "blocks-info --miner-info --fields=all --stats=all +1"),
  81. ),
  82. 'feeview': (
  83. "'mmnode-feeview' script",
  84. ('feeview_setup', 'setting up feeview test'),
  85. ('feeview1', "'mmnode-feeview'"),
  86. ('feeview2', "'mmnode-feeview --columns=40 --include-current'"),
  87. ('feeview3', "'mmnode-feeview --precision=6'"),
  88. ('feeview4', "'mmnode-feeview --detail'"),
  89. ('feeview5', "'mmnode-feeview --show-empty --log'"),
  90. ('feeview6', "'mmnode-feeview --ignore-below=1MB'"),
  91. ('feeview7', "'mmnode-feeview --ignore-below=20kB'"),
  92. ('feeview8', "'mmnode-feeview' (empty mempool)"),
  93. ),
  94. }
  95. def __init__(self,trunner,cfgs,spawn):
  96. TestSuiteBase.__init__(self,trunner,cfgs,spawn)
  97. if trunner == None:
  98. return
  99. if self.proto.testnet:
  100. die(2,'--testnet and --regtest options incompatible with regtest test suite')
  101. self.proto = init_proto(self.proto.coin,network='regtest',need_amt=True)
  102. self.addrs = gen_addrs(self.proto,'regtest',[1,2,3,4,5])
  103. self.regtest = MMGenRegtest(self.proto.coin)
  104. def setup(self):
  105. stop_test_daemons(self.proto.network_id,force=True,remove_datadir=True)
  106. from shutil import rmtree
  107. try: rmtree(joinpath(self.tr.data_dir,'regtest'))
  108. except: pass
  109. t = self.spawn('mmgen-regtest',['-n','setup'])
  110. for s in ('Starting','Creating','Creating','Creating','Mined','Setup complete'):
  111. t.expect(s)
  112. return t
  113. def halving_calculator(self,add_args,expect_list):
  114. t = self.spawn('mmnode-halving-calculator',args1+add_args)
  115. t.match_expect_list(expect_list)
  116. return t
  117. def halving_calculator1(self):
  118. return self.halving_calculator(['--help'],['USAGE:'])
  119. def halving_calculator2(self):
  120. return self.halving_calculator([],['Current block: 393',f'Current block subsidy: 12.5 {g.coin}'])
  121. def halving_calculator3(self):
  122. return self.halving_calculator(['--list'],['33 4950','0'])
  123. def halving_calculator4(self):
  124. return self.halving_calculator(['--mined'],['0 0.0000015 14949.9999835'])
  125. def halving_calculator5(self):
  126. return self.halving_calculator(['--mined','--bdr-proj=5'],['5.00000 0 0.0000015 14949.9999835'])
  127. def halving_calculator6(self):
  128. return self.halving_calculator(['--mined','--sample-size=20'],['33 4950','0 0.0000015 14949.9999835'])
  129. def sendto(self,addr,amt):
  130. return self.spawn('mmgen-regtest',['send',addr,amt])
  131. def sendto1(self): return self.sendto(self.addrs[0],'0.123')
  132. def sendto2(self): return self.sendto(self.addrs[0],'0.234')
  133. def sendto3(self): return self.sendto(self.addrs[1],'0.345')
  134. def addrbal(self,args,expect_list):
  135. t = self.spawn('mmnode-addrbal',args)
  136. t.match_expect_list(expect_list)
  137. return t
  138. def addrbal_single(self):
  139. return self.addrbal(
  140. args2 + [self.addrs[0]],
  141. [
  142. f'Balance: 0.357 {g.coin}',
  143. '2 unspent outputs in 2 blocks',
  144. '394','0.123',
  145. '395','0.234'
  146. ])
  147. def addrbal_multiple(self):
  148. return self.addrbal(
  149. args2 + [self.addrs[1],self.addrs[0]],
  150. [
  151. '396','0.345',
  152. '394','0.123',
  153. '395','0.234'
  154. ])
  155. def addrbal_multiple_tabular1(self):
  156. return self.addrbal(
  157. args2 + ['--tabular',self.addrs[1],self.addrs[0]],
  158. [
  159. self.addrs[1] + ' 1 396','0.345',
  160. self.addrs[0] + ' 2 395','0.357'
  161. ])
  162. def addrbal_multiple_tabular2(self):
  163. return self.addrbal(
  164. args2 + ['--tabular','--first-block',self.addrs[1],self.addrs[0]],
  165. [
  166. self.addrs[1] + ' 1 396','396','0.345',
  167. self.addrs[0] + ' 2 394','395','0.357'
  168. ])
  169. def addrbal_nobal1(self):
  170. return self.addrbal(
  171. args2 + [self.addrs[2]], ['Address has no balance'] )
  172. def addrbal_nobal2(self):
  173. return self.addrbal(
  174. args2 + [self.addrs[2],self.addrs[3]], ['Addresses have no balances'] )
  175. def addrbal_nobal3(self):
  176. return self.addrbal(
  177. args2 + [self.addrs[4],self.addrs[0],self.addrs[3]],
  178. [
  179. 'No balance',
  180. '2 unspent outputs in 2 blocks',
  181. '394','0.123','395','0.234',
  182. 'No balance'
  183. ])
  184. def addrbal_nobal3_tabular1(self):
  185. return self.addrbal(
  186. args2 + ['--tabular',self.addrs[4],self.addrs[0],self.addrs[3]],
  187. [
  188. self.addrs[4] + ' - - -',
  189. self.addrs[0] + ' 2 395','0.357',
  190. self.addrs[3] + ' - - -',
  191. ])
  192. def addrbal_nobal3_tabular2(self):
  193. return self.addrbal(
  194. args2 + ['--tabular','--first-block',self.addrs[4],self.addrs[0],self.addrs[3]],
  195. [
  196. self.addrs[4] + ' - - - -',
  197. self.addrs[0] + ' 2 394','395','0.357',
  198. self.addrs[3] + ' - - - -',
  199. ])
  200. def blocks_info(self,args,expect_list):
  201. t = self.spawn('mmnode-blocks-info',args)
  202. t.match_expect_list(expect_list)
  203. return t
  204. def blocks_info1(self):
  205. return self.blocks_info( args1 + ['--help'], ['USAGE:','OPTIONS:'])
  206. def blocks_info2(self):
  207. return self.blocks_info( args1, [
  208. 'Current height: 396',
  209. ])
  210. def blocks_info3(self):
  211. return self.blocks_info( args1 + ['+100'], [
  212. 'Range: 297-396',
  213. 'Current height: 396',
  214. 'Next diff adjust: 2016'
  215. ])
  216. def blocks_info4(self):
  217. n1,i1,o1,n2,i2,o2 = (2,1,3,6,3,9) if g.coin == 'BCH' else (2,1,4,6,3,12)
  218. return self.blocks_info( args1 + ['--miner-info','--fields=all','--stats=all','+3'], [
  219. 'Averages',
  220. f'nTx: {n1}',
  221. f'Inputs: {i1}',
  222. f'Outputs: {o1}',
  223. 'Totals',
  224. f'nTx: {n2}',
  225. f'Inputs: {i2}',
  226. f'Outputs: {o2}',
  227. 'Current height: 396',
  228. 'Next diff adjust: 2016'
  229. ])
  230. async def feeview_setup(self):
  231. def create_pairs(nPairs):
  232. from mmgen.tool.api import tool_api
  233. from collections import namedtuple
  234. t = tool_api()
  235. t.init_coin(self.proto.coin,self.proto.network)
  236. t.addrtype = 'compressed' if self.proto.coin == 'BCH' else 'bech32'
  237. wp = namedtuple('wifaddrpair',['wif','addr'])
  238. def gen():
  239. for n in range(1,nPairs+1):
  240. wif = t.hex2wif(f'{n:064x}')
  241. yield wp( wif, t.wif2addr(wif) )
  242. return list(gen())
  243. def gen_fees(n_in,low,high):
  244. # very approximate tx size estimation:
  245. ibytes,wbytes,obytes = (148,0,34) if self.proto.coin == 'BCH' else (43,108,31)
  246. x = (ibytes + (wbytes//4) + (obytes * nPairs)) * self.proto.coin_amt(self.proto.coin_amt.satoshi)
  247. n = n_in - 1
  248. vmax = high - low
  249. for i in range(n_in):
  250. yield (low + (i/n)**6 * vmax) * x
  251. async def do_tx(inputs,outputs,wif):
  252. tx_hex = await r.rpc_call( 'createrawtransaction', inputs, outputs )
  253. tx = await r.rpc_call( 'signrawtransactionwithkey', tx_hex, [wif], [], self.proto.sighash_type )
  254. assert tx['complete'] == True
  255. return tx['hex']
  256. async def do_tx1():
  257. us = await r.rpc_call('listunspent',wallet='miner')
  258. fee = self.proto.coin_amt('0.001')
  259. outputs = {p.addr:tx1_amt for p in pairs[:nTxs]}
  260. outputs.update({burn_addr: us[0]['amount'] - (tx1_amt*nTxs) - fee})
  261. return await do_tx(
  262. [{ 'txid': us[0]['txid'], 'vout': 0 }],
  263. outputs,
  264. r.miner_wif )
  265. async def do_tx2(tx,pairno):
  266. fee = fees[pairno]
  267. outputs = {p.addr:tx2_amt for p in pairs}
  268. outputs.update({burn_addr: tx1_amt - (tx2_amt*len(pairs)) - fee})
  269. return await do_tx(
  270. [{ 'txid': tx['txid'], 'vout': pairno }],
  271. outputs,
  272. pairs[pairno].wif )
  273. async def do_txs(tx_in):
  274. for pairno in range(nTxs):
  275. tx_hex = await do_tx2(tx_in,pairno)
  276. await r.rpc_call('sendrawtransaction',tx_hex)
  277. self.spawn('',msg_only=True)
  278. r = self.regtest
  279. nPairs = 100
  280. nTxs = 25
  281. tx1_amt = self.proto.coin_amt('{:0.4f}'.format(24 / (nTxs+1))) # 25 BTC subsidy
  282. tx2_amt = self.proto.coin_amt('0.0001')
  283. imsg(f'Creating {nPairs} key-address pairs')
  284. pairs = create_pairs(nPairs+1)
  285. burn_addr = pairs.pop()[1]
  286. imsg(f'Creating funding transaction with {nTxs} outputs of value {tx1_amt} {self.proto.coin}')
  287. tx1_hex = await do_tx1()
  288. imsg(f'Relaying funding transaction')
  289. await r.rpc_call('sendrawtransaction',tx1_hex)
  290. imsg(f'Mining a block')
  291. await r.generate(1,silent=True)
  292. imsg(f'Generating fees for mempool transactions')
  293. fees = list(gen_fees(nTxs,2,120))
  294. imsg(f'Creating and relaying {nTxs} mempool transactions with {nPairs} outputs each')
  295. await do_txs(await r.rpc_call('decoderawtransaction',tx1_hex))
  296. return 'ok'
  297. def _feeview(self,args,expect_list=[]):
  298. t = self.spawn('mmnode-feeview',args)
  299. if expect_list:
  300. t.match_expect_list(expect_list)
  301. return t
  302. def feeview1(self):
  303. return self._feeview([])
  304. def feeview2(self):
  305. return self._feeview(['--columns=40','--include-current'])
  306. def feeview3(self):
  307. return self._feeview(['--precision=6'])
  308. def feeview4(self):
  309. return self._feeview(['--detail'])
  310. def feeview5(self):
  311. return self._feeview(['--show-empty','--log',f'--outdir={self.tmpdir}'])
  312. def feeview6(self):
  313. return self._feeview(['--ignore-below=1MB'])
  314. def feeview7(self):
  315. return self._feeview(['--ignore-below=4kB'])
  316. async def feeview8(self):
  317. imsg('Clearing mempool')
  318. await self.regtest.generate(1,silent=True)
  319. return self._feeview([])
  320. def stop(self):
  321. if opt.no_daemon_stop:
  322. self.spawn('',msg_only=True)
  323. msg_r('(leaving daemon running by user request)')
  324. return 'ok'
  325. else:
  326. return self.spawn('mmgen-regtest',['stop'])