github.com/pvitto98/fabric@v2.1.1+incompatible/common/chaincode/metadata.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package chaincode
     8  
     9  import (
    10  	"sync"
    11  
    12  	"github.com/hyperledger/fabric-protos-go/gossip"
    13  	"github.com/hyperledger/fabric-protos-go/peer"
    14  )
    15  
    16  // InstalledChaincode defines metadata about an installed chaincode
    17  type InstalledChaincode struct {
    18  	PackageID string
    19  	Hash      []byte
    20  	Label     string
    21  	// References is a map of channel name to chaincode
    22  	// metadata. This represents the channels and chaincode
    23  	// definitions that use this installed chaincode package.
    24  	References map[string][]*Metadata
    25  
    26  	// FIXME: we should remove these two
    27  	// fields since they are not properties
    28  	// of the chaincode (FAB-14561)
    29  	Name    string
    30  	Version string
    31  }
    32  
    33  // Metadata defines channel-scoped metadata of a chaincode
    34  type Metadata struct {
    35  	Name    string
    36  	Version string
    37  	Policy  []byte
    38  	// CollectionPolicies will only be set for _lifecycle
    39  	// chaincodes and stores a map from collection name to
    40  	// that collection's endorsement policy if one exists.
    41  	CollectionPolicies map[string][]byte
    42  	Id                 []byte
    43  	CollectionsConfig  *peer.CollectionConfigPackage
    44  	// These two fields (Approved, Installed) are only set for
    45  	// _lifecycle chaincodes. They are used to ensure service
    46  	// discovery doesn't publish a stale chaincode definition
    47  	// when the _lifecycle definition exists but has not yet
    48  	// been installed or approved by the peer's org.
    49  	Approved  bool
    50  	Installed bool
    51  }
    52  
    53  // MetadataSet defines an aggregation of Metadata
    54  type MetadataSet []Metadata
    55  
    56  // AsChaincodes converts this MetadataSet to a slice of gossip.Chaincodes
    57  func (ccs MetadataSet) AsChaincodes() []*gossip.Chaincode {
    58  	var res []*gossip.Chaincode
    59  	for _, cc := range ccs {
    60  		res = append(res, &gossip.Chaincode{
    61  			Name:    cc.Name,
    62  			Version: cc.Version,
    63  		})
    64  	}
    65  	return res
    66  }
    67  
    68  // MetadataMapping defines a mapping from chaincode name to Metadata
    69  type MetadataMapping struct {
    70  	sync.RWMutex
    71  	mdByName map[string]Metadata
    72  }
    73  
    74  // NewMetadataMapping creates a new metadata mapping
    75  func NewMetadataMapping() *MetadataMapping {
    76  	return &MetadataMapping{
    77  		mdByName: make(map[string]Metadata),
    78  	}
    79  }
    80  
    81  // Lookup returns the Metadata that is associated with the given chaincode
    82  func (m *MetadataMapping) Lookup(cc string) (Metadata, bool) {
    83  	m.RLock()
    84  	defer m.RUnlock()
    85  	md, exists := m.mdByName[cc]
    86  	return md, exists
    87  }
    88  
    89  // Update updates the chaincode metadata in the mapping
    90  func (m *MetadataMapping) Update(ccMd Metadata) {
    91  	m.Lock()
    92  	defer m.Unlock()
    93  	m.mdByName[ccMd.Name] = ccMd
    94  }
    95  
    96  // Aggregate aggregates all Metadata to a MetadataSet
    97  func (m *MetadataMapping) Aggregate() MetadataSet {
    98  	m.RLock()
    99  	defer m.RUnlock()
   100  	var set MetadataSet
   101  	for _, md := range m.mdByName {
   102  		set = append(set, md)
   103  	}
   104  	return set
   105  }