[英]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. 通过在Hyperledger中运行我的Chaincode程序之一,我正在错误堆栈以下。 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 任何帮助将不胜感激,因为我当前正在使用Hyperledger v 1.0.0
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
在您的
func
您定义了一种返回类型: pb.Response
But in your return statement you are returning 2 values. 但是在您的return语句中,您将返回2个值。
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 要解决此问题,您可以更改函数的签名或return语句
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
I updated the signature and return statements of my functions and it worked perfectly fine. 我更新了函数的签名和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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.