Blockchaindeveloper
Blockchaindeveloper

Reputation: 117

Chaincode in go - Hyperledger v 1.0 - too many arguments to return

I am getting below error stack by running one of my Chaincode program in Hyperledger. I am trying to build a small application that will insert a keyvalue pair of username and a status and using this I can insert or read values from the ledger:

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

.... This continues for all other functions in my code and final ones are below :

 ./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

My code is below :

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")
    }

}

Any help will be appreciated as I am currently using Hyperledger v 1.0.0

Upvotes: 0

Views: 581

Answers (2)

Blockchaindeveloper
Blockchaindeveloper

Reputation: 117

I updated the signature and return statements of my functions and it worked perfectly fine.

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)


}

Upvotes: 0

apxp
apxp

Reputation: 5914

The answer here is pretty simple. I explain it for the first error. The rest is similar.

In your func you are defining one return type: pb.Response

But in your return statement you are returning 2 values.

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

To fix this you can change the signature of your func or the return statement

Solution change the signature:

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

Solution change the return (then no error handling):

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

Upvotes: 1

Related Questions