@@ -11,7 +11,6 @@ import (
11
11
"time"
12
12
13
13
"github.com/golang/protobuf/proto"
14
- "github.com/hyperledger/fabric/core/chaincode/accesscontrol"
15
14
"github.com/hyperledger/fabric/core/common/ccprovider"
16
15
"github.com/hyperledger/fabric/core/common/sysccprovider"
17
16
"github.com/hyperledger/fabric/core/container"
@@ -21,54 +20,48 @@ import (
21
20
"golang.org/x/net/context"
22
21
)
23
22
24
- // CertGenerator generate client certificates for chaincode
25
- type CertGenerator interface {
26
- // Generate returns a certificate and private key and associates
27
- // the hash of the certificates with the given chaincode name
28
- Generate (ccName string ) (* accesscontrol.CertAndPrivKeyPair , error )
29
- }
30
-
31
23
// Runtime is used to manage chaincode runtime instances.
32
24
type Runtime interface {
33
25
Start (ctxt context.Context , cccid * ccprovider.CCContext , cds * pb.ChaincodeDeploymentSpec ) error
34
26
Stop (ctxt context.Context , cccid * ccprovider.CCContext , cds * pb.ChaincodeDeploymentSpec ) error
35
27
}
36
28
37
- // PackageProvider is responsible for getting the chaincode package from
38
- // the filesystem.
39
- type PackageProvider interface {
40
- GetChaincode (ccname string , ccversion string ) (ccprovider.CCPackage , error )
29
+ // ChaincodeSupport responsible for providing interfacing with chaincodes from the Peer.
30
+ type ChaincodeSupport struct {
31
+ Keepalive time.Duration
32
+ ExecuteTimeout time.Duration
33
+ UserRunsCC bool
34
+ Runtime Runtime
35
+ ACLProvider ACLProvider
36
+ HandlerRegistry * HandlerRegistry
37
+ Launcher * Launcher
38
+ sccp sysccprovider.SystemChaincodeProvider
41
39
}
42
40
43
41
// NewChaincodeSupport creates a new ChaincodeSupport instance.
44
42
func NewChaincodeSupport (
45
43
config * Config ,
46
44
peerAddress string ,
47
- userrunsCC bool ,
45
+ userRunsCC bool ,
48
46
caCert []byte ,
49
47
certGenerator CertGenerator ,
50
48
packageProvider PackageProvider ,
51
49
aclProvider ACLProvider ,
52
50
) * ChaincodeSupport {
53
51
cs := & ChaincodeSupport {
54
- caCert : caCert ,
55
- peerNetworkID : config .PeerNetworkID ,
56
- peerID : config .PeerID ,
57
- userRunsCC : userrunsCC ,
58
- ccStartupTimeout : config .StartupTimeout ,
59
- keepalive : config .Keepalive ,
60
- executetimeout : config .ExecuteTimeout ,
61
- HandlerRegistry : NewHandlerRegistry (userrunsCC ),
62
- PackageProvider : packageProvider ,
63
- ACLProvider : aclProvider ,
52
+ UserRunsCC : userRunsCC ,
53
+ Keepalive : config .Keepalive ,
54
+ ExecuteTimeout : config .ExecuteTimeout ,
55
+ HandlerRegistry : NewHandlerRegistry (userRunsCC ),
56
+ ACLProvider : aclProvider ,
64
57
}
65
58
66
59
// Keep TestQueries working
67
60
if ! config .TLSEnabled {
68
61
certGenerator = nil
69
62
}
70
63
71
- cs .ContainerRuntime = & ContainerRuntime {
64
+ cs .Runtime = & ContainerRuntime {
72
65
CertGenerator : certGenerator ,
73
66
Processor : ProcessFunc (container .VMCProcess ),
74
67
CACert : caCert ,
@@ -83,34 +76,16 @@ func NewChaincodeSupport(
83
76
}
84
77
85
78
cs .Launcher = & Launcher {
86
- Runtime : cs .ContainerRuntime ,
79
+ Runtime : cs .Runtime ,
87
80
Registry : cs .HandlerRegistry ,
88
- PackageProvider : cs . PackageProvider ,
81
+ PackageProvider : packageProvider ,
89
82
Lifecycle : & Lifecycle {Executor : cs },
90
83
StartupTimeout : config .StartupTimeout ,
91
84
}
92
85
93
86
return cs
94
87
}
95
88
96
- // ChaincodeSupport responsible for providing interfacing with chaincodes from the Peer.
97
- type ChaincodeSupport struct {
98
- caCert []byte
99
- peerAddress string
100
- ccStartupTimeout time.Duration
101
- peerNetworkID string
102
- peerID string
103
- keepalive time.Duration
104
- executetimeout time.Duration
105
- userRunsCC bool
106
- ContainerRuntime Runtime
107
- PackageProvider PackageProvider
108
- ACLProvider ACLProvider
109
- HandlerRegistry * HandlerRegistry
110
- Launcher * Launcher
111
- sccp sysccprovider.SystemChaincodeProvider
112
- }
113
-
114
89
// SetSysCCProvider is a bit of a hack to make a latent dependency of ChaincodeSupport
115
90
// be an explicit dependency. Because the chaincode support must be registered before
116
91
// the sysccprovider implementation can be created, we cannot make the sccp part of the
@@ -127,7 +102,7 @@ func (cs *ChaincodeSupport) Launch(ctx context.Context, cccid *ccprovider.CCCont
127
102
}
128
103
129
104
// TODO: There has to be a better way to do this...
130
- if cs .userRunsCC && ! cccid .Syscc {
105
+ if cs .UserRunsCC && ! cccid .Syscc {
131
106
chaincodeLogger .Error (
132
107
"You are attempting to perform an action other than Deploy on Chaincode that is not ready and you are in developer mode. Did you forget to Deploy your chaincode?" ,
133
108
)
@@ -146,7 +121,7 @@ func (cs *ChaincodeSupport) Stop(ctx context.Context, cccid *ccprovider.CCContex
146
121
cname := cccid .GetCanonicalName ()
147
122
defer cs .HandlerRegistry .Deregister (cname )
148
123
149
- err := cs .ContainerRuntime .Stop (ctx , cccid , cds )
124
+ err := cs .Runtime .Stop (ctx , cccid , cds )
150
125
if err != nil {
151
126
return err
152
127
}
@@ -179,23 +154,24 @@ func createCCMessage(messageType pb.ChaincodeMessage_Type, cid string, txid stri
179
154
return ccmsg , nil
180
155
}
181
156
182
- // execute executes a transaction and waits for it to complete until a timeout value.
183
- func (cs * ChaincodeSupport ) execute (ctxt context.Context , cccid * ccprovider.CCContext , msg * pb.ChaincodeMessage , timeout time.Duration ) (* pb.ChaincodeMessage , error ) {
184
- cname := cccid .GetCanonicalName ()
185
- chaincodeLogger .Debugf ("canonical name: %s" , cname )
186
-
187
- handler := cs .HandlerRegistry .Handler (cname )
188
- if handler == nil {
189
- chaincodeLogger .Debugf ("chaincode is not running: %s" , cname )
190
- return nil , errors .Errorf ("unable to invoke chaincode %s" , cname )
157
+ // ExecuteChaincode invokes chaincode with the provided arguments.
158
+ func (cs * ChaincodeSupport ) ExecuteChaincode (ctxt context.Context , cccid * ccprovider.CCContext , args [][]byte ) (* pb.Response , * pb.ChaincodeEvent , error ) {
159
+ invocationSpec := & pb.ChaincodeInvocationSpec {
160
+ ChaincodeSpec : & pb.ChaincodeSpec {
161
+ Type : pb .ChaincodeSpec_GOLANG ,
162
+ ChaincodeId : & pb.ChaincodeID {Name : cccid .Name },
163
+ Input : & pb.ChaincodeInput {Args : args },
164
+ },
191
165
}
192
166
193
- ccresp , err := handler . Execute (ctxt , cccid , msg , timeout )
167
+ res , ccevent , err := cs . ExecuteSpec (ctxt , cccid , invocationSpec )
194
168
if err != nil {
195
- return nil , errors .WithMessage (err , fmt .Sprintf ("error sending" ))
169
+ err = errors .WithMessage (err , "error invoking chaincode" )
170
+ chaincodeLogger .Errorf ("%+v" , err )
171
+ return nil , nil , err
196
172
}
197
173
198
- return ccresp , nil
174
+ return res , ccevent , err
199
175
}
200
176
201
177
//Execute - execute proposal, return original response of chaincode
@@ -222,7 +198,7 @@ func (cs *ChaincodeSupport) ExecuteSpec(ctxt context.Context, cccid *ccprovider.
222
198
return nil , nil , errors .WithMessage (err , "failed to create chaincode message" )
223
199
}
224
200
225
- resp , err := cs .execute (ctxt , cccid , ccMsg , cs . executetimeout )
201
+ resp , err := cs .execute (ctxt , cccid , ccMsg )
226
202
if err != nil {
227
203
return nil , nil , errors .Wrapf (err , "failed to execute transaction %s" , cccid .TxID )
228
204
}
@@ -252,22 +228,21 @@ func (cs *ChaincodeSupport) ExecuteSpec(ctxt context.Context, cccid *ccprovider.
252
228
}
253
229
}
254
230
255
- // ExecuteChaincode invokes chaincode with the provided arguments.
256
- func (cs * ChaincodeSupport ) ExecuteChaincode (ctxt context.Context , cccid * ccprovider.CCContext , args [][]byte ) (* pb.Response , * pb.ChaincodeEvent , error ) {
257
- invocationSpec := & pb.ChaincodeInvocationSpec {
258
- ChaincodeSpec : & pb.ChaincodeSpec {
259
- Type : pb .ChaincodeSpec_GOLANG ,
260
- ChaincodeId : & pb.ChaincodeID {Name : cccid .Name },
261
- Input : & pb.ChaincodeInput {Args : args },
262
- },
231
+ // execute executes a transaction and waits for it to complete until a timeout value.
232
+ func (cs * ChaincodeSupport ) execute (ctxt context.Context , cccid * ccprovider.CCContext , msg * pb.ChaincodeMessage ) (* pb.ChaincodeMessage , error ) {
233
+ cname := cccid .GetCanonicalName ()
234
+ chaincodeLogger .Debugf ("canonical name: %s" , cname )
235
+
236
+ handler := cs .HandlerRegistry .Handler (cname )
237
+ if handler == nil {
238
+ chaincodeLogger .Debugf ("chaincode is not running: %s" , cname )
239
+ return nil , errors .Errorf ("unable to invoke chaincode %s" , cname )
263
240
}
264
241
265
- res , ccevent , err := cs . ExecuteSpec (ctxt , cccid , invocationSpec )
242
+ ccresp , err := handler . Execute (ctxt , cccid , msg , cs . ExecuteTimeout )
266
243
if err != nil {
267
- err = errors .WithMessage (err , "error invoking chaincode" )
268
- chaincodeLogger .Errorf ("%+v" , err )
269
- return nil , nil , err
244
+ return nil , errors .WithMessage (err , fmt .Sprintf ("error sending" ))
270
245
}
271
246
272
- return res , ccevent , err
247
+ return ccresp , nil
273
248
}
0 commit comments