Exemplo n.º 1
0
def _do_config_proposal_create(args):
    """Executes the 'proposal create' subcommand.  Given a key file, and a
    series of code/value pairs, it generates batches of hashblock_resource
    transactions in a BatchList instance.  The BatchList is either stored to a
    file or submitted to a validator, depending on the supplied CLI arguments.
    """
    resources = [s.split('=', 1) for s in args.resource]

    signer = _read_signer(args.key)

    txns = [_create_propose_txn(signer, resource) for resource in resources]

    batch = _create_batch(signer, txns)

    batch_list = BatchList(batches=[batch])

    if args.output is not None:
        try:
            with open(args.output, 'wb') as batch_file:
                batch_file.write(batch_list.SerializeToString())
        except IOError as e:
            raise CliException('Unable to write to batch file: {}'.format(
                str(e)))
    elif args.url is not None:
        rest_client = RestClient(args.url)
        rest_client.send_batches(batch_list)
    else:
        raise AssertionError('No target for create set.')
Exemplo n.º 2
0
def _do_config_proposal_vote(args):
    """Executes the 'proposal vote' subcommand.  Given a key file, a proposal
    id and a vote value, it generates a batch of hashblock_resource transactions
    in a BatchList instance.  The BatchList is file or submitted to a
    validator.
    """
    signer = _read_signer(args.key)
    rest_client = RestClient(args.url)

    proposals = _get_proposals(rest_client)

    proposal = None
    for candidate in proposals.candidates:
        if candidate.proposal_id == args.proposal_id:
            proposal = candidate
            break

    if proposal is None:
        raise CliException('No proposal exists with the given id')

    for vote_record in proposal.votes:
        if vote_record.public_key == signer.get_public_key().as_hex():
            raise CliException(
                'A vote has already been recorded with this signing key')

    txn = _create_vote_txn(signer, args.proposal_id, proposal.proposal.code,
                           args.vote_value)
    batch = _create_batch(signer, [txn])

    batch_list = BatchList(batches=[batch])

    rest_client.send_batches(batch_list)
Exemplo n.º 3
0
def _do_match_initiate(args):
    """Executes the 'event initiate' subcommand.  Given a signing private key file, an
    assignment public key file, and a quantity, it generates batches of hashblock_events
    transactions in a BatchList instance.  The BatchList is either stored to a
    file or submitted to a validator, depending on the supplied CLI arguments.
    """
    raw_quantity = []
    ast = parser.parse(args.expr)
    if ast[0].lower() != 'event_quantity':
        raise AssertionError('Invalid quantity specification.')
    else:
        quantity = ast[1]
        if quantity[0].lower() != 'quantity':
            raise AssertionError('Invalid quantity specification.')
        else:
            term_binary = quantity[1]
            if term_binary[0].lower(
            ) != 'term_binary' and term_binary[1].lower() != '.':
                raise AssertionError('Invalid quantity specification.')
            else:
                term = term_binary[2]
                if term[0].lower() != 'term':
                    raise AssertionError('Invalid quantity specification.')
                else:
                    component_unary = term[1]
                    if component_unary[0].lower() != 'component_unary':
                        raise AssertionError('Invalid quantity specification.')
                    else:
                        factor = component_unary[1]
                        if factor[0].lower() != 'factor':
                            raise AssertionError(
                                'Invalid quantity specification.')
                        else:
                            raw_quantity.append(factor[1])

                component_binary = term_binary[3]
                if component_binary[0].lower() != 'component_binary':
                    raise AssertionError('Invalid quantity specification.')
                else:
                    annotatable = component_binary[1]
                    resource_unit = component_binary[2]
                    if annotatable[0].lower() != 'annotatable':
                        raise AssertionError('Invalid quantity specification.')
                    else:
                        simple_unit = annotatable[1]
                        if simple_unit[0].lower() != 'simple_unit':
                            raise AssertionError(
                                'Invalid quantity specification.')
                        else:
                            value_unit = simple_unit[1]
                            raw_quantity.append(hash_lookup[value_unit])
                            raw_quantity.append(hash_lookup[resource_unit])

    signer = _read_signer(args.skey)
    txns = [_create_initiate_txn(args.cmd, signer, raw_quantity)]
    batch = _create_batch(signer, txns)
    batch_list = BatchList(batches=[batch])
    rest_client = RestClient(args.url)
    rest_client.send_batches(batch_list)
Exemplo n.º 4
0
def _do_config_genesis(args):
    signer = _read_signer(args.key)
    public_key = signer.get_public_key().as_hex()

    authorized_keys = args.authorized_key if args.authorized_key else \
        [public_key]
    if public_key not in authorized_keys:
        authorized_keys.append(public_key)

    txns = []

    txns.append(
        _create_propose_txn(signer, ('hashblock.resource.vote.authorized_keys',
                                     ','.join(authorized_keys))))

    if args.approval_threshold is not None:
        if args.approval_threshold < 1:
            raise CliException('approval threshold must not be less than 1')

        if args.approval_threshold > len(authorized_keys):
            raise CliException(
                'approval threshold must not be greater than the number of '
                'authorized keys')

        txns.append(
            _create_propose_txn(signer,
                                ('hashblock.resource.vote.approval_threshold',
                                 str(args.approval_threshold))))

    batch = _create_batch(signer, txns)
    batch_list = BatchList(batches=[batch])

    try:
        with open(args.output, 'wb') as batch_file:
            batch_file.write(batch_list.SerializeToString())
        print('Generated {}'.format(args.output))
    except IOError as e:
        raise CliException('Unable to write to batch file: {}'.format(str(e)))
