Hyperledger构建错误:创建新智能合约时出错:未提供错误链码ID

时间:2018-01-15 03:57:44

标签: hyperledger-fabric

我正在学习如何使用Hyperledger Fabric DLT框架。

我正在构建一个应用程序,允许 Tuna 的卖家以折扣价与买家进行交易,而不会影响卖家向其他买家销售的标记价格。

问题:但每次执行代码时都会抛出此消息

2018-01-14 18:45:11.292 EST [shim] SetupChaincodeLogging -> INFO 001 Chaincode log level not provided; defaulting to: INFO

2018-01-14 18:45:11.292 EST [shim] SetupChaincodeLogging -> INFO 002 Chaincode (build level: ) starting up ...

这是我每次运行代码时经常出现的错误:

Error creating new Smart Contract: Error chaincode id not providedeloiim:fabric_

这是我创建的代码

package main

import ( "github.com/hyperledger/fabric/core/chaincode/shim"; sc"github.com/hyperledger/fabric/protos/peer"; "bytes"; "encoding/json"; "strconv"; "fmt"; )

type SmartContract struct {
}

type Tuna struct {
  TxID int `json:"txid"`
  Container string `json:"container"`
  Timestamp string `json:"timestamp"`
  Location string `json:"location"`
  Owner string `json:"owner"`

}

changeTunaHolder - 当金枪鱼被传递给供应链中的不同方时,世界各州的数据可以通过谁拥有来更新。 changeTunaHolder方法接受2个参数,tuna id和new holder name。

我怀疑错误是使用changeTunaHolder方法,因为它需要id和Owner字段

func (S_ *SmartContract) changeTunaHolder(APIstub shim.ChaincodeStubInterface, parameters []string) sc.Response {

   if len(parameters) != 2 {
        return shim.Error("Incorrect number of arguments. Expecting 2")
  }

  tunaAsBytes, _ := APIstub.GetState(parameters[0])
    if tunaAsBytes == nil {
        return shim.Error("Could not locate tuna")
    }

  TUNA := Tuna{}
  json.Unmarshal(tunaAsBytes, &TUNA)

  // Normally check that the specified argument is a valid holder of tuna
    // we are skipping this check for this example
    TUNA.Owner = parameters[1]

  tunaAsBytes, _ = json.Marshal(TUNA)

  _error := APIstub.PutState(parameters[0], tunaAsBytes)

  if _error != nil {
      return shim.Error(fmt.Sprintf("Failed to change tuna holder: %s", parameters[0]))
  }

  return shim.Success(nil)

 }

初始化方法 - 在此代码中,代码尝试检索所请求的智能合约函数和参数

 func (S_ *SmartContract) Init(APIstub shim.ChaincodeStubInterface) sc.Response {
  return shim.Success(nil)
}

