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

标签 go hyperledger-fabric blockchain

我在 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/

相关文章:

blockchain - NEAR FunctionCall `args` 字段

go - 如何使用 go-playground/validator.v9 验证 time.Time 值?

go - 如何调用 golang struct 字段的函数?

go - 对所有文件使用 package main,仍然说未定义

hyperledger-fabric - 多个订购者组织

go - 如何在 Hyperledger Fabric 代码上运行测试?

java - 无法调用链码名称 :"lscc",错误 : transaction returned with failure: Undefined contract method called

node.js - 如何通过web3使用私钥登录以太坊账户?

json - 我可以在发布之前获取 http.NewRequest 的大小吗?

Java Hyperledger-panic : runtimr error: invalid memory address or nil pointer dereference