Example #1
0
    def query(self,
              name,
              counter=1,
              creator_account=None,
              created_time=None,
              page_size=None,
              first_tx_hash=None,
              **kwargs):
        """
        Creates a protobuf query with specified set of entities
        :param name: CamelCased name of query to be executed
        :param counter: query counter, should be incremented for each new query
        :param creator_account: account id of query creator
        :param created_time: query creation timestamp in milliseconds
        :param page_size: a non-zero positive number, size of result rowset for queries with pagination
        :param first_tx_hash: optional hash of a transaction that will be the beginning of the next page
        :param kwargs: query arguments as they defined in schema
        :return: a proto query
        """
        assert creator_account or self.creator_account, "No account name specified as query creator id"
        pagination_meta = None
        if not created_time:
            created_time = self.now()
        if not creator_account:
            creator_account = self.creator_account
        if page_size or first_tx_hash:
            pagination_meta = queries_pb2.TxPaginationMeta()
            pagination_meta.page_size = page_size
            if first_tx_hash:
                pagination_meta.first_tx_hash = first_tx_hash

        meta = queries_pb2.QueryPayloadMeta()
        meta.created_time = created_time
        meta.creator_account_id = creator_account
        meta.query_counter = counter

        query_wrapper = queries_pb2.Query()
        query_wrapper.payload.meta.CopyFrom(meta)
        field_name = Iroha._camel_case_to_snake_case(name)
        internal_query = getattr(query_wrapper.payload, field_name)
        for key, value in kwargs.items():
            if 'tx_hashes' == key:
                hashes_attr = getattr(internal_query, key)
                hashes_attr.extend(value)
                continue
            setattr(internal_query, key, value)
        if pagination_meta:
            pagination_meta_attr = getattr(internal_query, 'pagination_meta')
            pagination_meta_attr.CopyFrom(pagination_meta)
        if not len(kwargs):
            message = getattr(queries_pb2, name)()
            internal_query.CopyFrom(message)
        return query_wrapper
Example #2
0
def send_query(query, key_pair):
    query_blob = proto_query_helper.signAndAddSignature(query, key_pair).blob()

    proto_query = queries_pb2.Query()

    if sys.version_info[0] == 2:
        tmp = ''.join(map(chr, query_blob))
    else:
        tmp = bytes(query_blob)

    proto_query.ParseFromString(tmp)

    channel = grpc.insecure_channel('127.0.0.1:50051')
    query_stub = endpoint_pb2_grpc.QueryServiceStub(channel)
    query_response = query_stub.Find(proto_query)

    return query_response
def send_query(query, key_pair, address='127.0.0.1:50051'):
    """
    Send query to iroha address
    :param query: to send
    :param key_pair: for signatures
    :param address: iroha address
    :return: query response
    """
    query_blob = proto_query_helper.signAndAddSignature(query, key_pair).blob()

    proto_query = queries_pb2.Query()

    if sys.version_info[0] == 2:
        tmp = ''.join(map(chr, query_blob))
    else:
        tmp = bytes(query_blob)

    proto_query.ParseFromString(tmp)

    channel = grpc.insecure_channel(address)
    query_stub = endpoint_pb2_grpc.QueryServiceStub(channel)
    query_response = query_stub.Find(proto_query)

    return MessageToJson(query_response)
Example #4
0
response = stub.Status(request)
status = endpoint_pb2.TxStatus.Name(response.tx_status)
print("Status of transaction is:", status)

if status != "COMMITTED":
    print("Your transaction wasn't committed")
    exit(1)

query = query_builder.creatorAccountId(creator) \
    .createdTime(current_time) \
    .queryCounter(start_query_counter) \
    .getAssetInfo("dollar#ru") \
    .build()
query_blob = proto_query_helper.signAndAddSignature(query, me_kp).blob()

proto_query = queries_pb2.Query()
proto_query.ParseFromString(''.join(map(chr, query_blob)))

query_stub = endpoint_pb2_grpc.QueryServiceStub(channel)
query_response = query_stub.Find(proto_query)

if not query_response.HasField("asset_response"):
    print("Query response error")
    exit(1)
else:
    print("Query responded with asset response")

asset_info = query_response.asset_response.asset
print("Asset Id =", asset_info.asset_id)
print("Precision =", asset_info.precision)