Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ordering Transaction Results Issue #72

Closed
LiraLemur opened this issue Jun 30, 2021 · 3 comments · Fixed by #73
Closed

Ordering Transaction Results Issue #72

LiraLemur opened this issue Jun 30, 2021 · 3 comments · Fixed by #73

Comments

@LiraLemur
Copy link

https://iroha.readthedocs.io/en/main/develop/api/queries.html#result-pagination seems not to be working very well:

by @baziorek:

"I've tried:
pagination_meta = queries_pb2.TxPaginationMeta()
pagination_meta.ordering.sequence = [queries_pb2.kCreatedTime, queries_pb2.kAscending]

        query = iroha.query('GetAccountAssetTransactions', account_id=account_id, asset_id=asset_name, page_size=page_size, pagination_meta=pagination_meta)

But for each try I'm seeing problem, e.g. for above:
AttributeError: Assignment not allowed to repeated field "sequence" in protocol message object."

@baziorek
Copy link

@LiraLemur As You asked on the chat here is full code (I'm not professional python programmer):

import os
import binascii
from iroha import IrohaCrypto, Iroha, IrohaGrpc, queries_pb2


IROHA_HOST_ADDR = os.getenv('IROHA_HOST_ADDR', '127.0.0.1')
IROHA_PORT = os.getenv('IROHA_PORT', '50051')
ADMIN_ACCOUNT_ID = os.getenv('ADMIN_ACCOUNT_ID', 'admin@test')
ADMIN_PRIVATE_KEY = os.getenv(
   'ADMIN_PRIVATE_KEY', 'f101537e319568c765b2cc89698325604991dca57b9716b58016b253506cab70')


user_account = 'a@test'
user_private_key = '1234567890123456789012345678901234567890123456789012345678901234'
user_public_key = IrohaCrypto.derive_public_key(user_private_key)

iroha = Iroha(ADMIN_ACCOUNT_ID)
net = IrohaGrpc(f'{IROHA_HOST_ADDR}:{IROHA_PORT}')

ASSET_ID = 'lemurcoin#test'


def trace(func):
   """ A decorator for tracing methods' begin/end execution points """

   def tracer(*args, **kwargs):
       name = func.__name__
       print(f'\tEntering "{name}"')
       result = func(*args, **kwargs)
       print(f'\tLeaving "{name}"\n')
       return result

   return tracer


@trace
def send_transaction_and_print_status(transaction):
   hex_hash = binascii.hexlify(IrohaCrypto.hash(transaction))
   print(f'Transaction "{get_command_from_tx(transaction)}" hash = {hex_hash}, '
         f'creator = {transaction.payload.reduced_payload.creator_account_id}')
   net.send_tx(transaction)
   for status in net.tx_status_stream(transaction):
       print('\t\t', status)


def get_command_from_tx(tx):
   commands_from_tx = []
   for command in tx.payload.reduced_payload.__getattribute__("commands"):
       listed_fields = command.ListFields()
       commands_from_tx.append(listed_fields[0][0].name)
   return commands_from_tx[0]


@trace
def create_account(user_account: str, user_public_key: str):
   account, domain = user_account.split('@')
   tx = iroha.transaction([
       iroha.command('CreateAccount', account_name=account, domain_id=domain,
                     public_key=user_public_key)
   ])
   IrohaCrypto.sign_transaction(tx, ADMIN_PRIVATE_KEY)
   send_transaction_and_print_status(tx)


@trace
def create_asset(asset_id: str):
   asset, domain = asset_id.split('#')
   tx = iroha.transaction([
       iroha.command('CreateAsset', asset_name=asset, domain_id=domain, precision=2)
   ])
   IrohaCrypto.sign_transaction(tx, ADMIN_PRIVATE_KEY)
   send_transaction_and_print_status(tx)


@trace
def add_coin_to_admin(asset_id: str, amount: int):
   tx = iroha.transaction([
       iroha.command('AddAssetQuantity', asset_id=asset_id, amount=str(amount))
   ])
   IrohaCrypto.sign_transaction(tx, ADMIN_PRIVATE_KEY)
   send_transaction_and_print_status(tx)


@trace
def perform_few_transactions(destination_account: str, how_many: int):
   for i in range(1, how_many + 1):
       tx = iroha.transaction([
           iroha.command('TransferAsset', src_account_id=ADMIN_ACCOUNT_ID,
                         dest_account_id=destination_account,
                         asset_id=ASSET_ID, description=f'tx {i}', amount=str(i))
       ])

       IrohaCrypto.sign_transaction(tx, ADMIN_PRIVATE_KEY)
       send_transaction_and_print_status(tx)


def print_paragraph(text: str):
   print(10 * '-', text, ':', 10 * '-')


@trace
def get_account_transactions(user_account: str, user_private_key: str,
                            asset_name: str, transactions_limit: int):
   iroha2 = Iroha(user_account)
   query = iroha2.query('GetAccountAssetTransactions', account_id=user_account,
                        asset_id=asset_name, page_size=transactions_limit)

   IrohaCrypto.sign_query(query, user_private_key)
   response = net.send_query(query)

   transactions = response.transactions_page_response.transactions
   for tx in transactions:
       transfer_tx = tx.payload.reduced_payload.commands[0].transfer_asset
       print(transfer_tx)


@trace
def get_account_transactions_different_order(user_account: str, user_private_key: str,
                                            asset_name: str, transactions_limit: int):
   pagination_meta = queries_pb2.TxPaginationMeta()

   iroha2 = Iroha(user_account)
   query = iroha2.query('GetAccountAssetTransactions', account_id=user_account,
                        asset_id=asset_name, page_size=transactions_limit,
                        ordering=pagination_meta)

   IrohaCrypto.sign_query(query, user_private_key)
   response = net.send_query(query)

   transactions = response.transactions_page_response.transactions
   for tx in transactions:
       transfer_tx = tx.payload.reduced_payload.commands[0].transfer_asset
       print(transfer_tx)


if __name__ == '__main__':
   print_paragraph('Preparing')
   create_account(user_account, user_public_key)
   create_asset(ASSET_ID)
   add_coin_to_admin(ASSET_ID, 10000)

   print_paragraph('Creating transactions')
   perform_few_transactions(user_account, 11)

   print_paragraph('Quering as usual')
   get_account_transactions(user_account, user_private_key, ASSET_ID, 5)

   print_paragraph('Quering as with different order - PROBLEM')
   get_account_transactions_different_order(user_account, user_private_key, ASSET_ID, 5)

The problematic is changing order of transactions, I'm trying its at the function:

@trace
def get_account_transactions_different_order(user_account: str, user_private_key: str,
                                             asset_name: str, transactions_limit: int):
    pagination_meta = queries_pb2.TxPaginationMeta()

    iroha2 = Iroha(user_account)
    query = iroha2.query('GetAccountAssetTransactions', account_id=user_account,
                         asset_id=asset_name, page_size=transactions_limit,
                         ordering=pagination_meta)

    IrohaCrypto.sign_query(query, user_private_key)
    response = net.send_query(query)

    transactions = response.transactions_page_response.transactions
    for tx in transactions:
        transfer_tx = tx.payload.reduced_payload.commands[0].transfer_asset
        print(transfer_tx)

The logs of the function is:

Traceback (most recent call last):
  File "/home/agh/Pulpit/blockchain/Watra-Ledger/src/ordering.py", line 151, in <module>
    get_account_transactions_different_order(user_account, user_private_key, ASSET_ID, 5)
  File "/home/agh/Pulpit/blockchain/Watra-Ledger/src/ordering.py", line 31, in tracer
    result = func(*args, **kwargs)
  File "/home/agh/Pulpit/blockchain/Watra-Ledger/src/ordering.py", line 125, in get_account_transactions_different_order
    query = iroha2.query('GetAccountAssetTransactions', account_id=user_account,
  File "/home/agh/Pulpit/blockchain/Watra-Ledger/src/venv/lib/python3.9/site-packages/iroha/iroha.py", line 287, in query
    setattr(internal_query, key, value)
AttributeError: 'GetAccountAssetTransactions' object has no attribute 'ordering'

The same problem is with another combinations from: https://iroha.readthedocs.io/en/main/develop/api/queries.html#result-pagination :

AttributeError: 'GetAccountAssetTransactions' object has no attribute 'field'
...
AttributeError: 'GetAccountAssetTransactions' object has no attribute 'direction'
...
AttributeError: 'GetAccountAssetTransactions' object has no attribute 'sequence'

but the field pagination_meta is not allowed to assign:

AttributeError: Assignment not allowed to field "pagination_meta" in protocol message object.

So the question is: how to change order of transactions fetched with query GetAccountAssetTransactions?

@baziorek
Copy link

Config file and genesis.block, and keys of admin and nodes are the same as here: https://github.com/hyperledger/iroha/tree/main/example the only difference in config file is in database connection string and blockchain path.
I'm using iroha 1.2.1.

@turbopape
Copy link

Hello, I added ordering_sequence as query params in iroha.py. I made a PR: #73 that should fix the problem.

LiraLemur pushed a commit that referenced this issue Jul 13, 2021
* Add ordering sequence to params

Makes possible to add ordering sequence to Query. Fixes #72

Signed-off-by: Rafik Naccache <[email protected]>

* Add :param ordering_sequence:

Signed-off-by: Rafik Naccache <[email protected]>

* Fix wrong example for :param ordering_sequence:

Signed-off-by: Rafik Naccache <[email protected]>

* fix wrong ordering message construction in query

After more documentation I got to the way to construct to ordering object

Signed-off-by: Rafik Naccache <[email protected]>

* remove redundant OR clause to manage ordering_sequence

Signed-off-by: Rafik Naccache <[email protected]>
baziorek added a commit that referenced this issue Feb 17, 2022
* Add ordering sequence to params in query (#73)

* Add ordering sequence to params

Makes possible to add ordering sequence to Query. Fixes #72

Signed-off-by: Rafik Naccache <[email protected]>

* Add :param ordering_sequence:

Signed-off-by: Rafik Naccache <[email protected]>

* Fix wrong example for :param ordering_sequence:

Signed-off-by: Rafik Naccache <[email protected]>

* fix wrong ordering message construction in query

After more documentation I got to the way to construct to ordering object

Signed-off-by: Rafik Naccache <[email protected]>

* remove redundant OR clause to manage ordering_sequence

Signed-off-by: Rafik Naccache <[email protected]>

* Correction after review

Signed-off-by: Grzegorz Bazior <[email protected]>

* Migrated from main branch also protobuf files

Signed-off-by: G.Bazior <[email protected]>

* Updated proto files with script download-schema.py

Signed-off-by: G.Bazior <[email protected]>

* Updated generated from protobuf python files (script compile-proto.py)

Signed-off-by: G.Bazior <[email protected]>

Co-authored-by: Rafik NACCACHE <[email protected]>
baziorek pushed a commit to baziorek/iroha-python that referenced this issue Feb 28, 2022
* Add ordering sequence to params

Makes possible to add ordering sequence to Query. Fixes hyperledger-iroha#72

Signed-off-by: Rafik Naccache <[email protected]>

* Add :param ordering_sequence:

Signed-off-by: Rafik Naccache <[email protected]>

* Fix wrong example for :param ordering_sequence:

Signed-off-by: Rafik Naccache <[email protected]>

* fix wrong ordering message construction in query

After more documentation I got to the way to construct to ordering object

Signed-off-by: Rafik Naccache <[email protected]>

* remove redundant OR clause to manage ordering_sequence

Signed-off-by: Rafik Naccache <[email protected]>
baziorek pushed a commit to baziorek/iroha-python that referenced this issue Feb 28, 2022
* Add ordering sequence to params

Makes possible to add ordering sequence to Query. Fixes hyperledger-iroha#72

Signed-off-by: Rafik Naccache <[email protected]>

* Add :param ordering_sequence:

Signed-off-by: Rafik Naccache <[email protected]>

* Fix wrong example for :param ordering_sequence:

Signed-off-by: Rafik Naccache <[email protected]>

* fix wrong ordering message construction in query

After more documentation I got to the way to construct to ordering object

Signed-off-by: Rafik Naccache <[email protected]>

* remove redundant OR clause to manage ordering_sequence

Signed-off-by: Rafik Naccache <[email protected]>
Signed-off-by: G.Bazior <[email protected]>
baziorek added a commit that referenced this issue Mar 23, 2022
* Extending TxPaginationMeta in queries (#77)

* quries extended
* example added
* docs comment added
* schema changed
* formatting fixed
* ordering added
* edge case fix, now 0 passed as height or timestamp is passing
* PaginationMeta in GetPendingTransactions
* examples/query_transactions.py

Signed-off-by: Piotr Pawlowski <[email protected]>
Signed-off-by: G.Bazior <[email protected]>

* Update proto files and generated python files to support Iroha 1.4 (#96)

* Update proto files with script `download-schema.py`

Signed-off-by: Grzegorz Bazior <[email protected]>

* Generated python files from protobuf files with script `compile-proto.py`

Signed-off-by: G.Bazior <[email protected]>

Co-authored-by: Grzegorz Bazior <[email protected]>
Signed-off-by: G.Bazior <[email protected]>

* Corrected merge - rerun scripts: download-schema.py and compile-proto.py

Signed-off-by: G.Bazior <[email protected]>

* Tab -> spaces

Signed-off-by: G.Bazior <[email protected]>

* Add ability to provide custom TLS cert (#63)

Signed-off-by: Stepan Lavrentev <[email protected]>
Signed-off-by: G.Bazior <[email protected]>

* Add ordering sequence to params in query (#73)

* Add ordering sequence to params

Makes possible to add ordering sequence to Query. Fixes #72

Signed-off-by: Rafik Naccache <[email protected]>

* Add :param ordering_sequence:

Signed-off-by: Rafik Naccache <[email protected]>

* Fix wrong example for :param ordering_sequence:

Signed-off-by: Rafik Naccache <[email protected]>

* fix wrong ordering message construction in query

After more documentation I got to the way to construct to ordering object

Signed-off-by: Rafik Naccache <[email protected]>

* remove redundant OR clause to manage ordering_sequence

Signed-off-by: Rafik Naccache <[email protected]>
Signed-off-by: G.Bazior <[email protected]>

* Extending TxPaginationMeta in queries (#77)

* quries extended
* example added
* docs comment added
* schema changed
* formatting fixed
* ordering added
* edge case fix, now 0 passed as height or timestamp is passing
* PaginationMeta in GetPendingTransactions
* examples/query_transactions.py

Signed-off-by: Piotr Pawlowski <[email protected]>
Signed-off-by: G.Bazior <[email protected]>

* Added missing changes from Pawlak00@b6d7f42 corrected in #77

Signed-off-by: G.Bazior <[email protected]>

Co-authored-by: Piotr Pawłowski <[email protected]>
Co-authored-by: Grzegorz Bazior <[email protected]>
Co-authored-by: Stepan Lavrentev <[email protected]>
Co-authored-by: Rafik NACCACHE <[email protected]>
baziorek added a commit that referenced this issue Aug 22, 2022
* Extending TxPaginationMeta in queries (#77)

* quries extended
* example added
* docs comment added
* schema changed
* formatting fixed
* ordering added
* edge case fix, now 0 passed as height or timestamp is passing
* PaginationMeta in GetPendingTransactions
* examples/query_transactions.py

Signed-off-by: Piotr Pawlowski <[email protected]>
Signed-off-by: G.Bazior <[email protected]>

* Update proto files and generated python files to support Iroha 1.4 (#96)

* Update proto files with script `download-schema.py`

Signed-off-by: Grzegorz Bazior <[email protected]>

* Generated python files from protobuf files with script `compile-proto.py`

Signed-off-by: G.Bazior <[email protected]>

Co-authored-by: Grzegorz Bazior <[email protected]>
Signed-off-by: G.Bazior <[email protected]>

* Corrected merge - rerun scripts: download-schema.py and compile-proto.py

Signed-off-by: G.Bazior <[email protected]>

* Tab -> spaces

Signed-off-by: G.Bazior <[email protected]>

* Add ability to provide custom TLS cert (#63)

Signed-off-by: Stepan Lavrentev <[email protected]>
Signed-off-by: G.Bazior <[email protected]>

* Add ordering sequence to params in query (#73)

* Add ordering sequence to params

Makes possible to add ordering sequence to Query. Fixes #72

Signed-off-by: Rafik Naccache <[email protected]>

* Add :param ordering_sequence:

Signed-off-by: Rafik Naccache <[email protected]>

* Fix wrong example for :param ordering_sequence:

Signed-off-by: Rafik Naccache <[email protected]>

* fix wrong ordering message construction in query

After more documentation I got to the way to construct to ordering object

Signed-off-by: Rafik Naccache <[email protected]>

* remove redundant OR clause to manage ordering_sequence

Signed-off-by: Rafik Naccache <[email protected]>
Signed-off-by: G.Bazior <[email protected]>

* Extending TxPaginationMeta in queries (#77)

* quries extended
* example added
* docs comment added
* schema changed
* formatting fixed
* ordering added
* edge case fix, now 0 passed as height or timestamp is passing
* PaginationMeta in GetPendingTransactions
* examples/query_transactions.py

Signed-off-by: Piotr Pawlowski <[email protected]>
Signed-off-by: G.Bazior <[email protected]>

* Added missing changes from Pawlak00@b6d7f42 corrected in #77

Signed-off-by: G.Bazior <[email protected]>

* Examples refactor + added example of MST developed by Leo (#103)

* Refactoring of example tx-example.py

Signed-off-by: G.Bazior <[email protected]>

* Added sample of MST transactions. The code was originally developed by Leo: https://github.com/iptelephony/jubilant-engine but he agreed to add his code to examples

Signed-off-by: G.Bazior <[email protected]>

* Generated protobuf files with protobuf 3.12.4 on Ubuntu 21.04 (#104)

Signed-off-by: G.Bazior <[email protected]>

* Serious refactoring + improvements + adding new examples for Iroha 1 (#107)

* Update README
* Refactoring in tls-example.py
* A little refactoring of batch-example.py
* Small refactoring of blocks-query.py
* Small refactoring of infinite-blocks-stream.py
* Added sample how to use ordering of result transactions
* Refacroting in query_transactions.py
* Renamed file with convention used in other files
* Added examples in comments
* Some refactoring in examples + added docstring in each file
* chmod +x
* Updated iroha version from 1.4 -> 1.5

Signed-off-by: G.Bazior <[email protected]>

* Add autorestart to iroha

Signed-off-by: Alexey Rodionov <[email protected]>

* Cherry-pick fix

Signed-off-by: G.Bazior <[email protected]>

* Generated python files from protobuf on Ubuntu:20.04

Signed-off-by: G.Bazior <[email protected]>

* Dependencies fix

Signed-off-by: G.Bazior <[email protected]>

* Added space in logging after review

Signed-off-by: G.Bazior <[email protected]>

* Made import in code more readable after review

Signed-off-by: G.Bazior <[email protected]>

Signed-off-by: Piotr Pawlowski <[email protected]>
Signed-off-by: G.Bazior <[email protected]>
Signed-off-by: Stepan Lavrentev <[email protected]>
Signed-off-by: Rafik Naccache <[email protected]>
Signed-off-by: Alexey Rodionov <[email protected]>
Co-authored-by: Piotr Pawłowski <[email protected]>
Co-authored-by: Grzegorz Bazior <[email protected]>
Co-authored-by: Stepan Lavrentev <[email protected]>
Co-authored-by: Rafik NACCACHE <[email protected]>
Co-authored-by: Alexey Rodionov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants