一个用于hyperledger结构的非官方python sdk
snakeskin-fabric的Python项目详细描述
蛇皮
Python + Fabric == snakeskin
这个库是官方hyperledger fabric(hlf)python sdk的重新实现,它提供了更简单的与hlf的python接口,同时还允许对与网络的通信进行细粒度的控制。
具体地说,我们正在建设这个图书馆,我们的目标如下:
- 清晰配置-提供清晰记录、易于使用且无冗余的配置
- 简单-提供简单的高级接口,用于较不复杂的网络
- 控制-为与网络的交互提供细粒度的通信接口,这些接口与更高级别的通信接口一样有清晰的文档记录
- 无状态-在可行的情况下,避免管理状态转换的复杂性,允许更可靠的开发经验
- 优雅地失败-引发语义、层次和有意义的错误消息
- pythonic-使用python命名约定允许更干净地集成到python应用程序中
使用snakeskin,您可以管理事务生命周期、请求重试和具有高度专用性和控制的复杂网络网关,同时在不需要或不需要控制时仍可以利用更高级别的操作。
安装
$[sudo] pip install snakeskin-fabric
配置
SnkestChin可以使用静态文件配置,也可以在Python中动态配置,并且相对灵活,因为配置的需求很大程度上取决于用例。
有关配置结构示例,请参见examples/config-files/example-blockchain-config.yaml
中的配置示例。请注意,支持以下文件格式:
- 山药-(
.yaml
,.yml
) - json-(
.json
)
要将配置加载到框架中,只需执行以下操作:
fromsnakeskin.configimportBlockchainConfig# Or load from a static fileblockchain=BlockchainConfig.from_file('/path/to/config/file.yaml')# Or load from a dictionaryblockchain=BlockchainConfig.from_dict({# ...})
与区块链互动
要针对区块链运行交易,最容易使用网关,
它预先配置了必需的对等方、订购方、通道和链码
规范。参见示例配置的gateways
部分
配置选项。配置后,您可以从
区块链配置:
gateway=blockchain.get_gateway('my-gateway')# or build your own programatically:fromsnakeskin.models.gatewayimportGatewaygateway=Gateway(# ...)
交易
一旦配置好,对链码执行事务就相对简单了。网关公开与hlfpeer
cli命令相同的高级抽象-invoke
和query
。
# query the chaincode to retrieve datatransaction=awaitgateway.query(fcn='doSomething',args=['arg1','arg2'])# invoke the chaincode to persist data, waiting for the chaincode to# successfully committransaction=awaitgateway.invoke(fcn='doSomething',args=['arg1','arg2'],timeout=50)transaction.response_payload#=> b'<chaincode response>'
但是,如果希望对事务流进行更多控制,可以使用transact
方法和链操作(有关可用选项,请参见snakeskin.models.GatewayTXBuilder
):
# A step-by-step transaction flowtransaction=await(gateway.transact(fcn='doSomething',args=['arg1','arg2'])# Sends the transaction to the peers for endorsement.propose()# Optionally sends the transaction to the orderers for committing.submit()# Optionally wait (up to 50 seconds) for the transaction to be successfully# committed to the peer.wait_for_commit(timeout=50))transaction.response_payload#=> b'<chaincode response>'
网络管理
大多数管理操作也可以通过网关进行:
# Creates the channel, via the ordererawaitgateway.create_channel(tx_file_path='/path/to/channel.tx')# Joins the gateway's endorsing peers to the channelawaitgateway.join_channel()# Query instantiated chaincode for the channelresp=awaitgateway.query_instantiated_chaincodes()resp.chaincodes[0].name# => 'my-chaincode'# Installs the chaincodeawaitgateway.install_chaincode()# Instantiates the chaincodeawaitgateway.instantiate_chaincode(timeout=60)# Upgrades the chaincodeawaitgateway.instantiate_chaincode(timeout=60,upgrade=True)
请注意,并非每个管理操作都需要网关上的所有资产。create_channel
例如,不需要在网关上定义任何背书对等体。
独立操作
网关定义不需要对区块链执行任何操作,它只是配置资产分组的有用工具。要在没有网关的情况下执行操作,请从snakeskin.operations
模块导入它们。例如:
fromsnakeskin.operationsimportcreate_channelawaitcreate_channel(requestor=config.get_user('my-user'),orderers=[config.get_orderer('my-orderer')],channel=config.get_channel('my-channel'),tx_file_path='/path/to/channel.tx')
类似地,细粒度事务管理可以从snakeskin.transact
:
fromsnakeskin.transactimport(generate_cc_tx,propose_tx,commit_tx,raise_tx_proposal_error)requestor=config.get_user('my-user')# Generate a new chaincode transaction against mycc on my-channelgenerated_tx=awaitgenerate_cc_tx(requestor=requestor,cc_name=config.get_chaincode('mycc').name,channel=config.get_channel('my-channel'),fcn='doSomething',args=['arg1','arg2'])# Send the proposal to my-peerendorsed_tx=awaitpropose_tx(peers=[config.get_peer('my-peer')],generated_tx=generated_tx)# Raise errors on endorsement failureraise_tx_proposal_error(endorsed_tx,'Endorsements failed for ')# Commit the transaction via the orderercommitted_transaction=awaitcommit_tx(requestor=requestor,orderers=[config.get_orderer('my-orderer')],endorsed_tx=endorsed_tx,)
事件
要从对等节点或订购方节点流式传输块,请使用snakeskin.events
模块,如下所示:
fromsnakeskin.eventsimportPeerEventsevents=PeerEvents(requestor=requestor,channel=config.get('my-channel'),peer=config.get('my-peer'))stream=events.stream_blocks(start=0,# start at block 0stop=10# end at block 10 (if omitted, will stream forever))asyncforraw_blockinstream:block=raw_block.decode()fortransactioninblock.transactions:transaction.tx_id# => '1234567'
若要从对等方流filtered blocks,请使用snakeskin.events.PeerFilteredEvents
,若要从排序程序流块,请使用snakeskin.events.OrdererEvents
。所有这些类都实现了类似的接口。
贡献
即将推出
许可证
此软件使用apache许可证2.0版软件许可证。