2017-08-12 60 views
1

我在构建自己的链式代码时遇到了问题。HyperledgerFabric v1.0.0 * SampleChaincode没有实现shim.Chaincode

Hypeledger面料版本1.0.0 -v

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

} 

回答

2

在1.0.0版本chaincode的接口已更改为封装内的响应:

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

0

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

V0.6 hyperledger织物例如:

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织物例如:

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" 

我只是觉得我应该分享一下:)