From 481636476356b9f09e7c1ea6d682014b4b2f78c6 Mon Sep 17 00:00:00 2001 From: Troy Ronda Date: Thu, 1 Mar 2018 14:58:51 -0500 Subject: [PATCH] [FAB-8639] Create peers via factory Peers should be created by factory method. This allows injecting of caches into the Peer implementation. Follow-up changes will refactor additional code to create peers via the factory, along with orderers. Change-Id: Ic763748bb09f2b9341bcb1962e3ba853c10ebc54 Signed-off-by: Troy Ronda --- .../common/discovery/discoveryfilter_test.go | 13 ++++++++++++- .../staticdiscovery/staticdiscovery.go | 16 ++++++++++------ .../staticdiscovery/staticdiscovery_test.go | 14 +++++++++++++- .../dynamicselection/ccpolicyprovider.go | 19 ++++++++++++++++--- pkg/fabsdk/api/factory.go | 2 +- pkg/fabsdk/fabsdk.go | 2 +- pkg/fabsdk/fabsdk_test.go | 2 +- .../factory/defclient/sessfactory_test.go | 2 +- pkg/fabsdk/factory/defsvc/svcfactory.go | 9 +++++++-- pkg/fabsdk/factory/defsvc/svcfactory_test.go | 18 ++++++++++++++++-- pkg/fabsdk/mocks/mockfabsdkapi.gen.go | 8 ++++---- 11 files changed, 82 insertions(+), 23 deletions(-) diff --git a/pkg/client/common/discovery/discoveryfilter_test.go b/pkg/client/common/discovery/discoveryfilter_test.go index af868bdec7..0addbcd4f0 100644 --- a/pkg/client/common/discovery/discoveryfilter_test.go +++ b/pkg/client/common/discovery/discoveryfilter_test.go @@ -10,8 +10,10 @@ import ( "testing" "github.com/hyperledger/fabric-sdk-go/pkg/client/common/discovery/staticdiscovery" + "github.com/hyperledger/fabric-sdk-go/pkg/context/api/core" "github.com/hyperledger/fabric-sdk-go/pkg/context/api/fab" "github.com/hyperledger/fabric-sdk-go/pkg/core/config" + "github.com/hyperledger/fabric-sdk-go/pkg/fab/peer" ) type mockFilter struct { @@ -31,7 +33,8 @@ func TestDiscoveryFilter(t *testing.T) { t.Fatalf(err.Error()) } - discoveryProvider, err := staticdiscovery.New(config) + peerCreator := defPeerCreator{config: config} + discoveryProvider, err := staticdiscovery.New(config, &peerCreator) if err != nil { t.Fatalf("Failed to setup discovery provider: %s", err) } @@ -61,3 +64,11 @@ func TestDiscoveryFilter(t *testing.T) { } } + +type defPeerCreator struct { + config core.Config +} + +func (pc *defPeerCreator) CreatePeerFromConfig(peerCfg *core.NetworkPeer) (fab.Peer, error) { + return peer.New(pc.config, peer.FromPeerConfig(peerCfg)) +} diff --git a/pkg/client/common/discovery/staticdiscovery/staticdiscovery.go b/pkg/client/common/discovery/staticdiscovery/staticdiscovery.go index e597313816..c795fcf32f 100644 --- a/pkg/client/common/discovery/staticdiscovery/staticdiscovery.go +++ b/pkg/client/common/discovery/staticdiscovery/staticdiscovery.go @@ -10,17 +10,21 @@ import ( "github.com/hyperledger/fabric-sdk-go/pkg/context/api/core" "github.com/hyperledger/fabric-sdk-go/pkg/context/api/fab" - "github.com/hyperledger/fabric-sdk-go/pkg/fab/peer" "github.com/pkg/errors" ) +type peerCreator interface { + CreatePeerFromConfig(peerCfg *core.NetworkPeer) (fab.Peer, error) +} + /** * Discovery Provider is used to discover peers on the network */ // DiscoveryProvider implements discovery provider type DiscoveryProvider struct { - config core.Config + config core.Config + fabPvdr peerCreator } // discoveryService implements discovery service @@ -30,8 +34,8 @@ type discoveryService struct { } // New returns discovery provider -func New(config core.Config) (*DiscoveryProvider, error) { - return &DiscoveryProvider{config: config}, nil +func New(config core.Config, fabPvdr peerCreator) (*DiscoveryProvider, error) { + return &DiscoveryProvider{config: config, fabPvdr: fabPvdr}, nil } // NewDiscoveryService return discovery service for specific channel @@ -49,7 +53,7 @@ func (dp *DiscoveryProvider) NewDiscoveryService(channelID string) (fab.Discover for _, p := range chPeers { - newPeer, err := peer.New(dp.config, peer.FromPeerConfig(&p.NetworkPeer)) + newPeer, err := dp.fabPvdr.CreatePeerFromConfig(&p.NetworkPeer) if err != nil || newPeer == nil { return nil, errors.WithMessage(err, "NewPeer failed") } @@ -65,7 +69,7 @@ func (dp *DiscoveryProvider) NewDiscoveryService(channelID string) (fab.Discover } for _, p := range netPeers { - newPeer, err := peer.New(dp.config, peer.FromPeerConfig(&p)) + newPeer, err := dp.fabPvdr.CreatePeerFromConfig(&p) if err != nil { return nil, errors.WithMessage(err, "NewPeerFromConfig failed") } diff --git a/pkg/client/common/discovery/staticdiscovery/staticdiscovery_test.go b/pkg/client/common/discovery/staticdiscovery/staticdiscovery_test.go index 3cfcefacd0..13eac2bc74 100644 --- a/pkg/client/common/discovery/staticdiscovery/staticdiscovery_test.go +++ b/pkg/client/common/discovery/staticdiscovery/staticdiscovery_test.go @@ -9,7 +9,10 @@ package staticdiscovery import ( "testing" + "github.com/hyperledger/fabric-sdk-go/pkg/context/api/core" + "github.com/hyperledger/fabric-sdk-go/pkg/context/api/fab" "github.com/hyperledger/fabric-sdk-go/pkg/core/config" + "github.com/hyperledger/fabric-sdk-go/pkg/fab/peer" ) func TestStaticDiscovery(t *testing.T) { @@ -19,7 +22,8 @@ func TestStaticDiscovery(t *testing.T) { t.Fatalf(err.Error()) } - discoveryProvider, err := New(config) + peerCreator := defPeerCreator{config: config} + discoveryProvider, err := New(config, &peerCreator) if err != nil { t.Fatalf("Failed to setup discovery provider: %s", err) } @@ -63,3 +67,11 @@ func TestStaticDiscovery(t *testing.T) { } } + +type defPeerCreator struct { + config core.Config +} + +func (pc *defPeerCreator) CreatePeerFromConfig(peerCfg *core.NetworkPeer) (fab.Peer, error) { + return peer.New(pc.config, peer.FromPeerConfig(peerCfg)) +} diff --git a/pkg/client/common/selection/dynamicselection/ccpolicyprovider.go b/pkg/client/common/selection/dynamicselection/ccpolicyprovider.go index 6201a492b7..3ec95fd918 100644 --- a/pkg/client/common/selection/dynamicselection/ccpolicyprovider.go +++ b/pkg/client/common/selection/dynamicselection/ccpolicyprovider.go @@ -17,7 +17,7 @@ import ( "github.com/hyperledger/fabric-sdk-go/pkg/client/channel" "github.com/hyperledger/fabric-sdk-go/pkg/context/api/core" - peerImpl "github.com/hyperledger/fabric-sdk-go/pkg/fab/peer" + "github.com/hyperledger/fabric-sdk-go/pkg/context/api/fab" "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk" "github.com/hyperledger/fabric-sdk-go/pkg/logging" "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/core/common/ccprovider" @@ -31,6 +31,10 @@ const ( ccDataProviderfunction = "getccdata" ) +type peerCreator interface { + CreatePeerFromConfig(peerCfg *core.NetworkPeer) (fab.Peer, error) +} + // CCPolicyProvider retrieves policy for the given chaincode ID type CCPolicyProvider interface { GetChaincodePolicy(chaincodeID string) (*common.SignaturePolicyEnvelope, error) @@ -54,7 +58,15 @@ func newCCPolicyProvider(sdk *fabsdk.FabricSDK, channelID string, userName strin return nil, errors.WithMessage(err, "unable to read configuration for channel peers") } - return &ccPolicyProvider{config: sdk.Config(), client: client, channelID: channelID, targetPeers: targetPeers, ccDataMap: make(map[string]*ccprovider.ChaincodeData)}, nil + cpp := ccPolicyProvider{ + config: sdk.Config(), + client: client, + channelID: channelID, + targetPeers: targetPeers, + ccDataMap: make(map[string]*ccprovider.ChaincodeData), + provider: sdk.FabricProvider(), + } + return &cpp, nil } type ccPolicyProvider struct { @@ -64,6 +76,7 @@ type ccPolicyProvider struct { targetPeers []core.ChannelPeer ccDataMap map[string]*ccprovider.ChaincodeData // TODO: Add expiry and configurable timeout for map entries mutex sync.RWMutex + provider peerCreator } func (dp *ccPolicyProvider) GetChaincodePolicy(chaincodeID string) (*common.SignaturePolicyEnvelope, error) { @@ -123,7 +136,7 @@ func (dp *ccPolicyProvider) queryChaincode(ccID string, ccFcn string, ccArgs [][ for _, p := range dp.targetPeers { - peer, err := peerImpl.New(dp.config, peerImpl.FromPeerConfig(&p.NetworkPeer)) + peer, err := dp.provider.CreatePeerFromConfig(&p.NetworkPeer) if err != nil { queryErrors = append(queryErrors, err.Error()) continue diff --git a/pkg/fabsdk/api/factory.go b/pkg/fabsdk/api/factory.go index 0c64ef424e..41e73a88ce 100644 --- a/pkg/fabsdk/api/factory.go +++ b/pkg/fabsdk/api/factory.go @@ -25,7 +25,7 @@ type CoreProviderFactory interface { // ServiceProviderFactory allows overriding default service providers (such as peer discovery) type ServiceProviderFactory interface { - CreateDiscoveryProvider(config core.Config) (fab.DiscoveryProvider, error) + CreateDiscoveryProvider(config core.Config, fabPvdr FabricProvider) (fab.DiscoveryProvider, error) CreateSelectionProvider(config core.Config) (fab.SelectionProvider, error) //CreateChannelProvider(ctx Context, channelID string) (ChannelProvider, error) } diff --git a/pkg/fabsdk/fabsdk.go b/pkg/fabsdk/fabsdk.go index 52d0ecd91a..81ff87a6fe 100644 --- a/pkg/fabsdk/fabsdk.go +++ b/pkg/fabsdk/fabsdk.go @@ -211,7 +211,7 @@ func initSDK(sdk *FabricSDK, opts []Option) error { sdk.fabricProvider = fabricProvider // Initialize discovery provider - discoveryProvider, err := sdk.opts.Service.CreateDiscoveryProvider(sdk.config) + discoveryProvider, err := sdk.opts.Service.CreateDiscoveryProvider(sdk.config, fabricProvider) if err != nil { return errors.WithMessage(err, "failed to initialize discovery provider") } diff --git a/pkg/fabsdk/fabsdk_test.go b/pkg/fabsdk/fabsdk_test.go index 4ad5f10896..fc68f277ae 100644 --- a/pkg/fabsdk/fabsdk_test.go +++ b/pkg/fabsdk/fabsdk_test.go @@ -109,7 +109,7 @@ func TestWithServicePkg(t *testing.T) { defer mockCtrl.Finish() factory := mockapisdk.NewMockServiceProviderFactory(mockCtrl) - factory.EXPECT().CreateDiscoveryProvider(c).Return(nil, nil) + factory.EXPECT().CreateDiscoveryProvider(c, gomock.Any()).Return(nil, nil) factory.EXPECT().CreateSelectionProvider(c).Return(nil, nil) _, err = New(WithConfig(c), WithServicePkg(factory)) diff --git a/pkg/fabsdk/factory/defclient/sessfactory_test.go b/pkg/fabsdk/factory/defclient/sessfactory_test.go index 75fd2d07cb..11e7aeee9e 100644 --- a/pkg/fabsdk/factory/defclient/sessfactory_test.go +++ b/pkg/fabsdk/factory/defclient/sessfactory_test.go @@ -108,7 +108,7 @@ func newMockProviders(t *testing.T) *mockProviders { t.Fatalf("Unexpected error creating fabric provider %v", err) } - dp, err := svcFactory.CreateDiscoveryProvider(config) + dp, err := svcFactory.CreateDiscoveryProvider(config, fabricProvider) if err != nil { t.Fatalf("Unexpected error creating discovery provider %v", err) } diff --git a/pkg/fabsdk/factory/defsvc/svcfactory.go b/pkg/fabsdk/factory/defsvc/svcfactory.go index 4019fe83b8..2d313fe99f 100644 --- a/pkg/fabsdk/factory/defsvc/svcfactory.go +++ b/pkg/fabsdk/factory/defsvc/svcfactory.go @@ -9,11 +9,16 @@ package defsvc import ( "github.com/hyperledger/fabric-sdk-go/pkg/context/api/core" "github.com/hyperledger/fabric-sdk-go/pkg/context/api/fab" + "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/api" discovery "github.com/hyperledger/fabric-sdk-go/pkg/client/common/discovery/staticdiscovery" selection "github.com/hyperledger/fabric-sdk-go/pkg/client/common/selection/staticselection" ) +type peerCreator interface { + CreatePeerFromConfig(peerCfg *core.NetworkPeer) (fab.Peer, error) +} + // ProviderFactory represents the default SDK provider factory for services. type ProviderFactory struct{} @@ -24,8 +29,8 @@ func NewProviderFactory() *ProviderFactory { } // CreateDiscoveryProvider returns a new default implementation of discovery provider -func (f *ProviderFactory) CreateDiscoveryProvider(config core.Config) (fab.DiscoveryProvider, error) { - return discovery.New(config) +func (f *ProviderFactory) CreateDiscoveryProvider(config core.Config, fabPvdr api.FabricProvider) (fab.DiscoveryProvider, error) { + return discovery.New(config, fabPvdr) } // CreateSelectionProvider returns a new default implementation of selection service diff --git a/pkg/fabsdk/factory/defsvc/svcfactory_test.go b/pkg/fabsdk/factory/defsvc/svcfactory_test.go index b231d2fdda..c0a090c1ab 100644 --- a/pkg/fabsdk/factory/defsvc/svcfactory_test.go +++ b/pkg/fabsdk/factory/defsvc/svcfactory_test.go @@ -11,15 +11,21 @@ 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/context/api/core" + "github.com/hyperledger/fabric-sdk-go/pkg/context/api/fab" "github.com/hyperledger/fabric-sdk-go/pkg/fab/mocks" + "github.com/hyperledger/fabric-sdk-go/pkg/fab/peer" + "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/provider/fabpvdr" ) func TestCreateDiscoveryProvider(t *testing.T) { - factory := NewProviderFactory() + ctx := mocks.NewMockContext(mocks.NewMockUser("testuser")) + fabPvdr := fabpvdr.New(ctx) + factory := NewProviderFactory() config := mocks.NewMockConfig() - dp, err := factory.CreateDiscoveryProvider(config) + dp, err := factory.CreateDiscoveryProvider(config, fabPvdr) if err != nil { t.Fatalf("Unexpected error creating discovery provider %v", err) } @@ -45,3 +51,11 @@ func TestCreateSelectionProvider(t *testing.T) { t.Fatalf("Unexpected selection provider created") } } + +type defPeerCreator struct { + config core.Config +} + +func (pc *defPeerCreator) CreatePeerFromConfig(peerCfg *core.NetworkPeer) (fab.Peer, error) { + return peer.New(pc.config, peer.FromPeerConfig(peerCfg)) +} diff --git a/pkg/fabsdk/mocks/mockfabsdkapi.gen.go b/pkg/fabsdk/mocks/mockfabsdkapi.gen.go index 653f0e354a..a8ca373921 100644 --- a/pkg/fabsdk/mocks/mockfabsdkapi.gen.go +++ b/pkg/fabsdk/mocks/mockfabsdkapi.gen.go @@ -389,16 +389,16 @@ func (m *MockServiceProviderFactory) EXPECT() *MockServiceProviderFactoryMockRec } // CreateDiscoveryProvider mocks base method -func (m *MockServiceProviderFactory) CreateDiscoveryProvider(arg0 core.Config) (fab.DiscoveryProvider, error) { - ret := m.ctrl.Call(m, "CreateDiscoveryProvider", arg0) +func (m *MockServiceProviderFactory) CreateDiscoveryProvider(arg0 core.Config, arg1 api0.FabricProvider) (fab.DiscoveryProvider, error) { + ret := m.ctrl.Call(m, "CreateDiscoveryProvider", arg0, arg1) ret0, _ := ret[0].(fab.DiscoveryProvider) ret1, _ := ret[1].(error) return ret0, ret1 } // CreateDiscoveryProvider indicates an expected call of CreateDiscoveryProvider -func (mr *MockServiceProviderFactoryMockRecorder) CreateDiscoveryProvider(arg0 interface{}) *gomock.Call { - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateDiscoveryProvider", reflect.TypeOf((*MockServiceProviderFactory)(nil).CreateDiscoveryProvider), arg0) +func (mr *MockServiceProviderFactoryMockRecorder) CreateDiscoveryProvider(arg0, arg1 interface{}) *gomock.Call { + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateDiscoveryProvider", reflect.TypeOf((*MockServiceProviderFactory)(nil).CreateDiscoveryProvider), arg0, arg1) } // CreateSelectionProvider mocks base method