Skip to content

Commit 75327ff

Browse files
author
Jason Yellick
committed
[FAB-2197] Factor out configmap construction
https://jira.hyperledger.org/browse/FAB-2197 The configtx processing currently performs validation while applying configuration. In order to split this process, the config map construction will need to be used in two different code paths, so this CR factors it out. Change-Id: Ib230c3f363d490fb89b83fcd64bf2c9ec4fc67a4 Signed-off-by: Jason Yellick <[email protected]>
1 parent c16f5b3 commit 75327ff

File tree

5 files changed

+201
-58
lines changed

5 files changed

+201
-58
lines changed

common/configtx/compare.go

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type comparable struct {
2626
*cb.ConfigGroup
2727
*cb.ConfigValue
2828
*cb.ConfigPolicy
29+
key string
2930
path []string
3031
}
3132

common/configtx/configmap.go

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package configtx
18+
19+
import (
20+
"fmt"
21+
"strings"
22+
23+
cb "github.com/hyperledger/fabric/protos/common"
24+
)
25+
26+
const (
27+
RootGroupKey = "Channel"
28+
29+
GroupPrefix = "[Groups] "
30+
ValuePrefix = "[Values] "
31+
PolicyPrefix = "[Policy] " // The plurarility doesn't match, but, it makes the logs much easier being the same lenght as "Groups" and "Values"
32+
33+
PathSeparator = "/"
34+
)
35+
36+
// mapConfig is the only method in this file intended to be called outside this file
37+
// it takes a ConfigGroup and generates a map of fqPath to comparables (or error on invalid keys)
38+
func mapConfig(channelGroup *cb.ConfigGroup) (map[string]comparable, error) {
39+
result := make(map[string]comparable)
40+
err := recurseConfig(result, []string{RootGroupKey}, channelGroup)
41+
if err != nil {
42+
return nil, err
43+
}
44+
return result, nil
45+
}
46+
47+
func addToMap(cg comparable, result map[string]comparable) error {
48+
var fqPath string
49+
50+
switch {
51+
case cg.ConfigGroup != nil:
52+
fqPath = GroupPrefix
53+
case cg.ConfigValue != nil:
54+
fqPath = ValuePrefix
55+
case cg.ConfigPolicy != nil:
56+
fqPath = PolicyPrefix
57+
}
58+
59+
// TODO rename validateChainID to validateConfigID
60+
if err := validateChainID(cg.key); err != nil {
61+
return fmt.Errorf("Illegal characters in key: %s", fqPath)
62+
}
63+
64+
if len(cg.path) == 0 {
65+
fqPath += PathSeparator + cg.key
66+
} else {
67+
fqPath += PathSeparator + strings.Join(cg.path, PathSeparator) + PathSeparator + cg.key
68+
}
69+
70+
logger.Debugf("Adding to config map: %s", fqPath)
71+
72+
result[fqPath] = cg
73+
74+
return nil
75+
}
76+
77+
func recurseConfig(result map[string]comparable, path []string, group *cb.ConfigGroup) error {
78+
if err := addToMap(comparable{key: path[len(path)-1], path: path[:len(path)-1], ConfigGroup: group}, result); err != nil {
79+
return err
80+
}
81+
82+
for key, group := range group.Groups {
83+
nextPath := append(path, key)
84+
if err := recurseConfig(result, nextPath, group); err != nil {
85+
return err
86+
}
87+
}
88+
89+
for key, value := range group.Values {
90+
if err := addToMap(comparable{key: key, path: path, ConfigValue: value}, result); err != nil {
91+
return err
92+
}
93+
}
94+
95+
for key, policy := range group.Policies {
96+
if err := addToMap(comparable{key: key, path: path, ConfigPolicy: policy}, result); err != nil {
97+
return err
98+
}
99+
}
100+
101+
return nil
102+
}

