Commit d8f86751 authored by Alessandro Sorniotti's avatar Alessandro Sorniotti Committed by Yacov Manevich
Browse files

[FAB-11552] Call right validator given the capability



Validation logic might change across releases. If this happens, the
validation logic is cloned, the default validation plugin instantiates
the validation logic for all releases that are still supported and
capabilities are used to determine which validation logic is used.

Change-Id: I8b87438feb84e981d750b66c285f82095e6f55d6
Signed-off-by: default avatarAlessandro Sorniotti <ale.linux@sopit.net>
Signed-off-by: default avatarMatthias Neugschwandtner <eug@zurich.ibm.com>
parent dbc194a9
......@@ -18,6 +18,7 @@ import (
. "github.com/hyperledger/fabric/core/handlers/validation/api/policies"
. "github.com/hyperledger/fabric/core/handlers/validation/api/state"
"github.com/hyperledger/fabric/core/handlers/validation/builtin/1.2"
"github.com/hyperledger/fabric/core/handlers/validation/builtin/1.3"
"github.com/hyperledger/fabric/protos/common"
"github.com/pkg/errors"
)
......@@ -32,7 +33,9 @@ func (*DefaultValidationFactory) New() validation.Plugin {
}
type DefaultValidation struct {
TxValidator TransactionValidator
Capabilities Capabilities
TxValidatorV1_2 TransactionValidator
TxValidatorV1_3 TransactionValidator
}
//go:generate mockery -dir . -name TransactionValidator -case underscore -output mocks/
......@@ -58,7 +61,19 @@ func (v *DefaultValidation) Validate(block *common.Block, namespace string, txPo
if block.Header == nil {
return errors.Errorf("no block header")
}
err := v.TxValidator.Validate(block, namespace, txPosition, actionPosition, serializedPolicy.Bytes())
var err error
switch {
case v.Capabilities.V1_3Validation():
err = v.TxValidatorV1_3.Validate(block, namespace, txPosition, actionPosition, serializedPolicy.Bytes())
case v.Capabilities.V1_2Validation():
fallthrough
default:
err = v.TxValidatorV1_2.Validate(block, namespace, txPosition, actionPosition, serializedPolicy.Bytes())
}
logger.Debugf("block %d, namespace: %s, tx %d validation results is: %v", block.Header.Number, namespace, txPosition, err)
return convertErrorTypeOrPanic(err)
}
......@@ -112,6 +127,10 @@ func (v *DefaultValidation) Init(dependencies ...validation.Dependency) error {
if pe == nil {
return errors.New("policy fetcher not passed in init")
}
v.TxValidator = builtin1_2.New(c, sf, d, pe)
v.Capabilities = c
v.TxValidatorV1_2 = builtin1_2.New(c, sf, d, pe)
v.TxValidatorV1_3 = builtin1_3.New(c, sf, d, pe)
return nil
}
......@@ -40,8 +40,10 @@ func TestInit(t *testing.T) {
func TestErrorConversion(t *testing.T) {
validator := &vmocks.TransactionValidator{}
capabilities := &mocks.Capabilities{}
validation := &DefaultValidation{
TxValidator: validator,
TxValidatorV1_2: validator,
Capabilities: capabilities,
}
block := &common.Block{
Header: &common.BlockHeader{},
......@@ -50,6 +52,9 @@ func TestErrorConversion(t *testing.T) {
},
}
capabilities.On("V1_3Validation").Return(false)
capabilities.On("V1_2Validation").Return(true)
// Scenario I: An error that isn't *commonerrors.ExecutionFailureError or *commonerrors.VSCCEndorsementPolicyError
// should cause a panic
validator.On("Validate", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(errors.New("bla bla")).Once()
......@@ -75,7 +80,7 @@ func TestErrorConversion(t *testing.T) {
func TestValidateBadInput(t *testing.T) {
validator := &vmocks.TransactionValidator{}
validation := &DefaultValidation{
TxValidator: validator,
TxValidatorV1_2: validator,
}
// Scenario I: Nil block
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment