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

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package valinforetriever
     8  
     9  import (
    10  	"github.com/golang/protobuf/proto"
    11  	"github.com/hechain20/hechain/core/committer/txvalidator/v20/plugindispatcher"
    12  	"github.com/hechain20/hechain/core/ledger"
    13  	"github.com/hyperledger/fabric-protos-go/common"
    14  	"github.com/hyperledger/fabric-protos-go/peer"
    15  )
    16  
    17  //go:generate mockery -dir . -name LifecycleResources -case underscore -output mocks/
    18  
    19  // LifecycleResources is the local interface that used to generate mocks for foreign interface.
    20  type LifecycleResources interface {
    21  	plugindispatcher.LifecycleResources
    22  }
    23  
    24  // ValidationInfoRetrieveShim implements plugindispatcher.LifecycleResource
    25  // by attempting to retrieve validation information from the two
    26  // supplied sources - a legacy source and a new source. The ValidationInfo
    27  // function will return info from the new source (if available) or
    28  // info from the legacy source
    29  type ValidationInfoRetrieveShim struct {
    30  	Legacy plugindispatcher.LifecycleResources
    31  	New    plugindispatcher.LifecycleResources
    32  }
    33  
    34  // ValidationInfo implements the function of the LifecycleResources interface.
    35  // It returns the name and arguments of the validation plugin for the supplied
    36  // chaincode. The function merely acts as a pass-through - returning validation
    37  // info from the new or the legacy lifecycle, unless the legacy lifecycle successfully
    38  // returns validation info requiring the built-in validation plugin and arguments
    39  // that can be successfully parsed as a SignaturePolicyEnvelope and that can be
    40  // successfully converted into ApplicationPolicy. In that case, and in that case
    41  // only does this function modify the return values from the underlying lifecycle.
    42  func (v *ValidationInfoRetrieveShim) ValidationInfo(channelID, chaincodeName string, qe ledger.SimpleQueryExecutor) (plugin string, args []byte, unexpectedErr error, validationErr error) {
    43  	plugin, args, unexpectedErr, validationErr = v.New.ValidationInfo(channelID, chaincodeName, qe)
    44  	if unexpectedErr != nil || validationErr != nil || plugin != "" || len(args) != 0 {
    45  		// in case of any errors or any actual information
    46  		// returned by the new lifecycle, we don't perform
    47  		// any translation
    48  		return
    49  	}
    50  
    51  	plugin, args, unexpectedErr, validationErr = v.Legacy.ValidationInfo(channelID, chaincodeName, qe)
    52  	if unexpectedErr != nil || validationErr != nil || plugin != "vscc" || len(args) == 0 {
    53  		// in case of any errors or in case the plugin is
    54  		// not the default one ("vscc") or we have an empty
    55  		// policy, we don't perform any translation
    56  		return
    57  	}
    58  
    59  	spe := &common.SignaturePolicyEnvelope{}
    60  	err := proto.Unmarshal(args, spe)
    61  	if err != nil {
    62  		// if we can't interpret the policy, we don't attempt
    63  		// any translation
    64  		return
    65  	}
    66  
    67  	newArgs, err := proto.Marshal(
    68  		&peer.ApplicationPolicy{
    69  			Type: &peer.ApplicationPolicy_SignaturePolicy{
    70  				SignaturePolicy: spe,
    71  			},
    72  		},
    73  	)
    74  	if err != nil {
    75  		// if we can't marshal the translated policy, we don't attempt
    76  		// any translation
    77  		return
    78  	}
    79  
    80  	// if marshalling is successful, we return the translated policy
    81  	args = newArgs
    82  	return
    83  }