From 1a4824e4535ae3a7d25bd9f0f043f052129c7e70 Mon Sep 17 00:00:00 2001 From: Bob Stasyszyn Date: Wed, 23 May 2018 12:21:26 -0400 Subject: [PATCH] [FAB-10325] Moved channel funcs out of InfraProvider Moved the following functions from InfraProvider to ChannelProvider/ChannelService: - CreateChannelConfig - CreateChannelCfg - CreateChannelTransactor - CreateChannelMembership - CreateEventService Added CreateChannelProvider to ServiceProviderFactory in order to allow users to override the ChannelProvider. Change-Id: I9e30c1a8d8154cb81472e1eed91af8962b4cc5eb Signed-off-by: Bob Stasyszyn --- pkg/client/channel/chclient.go | 6 +- pkg/client/channel/chclient_test.go | 5 +- pkg/client/event/event_test.go | 3 +- pkg/client/ledger/ledger_test.go | 3 +- pkg/client/resmgmt/resmgmt.go | 6 +- pkg/client/resmgmt/resmgmt_test.go | 44 +---- pkg/common/providers/fab/context.go | 7 +- pkg/common/providers/fab/provider.go | 5 - pkg/fab/mocks/mockchprovider.go | 24 ++- pkg/fab/mocks/mockfabricprovider.go | 21 +- pkg/fabsdk/api/factory.go | 1 + pkg/fabsdk/fabsdk.go | 10 +- pkg/fabsdk/fabsdk_chconfig_test.go | 12 +- pkg/fabsdk/fabsdk_test.go | 1 + pkg/fabsdk/factory/defsvc/svcfactory.go | 6 + .../provider/{fabpvdr => chpvdr}/cachekey.go | 2 +- pkg/fabsdk/provider/chpvdr/chprovider.go | 187 ++++++++++++++++-- pkg/fabsdk/provider/chpvdr/chprovider_test.go | 70 +++---- .../{fabpvdr => chpvdr}/eventserviceref.go | 2 +- .../fabprovider_testing_env.go | 6 +- pkg/fabsdk/provider/fabpvdr/chconfig_test.go | 41 ---- pkg/fabsdk/provider/fabpvdr/fabpvdr.go | 167 +--------------- pkg/fabsdk/provider/fabpvdr/fabpvdr_test.go | 36 ---- pkg/fabsdk/test/mocksdkapi/mocksdkapi.gen.go | 13 ++ 24 files changed, 275 insertions(+), 403 deletions(-) rename pkg/fabsdk/provider/{fabpvdr => chpvdr}/cachekey.go (99%) rename pkg/fabsdk/provider/{fabpvdr => chpvdr}/eventserviceref.go (99%) rename pkg/fabsdk/provider/{fabpvdr => chpvdr}/fabprovider_testing_env.go (91%) delete mode 100644 pkg/fabsdk/provider/fabpvdr/chconfig_test.go diff --git a/pkg/client/channel/chclient.go b/pkg/client/channel/chclient.go index fd654cd21e..38c8cd71a0 100644 --- a/pkg/client/channel/chclient.go +++ b/pkg/client/channel/chclient.go @@ -218,11 +218,7 @@ func (cc *Client) prepareHandlerContexts(reqCtx reqContext.Context, request Requ return nil, nil, errors.New("ChaincodeID and Fcn are required") } - chConfig, err := cc.context.ChannelService().ChannelConfig() - if err != nil { - return nil, nil, errors.WithMessage(err, "failed to retrieve channel config") - } - transactor, err := cc.context.InfraProvider().CreateChannelTransactor(reqCtx, chConfig) + transactor, err := cc.context.ChannelService().Transactor(reqCtx) if err != nil { return nil, nil, errors.WithMessage(err, "failed to create transactor") } diff --git a/pkg/client/channel/chclient_test.go b/pkg/client/channel/chclient_test.go index 3554200e2e..0392f0fbfa 100644 --- a/pkg/client/channel/chclient_test.go +++ b/pkg/client/channel/chclient_test.go @@ -547,11 +547,12 @@ func setupCustomTestContext(t *testing.T, selectionService fab.SelectionService, Orderers: orderers, } - ctx.InfraProvider().(*fcmocks.MockInfraProvider).SetCustomTransactor(&transactor) - testChannelSvc, err := setupTestChannelService(ctx, orderers) assert.Nil(t, err, "Got error %s", err) + mockChService := testChannelSvc.(*fcmocks.MockChannelService) + mockChService.SetTransactor(&transactor) + //Modify for custom mockcore to test scenarios selectionProvider := ctx.MockProviderContext.SelectionProvider() selectionProvider.(*fcmocks.MockSelectionProvider).SetCustomSelectionService(selectionService) diff --git a/pkg/client/event/event_test.go b/pkg/client/event/event_test.go index d8a8881760..7f4c609af5 100644 --- a/pkg/client/event/event_test.go +++ b/pkg/client/event/event_test.go @@ -346,9 +346,8 @@ func setupCustomTestContext(t *testing.T, orderers []fab.Orderer) context.Client Orderers: orderers, } - ctx.InfraProvider().(*fcmocks.MockInfraProvider).SetCustomTransactor(&transactor) - testChannelSvc, err := setupTestChannelService(ctx, orderers) + testChannelSvc.(*fcmocks.MockChannelService).SetTransactor(&transactor) assert.Nil(t, err, "Got error %s", err) channelProvider := ctx.MockProviderContext.ChannelProvider() diff --git a/pkg/client/ledger/ledger_test.go b/pkg/client/ledger/ledger_test.go index 4253f80699..9ec2214ab1 100644 --- a/pkg/client/ledger/ledger_test.go +++ b/pkg/client/ledger/ledger_test.go @@ -305,10 +305,9 @@ func setupCustomTestContext(t *testing.T, discoveryService fab.DiscoveryService, Orderers: orderers, } - ctx.InfraProvider().(*fcmocks.MockInfraProvider).SetCustomTransactor(&transactor) - testChannelSvc, err := setupTestChannelService(ctx, orderers) assert.Nil(t, err, "Got error %s", err) + testChannelSvc.(*fcmocks.MockChannelService).SetTransactor(&transactor) channelProvider := ctx.MockProviderContext.ChannelProvider() channelProvider.(*fcmocks.MockChannelProvider).SetCustomChannelService(testChannelSvc) diff --git a/pkg/client/resmgmt/resmgmt.go b/pkg/client/resmgmt/resmgmt.go index 27bc0e5790..8cee0e7082 100644 --- a/pkg/client/resmgmt/resmgmt.go +++ b/pkg/client/resmgmt/resmgmt.go @@ -719,11 +719,7 @@ func (rc *Client) sendCCProposal(reqCtx reqContext.Context, ccProposalType chain return fab.EmptyTransactionID, errors.WithMessage(err, "Unable to get channel service") } - chConfig, err := channelService.ChannelConfig() - if err != nil { - return fab.EmptyTransactionID, errors.WithMessage(err, "get channel config failed") - } - transactor, err := rc.ctx.InfraProvider().CreateChannelTransactor(reqCtx, chConfig) + transactor, err := channelService.Transactor(reqCtx) if err != nil { return fab.EmptyTransactionID, errors.WithMessage(err, "get channel transactor failed") } diff --git a/pkg/client/resmgmt/resmgmt_test.go b/pkg/client/resmgmt/resmgmt_test.go index 17663b2a6e..6cbfc4f86c 100644 --- a/pkg/client/resmgmt/resmgmt_test.go +++ b/pkg/client/resmgmt/resmgmt_test.go @@ -22,7 +22,6 @@ import ( "github.com/stretchr/testify/assert" "google.golang.org/grpc" - txnmocks "github.com/hyperledger/fabric-sdk-go/pkg/client/common/mocks" "github.com/hyperledger/fabric-sdk-go/pkg/common/errors/status" "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context" "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core" @@ -836,28 +835,6 @@ func TestInstantiateCCDiscoveryError(t *testing.T) { } } -func TestInstantiateCCTransactorError(t *testing.T) { - - // Setup test client - ctx := setupTestContext("test", "Org1MSP") - rc := setupResMgmtClient(t, ctx) - - transactor := txnmocks.MockTransactor{ - Ctx: ctx, - ChannelID: "mychannel", - } - rc.ctx.InfraProvider().(*fcmocks.MockInfraProvider).SetCustomTransactor(&transactor) - - ccPolicy := cauthdsl.SignedByMspMember("Org1MSP") - req := InstantiateCCRequest{Name: "name", Version: "version", Path: "path", Policy: ccPolicy} - - // Test InstantiateCC for transactor error - _, err := rc.InstantiateCC("mychannel", req) - if err == nil || !strings.Contains(err.Error(), "Failed to verify signature") { - t.Fatalf("Should have failed to instantiate cc with opts with get discovery service error: %s", err) - } -} - func TestUpgradeCCRequiredParameters(t *testing.T) { rc := setupDefaultResMgmtClient(t) @@ -1003,22 +980,6 @@ func TestCCProposal(t *testing.T) { t.Fatalf("Should have failed for invalid chaincode deployment type: %s", err) } - // Create mock orderer - orderer := fcmocks.NewMockOrderer("", nil) - - transactor := txnmocks.MockTransactor{ - Ctx: ctx, - ChannelID: "mychannel", - Orderers: []fab.Orderer{orderer}, - } - rc.ctx.InfraProvider().(*fcmocks.MockInfraProvider).SetCustomTransactor(&transactor) - - // Test error in transactor - _, err = rc.InstantiateCC("mychannel", instantiateReq) - if err == nil || !strings.Contains(err.Error(), "Failed to verify signature") { - t.Fatalf("Should have failed due to error in commit: %s", err) - } - // Test no event source in config backends, err := configImpl.FromFile(configPath)() if err != nil { @@ -1032,10 +993,7 @@ func TestCCProposal(t *testing.T) { ctx.SetEndpointConfig(cfg) rc = setupResMgmtClient(t, ctx, getDefaultTargetFilterOption()) _, err = rc.InstantiateCC("mychannel", instantiateReq) - // TODO: Add verification - if err == nil { - t.Fatalf("Should have failed since no event source has been configured") - } + assert.NoError(t, err) } func getDefaultTargetFilterOption() ClientOption { diff --git a/pkg/common/providers/fab/context.go b/pkg/common/providers/fab/context.go index ba24a9e364..17ac682f98 100644 --- a/pkg/common/providers/fab/context.go +++ b/pkg/common/providers/fab/context.go @@ -6,7 +6,11 @@ SPDX-License-Identifier: Apache-2.0 package fab -import "github.com/hyperledger/fabric-sdk-go/pkg/common/options" +import ( + reqContext "context" + + "github.com/hyperledger/fabric-sdk-go/pkg/common/options" +) // ChannelService supplies services related to a channel. type ChannelService interface { @@ -14,6 +18,7 @@ type ChannelService interface { EventService(opts ...options.Opt) (EventService, error) Membership() (ChannelMembership, error) ChannelConfig() (ChannelCfg, error) + Transactor(reqCtx reqContext.Context) (Transactor, error) } // Transactor supplies methods for sending transaction proposals and transactions. diff --git a/pkg/common/providers/fab/provider.go b/pkg/common/providers/fab/provider.go index 3ceffee061..fa39c3ed0c 100644 --- a/pkg/common/providers/fab/provider.go +++ b/pkg/common/providers/fab/provider.go @@ -30,11 +30,6 @@ type ClientContext interface { // InfraProvider enables access to fabric objects such as peer and user based on config or type InfraProvider interface { - CreateChannelConfig(name string) (ChannelConfig, error) - CreateChannelCfg(ctx ClientContext, channelID string) (ChannelCfg, error) - CreateChannelTransactor(reqCtx reqContext.Context, cfg ChannelCfg) (Transactor, error) - CreateChannelMembership(ctx ClientContext, channelID string) (ChannelMembership, error) - CreateEventService(ctx ClientContext, channelID string, opts ...options.Opt) (EventService, error) CreatePeerFromConfig(peerCfg *NetworkPeer) (Peer, error) CreateOrdererFromConfig(cfg *OrdererConfig) (Orderer, error) CommManager() CommManager diff --git a/pkg/fab/mocks/mockchprovider.go b/pkg/fab/mocks/mockchprovider.go index 2bbe1d41f8..080a50b40e 100644 --- a/pkg/fab/mocks/mockchprovider.go +++ b/pkg/fab/mocks/mockchprovider.go @@ -7,6 +7,8 @@ SPDX-License-Identifier: Apache-2.0 package mocks import ( + reqContext "context" + "github.com/hyperledger/fabric-sdk-go/pkg/common/options" "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core" "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab" @@ -14,9 +16,9 @@ import ( // MockChannelProvider holds a mock channel provider. type MockChannelProvider struct { - ctx core.Providers - transactor fab.Transactor - customSelectionService fab.ChannelService + ctx core.Providers + transactor fab.Transactor + customChannelService fab.ChannelService } // MockChannelService holds a mock channel service. @@ -44,8 +46,8 @@ func (cp *MockChannelProvider) SetTransactor(transactor fab.Transactor) { // ChannelService returns a mock ChannelService func (cp *MockChannelProvider) ChannelService(ctx fab.ClientContext, channelID string) (fab.ChannelService, error) { - if cp.customSelectionService != nil { - return cp.customSelectionService, nil + if cp.customChannelService != nil { + return cp.customChannelService, nil } cs := MockChannelService{ @@ -57,8 +59,8 @@ func (cp *MockChannelProvider) ChannelService(ctx fab.ClientContext, channelID s } // SetCustomChannelService sets custom channel service for unit-test purposes -func (cp *MockChannelProvider) SetCustomChannelService(customSelectionService fab.ChannelService) { - cp.customSelectionService = customSelectionService +func (cp *MockChannelProvider) SetCustomChannelService(customChannelService fab.ChannelService) { + cp.customChannelService = customChannelService } // SetOrderers sets orderes to mock channel service for unit-test purposes @@ -76,6 +78,14 @@ func (cs *MockChannelService) SetTransactor(t fab.Transactor) { cs.transactor = t } +// Transactor returns a mock transactor +func (cs *MockChannelService) Transactor(reqCtx reqContext.Context) (fab.Transactor, error) { + if cs.transactor != nil { + return cs.transactor, nil + } + return &MockTransactor{ChannelID: cs.channelID, Ctx: reqCtx}, nil +} + // Config ... func (cs *MockChannelService) Config() (fab.ChannelConfig, error) { return nil, nil diff --git a/pkg/fab/mocks/mockfabricprovider.go b/pkg/fab/mocks/mockfabricprovider.go index 71b0519c6d..d0417064d3 100644 --- a/pkg/fab/mocks/mockfabricprovider.go +++ b/pkg/fab/mocks/mockfabricprovider.go @@ -9,16 +9,13 @@ package mocks import ( "fmt" - reqContext "context" - "github.com/hyperledger/fabric-sdk-go/pkg/common/options" "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab" ) // MockInfraProvider represents the default implementation of Fabric objects. type MockInfraProvider struct { - customOrderer fab.Orderer - customTransactor fab.Transactor + customOrderer fab.Orderer } // CreateEventService creates the event service. @@ -41,17 +38,6 @@ func (f *MockInfraProvider) CreateChannelConfig(channelID string) (fab.ChannelCo return nil, nil } -// CreateChannelTransactor initializes the transactor -func (f *MockInfraProvider) CreateChannelTransactor(reqCtx reqContext.Context, cfg fab.ChannelCfg) (fab.Transactor, error) { - if f.customTransactor != nil { - return f.customTransactor, nil - } - if cfg == nil { - return &MockTransactor{}, nil - } - return &MockTransactor{ChannelID: cfg.ID(), Ctx: reqCtx}, nil -} - // CreatePeerFromConfig returns a new default implementation of Peer based configuration func (f *MockInfraProvider) CreatePeerFromConfig(peerCfg *fab.NetworkPeer) (fab.Peer, error) { if peerCfg != nil { @@ -86,11 +72,6 @@ func (f *MockInfraProvider) SetCustomOrderer(customOrderer fab.Orderer) { f.customOrderer = customOrderer } -// SetCustomTransactor sets custom transactor for unit-test purposes -func (f *MockInfraProvider) SetCustomTransactor(customTransactor fab.Transactor) { - f.customTransactor = customTransactor -} - //Close mock close function func (f *MockInfraProvider) Close() { } diff --git a/pkg/fabsdk/api/factory.go b/pkg/fabsdk/api/factory.go index 61ae859b26..aa7bf486fc 100644 --- a/pkg/fabsdk/api/factory.go +++ b/pkg/fabsdk/api/factory.go @@ -37,4 +37,5 @@ type ServiceProviderFactory interface { CreateDiscoveryProvider(config fab.EndpointConfig) (fab.DiscoveryProvider, error) CreateLocalDiscoveryProvider(config fab.EndpointConfig) (fab.LocalDiscoveryProvider, error) CreateSelectionProvider(config fab.EndpointConfig) (fab.SelectionProvider, error) + CreateChannelProvider(config fab.EndpointConfig) (fab.ChannelProvider, error) } diff --git a/pkg/fabsdk/fabsdk.go b/pkg/fabsdk/fabsdk.go index 58edc4e88a..d186e4b0d7 100644 --- a/pkg/fabsdk/fabsdk.go +++ b/pkg/fabsdk/fabsdk.go @@ -23,7 +23,6 @@ import ( "github.com/hyperledger/fabric-sdk-go/pkg/core/cryptosuite" fabImpl "github.com/hyperledger/fabric-sdk-go/pkg/fab" sdkApi "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/api" - "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/provider/chpvdr" mspImpl "github.com/hyperledger/fabric-sdk-go/pkg/msp" "github.com/pkg/errors" ) @@ -268,7 +267,7 @@ func initSDK(sdk *FabricSDK, configProvider core.ConfigProvider, opts []Option) return errors.WithMessage(err, "failed to create selection provider") } - channelProvider, err := chpvdr.New(infraProvider) + channelProvider, err := sdk.opts.Service.CreateChannelProvider(cfg.endpointConfig) if err != nil { return errors.WithMessage(err, "failed to create channel provider") } @@ -316,6 +315,13 @@ func initSDK(sdk *FabricSDK, configProvider core.ConfigProvider, opts []Option) } } + if pi, ok := channelProvider.(providerInit); ok { + err = pi.Initialize(sdk.provider) + if err != nil { + return errors.WithMessage(err, "failed to initialize channel provider") + } + } + return nil } diff --git a/pkg/fabsdk/fabsdk_chconfig_test.go b/pkg/fabsdk/fabsdk_chconfig_test.go index f04054fa92..d86606c9b3 100644 --- a/pkg/fabsdk/fabsdk_chconfig_test.go +++ b/pkg/fabsdk/fabsdk_chconfig_test.go @@ -18,7 +18,7 @@ import ( "github.com/hyperledger/fabric-sdk-go/pkg/core/config/lookup" mockCore "github.com/hyperledger/fabric-sdk-go/pkg/core/mocks" "github.com/hyperledger/fabric-sdk-go/pkg/fab/mocks" - "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/provider/fabpvdr" + "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/provider/chpvdr" "github.com/hyperledger/fabric-sdk-go/pkg/msp" ) @@ -39,8 +39,8 @@ func TestNewDefaultSDK(t *testing.T) { func verifySDK(t *testing.T, sdk *FabricSDK) { // Mock channel provider cache - sdk.provider.InfraProvider().(*fabpvdr.InfraProvider).SetChannelConfig(mocks.NewMockChannelCfg("mychannel")) - sdk.provider.InfraProvider().(*fabpvdr.InfraProvider).SetChannelConfig(mocks.NewMockChannelCfg("orgchannel")) + sdk.provider.ChannelProvider().(*chpvdr.ChannelProvider).SetChannelConfig(mocks.NewMockChannelCfg("mychannel")) + sdk.provider.ChannelProvider().(*chpvdr.ChannelProvider).SetChannelConfig(mocks.NewMockChannelCfg("orgchannel")) // Get a common client context for the following tests chCtx := sdk.ChannelContext("orgchannel", WithUser(sdkValidClientUser), WithOrg(sdkValidClientOrg2)) @@ -72,8 +72,8 @@ func TestNewDefaultTwoValidSDK(t *testing.T) { // Mock channel provider cache - sdk1.provider.InfraProvider().(*fabpvdr.InfraProvider).SetChannelConfig(mocks.NewMockChannelCfg("mychannel")) - sdk1.provider.InfraProvider().(*fabpvdr.InfraProvider).SetChannelConfig(mocks.NewMockChannelCfg("orgchannel")) + sdk1.provider.ChannelProvider().(*chpvdr.ChannelProvider).SetChannelConfig(mocks.NewMockChannelCfg("mychannel")) + sdk1.provider.ChannelProvider().(*chpvdr.ChannelProvider).SetChannelConfig(mocks.NewMockChannelCfg("orgchannel")) //prepare config backend for sdk2 @@ -91,7 +91,7 @@ func TestNewDefaultTwoValidSDK(t *testing.T) { } // Mock channel provider cache - sdk2.provider.InfraProvider().(*fabpvdr.InfraProvider).SetChannelConfig(mocks.NewMockChannelCfg("orgchannel")) + sdk2.provider.ChannelProvider().(*chpvdr.ChannelProvider).SetChannelConfig(mocks.NewMockChannelCfg("orgchannel")) // Default sdk with two channels configBackend, err := sdk1.Config() diff --git a/pkg/fabsdk/fabsdk_test.go b/pkg/fabsdk/fabsdk_test.go index 4a79cc1018..08dcbdbcd6 100644 --- a/pkg/fabsdk/fabsdk_test.go +++ b/pkg/fabsdk/fabsdk_test.go @@ -129,6 +129,7 @@ func TestWithServicePkg(t *testing.T) { factory.EXPECT().CreateDiscoveryProvider(gomock.Any()).Return(nil, nil) factory.EXPECT().CreateLocalDiscoveryProvider(gomock.Any()).Return(nil, nil) factory.EXPECT().CreateSelectionProvider(gomock.Any()).Return(nil, nil) + factory.EXPECT().CreateChannelProvider(gomock.Any()).Return(nil, nil) _, err = New(c, WithServicePkg(factory)) if err != nil { diff --git a/pkg/fabsdk/factory/defsvc/svcfactory.go b/pkg/fabsdk/factory/defsvc/svcfactory.go index c16f6ffab1..050adc02aa 100644 --- a/pkg/fabsdk/factory/defsvc/svcfactory.go +++ b/pkg/fabsdk/factory/defsvc/svcfactory.go @@ -11,6 +11,7 @@ import ( discovery "github.com/hyperledger/fabric-sdk-go/pkg/client/common/discovery/staticdiscovery" selection "github.com/hyperledger/fabric-sdk-go/pkg/client/common/selection/staticselection" + "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/provider/chpvdr" ) // ProviderFactory represents the default SDK provider factory for services. @@ -32,6 +33,11 @@ func (f *ProviderFactory) CreateLocalDiscoveryProvider(config fab.EndpointConfig return discovery.New(config) } +// CreateChannelProvider returns a new default implementation of channel provider +func (f *ProviderFactory) CreateChannelProvider(config fab.EndpointConfig) (fab.ChannelProvider, error) { + return chpvdr.New(config) +} + // CreateSelectionProvider returns a new default implementation of selection service func (f *ProviderFactory) CreateSelectionProvider(config fab.EndpointConfig) (fab.SelectionProvider, error) { return selection.New(config) diff --git a/pkg/fabsdk/provider/fabpvdr/cachekey.go b/pkg/fabsdk/provider/chpvdr/cachekey.go similarity index 99% rename from pkg/fabsdk/provider/fabpvdr/cachekey.go rename to pkg/fabsdk/provider/chpvdr/cachekey.go index c9419a282b..0f86b92df1 100644 --- a/pkg/fabsdk/provider/fabpvdr/cachekey.go +++ b/pkg/fabsdk/provider/chpvdr/cachekey.go @@ -4,7 +4,7 @@ Copyright SecureKey Technologies Inc. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ -package fabpvdr +package chpvdr import ( "crypto/sha256" diff --git a/pkg/fabsdk/provider/chpvdr/chprovider.go b/pkg/fabsdk/provider/chpvdr/chprovider.go index b0ebad6fef..e93189e1a0 100644 --- a/pkg/fabsdk/provider/chpvdr/chprovider.go +++ b/pkg/fabsdk/provider/chpvdr/chprovider.go @@ -7,32 +7,90 @@ SPDX-License-Identifier: Apache-2.0 package chpvdr import ( + reqContext "context" + + "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" "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab" + channelImpl "github.com/hyperledger/fabric-sdk-go/pkg/fab/channel" + "github.com/hyperledger/fabric-sdk-go/pkg/fab/channel/membership" + "github.com/hyperledger/fabric-sdk-go/pkg/fab/chconfig" + "github.com/hyperledger/fabric-sdk-go/pkg/fab/events/deliverclient" + "github.com/hyperledger/fabric-sdk-go/pkg/fab/events/eventhubclient" + "github.com/hyperledger/fabric-sdk-go/pkg/util/concurrent/lazycache" + "github.com/pkg/errors" ) +var logger = logging.NewLogger("fabsdk") + +type cache interface { + Get(lazycache.Key) (interface{}, error) + Close() +} + // ChannelProvider keeps context across ChannelService instances. // // TODO: add listener for channel config changes. Upon channel config change, // underlying channel services need to recreate their channel clients. type ChannelProvider struct { - infraProvider fab.InfraProvider + providerContext context.Providers + eventServiceCache cache + chCfgCache cache + membershipCache cache } // New creates a ChannelProvider based on a context -func New(infraProvider fab.InfraProvider) (*ChannelProvider, error) { - cp := ChannelProvider{infraProvider: infraProvider} +func New(config fab.EndpointConfig) (*ChannelProvider, error) { + eventIdleTime := config.Timeout(fab.EventServiceIdle) + chConfigRefresh := config.Timeout(fab.ChannelConfigRefresh) + membershipRefresh := config.Timeout(fab.ChannelMembershipRefresh) + + eventServiceCache := lazycache.New( + "Event_Service_Cache", + func(key lazycache.Key) (interface{}, error) { + ck := key.(cacheKey) + return NewEventClientRef( + eventIdleTime, + func() (fab.EventClient, error) { + return getEventClient(ck.Context(), ck.ChannelConfig(), ck.Opts()...) + }, + ), nil + }, + ) + + cp := ChannelProvider{ + eventServiceCache: eventServiceCache, + chCfgCache: chconfig.NewRefCache(chConfigRefresh), + membershipCache: membership.NewRefCache(membershipRefresh), + } return &cp, nil } +// Initialize sets the provider context +func (cp *ChannelProvider) Initialize(providers context.Providers) error { + cp.providerContext = providers + return nil +} + +// Close frees resources and caches. +func (cp *ChannelProvider) Close() { + logger.Debug("Closing event service cache...") + cp.eventServiceCache.Close() + + logger.Debug("Closing membership cache...") + cp.membershipCache.Close() + + logger.Debug("Closing channel configuration cache...") + cp.chCfgCache.Close() +} + // ChannelService creates a ChannelService for an identity func (cp *ChannelProvider) ChannelService(ctx fab.ClientContext, channelID string) (fab.ChannelService, error) { cs := ChannelService{ - provider: cp, - infraProvider: cp.infraProvider, - context: ctx, - channelID: channelID, + provider: cp, + context: ctx, + channelID: channelID, } return &cs, nil @@ -41,28 +99,125 @@ func (cp *ChannelProvider) ChannelService(ctx fab.ClientContext, channelID strin // ChannelService provides Channel clients and maintains contexts for them. // the identity context is used type ChannelService struct { - provider *ChannelProvider - infraProvider fab.InfraProvider - context context.Client - channelID string + provider *ChannelProvider + context context.Client + channelID string } // Config returns the Config for the named channel func (cs *ChannelService) Config() (fab.ChannelConfig, error) { - return cs.infraProvider.CreateChannelConfig(cs.channelID) + return chconfig.New(cs.channelID) } // EventService returns the EventService. func (cs *ChannelService) EventService(opts ...options.Opt) (fab.EventService, error) { - return cs.infraProvider.CreateEventService(cs.context, cs.channelID, opts...) + chnlCfg, err := cs.ChannelConfig() + if err != nil { + return nil, err + } + key, err := NewCacheKey(cs.context, chnlCfg, opts...) + if err != nil { + return nil, err + } + eventService, err := cs.provider.eventServiceCache.Get(key) + if err != nil { + return nil, err + } + + return eventService.(fab.EventService), nil } -// Membership returns the member identifier for this channel +// Membership returns and caches a channel member identifier +// A membership reference is returned that refreshes with the configured interval func (cs *ChannelService) Membership() (fab.ChannelMembership, error) { - return cs.infraProvider.CreateChannelMembership(cs.context, cs.channelID) + chCfgRef, err := cs.loadChannelCfgRef() + if err != nil { + return nil, err + } + key, err := membership.NewCacheKey(membership.Context{Providers: cs.provider.providerContext, EndpointConfig: cs.context.EndpointConfig()}, + chCfgRef.Reference, cs.channelID) + if err != nil { + return nil, err + } + ref, err := cs.provider.membershipCache.Get(key) + if err != nil { + return nil, err + } + + return ref.(*membership.Ref), nil } // ChannelConfig returns the channel config for this channel func (cs *ChannelService) ChannelConfig() (fab.ChannelCfg, error) { - return cs.infraProvider.CreateChannelCfg(cs.context, cs.channelID) + if cs.channelID == "" { + // System channel + return chconfig.NewChannelCfg(""), nil + } + chCfgRef, err := cs.loadChannelCfgRef() + if err != nil { + return nil, err + } + chCfg, err := chCfgRef.Get() + if err != nil { + return nil, errors.WithMessage(err, "could not get chConfig cache reference") + } + return chCfg.(fab.ChannelCfg), nil +} + +// Transactor returns the transactor +func (cs *ChannelService) Transactor(reqCtx reqContext.Context) (fab.Transactor, error) { + cfg, err := cs.ChannelConfig() + if err != nil { + return nil, err + } + return channelImpl.NewTransactor(reqCtx, cfg) +} + +func (cs *ChannelService) loadChannelCfgRef() (*chconfig.Ref, error) { + key, err := chconfig.NewCacheKey(cs.context, func(string) (fab.ChannelConfig, error) { return cs.Config() }, cs.channelID) + if err != nil { + return nil, err + } + c, err := cs.provider.chCfgCache.Get(key) + if err != nil { + return nil, err + } + + return c.(*chconfig.Ref), nil +} + +func getEventClient(ctx context.Client, chConfig fab.ChannelCfg, opts ...options.Opt) (fab.EventClient, error) { + useDeliver, err := useDeliverEvents(ctx, chConfig) + if err != nil { + return nil, err + } + + if useDeliver { + logger.Debugf("Using deliver events for channel [%s]", chConfig.ID()) + return deliverclient.New(ctx, chConfig, opts...) + } + + logger.Debugf("Using event hub events for channel [%s]", chConfig.ID()) + return eventhubclient.New(ctx, chConfig, opts...) +} + +func useDeliverEvents(ctx context.Client, chConfig fab.ChannelCfg) (bool, error) { + switch ctx.EndpointConfig().EventServiceType() { + case fab.DeliverEventServiceType: + return true, nil + case fab.EventHubEventServiceType: + return false, nil + case fab.AutoDetectEventServiceType: + logger.Debugf("Determining event service type from channel capabilities...") + return chConfig.HasCapability(fab.ApplicationGroupKey, fab.V1_1Capability), nil + default: + return false, errors.Errorf("unsupported event service type: %d", ctx.EndpointConfig().EventServiceType()) + } +} + +type cacheKey interface { + lazycache.Key + Context() fab.ClientContext + ChannelConfig() fab.ChannelCfg + Opts() []options.Opt } diff --git a/pkg/fabsdk/provider/chpvdr/chprovider_test.go b/pkg/fabsdk/provider/chpvdr/chprovider_test.go index e9d13a3009..d71d45399f 100644 --- a/pkg/fabsdk/provider/chpvdr/chprovider_test.go +++ b/pkg/fabsdk/provider/chpvdr/chprovider_test.go @@ -1,3 +1,5 @@ +// +build testing + /* Copyright SecureKey Technologies Inc. All Rights Reserved. @@ -12,9 +14,8 @@ import ( "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context" "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab" "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp" + "github.com/hyperledger/fabric-sdk-go/pkg/fab/chconfig" "github.com/hyperledger/fabric-sdk-go/pkg/fab/mocks" - "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/factory/defcore" - "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/provider/fabpvdr" mspmocks "github.com/hyperledger/fabric-sdk-go/pkg/msp/test/mockmsp" "github.com/stretchr/testify/assert" ) @@ -34,25 +35,25 @@ func TestBasicValidChannel(t *testing.T) { SigningIdentity: user, } - pf := &MockProviderFactory{ctx: clientCtx} - - fp, err := pf.CreateInfraProvider(ctx.EndpointConfig()) - if err != nil { - t.Fatalf("Unexpected error creating Fabric Provider: %v", err) - } - - cp, err := New(fp) + cp, err := New(clientCtx.EndpointConfig()) if err != nil { t.Fatalf("Unexpected error creating Channel Provider: %v", err) } - _, err = cp.ChannelService(clientCtx, "mychannel") + err = cp.Initialize(ctx) + assert.NoError(t, err) + + mockChConfigCache := newMockChCfgCache(chconfig.NewChannelCfg("")) + mockChConfigCache.Put(chconfig.NewChannelCfg("mychannel")) + cp.chCfgCache = mockChConfigCache + + channelService, err := cp.ChannelService(clientCtx, "mychannel") if err != nil { t.Fatalf("Unexpected error creating Channel Service: %v", err) } // System channel - channelService, err := cp.ChannelService(clientCtx, "") + channelService, err = cp.ChannelService(clientCtx, "") if err != nil { t.Fatalf("Unexpected error creating Channel Service: %v", err) } @@ -60,40 +61,27 @@ func TestBasicValidChannel(t *testing.T) { m, err := channelService.Membership() assert.Nil(t, err) assert.NotNil(t, m) -} -// MockProviderFactory is configured to retrieve channel config from orderer -type MockProviderFactory struct { - defcore.ProviderFactory - ctx context.Client -} - -// MockInfraProvider overrides channel config default implementation -type MockInfraProvider struct { - *fabpvdr.InfraProvider - ctx context.Client -} + chConfig, err := channelService.Config() + assert.Nil(t, err) + assert.NotNil(t, chConfig) -// CreateChannelConfig initializes the channel config -func (f *MockInfraProvider) CreateChannelConfig(channelID string) (fab.ChannelConfig, error) { - return mocks.NewMockChannelConfig(f.ctx, "mychannel") + channelConfig, err := channelService.ChannelConfig() + assert.Nil(t, err) + assert.NotNil(t, channelConfig) } -func (f *MockInfraProvider) CreateChannelCfg(ctx fab.ClientContext, channel string) (fab.ChannelCfg, error) { - return mocks.NewMockChannelCfg(channel), nil -} +func TestResolveEventServiceType(t *testing.T) { + ctx := mocks.NewMockContext(mspmocks.NewMockSigningIdentity("test", "Org1MSP")) + chConfig := mocks.NewMockChannelCfg("mychannel") -func (f *MockInfraProvider) CreateChannelMembership(ctx fab.ClientContext, channel string) (fab.ChannelMembership, error) { - return mocks.NewMockMembership(), nil -} + useDeliver, err := useDeliverEvents(ctx, chConfig) + assert.NoError(t, err) + assert.Falsef(t, useDeliver, "expecting deliver events not to be used") -// CreateInfraProvider mockcore new default implementation of fabric primitives -func (f *MockProviderFactory) CreateInfraProvider(config fab.EndpointConfig) (fab.InfraProvider, error) { - fabProvider := fabpvdr.New(config) + chConfig.MockCapabilities[fab.ApplicationGroupKey][fab.V1_1Capability] = true - cfp := MockInfraProvider{ - InfraProvider: fabProvider, - ctx: f.ctx, - } - return &cfp, nil + useDeliver, err = useDeliverEvents(ctx, chConfig) + assert.NoError(t, err) + assert.Truef(t, useDeliver, "expecting deliver events to be used") } diff --git a/pkg/fabsdk/provider/fabpvdr/eventserviceref.go b/pkg/fabsdk/provider/chpvdr/eventserviceref.go similarity index 99% rename from pkg/fabsdk/provider/fabpvdr/eventserviceref.go rename to pkg/fabsdk/provider/chpvdr/eventserviceref.go index f8adae17c3..3158709138 100644 --- a/pkg/fabsdk/provider/fabpvdr/eventserviceref.go +++ b/pkg/fabsdk/provider/chpvdr/eventserviceref.go @@ -4,7 +4,7 @@ Copyright SecureKey Technologies Inc. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ -package fabpvdr +package chpvdr import ( "sync/atomic" diff --git a/pkg/fabsdk/provider/fabpvdr/fabprovider_testing_env.go b/pkg/fabsdk/provider/chpvdr/fabprovider_testing_env.go similarity index 91% rename from pkg/fabsdk/provider/fabpvdr/fabprovider_testing_env.go rename to pkg/fabsdk/provider/chpvdr/fabprovider_testing_env.go index cf17de21a4..8aa3e2da38 100644 --- a/pkg/fabsdk/provider/fabpvdr/fabprovider_testing_env.go +++ b/pkg/fabsdk/provider/chpvdr/fabprovider_testing_env.go @@ -6,7 +6,7 @@ Copyright SecureKey Technologies Inc. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ -package fabpvdr +package chpvdr import ( "sync" @@ -20,7 +20,7 @@ import ( // SetChannelConfig allows setting channel configuration. // This method is intended to enable tests and should not be called. -func (f *InfraProvider) SetChannelConfig(cfg fab.ChannelCfg) { +func (f *ChannelProvider) SetChannelConfig(cfg fab.ChannelCfg) { if _, ok := f.chCfgCache.(*chCfgCache); !ok { f.chCfgCache = newMockChCfgCache(cfg) } else { @@ -32,7 +32,7 @@ type chCfgCache struct { cfgMap sync.Map } -func newMockChCfgCache(cfg fab.ChannelCfg) cache { +func newMockChCfgCache(cfg fab.ChannelCfg) *chCfgCache { c := &chCfgCache{} c.cfgMap.Store(cfg.ID(), newChCfgRef(cfg)) return c diff --git a/pkg/fabsdk/provider/fabpvdr/chconfig_test.go b/pkg/fabsdk/provider/fabpvdr/chconfig_test.go deleted file mode 100644 index d615452193..0000000000 --- a/pkg/fabsdk/provider/fabpvdr/chconfig_test.go +++ /dev/null @@ -1,41 +0,0 @@ -// +build testing - -/* -Copyright SecureKey Technologies Inc. All Rights Reserved. - -SPDX-License-Identifier: Apache-2.0 -*/ - -package fabpvdr - -import ( - "testing" - - "github.com/hyperledger/fabric-sdk-go/pkg/fab/chconfig" - "github.com/hyperledger/fabric-sdk-go/pkg/fab/mocks" - "github.com/stretchr/testify/assert" - - mspmocks "github.com/hyperledger/fabric-sdk-go/pkg/msp/test/mockmsp" -) - -func TestCreateChannelCfg(t *testing.T) { - p := newInfraProvider(t) - testChannelID := "test" - p.chCfgCache = newMockChCfgCache(chconfig.NewChannelCfg(testChannelID)) - ctx := mocks.NewMockProviderContext() - user := mspmocks.NewMockSigningIdentity("user", "user") - clientCtx := &mockClientContext{ - Providers: ctx, - SigningIdentity: user, - } - - m, err := p.CreateChannelCfg(clientCtx, "") - assert.Nil(t, err) - assert.NotNil(t, m) - - m, err = p.CreateChannelCfg(clientCtx, testChannelID) - assert.Nil(t, err) - assert.NotNil(t, m) - - p.Close() -} diff --git a/pkg/fabsdk/provider/fabpvdr/fabpvdr.go b/pkg/fabsdk/provider/fabpvdr/fabpvdr.go index f101558d9f..94039c656f 100644 --- a/pkg/fabsdk/provider/fabpvdr/fabpvdr.go +++ b/pkg/fabsdk/provider/fabpvdr/fabpvdr.go @@ -7,73 +7,30 @@ SPDX-License-Identifier: Apache-2.0 package fabpvdr import ( - reqContext "context" - "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" "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab" - channelImpl "github.com/hyperledger/fabric-sdk-go/pkg/fab/channel" - "github.com/hyperledger/fabric-sdk-go/pkg/fab/channel/membership" - "github.com/hyperledger/fabric-sdk-go/pkg/fab/chconfig" "github.com/hyperledger/fabric-sdk-go/pkg/fab/comm" - "github.com/hyperledger/fabric-sdk-go/pkg/fab/events/deliverclient" - "github.com/hyperledger/fabric-sdk-go/pkg/fab/events/eventhubclient" "github.com/hyperledger/fabric-sdk-go/pkg/fab/orderer" peerImpl "github.com/hyperledger/fabric-sdk-go/pkg/fab/peer" - "github.com/hyperledger/fabric-sdk-go/pkg/util/concurrent/lazycache" "github.com/pkg/errors" ) var logger = logging.NewLogger("fabsdk") -type cacheKey interface { - lazycache.Key - Context() fab.ClientContext - ChannelConfig() fab.ChannelCfg - Opts() []options.Opt -} - -type cache interface { - Get(lazycache.Key) (interface{}, error) - Close() -} - // InfraProvider represents the default implementation of Fabric objects. type InfraProvider struct { - providerContext context.Providers - commManager *comm.CachingConnector - eventServiceCache cache - chCfgCache cache - membershipCache cache + providerContext context.Providers + commManager *comm.CachingConnector } // New creates a InfraProvider enabling access to core Fabric objects and functionality. func New(config fab.EndpointConfig) *InfraProvider { idleTime := config.Timeout(fab.ConnectionIdle) sweepTime := config.Timeout(fab.CacheSweepInterval) - eventIdleTime := config.Timeout(fab.EventServiceIdle) - chConfigRefresh := config.Timeout(fab.ChannelConfigRefresh) - membershipRefresh := config.Timeout(fab.ChannelMembershipRefresh) - - eventServiceCache := lazycache.New( - "Event_Service_Cache", - func(key lazycache.Key) (interface{}, error) { - ck := key.(cacheKey) - return NewEventClientRef( - eventIdleTime, - func() (fab.EventClient, error) { - return getEventClient(ck.Context(), ck.ChannelConfig(), ck.Opts()...) - }, - ), nil - }, - ) return &InfraProvider{ - commManager: comm.NewCachingConnector(sweepTime, idleTime), - eventServiceCache: eventServiceCache, - chCfgCache: chconfig.NewRefCache(chConfigRefresh), - membershipCache: membership.NewRefCache(membershipRefresh), + commManager: comm.NewCachingConnector(sweepTime, idleTime), } } @@ -85,17 +42,6 @@ func (f *InfraProvider) Initialize(providers context.Providers) error { // Close frees resources and caches. func (f *InfraProvider) Close() { - logger.Debug("Closing event service cache...") - f.eventServiceCache.Close() - - logger.Debug("Closing membership cache...") - f.membershipCache.Close() - - logger.Debug("Closing channel configuration cache...") - f.chCfgCache.Close() - - // Comm Manager must be closed last since other resources - // may still be using it. logger.Debug("Closing comm manager...") f.commManager.Close() } @@ -105,71 +51,6 @@ func (f *InfraProvider) CommManager() fab.CommManager { return f.commManager } -// CreateEventService creates the event service. -func (f *InfraProvider) CreateEventService(ctx fab.ClientContext, channelID string, opts ...options.Opt) (fab.EventService, error) { - chnlCfg, err := f.CreateChannelCfg(ctx, channelID) - if err != nil { - return nil, err - } - key, err := NewCacheKey(ctx, chnlCfg, opts...) - if err != nil { - return nil, err - } - eventService, err := f.eventServiceCache.Get(key) - if err != nil { - return nil, err - } - - return eventService.(fab.EventService), nil -} - -// CreateChannelConfig initializes the channel config -func (f *InfraProvider) CreateChannelConfig(channelID string) (fab.ChannelConfig, error) { - return chconfig.New(channelID) -} - -// CreateChannelCfg creates and caches the channel configuration -func (f *InfraProvider) CreateChannelCfg(ctx fab.ClientContext, channelID string) (fab.ChannelCfg, error) { - if channelID == "" { - // System channel - return chconfig.NewChannelCfg(""), nil - } - chCfgRef, err := f.loadChannelCfgRef(ctx, channelID) - if err != nil { - return nil, err - } - chCfg, err := chCfgRef.Get() - if err != nil { - return nil, errors.WithMessage(err, "could not get chConfig cache reference") - } - return chCfg.(fab.ChannelCfg), nil -} - -// CreateChannelMembership returns and caches a channel member identifier -// A membership reference is returned that refreshes with the configured interval -func (f *InfraProvider) CreateChannelMembership(ctx fab.ClientContext, channelID string) (fab.ChannelMembership, error) { - chCfgRef, err := f.loadChannelCfgRef(ctx, channelID) - if err != nil { - return nil, err - } - key, err := membership.NewCacheKey(membership.Context{Providers: f.providerContext, EndpointConfig: ctx.EndpointConfig()}, - chCfgRef.Reference, channelID) - if err != nil { - return nil, err - } - ref, err := f.membershipCache.Get(key) - if err != nil { - return nil, err - } - - return ref.(*membership.Ref), nil -} - -// CreateChannelTransactor initializes the transactor -func (f *InfraProvider) CreateChannelTransactor(reqCtx reqContext.Context, cfg fab.ChannelCfg) (fab.Transactor, error) { - return channelImpl.NewTransactor(reqCtx, cfg) -} - // CreatePeerFromConfig returns a new default implementation of Peer based configuration func (f *InfraProvider) CreatePeerFromConfig(peerCfg *fab.NetworkPeer) (fab.Peer, error) { return peerImpl.New(f.providerContext.EndpointConfig(), peerImpl.FromPeerConfig(peerCfg)) @@ -183,45 +64,3 @@ func (f *InfraProvider) CreateOrdererFromConfig(cfg *fab.OrdererConfig) (fab.Ord } return newOrderer, nil } - -func (f *InfraProvider) loadChannelCfgRef(ctx fab.ClientContext, channelID string) (*chconfig.Ref, error) { - key, err := chconfig.NewCacheKey(ctx, f.CreateChannelConfig, channelID) - if err != nil { - return nil, err - } - c, err := f.chCfgCache.Get(key) - if err != nil { - return nil, err - } - - return c.(*chconfig.Ref), nil -} - -func getEventClient(ctx context.Client, chConfig fab.ChannelCfg, opts ...options.Opt) (fab.EventClient, error) { - useDeliver, err := useDeliverEvents(ctx, chConfig) - if err != nil { - return nil, err - } - - if useDeliver { - logger.Debugf("Using deliver events for channel [%s]", chConfig.ID()) - return deliverclient.New(ctx, chConfig, opts...) - } - - logger.Debugf("Using event hub events for channel [%s]", chConfig.ID()) - return eventhubclient.New(ctx, chConfig, opts...) -} - -func useDeliverEvents(ctx context.Client, chConfig fab.ChannelCfg) (bool, error) { - switch ctx.EndpointConfig().EventServiceType() { - case fab.DeliverEventServiceType: - return true, nil - case fab.EventHubEventServiceType: - return false, nil - case fab.AutoDetectEventServiceType: - logger.Debugf("Determining event service type from channel capabilities...") - return chConfig.HasCapability(fab.ApplicationGroupKey, fab.V1_1Capability), nil - default: - return false, errors.Errorf("unsupported event service type: %d", ctx.EndpointConfig().EventServiceType()) - } -} diff --git a/pkg/fabsdk/provider/fabpvdr/fabpvdr_test.go b/pkg/fabsdk/provider/fabpvdr/fabpvdr_test.go index 26512f3f4d..9f4a73b66c 100644 --- a/pkg/fabsdk/provider/fabpvdr/fabpvdr_test.go +++ b/pkg/fabsdk/provider/fabpvdr/fabpvdr_test.go @@ -10,7 +10,6 @@ import ( "fmt" "testing" - "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context" "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab" "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp" "github.com/hyperledger/fabric-sdk-go/pkg/core/config" @@ -23,14 +22,8 @@ import ( peerImpl "github.com/hyperledger/fabric-sdk-go/pkg/fab/peer" mspImpl "github.com/hyperledger/fabric-sdk-go/pkg/msp" mspmocks "github.com/hyperledger/fabric-sdk-go/pkg/msp/test/mockmsp" - "github.com/stretchr/testify/assert" ) -type mockClientContext struct { - context.Providers - msp.SigningIdentity -} - func TestCreateInfraProvider(t *testing.T) { newInfraProvider(t) } @@ -69,35 +62,6 @@ func TestCreatePeerFromConfig(t *testing.T) { verifyPeer(t, peer, url) } -func TestCreateMembership(t *testing.T) { - p := newInfraProvider(t) - ctx := mocks.NewMockProviderContext() - user := mspmocks.NewMockSigningIdentity("user", "user") - clientCtx := &mockClientContext{ - Providers: ctx, - SigningIdentity: user, - } - - m, err := p.CreateChannelMembership(clientCtx, "test") - assert.Nil(t, err) - assert.NotNil(t, m) -} - -func TestResolveEventServiceType(t *testing.T) { - ctx := mocks.NewMockContext(mspmocks.NewMockSigningIdentity("test", "Org1MSP")) - chConfig := mocks.NewMockChannelCfg("mychannel") - - useDeliver, err := useDeliverEvents(ctx, chConfig) - assert.NoError(t, err) - assert.Falsef(t, useDeliver, "expecting deliver events not to be used") - - chConfig.MockCapabilities[fab.ApplicationGroupKey][fab.V1_1Capability] = true - - useDeliver, err = useDeliverEvents(ctx, chConfig) - assert.NoError(t, err) - assert.Truef(t, useDeliver, "expecting deliver events to be used") -} - func newInfraProvider(t *testing.T) *InfraProvider { configBackend, err := config.FromFile("../../../../test/fixtures/config/config_test.yaml")() if err != nil { diff --git a/pkg/fabsdk/test/mocksdkapi/mocksdkapi.gen.go b/pkg/fabsdk/test/mocksdkapi/mocksdkapi.gen.go index ac7061be50..513c712071 100644 --- a/pkg/fabsdk/test/mocksdkapi/mocksdkapi.gen.go +++ b/pkg/fabsdk/test/mocksdkapi/mocksdkapi.gen.go @@ -147,6 +147,19 @@ func (m *MockServiceProviderFactory) EXPECT() *MockServiceProviderFactoryMockRec return m.recorder } +// CreateChannelProvider mocks base method +func (m *MockServiceProviderFactory) CreateChannelProvider(arg0 fab.EndpointConfig) (fab.ChannelProvider, error) { + ret := m.ctrl.Call(m, "CreateChannelProvider", arg0) + ret0, _ := ret[0].(fab.ChannelProvider) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// CreateChannelProvider indicates an expected call of CreateChannelProvider +func (mr *MockServiceProviderFactoryMockRecorder) CreateChannelProvider(arg0 interface{}) *gomock.Call { + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateChannelProvider", reflect.TypeOf((*MockServiceProviderFactory)(nil).CreateChannelProvider), arg0) +} + // CreateDiscoveryProvider mocks base method func (m *MockServiceProviderFactory) CreateDiscoveryProvider(arg0 fab.EndpointConfig) (fab.DiscoveryProvider, error) { ret := m.ctrl.Call(m, "CreateDiscoveryProvider", arg0)