Skip to content

Commit bb602d5

Browse files
author
Jason Yellick
committed
FAB-9568 Remove package scoped syscc functions
The assorted DeploySysCC and RegisterSysCC functions are all package scoped and imply some operations on some local data. Additionally, as a bit of a hack, the chaincodes slice was modified to not actually initialize the chaincode, but instead defer initialization through a function. This CR fixes up both of these issues, creating a true 'Provider' for system chaincode with instance scoped verbs. Change-Id: I499bb1bdc8469302626d39c8a0bb59ea2da2757d Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent 68f5ded commit bb602d5

13 files changed

+188
-273
lines changed

core/chaincode/chaincode_support_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ func initMockPeer(chainIDs ...string) (*ChaincodeSupport, error) {
150150
GetApplicationConfigBoolRv: true,
151151
}
152152

153-
sccp := &scc.ProviderImpl{Peer: peer.Default, PeerSupport: msi}
153+
ipRegistry := inproccontroller.NewRegistry()
154+
sccp := &scc.Provider{Peer: peer.Default, PeerSupport: msi, Registrar: ipRegistry}
154155

155156
mockAclProvider = &mocks.MockACLProvider{}
156157
mockAclProvider.Reset()
@@ -171,7 +172,6 @@ func initMockPeer(chainIDs ...string) (*ChaincodeSupport, error) {
171172
config := GlobalConfig()
172173
config.StartupTimeout = 10 * time.Second
173174
config.ExecuteTimeout = 1 * time.Second
174-
ipRegistry := inproccontroller.NewRegistry()
175175
chaincodeSupport := NewChaincodeSupport(
176176
config,
177177
"0.0.0.0:7052",
@@ -193,15 +193,16 @@ func initMockPeer(chainIDs ...string) (*ChaincodeSupport, error) {
193193
// Mock policy checker
194194
policy.RegisterPolicyCheckerFactory(&mockPolicyCheckerFactory{})
195195

196-
scc.RegisterSysCCs(ipRegistry)
196+
for _, cc := range scc.CreateSysCCs(sccp) {
197+
sccp.RegisterSysCC(cc)
198+
}
197199

198200
globalBlockNum = make(map[string]uint64, len(chainIDs))
199201
for _, id := range chainIDs {
200-
scc.DeDeploySysCCs(id)
201202
if err := peer.MockCreateChain(id); err != nil {
202203
return nil, err
203204
}
204-
scc.DeploySysCCs(id)
205+
sccp.DeploySysCCs(id)
205206
// any chain other than the default testchainid does not have a MSP set up -> create one
206207
if id != util.GetTestChainID() {
207208
mspmgmt.XXXSetMSPManager(id, mspmgmt.GetManagerForChain(util.GetTestChainID()))
@@ -214,7 +215,6 @@ func initMockPeer(chainIDs ...string) (*ChaincodeSupport, error) {
214215

215216
func finitMockPeer(chainIDs ...string) {
216217
for _, c := range chainIDs {
217-
scc.DeDeploySysCCs(c)
218218
if lgr := peer.GetLedger(c); lgr != nil {
219219
lgr.Close()
220220
}
@@ -1088,7 +1088,7 @@ func TestStartAndWaitLaunchError(t *testing.T) {
10881088
}
10891089

10901090
func TestGetTxContextFromHandler(t *testing.T) {
1091-
h := Handler{txContexts: NewTransactionContexts(), sccp: &scc.ProviderImpl{Peer: peer.Default, PeerSupport: peer.DefaultSupport}}
1091+
h := Handler{txContexts: NewTransactionContexts(), sccp: &scc.Provider{Peer: peer.Default, PeerSupport: peer.DefaultSupport, Registrar: inproccontroller.NewRegistry()}}
10921092

10931093
chnl := "test"
10941094
txid := "1"

core/chaincode/exectransaction_test.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ func initPeer(chainIDs ...string) (net.Listener, *ChaincodeSupport, func(), erro
7878
GetApplicationConfigRv: &mc.MockApplication{CapabilitiesRv: &mc.MockApplicationCapabilities{}},
7979
GetApplicationConfigBoolRv: true,
8080
}
81-
sccp := &scc.ProviderImpl{Peer: peer.Default, PeerSupport: msi}
81+
82+
ipRegistry := inproccontroller.NewRegistry()
83+
sccp := &scc.Provider{Peer: peer.Default, PeerSupport: msi, Registrar: ipRegistry}
8284

8385
mockAclProvider = &aclmocks.MockACLProvider{}
8486
mockAclProvider.Reset()
@@ -119,7 +121,6 @@ func initPeer(chainIDs ...string) (net.Listener, *ChaincodeSupport, func(), erro
119121
certGenerator := accesscontrol.NewAuthenticator(ca)
120122
config := GlobalConfig()
121123
config.StartupTimeout = 3 * time.Minute
122-
ipRegistry := inproccontroller.NewRegistry()
123124
chaincodeSupport := NewChaincodeSupport(
124125
config,
125126
peerAddress,
@@ -142,15 +143,17 @@ func initPeer(chainIDs ...string) (net.Listener, *ChaincodeSupport, func(), erro
142143
// Mock policy checker
143144
policy.RegisterPolicyCheckerFactory(&mockPolicyCheckerFactory{})
144145

145-
scc.RegisterSysCCs(ipRegistry)
146+
for _, cc := range scc.CreateSysCCs(sccp) {
147+
sccp.RegisterSysCC(cc)
148+
}
146149

147150
for _, id := range chainIDs {
148-
scc.DeDeploySysCCs(id)
151+
sccp.DeDeploySysCCs(id)
149152
if err = peer.MockCreateChain(id); err != nil {
150153
closeListenerAndSleep(lis)
151154
return nil, nil, nil, err
152155
}
153-
scc.DeploySysCCs(id)
156+
sccp.DeploySysCCs(id)
154157
// any chain other than the default testchainid does not have a MSP set up -> create one
155158
if id != util.GetTestChainID() {
156159
mspmgmt.XXXSetMSPManager(id, mspmgmt.GetManagerForChain(util.GetTestChainID()))
@@ -166,7 +169,6 @@ func initPeer(chainIDs ...string) (net.Listener, *ChaincodeSupport, func(), erro
166169
func finitPeer(lis net.Listener, chainIDs ...string) {
167170
if lis != nil {
168171
for _, c := range chainIDs {
169-
scc.DeDeploySysCCs(c)
170172
if lgr := peer.GetLedger(c); lgr != nil {
171173
lgr.Close()
172174
}

core/chaincode/systemchaincode_test.go

+11-13
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/hyperledger/fabric/core/chaincode/accesscontrol"
1818
"github.com/hyperledger/fabric/core/chaincode/shim"
1919
"github.com/hyperledger/fabric/core/common/ccprovider"
20-
"github.com/hyperledger/fabric/core/common/sysccprovider"
2120
"github.com/hyperledger/fabric/core/container"
2221
"github.com/hyperledger/fabric/core/container/dockercontroller"
2322
"github.com/hyperledger/fabric/core/container/inproccontroller"
@@ -35,7 +34,6 @@ type oldSysCCInfo struct {
3534
}
3635

3736
func (osyscc *oldSysCCInfo) reset() {
38-
scc.MockResetSysCCs(osyscc.origSystemCC)
3937
viper.Set("chaincode.system", osyscc.origSysCCWhitelist)
4038
}
4139

@@ -142,36 +140,36 @@ func initSysCCTests() (*oldSysCCInfo, net.Listener, *ChaincodeSupport, error) {
142140
},
143141
),
144142
)
143+
sccp := &scc.Provider{Peer: peer.Default, PeerSupport: peer.DefaultSupport, Registrar: ipRegistry}
144+
chaincodeSupport.SetSysCCProvider(sccp)
145145
pb.RegisterChaincodeSupportServer(grpcServer, chaincodeSupport)
146146

147147
go grpcServer.Serve(lis)
148148

149149
//set systemChaincodes to sample
150-
sysccs := []*scc.SystemChaincode{
151-
{
152-
Enabled: true,
153-
Name: "sample_syscc",
154-
Path: "github.com/hyperledger/fabric/core/scc/samplesyscc",
155-
InitArgs: [][]byte{},
156-
Chaincode: func(sccp sysccprovider.SystemChaincodeProvider) shim.Chaincode { return &SampleSysCC{} },
157-
},
150+
syscc := &scc.SystemChaincode{
151+
Enabled: true,
152+
Name: "sample_syscc",
153+
Path: "github.com/hyperledger/fabric/core/scc/samplesyscc",
154+
InitArgs: [][]byte{},
155+
Chaincode: &SampleSysCC{},
158156
}
159157

160158
sysccinfo := &oldSysCCInfo{origSysCCWhitelist: viper.GetStringMapString("chaincode.system")}
161159

162160
// System chaincode has to be enabled
163161
viper.Set("chaincode.system", map[string]string{"sample_syscc": "true"})
164162

165-
sysccinfo.origSystemCC = scc.MockRegisterSysCCs(sysccs, ipRegistry)
163+
sccp.RegisterSysCC(syscc)
166164

167165
/////^^^ system initialization completed ^^^
168166
return sysccinfo, lis, chaincodeSupport, nil
169167
}
170168

171169
func deploySampleSysCC(t *testing.T, ctxt context.Context, chainID string, chaincodeSupport *ChaincodeSupport) error {
172-
scc.DeploySysCCs(chainID)
170+
chaincodeSupport.sccp.(*scc.Provider).DeploySysCCs(chainID)
173171

174-
defer scc.DeDeploySysCCs(chainID)
172+
defer chaincodeSupport.sccp.(*scc.Provider).DeDeploySysCCs(chainID)
175173

176174
url := "github.com/hyperledger/fabric/core/scc/sample_syscc"
177175

core/endorser/support.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type SupportImpl struct {
3333
Peer peer.Operations
3434
PeerSupport peer.Support
3535
ChaincodeSupport *chaincode.ChaincodeSupport
36+
SysCCProvider *scc.Provider
3637
}
3738

3839
func (s *SupportImpl) NewQueryCreator(channel string) (QueryCreator, error) {
@@ -50,7 +51,7 @@ func (s *SupportImpl) SigningIdentityForRequest(*pb.SignedProposal) (SigningIden
5051
// IsSysCCAndNotInvokableExternal returns true if the supplied chaincode is
5152
// ia system chaincode and it NOT invokable
5253
func (s *SupportImpl) IsSysCCAndNotInvokableExternal(name string) bool {
53-
return scc.IsSysCCAndNotInvokableExternal(name)
54+
return s.SysCCProvider.IsSysCCAndNotInvokableExternal(name)
5455
}
5556

5657
// GetTxSimulator returns the transaction simulator for the specified ledger
@@ -90,7 +91,7 @@ func (s *SupportImpl) GetTransactionByID(chid, txID string) (*pb.ProcessedTransa
9091
// IsSysCC returns true if the name matches a system chaincode's
9192
// system chaincode names are system, chain wide
9293
func (s *SupportImpl) IsSysCC(name string) bool {
93-
return scc.IsSysCC(name)
94+
return s.SysCCProvider.IsSysCC(name)
9495
}
9596

9697
// Execute a proposal and return the chaincode response

core/scc/importsysccs.go

+42-91
Original file line numberDiff line numberDiff line change
@@ -8,112 +8,63 @@ package scc
88

99
import (
1010
//import system chaincodes here
11-
"github.com/hyperledger/fabric/core/container/inproccontroller"
1211
"github.com/hyperledger/fabric/core/scc/cscc"
1312
"github.com/hyperledger/fabric/core/scc/lscc"
1413
"github.com/hyperledger/fabric/core/scc/qscc"
1514
"github.com/hyperledger/fabric/core/scc/vscc"
1615
)
1716

18-
//see systemchaincode_test.go for an example using "sample_syscc"
19-
var systemChaincodes = []*SystemChaincode{
20-
{
21-
Enabled: true,
22-
Name: "cscc",
23-
Path: "github.com/hyperledger/fabric/core/scc/cscc",
24-
InitArgs: nil,
25-
Chaincode: cscc.NewAsChaincode,
26-
InvokableExternal: true, // cscc is invoked to join a channel
27-
},
28-
{
29-
Enabled: true,
30-
Name: "lscc",
31-
Path: "github.com/hyperledger/fabric/core/scc/lscc",
32-
InitArgs: nil,
33-
Chaincode: lscc.NewAsChaincode,
34-
InvokableExternal: true, // lscc is invoked to deploy new chaincodes
35-
InvokableCC2CC: true, // lscc can be invoked by other chaincodes
36-
},
37-
{
38-
Enabled: true,
39-
Name: "vscc",
40-
Path: "github.com/hyperledger/fabric/core/scc/vscc",
41-
InitArgs: nil,
42-
Chaincode: vscc.NewAsChaincode,
43-
},
44-
{
45-
Enabled: true,
46-
Name: "qscc",
47-
Path: "github.com/hyperledger/fabric/core/chaincode/qscc",
48-
InitArgs: nil,
49-
Chaincode: qscc.NewAsChaincode,
50-
InvokableExternal: true, // qscc can be invoked to retrieve blocks
51-
InvokableCC2CC: true, // qscc can be invoked to retrieve blocks also by a cc
52-
},
17+
func builtInSystemChaincodes(p *Provider) []*SystemChaincode {
18+
return []*SystemChaincode{
19+
{
20+
Enabled: true,
21+
Name: "cscc",
22+
Path: "github.com/hyperledger/fabric/core/scc/cscc",
23+
InitArgs: nil,
24+
Chaincode: cscc.New(p),
25+
InvokableExternal: true, // cscc is invoked to join a channel
26+
},
27+
{
28+
Enabled: true,
29+
Name: "lscc",
30+
Path: "github.com/hyperledger/fabric/core/scc/lscc",
31+
InitArgs: nil,
32+
Chaincode: lscc.New(p),
33+
InvokableExternal: true, // lscc is invoked to deploy new chaincodes
34+
InvokableCC2CC: true, // lscc can be invoked by other chaincodes
35+
},
36+
{
37+
Enabled: true,
38+
Name: "vscc",
39+
Path: "github.com/hyperledger/fabric/core/scc/vscc",
40+
InitArgs: nil,
41+
Chaincode: vscc.New(p),
42+
},
43+
{
44+
Enabled: true,
45+
Name: "qscc",
46+
Path: "github.com/hyperledger/fabric/core/chaincode/qscc",
47+
InitArgs: nil,
48+
Chaincode: qscc.New(),
49+
InvokableExternal: true, // qscc can be invoked to retrieve blocks
50+
InvokableCC2CC: true, // qscc can be invoked to retrieve blocks also by a cc
51+
},
52+
}
5353
}
5454

5555
//DeploySysCCs is the hook for system chaincodes where system chaincodes are registered with the fabric
5656
//note the chaincode must still be deployed and launched like a user chaincode will be
57-
func DeploySysCCs(chainID string) {
58-
for _, sysCC := range systemChaincodes {
59-
deploySysCC(chainID, sysCC)
57+
func (p *Provider) DeploySysCCs(chainID string) {
58+
for _, sysCC := range p.SysCCs {
59+
sysCC.deploySysCC(chainID)
6060
}
6161
}
6262

6363
//DeDeploySysCCs is used in unit tests to stop and remove the system chaincodes before
6464
//restarting them in the same process. This allows clean start of the system
6565
//in the same process
66-
func DeDeploySysCCs(chainID string) {
67-
for _, sysCC := range systemChaincodes {
68-
DeDeploySysCC(chainID, sysCC)
69-
}
70-
}
71-
72-
//IsSysCC returns true if the name matches a system chaincode's
73-
//system chaincode names are system, chain wide
74-
func IsSysCC(name string) bool {
75-
for _, sysCC := range systemChaincodes {
76-
if sysCC.Name == name {
77-
return true
78-
}
79-
}
80-
return false
81-
}
82-
83-
// IsSysCCAndNotInvokableExternal returns true if the chaincode
84-
// is a system chaincode and *CANNOT* be invoked through
85-
// a proposal to this peer
86-
func IsSysCCAndNotInvokableExternal(name string) bool {
87-
for _, sysCC := range systemChaincodes {
88-
if sysCC.Name == name {
89-
return !sysCC.InvokableExternal
90-
}
66+
func (p *Provider) DeDeploySysCCs(chainID string) {
67+
for _, sysCC := range p.SysCCs {
68+
sysCC.deDeploySysCC(chainID)
9169
}
92-
return false
93-
}
94-
95-
// IsSysCCAndNotInvokableCC2CC returns true if the chaincode
96-
// is a system chaincode and *CANNOT* be invoked through
97-
// a cc2cc invocation
98-
func IsSysCCAndNotInvokableCC2CC(name string) bool {
99-
for _, sysCC := range systemChaincodes {
100-
if sysCC.Name == name {
101-
return !sysCC.InvokableCC2CC
102-
}
103-
}
104-
return false
105-
}
106-
107-
// MockRegisterSysCCs is used only for testing
108-
// This is needed to break import cycle
109-
func MockRegisterSysCCs(mockSysCCs []*SystemChaincode, ipRegistry *inproccontroller.Registry) []*SystemChaincode {
110-
orig := systemChaincodes
111-
systemChaincodes = mockSysCCs
112-
RegisterSysCCs(ipRegistry)
113-
return orig
114-
}
115-
116-
// MockResetSysCCs restore orig system ccs - is used only for testing
117-
func MockResetSysCCs(mockSysCCs []*SystemChaincode) {
118-
systemChaincodes = mockSysCCs
11970
}

core/scc/loadsysccs.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
"github.com/hyperledger/fabric/common/viperutil"
1616
"github.com/hyperledger/fabric/core/chaincode/shim"
17-
"github.com/hyperledger/fabric/core/common/sysccprovider"
1817
"github.com/pkg/errors"
1918
)
2019

@@ -35,7 +34,7 @@ var once sync.Once
3534
var sccPlugins []*SystemChaincode
3635

3736
// loadSysCCs reads system chaincode plugin configuration and loads them
38-
func loadSysCCs() []*SystemChaincode {
37+
func loadSysCCs(p *Provider) []*SystemChaincode {
3938
once.Do(func() {
4039
var config []*PluginConfig
4140
err := viperutil.EnhancedExactUnmarshalKey("chaincode.systemPlugins", &config)
@@ -54,7 +53,7 @@ func loadSysCCsWithConfig(configs []*PluginConfig) {
5453
Enabled: conf.Enabled,
5554
Name: conf.Name,
5655
Path: conf.Path,
57-
Chaincode: func(sccp sysccprovider.SystemChaincodeProvider) shim.Chaincode { return *plugin },
56+
Chaincode: *plugin,
5857
InvokableExternal: conf.InvokableExternal,
5958
InvokableCC2CC: conf.InvokableCC2CC,
6059
}

core/scc/loadsysccs_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ func TestLoadSCCPlugin(t *testing.T) {
4949
viper.SetConfigType("yaml")
5050
viper.ReadConfig(bytes.NewBuffer([]byte(testConfig)))
5151

52-
sccs := loadSysCCs()
52+
sccs := loadSysCCs(&Provider{})
5353
assert.Len(t, sccs, 1, "expected one SCC to be loaded")
54-
resp := sccs[0].Chaincode(nil).Invoke(nil)
54+
resp := sccs[0].Chaincode.Invoke(nil)
5555
assert.Equal(t, int32(shim.OK), resp.Status, "expected success response from scc")
5656
}
5757

0 commit comments

Comments
 (0)