-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
metadata_manager.go
129 lines (107 loc) · 4.06 KB
/
metadata_manager.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package lifecycle
import (
"sync"
"github.com/hyperledger/fabric/common/chaincode"
)
// MetadataUpdateListener runs whenever there is a change to
// the metadata of a chaincode in the context of a specific
// channel.
type MetadataUpdateListener interface {
HandleMetadataUpdate(channel string, metadata chaincode.MetadataSet)
}
// HandleMetadataUpdateFunc is triggered upon a change in the
// chaincode lifecycle.
type HandleMetadataUpdateFunc func(channel string, metadata chaincode.MetadataSet)
// HandleMetadataUpdate runs whenever there is a change to
// the metadata of a chaincode in the context of a specific
// channel.
func (handleMetadataUpdate HandleMetadataUpdateFunc) HandleMetadataUpdate(channel string, metadata chaincode.MetadataSet) {
handleMetadataUpdate(channel, metadata)
}
// MetadataManager stores metadata about the chaincodes
// installed/deployed via _lifecycle (Metadaset) and
// lscc (LegacyMetadataSet) and updates any registered
// listeners upon a change in the metadata.
type MetadataManager struct {
mutex sync.Mutex
listeners []MetadataUpdateListener
LegacyMetadataSet map[string]chaincode.MetadataSet
MetadataSet map[string]chaincode.MetadataSet
}
func NewMetadataManager() *MetadataManager {
return &MetadataManager{
LegacyMetadataSet: map[string]chaincode.MetadataSet{},
MetadataSet: map[string]chaincode.MetadataSet{},
}
}
// HandleMetadataUpdate implements the function of the same
// name in the cclifecycle.LifecycleChangeListener interface.
// This function is called by the legacy lifecycle (lscc) to
// deliver updates so we aggregate them and propagate them to
// our listeners. This function is also called to initialise
// data structures right at peer startup time.
func (m *MetadataManager) HandleMetadataUpdate(channel string, metadata chaincode.MetadataSet) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.LegacyMetadataSet[channel] = metadata
m.fireListenersForChannel(channel)
}
// UpdateMetadata implements the function of the same name in
// the lifecycle.MetadataManager interface. This function is
// called by _lifecycle to deliver updates so we aggregate them
// and propagate them to our listeners.
func (m *MetadataManager) UpdateMetadata(channel string, metadata chaincode.MetadataSet) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.MetadataSet[channel] = metadata
m.fireListenersForChannel(channel)
}
// InitializeMetadata implements the function of the
// same name in the lifecycle.MetadataManager interface.
// This function is called by _lifecycle to initialize
// metadata for the given channel.
func (m *MetadataManager) InitializeMetadata(channel string, metadata chaincode.MetadataSet) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.MetadataSet[channel] = metadata
}
// AddListener registers the given listener to be triggered upon
// a lifecycle change
func (m *MetadataManager) AddListener(listener MetadataUpdateListener) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.listeners = append(m.listeners, listener)
}
// NOTE: caller must hold the mutex
func (m *MetadataManager) fireListenersForChannel(channel string) {
aggregatedMD := chaincode.MetadataSet{}
mdMapNewLifecycle := map[string]struct{}{}
for _, meta := range m.MetadataSet[channel] {
mdMapNewLifecycle[meta.Name] = struct{}{}
// in mdMapNewLifecycle we keep track of all
// metadata from the new lifecycle so that
// we can appropriately shadow any definition
// in the old lifecycle. However we return
// metadata to our caller only for chaincodes
// that are approved and locally installed,
// which is why we put metadata in the
// aggregatedMD slice only if it is approved
// and installed.
if meta.Installed && meta.Approved {
aggregatedMD = append(aggregatedMD, meta)
}
}
for _, meta := range m.LegacyMetadataSet[channel] {
if _, in := mdMapNewLifecycle[meta.Name]; in {
continue
}
aggregatedMD = append(aggregatedMD, meta)
}
for _, listener := range m.listeners {
listener.HandleMetadataUpdate(channel, aggregatedMD)
}
}