2017-08-03 76 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組。稍後,您可以檢查回覆,以便在發生錯誤或未發生錯誤時查看,也可以使用消息擴展您的回覆,以提供有關所發生情況的更多詳細信息。

相關問題