Hyperledger fabric-SDK-GO客户端开发篇(六) (4)

(1)客户端实现

/** author: liuhui */ package sdkInit import ( "fmt" "github.com/astaxie/beego/logs" mspclient "github.com/hyperledger/fabric-sdk-go/pkg/client/msp" "github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt" "github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry" "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp" "github.com/hyperledger/fabric-sdk-go/pkg/core/config" "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk" "github.com/hyperledger/fabric-sdk-go/pkg/client/channel" "github.com/hyperledger/fabric-sdk-go/pkg/fab/ccpackager/gopackager" "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/common/policydsl" ) const ChaincodeVersion = "1.0" //initialized fabric sdk func SetupSDK(ConfigFile string, initialized bool) (*fabsdk.FabricSDK, error) { if initialized { //logs.Error("Fabric SDK has been initialized") return nil, fmt.Errorf("Fabric SDK has been initialized") } sdk, err := fabsdk.New(config.FromFile(ConfigFile)) if err != nil { //logs.Error("Instantiation Fabric SDK failed") return nil, fmt.Errorf("Instantiation Fabric SDK failed: %v", err) } logs.Informational("Fabric SDK is initialized successfully") return sdk, nil } // create channel and join peers func CreateChannel(sdk *fabsdk.FabricSDK, info *InitInfo) error { clientContext := sdk.Context(fabsdk.WithUser(info.OrgAdmin), fabsdk.WithOrg(info.OrgName)) if clientContext == nil { return fmt.Errorf("Failed to create client context based on organization name and administrator user") } // New returns a resource management client instance. resMgmtClient, err := resmgmt.New(clientContext) if err != nil { return fmt.Errorf("Failed to create resource management client by client context: %v", err) } // New creates a new Client instance mspClient, err := mspclient.New(sdk.Context(), mspclient.WithOrg(info.OrgName)) if err != nil { return fmt.Errorf("Failed to create Org MSP client by specified OrgName: %v", err) } // Returns: signing identity adminIdentity, err := mspClient.GetSigningIdentity(info.OrgAdmin) if err != nil { return fmt.Errorf("Failed to get the signature of the specified ID: %v", err) } // SaveChannelRequest holds parameters for save channel request channelReq := resmgmt.SaveChannelRequest{ChannelID: info.ChannelID, ChannelConfigPath: info.ChannelConfig, SigningIdentities: []msp.SigningIdentity{adminIdentity}} // save channel response with transaction ID _, err = resMgmtClient.SaveChannel(channelReq, resmgmt.WithRetry(retry.DefaultResMgmtOpts), resmgmt.WithOrdererEndpoint(info.OrdererOrgName)) if err != nil { return fmt.Errorf("Failed to create channle: %v", err) } logs.Informational("Create channel successful") info.OrgResMgmt = resMgmtClient // allows for peers to join existing channel with optional custom options (specific peers, filtered peers). If peer(s) are not specified in options it will default to all peers that belong to client's MSP. err = info.OrgResMgmt.JoinChannel(info.ChannelID, resmgmt.WithRetry(retry.DefaultResMgmtOpts), resmgmt.WithOrdererEndpoint(info.OrdererOrgName)) if err != nil { return fmt.Errorf("Peers failed to join channel: %v", err) } logs.Informational("Peers join channel successful") return nil } //install and instantiate chaincode func InstallAndInstantiateCC(sdk *fabsdk.FabricSDK, info *InitInfo) (*channel.Client, error) { logs.Informational("Start to install chaincode") // creates new go lang chaincode package ccPkg, err := gopackager.NewCCPackage(info.ChaincodePath, info.ChaincodeGoPath) if err != nil { return nil, fmt.Errorf("Failed to create chaincode package: %v", err) } // contains install chaincode request parameters installCCReq := resmgmt.InstallCCRequest{Name: info.ChaincodeID, Path: info.ChaincodePath, Version: ChaincodeVersion, Package: ccPkg} // allows administrators to install chaincode onto the filesystem of a peer _, err = info.OrgResMgmt.InstallCC(installCCReq, resmgmt.WithRetry(retry.DefaultResMgmtOpts)) if err != nil { return nil, fmt.Errorf("Failed to install chaincode: %v", err) } logs.Informational("Install chaincode successful") logs.Informational("Start to instantiate chaincode") // returns a policy that requires one valid ccPolicy := policydsl.SignedByAnyMember([]string{"org1.institution.com"}) instantiateCCReq := resmgmt.InstantiateCCRequest{Name: info.ChaincodeID, Path: info.ChaincodePath, Version: ChaincodeVersion, Args: [][]byte{[]byte("init")}, Policy: ccPolicy} // instantiates chaincode with optional custom options (specific peers, filtered peers, timeout). If peer(s) are not specified _, err = info.OrgResMgmt.InstantiateCC(info.ChannelID, instantiateCCReq, resmgmt.WithRetry(retry.DefaultResMgmtOpts)) if err != nil { return nil, fmt.Errorf("Failed to instantiate chaincode: %v", err) } logs.Informational("Instantiate chaincode successful") clientChannelContext := sdk.ChannelContext(info.ChannelID, fabsdk.WithUser(info.UserName), fabsdk.WithOrg(info.OrgName)) // returns a Client instance. Channel client can query chaincode, execute chaincode and register/unregister for chaincode events on specific channel. channelClient, err := channel.New(clientChannelContext) if err != nil { return nil, fmt.Errorf("Failed to create channel context: %v", err) } logs.Informational("Create channel client successful ,you can use it to execute transactions.") return channelClient, nil } func ChannelClient(sdk *fabsdk.FabricSDK, info *InitInfo) (*channel.Client,error){ clientChannelContext := sdk.ChannelContext(info.ChannelID, fabsdk.WithUser(info.UserName), fabsdk.WithOrg(info.OrgName)) // returns a Client instance. Channel client can query chaincode, execute chaincode and register/unregister for chaincode events on specific channel. channelClient, err := channel.New(clientChannelContext) if err != nil { return nil, fmt.Errorf("Failed to create channel context: %v", err) } logs.Informational("Create channel client successful ,you can use it to execute transactions.") return channelClient, nil }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpygdx.html