-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
create.go
214 lines (168 loc) · 5.4 KB
/
create.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
/*
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 channel
import (
"fmt"
"io/ioutil"
"errors"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/configtx"
genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
localsigner "github.com/hyperledger/fabric/common/localmsp"
"github.com/hyperledger/fabric/common/util"
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/peer/common"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"
"github.com/spf13/cobra"
)
//ConfigTxFileNotFound channel create configuration tx file not found
type ConfigTxFileNotFound string
const createCmdDescription = "Create a channel"
func (e ConfigTxFileNotFound) Error() string {
return fmt.Sprintf("channel create configuration tx file not found %s", string(e))
}
//InvalidCreateTx invalid channel create transaction
type InvalidCreateTx string
func (e InvalidCreateTx) Error() string {
return fmt.Sprintf("Invalid channel create transaction : %s", string(e))
}
func createCmd(cf *ChannelCmdFactory) *cobra.Command {
createCmd := &cobra.Command{
Use: "create",
Short: createCmdDescription,
Long: createCmdDescription,
RunE: func(cmd *cobra.Command, args []string) error {
return create(cmd, args, cf)
},
}
flagList := []string{
"channelID",
"file",
"timeout",
}
attachFlags(createCmd, flagList)
return createCmd
}
func createChannelFromDefaults(cf *ChannelCmdFactory) (*cb.Envelope, error) {
signer, err := mspmgmt.GetLocalMSP().GetDefaultSigningIdentity()
if err != nil {
return nil, err
}
chCrtEnv, err := configtx.MakeChainCreationTransaction(chainID, genesisconfig.SampleConsortiumName, signer)
if err != nil {
return nil, err
}
return chCrtEnv, nil
}
func createChannelFromConfigTx(configTxFileName string) (*cb.Envelope, error) {
cftx, err := ioutil.ReadFile(configTxFileName)
if err != nil {
return nil, ConfigTxFileNotFound(err.Error())
}
return utils.UnmarshalEnvelope(cftx)
}
func sanityCheckAndSignConfigTx(envConfigUpdate *cb.Envelope) (*cb.Envelope, error) {
payload, err := utils.ExtractPayload(envConfigUpdate)
if err != nil {
return nil, InvalidCreateTx("bad payload")
}
if payload.Header == nil || payload.Header.ChannelHeader == nil {
return nil, InvalidCreateTx("bad header")
}
ch, err := utils.UnmarshalChannelHeader(payload.Header.ChannelHeader)
if err != nil {
return nil, InvalidCreateTx("could not unmarshall channel header")
}
if ch.Type != int32(cb.HeaderType_CONFIG_UPDATE) {
return nil, InvalidCreateTx("bad type")
}
if ch.ChannelId == "" {
return nil, InvalidCreateTx("empty channel id")
}
if ch.ChannelId != chainID {
return nil, InvalidCreateTx(fmt.Sprintf("mismatched channel ID %s != %s", ch.ChannelId, chainID))
}
configUpdateEnv, err := configtx.UnmarshalConfigUpdateEnvelope(payload.Data)
if err != nil {
return nil, InvalidCreateTx("Bad config update env")
}
signer := localsigner.NewSigner()
sigHeader, err := signer.NewSignatureHeader()
if err != nil {
return nil, err
}
configSig := &cb.ConfigSignature{
SignatureHeader: utils.MarshalOrPanic(sigHeader),
}
configSig.Signature, err = signer.Sign(util.ConcatenateBytes(configSig.SignatureHeader, configUpdateEnv.ConfigUpdate))
configUpdateEnv.Signatures = append(configUpdateEnv.Signatures, configSig)
return utils.CreateSignedEnvelope(cb.HeaderType_CONFIG_UPDATE, chainID, signer, configUpdateEnv, 0, 0)
}
func sendCreateChainTransaction(cf *ChannelCmdFactory) error {
var err error
var chCrtEnv *cb.Envelope
if channelTxFile != "" {
if chCrtEnv, err = createChannelFromConfigTx(channelTxFile); err != nil {
return err
}
} else {
if chCrtEnv, err = createChannelFromDefaults(cf); err != nil {
return err
}
}
if chCrtEnv, err = sanityCheckAndSignConfigTx(chCrtEnv); err != nil {
return err
}
var broadcastClient common.BroadcastClient
broadcastClient, err = cf.BroadcastFactory()
if err != nil {
return fmt.Errorf("Error getting broadcast client: %s", err)
}
defer broadcastClient.Close()
err = broadcastClient.Send(chCrtEnv)
return err
}
func executeCreate(cf *ChannelCmdFactory) error {
var err error
if err = sendCreateChainTransaction(cf); err != nil {
return err
}
var block *cb.Block
if block, err = getGenesisBlock(cf); err != nil {
return err
}
b, err := proto.Marshal(block)
if err != nil {
return err
}
file := chainID + ".block"
if err = ioutil.WriteFile(file, b, 0644); err != nil {
return err
}
return nil
}
func create(cmd *cobra.Command, args []string, cf *ChannelCmdFactory) error {
//the global chainID filled by the "-c" command
if chainID == common.UndefinedParamValue {
return errors.New("Must supply channel ID")
}
var err error
if cf == nil {
cf, err = InitCmdFactory(EndorserNotRequired, OrdererRequired)
if err != nil {
return err
}
}
return executeCreate(cf)
}