1

有没有办法使用 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"
                                            }
                                    ]
                            }
                    }
            }
    ]
}

如果不是,那么客户端如何确保他们调用的是哪个版本的链码?

4

1 回答 1

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
}

于 2020-08-21T13:25:23.253 回答