Skip to content

Commit

Permalink
[FAB-10605] Choose Selection Service using capabilities
Browse files Browse the repository at this point in the history
The SDK will use the Fabric Selection service if the channel
has the V1_2 (or greater) capability, otherwise it will
use the selection service built into the SDK.

Change-Id: I40216a2572f28103ceecd8f0ef61f8ec336683d3
Signed-off-by: Bob Stasyszyn <Bob.Stasyszyn@securekey.com>
  • Loading branch information
bstasyszyn committed Jun 12, 2018
1 parent 74c04ea commit c235146
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
7 changes: 7 additions & 0 deletions pkg/fabsdk/provider/chpvdr/chprovider.go
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/discovery/dynamicdiscovery"
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/discovery/staticdiscovery"
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/selection/dynamicselection"
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/selection/fabricselection"
"github.com/hyperledger/fabric-sdk-go/pkg/common/logging"
"github.com/hyperledger/fabric-sdk-go/pkg/common/options"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
Expand Down Expand Up @@ -145,6 +146,7 @@ func (cp *ChannelProvider) createEventClient(ctx context.Client, chConfig fab.Ch

func (cp *ChannelProvider) createDiscoveryService(ctx context.Client, chConfig fab.ChannelCfg) (fab.DiscoveryService, error) {
if chConfig.HasCapability(fab.ApplicationGroupKey, fab.V1_2Capability) {
logger.Debugf("Using Dynamic Discovery based on V1_2 capability.")
return dynamicdiscovery.NewChannelService(ctx, chConfig.ID())
}
return staticdiscovery.NewService(ctx.EndpointConfig(), ctx.InfraProvider(), chConfig.ID())
Expand All @@ -171,6 +173,11 @@ func (cp *ChannelProvider) createSelectionService(ctx context.Client, chConfig f
if err != nil {
return nil, err
}

if chConfig.HasCapability(fab.ApplicationGroupKey, fab.V1_2Capability) {
logger.Debugf("Using Fabric Selection based on V1_2 capability.")
return fabricselection.New(ctx, chConfig.ID(), discovery)
}
return dynamicselection.NewService(ctx, chConfig.ID(), discovery)
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/fabsdk/provider/chpvdr/chprovider_test.go
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/discovery/dynamicdiscovery"
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/discovery/staticdiscovery"
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/selection/dynamicselection"
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/selection/fabricselection"

"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
Expand Down Expand Up @@ -105,6 +106,12 @@ func TestBasicValidChannel(t *testing.T) {
require.NotNil(t, discovery)
_, ok = discovery.(*dynamicdiscovery.ChannelService)
assert.Truef(t, ok, "Expecting discovery to be Dynamic for v1_2")

selection, err = channelService.Selection()
require.NoError(t, err)
require.NotNil(t, selection)
_, ok = selection.(*fabricselection.Service)
assert.Truef(t, ok, "Expecting selection to be Fabric for v1_2")
}

func TestResolveEventServiceType(t *testing.T) {
Expand Down
10 changes: 7 additions & 3 deletions test/integration/sdk/channel_client_test.go
Expand Up @@ -353,14 +353,18 @@ func TestNoEndpoints(t *testing.T) {
// Test query chaincode: since peer has been disabled for chaincode query this query should fail
_, err = chClient.Query(channel.Request{ChaincodeID: mainChaincodeID, Fcn: "invoke", Args: integration.ExampleCCQueryArgs()},
channel.WithRetry(retry.DefaultChannelOpts))
if err == nil || !strings.Contains(err.Error(), "targets were not provided") {
require.Error(t, err)

expected1_1Err := "targets were not provided" // When running with 1.1 DynamicSelection
expected1_2Err := "no endorsement combination can be satisfied" // When running with 1.2 FabricSelection
if !strings.Contains(err.Error(), expected1_1Err) && !strings.Contains(err.Error(), expected1_2Err) {
t.Fatal("Should have failed due to no chaincode query peers")
}

// Test execute transaction: since peer has been disabled for endorsement this transaction should fail
_, err = chClient.Execute(channel.Request{ChaincodeID: mainChaincodeID, Fcn: "invoke", Args: integration.ExampleCCTxArgs()},
channel.WithRetry(retry.DefaultChannelOpts))
if err == nil || !strings.Contains(err.Error(), "targets were not provided") {
t.Fatal("Should have failed due to no endorsing peers")
if !strings.Contains(err.Error(), expected1_1Err) && !strings.Contains(err.Error(), expected1_2Err) {
t.Fatal("Should have failed due to no chaincode query peers")
}
}

0 comments on commit c235146

Please sign in to comment.