Python Light客户端Exonum
exonum-python-client的Python项目详细描述
Exonum Python轻型客户端
Exonum Framework的Python客户端。在
概述
Exonum Light Client是一个用于处理Exonum区块链的Python库 从客户端。它可以很容易地集成到现有的 应用程序。此外,exnum Light客户端还提供了对常用实用程序的访问 工具箱中包含一些有用的函数,如散列,加密, 序列化等
能力
通过使用客户机,您可以执行以下操作:
- 向节点提交事务处理
- 接收交易信息
- 接收区块链区块信息
- 接收节点系统信息
- 接收有关节点状态的信息
兼容性
下表显示了版本兼容性:
Light Client | Exonum |
---|---|
0.1 | 0.9.* |
0.2 | 0.10.* |
0.3.1 | 0.12.* |
1.0.x | 1.0.* |
master | ^{ |
系统依赖项
- Python 3.5或更高版本。在
- Python3(pip3)的包安装程序
示例
下面的示例演示如何创建Exonum客户机的实例
它将能够与Exonum节点一起使用
加密货币高级服务安装在http://localhost:8080
地址:
安装Python Light Client
首先,我们需要安装客户端库:
git clone git@github.com:exonum/exonum-python-client.git
pip3 install -e exonum-python-client --no-binary=protobuf
Exonum客户端初始化
^{pr2}$编译Proto文件
要将proto文件编译成Python模拟程序,我们需要一个protobuf加载程序:
withclient.protobuf_loader()asloader:# Your code goes here.
由于加载程序在初始化时获取资源,建议通过上下文管理器创建。 否则,应手动初始化和取消初始化客户端:
loader=client.protobuf_loader()loader.initialize()# ... Some usageloader.deinitialize()
然后我们需要运行以下代码:
loader.load_main_proto_files()# Load and compile main proto files, such as `runtime.proto`, `consensus.proto`, etc.loader.load_service_proto_files(runtime_id=0,service_name='exonum-supervisor:1.0.0')# Same for specific service.
- runtime_id=0在这里意味着,服务在Rust运行时工作。在
创建事务消息
以下示例显示如何创建事务消息:
alice_keys=KeyPair.generate()cryptocurrency_artifact_name="exonum-cryptocurrency-advanced"cryptocurrency_artifact_version="1.0.0"loader.load_service_proto_files(runtime_id=0,artifact_name=cryptocurrency_artifact_name,artifact_version=cryptocurrency_artifact_version)cryptocurrency_module=ModuleManager.import_service_module(cryptocurrency_artifact_name,cryptocurrency_artifact_version,"service")cryptocurrency_message_generator=MessageGenerator(instance_id=1024,artifact_name=cryptocurrency_artifact_name,artifact_version=cryptocurrency_artifact_version)create_wallet_alice=cryptocurrency_module.CreateWallet()create_wallet_alice.name='Alice'create_wallet_alice_tx=cryptocurrency_message_generator.create_message(create_wallet_alice)create_wallet_alice_tx.sign(alice_keys)
- 1024-服务实例ID
- alice_keys-ed25519公钥签名的公钥和私钥 系统。在
在调用sign方法之后,我们得到一个签名的事务。 此事务已准备好发送到Exonum节点。在
向Exonum节点发送事务
成功发送消息后,我们将得到一个响应,该响应将 包含事务的哈希:
response=client.public_api.send_transaction(create_wallet_alice_tx)
{"tx_hash":"3541201bb7f367b802d089d8765cc7de3b7dfc253b12330b8974268572c54c01"}
订阅事件
如果要订阅事件(订阅类型:“事务”或“块”),请使用以下代码:
withclient.create_subscriber(subscription_type="blocks")assubscriber:subscriber.wait_for_new_event()subscriber.wait_for_new_event()
上下文管理器将自动创建连接,并在使用后断开连接。 也可以手动执行相同操作:
subscriber=client.create_subscriber(subscription_type="blocks")subscriber.connect()# ... Your codesubscriber.stop()
请记住,如果您忘记停止订阅服务器,您可能会发现HTTP 尝试使用Exonum API时出错。在
获取可用服务的数据
client.public_api.available_services().json()
代码将显示可用于启动的工件列表和 工作服务:
{"artifacts":[{"runtime_id":0,"name":"exonum-supervisor","version":"1.0.0"},{"runtime_id":0,"name":"exonum-explorer-service","version":"1.0.0"}],"services":[{"spec":{"id":2,"name":"explorer","artifact":{"runtime_id":0,"name":"exonum-explorer-service","version":"1.0.0"}},"status":"Active","pending_status":null},{"spec":{"id":0,"name":"supervisor","artifact":{"runtime_id":0,"name":"exonum-supervisor","version":"1.0.0"}},"status":"Active","pending_status":null}]}
更多示例
若要查看更多示例并了解如何使用证明,请转到here。在
您还可以在examples部分找到示例脚本。在
测试
要运行测试,请使用以下命令:
python3 -m unittest
贡献
您可以在Contribution Guide中看到开发人员的注释 第页。在
已知问题
如果在使用中发现以下错误:
TypeError: Couldn't build proto file into descriptor pool!
这是由于Protobuf双星轮的问题。唯一的办法就是 安装纯Python实现。在
pip uninstall protobuf
pip install --no-binary=protobuf protobuf
许可证
Apache2.0-有关更多信息,请参见LICENSE。在
- 项目
标签: