Skip to content

Commit 12e85d8

Browse files
committed
[FAB-1878]: Add fetch config CLI command
Adding an ability to fetch configuration block, usefull for complex setups such as many orgs having many peers. The syntax is following: peer channel fetch -c chainID Change-Id: I20ef7091be0c76e42d8fb1a4913e8f3f8e7d5748 Signed-off-by: Artem Barger <[email protected]>
1 parent 62eac5b commit 12e85d8

File tree

4 files changed

+148
-5
lines changed

4 files changed

+148
-5
lines changed

peer/channel/channel.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func Cmd(cf *ChannelCmdFactory) *cobra.Command {
5353
AddFlags(channelCmd)
5454
channelCmd.AddCommand(joinCmd(cf))
5555
channelCmd.AddCommand(createCmd(cf))
56+
channelCmd.AddCommand(fetchCmd(cf))
5657

5758
return channelCmd
5859
}
@@ -80,7 +81,7 @@ type ChannelCmdFactory struct {
8081
}
8182

8283
// InitCmdFactory init the ChannelCmdFactor with default clients
83-
func InitCmdFactory(forJoin bool) (*ChannelCmdFactory, error) {
84+
func InitCmdFactory(isOrdererRequired bool) (*ChannelCmdFactory, error) {
8485
var err error
8586

8687
cmdFact := &ChannelCmdFactory{}
@@ -96,7 +97,7 @@ func InitCmdFactory(forJoin bool) (*ChannelCmdFactory, error) {
9697
}
9798

9899
//for join, we need the endorser as well
99-
if forJoin {
100+
if isOrdererRequired {
100101
cmdFact.EndorserClient, err = common.GetEndorserClient()
101102
if err != nil {
102103
return nil, fmt.Errorf("Error getting endorser client %s: %s", channelFuncName, err)

peer/channel/create.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ import (
2020
"io/ioutil"
2121
"time"
2222

23+
"github.com/golang/protobuf/proto"
2324
"github.com/hyperledger/fabric/common/configtx"
2425
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
26+
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
2527
"github.com/hyperledger/fabric/orderer/common/bootstrap/provisional"
2628
cb "github.com/hyperledger/fabric/protos/common"
2729
"github.com/hyperledger/fabric/protos/utils"
28-
29-
"github.com/golang/protobuf/proto"
30-
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
3130
"github.com/spf13/cobra"
3231
)
3332

peer/channel/fetchconfig.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
22+
"github.com/golang/protobuf/proto"
23+
cb "github.com/hyperledger/fabric/protos/common"
24+
"github.com/spf13/cobra"
25+
)
26+
27+
func fetchCmd(cf *ChannelCmdFactory) *cobra.Command {
28+
createCmd := &cobra.Command{
29+
Use: "fetch",
30+
Short: "Fetch configuration block.",
31+
Long: `Fetch configuration block.`,
32+
RunE: func(cmd *cobra.Command, args []string) error {
33+
return fetch(cmd, args, cf)
34+
},
35+
}
36+
37+
return createCmd
38+
}
39+
40+
func fetch(cmd *cobra.Command, args []string, cf *ChannelCmdFactory) error {
41+
var err error
42+
if cf == nil {
43+
cf, err = InitCmdFactory(false)
44+
if err != nil {
45+
return err
46+
}
47+
}
48+
49+
defer cf.BroadcastClient.Close()
50+
51+
var block *cb.Block
52+
if block, err = cf.DeliverClient.getBlock(); err != nil {
53+
return err
54+
}
55+
56+
b, err := proto.Marshal(block)
57+
if err != nil {
58+
return err
59+
}
60+
61+
file := chainID + ".block"
62+
if err = ioutil.WriteFile(file, b, 0644); err != nil {
63+
return err
64+
}
65+
66+
return nil
67+
}

peer/channel/fetchconfig_test.go

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
"os"
21+
"testing"
22+
23+
"github.com/hyperledger/fabric/peer/common"
24+
)
25+
26+
func TestFetchChain(t *testing.T) {
27+
InitMSP()
28+
29+
mockchain := "mockchain"
30+
31+
signer, err := common.GetDefaultSigner()
32+
if err != nil {
33+
t.Fatalf("Get default signer error: %v", err)
34+
}
35+
36+
mockBroadcastClient := common.GetMockBroadcastClient(nil)
37+
38+
mockCF := &ChannelCmdFactory{
39+
BroadcastClient: mockBroadcastClient,
40+
Signer: signer,
41+
DeliverClient: &mockDeliverClient{},
42+
}
43+
44+
cmd := createCmd(mockCF)
45+
46+
AddFlags(cmd)
47+
48+
args := []string{"-c", mockchain}
49+
cmd.SetArgs(args)
50+
51+
if err := cmd.Execute(); err != nil {
52+
t.Fail()
53+
t.Errorf("expected join command to succeed")
54+
}
55+
56+
os.Remove(mockchain + ".block")
57+
58+
cmd = fetchCmd(mockCF)
59+
defer os.Remove(mockchain + ".block")
60+
61+
AddFlags(cmd)
62+
63+
args = []string{"-c", mockchain}
64+
cmd.SetArgs(args)
65+
66+
if err := cmd.Execute(); err != nil {
67+
t.Fail()
68+
t.Errorf("expected join command to succeed")
69+
}
70+
71+
if _, err := os.Stat(mockchain + ".block"); os.IsNotExist(err) {
72+
// path/to/whatever does not exist
73+
t.Fail()
74+
t.Error("expected configuration block to be fetched")
75+
}
76+
}

0 commit comments

Comments
 (0)