如何使用 hyperledger fabric network SDK 查询 chaincode metada
how to query chaincode metada with hyperledger fabric network SDK
有没有办法使用 NodeJS 或 Go fabric-network SDK 获取链代码元数据?
类似于peer lifecycle chaincode queryinstalled
命令的内容:
{
"installed_chaincodes": [
{
"package_id": "testcc_1:75afd7c4c165c56e8b8f3bd4c53cea8b420f4d94a3d53093aa0ec0229f5c738a",
"label": "testcc_1",
"references": {
"mychannel": {
"chaincodes": [
{
"name": "testcc",
"version": "1"
}
]
}
}
}
]
}
如果不是,那么客户端如何确保他们调用的是哪个版本的链码?
每个SDK都实现了cli中几乎所有可用的方法。
在 go-sdk
的情况下,你可以这样实现。
import (
"fmt"
"strings"
"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
fabAPI "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
lcpackager "github.com/hyperledger/fabric-sdk-go/pkg/fab/ccpackager/lifecycle"
"github.com/pkg/errors"
)
// QueryInstalledCC : query installed CC
func QueryInstalledCC(setup *OrgSetup, ccName, ccVersion, packageID string) (string, string, map[string][]resmgmt.CCReference, error) {
label, _, _ := PackageCC(ccName, ccVersion)
resp, err := setup.Resmgmt.LifecycleQueryInstalledCC(resmgmt.WithTargetEndpoints(peer1), resmgmt.WithRetry(retry.DefaultResMgmtOpts))
if err != nil {
fmt.Printf("\n Error occurred in queryInstalledCC func and error is %s", err)
}
if !strings.EqualFold(packageID, resp[0].PackageID) {
fmt.Print("Unable to match packageID in QueryInstalledCC")
return "", "", nil, nil
}
if !strings.EqualFold(label, resp[0].Label) {
fmt.Print("Unable to match labels in QueryInstalledCC")
return "", "", nil, nil
}
ref := resp[0].References
return resp[0].Label, resp[0].PackageID, ref, nil
}
有没有办法使用 NodeJS 或 Go fabric-network SDK 获取链代码元数据?
类似于peer lifecycle chaincode queryinstalled
命令的内容:
{
"installed_chaincodes": [
{
"package_id": "testcc_1:75afd7c4c165c56e8b8f3bd4c53cea8b420f4d94a3d53093aa0ec0229f5c738a",
"label": "testcc_1",
"references": {
"mychannel": {
"chaincodes": [
{
"name": "testcc",
"version": "1"
}
]
}
}
}
]
}
如果不是,那么客户端如何确保他们调用的是哪个版本的链码?
每个SDK都实现了cli中几乎所有可用的方法。
在 go-sdk
的情况下,你可以这样实现。
import (
"fmt"
"strings"
"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
fabAPI "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
lcpackager "github.com/hyperledger/fabric-sdk-go/pkg/fab/ccpackager/lifecycle"
"github.com/pkg/errors"
)
// QueryInstalledCC : query installed CC
func QueryInstalledCC(setup *OrgSetup, ccName, ccVersion, packageID string) (string, string, map[string][]resmgmt.CCReference, error) {
label, _, _ := PackageCC(ccName, ccVersion)
resp, err := setup.Resmgmt.LifecycleQueryInstalledCC(resmgmt.WithTargetEndpoints(peer1), resmgmt.WithRetry(retry.DefaultResMgmtOpts))
if err != nil {
fmt.Printf("\n Error occurred in queryInstalledCC func and error is %s", err)
}
if !strings.EqualFold(packageID, resp[0].PackageID) {
fmt.Print("Unable to match packageID in QueryInstalledCC")
return "", "", nil, nil
}
if !strings.EqualFold(label, resp[0].Label) {
fmt.Print("Unable to match labels in QueryInstalledCC")
return "", "", nil, nil
}
ref := resp[0].References
return resp[0].Label, resp[0].PackageID, ref, nil
}