Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 21d796d

Browse files
committed
[FAB-7831] Move funcs that propagate SDK context
This change moves SDK provides to a context object that can be passed around. This removes the need to expose all methods directly from the SDK object. The underlying FabricClient method is also refactored into a smaller Resource interface to limit exposure from the SDK. This work continues in upcoming changes. Change-Id: I9233fd6278683cd2b24a47c6d4c10032e75f7c5f Signed-off-by: Troy Ronda <troy@troyronda.com>
1 parent 6d35bfc commit 21d796d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+835
-782
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ mock-gen:
323323
mockgen -build_flags '$(GO_LDFLAGS_ARG)' github.com/hyperledger/fabric-sdk-go/api/apitxn ProposalProcessor | sed "s/github.com\/hyperledger\/fabric-sdk-go\/vendor\///g" | goimports > api/apitxn/mocks/mockapitxn.gen.go
324324
mockgen -build_flags '$(GO_LDFLAGS_ARG)' github.com/hyperledger/fabric-sdk-go/api/apiconfig Config | sed "s/github.com\/hyperledger\/fabric-sdk-go\/vendor\///g" | goimports > api/apiconfig/mocks/mockconfig.gen.go
325325
mockgen -build_flags '$(GO_LDFLAGS_ARG)' github.com/hyperledger/fabric-sdk-go/api/apifabca FabricCAClient | sed "s/github.com\/hyperledger\/fabric-sdk-go\/vendor\///g" | goimports > api/apifabca/mocks/mockfabriccaclient.gen.go
326-
mockgen -build_flags '$(GO_LDFLAGS_ARG)' github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/api SDK,CoreProviderFactory,ServiceProviderFactory,OrgClientFactory,SessionClientFactory | sed "s/github.com\/hyperledger\/fabric-sdk-go\/vendor\///g" | goimports > pkg/fabsdk/api/mocks/mocksdkapi.gen.go
326+
mockgen -build_flags '$(GO_LDFLAGS_ARG)' github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/api CoreProviders,SvcProviders,Providers,CoreProviderFactory,ServiceProviderFactory,OrgClientFactory,SessionClientFactory | sed "s/github.com\/hyperledger\/fabric-sdk-go\/vendor\///g" | goimports > pkg/fabsdk/api/mocks/mocksdkapi.gen.go
327327

328328
# TODO - Add cryptogen
329329
.PHONY: channel-config-gen

api/apicore/fabric.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ import (
1010
"crypto/x509"
1111

1212
"github.com/hyperledger/fabric-sdk-go/api/apiconfig"
13+
"github.com/hyperledger/fabric-sdk-go/api/apifabca"
1314
"github.com/hyperledger/fabric-sdk-go/api/apifabclient"
1415
)
1516

16-
// FabricProvider allows overriding of fabric objects such as peer and user
17+
// FabricProvider enables access to fabric objects such as peer and user
1718
type FabricProvider interface {
18-
NewClient(user apifabclient.User) (apifabclient.FabricClient, error)
19+
NewChannelClient(user apifabclient.IdentityContext, name string) (apifabclient.Channel, error)
20+
NewResourceClient(user apifabclient.IdentityContext) (apifabclient.Resource, error)
21+
NewCAClient(orgID string) (apifabca.FabricCAClient, error)
22+
1923
NewPeer(url string, certificate *x509.Certificate, serverHostOverride string) (apifabclient.Peer, error)
2024
NewPeerFromConfig(peerCfg *apiconfig.NetworkPeer) (apifabclient.Peer, error)
21-
// EnrollUser(orgID, name, pwd string) (apifabca.User, error)
2225
NewUser(name string, signingIdentity *apifabclient.SigningIdentity) (apifabclient.User, error)
2326
}

