Ejemplo n.º 1
0
def parse_arguments():
    from create_init_data import KeyKeyValueDict, KeyKeyValueAction

    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers(dest='subcmd',
                                       title='subcommands',
                                       help='additional help')

    #
    # Subcommand: create
    #

    pcreate = subparsers.add_parser(SUBCMD_CREATE,
                                    help='create a chain with nodes')

    pcreate.add_argument(
        '--authorities',
        type=AddressList.from_str,
        metavar='{var}[,{var}[,{var}[,{var}[, ...]]]]'.format(var='AUTHORITY'),
        help='Authorities (addresses) list.')
    pcreate.add_argument('--chain_name',
                         default='test-chain',
                         help='Name of the new chain.')

    pcreate.add_argument(
        '--nodes',
        type=NetworkAddressList.from_str,
        default=NetworkAddressList(),
        metavar='{var}[,{var}[,{var}[,{var}[, ...]]]]'.format(var='IP:PORT'),
        help='Node network addresses for new nodes.')

    # For create init data
    pcreate.add_argument('--super_admin', help='Address of super admin.')
    pcreate.add_argument(
        '--contract_arguments',
        nargs='+',
        type=KeyKeyValueDict.str2tuple,
        action=KeyKeyValueAction,
        default=KeyKeyValueDict(),
        metavar='Contract.Argument=Value',
        help='Update constructor arguments for system contract.'
        ' Can be specify more than once.')

    # For create genesis
    pcreate.add_argument('--timestamp',
                         type=int,
                         help='Specify a timestamp to use.')
    pcreate.add_argument('--resource_dir', help='Chain resource directory.')

    # Modify ports
    pcreate.add_argument('--jsonrpc_port',
                         type=int,
                         default=1337,
                         help='jsonrpc port for this chain')
    pcreate.add_argument('--ws_port',
                         type=int,
                         default=4337,
                         help='websocket port for this chain')

    # enable encrypted
    pcreate.add_argument(
        '--enable_tls',
        action='store_true',
        help='The data is encrypted and transmitted on the network')

    # enable get version
    pcreate.add_argument('--enable_version',
                         action='store_true',
                         help='Jsonrpc will return cita version')

    # switch to stdout
    pcreate.add_argument('--stdout',
                         action='store_true',
                         help='Logs will output to stdout')

    pcreate.add_argument(
        '--init_token',
        type=lambda x: hex(int(x, 16)),
        default=hex(int("0xffffffffffffffffffffffffff", 16)),
        help='Init token for this chain, INIT_TOKEN is a hexadecimal number')

    #
    # Subcommand: append
    #

    pappend = subparsers.add_parser(SUBCMD_APPEND,
                                    help='append a node into a existed chain')

    pappend.add_argument('--chain_name',
                         required=True,
                         help='Name of the existed chain.')

    pappend.add_argument('--node',
                         required=True,
                         type=NetworkAddressList.from_str_get_one,
                         metavar='IP:PORT',
                         help='Node network addresses for new nodes.')

    pappend.add_argument(
        '--address',
        help=
        'The address of new node. Will generate a new address (with privkey) if not set.'
    )

    # enable encrypted
    pappend.add_argument(
        '--enable_tls',
        action='store_true',
        help='The data is encrypted and transmitted on the network')

    # enable get version
    pappend.add_argument('--enable_version',
                         action='store_true',
                         help='Jsonrpc will return cita version')

    # switch to stdout
    pappend.add_argument('--stdout',
                         action='store_true',
                         help='Logs will output to stdout')

    args = parser.parse_args()

    # Check arguments
    if args.subcmd == SUBCMD_CREATE:
        if len(args.nodes) > 256:
            logging.critical(
                'The number of nodes exceeds the maximum limit(256).')
            sys.exit(1)
        if not args.super_admin:
            logging.critical('--super_admin is empty, it\'s required'
                             ' to continue.')
            sys.exit(1)
        if not args.authorities:
            if not args.nodes:
                logging.critical('Both --authorities and --nodes is empty.')
                sys.exit(1)
            authorities, privkeys = generate_keypairs(len(args.nodes))
            args.nodes.add_privkeys(privkeys)
            setattr(args, 'authorities', authorities)
        elif len(args.nodes) != len(args.authorities):
            logging.critical(
                'The number of nodes is not equal to the number of authorities.'
            )
            sys.exit(1)
        args.nodes.add_addresses(args.authorities)
        for val in (('authorities', 'NodeManager', 'nodes'),
                    ('chain_name', 'SysConfig', 'chainName')):
            if args.contract_arguments.kkv_get(val[1], val[2]):
                logging.critical(
                    'Please use --%s to instead of specify'
                    ' --contract_arguments %s.%s directly', val[0], val[1],
                    val[2])
                sys.exit(1)
        args.contract_arguments.kkv_set('SysConfig', 'chainName',
                                        args.chain_name)
        args.contract_arguments.kkv_set('NodeManager', 'nodes',
                                        args.authorities.to_str())
        if not args.contract_arguments.kkv_get('NodeManager', 'stakes'):
            stakes = ','.join(['0' for _ in args.authorities])
            args.contract_arguments.kkv_set('NodeManager', 'stakes', stakes)
    elif args.subcmd == SUBCMD_APPEND:
        if args.address:
            args.node.add_addresses([args.address])
        else:
            addresses, privkeys = generate_keypairs(1)
            args.node.add_addresses(addresses)
            args.node.add_privkeys(privkeys)
    else:
        logging.critical('Please select a valid subcommand.')
        sys.exit(1)
    return args
