github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/ledger/kvledger/txmgmt/txmgr/collection_val.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package txmgr
     8  
     9  import (
    10  	"github.com/hechain20/hechain/core/ledger"
    11  	"github.com/hyperledger/fabric-protos-go/peer"
    12  )
    13  
    14  // collNameValidator validates the presence of a collection in a namespace
    15  // This is expected to be instantiated in the context of a simulator/queryexecutor
    16  type collNameValidator struct {
    17  	ledgerID       string
    18  	ccInfoProvider ledger.DeployedChaincodeInfoProvider
    19  	queryExecutor  *queryExecutor
    20  	cache          collConfigCache
    21  	noop           bool
    22  }
    23  
    24  func newCollNameValidator(ledgerID string, ccInfoProvider ledger.DeployedChaincodeInfoProvider, qe *queryExecutor, noop bool) *collNameValidator {
    25  	return &collNameValidator{ledgerID, ccInfoProvider, qe, make(collConfigCache), noop}
    26  }
    27  
    28  func (v *collNameValidator) validateCollName(ns, coll string) error {
    29  	if v.noop {
    30  		return nil
    31  	}
    32  	if !v.cache.isPopulatedFor(ns) {
    33  		conf, err := v.retrieveCollConfigFromStateDB(ns)
    34  		if err != nil {
    35  			return err
    36  		}
    37  		v.cache.populate(ns, conf)
    38  	}
    39  	if !v.cache.containsCollName(ns, coll) {
    40  		return &ledger.InvalidCollNameError{
    41  			Ns:   ns,
    42  			Coll: coll,
    43  		}
    44  	}
    45  	return nil
    46  }
    47  
    48  func (v *collNameValidator) retrieveCollConfigFromStateDB(ns string) (*peer.CollectionConfigPackage, error) {
    49  	logger.Debugf("retrieveCollConfigFromStateDB() begin - ns=[%s]", ns)
    50  	confPkg, err := v.ccInfoProvider.AllCollectionsConfigPkg(v.ledgerID, ns, v.queryExecutor)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	if confPkg == nil {
    55  		return nil, &ledger.CollConfigNotDefinedError{Ns: ns}
    56  	}
    57  	logger.Debugf("retrieveCollConfigFromStateDB() successfully retrieved - ns=[%s], confPkg=[%s]", ns, confPkg)
    58  	return confPkg, nil
    59  }
    60  
    61  type collConfigCache map[collConfigkey]bool
    62  
    63  type collConfigkey struct {
    64  	ns, coll string
    65  }
    66  
    67  func (c collConfigCache) populate(ns string, pkg *peer.CollectionConfigPackage) {
    68  	// an entry with an empty collection name to indicate that the cache is populated for the namespace 'ns'
    69  	// see function 'isPopulatedFor'
    70  	c[collConfigkey{ns, ""}] = true
    71  	for _, config := range pkg.Config {
    72  		sConfig := config.GetStaticCollectionConfig()
    73  		if sConfig == nil {
    74  			continue
    75  		}
    76  		c[collConfigkey{ns, sConfig.Name}] = true
    77  	}
    78  }
    79  
    80  func (c collConfigCache) isPopulatedFor(ns string) bool {
    81  	return c[collConfigkey{ns, ""}]
    82  }
    83  
    84  func (c collConfigCache) containsCollName(ns, coll string) bool {
    85  	return c[collConfigkey{ns, coll}]
    86  }