Hyperledger Fabric Chaincode Error : cannot refer to unexported name shim.success
Hyperledger Fabric Chaincode Error : cannot refer to unexported name shim.success
我正在尝试使用 hyperledger 构建链代码。我正在使用 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
}
假设给定的代码是您的完整代码,我可以看出您的代码中存在一些严重的问题。
- 检查结构不存在
- 您在获取状态信息后没有处理错误(但这不会阻止您的程序构建)
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)
空字符串,因为它会以错误结束。
我正在尝试使用 hyperledger 构建链代码。我正在使用 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
}
假设给定的代码是您的完整代码,我可以看出您的代码中存在一些严重的问题。
- 检查结构不存在
- 您在获取状态信息后没有处理错误(但这不会阻止您的程序构建)
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)
空字符串,因为它会以错误结束。