Skip to content

Commit

Permalink
[FAB-10325] Moved channel funcs out of InfraProvider
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
bstasyszyn committed May 29, 2018
1 parent f3528ca commit 1a4824e
Show file tree
Hide file tree
Showing 24 changed files with 275 additions and 403 deletions.
6 changes: 1 addition & 5 deletions pkg/client/channel/chclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/client/channel/chclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions pkg/client/event/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 1 addition & 2 deletions pkg/client/ledger/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions pkg/client/resmgmt/resmgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
44 changes: 1 addition & 43 deletions pkg/client/resmgmt/resmgmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion pkg/common/providers/fab/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ 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 {
Config() (ChannelConfig, error)
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.
Expand Down
5 changes: 0 additions & 5 deletions pkg/common/providers/fab/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 17 additions & 7 deletions pkg/fab/mocks/mockchprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ 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"
)

// 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.
Expand Down Expand Up @@ -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{
Expand All @@ -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
Expand All @@ -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
Expand Down
21 changes: 1 addition & 20 deletions pkg/fab/mocks/mockfabricprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -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() {
}
1 change: 1 addition & 0 deletions pkg/fabsdk/api/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
10 changes: 8 additions & 2 deletions pkg/fabsdk/fabsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/fabsdk/fabsdk_chconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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))
Expand Down Expand Up @@ -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

Expand All @@ -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()
Expand Down
1 change: 1 addition & 0 deletions pkg/fabsdk/fabsdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions pkg/fabsdk/factory/defsvc/svcfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package fabpvdr
package chpvdr

import (
"crypto/sha256"
Expand Down
Loading

0 comments on commit 1a4824e

Please sign in to comment.