Skip to content

Commit e7b20bd

Browse files
Srinivasan MuralidharanC0rWin
Srinivasan Muralidharan
authored andcommitted
[FAB-5458] wrong type check in validator
Validator is sending peer.TxValidationCode_INVALID_OTHER_REASON instead of peer.TxValidationCode_ENDORSEMENT_POLICY_FAILURE due to a wrong type check. Added UT to cover the switch and ensure correct tx flag been assigned. Change-Id: I7abe8828323780d527b24905f54d9a06e5f70b4e Signed-off-by: Srinivasan Muralidharan <[email protected]> Signed-off-by: Artem Barger <[email protected]>
1 parent e5b46d1 commit e7b20bd

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

core/committer/txvalidator/validator.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ func (v *vsccValidatorImpl) VSCCValidateTx(payload *common.Payload, envBytes []b
557557
// do VSCC validation
558558
if err = v.VSCCValidateTxForCC(envBytes, chdr.TxId, chdr.ChannelId, vscc.ChaincodeName, vscc.ChaincodeVersion, policy); err != nil {
559559
switch err.(type) {
560-
case VSCCEndorsementPolicyError:
560+
case *VSCCEndorsementPolicyError:
561561
return err, peer.TxValidationCode_ENDORSEMENT_POLICY_FAILURE
562562
default:
563563
return err, peer.TxValidationCode_INVALID_OTHER_REASON
@@ -588,7 +588,7 @@ func (v *vsccValidatorImpl) VSCCValidateTx(payload *common.Payload, envBytes []b
588588
// they have to modify VSCC to provide appropriate validation
589589
if err = v.VSCCValidateTxForCC(envBytes, chdr.TxId, vscc.ChainID, vscc.ChaincodeName, vscc.ChaincodeVersion, policy); err != nil {
590590
switch err.(type) {
591-
case VSCCEndorsementPolicyError:
591+
case *VSCCEndorsementPolicyError:
592592
return err, peer.TxValidationCode_ENDORSEMENT_POLICY_FAILURE
593593
default:
594594
return err, peer.TxValidationCode_INVALID_OTHER_REASON

core/committer/txvalidator/validator_test.go

+63-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/hyperledger/fabric/common/ledger/testutil"
2929
"github.com/hyperledger/fabric/common/mocks/scc"
3030
"github.com/hyperledger/fabric/common/util"
31+
"github.com/hyperledger/fabric/core/chaincode/shim"
3132
ccp "github.com/hyperledger/fabric/core/common/ccprovider"
3233
"github.com/hyperledger/fabric/core/common/sysccprovider"
3334
"github.com/hyperledger/fabric/core/ledger"
@@ -531,12 +532,73 @@ func TestLedgerIsNoAvailable(t *testing.T) {
531532
assertion.NotNil(err.(*VSCCInfoLookupFailureError))
532533
}
533534

535+
func TestValidationInvalidEndorsing(t *testing.T) {
536+
theLedger := new(mockLedger)
537+
validator := NewTxValidator(&mockSupport{l: theLedger})
538+
539+
ccID := "mycc"
540+
tx := getEnv(ccID, createRWset(t, ccID), t)
541+
542+
theLedger.On("GetTransactionByID", mock.Anything).Return(&peer.ProcessedTransaction{}, errors.New("Cannot find the transaction"))
543+
544+
cd := &ccp.ChaincodeData{
545+
Name: ccID,
546+
Version: ccVersion,
547+
Vscc: "vscc",
548+
Policy: signedByAnyMember([]string{"DEFAULT"}),
549+
}
550+
551+
cdbytes := utils.MarshalOrPanic(cd)
552+
553+
queryExecutor := new(mockQueryExecutor)
554+
queryExecutor.On("GetState", "lscc", ccID).Return(cdbytes, nil)
555+
theLedger.On("NewQueryExecutor", mock.Anything).Return(queryExecutor, nil)
556+
557+
b := &common.Block{Data: &common.BlockData{Data: [][]byte{utils.MarshalOrPanic(tx)}}}
558+
559+
// Keep default callback
560+
c := executeChaincodeProvider.getCallback()
561+
executeChaincodeProvider.setCallback(func() (*peer.Response, *peer.ChaincodeEvent, error) {
562+
return &peer.Response{Status: shim.ERROR}, nil, nil
563+
})
564+
err := validator.Validate(b)
565+
// Restore default callback
566+
executeChaincodeProvider.setCallback(c)
567+
assert.NoError(t, err)
568+
assertInvalid(b, t, peer.TxValidationCode_ENDORSEMENT_POLICY_FAILURE)
569+
}
570+
571+
type ccResultCallback func() (*peer.Response, *peer.ChaincodeEvent, error)
572+
573+
type ccExecuteChaincode struct {
574+
executeChaincodeCalback ccResultCallback
575+
}
576+
577+
func (cc *ccExecuteChaincode) ExecuteChaincodeResult() (*peer.Response, *peer.ChaincodeEvent, error) {
578+
return cc.executeChaincodeCalback()
579+
}
580+
581+
func (cc *ccExecuteChaincode) getCallback() ccResultCallback {
582+
return cc.executeChaincodeCalback
583+
}
584+
585+
func (cc *ccExecuteChaincode) setCallback(calback ccResultCallback) {
586+
cc.executeChaincodeCalback = calback
587+
}
588+
534589
var signer msp.SigningIdentity
590+
535591
var signerSerialized []byte
536592

593+
var executeChaincodeProvider = &ccExecuteChaincode{
594+
executeChaincodeCalback: func() (*peer.Response, *peer.ChaincodeEvent, error) {
595+
return &peer.Response{Status: shim.OK}, nil, nil
596+
},
597+
}
598+
537599
func TestMain(m *testing.M) {
538600
sysccprovider.RegisterSystemChaincodeProviderFactory(&scc.MocksccProviderFactory{})
539-
ccp.RegisterChaincodeProviderFactory(&ccprovider.MockCcProviderFactory{})
601+
ccp.RegisterChaincodeProviderFactory(&ccprovider.MockCcProviderFactory{executeChaincodeProvider})
540602

541603
msptesttools.LoadMSPSetupForTesting()
542604

core/mocks/ccprovider/ccprovider.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,24 @@ import (
2525
"github.com/hyperledger/fabric/protos/peer"
2626
)
2727

28+
type ExecuteChaincodeResultProvider interface {
29+
ExecuteChaincodeResult() (*peer.Response, *peer.ChaincodeEvent, error)
30+
}
31+
2832
// MockCcProviderFactory is a factory that returns
2933
// mock implementations of the ccprovider.ChaincodeProvider interface
3034
type MockCcProviderFactory struct {
35+
ExecuteResultProvider ExecuteChaincodeResultProvider
3136
}
3237

3338
// NewChaincodeProvider returns a mock implementation of the ccprovider.ChaincodeProvider interface
3439
func (c *MockCcProviderFactory) NewChaincodeProvider() ccprovider.ChaincodeProvider {
35-
return &mockCcProviderImpl{}
40+
return &mockCcProviderImpl{c.ExecuteResultProvider}
3641
}
3742

3843
// mockCcProviderImpl is a mock implementation of the chaincode provider
3944
type mockCcProviderImpl struct {
45+
executeResultProvider ExecuteChaincodeResultProvider
4046
}
4147

4248
type mockCcProviderContextImpl struct {
@@ -59,6 +65,9 @@ func (c *mockCcProviderImpl) GetCCValidationInfoFromLSCC(ctxt context.Context, t
5965

6066
// ExecuteChaincode does nothing
6167
func (c *mockCcProviderImpl) ExecuteChaincode(ctxt context.Context, cccid interface{}, args [][]byte) (*peer.Response, *peer.ChaincodeEvent, error) {
68+
if c.executeResultProvider != nil {
69+
return c.executeResultProvider.ExecuteChaincodeResult()
70+
}
6271
return &peer.Response{Status: shim.OK}, nil, nil
6372
}
6473

0 commit comments

Comments
 (0)