-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
291 lines (243 loc) · 8.9 KB
/
template.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
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"
"github.com/hyperledger/fabric/common/config"
configmsp "github.com/hyperledger/fabric/common/config/msp"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/msp"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"
"github.com/golang/protobuf/proto"
)
const (
// CreationPolicyKey defines the config key used in the channel
// config, under which the creation policy is defined.
CreationPolicyKey = "CreationPolicy"
msgVersion = int32(0)
epoch = 0
)
// Template can be used to facilitate creation of config transactions
type Template interface {
// Envelope returns a ConfigUpdateEnvelope for the given chainID
Envelope(chainID string) (*cb.ConfigUpdateEnvelope, error)
}
type simpleTemplate struct {
configGroup *cb.ConfigGroup
}
// NewSimpleTemplate creates a Template using the supplied ConfigGroups
func NewSimpleTemplate(configGroups ...*cb.ConfigGroup) Template {
sts := make([]Template, len(configGroups))
for i, group := range configGroups {
sts[i] = &simpleTemplate{
configGroup: group,
}
}
return NewCompositeTemplate(sts...)
}
// Envelope returns a ConfigUpdateEnvelope for the given chainID
func (st *simpleTemplate) Envelope(chainID string) (*cb.ConfigUpdateEnvelope, error) {
config, err := proto.Marshal(&cb.ConfigUpdate{
ChannelId: chainID,
WriteSet: st.configGroup,
})
if err != nil {
return nil, err
}
return &cb.ConfigUpdateEnvelope{
ConfigUpdate: config,
}, nil
}
type compositeTemplate struct {
templates []Template
}
// NewCompositeTemplate creates a Template using the source Templates
func NewCompositeTemplate(templates ...Template) Template {
return &compositeTemplate{templates: templates}
}
func copyGroup(source *cb.ConfigGroup, target *cb.ConfigGroup) error {
for key, value := range source.Values {
_, ok := target.Values[key]
if ok {
return fmt.Errorf("Duplicate key: %s", key)
}
target.Values[key] = value
}
for key, policy := range source.Policies {
_, ok := target.Policies[key]
if ok {
return fmt.Errorf("Duplicate policy: %s", key)
}
target.Policies[key] = policy
}
for key, group := range source.Groups {
_, ok := target.Groups[key]
if !ok {
newGroup := cb.NewConfigGroup()
newGroup.ModPolicy = group.ModPolicy
target.Groups[key] = newGroup
}
err := copyGroup(group, target.Groups[key])
if err != nil {
return fmt.Errorf("Error copying group %s: %s", key, err)
}
}
return nil
}
// Envelope returns a ConfigUpdateEnvelope for the given chainID
func (ct *compositeTemplate) Envelope(chainID string) (*cb.ConfigUpdateEnvelope, error) {
channel := cb.NewConfigGroup()
for i := range ct.templates {
configEnv, err := ct.templates[i].Envelope(chainID)
if err != nil {
return nil, err
}
config, err := UnmarshalConfigUpdate(configEnv.ConfigUpdate)
if err != nil {
return nil, err
}
err = copyGroup(config.WriteSet, channel)
if err != nil {
return nil, err
}
}
marshaledConfig, err := proto.Marshal(&cb.ConfigUpdate{
ChannelId: chainID,
WriteSet: channel,
})
if err != nil {
return nil, err
}
return &cb.ConfigUpdateEnvelope{ConfigUpdate: marshaledConfig}, nil
}
type modPolicySettingTemplate struct {
modPolicy string
template Template
}
// NewModPolicySettingTemplate wraps another template and sets the ModPolicy of
// every ConfigGroup/ConfigValue/ConfigPolicy without a modPolicy to modPolicy
func NewModPolicySettingTemplate(modPolicy string, template Template) Template {
return &modPolicySettingTemplate{
modPolicy: modPolicy,
template: template,
}
}
func setGroupModPolicies(modPolicy string, group *cb.ConfigGroup) {
if group.ModPolicy == "" {
group.ModPolicy = modPolicy
}
for _, value := range group.Values {
if value.ModPolicy != "" {
continue
}
value.ModPolicy = modPolicy
}
for _, policy := range group.Policies {
if policy.ModPolicy != "" {
continue
}
policy.ModPolicy = modPolicy
}
for _, nextGroup := range group.Groups {
setGroupModPolicies(modPolicy, nextGroup)
}
}
func (mpst *modPolicySettingTemplate) Envelope(channelID string) (*cb.ConfigUpdateEnvelope, error) {
configUpdateEnv, err := mpst.template.Envelope(channelID)
if err != nil {
return nil, err
}
config, err := UnmarshalConfigUpdate(configUpdateEnv.ConfigUpdate)
if err != nil {
return nil, err
}
setGroupModPolicies(mpst.modPolicy, config.WriteSet)
configUpdateEnv.ConfigUpdate = utils.MarshalOrPanic(config)
return configUpdateEnv, nil
}
type channelCreationTemplate struct {
consortiumName string
orgs []string
}
// NewChainCreationTemplate takes a consortium name and a Template to produce a
// Template which outputs an appropriately constructed list of ConfigUpdateEnvelopes.
func NewChainCreationTemplate(consortiumName string, orgs []string) Template {
return &channelCreationTemplate{
consortiumName: consortiumName,
orgs: orgs,
}
}
func (cct *channelCreationTemplate) Envelope(channelID string) (*cb.ConfigUpdateEnvelope, error) {
rSet := config.TemplateConsortium(cct.consortiumName)
wSet := config.TemplateConsortium(cct.consortiumName)
rSet.Groups[config.ApplicationGroupKey] = cb.NewConfigGroup()
wSet.Groups[config.ApplicationGroupKey] = cb.NewConfigGroup()
for _, org := range cct.orgs {
rSet.Groups[config.ApplicationGroupKey].Groups[org] = cb.NewConfigGroup()
wSet.Groups[config.ApplicationGroupKey].Groups[org] = cb.NewConfigGroup()
}
wSet.Groups[config.ApplicationGroupKey].ModPolicy = configmsp.AdminsPolicyKey
wSet.Groups[config.ApplicationGroupKey].Policies[configmsp.AdminsPolicyKey] = policies.ImplicitMetaPolicyWithSubPolicy(configmsp.AdminsPolicyKey, cb.ImplicitMetaPolicy_MAJORITY)
wSet.Groups[config.ApplicationGroupKey].Policies[configmsp.AdminsPolicyKey].ModPolicy = configmsp.AdminsPolicyKey
wSet.Groups[config.ApplicationGroupKey].Policies[configmsp.WritersPolicyKey] = policies.ImplicitMetaPolicyWithSubPolicy(configmsp.WritersPolicyKey, cb.ImplicitMetaPolicy_ANY)
wSet.Groups[config.ApplicationGroupKey].Policies[configmsp.WritersPolicyKey].ModPolicy = configmsp.AdminsPolicyKey
wSet.Groups[config.ApplicationGroupKey].Policies[configmsp.ReadersPolicyKey] = policies.ImplicitMetaPolicyWithSubPolicy(configmsp.ReadersPolicyKey, cb.ImplicitMetaPolicy_ANY)
wSet.Groups[config.ApplicationGroupKey].Policies[configmsp.ReadersPolicyKey].ModPolicy = configmsp.AdminsPolicyKey
wSet.Groups[config.ApplicationGroupKey].Version = 1
return &cb.ConfigUpdateEnvelope{
ConfigUpdate: utils.MarshalOrPanic(&cb.ConfigUpdate{
ChannelId: channelID,
ReadSet: rSet,
WriteSet: wSet,
}),
}, nil
}
// MakeChainCreationTransaction is a handy utility function for creating new chain transactions using the underlying Template framework
func MakeChainCreationTransaction(channelID string, consortium string, signer msp.SigningIdentity, orgs ...string) (*cb.Envelope, error) {
newChainTemplate := NewChainCreationTemplate(consortium, orgs)
newConfigUpdateEnv, err := newChainTemplate.Envelope(channelID)
if err != nil {
return nil, err
}
payloadSignatureHeader := &cb.SignatureHeader{}
if signer != nil {
sSigner, err := signer.Serialize()
if err != nil {
return nil, fmt.Errorf("Serialization of identity failed, err %s", err)
}
newConfigUpdateEnv.Signatures = []*cb.ConfigSignature{&cb.ConfigSignature{
SignatureHeader: utils.MarshalOrPanic(utils.MakeSignatureHeader(sSigner, utils.CreateNonceOrPanic())),
}}
newConfigUpdateEnv.Signatures[0].Signature, err = signer.Sign(util.ConcatenateBytes(newConfigUpdateEnv.Signatures[0].SignatureHeader, newConfigUpdateEnv.ConfigUpdate))
if err != nil {
return nil, err
}
payloadSignatureHeader = utils.MakeSignatureHeader(sSigner, utils.CreateNonceOrPanic())
}
payloadChannelHeader := utils.MakeChannelHeader(cb.HeaderType_CONFIG_UPDATE, msgVersion, channelID, epoch)
utils.SetTxID(payloadChannelHeader, payloadSignatureHeader)
payloadHeader := utils.MakePayloadHeader(payloadChannelHeader, payloadSignatureHeader)
payload := &cb.Payload{Header: payloadHeader, Data: utils.MarshalOrPanic(newConfigUpdateEnv)}
paylBytes := utils.MarshalOrPanic(payload)
var sig []byte
if signer != nil {
// sign the payload
sig, err = signer.Sign(paylBytes)
if err != nil {
return nil, err
}
}
return &cb.Envelope{Payload: paylBytes, Signature: sig}, nil
}