fabric智能合约(chaincode)升级版本

基于fabric-sdk-go

1.安装chaincode对应版本func

/**
  安裝
 */
func InstallPackage(client *channel.Client, sdk *fabsdk.FabricSDK, version string) {
    adminContext := sdk.Context(fabsdk.WithUser(OrgAdmin), fabsdk.WithOrg(OrgName))
    // Org resource management client
    orgResMgmt, err := resmgmt.New(adminContext)
    if err != nil {
        fmt.Println("error", err)
    }
    //chaincodeexample 相对路径如果设置错误就无法找到
    ccPkg, err := packager.NewCCPackage("github.com/example_cc", "chaincodeexample")
    if err != nil {
        fmt.Println(err)
    }
    // Install example cc to org peers
    installCCReq := resmgmt.InstallCCRequest{Name: CcID, Path: "github.com/example_cc", Version: version, Package: ccPkg}
    res, err := orgResMgmt.InstallCC(installCCReq)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(" ==================== ", res)
}

2.更新chaincode对应版本func

func UpdateCC(sdk *fabsdk.FabricSDK, version string, args [][]byte) {
    adminContext := sdk.Context(fabsdk.WithUser(OrgAdmin), fabsdk.WithOrg(OrgName))
    // Org resource management client
    orgResMgmt, err := resmgmt.New(adminContext)
    if err != nil {
        fmt.Println("error", err)
    }
    ccPolicy := cauthdsl.SignedByAnyMember([]string{"Org1MSP"})
    req := resmgmt.UpgradeCCRequest{Name: CcID, Version: version, Path: "github.com/example_cc", Policy: ccPolicy, Args: args}
    res, err := orgResMgmt.UpgradeCC(ChannelID, req)
    if err != nil {
        fmt.Println(err)
    } else{
        fmt.Println(" ========== UpdateCC ========== ", res)
    }
}

3.调用

InstallPackage(client, sdk,"2")
UpdateCC(sdk, "2", [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")})

docker ps 结果

CONTAINER ID        IMAGE                                                                                                        COMMAND                  CREATED             STATUS              PORTS                                            NAMES
5ddd8a57f280        dev-peer0.org1.example.com-e2eexamplecc-2-78de0b9a1866ca9f8a20d7fd9c922fd22a08a70365f165cf32d68490dd04f837   "chaincode -peer.a..."   2 minutes ago       Up 2 minutes                                                         dev-peer0.org1.example.com-e2eExampleCC-2
2fcc07b3633e        hyperledger/fabric-peer                                                                                      "peer node start"        5 hours ago         Up 5 hours          0.0.0.0:7055->7051/tcp, 0.0.0.0:7056->7053/tcp   peer1.org1.example.com
8c3db56a9164        hyperledger/fabric-peer                                                                                      "peer node start"        5 hours ago         Up 5 hours          0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp   peer0.org1.example.com
eb865ef8f10d        hyperledger/fabric-couchdb                                                                                   "tini -- /docker-e..."   5 hours ago         Up 5 hours          4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp       couchdb
0be71c923b02        hyperledger/fabric-ca                                                                                        "sh -c 'fabric-ca-..."   5 hours ago         Up 5 hours          0.0.0.0:7054->7054/tcp                           ca.example.com
47b76dbf1583        hyperledger/fabric-orderer                                                                                   "orderer"                5 hours ago         Up 5 hours          0.0.0.0:7050->7050/tcp                           orderer.example.com

现在调用如query或invoke都会执行chaincode版本2

fabric智能合约(chaincode)升级版本_第1张图片

你可能感兴趣的:(2018)