gpt4 book ai didi

go - stub.GetCallerCertificate()、stub.GetCallerMetadata()、stub.GetPayload() 返回空白

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

package main

import (
"fmt"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
)

var matchLogger = shim.NewLogger("Helper")

type PlaceHolder struct {
ValA string `json:"ValA"`
ValB string `json:"ValB"`
Match bool `json:"Match"`
}

type Agreement struct {
TradeNumber PlaceHolder `json:"TradeNumber"`
BuyerName PlaceHolder `json:"BuyerName"`
SellerName PlaceHolder `json:"SellerName"`
BuyerID PlaceHolder `json:"BuyerID"`
SellerID PlaceHolder `json:"SellerID"`
}

type ContractContaineer struct {
FirmID string `json:"FirmID"`
ContractList []string `json:"contractList"`
}


func (t *Agreement) Init(stub shim.ChaincodeStubInterface) pb.Response {

xx, err1 := stub.GetCallerCertificate()
if err1 != nil {
matchLogger.Info(err1)
}
matchLogger.Info("Cert ----")
matchLogger.Info(string(xx))
matchLogger.Info("----")

xy, err3 := stub.GetCallerMetadata()
if err3 != nil {
matchLogger.Info(err3)
}
matchLogger.Info("Meta ----")
matchLogger.Info(string(xy))
matchLogger.Info("----")

yy, err4 := stub.GetPayload()
if err4 != nil {
matchLogger.Info(err4)
}
matchLogger.Info("PLD ----")
matchLogger.Info(string(yy))
matchLogger.Info("----")


return shim.Success(nil)
}


func (t *Agreement) Query(stub shim.ChaincodeStubInterface) pb.Response {
return shim.Success(nil)
}


// Transaction makes payment of X units from A to B
func (t *Agreement) Invoke(stub shim.ChaincodeStubInterface) pb.Response {

xx, err1 := stub.GetCallerCertificate()
if err1 != nil {
matchLogger.Info(err1)
}
matchLogger.Info("Cert ----")
matchLogger.Info(string(xx))
matchLogger.Info("----")

xy, err3 := stub.GetCallerMetadata()
if err3 != nil {
matchLogger.Info(err3)
}
matchLogger.Info("Meta ----")
matchLogger.Info(string(xy))
matchLogger.Info("----")

yy, err4 := stub.GetPayload()
if err4 != nil {
matchLogger.Info(err4)
}
matchLogger.Info("PLD ----")
matchLogger.Info(string(yy))
matchLogger.Info("----")

return shim.Success(nil)
}


func main() {

lld, _ := shim.LogLevel("INFO")
matchLogger.Info(lld)
matchLogger.SetLevel(lld)
matchLogger.Info(matchLogger.IsEnabledFor(lld))
err := shim.Start(new(Agreement))
if err != nil {
fmt.Printf("Error starting Simple chaincode: %s", err)
}
}

这是我完整的 GO 文件。请建议我做错了什么。 xx、xy、yy 的记录器输出为空白。错误也为零。我已经实现了所有 3 种方法,即初始化、调用和查询。代码编译得很好

最佳答案

如果您使用 https://github.com/hyperledger-archives/fabric/blob/master/core/chaincode/shim/chaincode.go#L628我怀疑 shim.ChainCodeStubInterface 在传入此方法之前未正确初始化。

ChainCodeStub 的每个实现都需要满足 this interface

type Chaincode interface {
// Init is called during Deploy transaction after the container has been
// established, allowing the chaincode to initialize its internal data
Init(stub *ChaincodeStub, function string, args []string) ([]byte, error)

// Invoke is called for every Invoke transactions. The chaincode may change
// its state variables
Invoke(stub *ChaincodeStub, function string, args []string) ([]byte, error)

// Query is called for Query transactions. The chaincode may only read
// (but not modify) its state variables and return the result
Query(stub *ChaincodeStub, function string, args []string) ([]byte, error)
}

Init 方法是我希望对 stub 进行初始化的地方,这样您的值就不会返回为空

关于go - stub.GetCallerCertificate()、stub.GetCallerMetadata()、stub.GetPayload() 返回空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43471031/

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