func (S_ *SmartContract ) Invoke(APIstub shim.ChaincodeStubInterface) sc.Response {

    function, parameters := APIstub.GetFunctionAndParameters()

    if function == "queryTuna" {
      return S_.queryTuna(APIstub, parameters)

    } else if function == "initLedger"{
      return S_.initLedger(APIstub)

    } else if function == "recordTuna" {
      return S_.recordTuna(APIstub, parameters)

    } else if function == "changeTunaHolder"  {
      return S_.changeTunaHolder(APIstub, parameters)

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

   }

queryTuna - queryTuna方法将由渔夫,监管机构或餐馆老板用于查看某个特定金枪鱼的记录。这需要一个论点 - 有关金枪鱼的关键。

func (S_ *SmartContract) queryTuna(APIstub shim.ChaincodeStubInterface, parameters []string) sc.Response {

  if len(parameters) != 1{
    return shim.Error("Incorrect number of arguments. Expecting 1")
  }

  tunaAsBytes, _ := APIstub.GetState(parameters[0])

  if tunaAsBytes == nil {
    return shim.Error("Could not locate tuna")
  }
  return shim.Success(tunaAsBytes)

}

initLedger - initLedger方法会将测试数据添加到我们的网络中。

func (S_ *SmartContract) initLedger(APIstub shim.ChaincodeStubInterface) sc.Response{


  TUNA := []Tuna {

        Tuna{TxID: 1,Container: "923F", Location: "67.0006, -70.5476", Timestamp: "1504054225", Owner: "Miriam"},
        Tuna{TxID: 2,Container: "M83T", Location: "91.2395, -49.4594", Timestamp: "1504057825", Owner: "Dave"},
        Tuna{TxID: 3,Container: "T012", Location: "58.0148, 59.01391", Timestamp: "1493517025", Owner: "Igor"},
        Tuna{TxID: 4,Container: "P490", Location: "-45.0945, 0.7949", Timestamp: "1496105425", Owner: "Amalea"},
        Tuna{TxID: 5,Container: "S439", Location: "-107.6043, 19.5003", Timestamp: "1493512301", Owner: "Rafa"},
        Tuna{TxID: 6,Container: "J205", Location: "-155.2304, -15.8723", Timestamp: "1494117101", Owner: "Shen"},
        Tuna{TxID: 7,Container: "S22L", Location: "103.8842, 22.1277", Timestamp: "1496104301", Owner: "Leila"},
        Tuna{TxID: 8,Container: "EI89", Location: "-132.3207, -34.0983", Timestamp: "1485066691", Owner: "Yuan"},
        Tuna{TxID: 9,Container: "129R", Location: "153.0054, 12.6429", Timestamp: "1485153091", Owner: "Carlo"},
        Tuna{TxID: 10,Container: "49W4", Location: "51.9435, 8.2735", Timestamp: "1487745091", Owner: "Fatima"},
  }

  iter := 0

  for iter < len(TUNA) {
    println("iter is", iter)

    tunaAsBytes, _ := json.Marshal(TUNA[iter])

    APIstub.PutState(strconv.Itoa(iter+1), tunaAsBytes)

    fmt.Println("ADDED", TUNA[iter])
    iter += 1
  }
  return shim.Success(nil)

}

recordTuna - recordTuna方法是像莎拉这样的渔民用来记录每个金枪鱼捕捞量的方法。此方法接受五个参数(要在分类帐中保存的属性)。

func (S_ *SmartContract) recordTuna(APIstub shim.ChaincodeStubInterface, parameters[]string) sc.Response {
  if len(parameters) != 5 {
    return shim.Error("Incorrect number of arguments. Expecting 5")

  }
  TUNA  := Tuna{Container:parameters[1], Location:parameters[2], Timestamp:parameters[3], Owner:parameters[4]}

  tunaAsBytes, _:=json.Marshal(TUNA)
  _error := APIstub.PutState(parameters[0], tunaAsBytes)
  if _error != nil {
    return shim.Error(fmt.Sprintf("Failed to record tuna catch: %s", parameters[0]))
  }
  return shim.Success(nil)

}

queryAllTuna - queryAllTuna方法允许评估所有记录;在这种情况下,所有金枪鱼记录都添加到分类帐中。此方法不带任何参数。它将返回包含结果的JSON字符串。

func (S_ *SmartContract) queryAllTuna(APIstub shim.ChaincodeStubInterface) sc.Response {

  init_key := "0"
  end_key := "999"
  results_iterator, _error := APIstub.GetStateByRange(init_key, end_key)

  if _error != nil {
        return shim.Error(_error.Error())
  }
  defer results_iterator.Close()
  var buffer bytes.Buffer
  buffer.WriteString("[")

  b_array_member_already_written := false
  for results_iterator.HasNext() {
    query_response, _error := results_iterator.Next()
    if _error != nil {
            return shim.Error(_error.Error())
        }

    if b_array_member_already_written == true {
      buffer.WriteString(",")
    }
    buffer.WriteString("{\"Key\":")
        buffer.WriteString("\"")
        buffer.WriteString(query_response.Key)
        buffer.WriteString("\"")

    buffer.WriteString(", \"Record\":")

    buffer.WriteString(string(query_response.Value))
        buffer.WriteString("}")
    b_array_member_already_written = true
  }

  buffer.WriteString("]")
  fmt.Printf("- queryAllTuna:\n%s\n", buffer.String())
  return shim.Success(buffer.Bytes())

}

主要功能

func main()  {
        _error := shim.Start(new(SmartContract))
        if _error != nil {
          fmt.Printf("Error creating new Smart Contract: %s", _error)

        }
     }

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用chaincode-docker-devmode的终端?

cd fabric-samples/chaincode-docker-devmode

Chaincode for Developers中一样 我忘了跳进资料库时遇到了同样的错误 并确保将版本放在CORE_CHAINCODE_ID_NAME中(例如:CORE_CHAINCODE_ID_NAME = mycc:0)