forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
330 lines (282 loc) · 11.2 KB
/
main.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
Copyright IBM Corp. 2017 All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/golang/protobuf/proto"
cb "github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric/bccsp/factory"
"github.com/hyperledger/fabric/common/channelconfig"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/common/tools/protolator"
"github.com/hyperledger/fabric/common/tools/protolator/protoext/ordererext"
"github.com/hyperledger/fabric/internal/configtxgen/encoder"
"github.com/hyperledger/fabric/internal/configtxgen/genesisconfig"
"github.com/hyperledger/fabric/internal/configtxgen/metadata"
"github.com/hyperledger/fabric/internal/configtxlator/update"
"github.com/hyperledger/fabric/protoutil"
"github.com/pkg/errors"
)
var logger = flogging.MustGetLogger("common.tools.configtxgen")
func doOutputBlock(config *genesisconfig.Profile, channelID string, outputBlock string) error {
pgen, err := encoder.NewBootstrapper(config)
if err != nil {
return errors.WithMessage(err, "could not create bootstrapper")
}
logger.Info("Generating genesis block")
if config.Orderer == nil {
return errors.Errorf("refusing to generate block which is missing orderer section")
}
if config.Consortiums == nil {
logger.Warning("Genesis block does not contain a consortiums group definition. This block cannot be used for orderer bootstrap.")
}
genesisBlock := pgen.GenesisBlockForChannel(channelID)
logger.Info("Writing genesis block")
err = writeFile(outputBlock, protoutil.MarshalOrPanic(genesisBlock), 0640)
if err != nil {
return fmt.Errorf("Error writing genesis block: %s", err)
}
return nil
}
func doOutputChannelCreateTx(conf, baseProfile *genesisconfig.Profile, channelID string, outputChannelCreateTx string) error {
logger.Info("Generating new channel configtx")
var configtx *cb.Envelope
var err error
if baseProfile == nil {
configtx, err = encoder.MakeChannelCreationTransaction(channelID, nil, conf)
} else {
configtx, err = encoder.MakeChannelCreationTransactionWithSystemChannelContext(channelID, nil, conf, baseProfile)
}
if err != nil {
return err
}
logger.Info("Writing new channel tx")
err = writeFile(outputChannelCreateTx, protoutil.MarshalOrPanic(configtx), 0640)
if err != nil {
return fmt.Errorf("Error writing channel create tx: %s", err)
}
return nil
}
func doOutputAnchorPeersUpdate(conf *genesisconfig.Profile, channelID string, outputAnchorPeersUpdate string, asOrg string) error {
logger.Info("Generating anchor peer update")
if asOrg == "" {
return fmt.Errorf("Must specify an organization to update the anchor peer for")
}
if conf.Application == nil {
return fmt.Errorf("Cannot update anchor peers without an application section")
}
original, err := encoder.NewChannelGroup(conf)
if err != nil {
return errors.WithMessage(err, "error parsing profile as channel group")
}
original.Groups[channelconfig.ApplicationGroupKey].Version = 1
updated := proto.Clone(original).(*cb.ConfigGroup)
originalOrg, ok := original.Groups[channelconfig.ApplicationGroupKey].Groups[asOrg]
if !ok {
return errors.Errorf("org with name '%s' does not exist in config", asOrg)
}
if _, ok = originalOrg.Values[channelconfig.AnchorPeersKey]; !ok {
return errors.Errorf("org '%s' does not have any anchor peers defined", asOrg)
}
delete(originalOrg.Values, channelconfig.AnchorPeersKey)
updt, err := update.Compute(&cb.Config{ChannelGroup: original}, &cb.Config{ChannelGroup: updated})
if err != nil {
return errors.WithMessage(err, "could not compute update")
}
updt.ChannelId = channelID
newConfigUpdateEnv := &cb.ConfigUpdateEnvelope{
ConfigUpdate: protoutil.MarshalOrPanic(updt),
}
updateTx, err := protoutil.CreateSignedEnvelope(cb.HeaderType_CONFIG_UPDATE, channelID, nil, newConfigUpdateEnv, 0, 0)
logger.Info("Writing anchor peer update")
err = writeFile(outputAnchorPeersUpdate, protoutil.MarshalOrPanic(updateTx), 0640)
if err != nil {
return fmt.Errorf("Error writing channel anchor peer update: %s", err)
}
return nil
}
func doInspectBlock(inspectBlock string) error {
logger.Info("Inspecting block")
data, err := ioutil.ReadFile(inspectBlock)
if err != nil {
return fmt.Errorf("Could not read block %s", inspectBlock)
}
logger.Info("Parsing genesis block")
block, err := protoutil.UnmarshalBlock(data)
if err != nil {
return fmt.Errorf("error unmarshaling to block: %s", err)
}
err = protolator.DeepMarshalJSON(os.Stdout, block)
if err != nil {
return fmt.Errorf("malformed block contents: %s", err)
}
return nil
}
func doInspectChannelCreateTx(inspectChannelCreateTx string) error {
logger.Info("Inspecting transaction")
data, err := ioutil.ReadFile(inspectChannelCreateTx)
if err != nil {
return fmt.Errorf("could not read channel create tx: %s", err)
}
logger.Info("Parsing transaction")
env, err := protoutil.UnmarshalEnvelope(data)
if err != nil {
return fmt.Errorf("Error unmarshaling envelope: %s", err)
}
err = protolator.DeepMarshalJSON(os.Stdout, env)
if err != nil {
return fmt.Errorf("malformed transaction contents: %s", err)
}
return nil
}
func doPrintOrg(t *genesisconfig.TopLevel, printOrg string) error {
for _, org := range t.Organizations {
if org.Name == printOrg {
og, err := encoder.NewOrdererOrgGroup(org)
if err != nil {
return errors.Wrapf(err, "bad org definition for org %s", org.Name)
}
if err := protolator.DeepMarshalJSON(os.Stdout, &ordererext.DynamicOrdererOrgGroup{ConfigGroup: og}); err != nil {
return errors.Wrapf(err, "malformed org definition for org: %s", org.Name)
}
return nil
}
}
return errors.Errorf("organization %s not found", printOrg)
}
func writeFile(filename string, data []byte, perm os.FileMode) error {
dirPath := filepath.Dir(filename)
exists, err := dirExists(dirPath)
if err != nil {
return err
}
if !exists {
err = os.MkdirAll(dirPath, 0750)
if err != nil {
return err
}
}
return ioutil.WriteFile(filename, data, perm)
}
func dirExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func main() {
var outputBlock, outputChannelCreateTx, channelCreateTxBaseProfile, profile, configPath, channelID, inspectBlock, inspectChannelCreateTx, outputAnchorPeersUpdate, asOrg, printOrg string
flag.StringVar(&outputBlock, "outputBlock", "", "The path to write the genesis block to (if set)")
flag.StringVar(&channelID, "channelID", "", "The channel ID to use in the configtx")
flag.StringVar(&outputChannelCreateTx, "outputCreateChannelTx", "", "The path to write a channel creation configtx to (if set)")
flag.StringVar(&channelCreateTxBaseProfile, "channelCreateTxBaseProfile", "", "Specifies a profile to consider as the orderer system channel current state to allow modification of non-application parameters during channel create tx generation. Only valid in conjunction with 'outputCreateChannelTx'.")
flag.StringVar(&profile, "profile", "", "The profile from configtx.yaml to use for generation.")
flag.StringVar(&configPath, "configPath", "", "The path containing the configuration to use (if set)")
flag.StringVar(&inspectBlock, "inspectBlock", "", "Prints the configuration contained in the block at the specified path")
flag.StringVar(&inspectChannelCreateTx, "inspectChannelCreateTx", "", "Prints the configuration contained in the transaction at the specified path")
flag.StringVar(&outputAnchorPeersUpdate, "outputAnchorPeersUpdate", "", "[DEPRECATED] Creates a config update to update an anchor peer (works only with the default channel creation, and only for the first update)")
flag.StringVar(&asOrg, "asOrg", "", "Performs the config generation as a particular organization (by name), only including values in the write set that org (likely) has privilege to set")
flag.StringVar(&printOrg, "printOrg", "", "Prints the definition of an organization as JSON. (useful for adding an org to a channel manually)")
version := flag.Bool("version", false, "Show version information")
flag.Parse()
if channelID == "" && (outputBlock != "" || outputChannelCreateTx != "" || outputAnchorPeersUpdate != "") {
logger.Fatalf("Missing channelID, please specify it with '-channelID'")
}
// show version
if *version {
printVersion()
os.Exit(0)
}
// don't need to panic when running via command line
defer func() {
if err := recover(); err != nil {
if strings.Contains(fmt.Sprint(err), "Error reading configuration: Unsupported Config Type") {
logger.Error("Could not find configtx.yaml. " +
"Please make sure that FABRIC_CFG_PATH or -configPath is set to a path " +
"which contains configtx.yaml")
os.Exit(1)
}
if strings.Contains(fmt.Sprint(err), "Could not find profile") {
logger.Error(fmt.Sprint(err) + ". " +
"Please make sure that FABRIC_CFG_PATH or -configPath is set to a path " +
"which contains configtx.yaml with the specified profile")
os.Exit(1)
}
logger.Panic(err)
}
}()
logger.Info("Loading configuration")
factory.InitFactories(nil)
var profileConfig *genesisconfig.Profile
if outputBlock != "" || outputChannelCreateTx != "" || outputAnchorPeersUpdate != "" {
if profile == "" {
logger.Fatalf("The '-profile' is required when '-outputBlock', '-outputChannelCreateTx', or '-outputAnchorPeersUpdate' is specified")
}
if configPath != "" {
profileConfig = genesisconfig.Load(profile, configPath)
} else {
profileConfig = genesisconfig.Load(profile)
}
}
var baseProfile *genesisconfig.Profile
if channelCreateTxBaseProfile != "" {
if outputChannelCreateTx == "" {
logger.Warning("Specified 'channelCreateTxBaseProfile', but did not specify 'outputChannelCreateTx', 'channelCreateTxBaseProfile' will not affect output.")
}
if configPath != "" {
baseProfile = genesisconfig.Load(channelCreateTxBaseProfile, configPath)
} else {
baseProfile = genesisconfig.Load(channelCreateTxBaseProfile)
}
}
if outputBlock != "" {
if err := doOutputBlock(profileConfig, channelID, outputBlock); err != nil {
logger.Fatalf("Error on outputBlock: %s", err)
}
}
if outputChannelCreateTx != "" {
if err := doOutputChannelCreateTx(profileConfig, baseProfile, channelID, outputChannelCreateTx); err != nil {
logger.Fatalf("Error on outputChannelCreateTx: %s", err)
}
}
if inspectBlock != "" {
if err := doInspectBlock(inspectBlock); err != nil {
logger.Fatalf("Error on inspectBlock: %s", err)
}
}
if inspectChannelCreateTx != "" {
if err := doInspectChannelCreateTx(inspectChannelCreateTx); err != nil {
logger.Fatalf("Error on inspectChannelCreateTx: %s", err)
}
}
if outputAnchorPeersUpdate != "" {
if err := doOutputAnchorPeersUpdate(profileConfig, channelID, outputAnchorPeersUpdate, asOrg); err != nil {
logger.Fatalf("Error on inspectChannelCreateTx: %s", err)
}
}
if printOrg != "" {
var topLevelConfig *genesisconfig.TopLevel
if configPath != "" {
topLevelConfig = genesisconfig.LoadTopLevel(configPath)
} else {
topLevelConfig = genesisconfig.LoadTopLevel()
}
if err := doPrintOrg(topLevelConfig, printOrg); err != nil {
logger.Fatalf("Error on printOrg: %s", err)
}
}
}
func printVersion() {
fmt.Println(metadata.GetVersionInfo())
}