common/configtx/configmap_test.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package configtx
18+
19+
import (
20+
"github.com/stretchr/testify/assert"
21+
"testing"
22+
23+
cb "github.com/hyperledger/fabric/protos/common"
24+
)
25+
26+
func TestBadKey(t *testing.T) {
27+
assert.Error(t, addToMap(comparable{key: "[Label]", path: []string{}}, make(map[string]comparable)),
28+
"Should have errored on key with illegal characters")
29+
}
30+
31+
func TestConfigMap(t *testing.T) {
32+
config := cb.NewConfigGroup()
33+
config.Groups["0DeepGroup"] = cb.NewConfigGroup()
34+
config.Values["0DeepValue1"] = &cb.ConfigValue{}
35+
config.Values["0DeepValue2"] = &cb.ConfigValue{}
36+
config.Groups["0DeepGroup"].Policies["1DeepPolicy"] = &cb.ConfigPolicy{}
37+
config.Groups["0DeepGroup"].Groups["1DeepGroup"] = cb.NewConfigGroup()
38+
config.Groups["0DeepGroup"].Groups["1DeepGroup"].Values["2DeepValue"] = &cb.ConfigValue{}
39+
40+
confMap, err := mapConfig(config)
41+
assert.NoError(t, err, "Should not have errored building map")
42+
43+
assert.Len(t, confMap, 7, "There should be 7 entries in the config map")
44+
45+
assert.Equal(t, comparable{key: "Channel", path: []string{}, ConfigGroup: config},
46+
confMap["[Groups] /Channel"])
47+
assert.Equal(t, comparable{key: "0DeepGroup", path: []string{"Channel"}, ConfigGroup: config.Groups["0DeepGroup"]},
48+
confMap["[Groups] /Channel/0DeepGroup"])
49+
assert.Equal(t, comparable{key: "0DeepValue1", path: []string{"Channel"}, ConfigValue: config.Values["0DeepValue1"]},
50+
confMap["[Values] /Channel/0DeepValue1"])
51+
assert.Equal(t, comparable{key: "0DeepValue2", path: []string{"Channel"}, ConfigValue: config.Values["0DeepValue2"]},
52+
confMap["[Values] /Channel/0DeepValue2"])
53+
assert.Equal(t, comparable{key: "1DeepPolicy", path: []string{"Channel", "0DeepGroup"}, ConfigPolicy: config.Groups["0DeepGroup"].Policies["1DeepPolicy"]},
54+
confMap["[Policy] /Channel/0DeepGroup/1DeepPolicy"])
55+
assert.Equal(t, comparable{key: "1DeepGroup", path: []string{"Channel", "0DeepGroup"}, ConfigGroup: config.Groups["0DeepGroup"].Groups["1DeepGroup"]},
56+
confMap["[Groups] /Channel/0DeepGroup/1DeepGroup"])
57+
assert.Equal(t, comparable{key: "2DeepValue", path: []string{"Channel", "0DeepGroup", "1DeepGroup"}, ConfigValue: config.Groups["0DeepGroup"].Groups["1DeepGroup"].Values["2DeepValue"]},
58+
confMap["[Values] /Channel/0DeepGroup/1DeepGroup/2DeepValue"])
59+
}

common/configtx/initializer.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package configtx
1818

1919
import (
20+
"fmt"
21+
2022
"github.com/hyperledger/fabric/common/cauthdsl"
2123
"github.com/hyperledger/fabric/common/configtx/api"
2224
configtxapplication "github.com/hyperledger/fabric/common/configtx/handlers/application"
@@ -134,5 +136,15 @@ func (i *initializer) PolicyProposer() api.Handler {
134136
}
135137

136138
func (i *initializer) Handler(path []string) (api.Handler, error) {
137-
return i.channelConfig.Handler(path)
139+
if len(path) == 0 {
140+
return nil, fmt.Errorf("Empty path")
141+
}
142+
143+
switch path[0] {
144+
case RootGroupKey:
145+
return i.channelConfig.Handler(path[1:])
146+
default:
147+
return nil, fmt.Errorf("Unknown root group: %s", path[0])
148+
}
149+
138150
}

common/configtx/manager.go

+26-57
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"errors"
2121
"fmt"
2222
"regexp"
23-
"strings"
2423

2524
"github.com/hyperledger/fabric/common/configtx/api"
2625
"github.com/hyperledger/fabric/common/policies"
@@ -186,66 +185,32 @@ func (cm *configManager) commitHandlers() {
186185
}
187186
}
188187

