Skip to content

Commit

Permalink
[FAB-10180] Discovery service based on capabilities
Browse files Browse the repository at this point in the history
Channel capabilities should be used to determine
whether to use the Dynamic Discovery service or the
Static Discovery service.

Change-Id: I380159f13db8ad6600e5328eb22e95895f89de67
Signed-off-by: Bob Stasyszyn <Bob.Stasyszyn@securekey.com>
  • Loading branch information
bstasyszyn authored and troyronda committed May 31, 2018
1 parent 29d1334 commit 68c1ad0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
7 changes: 6 additions & 1 deletion pkg/common/errors/status/status.go
Expand Up @@ -116,7 +116,12 @@ func FromError(err error) (s *Status, ok bool) {
return s, true
}
if m, ok := unwrappedErr.(multi.Errors); ok {
return New(ClientStatus, MultipleErrors.ToInt32(), m.Error(), nil), true
// Return all of the errors in the details
var errors []interface{}
for _, err := range m {
errors = append(errors, err)
}
return New(ClientStatus, MultipleErrors.ToInt32(), m.Error(), errors), true
}

return nil, false
Expand Down
7 changes: 5 additions & 2 deletions pkg/fabsdk/provider/chpvdr/chprovider.go
Expand Up @@ -9,9 +9,9 @@ package chpvdr
import (
reqContext "context"

"github.com/hyperledger/fabric-sdk-go/pkg/client/common/selection/staticselection"

"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/staticselection"
"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 @@ -144,6 +144,9 @@ 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) {
return dynamicdiscovery.NewChannelService(ctx, chConfig.ID())
}
return staticdiscovery.NewService(ctx.EndpointConfig(), ctx.InfraProvider(), chConfig.ID())
}

Expand Down
15 changes: 15 additions & 0 deletions pkg/fabsdk/provider/chpvdr/chprovider_test.go
Expand Up @@ -11,6 +11,7 @@ package chpvdr
import (
"testing"

"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/staticselection"

Expand Down Expand Up @@ -47,8 +48,12 @@ func TestBasicValidChannel(t *testing.T) {
err = cp.Initialize(ctx)
assert.NoError(t, err)

testChannelCfg := mocks.NewMockChannelCfg("testchannel")
testChannelCfg.MockCapabilities[fab.ApplicationGroupKey][fab.V1_2Capability] = true

mockChConfigCache := newMockChCfgCache(chconfig.NewChannelCfg(""))
mockChConfigCache.Put(chconfig.NewChannelCfg("mychannel"))
mockChConfigCache.Put(testChannelCfg)
cp.chCfgCache = mockChConfigCache

// System channel
Expand Down Expand Up @@ -90,6 +95,16 @@ func TestBasicValidChannel(t *testing.T) {
require.NotNil(t, selection)
_, ok = selection.(*staticselection.SelectionService)
assert.Truef(t, ok, "Expecting selection to be Static")

// testchannel has v1_2 capabilities
channelService, err = cp.ChannelService(clientCtx, "testchannel")
require.NoError(t, err)
require.NotNil(t, channelService)
discovery, err = channelService.Discovery()
require.NoError(t, err)
require.NotNil(t, discovery)
_, ok = discovery.(*dynamicdiscovery.ChannelService)
assert.Truef(t, ok, "Expecting discovery to be Dynamic for v1_2")
}

func TestResolveEventServiceType(t *testing.T) {
Expand Down
24 changes: 21 additions & 3 deletions test/integration/sdk/channel_client_test.go
Expand Up @@ -302,9 +302,27 @@ func testChaincodeError(ccID string, client *channel.Client, t *testing.T) {
require.Error(t, err)
s, ok := status.FromError(err)
require.True(t, ok, "expected status error")
require.EqualValues(t, status.ChaincodeStatus, s.Group, "expected ChaincodeStatus")
require.Equal(t, int32(500), s.Code)
require.Equal(t, "Unknown function call", s.Message)

checkError := func(s *status.Status) {
require.EqualValues(t, status.ChaincodeStatus, s.Group, "expected ChaincodeStatus")
require.Equal(t, int32(500), s.Code)
require.Equal(t, "Unknown function call", s.Message)
}

if s.Code == int32(status.MultipleErrors) {
t.Logf("Received multiple errors from endorsement:")
for i, d := range s.Details {
err, ok := d.(error)
require.Truef(t, ok, "expecting error from status detail")
s, ok = status.FromError(err)
require.True(t, ok, "expected status error")
t.Logf("(%d) - %#v", i, s)
checkError(s)
}
} else {
t.Logf("Received single error from endorsement: %#v", s)
checkError(s)
}
}

func TestNoEndpoints(t *testing.T) {
Expand Down

0 comments on commit 68c1ad0

Please sign in to comment.