Skip to content

Commit

Permalink
[FAB-8639] Create peers via factory
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
troyronda committed Mar 2, 2018
1 parent 6b39e3a commit 4816364
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 23 deletions.
13 changes: 12 additions & 1 deletion pkg/client/common/discovery/discoveryfilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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))
}
16 changes: 10 additions & 6 deletions pkg/client/common/discovery/staticdiscovery/staticdiscovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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))
}
19 changes: 16 additions & 3 deletions pkg/client/common/selection/dynamicselection/ccpolicyprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/fabsdk/api/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/fabsdk/fabsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/fabsdk/fabsdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion pkg/fabsdk/factory/defclient/sessfactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/fabsdk/factory/defsvc/svcfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand All @@ -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
Expand Down
18 changes: 16 additions & 2 deletions pkg/fabsdk/factory/defsvc/svcfactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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))
}
8 changes: 4 additions & 4 deletions pkg/fabsdk/mocks/mockfabsdkapi.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4816364

Please sign in to comment.