Exemplo n.º 5
0
def _do_match_reciprocate(args):
    """Executes a reciprocate type subcommand.  Given a key file, an event
    id, a quantity, and a ratop, it generates a batch of hashblock-match transactions
    in a BatchList instance.  The BatchList is file or submitted to a
    validator.
    """
    signer = _read_signer(args.skey)
    rest_client = RestClient(args.url)

    unmatched_events = _get_unmatched_event_list(
        exchange_list_for('utxq', args.format, rest_client))

    initiate_event_id = None
    for unmatched_event in unmatched_events:
        if unmatched_event['event_id'] == args.utxq:
            initiate_event_id = unmatched_event
            break

    if initiate_event_id is None:
        raise CliException(
            'No unmatched initiating event exists with the given id:{}'.format(
                args.utxq))

    quantities = args.expr

    if quantities[1] != '@' and quantities[3].lower() != 'for':
        raise AssertionError('Invalid specification.')

    r_quantity = Quantity()
    event_quantity = parser.parse(quantities[0])
    if event_quantity[0].lower() != 'event_quantity':
        raise AssertionError('Invalid quantity specification.')
    else:
        quantity_prefix = event_quantity[1]
        if quantity_prefix[0].lower() != 'quantity_prefix':
            raise AssertionError('Invalid quantity specification.')
        else:
            value_unit = quantity_prefix[1]
            term = quantity_prefix[2]
            if term[0].lower() != 'term':
                raise AssertionError('Invalid quantity specification.')
            else:
                component_unary = term[1]
                if component_unary[0].lower() != 'component_unary':
                    raise AssertionError('Invalid quantity specification.')
                else:
                    factor_annotation = component_unary[1]
                    if factor_annotation[0].lower() != 'factor_annotation':
                        raise AssertionError('Invalid quantity specification.')
                    else:
                        r_quantity.value = (int(
                            factor_annotation[1])).to_bytes(2,
                                                            byteorder='little')
                        r_quantity.valueUnit = (int(
                            hash_lookup[value_unit])).to_bytes(
                                2, byteorder='little')
                        r_quantity.resourceUnit = (int(
                            hash_lookup[factor_annotation[2]])).to_bytes(
                                2, byteorder='little')

    numerator = Quantity()
    event_quantity = parser.parse(quantities[2])
    if event_quantity[0].lower() != 'event_quantity':
        raise AssertionError('Invalid quantity specification.')
    else:
        quantity_prefix = event_quantity[1]
        if quantity_prefix[0].lower() != 'quantity_prefix':
            raise AssertionError('Invalid quantity specification.')
        else:
            value_unit = quantity_prefix[1]
            term = quantity_prefix[2]
            if term[0].lower() != 'term':
                raise AssertionError('Invalid quantity specification.')
            else:
                component_unary = term[1]
                if component_unary[0].lower() != 'component_unary':
                    raise AssertionError('Invalid quantity specification.')
                else:
                    factor_annotation = component_unary[1]
                    if factor_annotation[0].lower() != 'factor_annotation':
                        raise AssertionError('Invalid quantity specification.')
                    else:
                        numerator.value = (int(factor_annotation[1])).to_bytes(
                            2, byteorder='little')
                        numerator.valueUnit = (int(
                            hash_lookup[value_unit])).to_bytes(
                                2, byteorder='little')
                        numerator.resourceUnit = (int(
                            hash_lookup[factor_annotation[2]])).to_bytes(
                                2, byteorder='little')

    denominator = Quantity()
    event_quantity = parser.parse(quantities[4])
    if event_quantity[0].lower() != 'event_quantity':
        raise AssertionError('Invalid quantity specification.')
    else:
        quantity = event_quantity[1]
        if quantity[0].lower() != 'quantity':
            raise AssertionError('Invalid quantity specification.')
        else:
            term_binary = quantity[1]
            if term_binary[0].lower(
            ) != 'term_binary' and term_binary[1].lower() != '.':
                raise AssertionError('Invalid quantity specification.')
            else:
                term = term_binary[2]
                if term[0].lower() != 'term':
                    raise AssertionError('Invalid quantity specification.')
                else:
                    component_unary = term[1]
                    if component_unary[0].lower() != 'component_unary':
                        raise AssertionError('Invalid quantity specification.')
                    else:
                        factor = component_unary[1]
                        if factor[0].lower() != 'factor':
                            raise AssertionError(
                                'Invalid quantity specification.')
                        else:
                            denominator.value = (int(factor[1])).to_bytes(
                                2, byteorder='little')

                component_binary = term_binary[3]
                if component_binary[0].lower() != 'component_binary':
                    raise AssertionError('Invalid quantity specification.')
                else:
                    annotatable = component_binary[1]
                    resource_unit = component_binary[2]
                    if annotatable[0].lower() != 'annotatable':
                        raise AssertionError('Invalid quantity specification.')
                    else:
                        simple_unit = annotatable[1]
                        if simple_unit[0].lower() != 'simple_unit':
                            raise AssertionError(
                                'Invalid quantity specification.')
                        else:
                            value_unit = simple_unit[1]
                            denominator.valueUnit = (int(
                                hash_lookup[value_unit])).to_bytes(
                                    2, byteorder='little')
                            denominator.resourceUnit = (int(
                                hash_lookup[resource_unit])).to_bytes(
                                    2, byteorder='little')

    ratio = Ratio(numerator=numerator, denominator=denominator)
    txn = _create_reciprocate_txn(args.cmd, signer, args.utxq, r_quantity,
                                  ratio)
    batch = _create_batch(signer, [txn])

    batch_list = BatchList(batches=[batch])

    rest_client.send_batches(batch_list)