forked from tw-bc-group/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
262 lines (214 loc) · 6.93 KB
/
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
Copyright IBM Corp. 2017 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package configtx
import (
"fmt"
"reflect"
"regexp"
"github.com/hyperledger/fabric/common/configtx/api"
"github.com/hyperledger/fabric/common/flogging"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"
)
var logger = flogging.MustGetLogger("common/configtx")
// Constraints for valid chain IDs
var (
allowedChars = "[a-zA-Z0-9.-]+"
maxLength = 249
illegalNames = map[string]struct{}{
".": struct{}{},
"..": struct{}{},
}
)
type configSet struct {
channelID string
sequence uint64
configMap map[string]comparable
configEnv *cb.ConfigEnvelope
}
type configManager struct {
api.Resources
callOnUpdate []func(api.Manager)
initializer api.Initializer
current *configSet
}
// validateChainID makes sure that proposed chain IDs (i.e. channel names)
// comply with the following restrictions:
// 1. Contain only ASCII alphanumerics, dots '.', dashes '-'
// 2. Are shorter than 250 characters.
// 3. Are not the strings "." or "..".
//
// Our hand here is forced by:
// https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/common/Topic.scala#L29
func validateChainID(chainID string) error {
re, _ := regexp.Compile(allowedChars)
// Length
if len(chainID) <= 0 {
return fmt.Errorf("chain ID illegal, cannot be empty")
}
if len(chainID) > maxLength {
return fmt.Errorf("chain ID illegal, cannot be longer than %d", maxLength)
}
// Illegal name
if _, ok := illegalNames[chainID]; ok {
return fmt.Errorf("name '%s' for chain ID is not allowed", chainID)
}
// Illegal characters
matched := re.FindString(chainID)
if len(matched) != len(chainID) {
return fmt.Errorf("Chain ID '%s' contains illegal characters", chainID)
}
return nil
}
func NewManagerImpl(envConfig *cb.Envelope, initializer api.Initializer, callOnUpdate []func(api.Manager)) (api.Manager, error) {
if envConfig == nil {
return nil, fmt.Errorf("Nil envelope")
}
configEnv := &cb.ConfigEnvelope{}
header, err := utils.UnmarshalEnvelopeOfType(envConfig, cb.HeaderType_CONFIG, configEnv)
if err != nil {
return nil, fmt.Errorf("Bad envelope: %s", err)
}
if configEnv.Config == nil {
return nil, fmt.Errorf("Nil config envelope Config")
}
if err := validateChainID(header.ChannelId); err != nil {
return nil, fmt.Errorf("Bad channel id: %s", err)
}
configMap, err := MapConfig(configEnv.Config.ChannelGroup)
if err != nil {
return nil, fmt.Errorf("Error converting config to map: %s", err)
}
cm := &configManager{
Resources: initializer,
initializer: initializer,
current: &configSet{
sequence: configEnv.Config.Sequence,
configMap: configMap,
channelID: header.ChannelId,
configEnv: configEnv,
},
callOnUpdate: callOnUpdate,
}
result, err := cm.processConfig(configEnv.Config.ChannelGroup)
if err != nil {
return nil, err
}
result.commit()
cm.commitCallbacks()
return cm, nil
}
func (cm *configManager) commitCallbacks() {
for _, callback := range cm.callOnUpdate {
callback(cm)
}
}
// ProposeConfigUpdate takes in an Envelope of type CONFIG_UPDATE and produces a
// ConfigEnvelope to be used as the Envelope Payload Data of a CONFIG message
func (cm *configManager) ProposeConfigUpdate(configtx *cb.Envelope) (*cb.ConfigEnvelope, error) {
return cm.proposeConfigUpdate(configtx)
}
func (cm *configManager) proposeConfigUpdate(configtx *cb.Envelope) (*cb.ConfigEnvelope, error) {
configUpdateEnv, err := envelopeToConfigUpdate(configtx)
if err != nil {
return nil, fmt.Errorf("Error converting envelope to config update: %s", err)
}
configMap, err := cm.authorizeUpdate(configUpdateEnv)
if err != nil {
return nil, fmt.Errorf("Error authorizing update: %s", err)
}
channelGroup, err := configMapToConfig(configMap)
if err != nil {
return nil, fmt.Errorf("Could not turn configMap back to channelGroup: %s", err)
}
result, err := cm.processConfig(channelGroup)
if err != nil {
return nil, fmt.Errorf("Error processing updated config: %s", err)
}
result.rollback()
return &cb.ConfigEnvelope{
Config: &cb.Config{
Sequence: cm.current.sequence + 1,
ChannelGroup: channelGroup,
},
LastUpdate: configtx,
}, nil
}
func (cm *configManager) prepareApply(configEnv *cb.ConfigEnvelope) (map[string]comparable, *configResult, error) {
if configEnv == nil {
return nil, nil, fmt.Errorf("Attempted to apply config with nil envelope")
}
if configEnv.Config == nil {
return nil, nil, fmt.Errorf("Config cannot be nil")
}
if configEnv.Config.Sequence != cm.current.sequence+1 {
return nil, nil, fmt.Errorf("Config at sequence %d, cannot prepare to update to %d", cm.current.sequence, configEnv.Config.Sequence)
}
configUpdateEnv, err := envelopeToConfigUpdate(configEnv.LastUpdate)
if err != nil {
return nil, nil, err
}
configMap, err := cm.authorizeUpdate(configUpdateEnv)
if err != nil {
return nil, nil, err
}
channelGroup, err := configMapToConfig(configMap)
if err != nil {
return nil, nil, fmt.Errorf("Could not turn configMap back to channelGroup: %s", err)
}
if !reflect.DeepEqual(channelGroup, configEnv.Config.ChannelGroup) {
return nil, nil, fmt.Errorf("ConfigEnvelope LastUpdate did not produce the supplied config result")
}
result, err := cm.processConfig(channelGroup)
if err != nil {
return nil, nil, err
}
return configMap, result, nil
}
// Validate simulates applying a ConfigEnvelope to become the new config
func (cm *configManager) Validate(configEnv *cb.ConfigEnvelope) error {
_, result, err := cm.prepareApply(configEnv)
if err != nil {
return err
}
result.rollback()
return nil
}
// Apply attempts to apply a ConfigEnvelope to become the new config
func (cm *configManager) Apply(configEnv *cb.ConfigEnvelope) error {
configMap, result, err := cm.prepareApply(configEnv)
if err != nil {
return err
}
result.commit()
cm.current = &configSet{
configMap: configMap,
channelID: cm.current.channelID,
sequence: configEnv.Config.Sequence,
configEnv: configEnv,
}
cm.commitCallbacks()
return nil
}
// ChainID retrieves the chain ID associated with this manager
func (cm *configManager) ChainID() string {
return cm.current.channelID
}
// Sequence returns the current sequence number of the config
func (cm *configManager) Sequence() uint64 {
return cm.current.sequence
}
// ConfigEnvelope returns the current config envelope
func (cm *configManager) ConfigEnvelope() *cb.ConfigEnvelope {
return cm.current.configEnv
}