0

我正在尝试使用超级账本构建链代码。我正在使用 GoLang 编写合同,在构建合同时我面临以下错误:

    cannot refer to unexported name shim.success
    undefined: shim.success

可能很少有变量未定义的错误。由于我的代码没有构建,我无法调试代码。请找到我正在使用的代码。我无法找到上述错误的原因。请帮我解决这个问题。

   import (
    "encoding/json"
    "fmt"
    "bytes"
    "time"
    "strings"
    "github.com/hyperledger/fabric/core/chaincode/shim"
    pb "github.com/hyperledger/fabric/protos/peer"
   )


   func (t *check) SurrenderSaves(stub shim.ChaincodeStubInterface, 
   args []string) pb.Response {
   fmt.Println("Entering CodeSurrenderSaves")
   var err error
   var lastImportDatekey string
   var lastImportDate []byte

   lastImportDate, err= stub.GetState("lastImprtDatesaved")
   fmt.Println("lastImportDate ...... ", lastImportDate)

   err = json.Unmarshal(lastImportDate, &lastImportDatekey)
   if err != nil {
   fmt.Printf("Unable to unmarshal lastImportDate input 
   lastImportDatekey: %s\n", err)
    return shim.Error(err.Error())
   }
   fmt.Println("lastImportDatekey ...... ", lastImportDatekey)

   if (lastImportDate == nil){
      currentTime := time.Now()
      var timeString = currentTime.Format("2006-01-02")
      lastImportDatekey = timeString
      fmt.Println("lastImportDatekey ...... ", lastImportDatekey)
   } else {
      err = json.Unmarshal(lastImportDate, &lastImportDatekey)
      if err != nil {
        fmt.Printf("Unable to unmarshal lastImportDate input 
        lastImportDate: %s\n", err)
        return shim.Error(err.Error())
      }
      fmt.Println(" lastImportDatekey end:",lastImportDatekey)
   }
    return shim.Success(nil)
   }

   func (t *check) Init(stub shim.ChaincodeStubInterface) pb.Response {
      fmt.Println("Initiate the chaincde")
      return shim.Success(nil)
   }
   func (t *check) Invoke(stub shim.ChaincodeStubInterface) pb.Response 
   {
      function, args := stub.GetFunctionAndParameters()
       if function == "SurrenderSaves" {
          return t.SurrenderSaves(stub, args)
       }
   fmt.Println("Function not found")
   return shim.Error("Received unknown function invocation")
   return nil, nil
   }
4

1 回答 1

0

假设给定的代码是您的完整代码,我可以在您的代码中看到一些严重的问题。

  1. 检查结构不存在
  2. 获取状态信息后您没有处理错误(但这不会阻止您的程序构建)
  3. shim.success(nil)应替换为shim.Success(nil)[您编辑了您的问题并更正了此错误,但指出此错误很重要,因为它导致了您的问题标题中的错误]
  4. 有一条不必要的return nil, nil行会阻止您的程序正确构建。我不知道你为什么要把这条线放在那里。只需将其删除。
  5. 没有主函数

纠正所有这些错误后,您的代码应如下所示:

package main

import (
    "encoding/json"
    "fmt"
    "github.com/hyperledger/fabric/core/chaincode/shim"
    pb "github.com/hyperledger/fabric/protos/peer"
    "time"
)

//To-Do: Create check struct
type check struct {
}

func (t *check) SurrenderSaves(stub shim.ChaincodeStubInterface,
    args []string) pb.Response {
    fmt.Println("Entering CodeSurrenderSaves")
    var err error
    var lastImportDatekey string
    var lastImportDate []byte

    lastImportDate, err = stub.GetState("lastImprtDatesaved")

    //To-Do: handle error after getting state information
    if err != nil {
        fmt.Printf("Unable to get state : %s\n", err)
        return shim.Error(err.Error())
    }
    fmt.Println("lastImportDate ...... ", lastImportDate)

    err = json.Unmarshal(lastImportDate, &lastImportDatekey)
    if err != nil {
        fmt.Printf("Unable to unmarshal lastImportDate input lastImportDatekey: %s\n", err)
        return shim.Error(err.Error())
    }
    fmt.Println("lastImportDatekey ...... ", lastImportDatekey)

    if (lastImportDate == nil) {
        currentTime := time.Now()
        var timeString = currentTime.Format("2006-01-02")
        lastImportDatekey = timeString
        fmt.Println("lastImportDatekey ...... ", lastImportDatekey)
    } else {
        err = json.Unmarshal(lastImportDate, &lastImportDatekey)
        if err != nil {
            fmt.Printf("Unable to unmarshal lastImportDate input lastImportDate: %s\n", err)
            return shim.Error(err.Error())
        }
        fmt.Println(" lastImportDatekey end:", lastImportDatekey)
    }
    return shim.Success(nil) //To-Do: Success has to begin with uppercase S
}

func (t *check) Init(stub shim.ChaincodeStubInterface) pb.Response {
    fmt.Println("Initiate the chaincde")
    return shim.Success(nil)
}

func (t *check) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
    function, args := stub.GetFunctionAndParameters()
    if function == "SurrenderSaves" {
    return t.SurrenderSaves(stub, args)
    }
    fmt.Println("Function not found")
    return shim.Error("Received unknown function invocation")
    //return nil, nil //To-Do: Remove this line
}
//To-Do: Add main function
func main() {
    // Create a new Smart Contract
    err := shim.Start(new(check))
    if err != nil {
        fmt.Printf("Error creating new Smart Contract: %s", err)
    }
}

PS您必须在网络中安装新代码才能更新更改。为此,您必须停止并拆除网络,然后重新启动它。之后,您将能够在新创建的网络中安装更新版本的代码。

警告:如果没有针对键“lastImprtDatesaved”存储的值,此链代码将失败。因为当你打电话时,stub.GetState("lastImprtDatesaved")你会得到[]byte一个空字符串的表示。而且您不能json.Unmarshal(lastImportDate, &lastImportDatekey)在空字符串上运行,因为它将以错误结尾。

于 2018-11-23T09:51:53.340 回答