2017-08-03 78 views
1

我一直在开发一个应用程序,涉及一个调用区块链链接的前端。chaincode如何将错误消息发送回来自哪个应用程序?

Chaincode为所有发送的事务返回OK消息。即使失败的交易也可以作为回应。尽管在区块链日志中可以看到错误。

有没有一种方法可以让链码将错误信​​息发送回前端以防发生错误,以便前端知道事务是否成功?

回答

2

Chaincode应符合下列API:

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

其中pb.Response是:

// 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垫片提供门面函数返回response.go定义的错误和成功状态。因此而implementting您chaincode流,你可以使用不同的反应类型信号和转发错误返回给客户端,为example

func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response { 
    fmt.Println("ex02 Invoke") 
    function, args := stub.GetFunctionAndParameters() 
    if function == "invoke" { 
     // Make payment of X units from A to B 
     return t.invoke(stub, args) 
    } else if function == "delete" { 
     // Deletes an entity from its state 
     return t.delete(stub, args) 
    } else if function == "query" { 
     // the old "Query" is now implemtned in invoke 
     return t.query(stub, args) 
    } 

    return shim.Error("Invalid invoke function name. Expecting \"invoke\" \"delete\" \"query\"") 
} 

将与错误回应,如果你会尝试调用与错误的参数chaincode组。稍后,您可以检查回复,以便在发生错误或未发生错误时查看,也可以使用消息扩展您的回复,以提供有关所发生情况的更多详细信息。

相关问题