Skip to content

Commit 51a606b

Browse files
author
Srinivasan Muralidharan
committed
[FAB-4347] prop resp to return error on cc error
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 <[email protected]>
1 parent a959653 commit 51a606b

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

core/endorser/endorser.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ import (
4444
putils "github.com/hyperledger/fabric/protos/utils"
4545
)
4646

47+
// >>>>> begin errors section >>>>>
48+
//chaincodeError is a fabric error signifying error from chaincode
49+
type chaincodeError struct {
50+
status int32
51+
msg string
52+
}
53+
54+
func (ce chaincodeError) Error() string {
55+
return fmt.Sprintf("chaincode error (status: %d, message: %s)", ce.status, ce.msg)
56+
}
57+
58+
// <<<<< end errors section <<<<<<
59+
4760
var endorserLogger = flogging.MustGetLogger("endorser")
4861

4962
// The Jira issue that documents Endorser flow along with its relationship to
@@ -477,7 +490,7 @@ func (e *Endorser) ProcessProposal(ctx context.Context, signedProp *pb.SignedPro
477490
}
478491
if res != nil {
479492
if res.Status >= shim.ERROR {
480-
endorserLogger.Debugf("simulateProposal() resulted in chaincode error for txid: %s", txid)
493+
endorserLogger.Errorf("simulateProposal() resulted in chaincode response status %d for txid: %s", res.Status, txid)
481494
var cceventBytes []byte
482495
if ccevent != nil {
483496
cceventBytes, err = putils.GetBytesChaincodeEvent(ccevent)
@@ -489,7 +502,8 @@ func (e *Endorser) ProcessProposal(ctx context.Context, signedProp *pb.SignedPro
489502
if err != nil {
490503
return &pb.ProposalResponse{Response: &pb.Response{Status: 500, Message: err.Error()}}, err
491504
}
492-
return pResp, nil
505+
506+
return pResp, &chaincodeError{res.Status, res.Message}
493507
}
494508
}
495509

@@ -508,7 +522,7 @@ func (e *Endorser) ProcessProposal(ctx context.Context, signedProp *pb.SignedPro
508522
if pResp != nil {
509523
if res.Status >= shim.ERROR {
510524
endorserLogger.Debugf("endorseProposal() resulted in chaincode error for txid: %s", txid)
511-
return pResp, nil
525+
return pResp, &chaincodeError{res.Status, res.Message}
512526
}
513527
}
514528
}

core/endorser/endorser_test.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func invoke(chainID string, spec *pb.ChaincodeSpec) (*pb.Proposal, *pb.ProposalR
288288

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

294294
return prop, resp, txID, nonce, err
@@ -484,12 +484,18 @@ func TestDeployAndInvoke(t *testing.T) {
484484
invokeArgs = append([]string{f}, args...)
485485
spec = &pb.ChaincodeSpec{Type: 1, ChaincodeId: chaincodeID, Input: &pb.ChaincodeInput{Args: util.ToChaincodeArgs(invokeArgs...)}}
486486
prop, resp, txid, nonce, err = invoke(chainID, spec)
487-
if err != nil {
487+
if err == nil {
488488
t.Fail()
489-
t.Logf("Error invoking transaction: %s", err)
489+
t.Logf("expecting fabric to report error from chaincode failure")
490+
chaincode.GetChain().Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID}})
491+
return
492+
} else if _, ok := err.(*chaincodeError); !ok {
493+
t.Fail()
494+
t.Logf("expecting chaincode error but found %v", err)
490495
chaincode.GetChain().Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID}})
491496
return
492497
}
498+
493499
if resp != nil {
494500
assert.Equal(t, int32(500), resp.Response.Status, "Unexpected response status")
495501
}

0 commit comments

Comments
 (0)