执行链码错误:执行事务失败:执行事务时超时到期

时间:2018-07-27 13:40:19

标签: hyperledger-fabric blockchain

我编写了此链码,当我调用它与GetOrgan一起读取时,命令成功执行,但是当我想用AddOrgan添加器官时,出现此错误:

  

错误支持调用:rpc错误:代码=未知desc =执行错误> chaincode:无法执行事务:执行> transaction时超时-

import (
"bytes"
"encoding/json"
"fmt"
"strconv"

"github.com/hyperledger/fabric/core/chaincode/shim"
sc "github.com/hyperledger/fabric/protos/peer"
)
 type SmartContract struct {
 }
type Organ struct {
ID             string `json:"ID"`
Type           string `json:"type"`
Timestamp      string `json:"timestamp"`
HolderHospital string `json:"HolderHospital"`
LifeSpan       string `json:"LifeSpan"`
}
func (s *SmartContract) Invoke(APIstub shim.ChaincodeStubInterface) 
sc.Response {

// Retrieve the requested Smart Contract function and arguments
function, args := APIstub.GetFunctionAndParameters()
// Route to the appropriate handler function to interact with the 
ledger
if function == "GetOrgan" {
    return s.GetOrgan(APIstub, args)
} else if function == "initLedger" {
    return s.initLedger(APIstub)
} else if function == "AddOrgan" {
    return s.AddOrgan(APIstub, args)
} else if function == "GetAllOrgan" {
    return s.GetAllOrgan(APIstub)
} else if function == "changeOrganHolder" {
    return s.changeOrganHolder(APIstub, args)
}

return shim.Error("InvalID Smart Contract function name.")
}


func (s *SmartContract) GetOrgan(APIstub shim.ChaincodeStubInterface, 
args []string) sc.Response {
fmt.println("hello from Chaincode")
if len(args) != 1 {
    return shim.Error("Incorrect number of arguments. Expecting 1")
}

organAsBytes, _ := APIstub.GetState(args[0])
if organAsBytes == nil {
    return shim.Error("Could not locate organ")
}
return shim.Success(organAsBytes)
}

/*
* The initLedger method *
Will add test data (10 organ catches)to our network
*/
func (s *SmartContract) initLedger(APIstub 
shim.ChaincodeStubInterface) 
sc.Response {
fmt.println("hello from Chaincode")
organ := []Organ{
    Organ{ID: "1", Type: "Foie", Timestamp: "12022003", 
HolderHospital: "nabeul", LifeSpan: "24"},
    Organ{ID: "2", Type: "Coeur", Timestamp: "12022003", 
HolderHospital: "Tunis", LifeSpan: "36"},
}

i := 0
for i < len(organ) {
    fmt.Println("i is ", i)
    organAsBytes, _ := json.Marshal(organ[i])
    APIstub.PutState(strconv.Itoa(i+1), organAsBytes)
    fmt.Println("Added", organ[i])
    i = i + 1
}

return shim.Success(nil)
}
func (s *SmartContract) AddOrgan(APIstub shim.ChaincodeStubInterface, 
args []string) sc.Response {
fmt.println("hello from Chaincode")
if len(args) != 6 {
    return shim.Error("Incorrect number of arguments. Expecting 6")
}

var organ = Organ{ID: args[1], Type: args[2], Timestamp: args[3], 
HolderHospital: args[4], LifeSpan: args[5]}

organAsBytes, _ := json.Marshal(organ)
err := APIstub.PutState(args[0], organAsBytes)
if err != nil {
    return shim.Error(fmt.Sprintf("Failed to add organ: %s", args[0]))
}
fmt.Print(err)

return shim.Success(nil)
}

0 个答案:

没有答案