Skip to content

hyperledger/iroha-python

Iroha python

Python library for Hyperledger Iroha 2.

Install

You should install rust (cargo) and python.

pip install maturin
maturin build

After the build, you'll a name of the generated package "wheel" in the log, similar to this one:

📦 Built wheel for CPython 3.10 to …/iroha-python/target/wheels/iroha_python-0.1.0-cp310-cp310-linux_x86_64.whl

Remember the version, change the directory and install the package:

cd target/wheels
pip install iroha_python-VERSION-linux_x86_64.whl
cd ~

After an installation, do a small test to check the installation was successful.

Normally, this test will display the library's contents:

python3 -c "import iroha2; print(dir(iroha2))"
['ClassPath', 'Client', 'Dict', 'Enum', 'KeyPair', 'List', 'Struct', 'Tuple', '_Client', '_Instruction', '_Query', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'data_model', 'get_class', 'import_module', 'iroha2', 'query', 'sys', 'to_rust']

If there was no error, you should be able to use iroha2-python library.

You may also see an error that ends like this:

ModuleNotFoundError: No module named 'iroha2.iroha2'

If it appears, your current directory may be the same as the directory with iroha2 and Python may be attempting to import it instead of the module. Switch to another directory and repeat the test.

Architecture

Stack

Architecturally python library uses rust client library. It is done via wrapping classes using rust library pyo3.

The data model of iroha itself is expected to be just classes without some special methods (without sidechannels, like doing http request). Communication channel for rust python methods using rust data model is achieved using pythonize rust library. It allows to represent almost all rust structures using python objects (via serde serialization).

Project layout

Python library

Python library right now is in iroha_python/iroha2. This directory has some directories:

  • iroha2 -- iroha2 library itself. Contains client and key generation at root
  • iroha2.crypto -- iroha crypto bindings
  • iroha2.data_model -- data model with all instructions, queries and expressions
  • iroha2.sys -- raw bindings to rust structures wrapped around python objects

Rust code

Generating python sys sources

There is a need to regenerate sys sources and update client library after major rust updates in data model.

cargo run -p generate -- iroha2/sys
yapf --style pep8 -r -i iroha2 example