api/apifabclient/channel.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type Channel interface {
5252
OrganizationUnits() ([]string, error)
5353

5454
// Channel Info
55-
GenesisBlock(request *GenesisBlockRequest) (*common.Block, error)
55+
GenesisBlock() (*common.Block, error)
5656
JoinChannel(request *JoinChannelRequest) error
5757
UpdateChannel() bool
5858
IsReadonly() bool
@@ -74,14 +74,8 @@ type OrgAnchorPeer struct {
7474
Port int32
7575
}
7676

77-
// GenesisBlockRequest ...
78-
type GenesisBlockRequest struct {
79-
TxnID txn.TransactionID
80-
}
81-
8277
// JoinChannelRequest allows a set of peers to transact on a channel on the network
8378
type JoinChannelRequest struct {
8479
Targets []Peer
8580
GenesisBlock *common.Block
86-
TxnID txn.TransactionID
8781
}

api/apifabclient/fabricclient.go

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,37 @@ import (
1414
pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
1515
)
1616

17-
// FabricClient ...
17+
// Resource is a client that provides access to fabric resources such as chaincode.
18+
type Resource interface {
19+
Context
20+
client
21+
22+
// TODO refactor into channel provider and remove from Resource (upcoming CR)
23+
NewChannel(name string) (Channel, error)
24+
Channel(name string) Channel
25+
}
26+
27+
type client interface {
28+
CreateChannel(request CreateChannelRequest) (txn.TransactionID, error)
29+
InstallChaincode(request InstallChaincodeRequest) ([]*txn.TransactionProposalResponse, string, error)
30+
QueryInstalledChaincodes(peer Peer) (*pb.ChaincodeQueryResponse, error)
31+
QueryChannels(peer Peer) (*pb.ChannelQueryResponse, error)
32+
33+
ExtractChannelConfig(configEnvelope []byte) ([]byte, error)
34+
SignChannelConfig(config []byte, signer IdentityContext) (*common.ConfigSignature, error)
35+
}
36+
37+
// Context supplies the configuration and signing identity to client objects.
38+
type Context interface {
39+
SigningManager() SigningManager
40+
Config() config.Config
41+
CryptoSuite() apicryptosuite.CryptoSuite
42+
IdentityContext() IdentityContext
43+
}
44+
45+
// FabricClient provides access to infrastructure functionality.
46+
//
47+
// Deprecated: this interface has been renamed.
1848
/*
1949
* Main interaction handler with end user. A client instance provides a handler to interact
2050
* with a network of peers, orderers and optionally member services. An application using the
@@ -32,24 +62,14 @@ import (
3262
*
3363
*/
3464
type FabricClient interface {
35-
NewChannel(name string) (Channel, error)
36-
Channel(name string) Channel
37-
ExtractChannelConfig(configEnvelope []byte) ([]byte, error)
38-
SignChannelConfig(config []byte, signer User) (*common.ConfigSignature, error)
39-
CreateChannel(request CreateChannelRequest) (txn.TransactionID, error)
65+
Resource
66+
4067
QueryChannelInfo(name string, peers []Peer) (Channel, error)
41-
StateStore() KeyValueStore
42-
SigningManager() SigningManager
43-
CryptoSuite() apicryptosuite.CryptoSuite
44-
SaveUserToStateStore(user User, skipPersistence bool) error
68+
69+
SetIdentityContext(identity IdentityContext)
70+
SaveUserToStateStore(user User) error
4571
LoadUserFromStateStore(name string) (User, error)
46-
InstallChaincode(request InstallChaincodeRequest) ([]*txn.TransactionProposalResponse, string, error)
47-
QueryChannels(peer Peer) (*pb.ChannelQueryResponse, error)
48-
QueryInstalledChaincodes(peer Peer) (*pb.ChaincodeQueryResponse, error)
49-
UserContext() User
50-
SetUserContext(user User)
51-
Config() config.Config // TODO: refactor to a fab client config interface
52-
NewTxnID() (txn.TransactionID, error)
72+
StateStore() KeyValueStore
5373
}
5474

5575
// CreateChannelRequest requests channel creation on the network

api/apifabclient/identity.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,18 @@ import "github.com/hyperledger/fabric-sdk-go/api/apicryptosuite"
2323
// An application cannot use the Peer identity to sign things because the application doesn’t
2424
// have access to the Peer identity’s private key.
2525
type User interface {
26+
IdentityContext
27+
2628
Name() string
27-
MspID() string
2829
EnrollmentCertificate() []byte
29-
PrivateKey() apicryptosuite.Key
3030
Roles() []string
31+
}
32+
33+
// IdentityContext supplies the serialized identity and key reference.
34+
//
35+
// TODO - refactor SigningIdentity and this interface.
36+
type IdentityContext interface {
37+
MspID() string
3138
Identity() ([]byte, error)
39+
PrivateKey() apicryptosuite.Key
3240
}

api/apitxn/chmgmtclient/chmgmt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type SaveChannelRequest struct {
1515
// Path to channel configuration file
1616
ChannelConfig string
1717
// User that signs channel configuration
18-
SigningUser fab.User
18+
SigningIdentity fab.IdentityContext
1919
}
2020

2121
// SaveChannelOpts contains options for saving channel

def/factory/defclient/sessfactory.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,21 @@ func NewSessionClientFactory() *SessionClientFactory {
3333
return &f
3434
}
3535

36-
/*
37-
// NewSystemClient returns a new FabricClient.
38-
// TODO: duplicate of core factory method or rename?
39-
func (f *SessionClientFactory) NewSystemClient(sdk apisdk.SDK, session apisdk.Session, config apiconfig.Config) (fab.FabricClient, error) {
40-
return sdk.FabricProvider().NewClient(session.Identity())
41-
}
42-
*/
43-
4436
// NewChannelMgmtClient returns a client that manages channels (create/join channel)
45-
func (f *SessionClientFactory) NewChannelMgmtClient(sdk apisdk.SDK, session apisdk.Session, config apiconfig.Config) (chmgmt.ChannelMgmtClient, error) {
37+
func (f *SessionClientFactory) NewChannelMgmtClient(sdk apisdk.Providers, session apisdk.Session, config apiconfig.Config) (chmgmt.ChannelMgmtClient, error) {
4638
// For now settings are the same as for system client
47-
client, err := sdk.FabricProvider().NewClient(session.Identity())
39+
client, err := sdk.FabricProvider().NewResourceClient(session.Identity())
4840
if err != nil {
4941
return nil, err
5042
}
5143
return chmgmtImpl.NewChannelMgmtClient(client, config)
5244
}
5345

5446
// NewResourceMgmtClient returns a client that manages resources
55-
func (f *SessionClientFactory) NewResourceMgmtClient(sdk apisdk.SDK, session apisdk.Session, config apiconfig.Config, filter resmgmt.TargetFilter) (resmgmt.ResourceMgmtClient, error) {
47+
func (f *SessionClientFactory) NewResourceMgmtClient(sdk apisdk.Providers, session apisdk.Session, config apiconfig.Config, filter resmgmt.TargetFilter) (resmgmt.ResourceMgmtClient, error) {
5648

5749
// For now settings are the same as for system client
58-
client, err := sdk.FabricProvider().NewClient(session.Identity())
50+
client, err := sdk.FabricProvider().NewResourceClient(session.Identity())
5951
if err != nil {
6052
return nil, err
6153
}
@@ -70,13 +62,13 @@ func (f *SessionClientFactory) NewResourceMgmtClient(sdk apisdk.SDK, session api
7062

7163
// NewChannelClient returns a client that can execute transactions on specified channel
7264
// TODO - better refactoring for testing and/or extract getChannelImpl to another package
73-
func (f *SessionClientFactory) NewChannelClient(sdk apisdk.SDK, session apisdk.Session, config apiconfig.Config, channelID string) (apitxn.ChannelClient, error) {
65+
func (f *SessionClientFactory) NewChannelClient(sdk apisdk.Providers, session apisdk.Session, config apiconfig.Config, channelID string) (apitxn.ChannelClient, error) {
7466
// TODO: Add capablity to override sdk's selection and discovery provider
7567

7668
client := clientImpl.NewClient(sdk.ConfigProvider())
7769
client.SetCryptoSuite(sdk.CryptoSuiteProvider())
7870
client.SetStateStore(sdk.StateStoreProvider())
79-
client.SetUserContext(session.Identity())
71+
client.SetIdentityContext(session.Identity())
8072
client.SetSigningManager(sdk.SigningManager())
8173

8274
channel, err := getChannel(client, channelID)
@@ -103,7 +95,7 @@ func (f *SessionClientFactory) NewChannelClient(sdk apisdk.SDK, session apisdk.S
10395
}
10496

10597
// getChannel is helper method to initializes and returns a channel based on config
106-
func getChannel(client fab.FabricClient, channelID string) (fab.Channel, error) {
98+
func getChannel(client fab.Resource, channelID string) (fab.Channel, error) {
10799

108100
channel, err := client.NewChannel(channelID)
109101
if err != nil {
@@ -135,7 +127,7 @@ func getChannel(client fab.FabricClient, channelID string) (fab.Channel, error)
135127
return channel, nil
136128
}
137129

138-
func getEventHub(client fab.FabricClient, channelID string, session apisdk.Session) (*events.EventHub, error) {
130+
func getEventHub(client fab.Resource, channelID string, session apisdk.Session) (*events.EventHub, error) {
139131

140132
peerConfig, err := client.Config().ChannelPeers(channelID)
141133
if err != nil {

def/factory/defclient/sessfactory_test.go

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,12 @@ import (
2626
mockapisdk "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/api/mocks"
2727
)
2828

29-
/*
30-
func TestNewSystemClient(t *testing.T) {
31-
p := newMockProviders(t)
32-
33-
mockCtrl := gomock.NewController(t)
34-
defer mockCtrl.Finish()
35-
mockSDK := mock_context.NewMockSDK(mockCtrl)
36-
37-
mockSDK.EXPECT().FabricProvider().Return(p.FabricProvider)
38-
39-
factory := NewSessionClientFactory()
40-
session := newMockSession()
41-
42-
client, err := factory.NewSystemClient(mockSDK, session, p.ConfigProvider)
43-
if err != nil {
44-
t.Fatalf("Unexpected error creating system client %v", err)
45-
}
46-
47-
_, ok := client.(*clientImpl.Client)
48-
if !ok {
49-
t.Fatalf("Unexpected client created")
50-
}
51-
}
52-
*/
53-
5429
func TestNewChannelMgmtClient(t *testing.T) {
5530
p := newMockProviders(t)
5631

5732
mockCtrl := gomock.NewController(t)
5833
defer mockCtrl.Finish()
59-
mockSDK := mockapisdk.NewMockSDK(mockCtrl)
34+
mockSDK := mockapisdk.NewMockProviders(mockCtrl)
6035

6136
mockSDK.EXPECT().FabricProvider().Return(p.FabricProvider)
6237

@@ -79,7 +54,7 @@ func TestNewResourceMgmtClient(t *testing.T) {
7954

8055
mockCtrl := gomock.NewController(t)
8156
defer mockCtrl.Finish()
82-
mockSDK := mockapisdk.NewMockSDK(mockCtrl)
57+
mockSDK := mockapisdk.NewMockProviders(mockCtrl)
8358

8459
mockSDK.EXPECT().FabricProvider().Return(p.FabricProvider)
8560
mockSDK.EXPECT().DiscoveryProvider().Return(p.DiscoveryProvider)
@@ -103,7 +78,7 @@ func TestNewChannelClient(t *testing.T) {
10378

10479
mockCtrl := gomock.NewController(t)
10580
defer mockCtrl.Finish()
106-
mockSDK := mockapisdk.NewMockSDK(mockCtrl)
81+
mockSDK := mockapisdk.NewMockProviders(mockCtrl)
10782

10883
mockSDK.EXPECT().ConfigProvider().Return(p.ConfigProvider)
10984
mockSDK.EXPECT().CryptoSuiteProvider().Return(p.CryptosuiteProvider)
@@ -131,7 +106,7 @@ func TestNewChannelClientBadChannel(t *testing.T) {
131106

132107
mockCtrl := gomock.NewController(t)
133108
defer mockCtrl.Finish()
134-
mockSDK := mockapisdk.NewMockSDK(mockCtrl)
109+
mockSDK := mockapisdk.NewMockProviders(mockCtrl)
135110

136111
mockSDK.EXPECT().ConfigProvider().Return(p.ConfigProvider)
137112
mockSDK.EXPECT().CryptoSuiteProvider().Return(p.CryptosuiteProvider)
@@ -152,7 +127,7 @@ func TestNewChannelClientBadOrg(t *testing.T) {
152127

153128
mockCtrl := gomock.NewController(t)
154129
defer mockCtrl.Finish()
155-
mockSDK := mockapisdk.NewMockSDK(mockCtrl)
130+
mockSDK := mockapisdk.NewMockProviders(mockCtrl)
156131

157132
mockSDK.EXPECT().ConfigProvider().Return(p.ConfigProvider)
158133
mockSDK.EXPECT().CryptoSuiteProvider().Return(p.CryptosuiteProvider)
@@ -170,7 +145,7 @@ func TestNewChannelClientBadOrg(t *testing.T) {
170145
}
171146
}
172147

173-
func getChannelMock(client apifabclient.FabricClient, channelID string) (apifabclient.Channel, error) {
148+
func getChannelMock(client apifabclient.Resource, channelID string) (apifabclient.Channel, error) {
174149
return channel.NewChannel("channel", client)
175150
}
176151

@@ -235,7 +210,7 @@ func newMockProviders(t *testing.T) *mockProviders {
235210
}
236211

237212
type mockSession struct {
238-
user apifabclient.User
213+
user apifabclient.IdentityContext
239214
}
240215

241216
func newMockSession() *mockSession {
@@ -250,6 +225,6 @@ func newMockSessionWithUser(username, mspID string) *mockSession {
250225
return &session
251226
}
252227

253-
func (s *mockSession) Identity() apifabclient.User {
228+
func (s *mockSession) Identity() apifabclient.IdentityContext {
254229
return s.user
255230
}

def/provider/fabpvdr/fabpvdr.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/hyperledger/fabric-sdk-go/api/apifabclient"
1616
fabricCAClient "github.com/hyperledger/fabric-sdk-go/pkg/fabric-ca-client"
1717
clientImpl "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client"
18+
channelImpl "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/channel"
1819
identityImpl "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/identity"
1920
peerImpl "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/peer"
2021
)
@@ -38,13 +39,13 @@ func NewFabricProvider(config apiconfig.Config, stateStore apifabclient.KeyValue
3839
return &f
3940
}
4041

41-
// NewClient returns a new FabricClient initialized for the current instance of the SDK
42-
func (f *FabricProvider) NewClient(user apifabclient.User) (apifabclient.FabricClient, error) {
42+
// NewResourceClient returns a new client initialized for the current instance of the SDK
43+
func (f *FabricProvider) NewResourceClient(ic apifabclient.IdentityContext) (apifabclient.Resource, error) {
4344
client := clientImpl.NewClient(f.config)
4445

4546
client.SetCryptoSuite(f.cryptoSuite)
4647
client.SetStateStore(f.stateStore)
47-
client.SetUserContext(user)
48+
client.SetIdentityContext(ic)
4849
client.SetSigningManager(f.signer)
4950

5051
return client, nil
@@ -76,6 +77,18 @@ func (f *FabricProvider) NewPeerFromConfig(peerCfg *apiconfig.NetworkPeer) (apif
7677
return peerImpl.New(f.config, peerImpl.FromPeerConfig(peerCfg))
7778
}
7879

80+
// NewChannelClient returns a new client initialized for the current instance of the SDK
81+
func (f *FabricProvider) NewChannelClient(ic apifabclient.IdentityContext, name string) (apifabclient.Channel, error) {
82+
client := clientImpl.NewClient(f.config)
83+
84+
client.SetCryptoSuite(f.cryptoSuite)
85+
client.SetStateStore(f.stateStore)
86+
client.SetIdentityContext(ic)
87+
client.SetSigningManager(f.signer)
88+
89+
return channelImpl.NewChannel(name, client)
90+
}
91+
7992
/*
8093
TODO: Unclear that this EnrollUser helper is really needed at this level - I think not.
8194
Note: I renamed NewPreEnrolledUser to NewUser; and the old NewUser to EnrollUser

0 commit comments

Comments
 (0)