-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.go
83 lines (70 loc) · 1.8 KB
/
metadata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package chaincode
import (
"sync"
"github.com/hyperledger/fabric/protos/gossip"
)
// InstalledChaincode defines metadata about an installed chaincode
type InstalledChaincode struct {
Name string
Version string
Id []byte
}
// Metadata defines channel-scoped metadata of a chaincode
type Metadata struct {
Name string
Version string
Policy []byte
Id []byte
CollectionsConfig []byte
}
// MetadataSet defines an aggregation of Metadata
type MetadataSet []Metadata
// AsChaincodes converts this MetadataSet to a slice of gossip.Chaincodes
func (ccs MetadataSet) AsChaincodes() []*gossip.Chaincode {
var res []*gossip.Chaincode
for _, cc := range ccs {
res = append(res, &gossip.Chaincode{
Name: cc.Name,
Version: cc.Version,
})
}
return res
}
// MetadataMapping defines a mapping from chaincode name to Metadata
type MetadataMapping struct {
sync.RWMutex
mdByName map[string]Metadata
}
// NewMetadataMapping creates a new metadata mapping
func NewMetadataMapping() *MetadataMapping {
return &MetadataMapping{
mdByName: make(map[string]Metadata),
}
}
// Lookup returns the Metadata that is associated with the given chaincode
func (m *MetadataMapping) Lookup(cc string) (Metadata, bool) {
m.RLock()
defer m.RUnlock()
md, exists := m.mdByName[cc]
return md, exists
}
// Update updates the chaincode metadata in the mapping
func (m *MetadataMapping) Update(ccMd Metadata) {
m.Lock()
defer m.Unlock()
m.mdByName[ccMd.Name] = ccMd
}
// Aggregate aggregates all Metadata to a MetadataSet
func (m *MetadataMapping) Aggregate() MetadataSet {
m.RLock()
defer m.RUnlock()
var set MetadataSet
for _, md := range m.mdByName {
set = append(set, md)
}
return set
}