Skip to content

Commit 580d091

Browse files
committed
[FAB-8660] Operations and PeerSupport interfaces
This introduces an interface that represents the currently exported functions of the peer. An instance of the interface is created during package initialization which delegates to the existing functions. - References to the peer functions in endorser.Support are replaced with a reference to the peer singleton. - CreatePeerServer is renamed to the more idiomatic NewPeerServer as it is a constructor - PeerSupportFactory is removed and an instance of PeerSupport is provided to endorser.SupportImpl and scc.ProviderFactory Change-Id: I07ae90092be8349bed60686f7b04a5b9ab02382a Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent 35f32a7 commit 580d091

13 files changed

+186
-108
lines changed

core/chaincode/chaincode_support_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/hyperledger/fabric/core/chaincode/accesscontrol"
3333
"github.com/hyperledger/fabric/core/chaincode/shim"
3434
"github.com/hyperledger/fabric/core/common/ccprovider"
35+
"github.com/hyperledger/fabric/core/common/sysccprovider"
3536
"github.com/hyperledger/fabric/core/config"
3637
"github.com/hyperledger/fabric/core/container"
3738
"github.com/hyperledger/fabric/core/ledger"
@@ -144,7 +145,9 @@ func initMockPeer(chainIDs ...string) error {
144145
GetApplicationConfigRv: &mc.MockApplication{&mc.MockApplicationCapabilities{}},
145146
GetApplicationConfigBoolRv: true,
146147
}
147-
peer.RegisterSupportFactory(&cmp.MockSupportFactoryImpl{msi})
148+
sysccprovider.RegisterSystemChaincodeProviderFactory(
149+
&scc.ProviderFactory{Peer: peer.Default, PeerSupport: msi},
150+
)
148151

149152
mockAclProvider = &mocks.MockACLProvider{}
150153
mockAclProvider.Reset()

core/endorser/endorser.go

+10
Original file line numberDiff line numberDiff line change
@@ -538,3 +538,13 @@ func (e *Endorser) ProcessProposal(ctx context.Context, signedProp *pb.SignedPro
538538

539539
return pResp, nil
540540
}
541+
542+
// shorttxid replicates the chaincode package function to shorten txids.
543+
// ~~TODO utilize a common shorttxid utility across packages.~~
544+
// TODO use a formal type for transaction ID and make it a stringer
545+
func shorttxid(txid string) string {
546+
if len(txid) < 8 {
547+
return txid
548+
}
549+
return txid[0:8]
550+
}

core/endorser/support.go

