Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit c235146

Browse files
committed
[FAB-10605] Choose Selection Service using capabilities
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>
1 parent 74c04ea commit c235146

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

pkg/fabsdk/provider/chpvdr/chprovider.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/discovery/dynamicdiscovery"
1313
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/discovery/staticdiscovery"
1414
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/selection/dynamicselection"
15+
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/selection/fabricselection"
1516
"github.com/hyperledger/fabric-sdk-go/pkg/common/logging"
1617
"github.com/hyperledger/fabric-sdk-go/pkg/common/options"
1718
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
@@ -145,6 +146,7 @@ func (cp *ChannelProvider) createEventClient(ctx context.Client, chConfig fab.Ch
145146

146147
func (cp *ChannelProvider) createDiscoveryService(ctx context.Client, chConfig fab.ChannelCfg) (fab.DiscoveryService, error) {
147148
if chConfig.HasCapability(fab.ApplicationGroupKey, fab.V1_2Capability) {
149+
logger.Debugf("Using Dynamic Discovery based on V1_2 capability.")
148150
return dynamicdiscovery.NewChannelService(ctx, chConfig.ID())
149151
}
150152
return staticdiscovery.NewService(ctx.EndpointConfig(), ctx.InfraProvider(), chConfig.ID())
@@ -171,6 +173,11 @@ func (cp *ChannelProvider) createSelectionService(ctx context.Client, chConfig f
171173
if err != nil {
172174
return nil, err
173175
}
176+
177+
if chConfig.HasCapability(fab.ApplicationGroupKey, fab.V1_2Capability) {
178+
logger.Debugf("Using Fabric Selection based on V1_2 capability.")
179+
return fabricselection.New(ctx, chConfig.ID(), discovery)
180+
}
174181
return dynamicselection.NewService(ctx, chConfig.ID(), discovery)
175182
}
176183

pkg/fabsdk/provider/chpvdr/chprovider_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/discovery/dynamicdiscovery"
1515
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/discovery/staticdiscovery"
1616
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/selection/dynamicselection"
17+
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/selection/fabricselection"
1718

1819
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
1920
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
@@ -105,6 +106,12 @@ func TestBasicValidChannel(t *testing.T) {
105106
require.NotNil(t, discovery)
106107
_, ok = discovery.(*dynamicdiscovery.ChannelService)
107108
assert.Truef(t, ok, "Expecting discovery to be Dynamic for v1_2")
109+
110+
selection, err = channelService.Selection()
111+
require.NoError(t, err)
112+
require.NotNil(t, selection)
113+
_, ok = selection.(*fabricselection.Service)
114+
assert.Truef(t, ok, "Expecting selection to be Fabric for v1_2")
108115
}
109116

110117
func TestResolveEventServiceType(t *testing.T) {

test/integration/sdk/channel_client_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,14 +353,18 @@ func TestNoEndpoints(t *testing.T) {
353353
// Test query chaincode: since peer has been disabled for chaincode query this query should fail
354354
_, err = chClient.Query(channel.Request{ChaincodeID: mainChaincodeID, Fcn: "invoke", Args: integration.ExampleCCQueryArgs()},
355355
channel.WithRetry(retry.DefaultChannelOpts))
356-
if err == nil || !strings.Contains(err.Error(), "targets were not provided") {
356+
require.Error(t, err)
357+
358+
expected1_1Err := "targets were not provided" // When running with 1.1 DynamicSelection
359+
expected1_2Err := "no endorsement combination can be satisfied" // When running with 1.2 FabricSelection
360+
if !strings.Contains(err.Error(), expected1_1Err) && !strings.Contains(err.Error(), expected1_2Err) {
357361
t.Fatal("Should have failed due to no chaincode query peers")
358362
}
359363

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

0 commit comments

Comments
 (0)