Skip to content

Commit

Permalink
[FAB-5109] Extracted primary peer joined logic
Browse files Browse the repository at this point in the history
also fixed the temp fix from FAB-3493

Change-Id: I5dca3278fa1ccd882bd68509f01953d04dba08e0
Signed-off-by: Emir Heidinger <emir.heidinger@securekey.com>
  • Loading branch information
emirsh committed Jun 29, 2017
1 parent 536c637 commit 1876110
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 50 deletions.
54 changes: 12 additions & 42 deletions fabric-txn/admin/transactionconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/fabric-client/channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
26 changes: 20 additions & 6 deletions test/integration/base_test_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions test/integration/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 1876110

Please sign in to comment.