Skip to content

Commit 531de02

Browse files
author
Jason Yellick
committed
[FAB-4265] Fix overloaded peer channel create cmd
The peer channel create command is currently being used in the e2e tests to perform a configuration update. This is very unintuitive and gives the wrong impression to anyone looking at the e2e example. This CR simply adds an 'update' command to peer channel to make the operation more clear. Change-Id: I33244affb5cd013ca878123b2bd7c8b2c00690b6 Signed-off-by: Jason Yellick <[email protected]>
1 parent 389ff3e commit 531de02

File tree

6 files changed

+249
-10
lines changed

6 files changed

+249
-10
lines changed

examples/e2e_cli/scripts/script.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ updateAnchorPeers() {
7272
setGlobals $PEER
7373

7474
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
75-
peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx >&log.txt
75+
peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx >&log.txt
7676
else
77-
peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA >&log.txt
77+
peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA >&log.txt
7878
fi
7979
res=$?
8080
cat log.txt

peer/channel/channel.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ import (
3232

3333
const (
3434
channelFuncName = "channel"
35-
shortDes = "Operate a channel: create|fetch|join|list."
36-
longDes = "Operate a channel: create|fetch|join|list."
35+
shortDes = "Operate a channel: create|fetch|update|join|list."
36+
longDes = "Operate a channel: create|fetch|update|join|list."
3737
)
3838

3939
type OrdererRequirement bool
@@ -65,6 +65,7 @@ func Cmd(cf *ChannelCmdFactory) *cobra.Command {
6565
AddFlags(channelCmd)
6666
channelCmd.AddCommand(joinCmd(cf))
6767
channelCmd.AddCommand(createCmd(cf))
68+
channelCmd.AddCommand(updateCmd(cf))
6869
channelCmd.AddCommand(fetchCmd(cf))
6970
channelCmd.AddCommand(listCmd(cf))
7071

peer/channel/create.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func createChannelFromConfigTx(configTxFileName string) (*cb.Envelope, error) {
8787
return utils.UnmarshalEnvelope(cftx)
8888
}
8989

90-
func sanityCheckAndSignChannelCreateTx(envConfigUpdate *cb.Envelope) (*cb.Envelope, error) {
90+
func sanityCheckAndSignConfigTx(envConfigUpdate *cb.Envelope) (*cb.Envelope, error) {
9191
payload, err := utils.ExtractPayload(envConfigUpdate)
9292
if err != nil {
9393
return nil, InvalidCreateTx("bad payload")
@@ -150,7 +150,7 @@ func sendCreateChainTransaction(cf *ChannelCmdFactory) error {
150150
}
151151
}
152152

153-
if chCrtEnv, err = sanityCheckAndSignChannelCreateTx(chCrtEnv); err != nil {
153+
if chCrtEnv, err = sanityCheckAndSignConfigTx(chCrtEnv); err != nil {
154154
return err
155155
}
156156

peer/channel/create_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ func TestSanityCheckAndSignChannelCreateTx(t *testing.T) {
551551
env := &cb.Envelope{}
552552
env.Payload = make([]byte, 10)
553553
var err error
554-
env, err = sanityCheckAndSignChannelCreateTx(env)
554+
env, err = sanityCheckAndSignConfigTx(env)
555555
assert.Error(t, err, "Error expected for nil payload")
556556
assert.Contains(t, err.Error(), "bad payload")
557557

@@ -560,7 +560,7 @@ func TestSanityCheckAndSignChannelCreateTx(t *testing.T) {
560560
data, err1 := proto.Marshal(p)
561561
assert.NoError(t, err1)
562562
env = &cb.Envelope{Payload: data}
563-
env, err = sanityCheckAndSignChannelCreateTx(env)
563+
env, err = sanityCheckAndSignConfigTx(env)
564564
assert.Error(t, err, "Error expected for bad payload header")
565565
assert.Contains(t, err.Error(), "bad header")
566566

@@ -570,7 +570,7 @@ func TestSanityCheckAndSignChannelCreateTx(t *testing.T) {
570570
data, err = proto.Marshal(p)
571571
assert.NoError(t, err)
572572
env = &cb.Envelope{Payload: data}
573-
env, err = sanityCheckAndSignChannelCreateTx(env)
573+
env, err = sanityCheckAndSignConfigTx(env)
574574
assert.Error(t, err, "Error expected for bad channel header")
575575
assert.Contains(t, err.Error(), "could not unmarshall channel header")
576576

@@ -588,7 +588,7 @@ func TestSanityCheckAndSignChannelCreateTx(t *testing.T) {
588588
data, err = proto.Marshal(p)
589589
assert.NoError(t, err)
590590
env = &cb.Envelope{Payload: data}
591-
env, err = sanityCheckAndSignChannelCreateTx(env)
591+
env, err = sanityCheckAndSignConfigTx(env)
592592
assert.Error(t, err, "Error expected for bad payload data")
593593
assert.Contains(t, err.Error(), "Bad config update env")
594594
}

peer/channel/update.go

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
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.
15+
*/
16+
17+
package channel
18+
19+
import (
20+
"fmt"
21+
"io/ioutil"
22+
23+
"errors"
24+
25+
"github.com/hyperledger/fabric/peer/common"
26+
"github.com/hyperledger/fabric/protos/utils"
27+
28+
"github.com/spf13/cobra"
29+
)
30+
31+
func updateCmd(cf *ChannelCmdFactory) *cobra.Command {
32+
updateCmd := &cobra.Command{
33+
Use: "update",
34+
Short: "Send a configtx update.",
35+
Long: "Signs and sends the supplied configtx update file to the channel. Requires '-f', '-o', '-c'.",
36+
RunE: func(cmd *cobra.Command, args []string) error {
37+
return update(cmd, args, cf)
38+
},
39+
}
40+
41+
return updateCmd
42+
}
43+
44+
func update(cmd *cobra.Command, args []string, cf *ChannelCmdFactory) error {
45+
//the global chainID filled by the "-c" command
46+
if chainID == common.UndefinedParamValue {
47+
return errors.New("Must supply channel ID")
48+
}
49+
50+
if channelTxFile == "" {
51+
return InvalidCreateTx("No configtx file name supplied")
52+
}
53+
54+
var err error
55+
if cf == nil {
56+
cf, err = InitCmdFactory(EndorserNotRequired, OrdererRequired)
57+
if err != nil {
58+
return err
59+
}
60+
}
61+
62+
fileData, err := ioutil.ReadFile(channelTxFile)
63+
if err != nil {
64+
return ConfigTxFileNotFound(err.Error())
65+
}
66+
67+
ctxEnv, err := utils.UnmarshalEnvelope(fileData)
68+
if err != nil {
69+
return err
70+
}
71+
72+
sCtxEnv, err := sanityCheckAndSignConfigTx(ctxEnv)
73+
if err != nil {
74+
return err
75+
}
76+
77+
var broadcastClient common.BroadcastClient
78+
broadcastClient, err = cf.BroadcastFactory()
79+
if err != nil {
80+
return fmt.Errorf("Error getting broadcast client: %s", err)
81+
}
82+
83+
defer broadcastClient.Close()
84+
return broadcastClient.Send(sCtxEnv)
85+
}

peer/channel/update_test.go

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
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.
15+
*/
16+
17+
package channel
18+
19+
import (
20+
"io/ioutil"
21+
"os"
22+
"path/filepath"
23+
"testing"
24+
25+
"github.com/hyperledger/fabric/peer/common"
26+
cb "github.com/hyperledger/fabric/protos/common"
27+
28+
"github.com/stretchr/testify/assert"
29+
)
30+
31+
func TestUpdateChannel(t *testing.T) {
32+
InitMSP()
33+
34+
dir, err := ioutil.TempDir("/tmp", "createinvaltest-")
35+
if err != nil {
36+
t.Fatalf("couldn't create temp dir")
37+
}
38+
defer os.RemoveAll(dir) // clean up
39+
40+
mockchannel := "mockchannel"
41+
42+
configtxFile := filepath.Join(dir, mockchannel)
43+
if _, err = createTxFile(configtxFile, cb.HeaderType_CONFIG_UPDATE, mockchannel); err != nil {
44+
t.Fatalf("couldn't create tx file")
45+
}
46+
47+
signer, err := common.GetDefaultSigner()
48+
if err != nil {
49+
t.Fatalf("Get default signer error: %v", err)
50+
}
51+
52+
mockCF := &ChannelCmdFactory{
53+
BroadcastFactory: mockBroadcastClientFactory,
54+
Signer: signer,
55+
DeliverClient: &mockDeliverClient{},
56+
}
57+
58+
cmd := updateCmd(mockCF)
59+
60+
AddFlags(cmd)
61+
62+
args := []string{"-c", mockchannel, "-f", configtxFile, "-o", "localhost:7050"}
63+
cmd.SetArgs(args)
64+
65+
assert.NoError(t, cmd.Execute())
66+
}
67+
68+
func TestUpdateChannelMissingConfigTxFlag(t *testing.T) {
69+
InitMSP()
70+
mockchannel := "mockchannel"
71+
72+
signer, err := common.GetDefaultSigner()
73+
if err != nil {
74+
t.Fatalf("Get default signer error: %v", err)
75+
}
76+
77+
mockCF := &ChannelCmdFactory{
78+
BroadcastFactory: mockBroadcastClientFactory,
79+
Signer: signer,
80+
DeliverClient: &mockDeliverClient{},
81+
}
82+
83+
cmd := updateCmd(mockCF)
84+
85+
AddFlags(cmd)
86+
87+
args := []string{"-c", mockchannel, "-o", "localhost:7050"}
88+
cmd.SetArgs(args)
89+
90+
assert.Error(t, cmd.Execute())
91+
}
92+
93+
func TestUpdateChannelMissingConfigTxFile(t *testing.T) {
94+
InitMSP()
95+
mockchannel := "mockchannel"
96+
97+
signer, err := common.GetDefaultSigner()
98+
if err != nil {
99+
t.Fatalf("Get default signer error: %v", err)
100+
}
101+
102+
mockCF := &ChannelCmdFactory{
103+
BroadcastFactory: mockBroadcastClientFactory,
104+
Signer: signer,
105+
DeliverClient: &mockDeliverClient{},
106+
}
107+
108+
cmd := createCmd(mockCF)
109+
110+
AddFlags(cmd)
111+
112+
args := []string{"-c", mockchannel, "-f", "Non-existant", "-o", "localhost:7050"}
113+
cmd.SetArgs(args)
114+
115+
assert.Error(t, cmd.Execute())
116+
}
117+
118+
func TestUpdateChannelMissingChannelID(t *testing.T) {
119+
InitMSP()
120+
121+
dir, err := ioutil.TempDir("/tmp", "createinvaltest-")
122+
if err != nil {
123+
t.Fatalf("couldn't create temp dir")
124+
}
125+
defer os.RemoveAll(dir) // clean up
126+
127+
mockchannel := "mockchannel"
128+
129+
configtxFile := filepath.Join(dir, mockchannel)
130+
if _, err = createTxFile(configtxFile, cb.HeaderType_CONFIG_UPDATE, mockchannel); err != nil {
131+
t.Fatalf("couldn't create tx file")
132+
}
133+
134+
signer, err := common.GetDefaultSigner()
135+
if err != nil {
136+
t.Fatalf("Get default signer error: %v", err)
137+
}
138+
139+
mockCF := &ChannelCmdFactory{
140+
BroadcastFactory: mockBroadcastClientFactory,
141+
Signer: signer,
142+
DeliverClient: &mockDeliverClient{},
143+
}
144+
145+
cmd := updateCmd(mockCF)
146+
147+
AddFlags(cmd)
148+
149+
args := []string{"-f", configtxFile, "-o", "localhost:7050"}
150+
cmd.SetArgs(args)
151+
152+
assert.Error(t, cmd.Execute())
153+
}

0 commit comments

Comments
 (0)