gpt4 book ai didi

go - go 中的链码 - Hyperledger v 1.0 - 返回的参数太多

转载 作者:数据小太阳 更新时间:2023-10-29 03:11:02 26 4
gpt4 key购买 nike

我在 Hyperledger 中运行我的 Chaincode 程序之一时遇到错误堆栈。我正在尝试构建一个小型应用程序,它将插入用户名和状态的键值对,并使用它我可以从分类帐中插入或读取值:

go build
# _/home/ubuntu/go/src/Chaincodeexample
./Samplesupplychain.go:28:9: too many arguments to return
have (nil, error)
want (peer.Response)

.... 这适用于我代码中的所有其他功能,最后的功能如下:

 ./Samplesupplychain.go:80:33: too many arguments in call to Checkuploadstatus
have (shim.ChaincodeStubInterface, []string)
want (shim.ChaincodeStubInterface)
./Samplesupplychain.go:90:26: too many arguments in call to CreateUser
have (shim.ChaincodeStubInterface, []string)
want (shim.ChaincodeStubInterface)
./Samplesupplychain.go:92:36: undefined: username
./Samplesupplychain.go:92:36: too many errors

我的代码如下:

package main

import(
"errors"
"fmt"
pb "github.com/hyperledger/fabric/protos/peer"

//"encoding/json"

"github.com/hyperledger/fabric/core/chaincode/shim"

)
type SampleChaincode struct {
}
var logger = shim.NewLogger("my logger")
//Create a struct for these 2 values
type testuser struct{
Username string `json:"username"`
Fileuploaded string `json:"fileuploaded"`
}

//A function to create user on the ledger

func CreateUser(stub shim.ChaincodeStubInterface) pb.Response {
function, args := stub.GetFunctionAndParameters()
if len(args) < 2 {
logger.Error("Invalid number of args")
return nil, errors.New("Expected atleast 1 argument for user creation")
}

var Username = args[0]
var UsernameInput = args[1]
//trying to understand
err := stub.PutState(Username, []byte(UsernameInput))
if err != nil {
logger.Error("Could not save new User to ledger", err)
return nil, err
}

var customEvent = "{eventType: 'UserCreation', description:" + Username + "' Successfully created'}"
err = stub.SetEvent("evtSender", []byte(customEvent))
if err != nil {
return nil, err
}
logger.Info("Successfully saved a supply chain user")
return nil, nil


}

func Checkuploadstatus(stub shim.ChaincodeStubInterface) pb.Response {
function, args := stub.GetFunctionAndParameters()
logger.Debug("Entering supplychain application")

if len(args) < 1 {
logger.Error("Invalid number of arguments")
return nil, errors.New("Missing details")
}

var Fileuploadedstatusforuser = args[0]

bytes, err := stub.GetState(Fileuploadedstatusforuser)
if err != nil {
logger.Error("Could not fetch Fileuploadedstatus with "+ Fileuploadedstatusforuser +" from ledger", err)
return nil, err
}
return bytes, nil
}

func (t *SampleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
function, args := stub.GetFunctionAndParameters()
return shim.Success(nil)

}

func (t *SampleChaincode) Query(stub shim.ChaincodeStubInterface) pb.Response {
function, args := stub.GetFunctionAndParameters()
if function == "Checkuploadstatus" {
return Checkuploadstatus(stub, args)
}
return shim.Success(nil)
}



func (t *SampleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
function, args := stub.GetFunctionAndParameters()
if function == "CreateUser" {
return CreateUser(stub, args)
} else {
return nil, errors.New(username + " does not have access to create a User")
}

return shim.Success(nil)
}

func main() {

lld, _ := shim.LogLevel("DEBUG")
fmt.Println(lld)

logger.SetLevel(lld)
fmt.Println(logger.IsEnabledFor(lld))

err := shim.Start(new(SampleChaincode))
if err != nil {
logger.Error("Could not start SampleChaincode")
} else {
logger.Info("SampleChaincode successfully started")
}

}

任何帮助将不胜感激,因为我目前正在使用 Hyperledger v 1.0.0

最佳答案

这里的答案很简单。我对第一个错误进行解释。其余类似。

在您的 func 中,您定义了一种返回类型:pb.Response

但在您的返回语句中,您返回了 2 个值。

func Checkuploadstatus(stub shim.ChaincodeStubInterface) pb.Response {
...
return nil, errors.New("Expected atleast 1 argument for user creation")

要解决此问题,您可以更改函数的签名或返回语句

解决方案更改签名:

func Checkuploadstatus(stub shim.ChaincodeStubInterface) (pb.Response, error) {
...
return nil, errors.New("Expected atleast 1 argument for user creation")

解决方案更改返回(然后没有错误处理):

func Checkuploadstatus(stub shim.ChaincodeStubInterface) pb.Response {
...
return nil

关于go - go 中的链码 - Hyperledger v 1.0 - 返回的参数太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51695402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com