Skip to content

Commit 68f5ded

Browse files
author
Jason Yellick
committed
FAB-9609 Remove inproccontroller singletons
As an ongoing effort to remove the assorted peer singletons, this CR addresses the inproccontroller singletons and instead passes explicit context for system chaincode registration. Change-Id: Ida28c682eeaff63f9319a3b72433511b144dc02b Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent b7dd5aa commit 68f5ded

12 files changed

+126
-144
lines changed

core/chaincode/chaincode_support_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ func initMockPeer(chainIDs ...string) (*ChaincodeSupport, error) {
171171
config := GlobalConfig()
172172
config.StartupTimeout = 10 * time.Second
173173
config.ExecuteTimeout = 1 * time.Second
174+
ipRegistry := inproccontroller.NewRegistry()
174175
chaincodeSupport := NewChaincodeSupport(
175176
config,
176177
"0.0.0.0:7052",
@@ -182,7 +183,7 @@ func initMockPeer(chainIDs ...string) (*ChaincodeSupport, error) {
182183
container.NewVMController(
183184
map[string]container.VMProvider{
184185
dockercontroller.ContainerType: dockercontroller.NewProvider(),
185-
inproccontroller.ContainerType: inproccontroller.NewProvider(),
186+
inproccontroller.ContainerType: ipRegistry,
186187
},
187188
),
188189
)
@@ -192,7 +193,7 @@ func initMockPeer(chainIDs ...string) (*ChaincodeSupport, error) {
192193
// Mock policy checker
193194
policy.RegisterPolicyCheckerFactory(&mockPolicyCheckerFactory{})
194195

195-
scc.RegisterSysCCs()
196+
scc.RegisterSysCCs(ipRegistry)
196197

197198
globalBlockNum = make(map[string]uint64, len(chainIDs))
198199
for _, id := range chainIDs {

core/chaincode/exectransaction_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ func initPeer(chainIDs ...string) (net.Listener, *ChaincodeSupport, func(), erro
119119
certGenerator := accesscontrol.NewAuthenticator(ca)
120120
config := GlobalConfig()
121121
config.StartupTimeout = 3 * time.Minute
122+
ipRegistry := inproccontroller.NewRegistry()
122123
chaincodeSupport := NewChaincodeSupport(
123124
config,
124125
peerAddress,
@@ -130,7 +131,7 @@ func initPeer(chainIDs ...string) (net.Listener, *ChaincodeSupport, func(), erro
130131
container.NewVMController(
131132
map[string]container.VMProvider{
132133
dockercontroller.ContainerType: dockercontroller.NewProvider(),
133-
inproccontroller.ContainerType: inproccontroller.NewProvider(),
134+
inproccontroller.ContainerType: ipRegistry,
134135
},
135136
),
136137
)
@@ -141,7 +142,7 @@ func initPeer(chainIDs ...string) (net.Listener, *ChaincodeSupport, func(), erro
141142
// Mock policy checker
142143
policy.RegisterPolicyCheckerFactory(&mockPolicyCheckerFactory{})
143144

144-
scc.RegisterSysCCs()
145+
scc.RegisterSysCCs(ipRegistry)
145146

146147
for _, id := range chainIDs {
147148
scc.DeDeploySysCCs(id)

core/chaincode/systemchaincode_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ func initSysCCTests() (*oldSysCCInfo, net.Listener, *ChaincodeSupport, error) {
126126
certGenerator := accesscontrol.NewAuthenticator(ca)
127127
config := GlobalConfig()
128128
config.ExecuteTimeout = 5 * time.Second
129+
ipRegistry := inproccontroller.NewRegistry()
129130
chaincodeSupport := NewChaincodeSupport(
130131
config,
131132
peerAddress,
@@ -137,7 +138,7 @@ func initSysCCTests() (*oldSysCCInfo, net.Listener, *ChaincodeSupport, error) {
137138
container.NewVMController(
138139
map[string]container.VMProvider{
139140
dockercontroller.ContainerType: dockercontroller.NewProvider(),
140-
inproccontroller.ContainerType: inproccontroller.NewProvider(),
141+
inproccontroller.ContainerType: ipRegistry,
141142
},
142143
),
143144
)
@@ -161,7 +162,7 @@ func initSysCCTests() (*oldSysCCInfo, net.Listener, *ChaincodeSupport, error) {
161162
// System chaincode has to be enabled
162163
viper.Set("chaincode.system", map[string]string{"sample_syscc": "true"})
163164

164-
sysccinfo.origSystemCC = scc.MockRegisterSysCCs(sysccs)
165+
sysccinfo.origSystemCC = scc.MockRegisterSysCCs(sysccs, ipRegistry)
165166

166167
/////^^^ system initialization completed ^^^
167168
return sysccinfo, lis, chaincodeSupport, nil

core/container/inproccontroller/inproccontroller.go

+44-28
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,6 @@ import (
2323
// is registered with the container.VMController
2424
const ContainerType = "SYSTEM"
2525

26-
// Provider implements container.VMProvider
27-
type Provider struct{}
28-
29-
// NewProvider creates a new instances of Provider
30-
func NewProvider() *Provider {
31-
return &Provider{}
32-
}
33-
34-
// NewVM creates an inproc VM instance
35-
func (c *Provider) NewVM() container.VM {
36-
return &InprocVM{}
37-
}
38-
3926
type inprocContainer struct {
4027
chaincode shim.Chaincode
4128
running bool
@@ -45,9 +32,10 @@ type inprocContainer struct {
4532
}
4633

4734
var (
48-
inprocLogger = flogging.MustGetLogger("inproccontroller")
49-
typeRegistry = make(map[string]*inprocContainer)
50-
instRegistry = make(map[string]*inprocContainer)
35+
inprocLogger = flogging.MustGetLogger("inproccontroller")
36+
37+
// TODO this is a very hacky way to do testing, we should find other ways
38+
// to test, or not statically inject these depenencies.
5139
_shimStartInProc = shim.StartInProc
5240
_inprocLoggerErrorf = inprocLogger.Errorf
5341
)
@@ -61,30 +49,58 @@ func (s SysCCRegisteredErr) Error() string {
6149
return fmt.Sprintf("%s already registered", string(s))
6250
}
6351

52+
// Registry stores registered system chaincodes.
53+
// It implements container.VMProvider and scc.Registrar
54+
type Registry struct {
55+
typeRegistry map[string]*inprocContainer
56+
instRegistry map[string]*inprocContainer
57+
}
58+
59+
// NewRegistry creates an initialized registry, ready to register system chaincodes.
60+
func NewRegistry() *Registry {
61+
return &Registry{
62+
typeRegistry: make(map[string]*inprocContainer),
63+
instRegistry: make(map[string]*inprocContainer),
64+
}
65+
}
66+
67+
// NewVM creates an inproc VM instance
68+
func (r *Registry) NewVM() container.VM {
69+
return NewInprocVM(r)
70+
}
71+
6472
//Register registers system chaincode with given path. The deploy should be called to initialize
65-
func Register(path string, cc shim.Chaincode) error {
66-
tmp := typeRegistry[path]
73+
func (r *Registry) Register(path string, cc shim.Chaincode) error {
74+
tmp := r.typeRegistry[path]
6775
if tmp != nil {
6876
return SysCCRegisteredErr(path)
6977
}
7078

71-
typeRegistry[path] = &inprocContainer{chaincode: cc}
79+
r.typeRegistry[path] = &inprocContainer{chaincode: cc}
7280
return nil
7381
}
7482

7583
//InprocVM is a vm. It is identified by a executable name
7684
type InprocVM struct {
77-
id string
85+
id string
86+
registry *Registry
87+
}
88+
89+
// NewInprocVM creates a new InprocVM
90+
func NewInprocVM(r *Registry) *InprocVM {
91+
return &InprocVM{
92+
registry: r,
93+
}
7894
}
7995

8096
func (vm *InprocVM) getInstance(ctxt context.Context, ipctemplate *inprocContainer, instName string, args []string, env []string) (*inprocContainer, error) {
81-
ipc := instRegistry[instName]
97+
ipc := vm.registry.instRegistry[instName]
8298
if ipc != nil {
8399
inprocLogger.Warningf("chaincode instance exists for %s", instName)
84100
return ipc, nil
85101
}
86102
ipc = &inprocContainer{args: args, env: env, chaincode: ipctemplate.chaincode, stopChan: make(chan struct{})}
87-
instRegistry[instName] = ipc
103+
vm.registry.instRegistry[instName] = ipc
88104
inprocLogger.Debugf("chaincode instance created for %s", instName)
89105
return ipc, nil
90106
}
@@ -93,7 +109,7 @@ func (vm *InprocVM) getInstance(ctxt context.Context, ipctemplate *inprocContain
93109
func (vm *InprocVM) Deploy(ctxt context.Context, ccid ccintf.CCID, args []string, env []string, reader io.Reader) error {
94110
path := ccid.ChaincodeSpec.ChaincodeId.Path
95111

96-
ipctemplate := typeRegistry[path]
112+
ipctemplate := vm.registry.typeRegistry[path]
97113
if ipctemplate == nil {
98114
return fmt.Errorf(fmt.Sprintf("%s not registered. Please register the system chaincode in inprocinstances.go", path))
99115
}
@@ -126,7 +142,7 @@ func (ipc *inprocContainer) launchInProc(ctxt context.Context, id string, args [
126142
if env == nil {
127143
env = ipc.env
128144
}
129-
err := _shimStartInProc(env, args, ipc.chaincode, ccRcvPeerSend, peerRcvCCSend)
145+
err := shim.StartInProc(env, args, ipc.chaincode, ccRcvPeerSend, peerRcvCCSend)
130146
if err != nil {
131147
err = fmt.Errorf("chaincode-support ended with err: %s", err)
132148
_inprocLoggerErrorf("%s", err)
@@ -165,7 +181,7 @@ func (ipc *inprocContainer) launchInProc(ctxt context.Context, id string, args [
165181
func (vm *InprocVM) Start(ctxt context.Context, ccid ccintf.CCID, args []string, env []string, filesToUpload map[string][]byte, builder container.Builder) error {
166182
path := ccid.ChaincodeSpec.ChaincodeId.Path
167183

168-
ipctemplate := typeRegistry[path]
184+
ipctemplate := vm.registry.typeRegistry[path]
169185

170186
if ipctemplate == nil {
171187
return fmt.Errorf(fmt.Sprintf("%s not registered", path))
@@ -208,14 +224,14 @@ func (vm *InprocVM) Start(ctxt context.Context, ccid ccintf.CCID, args []string,
208224
func (vm *InprocVM) Stop(ctxt context.Context, ccid ccintf.CCID, timeout uint, dontkill bool, dontremove bool) error {
209225
path := ccid.ChaincodeSpec.ChaincodeId.Path
210226

211-
ipctemplate := typeRegistry[path]
227+
ipctemplate := vm.registry.typeRegistry[path]
212228
if ipctemplate == nil {
213229
return fmt.Errorf("%s not registered", path)
214230
}
215231

216232
instName, _ := vm.GetVMName(ccid, nil)
217233

218-
ipc := instRegistry[instName]
234+
ipc := vm.registry.instRegistry[instName]
219235

220236
if ipc == nil {
221237
return fmt.Errorf("%s not found", instName)
@@ -227,7 +243,7 @@ func (vm *InprocVM) Stop(ctxt context.Context, ccid ccintf.CCID, timeout uint, d
227243

228244
ipc.stopChan <- struct{}{}
229245

230-
delete(instRegistry, instName)
246+
delete(vm.registry.instRegistry, instName)
231247
//TODO stop
232248
return nil
233249
}

0 commit comments

Comments
 (0)