Skip to content

Commit

Permalink
Prevent CLI to connect to ordering service on join.
Browse files Browse the repository at this point in the history
Currently peer cli commands tries to get connected to the ordering
service regardless whenever it's needed or not, this commit introduce
lazy initialization and therefore cli connected to the ordering only
when needed.

Change-Id: I590fa69473ec89194e7c609603d19f1de27ab68f
Signed-off-by: Artem Barger <bartem@il.ibm.com>
  • Loading branch information
C0rWin committed Feb 26, 2017
1 parent 6daec3f commit 4246971
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 60 deletions.
16 changes: 11 additions & 5 deletions peer/channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,15 @@ var channelCmd = &cobra.Command{
Long: fmt.Sprintf("%s specific commands.", channelFuncName),
}

type BroadcastClientFactory func() (common.BroadcastClient, error)

// ChannelCmdFactory holds the clients used by ChannelCmdFactory
type ChannelCmdFactory struct {
EndorserClient pb.EndorserClient
Signer msp.SigningIdentity
BroadcastClient common.BroadcastClient
DeliverClient deliverClientIntf
EndorserClient pb.EndorserClient
Signer msp.SigningIdentity
BroadcastClient common.BroadcastClient
DeliverClient deliverClientIntf
BroadcastFactory BroadcastClientFactory
}

// InitCmdFactory init the ChannelCmdFactor with default clients
Expand All @@ -93,7 +96,10 @@ func InitCmdFactory(isOrdererRequired bool) (*ChannelCmdFactory, error) {
return nil, fmt.Errorf("Error getting default signer: %s", err)
}

cmdFact.BroadcastClient, err = common.GetBroadcastClient()
cmdFact.BroadcastFactory = func() (common.BroadcastClient, error) {
return common.GetBroadcastClient()
}

if err != nil {
return nil, fmt.Errorf("Error getting broadcast client: %s", err)
}
Expand Down
10 changes: 7 additions & 3 deletions peer/channel/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,19 @@ func sendCreateChainTransaction(cf *ChannelCmdFactory) error {
return err
}
}
var broadcastClient common.BroadcastClient
broadcastClient, err = cf.BroadcastFactory()
if err != nil {
return err
}

err = cf.BroadcastClient.Send(chCrtEnv)
defer broadcastClient.Close()
err = broadcastClient.Send(chCrtEnv)

return err
}