+13-19
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ import (
2626

2727
// SupportImpl provides an implementation of the endorser.Support interface
2828
// issuing calls to various static methods of the peer
29-
type SupportImpl struct{}
29+
type SupportImpl struct {
30+
Peer peer.Operations
31+
PeerSupport peer.Support
32+
}
3033

3134
// IsSysCCAndNotInvokableExternal returns true if the supplied chaincode is
3235
// ia system chaincode and it NOT invokable
@@ -38,7 +41,7 @@ func (s *SupportImpl) IsSysCCAndNotInvokableExternal(name string) bool {
3841
// a client may obtain more than one such simulator; they are made unique
3942
// by way of the supplied txid
4043
func (s *SupportImpl) GetTxSimulator(ledgername string, txid string) (ledger.TxSimulator, error) {
41-
lgr := peer.GetLedger(ledgername)
44+
lgr := s.Peer.GetLedger(ledgername)
4245
if lgr == nil {
4346
return nil, errors.Errorf("channel does not exist: %s", ledgername)
4447
}
@@ -48,7 +51,7 @@ func (s *SupportImpl) GetTxSimulator(ledgername string, txid string) (ledger.TxS
4851
// GetHistoryQueryExecutor gives handle to a history query executor for the
4952
// specified ledger
5053
func (s *SupportImpl) GetHistoryQueryExecutor(ledgername string) (ledger.HistoryQueryExecutor, error) {
51-
lgr := peer.GetLedger(ledgername)
54+
lgr := s.Peer.GetLedger(ledgername)
5255
if lgr == nil {
5356
return nil, errors.Errorf("channel does not exist: %s", ledgername)
5457
}
@@ -57,7 +60,7 @@ func (s *SupportImpl) GetHistoryQueryExecutor(ledgername string) (ledger.History
5760

5861
// GetTransactionByID retrieves a transaction by id
5962
func (s *SupportImpl) GetTransactionByID(chid, txID string) (*pb.ProcessedTransaction, error) {
60-
lgr := peer.GetLedger(chid)
63+
lgr := s.Peer.GetLedger(chid)
6164
if lgr == nil {
6265
return nil, errors.Errorf("failed to look up the ledger for channel %s", chid)
6366
}
@@ -68,13 +71,13 @@ func (s *SupportImpl) GetTransactionByID(chid, txID string) (*pb.ProcessedTransa
6871
return tx, nil
6972
}
7073

71-
//IsSysCC returns true if the name matches a system chaincode's
72-
//system chaincode names are system, chain wide
74+
// IsSysCC returns true if the name matches a system chaincode's
75+
// system chaincode names are system, chain wide
7376
func (s *SupportImpl) IsSysCC(name string) bool {
7477
return scc.IsSysCC(name)
7578
}
7679

77-
//Execute - execute proposal, return original response of chaincode
80+
// Execute a proposal and return the chaincode response
7881
func (s *SupportImpl) Execute(ctxt context.Context, cid, name, version, txid string, syscc bool, signedProp *pb.SignedProposal, prop *pb.Proposal, spec interface{}) (*pb.Response, *pb.ChaincodeEvent, error) {
7982
cccid := ccprovider.NewCCContext(cid, name, version, txid, syscc, signedProp, prop)
8083

@@ -105,8 +108,8 @@ func (s *SupportImpl) GetChaincodeDefinition(ctx context.Context, chainID string
105108
return chaincode.GetChaincodeDefinition(ctxt, txid, signedProp, prop, chainID, chaincodeID)
106109
}
107110

108-
//CheckACL checks the ACL for the resource for the channel using the
109-
//SignedProposal from which an id can be extracted for testing against a policy
111+
// CheckACL checks the ACL for the resource for the channel using the
112+
// SignedProposal from which an id can be extracted for testing against a policy
110113
func (s *SupportImpl) CheckACL(signedProp *pb.SignedProposal, chdr *common.ChannelHeader, shdr *common.SignatureHeader, hdrext *pb.ChaincodeHeaderExtension) error {
111114
return aclmgmt.GetACLProvider().CheckACL(resources.PROPOSE, chdr.ChannelId, signedProp)
112115
}
@@ -132,14 +135,5 @@ func (s *SupportImpl) CheckInstantiationPolicy(name, version string, cd resource
132135
// GetApplicationConfig returns the configtxapplication.SharedConfig for the channel
133136
// and whether the Application config exists
134137
func (s *SupportImpl) GetApplicationConfig(cid string) (channelconfig.Application, bool) {
135-
return peer.GetSupport().GetApplicationConfig(cid)
136-
}
137-
138-
// shorttxid replicates the chaincode package function to shorten txids.
139-
// TODO utilize a common shorttxid utility across packages.
140-
func shorttxid(txid string) string {
141-
if len(txid) < 8 {
142-
return txid
143-
}
144-
return txid[0:8]
138+
return s.PeerSupport.GetApplicationConfig(cid)
145139
}

core/peer/peer.go

+3-10
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ func updateTrustedRoots(cm channelconfig.Resources) {
505505
trustedRoots = append(trustedRoots, serverConfig.SecOpts.ServerRootCAs...)
506506
}
507507

508-
server := GetPeerServer()
508+
server := peerServer
509509
// now update the client roots for the peerServer
510510
if server != nil {
511511
err := server.SetClientRootCAs(trustedRoots)
@@ -675,11 +675,9 @@ func (c *channelPolicyManagerGetter) Manager(channelID string) (policies.Manager
675675
return policyManager, policyManager != nil
676676
}
677677

678-
// CreatePeerServer creates an instance of comm.GRPCServer
678+
// NewPeerServer creates an instance of comm.GRPCServer
679679
// This server is used for peer communications
680-
func CreatePeerServer(listenAddress string,
681-
serverConfig comm.ServerConfig) (comm.GRPCServer, error) {
682-
680+
func NewPeerServer(listenAddress string, serverConfig comm.ServerConfig) (comm.GRPCServer, error) {
683681
var err error
684682
peerServer, err = comm.NewGRPCServer(listenAddress, serverConfig)
685683
if err != nil {
@@ -689,11 +687,6 @@ func CreatePeerServer(listenAddress string,
689687
return peerServer, nil
690688
}
691689

692-
// GetPeerServer returns the peer server instance
693-
func GetPeerServer() comm.GRPCServer {
694-
return peerServer
695-
}
696-
697690
type collectionSupport struct {
698691
ledger.PeerLedger
699692
}

core/peer/peer_impl.go

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//
2+
// Copyright IBM Corp. All Rights Reserved.
3+
//
4+
// SPDX-License-Identifier: Apache-2.0
5+
//
6+
7+
package peer
8+
9+
import (
10+
"github.com/hyperledger/fabric/common/channelconfig"
11+
"github.com/hyperledger/fabric/common/policies"
12+
"github.com/hyperledger/fabric/common/resourcesconfig"
13+
"github.com/hyperledger/fabric/core/ledger"
14+
"github.com/hyperledger/fabric/protos/common"
15+
pb "github.com/hyperledger/fabric/protos/peer"
16+
)
17+
18+
// Operations exposes an interface to the package level functions that operated
19+
// on singletons in the package. This is a step towards moving from package
20+
// level data for the peer to instance level data.
21+
type Operations interface {
22+
CreateChainFromBlock(cb *common.Block) error
23+
GetChannelConfig(cid string) channelconfig.Resources
24+
GetChannelsInfo() []*pb.ChannelInfo
25+
GetCurrConfigBlock(cid string) *common.Block
26+
GetLedger(cid string) ledger.PeerLedger
27+
GetMSPIDs(cid string) []string
28+
GetPolicyManager(cid string) policies.Manager
29+
GetResourcesConfig(cid string) resourcesconfig.Resources
30+
InitChain(cid string)
31+
Initialize(init func(string))
32+
}
33+
34+
type peerImpl struct {
35+
createChainFromBlock func(cb *common.Block) error
36+
getChannelConfig func(cid string) channelconfig.Resources
37+
getChannelsInfo func() []*pb.ChannelInfo
38+
getCurrConfigBlock func(cid string) *common.Block
39+
getLedger func(cid string) ledger.PeerLedger
40+
getMSPIDs func(cid string) []string
41+
getPolicyManager func(cid string) policies.Manager
42+
getResourcesConfig func(cid string) resourcesconfig.Resources
43+
initChain func(cid string)
44+
initialize func(init func(string))
45+
}
46+
47+
// Default provides in implementation of the Peer interface that provides
48+
// access to the package level state.
49+
var Default Operations = &peerImpl{
50+
createChainFromBlock: CreateChainFromBlock,
51+
getChannelConfig: GetChannelConfig,
52+
getChannelsInfo: GetChannelsInfo,
53+
getCurrConfigBlock: GetCurrConfigBlock,
54+
getLedger: GetLedger,
55+
getMSPIDs: GetMSPIDs,
56+
getPolicyManager: GetPolicyManager,
57+
getResourcesConfig: GetResourcesConfig,
58+
initChain: InitChain,
59+
initialize: Initialize,
60+
}
61+
62+
var DefaultSupport Support = &supportImpl{operations: Default}
63+
64+
func (p *peerImpl) CreateChainFromBlock(cb *common.Block) error { return p.createChainFromBlock(cb) }
65+
func (p *peerImpl) GetChannelConfig(cid string) channelconfig.Resources {
66+
return p.getChannelConfig(cid)
67+
}
68+
func (p *peerImpl) GetChannelsInfo() []*pb.ChannelInfo { return p.getChannelsInfo() }
69+
func (p *peerImpl) GetCurrConfigBlock(cid string) *common.Block { return p.getCurrConfigBlock(cid) }
70+
func (p *peerImpl) GetLedger(cid string) ledger.PeerLedger { return p.getLedger(cid) }
71+
func (p *peerImpl) GetMSPIDs(cid string) []string { return p.getMSPIDs(cid) }
72+
func (p *peerImpl) GetPolicyManager(cid string) policies.Manager { return p.getPolicyManager(cid) }
73+
func (p *peerImpl) GetResourcesConfig(cid string) resourcesconfig.Resources {
74+
return p.getResourcesConfig(cid)
75+
}
76+
func (p *peerImpl) InitChain(cid string) { p.initChain(cid) }
77+
func (p *peerImpl) Initialize(init func(string)) { p.initialize(init) }

core/peer/peer_test.go

+8-12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/hyperledger/fabric/peer/gossip/mocks"
3030
"github.com/spf13/viper"
3131
"github.com/stretchr/testify/assert"
32+
"github.com/stretchr/testify/require"
3233
"google.golang.org/grpc"
3334
)
3435

@@ -53,7 +54,6 @@ func (ds *mockDeliveryClient) StopDeliverForChannel(chainID string) error {
5354

5455
// Stop terminates delivery service and closes the connection
5556
func (*mockDeliveryClient) Stop() {
56-
5757
}
5858

5959
type mockDeliveryClientFactory struct {
@@ -63,21 +63,17 @@ func (*mockDeliveryClientFactory) Service(g service.GossipService, endpoints []s
6363
return &mockDeliveryClient{}, nil
6464
}
6565

66-
func TestCreatePeerServer(t *testing.T) {
67-
68-
server, err := CreatePeerServer(":4050", comm.ServerConfig{})
69-
assert.NoError(t, err, "CreatePeerServer returned unexpected error")
70-
assert.Equal(t, "[::]:4050", server.Address(),
71-
"CreatePeerServer returned the wrong address")
66+
func TestNewPeerServer(t *testing.T) {
67+
server, err := NewPeerServer(":4050", comm.ServerConfig{})
68+
assert.NoError(t, err, "NewPeerServer returned unexpected error")
69+
assert.Equal(t, "[::]:4050", server.Address(), "NewPeerServer returned the wrong address")
7270
server.Stop()
7371

74-
_, err = CreatePeerServer("", comm.ServerConfig{})
75-
assert.Error(t, err, "expected CreatePeerServer to return error with missing address")
76-
72+
_, err = NewPeerServer("", comm.ServerConfig{})
73+
assert.Error(t, err, "expected NewPeerServer to return error with missing address")
7774
}
7875

7976
func TestInitChain(t *testing.T) {
80-
8177
chainId := "testChain"
8278
chainInitializer = func(cid string) {
8379
assert.Equal(t, chainId, cid, "chainInitializer received unexpected cid")
@@ -108,7 +104,7 @@ func TestCreateChainFromBlock(t *testing.T) {
108104
// Initialize gossip service
109105
grpcServer := grpc.NewServer()
110106
socket, err := net.Listen("tcp", fmt.Sprintf("%s:%d", "", 13611))
111-
assert.NoError(t, err)
107+
require.NoError(t, err)
112108

113109
msptesttools.LoadMSPSetupForTesting()
114110

core/peer/pkg_test.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,11 @@ func TestUpdateRootsFromConfigBlock(t *testing.T) {
167167
"Org1-cert.pem"))
168168
viper.Set("peer.fileSystemPath", "/var/hyperledger/test/")
169169
defer os.RemoveAll("/var/hyperledger/test/")
170-
err := peer.CreateChainFromBlock(block)
170+
err := peer.Default.CreateChainFromBlock(block)
171171
if err != nil {
172172
t.Fatalf("Failed to create config block (%s)", err)
173173
}
174-
t.Logf("Channel %s MSPIDs: (%s)", cid, peer.GetMSPIDs(cid))
174+
t.Logf("Channel %s MSPIDs: (%s)", cid, peer.Default.GetMSPIDs(cid))
175175
}
176176

177177
org1CertPool, err := createCertPool([][]byte{org1CA})
@@ -292,14 +292,12 @@ func TestUpdateRootsFromConfigBlock(t *testing.T) {
292292
test := test
293293
t.Run(test.name, func(t *testing.T) {
294294
t.Logf("Running test %s ...", test.name)
295-
_, err := peer.CreatePeerServer(test.listenAddress, test.serverConfig)
295+
server, err := peer.NewPeerServer(test.listenAddress, test.serverConfig)
296296
if err != nil {
297-
t.Fatalf("CreatePeerServer failed with error [%s]", err)
297+
t.Fatalf("NewPeerServer failed with error [%s]", err)
298298
} else {
299-
assert.NoError(t, err, "CreatePeerServer should not have returned an error")
300-
// get the server from peer
301-
server := peer.GetPeerServer()
302-
assert.NotNil(t, server, "GetPeerServer should not return a nil value")
299+
assert.NoError(t, err, "NewPeerServer should not have returned an error")
300+
assert.NotNil(t, server, "NewPeerServer should have created a server")
303301
// register a GRPC test service
304302
testpb.RegisterTestServiceServer(server.Server(), &testServiceServer{})
305303
go server.Start()

core/peer/support.go

+3-30
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ type Support interface {
3131
}
3232

3333
type supportImpl struct {
34+
operations Operations
3435
}
3536

3637
func (s *supportImpl) GetApplicationConfig(cid string) (channelconfig.Application, bool) {
37-
cc := GetChannelConfig(cid)
38+
cc := s.operations.GetChannelConfig(cid)
3839
if cc == nil {
3940
return nil, false
4041
}
@@ -43,38 +44,10 @@ func (s *supportImpl) GetApplicationConfig(cid string) (channelconfig.Applicatio
4344
}
4445

4546
func (s *supportImpl) ChaincodeByName(chainname, ccname string) (resourcesconfig.ChaincodeDefinition, bool) {
46-
rc := GetResourcesConfig(chainname)
47+
rc := s.operations.GetResourcesConfig(chainname)
4748
if rc == nil {
4849
return nil, false
4950
}
5051

5152
return rc.ChaincodeRegistry().ChaincodeByName(ccname)
5253
}
53-
54-
type SupportFactoryImpl struct {
55-
}
56-
57-
func (c *SupportFactoryImpl) NewSupport() Support {
58-
return &supportImpl{}
59-
}
60-
61-
// RegisterSupportFactory should be called to specify
62-
// which factory should be used to serve GetSupport calls
63-
func RegisterSupportFactory(ccfact SupportFactory) {
64-
supportFactory = ccfact
65-
}
66-
67-
// GetSupport returns a new Support instance by calling the factory
68-
func GetSupport() Support {
69-
if supportFactory == nil {
70-
panic("The factory must be set first via RegisterSupportFactory")
71-
}
72-
return supportFactory.NewSupport()
73-
}
74-
75-
// init is called when this package is loaded; this way by default,
76-
// all calls to GetSupport will be satisfied by SupportFactoryImpl,
77-
// unless RegisterSupportFactory is called again
78-
func init() {
79-
RegisterSupportFactory(&SupportFactoryImpl{})
80-
}

0 commit comments

Comments
 (0)