github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/committer/txvalidator/router.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package txvalidator
     8  
     9  import (
    10  	"github.com/hechain20/hechain/common/channelconfig"
    11  	"github.com/hyperledger/fabric-protos-go/common"
    12  )
    13  
    14  //go:generate mockery -dir . -name Validator -case underscore -output mocks
    15  
    16  // Validator defines API to validate transactions in a block
    17  type Validator interface {
    18  	// Validate returns an error if validation could not be performed successfully
    19  	// In case of successful validation, the block is modified to reflect the validity
    20  	// of the transactions it contains
    21  	Validate(block *common.Block) error
    22  }
    23  
    24  //go:generate mockery -dir . -name CapabilityProvider -case underscore -output mocks
    25  
    26  // CapabilityProvider contains functions to retrieve capability information for a channel
    27  type CapabilityProvider interface {
    28  	// Capabilities defines the capabilities for the application portion of this channel
    29  	Capabilities() channelconfig.ApplicationCapabilities
    30  }
    31  
    32  // ValidationRouter dynamically invokes the appropriate validator depending on the
    33  // capabilities that are currently enabled in the channel.
    34  type ValidationRouter struct {
    35  	CapabilityProvider
    36  	V20Validator Validator
    37  	V14Validator Validator
    38  }
    39  
    40  // Validate returns an error if validation could not be performed successfully
    41  // In case of successful validation, the block is modified to reflect the validity
    42  // of the transactions it contains
    43  func (v *ValidationRouter) Validate(block *common.Block) error {
    44  	switch {
    45  	case v.Capabilities().V2_0Validation():
    46  		return v.V20Validator.Validate(block)
    47  	default:
    48  		return v.V14Validator.Validate(block)
    49  	}
    50  }