189-
func (cm *configManager) recurseConfig(result map[string]comparable, path []string, group *cb.ConfigGroup) error {
190-
191-
for key, group := range group.Groups {
192-
// TODO rename validateChainID to validateConfigID
193-
if err := validateChainID(key); err != nil {
194-
return fmt.Errorf("Illegal characters in group key: %s", key)
195-
}
196-
197-
if err := cm.recurseConfig(result, append(path, key), group); err != nil {
198-
return err
199-
}
200-
201-
// TODO, uncomment to validate version validation on groups
202-
// result["[Groups]"+strings.Join(path, ".")+key] = comparable{path: path, ConfigGroup: group}
203-
}
204-
205-
valueHandler, err := cm.initializer.Handler(path)
206-
if err != nil {
207-
return err
208-
}
209-
210-
for key, value := range group.Values {
211-
if err := validateChainID(key); err != nil {
212-
return fmt.Errorf("Illegal characters in values key: %s", key)
213-
}
214-
215-
err := valueHandler.ProposeConfig(key, &cb.ConfigValue{
216-
Value: value.Value,
217-
})
218-
if err != nil {
219-
return err
220-
}
221-
222-
result["[Values]"+strings.Join(path, ".")+key] = comparable{path: path, ConfigValue: value}
223-
}
224-
225-
logger.Debugf("Found %d policies", len(group.Policies))
226-
for key, policy := range group.Policies {
227-
if err := validateChainID(key); err != nil {
228-
return fmt.Errorf("Illegal characters in policies key: %s", key)
229-
}
230-
231-
logger.Debugf("Proposing policy: %s", key)
232-
err := cm.initializer.PolicyProposer().ProposeConfig(key, &cb.ConfigValue{
233-
// TODO, fix policy interface to take the policy directly
234-
Value: utils.MarshalOrPanic(policy.Policy),
235-
})
236-
237-
if err != nil {
238-
return err
188+
func (cm *configManager) proposeConfig(config map[string]comparable) error {
189+
for fqPath, c := range config {
190+
logger.Debugf("Proposing: %s", fqPath)
191+
switch {
192+
case c.ConfigValue != nil:
193+
valueHandler, err := cm.initializer.Handler(c.path)
194+
if err != nil {
195+
return err
196+
}
197+
if err := valueHandler.ProposeConfig(c.key, c.ConfigValue); err != nil {
198+
return err
199+
}
200+
case c.ConfigPolicy != nil:
201+
if err := cm.initializer.PolicyProposer().ProposeConfig(c.key, &cb.ConfigValue{
202+
// TODO, fix policy interface to take the policy directly
203+
Value: utils.MarshalOrPanic(c.ConfigPolicy.Policy),
204+
}); err != nil {
205+
return err
206+
}
239207
}
240-
241-
// TODO, uncomment to validate version validation on policies
242-
//result["[Policies]"+strings.Join(path, ".")+key] = comparable{path: path, ConfigPolicy: policy}
243208
}
244209

245210
return nil
246211
}
247212

248-
func (cm *configManager) processConfig(configtx *cb.ConfigEnvelope) (configMap map[string]comparable, err error) {
213+
func (cm *configManager) processConfig(configtx *cb.ConfigEnvelope) (map[string]comparable, error) {
249214
// XXX as a temporary hack to get the new protos working, we assume entire config is always in the ConfigUpdate.WriteSet
250215

251216
if configtx.LastUpdate == nil {
@@ -284,9 +249,13 @@ func (cm *configManager) processConfig(configtx *cb.ConfigEnvelope) (configMap m
284249
defaultModificationPolicy = &acceptAllPolicy{}
285250
}
286251

287-
configMap = make(map[string]comparable)
252+
configMap, err := mapConfig(config.WriteSet)
253+
if err != nil {
254+
return nil, err
255+
}
256+
delete(configMap, "[Groups] /Channel") // XXX temporary hack to prevent evaluating groups for modification
288257

289-
if err := cm.recurseConfig(configMap, []string{}, config.WriteSet); err != nil {
258+
if err := cm.proposeConfig(configMap); err != nil {
290259
return nil, err
291260
}
292261

0 commit comments

Comments
 (0)