我在构建自己的链代码时遇到了问题。
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结构中进行编译,但它成功了。但是,当我实例化链代码时,我收到了相同的消息,可能是因为我的区块链在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")
}
}
答案 0 :(得分:2)
答案 1 :(得分:2)
在版本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
答案 2 :(得分:0)
感谢分享,我比较了v0.6和v1.0.0 hyperledger结构示例。
v0.6 hyperledger fabric示例:
func (t *SampleChaincode) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
if function == "queryPatientInfo" {
return queryPatientInfo(stub, args)
}
return nil, nil
VS
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"
我只是觉得我应该分享这个:)