博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
fabric gosdk部分功能接口
阅读量:7197 次
发布时间:2019-06-29

本文共 14335 字,大约阅读时间需要 47 分钟。

fabric gosdk

pkg-client

1、channel包

导入:import ""

channel包能够获取fabric网络中的channel。一个channel client实例提供一个handler与指定的channel中的peer交互。channel client能够查询ChainCode,执行ChainCode,在指定的channel上注册或者注销ChainCode events。

一个需要和多个channel交互的应用需要为每一个channel分别创建channel client实例。

基本流程如下:

  1. 准备channel client context
  2. 创建channel client
  3. 执行ChainCode
  4. 查询ChainCode

type Client

  • func New

    func New(channelProvider context.ChannelProvider, opts ...ClientOption) (*Client, error)

    返回一个client实例

  • func (*Client)Execute

    func (cc *Client) Execute(request Request, options ...RequestOption) (Response, error)

    用request和options执行准备和执行交易

  • func (*Client)Query

    func (cc *Client) Query(request Request, options ...RequestOption) (Response, error)

    用request和options查询ChainCode

  • func (*Client)RegisterChaincodeEvent

    func (cc *Client) RegisterChaincodeEvent(chainCodeID string, eventFilter string) (fab.Registration, <-chan *fab.CCEvent, error)

    注册chaincode event,unregister必须在注册了不再需要的时候调用

  • func (*Client)UnregisterChaincodeEvent

    func (cc *Client) UnregisterChaincodeEvent(registration fab.Registration)

    移除registration,关掉event channel

type ClientOption

type Request

type RequestOption

  • func WithBeforeRetry

    func WithBeforeRetry(beforeRetry retry.BeforeRetryHandler) ReqiestOption

    指定在retry尝试之前调用的方法

  • func WithChaincodeFile

    func WithChaincodeFilter(ccFilter invoke.CCFilter) RequestOption

    添加chaincode filter,用来找出其他的endorsers

  • func WithParentContext

  • func With Retry

    ...

2.invoke包

导入:import ""

invoke包提供了执行chaincode调用的handler

type CCFilter

type CCFilter func(ccID string) bool复制代码

当计算endorser时,如果给定的chaincode会被执行,CCFilter返回true

  • func NewChainedCCFilter

    func NewChainedCCFilter(filters ...CCFilter) CCFilter

    返回一个chaincode filter,将多个filter链接在一起。 如果链中至少一个filter返回false,则返回False。

type ClientContext

ClientContext包含了用于handler执行的context参数

type CommitTxHandler

CommitTxHandler用来committing交易

  • func NewCommitHandler

    func NewCommitHandler(next ...Handler) *CommitTxHandler

    返回一个提交交易提议响应的handler

  • func (*CommitTxHandler)Handler

    func (c *CommitTxHandler) Handle(requestContext *RequestContext, clientContext *ClientContext)

    处理提交tx

type EndorsementHandler

EndorsementHandler用来处理背书交易

  • func NewEndorsementHandler

    func NewEndorsementHandler(next ...Handler) *EndorsementHandler

    返回一个背书交易提议的handler

  • func NewEndorsementHandlerWithOpts

    func NewEndorsementHandlerWithOpts(next Handler, provider TxnHeaderOptsProvider) *EndorsementHandler

    返回一个背书交易提议的handler

  • func (*EndorsementHandler)Handle

    func (e *EndorsementHandler) Handle(requestContext *RequestContext, clientContext *ClientContext)

    交易背书

    ...

3.event包

导入:import ""

event包能够获取fabric网络上的channel event。event client收到event,比如block,filtered block,chaincode,和事件的交易状态。

基本流程如下:

  1. 准备channel client Context
  2. 创建event client
  3. 注册event
  4. 运行event(或者超时)
  5. 注销

type Client

