HyperledgerFabric v1.0.0 *SampleChaincode 未实现 shim.Chaincode

HyperledgerFabric v1.0.0 *SampleChaincode does not implement shim.Chaincode

我在构建自己的链代码时遇到了问题。

Hypeledger Fabric 版本 -v 1.0.0

>  cannot use new(SampleChaincode) (type *SampleChaincode) as type
> shim.Chaincode in argument to shim.Start:
>         *SampleChaincode does not implement shim.Chaincode (wrong type for Init method)
>                 have Init(shim.ChaincodeStubInterface, string, []string) ([]byte, error)
>                 want Init(shim.ChaincodeStubInterface) peer.Response

我已经尝试在 v0.6 fabric 中编译并且成功了。然而,当我实例化链代码时,我收到了相同的消息,可能是因为我的区块链是 运行 on v1.0.0

那么有什么办法可以解决这个问题吗??

这是我的代码

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

}

对,界面不一样。 v1.0 链码 interface 与 v0.6 不同。 请查看 following 示例

在版本 1.0.0 中,链代码的接口已更改为将响应封装在:

// A response with a representation similar to an HTTP response that can
// be used within another message.
type Response struct {
    // A status code that should follow the HTTP status codes.
    Status int32 `protobuf:"varint,1,opt,name=status" json:"status,omitempty"`
    // A message associated with the response code.
    Message string `protobuf:"bytes,2,opt,name=message" json:"message,omitempty"`
    // A payload that can be used to include metadata with this response.
    Payload []byte `protobuf:"bytes,3,opt,name=payload,proto3" json:"payload,omitempty"`
}

因此接口中函数的签名更改为:

// Chaincode interface must be implemented by all chaincodes. The fabric runs
// the transactions by calling these functions as specified.
type Chaincode interface {
    // Init is called during Instantiate transaction after the chaincode container
    // has been established for the first time, allowing the chaincode to
    // initialize its internal data
    Init(stub ChaincodeStubInterface) pb.Response

    // Invoke is called to update or query the ledger in a proposal transaction.
    // Updated state variables are not committed to the ledger until the
    // transaction is committed.
    Invoke(stub ChaincodeStubInterface) pb.Response
}

因此您收到错误消息:

cannot use new(SampleChaincode) (type *SampleChaincode) as type shim.Chaincode in argument to shim.Start: *SampleChaincode does not implement shim.Chaincode (wrong type for Init method) have Init(shim.ChaincodeStubInterface, string, []string) ([]byte, error) want Init(shim.ChaincodeStubInterface) peer.Response

感谢分享,我比较了 v0.6 和 v1.0.0 hyperledger fabric 示例。

v0.6 超级账本结构示例:

func (t *SampleChaincode) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
if function == "queryPatientInfo" {
    return queryPatientInfo(stub, args)
}
return nil, nil

v1.0.0 hyperledger fabric 示例:

func (t *SampleChaincode) Query(stub shim.ChaincodeStubInterface) pb.Response {
function, args := stub.GetFunctionAndParameters()
if function == "queryPatientInfo" {
    return queryPatientInfo(stub, args)
}
return shim.Success(nil)
}

同时导入:

pb "github.com/hyperledger/fabric/protos/peer"

我只是觉得我应该分享这个:)