github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/cclifecycle/util.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package cclifecycle
     8  
     9  import (
    10  	"github.com/golang/protobuf/proto"
    11  	"github.com/hechain20/hechain/common/chaincode"
    12  	"github.com/hechain20/hechain/core/common/ccprovider"
    13  	"github.com/hechain20/hechain/core/common/privdata"
    14  	"github.com/pkg/errors"
    15  )
    16  
    17  // AcceptAll returns a predicate that accepts all Metadata
    18  var AcceptAll ChaincodePredicate = func(cc chaincode.Metadata) bool {
    19  	return true
    20  }
    21  
    22  // ChaincodePredicate accepts or rejects chaincode based on its metadata
    23  type ChaincodePredicate func(cc chaincode.Metadata) bool
    24  
    25  // DeployedChaincodes retrieves the metadata of the given deployed chaincodes
    26  func DeployedChaincodes(q Query, filter ChaincodePredicate, loadCollections bool, chaincodes ...string) (chaincode.MetadataSet, error) {
    27  	defer q.Done()
    28  
    29  	var res chaincode.MetadataSet
    30  	for _, cc := range chaincodes {
    31  		data, err := q.GetState("lscc", cc)
    32  		if err != nil {
    33  			Logger.Error("Failed querying lscc namespace:", err)
    34  			return nil, errors.WithStack(err)
    35  		}
    36  		if len(data) == 0 {
    37  			Logger.Info("Chaincode", cc, "isn't instantiated")
    38  			continue
    39  		}
    40  		ccInfo, err := extractCCInfo(data)
    41  		if err != nil {
    42  			Logger.Error("Failed extracting chaincode info about", cc, "from LSCC returned payload. Error:", err)
    43  			continue
    44  		}
    45  		if ccInfo.Name != cc {
    46  			Logger.Error("Chaincode", cc, "is listed in LSCC as", ccInfo.Name)
    47  			continue
    48  		}
    49  
    50  		instCC := chaincode.Metadata{
    51  			Name:    ccInfo.Name,
    52  			Version: ccInfo.Version,
    53  			Id:      ccInfo.Id,
    54  			Policy:  ccInfo.Policy,
    55  		}
    56  
    57  		if !filter(instCC) {
    58  			Logger.Debug("Filtered out", instCC)
    59  			continue
    60  		}
    61  
    62  		if loadCollections {
    63  			key := privdata.BuildCollectionKVSKey(cc)
    64  			collectionData, err := q.GetState("lscc", key)
    65  			if err != nil {
    66  				Logger.Errorf("Failed querying lscc namespace for %s: %v", key, err)
    67  				return nil, errors.WithStack(err)
    68  			}
    69  			ccp, err := privdata.ParseCollectionConfig(collectionData)
    70  			if err != nil {
    71  				Logger.Errorf("failed to parse collection config, error %s", err.Error())
    72  				return nil, errors.Wrapf(err, "failed to parse collection config")
    73  			}
    74  			instCC.CollectionsConfig = ccp
    75  			Logger.Debug("Retrieved collection config for", cc, "from", key)
    76  		}
    77  
    78  		res = append(res, instCC)
    79  	}
    80  	Logger.Debug("Returning", res)
    81  	return res, nil
    82  }
    83  
    84  func deployedCCToNameVersion(cc chaincode.Metadata) nameVersion {
    85  	return nameVersion{
    86  		name:    cc.Name,
    87  		version: cc.Version,
    88  	}
    89  }
    90  
    91  func extractCCInfo(data []byte) (*ccprovider.ChaincodeData, error) {
    92  	cd := &ccprovider.ChaincodeData{}
    93  	if err := proto.Unmarshal(data, cd); err != nil {
    94  		return nil, errors.Wrap(err, "failed unmarshalling lscc read value into ChaincodeData")
    95  	}
    96  	return cd, nil
    97  }
    98  
    99  type nameVersion struct {
   100  	name    string
   101  	version string
   102  }
   103  
   104  func installedCCToNameVersion(cc chaincode.InstalledChaincode) nameVersion {
   105  	return nameVersion{
   106  		name:    cc.Name,
   107  		version: cc.Version,
   108  	}
   109  }
   110  
   111  func names(installedChaincodes []chaincode.InstalledChaincode) []string {
   112  	var ccs []string
   113  	for _, cc := range installedChaincodes {
   114  		ccs = append(ccs, cc.Name)
   115  	}
   116  	return ccs
   117  }