Client能够获取fabric网络中的channel event。

  • func New

    func New(channelProvider context.ChannelProvider, opts ...ClientOption) (*Client, error)

    返回一个Client实例。Client能够收到event。

  • func (*Client)RegisterBlockEvent

    func (c *Client) RegisterBlockEvent(filter ...fab.BlockFilter) (fab.Registration, <-chan *fab.BlockEvent, error)

    注册block event。如果调用者没有注册block event的权限会返回一个error。unregister必须在注册了不再使用的时候调用。

  • func (*Client)RegisterChaincodeEvent

    func (c *Client) RegisterChaincodeEvent(ccID, eventFilter string) (fab.Registration, <-chan *fab.CCEvent, error)

    注册chaincode event。

  • func (*Client)RegisterFilteredBlockEvent

    func (c *Client) RegisterFilteredBlockEvent() (fab.Registration, <-chan *fab.FilteredBlockEvent, error)

    注册filtered block event。

  • func (*Client)RegisterTxStatusEvent

    func (c *Client) RegisterTxStatusEvent(txID string) (fab.Registration, <-chan *fab.TxStatusEvent, error)

    注册交易状态事件

  • func (*Client)Unregister

    func (c *Client) Unregister(reg fab.Registration)

    移除registration,关掉event channel

    ...

4.ledger包

导入:import ""

Ledger包可以在fabric网络中查询指定channel的账本。一个应用程序要查询多个channel的账本,需要为每一个channel创建一个Ledger client实例。Ledger client支持的查询有:QueryInfo, QueryBlock, QueryBlockByHash, QueryBlockByTxID, QueryTransaction and QueryConfig。

基本流程如下:

  1. 准备channel context
  2. 创建ledger client
  3. 查询账本

type Client

client能够查询fabric网络中的账本。

  • func New

    func New(channelProvider context.ChannelProvider, opts ...ClientOption) (*Client, error)

    返回一个ledger client实例。一个ledger client实例提供了查询指定channel中多种信息的handler。一个应用程序要与多个channel交互,需要为每个channel单独创建ledger client实例。ledger client只支持特定的查询。

  • func (*Client)QueryBlock

    func (c *Client) QueryBlock(blockNumber uint64, options ...RequestOption) (*common.Block, error)

    根据block number查询账本中的Block。

  • func (*Client)QueryBlockByHash

    func (c *Client) QueryBlockByHash(blockHash []byte, options ...RequestOption) (*common.Block, error)

    根据block hash查询账本中的block

  • func (*Client)QueryBlockByTxID

    func (c *Client) QueryBlockByTxID(txID fab.TransactionID, options ...RequestOption) (*common.Block, error)

    查询包含指定交易信息的block。

  • func (*Client)QueryConfig

    func (c *Client) QueryConfig(options ...RequestOption) (fab.ChannelCfg, error)

    查询channel的配置信息

  • func (*Client)QueryInfo

    func (c *Client) QueryInfo(options ...RequestOption) (*fab.BlockchainInfoResponse, error)

    查询当前channel中各种有用的区块链信息,比如区块高度,当前区块hash

  • func (*Client)QueryTransaction

    func (c *Client) QueryTransaction(transactionID fab.TransactionID, options ...RequestOption) (*pb.ProcessedTransaction, error)

    根据transaction ID查询账本中的交易信息

    ...

5.msp包

导入:import ""

msp包能够创建、更新fabric网络中的用户。msp client执行这些操作:enroll、reenroll、register、revoke和getSigningIdentity

基本流程如下:

  1. 准备client context
  2. 创建 msp client
  3. 注册用户
  4. 准入用户

type AffiliationInfo

AffiliationInfo包括了从属关系名称,子从属关系信息以及与此从属关系相关联的身份。

type AffiliationRequest

AffiliationRequest代表从fabric-ca-server移除或者添加请求。

type AffiliationResponse

AffiliationResponse包括了get、modify、add、remove一个从属关系的响应。

type Attribute

Attribute定义了在注册期间可以传递的其他属性

type AttributeRequest type Client

Client可以获取Client service。

  • func New

    func New(clientProvider context.ClientProvider, opts ...ClientOption) (*Client, error)

    创建一个client实例

  • func (*Client)AddAffiliation

    func (c *Client) AddAffiliation(request *AffiliationRequest) (*AffiliationResponse, error)

    添加一个新的从属关系到服务

  • func (*Client)CreateIdentity

    func (c *Client) CreateIdentity(request *IdentityRequest) (*IdentityResponse, error)

    用fabric CA服务创建一个新的身份。返回一个带enrollment ID的准入密钥,可以用来enroll一个新的身份

  • func (*Client)CreateSigningIdentity

    func (c *Client) CreateSigningIdentity(opts ...mspctx.SigningIdentityOption) (mspctx.SigningIdentity, error)

    用opts创建一个签名身份

  • func (*Client)Enroll

    func (c *Client) Enroll(enrollmentID string, opts ...EnrollmentOption) error

    为了得到一个签了名的X509证书,准入一个已经注册了的用户。为这个用户生成新的键对。CA颁发的私钥和准入证书存储在SDK存储中。可以通过调用IdentityMannager.GetSigningIdentity()获取到。

  • func (*Client)GetAffiliation

    func (c *Client) GetAffiliation(affiliation string, options ...RequestOption) (*AffiliationResponse, error)

    返回关于请求的从属关系的信息

  • func (*Client)GetAllAffiliation

    func (c *Client) GetAllAffiliations(options ...RequestOption) (*AffiliationResponse, error)

    返回调用者有权获取的所有从属关系

  • func (*Client)GetAllIdentities

    func (c *Client) GetAllIdentities(options ...RequestOption) ([]*IdentityResponse, error)

    返回调用者有权获取的所有身份信息

  • func (*Client)GetCAInfo

    func (c *Client) GetCAInfo() (*GetCAInfoResponse, error)

    返回CA信息

  • func (*Client)GetIndentity

    func (c *Client) GetIdentity(ID string, options ...RequestOption) (*IdentityResponse, error)

    根据identity ID获取身份信息

  • func (*Client)GetSigningIdentity

    func (c *Client) GetSigningIdentity(id string) (mspctx.SigningIdentity, error)

    根据id获取签名身份

  • func (*Client)ModifyAffiliation

    func (c *Client) ModifyAffiliation(request *ModifyAffiliationRequest) (*AffiliationResponse, error)

    对已经在服务中存在的从属关系重命名

  • func (*Client)ModifyIdentity

    func (c *Client) ModifyIdentity(request *IdentityRequest) (*IdentityResponse, error)

    用fabric CA 服务修改身份

  • func (*Client)Reenroll

    func (c *Client) Reenroll(enrollmentID string, opts ...EnrollmentOption) error

    为了获得一个新的签名的X509证书,重新准入一个已经准入的用户

  • func (*Client)Register

    func (c *Client) Register(request *RegistrationRequest) (string, error)

    用fabric CA注册一个用户

  • func (*Client)RemoveAffiliation

    func (c *Client) RemoveAffiliation(request *AffiliationRequest) (*AffiliationResponse, error)

    从服务中移除一个已经存在的从属关系

  • func (*Client)RemovedIdentity

    func (c *Client) RemoveIdentity(request *RemoveIdentityRequest) (*IdentityResponse, error)

    从fabric CA中移除身份

  • func (*Client)Revoke

    func (c *Client) Revoke(request *RevocationRequest) (*RevocationResponse, error)

    Fabric CA回收用户

    ...

6.resmgmt包

导入: import ""

resmgmt包能够创建和更新fabric网络中的资源。它允许管理员创建和/或更新channel,将peer加入channel。管理员也能够在peer上执行与chaincode相关的操作,例如installing、instantiating和upgrading chaincode。

基本流程如下:

  1. 准备client context
  2. 创建resource management client
  3. 创建channel
  4. 将peer加入channel
  5. 将chaincode按照到peer文件系统
  6. 在channel上实例化chaincode 7.查询channel中的peer,已安装/实例化的chaincode等

type Client

Client能够管理fabric网络资源

  • func New

    func New(ctxProvider context.ClientProvider, opts ...ClientOption) (*Client, error)

    返回一个资源管理的client实例

  • func (*Client)CreateConfigSignature

    func (rc *Client) CreateConfigSignature(signer msp.SigningIdentity, channelConfigPath string) (*common.ConfigSignature, error)

    根据channelConfigPath参数,为给定的client、自定义签名者和chConfig创建签名

  • func (*Client)CreateConfigSignatureData

    func (rc *Client) CreateConfigSignatureData(signer msp.SigningIdentity, channelConfigPath string) (signatureHeaderData resource.ConfigSignatureData, e error)

    准备SignatureHeader和完整地签名字节数组(signingBytes),供Channel Config签名使用

    一旦SigningBytes在外部签名(使用扩展工具,像OpenSSL签名signatureHeaderData.SigningBytes),指定如下操作:

    1. 创建一个common.ConfigSignature{}实例
    2. 将返回的属性"signatureHeaderData.signatureHeader"指定给它的SignatureHeader属性
    3. 将从扩展工具生成的签名"signatureHeaderData.signingBytes"指定给它的Signature属性

    使用WithConfigSignatures()可选项传递这个新的实例给channel更新

  • func (*Client)InstallCC

    func (rc *Client) InstallCC(req InstallCCRequest, options ...RequestOption) ([]InstallCCResponse, error)

    允许管理员安装chaincode到peer的文件系统中。如果可选项中没有指定peer,则默认是属于这个管理员所在MSP的所有peer。

  • func (*Client)InstantiateCC

    func (rc *Client) InstantiateCC(channelID string, req InstantiateCCRequest, options ...RequestOption) (InstantiateCCResponse, error)

    用可选的自定义选项(特定的peer、filtered peer、timeout)实例化chaincode。如果在可选项中没有指定peer,则默认是channel中所有的peer

  • func (*Client)JoinChannel

    func (rc *Client) JoinChannel(channelID string, options ...RequestOption) error

    允许peer根据可选的自定义选项(特定的peer、filtered peers)加入到已存在的channel中。如果peer在可选项中没有指定,默认是属于当前用户所在的MSP中所有的peer。

  • func (*Client)QueryChannels

    func (rc *Client) QueryChannels(options ...RequestOption) (*pb.ChannelQueryResponse, error)

    查询peer加入的所有的channel的名字

  • func (*Client)QueryCollectionsConfig

    func (rc *Client) QueryCollectionsConfig(channelID string, chaincodeName string, options ...RequestOption) (*common.CollectionConfigPackage, error)

    查询特定通道中peer上的配置集。如果在可选项中没有指定peer,会随机的查询在通道上的peer。参数:channel、chaincode name是必须的,options是可选的

  • func (*Client)QueryConfigFromOrderer

    func (rc *Client) QueryConfigFromOrderer(channelID string, options ...RequestOption) (fab.ChannelCfg, error)

    返回来自orderer的channel配置。如果options中没有提供orderer,会使用默认的channel orderer(如果有配置)或者配置中随机选orderer

  • func (*Client)QueryInstalledChaincodes

    func (rc *Client) QueryInstalledChaincodes(options ...RequestOption) (*pb.ChaincodeQueryResponse, error)

    查询peer上安装的chaincode

  • func (*Client)QueryInstantiatedChaincodes

    func (rc *Client) QueryInstantiatedChaincodes(channelID string, options ...RequestOption) (*pb.ChaincodeQueryResponse, error)

    查询指定channel中peer上实例化的的chaincode。如果options中没有指定peer,将在这个channel中随机peer上查询

  • func (*Client)SaveChannel

    func (rc *Client) SaveChannel(req SaveChannelRequest, options ...RequestOption) (SaveChannelResponse, error)

    创建或者更新channel
    参数:req持有必须的channel name和configuration信息;options是可选的,如果options有签名(WithConfigSignatures()或调用一个以上的WithConfigSignature()),SaveChannel将会使用这些签名,而不是创建一个新的。
    确保req.ChannelConfigPath/req.ChannelConfig有channel config并与这些签名匹配

  • func (*Client)UpgradeCC

    func (rc *Client) UpgradeCC(channelID string, req UpgradeCCRequest, options ...RequestOption) (UpgradeCCResponse, error)

    用可选的自定义选项(特定的peer,filtered peers,timeout)升级chaincode。如果options中没有指定peer,默认是所有的channel peer

    ...

pkg-fab

1.txn包

导入:import ""

txn包能够创建、背书和发送交易到fabric peer和orderer

  • func BroadcostPayload

    func BroadcastPayload(reqCtx reqContext.Context, payload *common.Payload, orderers []fab.Orderer) (*fab.TransactionResponse, error)

    发送payload到一些orderer,随机选取端点直到精疲力尽

  • func CreateChaincodeInvokeProposal

    func CreateChaincodeInvokeProposal(txh fab.TransactionHeader, request fab.ChaincodeInvokeRequest) (*fab.TransactionProposal, error)

    创建交易提议

  • func CreateChannelHeader

    func CreateChannelHeader(headerType common.HeaderType, opts ChannelHeaderOpts) (*common.ChannelHeader, error)

    构建公共的链头(common chain header),公共方法

  • func CreatePayload

    func CreatePayload(txh *TransactionHeader, channelHeader *common.ChannelHeader, data []byte) (*common.Payload, error)

    根据channelHeader和data切片创建payload字符切片

  • func CreateSignatureHeader

    func CreateSignatureHeader(txh *TransactionHeader) (*common.SignatureHeader, error)

    基于nonce和交易头的创建者来创建SignnatureHeader

  • func New

    func New(request fab.TransactionRequest) (*fab.Transaction, error)

    创建一笔交易,包括提议响应,背书策略

  • func Send

    func Send(reqCtx reqContext.Context, tx *fab.Transaction, orderers []fab.Orderer) (*fab.TransactionResponse, error)

    发送交易到链的orderer服务(一个或多个orderer端)以达成共识并提交到账本

  • func SendPayload

    func SendPayload(reqCtx reqContext.Context, payload *common.Payload, orderers []fab.Orderer) (*common.Block, error)

    发送payload到每个orderer并返回一个区块响应

  • func SendProposal

    func SendProposal(reqCtx reqContext.Context, proposal *fab.TransactionProposal, targets []fab.ProposalProcessor) ([]*fab.TransactionProposalResponse, error)

    发送TransactionProposal到ProposalProcessor

    ...

pkg-fabsdk

1. fabsdk包

fabsdk包允许用户使用Hyperledger fabric网络

type FabricSDK

FabricSDK为SDK管理的client提供访问(和context)

  • func New

    func New(configProvider core.ConfigProvider, opts ...Option) (*FabricSDK, error)

    基于提供的options集合新建初始化SDK。

  • func (*FabricSDK)ChannelContext

    func (sdk *FabricSDK) ChannelContext(channelID string, options ...ContextOption) contextApi.ChannelProvider

    创建并返回channel context

  • func (*FabricSDK)Close

    func (sdk *FabricSDK) Close()

    关闭并释放SDK持有的换成和连接

  • func (*FabricSDK)Config

    func (sdk *FabricSDK) Config() (core.ConfigBackend, error)

    返回所有SDK配置类型使用的配置后端

  • func (*FabricSDK)Context

    func (sdk *FabricSDK) Context(options ...ContextOption) contextApi.ClientProvider

    创建并返回具有所有必要提供者的context client

    ...

gosdk测试

  1. 获取fabric goSDK源码:git clone

  2. 进入fabric-sdk-go目录,执行make命令

    1. make或make all,执行单元测试和集成测试
    2. make dpend 安装测试依赖
    3. make unit-test 运行单元测试
    4. make integration-test 运行集成测试
    5. make clean

    (在执行单元时由于依赖下载不下来,另外gometalinter命令无法使用,就将scripts/populate-vendor.sh中109行以及scripts/unit.sh中的76、77、78行注释掉,手动将依赖导入,这样可以跑通单元测试。由于集成测试是在容器中运行的,需要将运行所需的依赖也挂载到容器中,所以将gopath整个路径挂载过去才运行起来的)

转载于:https://juejin.im/post/5c1644895188250d104ca929

你可能感兴趣的文章
hive-极致优化(二)-解释计划类别
查看>>
ajax跨域
查看>>
webpack4配置详解之常用插件分享
查看>>
phalcon7 阅读理解
查看>>
消息中间件 RocketMQ源码解析:Message顺序发送与消费
查看>>
Thread interrupt
查看>>
博客搬家至简书
查看>>
Linux上的free命令详解
查看>>
linux主目录下各个子目录的作用
查看>>
[问题]javax.servlet不存在的问题
查看>>
Hive学习总结之五:HBase和Hive的集成
查看>>
Windows7系统中启动Windows Event Log服务提示拒绝访问,错误5的解决方法
查看>>
mybatis--缓存(一级和二级缓存)
查看>>
centos 配置 nginx + fcgiwrap + git
查看>>
Eclipse下Maven打包非法字符问题
查看>>
FreeMarker标签
查看>>
AngularJS 中的 Promise 和 设计模式
查看>>
《从面试题来看源码》,单参数,多参数,如何正确使用@Param
查看>>
《JavaScript设计模式》学习日志
查看>>
MySql 建表、添加字段、修改字段、添加索引SQL语句写法
查看>>