Skip to content

Commit 0b2d615

Browse files
baziorekPawlak00Grzegorz BaziorstepanLavturbopape
authored
Cherry-pick changes from develop -> main (#122)
* 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]>
1 parent 19bd94f commit 0b2d615

23 files changed

+6315
-1357
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
This is a source repository for HL Iroha Python library.
55

6-
Currently, latest HL Iroha rc5 release (`hyperledger/iroha:latest` Docker image) is supported.
6+
Currently, latest [HL Iroha release 1.5](https://github.com/hyperledger/iroha/releases) is supported. It can be ussed with one of official docker images:
7+
- `hyperledger/iroha:latest`
8+
- `hyperledger/iroha-burrow:latest` with Hyperledger-Burrow support (smart contracts).
79

8-
The library works in Python 3 environment (Python 2 is not supported now).
10+
The library works in Python 3 environment (Python 2 is not supported).
911

1012
### Installation
1113

@@ -45,3 +47,5 @@ Please explore [examples](examples) directory for more usage examples.
4547
All the library methods have docstrings in its source [iroha.py](iroha/iroha.py).
4648

4749
*The links above are broken outside the [hyperledger/iroha-python](https://github.com/hyperledger/iroha-python) GitHub repository.*
50+
51+
If you are interested in different HL Iroha client libraries you can check our [Wiki](https://wiki.hyperledger.org/display/iroha/Hyperledger+Iroha).

dev-requirements.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PyNaCl==1.4.0
2-
protobuf==3.15.0
3-
grpcio_tools==1.21.1
4-
six==1.12.0
1+
PyNaCl==1.5.0
2+
protobuf==3.20.1
3+
grpcio_tools==1.47.0
4+
six==1.16.0

examples/add-syncing-peer.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
"""
2+
This example demonstrates how to add new peer, when new peer is syncing node.
3+
Details about how to add new peer to the network are here:
4+
https://iroha.readthedocs.io/en/develop/maintenance/add_peer.html
5+
"""
6+
7+
import os
8+
import sys
9+
from functools import wraps
10+
from iroha import primitive_pb2, IrohaCrypto, binascii, IrohaGrpc, Iroha
11+
from grpc import RpcError, StatusCode
12+
import inspect # inspect.stack(0)
13+
from utilities.errorCodes2Hr import get_proper_functions_for_commands
14+
15+
16+
if sys.version_info[0] < 3:
17+
raise Exception('Python 3 or a more recent version is required.')
18+
19+
20+
ADMIN_ACCOUNT_ID = os.getenv('ADMIN_ACCOUNT_ID', 'admin@test')
21+
ADMIN_PRIVATE_KEY = os.getenv(
22+
'ADMIN_PRIVATE_KEY', 'f101537e319568c765b2cc89698325604991dca57b9716b58016b253506cab70')
23+
24+
iroha_admin = Iroha(ADMIN_ACCOUNT_ID)
25+
26+
27+
IROHA_HOST_ADDR = os.getenv('IROHA_HOST_ADDR', 'localhost')
28+
IROHA_PORT = os.getenv('IROHA_PORT', '50051')
29+
30+
NEW_PEER_PUBLIC_KEY = os.getenv(
31+
'NEW_PEER_PUBLIC_KEY', 'edfa0f05d019c0a0dd6ca491e0c2e78d1ef4148ffa10ffc72f48a0fd6af08e5b')
32+
NEW_PEER_ADDRESS = os.getenv('NEW_PEER_ADDRESS', 'localhost')
33+
NEW_PEER_IROHA_PORT = os.getenv('NEW_PEER_IROHA_PORT', '50050')
34+
NEW_PEER_FULL_ADDRESS = f'{NEW_PEER_ADDRESS}:{NEW_PEER_IROHA_PORT}'
35+
36+
net = IrohaGrpc(f'{IROHA_HOST_ADDR}:{IROHA_PORT}')
37+
net_new_peer = IrohaGrpc(NEW_PEER_FULL_ADDRESS)
38+
39+
40+
def trace(func):
41+
"""
42+
A decorator for tracing methods' begin/end execution points
43+
"""
44+
@wraps(func)
45+
def tracer(*args, **kwargs):
46+
name = func.__name__
47+
stack_size = int(len(inspect.stack(0)) / 2) # @wraps(func) is also increasing the size
48+
indent = stack_size*'\t'
49+
print(f'{indent} > Entering "{name}": args: {args}')
50+
result = func(*args, **kwargs)
51+
print(f'{indent} < Leaving "{name}"')
52+
return result
53+
54+
return tracer
55+
56+
57+
@trace
58+
def add_peers(iroha_connection, peer_address: str, peer_public_key: str):
59+
peer = primitive_pb2.Peer(address=peer_address, peer_key=peer_public_key, syncing_peer=True)
60+
tx = iroha_connection.transaction([
61+
iroha_connection.command('AddPeer', peer=peer)
62+
])
63+
IrohaCrypto.sign_transaction(tx, ADMIN_PRIVATE_KEY)
64+
send_transaction_and_print_status(tx)
65+
66+
67+
@trace
68+
def send_transaction_and_print_status(transaction):
69+
hex_hash = binascii.hexlify(IrohaCrypto.hash(transaction))
70+
creator_id = transaction.payload.reduced_payload.creator_account_id
71+
commands = get_commands_from_tx(transaction)
72+
print(f'Transaction "{commands}",'
73+
f' hash = {hex_hash}, creator = {creator_id}')
74+
net.send_tx(transaction)
75+
for i, status in enumerate(net.tx_status_stream(transaction)):
76+
status_name, status_code, error_code = status
77+
print(f"{i}: status_name={status_name}, status_code={status_code}, "
78+
f"error_code={error_code}")
79+
if status_name in ('STATEFUL_VALIDATION_FAILED', 'STATELESS_VALIDATION_FAILED', 'REJECTED'):
80+
error_code_hr = get_proper_functions_for_commands(commands)(error_code)
81+
raise RuntimeError(f"{status_name} failed on tx: "
82+
f"{transaction} due to reason {error_code}: "
83+
f"{error_code_hr}")
84+
85+
86+
def get_commands_from_tx(transaction):
87+
commands_from_tx = []
88+
for command in transaction.payload.reduced_payload.__getattribute__("commands"):
89+
listed_fields = command.ListFields()
90+
commands_from_tx.append(listed_fields[0][0].name)
91+
return commands_from_tx
92+
93+
94+
@trace
95+
def print_peers(text: str, net_to_query: IrohaGrpc):
96+
print('-------', text, '-------')
97+
query = iroha_admin.query('GetPeers')
98+
IrohaCrypto.sign_query(query, ADMIN_PRIVATE_KEY)
99+
response = net_to_query.send_query(query)
100+
peers_response = response.peers_response.peers
101+
for i, p in enumerate(peers_response):
102+
print(i, p)
103+
104+
105+
if __name__ == '__main__':
106+
try:
107+
print(f'!!!New peer in address {NEW_PEER_FULL_ADDRESS} should work now! '
108+
f'It not the script {sys.argv[0]} would fail!!!')
109+
print_peers('Peers before adding new one', net)
110+
add_peers(iroha_connection=iroha_admin, peer_address=NEW_PEER_FULL_ADDRESS,
111+
peer_public_key=NEW_PEER_PUBLIC_KEY)
112+
print_peers('Peers after adding the new one - query to old peer', net)
113+
print_peers('Peers after adding the new one - query to new peer', net_new_peer)
114+
except RpcError as rpc_error:
115+
if rpc_error.code() == StatusCode.UNAVAILABLE:
116+
print(f'[E] Iroha is not running in address: '
117+
f'{IROHA_HOST_ADDR}:{IROHA_PORT}!')
118+
else:
119+
print(e)
120+
except RuntimeError as e:
121+
print(e)

0 commit comments

Comments
 (0)