@@ -23,19 +23,6 @@ import (
23
23
// is registered with the container.VMController
24
24
const ContainerType = "SYSTEM"
25
25
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
-
39
26
type inprocContainer struct {
40
27
chaincode shim.Chaincode
41
28
running bool
@@ -45,9 +32,10 @@ type inprocContainer struct {
45
32
}
46
33
47
34
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.
51
39
_shimStartInProc = shim .StartInProc
52
40
_inprocLoggerErrorf = inprocLogger .Errorf
53
41
)
@@ -61,30 +49,58 @@ func (s SysCCRegisteredErr) Error() string {
61
49
return fmt .Sprintf ("%s already registered" , string (s ))
62
50
}
63
51
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
+
64
72
//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 ]
67
75
if tmp != nil {
68
76
return SysCCRegisteredErr (path )
69
77
}
70
78
71
- typeRegistry [path ] = & inprocContainer {chaincode : cc }
79
+ r . typeRegistry [path ] = & inprocContainer {chaincode : cc }
72
80
return nil
73
81
}
74
82
75
83
//InprocVM is a vm. It is identified by a executable name
76
84
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
+ }
78
94
}
79
95
80
96
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 ]
82
98
if ipc != nil {
83
99
inprocLogger .Warningf ("chaincode instance exists for %s" , instName )
84
100
return ipc , nil
85
101
}
86
102
ipc = & inprocContainer {args : args , env : env , chaincode : ipctemplate .chaincode , stopChan : make (chan struct {})}
87
- instRegistry [instName ] = ipc
103
+ vm . registry . instRegistry [instName ] = ipc
88
104
inprocLogger .Debugf ("chaincode instance created for %s" , instName )
89
105
return ipc , nil
90
106
}
@@ -93,7 +109,7 @@ func (vm *InprocVM) getInstance(ctxt context.Context, ipctemplate *inprocContain
93
109
func (vm * InprocVM ) Deploy (ctxt context.Context , ccid ccintf.CCID , args []string , env []string , reader io.Reader ) error {
94
110
path := ccid .ChaincodeSpec .ChaincodeId .Path
95
111
96
- ipctemplate := typeRegistry [path ]
112
+ ipctemplate := vm . registry . typeRegistry [path ]
97
113
if ipctemplate == nil {
98
114
return fmt .Errorf (fmt .Sprintf ("%s not registered. Please register the system chaincode in inprocinstances.go" , path ))
99
115
}
@@ -126,7 +142,7 @@ func (ipc *inprocContainer) launchInProc(ctxt context.Context, id string, args [
126
142
if env == nil {
127
143
env = ipc .env
128
144
}
129
- err := _shimStartInProc (env , args , ipc .chaincode , ccRcvPeerSend , peerRcvCCSend )
145
+ err := shim . StartInProc (env , args , ipc .chaincode , ccRcvPeerSend , peerRcvCCSend )
130
146
if err != nil {
131
147
err = fmt .Errorf ("chaincode-support ended with err: %s" , err )
132
148
_inprocLoggerErrorf ("%s" , err )
@@ -165,7 +181,7 @@ func (ipc *inprocContainer) launchInProc(ctxt context.Context, id string, args [
165
181
func (vm * InprocVM ) Start (ctxt context.Context , ccid ccintf.CCID , args []string , env []string , filesToUpload map [string ][]byte , builder container.Builder ) error {
166
182
path := ccid .ChaincodeSpec .ChaincodeId .Path
167
183
168
- ipctemplate := typeRegistry [path ]
184
+ ipctemplate := vm . registry . typeRegistry [path ]
169
185
170
186
if ipctemplate == nil {
171
187
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,
208
224
func (vm * InprocVM ) Stop (ctxt context.Context , ccid ccintf.CCID , timeout uint , dontkill bool , dontremove bool ) error {
209
225
path := ccid .ChaincodeSpec .ChaincodeId .Path
210
226
211
- ipctemplate := typeRegistry [path ]
227
+ ipctemplate := vm . registry . typeRegistry [path ]
212
228
if ipctemplate == nil {
213
229
return fmt .Errorf ("%s not registered" , path )
214
230
}
215
231
216
232
instName , _ := vm .GetVMName (ccid , nil )
217
233
218
- ipc := instRegistry [instName ]
234
+ ipc := vm . registry . instRegistry [instName ]
219
235
220
236
if ipc == nil {
221
237
return fmt .Errorf ("%s not found" , instName )
@@ -227,7 +243,7 @@ func (vm *InprocVM) Stop(ctxt context.Context, ccid ccintf.CCID, timeout uint, d
227
243
228
244
ipc .stopChan <- struct {}{}
229
245
230
- delete (instRegistry , instName )
246
+ delete (vm . registry . instRegistry , instName )
231
247
//TODO stop
232
248
return nil
233
249
}
0 commit comments