github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+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  	Id                []byte
    39  	CollectionsConfig *peer.CollectionConfigPackage
    40  	// These two fields (Approved, Installed) are only set for
    41  	// _lifecycle chaincodes. They are used to ensure service
    42  	// discovery doesn't publish a stale chaincode definition
    43  	// when the _lifecycle definition exists but has not yet
    44  	// been installed or approved by the peer's org.
    45  	Approved  bool
    46  	Installed bool
    47  }
    48  
    49  // MetadataSet defines an aggregation of Metadata
    50  type MetadataSet []Metadata
    51  
    52  // AsChaincodes converts this MetadataSet to a slice of gossip.Chaincodes
    53  func (ccs MetadataSet) AsChaincodes() []*gossip.Chaincode {
    54  	var res []*gossip.Chaincode
    55  	for _, cc := range ccs {
    56  		res = append(res, &gossip.Chaincode{
    57  			Name:    cc.Name,
    58  			Version: cc.Version,
    59  		})
    60  	}
    61  	return res
    62  }
    63  
    64  // MetadataMapping defines a mapping from chaincode name to Metadata
    65  type MetadataMapping struct {
    66  	sync.RWMutex
    67  	mdByName map[string]Metadata
    68  }
    69  
    70  // NewMetadataMapping creates a new metadata mapping
    71  func NewMetadataMapping() *MetadataMapping {
    72  	return &MetadataMapping{
    73  		mdByName: make(map[string]Metadata),
    74  	}
    75  }
    76  
    77  // Lookup returns the Metadata that is associated with the given chaincode
    78  func (m *MetadataMapping) Lookup(cc string) (Metadata, bool) {
    79  	m.RLock()
    80  	defer m.RUnlock()
    81  	md, exists := m.mdByName[cc]
    82  	return md, exists
    83  }
    84  
    85  // Update updates the chaincode metadata in the mapping
    86  func (m *MetadataMapping) Update(ccMd Metadata) {
    87  	m.Lock()
    88  	defer m.Unlock()
    89  	m.mdByName[ccMd.Name] = ccMd
    90  }
    91  
    92  // Aggregate aggregates all Metadata to a MetadataSet
    93  func (m *MetadataMapping) Aggregate() MetadataSet {
    94  	m.RLock()
    95  	defer m.RUnlock()
    96  	var set MetadataSet
    97  	for _, md := range m.mdByName {
    98  		set = append(set, md)
    99  	}
   100  	return set
   101  }