Ejemplo n.º 2
0
def parse_arguments():
    from create_init_data import KeyKeyValueDict, KeyKeyValueAction

    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers(dest='subcmd',
                                       title='subcommands',
                                       help='additional help')

    #
    # Subcommand: create
    #

    pcreate = subparsers.add_parser(SUBCMD_CREATE,
                                    help='create a chain with nodes')

    pcreate.add_argument(
        '--authorities',
        type=AuthorityList.from_str,
        metavar='{var}[,{var}[,{var}[,{var}[, ...]]]]'.format(var='AUTHORITY'),
        help='Authorities (addresses) list.')
    pcreate.add_argument('--chain_name',
                         default='test-chain',
                         help='Name of the new chain.')

    pcreate.add_argument(
        '--nodes',
        type=AddressList.from_str,
        default=AddressList(),
        metavar='{var}[,{var}[,{var}[,{var}[, ...]]]]'.format(var='IP:PORT'),
        help='Node network addresses for new nodes.')

    # For create init data
    pcreate.add_argument('--super_admin', help='Address of super admin.')
    pcreate.add_argument(
        '--contract_arguments',
        nargs='+',
        type=KeyKeyValueDict.str2tuple,
        action=KeyKeyValueAction,
        default=KeyKeyValueDict(),
        metavar='Contract.Argument=Value',
        help='Update constructor arguments for system contract.'
        ' Can be specify more than once.')

    # For create genesis
    pcreate.add_argument('--timestamp',
                         type=int,
                         help='Specify a timestamp to use.')
    pcreate.add_argument('--resource_dir', help='Chain resource directory.')

    # Modify ports
    pcreate.add_argument('--grpc_port',
                         type=int,
                         default=5000,
                         help='grpc port for this chain')
    pcreate.add_argument('--jsonrpc_port',
                         type=int,
                         default=1337,
                         help='jsonrpc port for this chain')
    pcreate.add_argument('--ws_port',
                         type=int,
                         default=4337,
                         help='websocket port for this chain')

    #
    # Subcommand: append
    #

    pappend = subparsers.add_parser(SUBCMD_APPEND,
                                    help='append a node into a existed chain')

    pappend.add_argument('--chain_name',
                         required=True,
                         help='Name of the existed chain.')

    pappend.add_argument(
        '--signer',
        help='The signer of new node. Will generate a new if not set.')

    pappend.add_argument('--node',
                         required=False,
                         type=AddressList.from_str_get_one,
                         metavar='IP:PORT',
                         help='Node network addresses for new nodes.')

    args = parser.parse_args()

    # Check arguments
    if args.subcmd == SUBCMD_CREATE:
        if not args.authorities:
            if not args.nodes:
                logging.critical('Both --authorities and --nodes is empty.')
                sys.exit(1)
            authorities, signers = generate_authorities(len(args.nodes))
            args.nodes.add_signers(signers)
            setattr(args, 'authorities', authorities)
        for val in (('authorities', 'NodeManager', 'nodes'),
                    ('chain_name', 'SysConfig', 'chainName')):
            if args.contract_arguments.kkv_get(val[1], val[2]):
                logging.critical(
                    'Please use --%s to instead of specify'
                    ' --contract_arguments %s.%s directly', val[0], val[1],
                    val[2])
                sys.exit(1)
        args.contract_arguments.kkv_set('SysConfig', 'chainName',
                                        args.chain_name)
        args.contract_arguments.kkv_set('NodeManager', 'nodes',
                                        args.authorities.to_str())
        if not args.contract_arguments.kkv_get('NodeManager', 'stakes'):
            stakes = ','.join(['0' for _ in args.authorities])
            args.contract_arguments.kkv_set('NodeManager', 'stakes', stakes)
    elif args.subcmd == SUBCMD_APPEND:
        if args.signer:
            args.node.add_signers([args.signer])
        else:
            _, signers = generate_authorities(1)
            args.node.add_signers(signers)
    else:
        logging.critical('Please select a valid subcommand.')
        sys.exit(1)
    return args