func executeCreate(cf *ChannelCmdFactory) error {
defer cf.BroadcastClient.Close()

var err error

if err = sendCreateChainTransaction(cf); err != nil {
Expand Down
55 changes: 25 additions & 30 deletions peer/channel/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func initMSP() {
}
}

func mockBroadcastClientFactory() (common.BroadcastClient, error) {
return common.GetMockBroadcastClient(nil), nil
}

func TestCreateChain(t *testing.T) {
InitMSP()

Expand All @@ -89,12 +93,10 @@ func TestCreateChain(t *testing.T) {
t.Fatalf("Get default signer error: %v", err)
}

mockBroadcastClient := common.GetMockBroadcastClient(nil)

mockCF := &ChannelCmdFactory{
BroadcastClient: mockBroadcastClient,
Signer: signer,
DeliverClient: &mockDeliverClient{},
BroadcastFactory: mockBroadcastClientFactory,
Signer: signer,
DeliverClient: &mockDeliverClient{},
}

cmd := createCmd(mockCF)
Expand Down Expand Up @@ -122,12 +124,10 @@ func TestCreateChainWithDefaultAnchorPeers(t *testing.T) {
t.Fatalf("Get default signer error: %v", err)
}

mockBroadcastClient := common.GetMockBroadcastClient(nil)

mockCF := &ChannelCmdFactory{
BroadcastClient: mockBroadcastClient,
Signer: signer,
DeliverClient: &mockDeliverClient{},
BroadcastFactory: mockBroadcastClientFactory,
Signer: signer,
DeliverClient: &mockDeliverClient{},
}

cmd := createCmd(mockCF)
Expand Down Expand Up @@ -156,12 +156,13 @@ func TestCreateChainBCFail(t *testing.T) {
}

sendErr := fmt.Errorf("send create tx failed")
mockBroadcastClient := common.GetMockBroadcastClient(sendErr)

mockCF := &ChannelCmdFactory{
BroadcastClient: mockBroadcastClient,
Signer: signer,
DeliverClient: &mockDeliverClient{},
BroadcastFactory: func() (common.BroadcastClient, error) {
return common.GetMockBroadcastClient(sendErr), nil
},
Signer: signer,
DeliverClient: &mockDeliverClient{},
}

cmd := createCmd(mockCF)
Expand All @@ -173,7 +174,7 @@ func TestCreateChainBCFail(t *testing.T) {

expectedErrMsg := sendErr.Error()
if err := cmd.Execute(); err == nil {
t.Errorf("expected create chain to fail with broadcast error")
t.Error("expected create chain to fail with broadcast error")
} else {
if err.Error() != expectedErrMsg {
t.Errorf("Run create chain get unexpected error: %s(expected %s)", err.Error(), expectedErrMsg)
Expand All @@ -193,14 +194,12 @@ func TestCreateChainDeliverFail(t *testing.T) {
t.Fatalf("Get default signer error: %v", err)
}

mockBroadcastClient := common.GetMockBroadcastClient(nil)

recvErr := fmt.Errorf("deliver create tx failed")

mockCF := &ChannelCmdFactory{
BroadcastClient: mockBroadcastClient,
Signer: signer,
DeliverClient: &mockDeliverClient{recvErr},
BroadcastFactory: mockBroadcastClientFactory,
Signer: signer,
DeliverClient: &mockDeliverClient{recvErr},
}

cmd := createCmd(mockCF)
Expand Down Expand Up @@ -268,12 +267,10 @@ func TestCreateChainFromTx(t *testing.T) {
t.Fatalf("Get default signer error: %v", err)
}

mockBroadcastClient := common.GetMockBroadcastClient(nil)

mockCF := &ChannelCmdFactory{
BroadcastClient: mockBroadcastClient,
Signer: signer,
DeliverClient: &mockDeliverClient{},
BroadcastFactory: mockBroadcastClientFactory,
Signer: signer,
DeliverClient: &mockDeliverClient{},
}

cmd := createCmd(mockCF)
Expand Down Expand Up @@ -314,12 +311,10 @@ func TestCreateChainInvalidTx(t *testing.T) {
t.Fatalf("Get default signer error: %v", err)
}

mockBroadcastClient := common.GetMockBroadcastClient(nil)

mockCF := &ChannelCmdFactory{
BroadcastClient: mockBroadcastClient,
Signer: signer,
DeliverClient: &mockDeliverClient{},
BroadcastFactory: mockBroadcastClientFactory,
Signer: signer,
DeliverClient: &mockDeliverClient{},
}

cmd := createCmd(mockCF)
Expand Down
2 changes: 0 additions & 2 deletions peer/channel/fetchconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ func fetch(cmd *cobra.Command, args []string, cf *ChannelCmdFactory) error {
}
}

defer cf.BroadcastClient.Close()

var block *cb.Block
if block, err = cf.DeliverClient.getBlock(); err != nil {
return err
Expand Down
8 changes: 3 additions & 5 deletions peer/channel/fetchconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ func TestFetchChain(t *testing.T) {
t.Fatalf("Get default signer error: %v", err)
}

mockBroadcastClient := common.GetMockBroadcastClient(nil)

mockCF := &ChannelCmdFactory{
BroadcastClient: mockBroadcastClient,
Signer: signer,
DeliverClient: &mockDeliverClient{},
BroadcastFactory: mockBroadcastClientFactory,
Signer: signer,
DeliverClient: &mockDeliverClient{},
}

cmd := createCmd(mockCF)
Expand Down
24 changes: 9 additions & 15 deletions peer/channel/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ func TestJoin(t *testing.T) {

mockEndorerClient := common.GetMockEndorserClient(mockResponse, nil)

mockBroadcastClient := common.GetMockBroadcastClient(nil)

mockCF := &ChannelCmdFactory{
EndorserClient: mockEndorerClient,
BroadcastClient: mockBroadcastClient,
Signer: signer,
EndorserClient: mockEndorerClient,
BroadcastFactory: mockBroadcastClientFactory,
Signer: signer,
}

cmd := joinCmd(mockCF)
Expand Down Expand Up @@ -79,12 +77,10 @@ func TestJoinNonExistentBlock(t *testing.T) {

mockEndorerClient := common.GetMockEndorserClient(mockResponse, nil)

mockBroadcastClient := common.GetMockBroadcastClient(nil)

mockCF := &ChannelCmdFactory{
EndorserClient: mockEndorerClient,
BroadcastClient: mockBroadcastClient,
Signer: signer,
EndorserClient: mockEndorerClient,
BroadcastFactory: mockBroadcastClientFactory,
Signer: signer,
}

cmd := joinCmd(mockCF)
Expand Down Expand Up @@ -121,12 +117,10 @@ func TestBadProposalResponse(t *testing.T) {

mockEndorerClient := common.GetMockEndorserClient(mockResponse, nil)

mockBroadcastClient := common.GetMockBroadcastClient(nil)

mockCF := &ChannelCmdFactory{
EndorserClient: mockEndorerClient,
BroadcastClient: mockBroadcastClient,
Signer: signer,
EndorserClient: mockEndorerClient,
BroadcastFactory: mockBroadcastClientFactory,
Signer: signer,
}

cmd := joinCmd(mockCF)
Expand Down

0 comments on commit 4246971

Please sign in to comment.