Go中的Chaincode-Hyperledger v 1.0-太多参数无法返回

时间:2018-08-05 14:57:06

标签: 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

2 个答案:

答案 0 :(得分:1)

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

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

但是在您的return语句中,您将返回2个值。

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

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

解决方案更改签名:

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

答案 1 :(得分:0)

我更新了函数的签名和return语句,并且工作得很好。

func CreateUser(stub shim.ChaincodeStubInterface, args []string) pb.Response {
        if len(args) < 2 {
                logger.Error("Invalid number of args")
                return shim.Error("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 shim.Error(err.Error())
        }

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


}