From 1876110c8488ddce704d7f692b634ce9c5da1d0d Mon Sep 17 00:00:00 2001 From: Emir Heidinger Date: Thu, 29 Jun 2017 11:54:21 -0400 Subject: [PATCH] [FAB-5109] Extracted primary peer joined logic also fixed the temp fix from FAB-3493 Change-Id: I5dca3278fa1ccd882bd68509f01953d04dba08e0 Signed-off-by: Emir Heidinger --- fabric-txn/admin/transactionconfig.go | 54 ++++++--------------------- pkg/fabric-client/channel/channel.go | 4 +- test/integration/base_test_setup.go | 26 ++++++++++--- test/integration/utils.go | 24 ++++++++++++ 4 files changed, 58 insertions(+), 50 deletions(-) diff --git a/fabric-txn/admin/transactionconfig.go b/fabric-txn/admin/transactionconfig.go index 1619a1a2e0..e213a42dbf 100644 --- a/fabric-txn/admin/transactionconfig.go +++ b/fabric-txn/admin/transactionconfig.go @@ -75,31 +75,15 @@ func SendInstantiateCC(channel api.Channel, chainCodeID string, channelID string return nil } -// CreateChannel ... -func CreateChannel(client api.FabricClient, ordererUser api.User, orgUser api.User, channel api.Channel, channelConfig string) error { - // Check if primary peer has joined this channel - var foundChannel bool - primaryPeer := channel.GetPrimaryPeer() - client.SetUserContext(orgUser) - response, err := client.QueryChannels(primaryPeer) - if err != nil { - return fmt.Errorf("Error querying channels for primary peer: %s", err) - } - for _, responseChannel := range response.Channels { - if responseChannel.ChannelId == channel.GetName() { - foundChannel = true - } - } +// CreateOrUpdateChannel creates a channel if it does not exist or updates a channel +// if it does and a different channelConfig is used +func CreateOrUpdateChannel(client api.FabricClient, ordererUser api.User, orgUser api.User, channel api.Channel, channelConfig string) error { + logger.Debugf("***** Creating or updating channel: %s *****\n", channel.GetName()) - if foundChannel { - // There's no need to create a channel, initialize the channel from the orderer and return - if err = channel.Initialize(nil); err != nil { - return fmt.Errorf("Error initializing channel: %v", err) - } - return nil - } + currentUser := client.GetUserContext() + defer client.SetUserContext(currentUser) - logger.Debugf("***** Creating channel: %s *****\n", channel.GetName()) + client.SetUserContext(orgUser) configTx, err := ioutil.ReadFile(channelConfig) if err != nil { @@ -144,32 +128,18 @@ func CreateChannel(client api.FabricClient, ordererUser api.User, orgUser api.Us client.SetUserContext(ordererUser) err = client.CreateChannel(&request) if err != nil { - return fmt.Errorf("CreateChannel returned error") + return fmt.Errorf("CreateChannel returned error: %v", err) } return nil } -// JoinChannel ... +// JoinChannel joins a channel that has already been created func JoinChannel(client api.FabricClient, orgUser api.User, channel api.Channel) error { - // Check if primary peer has joined this channel - var foundChannel bool - primaryPeer := channel.GetPrimaryPeer() - client.SetUserContext(orgUser) - response, err := client.QueryChannels(primaryPeer) - if err != nil { - return fmt.Errorf("Error querying channels for primary peer: %s", err) - } - for _, responseChannel := range response.Channels { - if responseChannel.ChannelId == channel.GetName() { - foundChannel = true - } - } + currentUser := client.GetUserContext() + defer client.SetUserContext(currentUser) - if foundChannel { - // no need to join channel - return nil - } + client.SetUserContext(orgUser) creator, err := client.GetIdentity() if err != nil { diff --git a/pkg/fabric-client/channel/channel.go b/pkg/fabric-client/channel/channel.go index ea5ea999c2..cdff937f31 100644 --- a/pkg/fabric-client/channel/channel.go +++ b/pkg/fabric-client/channel/channel.go @@ -516,8 +516,8 @@ func (c *channel) getChannelConfig() (*common.ConfigEnvelope, error) { logger.Debugf("GetChannelConfig - Last config index: %d\n", lastConfig.Index) // Get the last config block - //block, err = c.getBlock(NewSpecificSeekPosition(lastConfig.Index)) - block, err = c.getBlock(fc.NewSpecificSeekPosition(0)) //FIXME: temporary hack to workaround https://jira.hyperledger.org/browse/FAB-3493 + block, err = c.getBlock(fc.NewSpecificSeekPosition(lastConfig.Index)) + if err != nil { return nil, fmt.Errorf("Unable to retrieve block at index %d: %v", lastConfig.Index, err) } diff --git a/test/integration/base_test_setup.go b/test/integration/base_test_setup.go index ee7c1cbf44..1e4f175e06 100644 --- a/test/integration/base_test_setup.go +++ b/test/integration/base_test_setup.go @@ -89,13 +89,27 @@ func (setup *BaseSetupImpl) Initialize() error { return fmt.Errorf("Error getting orderer admin user: %v", err) } - // Create and join channel - if err = admin.CreateChannel(client, ordererAdmin, org1Admin, channel, setup.ChannelConfig); err != nil { - return fmt.Errorf("CreateChannel returned error: %v", err) + // Check if primary peer has joined channel + alreadyJoined, err := HasPrimaryPeerJoinedChannel(client, org1Admin, channel) + if err != nil { + return fmt.Errorf("Error while checking if primary peer has already joined channel: %v", err) } - time.Sleep(time.Second * 3) - if err = admin.JoinChannel(client, org1Admin, channel); err != nil { - return fmt.Errorf("JoinChannel returned error: %v", err) + + if !alreadyJoined { + // Create, initialize and join channel + if err = admin.CreateOrUpdateChannel(client, ordererAdmin, org1Admin, channel, setup.ChannelConfig); err != nil { + return fmt.Errorf("CreateChannel returned error: %v", err) + } + time.Sleep(time.Second * 3) + + client.SetUserContext(org1Admin) + if err = channel.Initialize(nil); err != nil { + return fmt.Errorf("Error initializing channel: %v", err) + } + + if err = admin.JoinChannel(client, org1Admin, channel); err != nil { + return fmt.Errorf("JoinChannel returned error: %v", err) + } } //by default client's user context should use regular user, for admin actions, UserContext must be set to AdminUser diff --git a/test/integration/utils.go b/test/integration/utils.go index cf0ebf54f3..2348fe058b 100644 --- a/test/integration/utils.go +++ b/test/integration/utils.go @@ -101,3 +101,27 @@ func getFirstPathFromDir(dir string) (string, error) { return "", fmt.Errorf("No paths found in directory: %s", dir) } + +// HasPrimaryPeerJoinedChannel checks whether the primary peer of a channel +// has already joined the channel. It returns true if it has, false otherwise, +// or an error +func HasPrimaryPeerJoinedChannel(client api.FabricClient, orgUser api.User, channel api.Channel) (bool, error) { + foundChannel := false + primaryPeer := channel.GetPrimaryPeer() + + currentUser := client.GetUserContext() + defer client.SetUserContext(currentUser) + + client.SetUserContext(orgUser) + response, err := client.QueryChannels(primaryPeer) + if err != nil { + return false, fmt.Errorf("Error querying channel for primary peer: %s", err) + } + for _, responseChannel := range response.Channels { + if responseChannel.ChannelId == channel.GetName() { + foundChannel = true + } + } + + return foundChannel, nil +}