我正在尝试使用超级账本构建链码。我在构建合同时使用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
}
答案 0 :(得分:1)
假设给定的代码是您的完整代码,那么我会在您的代码中看到一些严重的问题。
shim.success(nil)
应该替换为shim.Success(nil)
[您编辑了问题并更正了此错误,但是指出该错误很重要,因为它导致了问题标题中的错误] return nil, nil
行,这会阻止程序正确构建。我不知道您为什么要把这条线放在那儿。只需将其删除即可。更正所有这些错误后,您的代码应如下所示:
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)
}
}
P.S。您必须在网络中安装新代码才能更新更改。为此,您必须停止并拆除网络,然后重新启动。之后,您将能够在新创建的网络中安装代码的更新版本。
警告::如果没有针对键“ lastImprtDatesaved”存储的值,则此链码将失败。因为当您调用stub.GetState("lastImprtDatesaved")
时,您将获得空字符串的[]byte
表示形式。而且您不能在空字符串上运行json.Unmarshal(lastImportDate, &lastImportDatekey)
,因为它将以错误结尾。