Skip to content

Commit

Permalink
FAB-2194 unmarshal Response only when resp. is from CC
Browse files Browse the repository at this point in the history
https://jira.hyperledger.org/browse/FAB-2194

Look for pb.Response only on pb.ChaincodeMessage_COMPLETED. Others are
non-CC errors and will not carry response from chaincode.

Change-Id: I24160a643dd09089f0e2f5cf694e620cde29b0fc
Signed-off-by: Srinivasan Muralidharan <muralisr@us.ibm.com>
  • Loading branch information
Srinivasan Muralidharan committed Feb 11, 2017
1 parent d58349a commit 70a8b01
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions core/chaincode/exectransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,28 @@ func Execute(ctxt context.Context, cccid *ccprovider.CCContext, spec interface{}
// Rollback transaction
return nil, nil, fmt.Errorf("Failed to receive a response for (%s)", cccid.TxID)
}
res := &pb.Response{}
unmarshalErr := proto.Unmarshal(resp.Payload, res)
if unmarshalErr != nil {
return nil, nil, fmt.Errorf("Failed to unmarshal response for (%s): %s", cccid.TxID, unmarshalErr)
}

if resp.ChaincodeEvent != nil {
resp.ChaincodeEvent.ChaincodeId = cccid.Name
resp.ChaincodeEvent.TxId = cccid.TxID
}

if resp.Type == pb.ChaincodeMessage_COMPLETED {
res := &pb.Response{}
unmarshalErr := proto.Unmarshal(resp.Payload, res)
if unmarshalErr != nil {
return nil, nil, fmt.Errorf("Failed to unmarshal response for (%s): %s", cccid.TxID, unmarshalErr)
}

// Success
return res, resp.ChaincodeEvent, nil
} else if resp.Type == pb.ChaincodeMessage_ERROR {
// Rollback transaction
return nil, resp.ChaincodeEvent, fmt.Errorf("Transaction returned with failure: %s", string(resp.Payload))
}

return res, nil, fmt.Errorf("receive a response for (%s) but in invalid state(%d)", cccid.TxID, resp.Type)
//TODO - this should never happen ... a panic is more appropriate but will save that for future
return nil, nil, fmt.Errorf("receive a response for (%s) but in invalid state(%d)", cccid.TxID, resp.Type)
}

// ExecuteWithErrorFilter is similar to Execute, but filters error contained in chaincode response and returns Payload of response only.
Expand Down

0 comments on commit 70a8b01

Please sign in to comment.