|
1 | 1 | /*
|
2 |
| -Copyright IBM Corp. 2016-2017 All Rights Reserved. |
| 2 | +Copyright IBM Corp. 2017 All Rights Reserved. |
3 | 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. |
| 4 | +SPDX-License-Identifier: Apache-2.0 |
15 | 5 | */
|
16 | 6 |
|
17 | 7 | package configtx
|
18 | 8 |
|
19 | 9 | import (
|
20 |
| - "bytes" |
21 | 10 | "fmt"
|
22 | 11 |
|
23 | 12 | "github.com/hyperledger/fabric/common/config"
|
24 | 13 | "github.com/hyperledger/fabric/common/configtx/api"
|
25 | 14 | "github.com/hyperledger/fabric/common/policies"
|
26 | 15 | cb "github.com/hyperledger/fabric/protos/common"
|
27 |
| - |
28 |
| - "github.com/golang/protobuf/jsonpb" |
29 |
| - "github.com/golang/protobuf/proto" |
30 | 16 | )
|
31 | 17 |
|
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 |
| - |
40 | 18 | 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 |
155 | 26 | }
|
156 | 27 |
|
157 | 28 | func (cr *configResult) preCommit() error {
|
@@ -208,35 +79,31 @@ func proposeGroup(result *configResult) error {
|
208 | 79 | }
|
209 | 80 |
|
210 | 81 | for key, value := range result.group.Values {
|
211 |
| - msg, err := valueDeserializer.Deserialize(key, value.Value) |
| 82 | + _, err := valueDeserializer.Deserialize(key, value.Value) |
212 | 83 | if err != nil {
|
213 | 84 | result.rollback()
|
214 | 85 | return fmt.Errorf("Error deserializing key %s for group %s: %s", key, result.groupName, err)
|
215 | 86 | }
|
216 |
| - result.deserializedValues[key] = msg |
217 | 87 | }
|
218 | 88 |
|
219 | 89 | 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) |
221 | 91 | if err != nil {
|
222 | 92 | result.rollback()
|
223 | 93 | return err
|
224 | 94 | }
|
225 |
| - result.deserializedPolicies[key] = policy |
226 | 95 | }
|
227 | 96 |
|
228 | 97 | result.subResults = make([]*configResult, 0, len(subGroups))
|
229 | 98 |
|
230 | 99 | for i, subGroup := range subGroups {
|
231 | 100 | 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], |
240 | 107 | })
|
241 | 108 |
|
242 | 109 | if err := proposeGroup(result.subResults[i]); err != nil {
|
|
0 commit comments