main_regtest.py 3.0 KB

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