main_regtest.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2023 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. mmgen-regtest: Coin daemon regression test mode setup and operations for the MMGen
  20. suite
  21. """
  22. import mmgen.opts as opts
  23. from .cfg import gc,Config
  24. from .util import die,async_run
  25. opts_data = {
  26. 'sets': [('yes', True, 'quiet', True)],
  27. 'text': {
  28. 'desc': f'Coin daemon regression test mode setup and operations for the {gc.proj_name} suite',
  29. 'usage': '[opts] <command>',
  30. 'options': """
  31. -h, --help Print this help message
  32. --, --longhelp Print help message for long options (common options)
  33. -e, --empty Don't fund Bob and Alice's wallets on setup
  34. -n, --setup-no-stop-daemon Don't stop daemon after setup is finished
  35. -q, --quiet Produce quieter output
  36. -v, --verbose Produce more verbose output
  37. """,
  38. 'notes': """
  39. AVAILABLE COMMANDS
  40. setup - set up Bob and Alice regtest mode
  41. start - start the regtest coin daemon
  42. stop - stop the regtest coin daemon
  43. generate N - mine N blocks (defaults to 1)
  44. send ADDR AMT - send amount AMT of miner funds to address ADDR
  45. state - show current state of daemon (ready, busy, or stopped)
  46. balances - get Bob and Alice's balances
  47. mempool - show transaction IDs in mempool
  48. cli - execute an RPC call with supplied arguments
  49. wallet_cli - execute a wallet RPC call with supplied arguments (wallet
  50. is first argument)
  51. """
  52. }
  53. }
  54. cfg = Config(opts_data=opts_data)
  55. cmd_args = cfg._args
  56. from .proto.btc.regtest import MMGenRegtest
  57. def check_num_args():
  58. m = getattr(MMGenRegtest,cmd_args[0])
  59. margs = m.__code__.co_varnames[1:m.__code__.co_argcount]
  60. mdfls = m.__defaults__ or ()
  61. amin = len(margs) - len(mdfls)
  62. amax = len(margs)
  63. args = cmd_args[1:]
  64. m = "{}: too {} arguments for command '%s' (must have no {} than {})" % cmd_args[0]
  65. if len(args) < amin:
  66. die(1,m.format(args,'few','less',amin))
  67. elif len(cmd_args[1:]) > amax:
  68. die(1,m.format(args,'many','more',amax))
  69. if not cmd_args:
  70. cfg._opts.usage()
  71. elif cmd_args[0] not in MMGenRegtest.usr_cmds:
  72. die(1,f'{cmd_args[0]!r}: invalid command')
  73. elif cmd_args[0] not in ('cli','wallet_cli','balances'):
  74. check_num_args()
  75. async def main():
  76. await MMGenRegtest(cfg,cfg.coin).cmd(cmd_args)
  77. async_run(main())