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

[FAB-11549] Split validation logic into own package



The validation logic needs to be into its own package. This way, different
versions of fabric can simply clone that package and make changes therein.
The benefits are that
1) the validation code from previous releases is immutable
2) new releases aren't forced to contain awkward case switches to handle
   different flavours of validation

Change-Id: If2f10922aeb5e9d95b359ed3794f674646937f0a
Signed-off-by: default avatarAlessandro Sorniotti <ale.linux@sopit.net>
Signed-off-by: default avatarMatthias Neugschwandtner <eug@zurich.ibm.com>
parent b3231445
......@@ -4,7 +4,7 @@ Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package builtin
package builtin1_2
import (
"bytes"
......@@ -46,15 +46,15 @@ const (
var validCollectionNameRegex = regexp.MustCompile(ccmetadata.AllowedCharsCollectionName)
//go:generate mockery -dir ../api/capabilities/ -name Capabilities -case underscore -output mocks/
//go:generate mockery -dir ../api/state/ -name StateFetcher -case underscore -output mocks/
//go:generate mockery -dir ../api/identities/ -name IdentityDeserializer -case underscore -output mocks/
//go:generate mockery -dir ../api/policies/ -name PolicyEvaluator -case underscore -output mocks/
//go:generate mockery -dir ../../api/capabilities/ -name Capabilities -case underscore -output mocks/
//go:generate mockery -dir ../../api/state/ -name StateFetcher -case underscore -output mocks/
//go:generate mockery -dir ../../api/identities/ -name IdentityDeserializer -case underscore -output mocks/
//go:generate mockery -dir ../../api/policies/ -name PolicyEvaluator -case underscore -output mocks/
// New creates a new instance of the default VSCC
// Typically this will only be invoked once per peer
func New(c Capabilities, s StateFetcher, d IdentityDeserializer, pe PolicyEvaluator) *ValidatorOneValidSignature {
return &ValidatorOneValidSignature{
func New(c Capabilities, s StateFetcher, d IdentityDeserializer, pe PolicyEvaluator) *Validator {
return &Validator{
capabilities: c,
stateFetcher: s,
deserializer: d,
......@@ -62,11 +62,11 @@ func New(c Capabilities, s StateFetcher, d IdentityDeserializer, pe PolicyEvalua
}
}
// ValidatorOneValidSignature implements the default transaction validation policy,
// Validator implements the default transaction validation policy,
// which is to check the correctness of the read-write set and the endorsement
// signatures against an endorsement policy that is supplied as argument to
// every invoke
type ValidatorOneValidSignature struct {
type Validator struct {
deserializer IdentityDeserializer
capabilities Capabilities
stateFetcher StateFetcher
......@@ -75,7 +75,7 @@ type ValidatorOneValidSignature struct {
// Validate validates the given envelope corresponding to a transaction with an endorsement
// policy as given in its serialized form
func (vscc *ValidatorOneValidSignature) Validate(
func (vscc *Validator) Validate(
block *common.Block,
namespace string,
txPosition int,
......@@ -150,7 +150,7 @@ func (vscc *ValidatorOneValidSignature) Validate(
}
// checkInstantiationPolicy evaluates an instantiation policy against a signed proposal
func (vscc *ValidatorOneValidSignature) checkInstantiationPolicy(chainName string, env *common.Envelope, instantiationPolicy []byte, payl *common.Payload) commonerrors.TxValidationError {
func (vscc *Validator) checkInstantiationPolicy(chainName string, env *common.Envelope, instantiationPolicy []byte, payl *common.Payload) commonerrors.TxValidationError {
// get the signature header
shdr, err := utils.GetSignatureHeader(payl.Header.SignatureHeader)
if err != nil {
......@@ -332,7 +332,7 @@ func validateCollectionName(collectionName string) error {
// validateRWSetAndCollection performs validation of the rwset
// of an LSCC deploy operation and then it validates any collection
// configuration
func (vscc *ValidatorOneValidSignature) validateRWSetAndCollection(
func (vscc *Validator) validateRWSetAndCollection(
lsccrwset *kvrwset.KVRWSet,
cdRWSet *ccprovider.ChaincodeData,
lsccArgs [][]byte,
......@@ -452,7 +452,7 @@ func (vscc *ValidatorOneValidSignature) validateRWSetAndCollection(
return nil
}
func (vscc *ValidatorOneValidSignature) ValidateLSCCInvocation(
func (vscc *Validator) ValidateLSCCInvocation(
chid string,
env *common.Envelope,
cap *pb.ChaincodeActionPayload,
......@@ -706,7 +706,7 @@ func (vscc *ValidatorOneValidSignature) ValidateLSCCInvocation(
}
}
func (vscc *ValidatorOneValidSignature) getInstantiatedCC(chid, ccid string) (cd *ccprovider.ChaincodeData, exists bool, err error) {
func (vscc *Validator) getInstantiatedCC(chid, ccid string) (cd *ccprovider.ChaincodeData, exists bool, err error) {
qe, err := vscc.stateFetcher.FetchState()
if err != nil {
err = fmt.Errorf("could not retrieve QueryExecutor for channel %s, error %s", chid, err)
......@@ -735,7 +735,7 @@ func (vscc *ValidatorOneValidSignature) getInstantiatedCC(chid, ccid string) (cd
return
}
func (vscc *ValidatorOneValidSignature) deduplicateIdentity(cap *pb.ChaincodeActionPayload) ([]*common.SignedData, error) {
func (vscc *Validator) deduplicateIdentity(cap *pb.ChaincodeActionPayload) ([]*common.SignedData, error) {
// this is the first part of the signed message
prespBytes := cap.Action.ProposalResponsePayload
......
......@@ -3,7 +3,7 @@ Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package builtin
package builtin1_2
import (
"archive/tar"
......@@ -32,7 +32,7 @@ import (
"github.com/hyperledger/fabric/core/common/privdata"
cutils "github.com/hyperledger/fabric/core/container/util"
"github.com/hyperledger/fabric/core/handlers/validation/api/capabilities"
"github.com/hyperledger/fabric/core/handlers/validation/builtin/mocks"
"github.com/hyperledger/fabric/core/handlers/validation/builtin/1.2/mocks"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/rwsetutil"
per "github.com/hyperledger/fabric/core/peer"
"github.com/hyperledger/fabric/core/policy"
......@@ -324,13 +324,13 @@ func getSignedByMSPAdminPolicy(mspID string) ([]byte, error) {
return b, err
}
func newValidationInstance(state map[string]map[string][]byte) *ValidatorOneValidSignature {
func newValidationInstance(state map[string]map[string][]byte) *Validator {
qec := &mocks2.QueryExecutorCreator{}
qec.On("NewQueryExecutor").Return(lm.NewMockQueryExecutor(state), nil)
return newCustomValidationInstance(qec, &mc.MockApplicationCapabilities{})
}
func newCustomValidationInstance(qec txvalidator.QueryExecutorCreator, c validation.Capabilities) *ValidatorOneValidSignature {
func newCustomValidationInstance(qec txvalidator.QueryExecutorCreator, c validation.Capabilities) *Validator {
sf := &txvalidator.StateFetcherImpl{QueryExecutorCreator: qec}
is := &mocks.IdentityDeserializer{}
pe := &txvalidator.PolicyEvaluator{
......@@ -366,7 +366,7 @@ func TestDeduplicateIdentity(t *testing.T) {
},
}
signedData, err := (&ValidatorOneValidSignature{}).deduplicateIdentity(chaincodeActionPayload)
signedData, err := (&Validator{}).deduplicateIdentity(chaincodeActionPayload)
assert.NoError(t, err)
// The original bytes of proposalResponsePayload are preserved
assert.Equal(t, proposalResponsePayload, signedData[0].Data[:len(proposalResponsePayload)])
......@@ -1792,7 +1792,7 @@ func createCollectionConfig(collectionName string, signaturePolicyEnvelope *comm
}
}
func testValidateCollection(t *testing.T, v *ValidatorOneValidSignature, collectionConfigs []*common.CollectionConfig, cdRWSet *ccprovider.ChaincodeData,
func testValidateCollection(t *testing.T, v *Validator, collectionConfigs []*common.CollectionConfig, cdRWSet *ccprovider.ChaincodeData,
lsccFunc string, ac channelconfig.ApplicationCapabilities, chid string,
) error {
ccp := &common.CollectionConfigPackage{Config: collectionConfigs}
......
......@@ -11,15 +11,19 @@ import (
"reflect"
commonerrors "github.com/hyperledger/fabric/common/errors"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/core/handlers/validation/api"
. "github.com/hyperledger/fabric/core/handlers/validation/api/capabilities"
. "github.com/hyperledger/fabric/core/handlers/validation/api/identities"
. "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/protos/common"
"github.com/pkg/errors"
)
var logger = flogging.MustGetLogger("vscc")
type DefaultValidationFactory struct {
}
......@@ -108,11 +112,6 @@ func (v *DefaultValidation) Init(dependencies ...validation.Dependency) error {
if pe == nil {
return errors.New("policy fetcher not passed in init")
}
v.TxValidator = &ValidatorOneValidSignature{
policyEvaluator: pe,
deserializer: d,
stateFetcher: sf,
capabilities: c,
}
v.TxValidator = builtin1_2.New(c, sf, d, pe)
return nil
}
......@@ -12,7 +12,8 @@ import (
commonerrors "github.com/hyperledger/fabric/common/errors"
"github.com/hyperledger/fabric/core/committer/txvalidator"
. "github.com/hyperledger/fabric/core/handlers/validation/api"
"github.com/hyperledger/fabric/core/handlers/validation/builtin/mocks"
"github.com/hyperledger/fabric/core/handlers/validation/builtin/1.2/mocks"
vmocks "github.com/hyperledger/fabric/core/handlers/validation/builtin/mocks"
"github.com/hyperledger/fabric/protos/common"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
......@@ -38,7 +39,7 @@ func TestInit(t *testing.T) {
}
func TestErrorConversion(t *testing.T) {
validator := &mocks.TransactionValidator{}
validator := &vmocks.TransactionValidator{}
validation := &DefaultValidation{
TxValidator: validator,
}
......@@ -72,7 +73,7 @@ func TestErrorConversion(t *testing.T) {
}
func TestValidateBadInput(t *testing.T) {
validator := &mocks.TransactionValidator{}
validator := &vmocks.TransactionValidator{}
validation := &DefaultValidation{
TxValidator: validator,
}
......
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