In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you what fabric go sdk is, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!
Fabric go sdk is the official Goto language development kit provided by Hyperledger Fabric. Applications can use fabric go sdk to interact with the fabric network and access chain codes. This tutorial describes how to use fabric go sdk to install chain codes, instantiate, submit transactions, and query status.
In this article, baas, application programs, and clients that interact with Hyperledger fabric networks are collectively referred to as clients. They can interact with fabric go sdk and fabric block chain networks, and fabric go sdk calls grpc to communicate with specified peer and orderer. The purpose of this article is to show how to use fabric go sdk to operate chain codes on the basis of fabric networks built by BYFN:
1. Introduction to fabric go sdk
Fabric go sdk is Fabric's official Go language SDK. Its directory structure is as follows:
There are two directories to note, internal and third_party, which contain some code that fabric go sdk depends on, from fabric and fabric-ca. When using some types of fabric, you should use the following methods instead of directly importing fabric or fabric-ca:
Import "github.com/hyperledger/fabric go sdk/third_party/github.com/hyperledger/fabric/xxx"
The pkg directory is the main implementation of fabric go sdk. The doc document describes the functions provided by different directories and gives examples of interface calls:
Pkg/fabsdk: the main package, which is mainly used to generate option context used by fabsdk and other pkg in fabric go sdk.
Pkg/client/channel: mainly used to call, query Fabric chain code, or register chain code events.
Pkg/client/resmgmt: mainly used for Hyperledger fabric network management, such as creating channels, joining channels, installing, instantiating, and upgrading chain codes.
Pkg/client/event: cooperate with channel module to register and filter Fabric chain code events.
Pkg/client/ledger: mainly used to query Fabric account books, query blocks, transactions, configuration and so on.
Pkg/client/msp: mainly used to manage membership in fabric network.
3. General steps for using fabric go sdk
Write a configuration file config.yaml for client
Create a fabric sdk instance fabsdk for client
Create a resource manage client for client, referred to as client used by RC,RC for administrative operations, such as channel creation, chain code installation, instantiation and upgrade, etc.
Create a channel client for client, referred to as CC,CC, which is used to call and query chain codes, and to register and unregister chain code events
4. Overview of fabric go sdk configuration file config.yaml
When client uses sdk to interact with the fabric network, you need to tell sdk two types of information:
Who am I: the current client information, including the organization to which I belong, the key, the path to the certificate file, and so on, which is specific to each client.
Who is the other party: that is, the information of the fabric network structure, and how the channel, org, orderer and peer combine the current fabric network, these structural information should be consistent with the configytx.yaml. This is a common configuration and can be used by each client. In addition, this part of the information does not need to be complete fabric network information, if the current client only interacts with some nodes, then the configuration file only needs to contain the network information used.
5. Use go mod to manage the dependencies of fabric go sdk projects
Fabric go sdk itself currently uses go modules to manage dependencies. As you can see from go.mod, some dependent packages specify specific versions. If your project depends on a different version than fabric go sdk, it will cause compilation problems.
Therefore, it is recommended that the project also use go moudles to manage dependencies, and then the same package can use the same version, which can be done as follows:
Go mod init initializes the go.mod file for the project.
Writing code and running go mod run after completion will automatically download dependent projects, but the version may be different from the dependent version in fabric go sdk, and there are problems with compilation.
Copy the contents of go.mod into the go.mod of the project, and then save, go mod will automatically merge the same dependencies, run go mod tidy, and automatically add new dependencies or delete unwanted dependencies.
6. Create an entry instance of fabric go sdk
Parse the configuration file through config.FromFile, and then create a portal instance of fabric go sdk through fabsdk.New.
Import "github.com/hyperledger/fabric go sdk/pkg/core/config" import "github.com/hyperledger/fabric go sdk/pkg/fabsdk" sdk, err: = fabsdk.New (config.FromFile (c.ConfigPath)) if err! = nil {log.Panicf ("failed to create fabric sdk:% s", err)} 7, create a resource management client for fabric go sdk
Only the administrator account can manage the Hyperledger fabric network, so you must use the administrator account to create the resource management client.
Specify the Admin account of Org1 through fabsdk.WithOrg ("Org1") and fabsdk.WithUser ("Admin"), create the clientProvider using sdk.Context, and then create the fabric go sdk resource management client through resmgmt.New.
Import "github.com/hyperledger/fabric go sdk/pkg/client/resmgmt" rcp: = sdk.Context (fabsdk.WithUser ("Admin"), fabsdk.WithOrg ("Org1")) rc, err: = resmgmt.New (rcp) if err! = nil {log.Panicf ("failed to create resource client:% s", err)} 8, create fabric go sdk's tunnel client
Use the user account to create a fabric go sdk channel client for calling and querying fabric chain codes. To create a channelProvider using sdk.ChannelContext, you need to specify channelID and user User1, and then create a channel client through channel.New, which calls the channel client of the chain code on the channel corresponding to channelID.
Import "github.com/hyperledger/fabric go sdk/pkg/client/channel" ccp: = sdk.ChannelContext (ChannelID, fabsdk.WithUser ("User1")) cc, err: = channel.New (ccp) if err! = nil {log.Panicf ("failed to create channel client:% s", err)} 9, install chain code for resource management client using fabric go sdk
Install the Fabric chain code using the InstallCC interface of the resource management client, you need to specify resmgmt.InstallCCRequest and which peers to install on. Resmgmt.InstallCCRequest indicates the chain code ID, chain code path, chain code version, and packaged chain code.
To package the chain code, you need to use the chain code path CCPath and GoPath,GoPath, that is, the native $GOPATH,CCPath is relative to the GoPath. If the path is not set correctly, the sdk will not find the chain code.
/ / pack the chaincodeccPkg, err: = gopackager.NewCCPackage ("github.com/hyperledger/fabric-samples/chaincode/chaincode_example02/go/", "/ Users/shitaibin/go") if err! = nil {return errors.WithMessage (err, "pack chaincode error")} / / new request of installing chaincodereq: = resmgmt.InstallCCRequest {Name: c.CCID, Path: c.CCPath, Version: v, Package: ccPkg,} reqPeers: = resmgmt.WithTargetEndpoints ("peer0.org1.example.com") resps Err: = rc.InstallCC (req, reqPeers) if err! = nil {return errors.WithMessage (err, "installCC error")} 10. Instantiate the chain code of the resource management client using fabric go sdk
To instantiate the chain code, you need to use the InstantiateCC interface of fabric go sdk's resource management client. You need to use ChannelID, resmgmt.InstantiateCCRequest and peers to indicate which channel to instantiate the chain code. The request contains the ID, path, version, initialization parameters and endorsement policy of the chain code. Endorsement policy can be generated through cauthdsl.FromString.
/ / endorser policyorg1OrOrg2: = "OR ('Org1MSP.member','Org2MSP.member')" ccPolicy, err: = cauthdsl.FromString (org1OrOrg2) if err! = nil {return errors.WithMessage (err, "gen policy from string error")} / / new requestargs: = packArgs ([] string {"init", "a", "100th", "b", "200"}) req: = resmgmt.InstantiateCCRequest {Name: c.CCID, Path: c.CCPath, Version: v, Args: args Policy: ccPolicy,} / / send request and handle responsereqPeers: = resmgmt.WithTargetEndpoints ("peer0.org1.example.com") resp, err: = rc.InstantiateCC (ChannelID, req, reqPeers) if err! = nil {return errors.WithMessage (err, "instantiate chaincode error")} 11. Upgrade chain code for resource management client using fabric go sdk
In fabric go sdk, the upgrade chain code is very similar to the instantiated chain code, except that the request is resmgmt.UpgradeCCRequest and the calling API is rc.UpgradeCC:
/ / endorser policyorg1AndOrg2: = "AND ('Org1MSP.member','Org2MSP.member')" ccPolicy, err: = c.genPolicy (org1AndOrg2) if err! = nil {return errors.WithMessage (err, "gen policy from string error")} / / new requestargs: = packArgs ([] string {"init", "a", "100th", "b", "200"}) req: = resmgmt.UpgradeCCRequest {Name: c.CCID, Path: c.CCPath, Version: v, Args: args Policy: ccPolicy,} / / send request and handle responsereqPeers: = resmgmt.WithTargetEndpoints ("peer0.org1.example.com") resp, err: = rc.UpgradeCC (ChannelID, req, reqPeers) if err! = nil {return errors.WithMessage (err, "instantiate chaincode error")} 12. The chain code is called by the channel client using fabric go sdk
In fabric go sdk, the chain code is called using the Execute interface of the channel client, and the input parameters channel.Request and peers are used to indicate which peer to execute the chain code for endorsement. Channel.Request indicates the chain code to be called, and the function args,args in the chain code to Invoke is the result of serialization, and serialization is custom, as long as the chain code can be deserialized according to the same rules.
/ / new channel request for invokeargs: = packArgs ([] string {"a", "b", "10"}) req: = channel.Request {ChaincodeID: c.CCID, Fcn: "invoke", Args: args,} / send request and handle response// peers is neededreqPeers: = channel.WithTargetEndpoints ("peer0.org1.example.com") resp, err: = cc.Execute (req, reqPeers) if err! = nil {return errors.WithMessage (err) "invoke chaincode error")} log.Printf ("invoke chaincode tx:% s", resp.TransactionID) 13. Use the channel client of fabric go sdk to query the chain code
In fabric go sdk, querying and calling chain codes are very similar, using the same channel.Request, indicating the query function in the Invoke chain code, and then calling cc.Query for query operation, so that the node does not endorse the request:
/ / new channel request for queryreq: = channel.Request {ChaincodeID: c.CCID, Fcn: "query", Args: packArgs ([] string {keys}),} / / send request and handle responsereqPeers: = channel.WithTargetEndpoints (peer) resp, err: = cc.Query (req, reqPeers) if err! = nil {return errors.WithMessage (err, "query chaincode error")} log.Printf ("query chaincode tx:% s", resp.TransactionID) log.Printf ("result:% v" String (resp.Payload)) these are all the contents of the article "what is fabric go sdk?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.