Skip to content

Commit f4427f9

Browse files
author
Jason Yellick
committed
[FAB-5550] configtxgen use protolator JSON parsing
For v1, configtxgen used some hacks into the configtx core code to get a JSON representation of the config. This CR switches the JSON encoding to use the same protolator helpers as the configtxlator code. The existing method was (as already noted) hacky, and not as expressive as the configtxlator method (which also expands other opaque fields like MSP principals and MSP definitions. Plus, it is unnecessary code to maintain. Change-Id: Ibfe79de834bae6d91e7d67a9f528564658650902 Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent 094244d commit f4427f9

File tree

5 files changed

+60
-384
lines changed

5 files changed

+60
-384
lines changed

common/configtx/config.go

Lines changed: 17 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,157 +1,28 @@
11
/*
2-
Copyright IBM Corp. 2016-2017 All Rights Reserved.
2+
Copyright IBM Corp. 2017 All Rights Reserved.
33
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.
4+
SPDX-License-Identifier: Apache-2.0
155
*/
166

177
package configtx
188

199
import (
20-
"bytes"
2110
"fmt"
2211

2312
"github.com/hyperledger/fabric/common/config"
2413
"github.com/hyperledger/fabric/common/configtx/api"
2514
"github.com/hyperledger/fabric/common/policies"
2615
cb "github.com/hyperledger/fabric/protos/common"
27-
28-
"github.com/golang/protobuf/jsonpb"
29-
"github.com/golang/protobuf/proto"
3016
)
3117

32-
type ConfigResult interface {
33-
JSON() string
34-
}
35-
36-
func NewConfigResult(config *cb.ConfigGroup, proposer api.Proposer) (ConfigResult, error) {
37-
return processConfig(config, proposer)
38-
}
39-
4018
type configResult struct {
41-
tx interface{}
42-
groupName string
43-
groupKey string
44-
group *cb.ConfigGroup
45-
valueHandler config.ValueProposer
46-
policyHandler policies.Proposer
47-
subResults []*configResult
48-
deserializedValues map[string]proto.Message
49-
deserializedPolicies map[string]proto.Message
50-
}
51-
52-
func (cr *configResult) JSON() string {
53-
var buffer bytes.Buffer
54-
buffer.WriteString("{")
55-
cr.subResults[0].bufferJSON(&buffer)
56-
buffer.WriteString("}")
57-
return buffer.String()
58-
59-
}
60-
61-
// bufferJSON takes a buffer and writes a JSON representation of the configResult into the buffer
62-
// Note that we use some mildly ad-hoc JSON encoding because the proto documentation explicitly
63-
// mentions that the encoding/json package does not correctly marshal proto objects, and we
64-
// do not have a proto object (nor can one be defined) which presents the mixed-map style of
65-
// keys mapping to different types of the config
66-
func (cr *configResult) bufferJSON(buffer *bytes.Buffer) {
67-
jpb := &jsonpb.Marshaler{
68-
EmitDefaults: true,
69-
Indent: " ",
70-
}
71-
72-
// "GroupName": {
73-
buffer.WriteString("\"")
74-
buffer.WriteString(cr.groupKey)
75-
buffer.WriteString("\": {")
76-
77-
// "Values": {
78-
buffer.WriteString("\"Values\": {")
79-
count := 0
80-
for key, value := range cr.group.Values {
81-
// "Key": {
82-
buffer.WriteString("\"")
83-
buffer.WriteString(key)
84-
buffer.WriteString("\": {")
85-
// "Version": "X",
86-
buffer.WriteString("\"Version\":\"")
87-
buffer.WriteString(fmt.Sprintf("%d", value.Version))
88-
buffer.WriteString("\",")
89-
// "ModPolicy": "foo",
90-
buffer.WriteString("\"ModPolicy\":\"")
91-
buffer.WriteString(value.ModPolicy)
92-
buffer.WriteString("\",")
93-
// "Value": protoAsJSON
94-
buffer.WriteString("\"Value\":")
95-
jpb.Marshal(buffer, cr.deserializedValues[key])
96-
// },
97-
buffer.WriteString("}")
98-
count++
99-
if count < len(cr.group.Values) {
100-
buffer.WriteString(",")
101-
}
102-
}
103-
// },
104-
buffer.WriteString("},")
105-
106-
count = 0
107-
// "Policies": {
108-
buffer.WriteString("\"Policies\": {")
109-
for key, policy := range cr.group.Policies {
110-
// "Key": {
111-
buffer.WriteString("\"")
112-
buffer.WriteString(key)
113-
buffer.WriteString("\": {")
114-
// "Version": "X",
115-
buffer.WriteString("\"Version\":\"")
116-
buffer.WriteString(fmt.Sprintf("%d", policy.Version))
117-
buffer.WriteString("\",")
118-
// "ModPolicy": "foo",
119-
buffer.WriteString("\"ModPolicy\":\"")
120-
buffer.WriteString(policy.ModPolicy)
121-
buffer.WriteString("\",")
122-
// "Policy": {
123-
buffer.WriteString("\"Policy\":{")
124-
// "PolicyType" :
125-
buffer.WriteString(fmt.Sprintf("\"PolicyType\":\"%d\",", policy.Policy.Type))
126-
// "Policy" : policyAsJSON
127-
buffer.WriteString("\"Policy\":")
128-
jpb.Marshal(buffer, cr.deserializedPolicies[key])
129-
// }
130-
// },
131-
buffer.WriteString("}}")
132-
count++
133-
if count < len(cr.group.Policies) {
134-
buffer.WriteString(",")
135-
}
136-
}
137-
// },
138-
buffer.WriteString("},")
139-
140-
// "Groups": {
141-
count = 0
142-
buffer.WriteString("\"Groups\": {")
143-
for _, subResult := range cr.subResults {
144-
subResult.bufferJSON(buffer)
145-
count++
146-
if count < len(cr.subResults) {
147-
buffer.WriteString(",")
148-
}
149-
}
150-
// }
151-
buffer.WriteString("}")
152-
153-
// }
154-
buffer.WriteString("}")
19+
tx interface{}
20+
groupName string
21+
groupKey string
22+
group *cb.ConfigGroup
23+
valueHandler config.ValueProposer
24+
policyHandler policies.Proposer
25+
subResults []*configResult
15526
}
15627

15728
func (cr *configResult) preCommit() error {
@@ -208,35 +79,31 @@ func proposeGroup(result *configResult) error {
20879
}
20980

21081
for key, value := range result.group.Values {
211-
msg, err := valueDeserializer.Deserialize(key, value.Value)
82+
_, err := valueDeserializer.Deserialize(key, value.Value)
21283
if err != nil {
21384
result.rollback()
21485
return fmt.Errorf("Error deserializing key %s for group %s: %s", key, result.groupName, err)
21586
}
216-
result.deserializedValues[key] = msg
21787
}
21888

21989
for key, policy := range result.group.Policies {
220-
policy, err := result.policyHandler.ProposePolicy(result.tx, key, policy)
90+
_, err := result.policyHandler.ProposePolicy(result.tx, key, policy)
22191
if err != nil {
22292
result.rollback()
22393
return err
22494
}
225-
result.deserializedPolicies[key] = policy
22695
}
22796

22897
result.subResults = make([]*configResult, 0, len(subGroups))
22998

23099
for i, subGroup := range subGroups {
231100
result.subResults = append(result.subResults, &configResult{
232-
tx: result.tx,
233-
groupKey: subGroup,
234-
groupName: result.groupName + "/" + subGroup,
235-
group: result.group.Groups[subGroup],
236-
valueHandler: subValueHandlers[i],
237-
policyHandler: subPolicyHandlers[i],
238-
deserializedValues: make(map[string]proto.Message),
239-
deserializedPolicies: make(map[string]proto.Message),
101+
tx: result.tx,
102+
groupKey: subGroup,
103+
groupName: result.groupName + "/" + subGroup,
104+
group: result.group.Groups[subGroup],
105+
valueHandler: subValueHandlers[i],
106+
policyHandler: subPolicyHandlers[i],
240107
})
241108

242109
if err := proposeGroup(result.subResults[i]); err != nil {

common/configtx/config_test.go

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)