【问题标题】:Error building chaincode written in go构建用 go 编写的链码时出错
【发布时间】:2018-04-28 16:02:34
【问题描述】:

当我尝试修改this hyperledger example 中描述的示例时,添加this external library 以获取链码状态的历史时出现一些错误。
为什么会这样?

我用 govendor 添加库,但是当我运行这个命令时:

docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" cli peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n $CC_NAME -l "$LANGUAGE" -v 1.0 -c $INIT_STR -P "OR ('Org1MSP.member','Org2MSP.member')"

我收到此错误:

错误:认可链码时出错:
rpc 错误:代码 = 未知 desc = 错误启动容器:无法生成特定于平台的 docker build:从 build 返回的错误:2 "# firstExample 链码/输入/src/firstExample/firstStep.go:104:11:无法将 *"github.com/hyperledger/fabric/protos/ledger/queryresult".KeyModification 分配给 kM(类型 *"firstExample/vendor/github.com/ hyperledger/fabric/protos/ledger/queryresult".KeyModification) 中的多重赋值 链码/输入/src/firstExample/firstStep.go:146:11:无法将 *"github.com/hyperledger/fabric/protos/ledger/queryresult".KeyModification 分配给 kM(类型 *"firstExample/vendor/github.com/ hyperledger/fabric/protos/ledger/queryresult".KeyModification) 中的多重赋值 链码/输入/src/firstExample/firstStep.go:156:11:无法将 *"github.com/hyperledger/fabric/protos/ledger/queryresult".KeyModification 分配给 kM(类型 *"firstExample/vendor/github.com/ hyperledger/fabric/protos/ledger/queryresult".KeyModification) 中的多重赋值

我对此有一些麻烦。我确定该库已导入,因为如果我使用以下命令构建用 go 编写的链代码:

go build 

我没有收到任何错误。
请帮帮我!

【问题讨论】:

    标签: go docker-compose hyperledger-fabric hyperledger


    【解决方案1】:

    如果没有看到导致编译错误的实际代码,很难猜测,而您似乎没有注意HistoryQueryIteratorInterface#Next() API 甚至ChaincodeStubInterface#GetHistoryForKey() 返回的第二个参数。请参阅如何正确使用这些 API 的示例:

    // GetPreviousValue reads previous value of given key
    func (pm *personManagement) GetPreviousValue(params []string, stub shim.ChaincodeStubInterface) peer.Response {
        historyIer, err := stub.GetHistoryForKey(params[0])
    
        if err != nil {
            errMsg := fmt.Sprintf("[ERROR] cannot retrieve history of key <%s>, due to %s", params[0], err)
            fmt.Println(errMsg)
            return shim.Error(errMsg)
        }
    
        if historyIer.HasNext() {
            modification, err := historyIer.Next()
            if err != nil {
                errMsg := fmt.Sprintf("[ERROR] cannot read key record modification, key <%s>, due to %s", params[0], err)
                fmt.Println(errMsg)
                return shim.Error(errMsg)
            }
            fmt.Println("Returning information about", string(modification.Value))
            return shim.Success(modification.Value)
        }
    
    
        fmt.Printf("No history found for key %s\n", params[0])
        return shim.Success([]byte(fmt.Sprintf("No history for key %s", params[0])))
    }
    

    注意:请注意historyIer.Next() 返回历史值和错误。

    【讨论】: