Skip to content

Commit

Permalink
[FAB-4347] prop resp to return error on cc error
Browse files Browse the repository at this point in the history
CC responses that are greater than an threshold (currently 500, soon
to be 400) will not be endorsed. As the error is from chaincode and not
directly from fabric, endorser returns error. However it makes sense to
return a special 500 error with message, "chaincode error" to signify that
the response was not endorsed.

SDK and users could deep inspect inner response from chaincode for more
information.

Change-Id: If0ba17908576791bd1933bec17a65db8698f8aef
Signed-off-by: Srinivasan Muralidharan <muralisr@us.ibm.com>
  • Loading branch information
Srinivasan Muralidharan committed Jun 4, 2017
1 parent a959653 commit 51a606b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
20 changes: 17 additions & 3 deletions core/endorser/endorser.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ import (
putils "github.com/hyperledger/fabric/protos/utils"
)

// >>>>> begin errors section >>>>>
//chaincodeError is a fabric error signifying error from chaincode
type chaincodeError struct {
status int32
msg string
}

func (ce chaincodeError) Error() string {
return fmt.Sprintf("chaincode error (status: %d, message: %s)", ce.status, ce.msg)
}

// <<<<< end errors section <<<<<<

var endorserLogger = flogging.MustGetLogger("endorser")

// The Jira issue that documents Endorser flow along with its relationship to
Expand Down Expand Up @@ -477,7 +490,7 @@ func (e *Endorser) ProcessProposal(ctx context.Context, signedProp *pb.SignedPro
}
if res != nil {
if res.Status >= shim.ERROR {
endorserLogger.Debugf("simulateProposal() resulted in chaincode error for txid: %s", txid)
endorserLogger.Errorf("simulateProposal() resulted in chaincode response status %d for txid: %s", res.Status, txid)
var cceventBytes []byte
if ccevent != nil {
cceventBytes, err = putils.GetBytesChaincodeEvent(ccevent)
Expand All @@ -489,7 +502,8 @@ func (e *Endorser) ProcessProposal(ctx context.Context, signedProp *pb.SignedPro
if err != nil {
return &pb.ProposalResponse{Response: &pb.Response{Status: 500, Message: err.Error()}}, err
}
return pResp, nil

return pResp, &chaincodeError{res.Status, res.Message}
}
}

Expand All @@ -508,7 +522,7 @@ func (e *Endorser) ProcessProposal(ctx context.Context, signedProp *pb.SignedPro
if pResp != nil {
if res.Status >= shim.ERROR {
endorserLogger.Debugf("endorseProposal() resulted in chaincode error for txid: %s", txid)
return pResp, nil
return pResp, &chaincodeError{res.Status, res.Message}
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions core/endorser/endorser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func invoke(chainID string, spec *pb.ChaincodeSpec) (*pb.Proposal, *pb.ProposalR

resp, err := endorserServer.ProcessProposal(context.Background(), signedProp)
if err != nil {
return nil, nil, "", nil, fmt.Errorf("Error endorsing %s: %s\n", spec.ChaincodeId, err)
return nil, nil, "", nil, err
}

return prop, resp, txID, nonce, err
Expand Down Expand Up @@ -484,12 +484,18 @@ func TestDeployAndInvoke(t *testing.T) {
invokeArgs = append([]string{f}, args...)
spec = &pb.ChaincodeSpec{Type: 1, ChaincodeId: chaincodeID, Input: &pb.ChaincodeInput{Args: util.ToChaincodeArgs(invokeArgs...)}}
prop, resp, txid, nonce, err = invoke(chainID, spec)
if err != nil {
if err == nil {
t.Fail()
t.Logf("Error invoking transaction: %s", err)
t.Logf("expecting fabric to report error from chaincode failure")
chaincode.GetChain().Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID}})
return
} else if _, ok := err.(*chaincodeError); !ok {
t.Fail()
t.Logf("expecting chaincode error but found %v", err)
chaincode.GetChain().Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID}})
return
}

if resp != nil {
assert.Equal(t, int32(500), resp.Response.Status, "Unexpected response status")
}
Expand Down

0 comments on commit 51a606b

Please sign in to comment.