Пример #1
0
def create_bgt_transaction(verb, name, value, signer, to=None):
    payload = BgtPayload(verb=verb, name=name, value=value, to=to)

    # The prefix should eventually be looked up from the
    # validator's namespace registry.
    addr = make_bgt_address(name)
    inputs = [addr]
    outputs = [addr]
    if to is not None:
        addr_to = make_bgt_address(to)
        inputs.append(addr_to)
        outputs.append(addr_to)

    header = transaction_pb2.TransactionHeader(
        signer_public_key=signer.get_public_key().as_hex(),
        family_name='bgt',
        family_version='1.0',
        inputs=inputs,
        outputs=outputs,
        dependencies=[],
        payload_sha512=payload.sha512(),
        batcher_public_key=signer.get_public_key().as_hex(),
        nonce=hex(random.randint(0, 2**64)))

    header_bytes = header.SerializeToString()

    signature = signer.sign(header_bytes)

    transaction = transaction_pb2.Transaction(header=header_bytes,
                                              payload=payload.to_cbor(),
                                              header_signature=signature)

    return transaction
Пример #2
0
    def create_set_request(self, name, value):
        address = make_bgt_address(name)

        if value is not None:
            data = self._dumps({name: value})
        else:
            data = None

        return self._factory.create_set_request({address: data})
Пример #3
0
def create_bgt_transaction(verb, name, value, deps, signer):
    """Creates a signed bgt transaction.

    Args:
        verb (str): the action the transaction takes, either 'set', 'inc',
            or 'dec'
        name (str): the variable name which is altered by verb and value
        value (int): the amount to set, increment, or decrement
        deps ([str]): a list of transaction header_signatures which are
            required dependencies which must be processed prior to
            processing this transaction
        signer (:obj:`Signer`): the cryptographic signer for signing the
            transaction

    Returns:
        transaction (transaction_pb2.Transaction): the signed bgt
            transaction
    """
    payload = BgtPayload(
        verb=verb, name=name, value=value)

    # The prefix should eventually be looked up from the
    # validator's namespace registry.
    addr = make_bgt_address(name)

    header = transaction_pb2.TransactionHeader(
        signer_public_key=signer.get_public_key().as_hex(),
        family_name='bgt',
        family_version='1.0',
        inputs=[addr],
        outputs=[addr],
        dependencies=deps,
        payload_sha512=payload.sha512(),
        batcher_public_key=signer.get_public_key().as_hex(),
        nonce=hex(random.randint(0, 2**64)))

    header_bytes = header.SerializeToString()

    signature = signer.sign(header_bytes)

    transaction = transaction_pb2.Transaction(
        header=header_bytes,
        payload=payload.to_cbor(),
        header_signature=signature)

    return transaction
Пример #4
0
 def create_set_response(self, name):
     addresses = [make_bgt_address(name)]
     return self._factory.create_set_response(addresses)
Пример #5
0
 def create_get_request(self, name):
     addresses = [make_bgt_address(name)]
     return self._factory.create_get_request(addresses)
Пример #6
0
    def _create_txn(self, txn_function, verb, name, value):
        payload = self._dumps({'Verb': verb, 'Name': name, 'Value': value})

        addresses = [make_bgt_address(name)]

        return txn_function(payload, addresses, addresses, [])
    async def run_transaction(self, request):
        """
        make transfer from wallet to wallet
        """
        family = request.url.query.get('family', None)
        if family == 'bgt' :
            cmd = request.url.query.get('cmd', None)
            arg1 = request.url.query.get('wallet', None)
            if cmd == 'show':
                address = make_bgt_address(arg1)
                error_traps = [error_handlers.InvalidAddressTrap,error_handlers.StateNotFoundTrap]
                response = await self._query_validator(
                    Message.CLIENT_STATE_GET_REQUEST,
                    client_state_pb2.ClientStateGetResponse,
                    client_state_pb2.ClientStateGetRequest(
                        state_root='',
                        address=address),
                    error_traps)
                LOGGER.debug('run_transaction: BGT show=%s (%s)!',arg1,response)
                if response['status'] == 'OK':
                    bgt = loads_bgt_token(response['value'],arg1)
                    LOGGER.debug('run_transaction: BGT[%s]=%s!',arg1,bgt)
                else:
                    bgt = response['value']
                return self._wrap_response(
                    request,
                    data=bgt,
                    metadata=self._get_metadata(request, response))


            
            arg2 = request.url.query.get('amount', None)
            arg3 = request.url.query.get('to', None)
            LOGGER.debug('run_transaction family=%s cmd=%s(%s,%s) query=%s!!!',family,cmd,arg1,arg2,request.url.query)
            transaction = create_bgt_transaction(verb=cmd,name=arg1,value=int(arg2),signer=self._signer,to=arg3)
            batch = self._create_batch([transaction])
            batch_id = batch.header_signature
        else:
            # undefined families
            batch_id = None
            link = ''

        if batch_id is not None:
            error_traps = [error_handlers.BatchInvalidTrap,error_handlers.BatchQueueFullTrap]
            validator_query = client_batch_submit_pb2.ClientBatchSubmitRequest(batches=[batch])
            LOGGER.debug('run_transaction send batch_id=%s',batch_id)

            with self._post_batches_validator_time.time():
                await self._query_validator(
                    Message.CLIENT_BATCH_SUBMIT_REQUEST,
                    client_batch_submit_pb2.ClientBatchSubmitResponse,
                    validator_query,
                    error_traps)
            link = self._build_url(request, path='/batch_statuses', id=batch_id)
        return self._wrap_response(
            request,
            data=None,
            metadata={
              'link': link,
            }
            )