@@ -42,7 +42,7 @@ type PackageProvider interface {
42
42
GetChaincode (ccname string , ccversion string ) (ccprovider.CCPackage , error )
43
43
}
44
44
45
- // NewChaincodeSupport creates a new ChaincodeSupport instance
45
+ // NewChaincodeSupport creates a new ChaincodeSupport instance.
46
46
func NewChaincodeSupport (
47
47
config * Config ,
48
48
peerAddress string ,
@@ -108,7 +108,7 @@ type ChaincodeSupport struct {
108
108
// SetSysCCProvider is a bit of a hack to make a latent dependency of ChaincodeSupport
109
109
// be an explicit dependency. Because the chaincode support must be registered before
110
110
// the sysccprovider implementation can be created, we cannot make the sccp part of the
111
- // constructor for ChaincodeSupport
111
+ // constructor for ChaincodeSupport.
112
112
func (cs * ChaincodeSupport ) SetSysCCProvider (sccp sysccprovider.SystemChaincodeProvider ) {
113
113
cs .sccp = sccp
114
114
}
@@ -141,7 +141,7 @@ func (cs *ChaincodeSupport) launchAndWaitForReady(ctx context.Context, cccid *cc
141
141
case <- ready :
142
142
case err = <- launchFail :
143
143
case <- time .After (cs .ccStartupTimeout ):
144
- err = errors .Errorf ("timeout expired while starting chaincode %s(tx:%s) " , cname , cccid .TxID )
144
+ err = errors .Errorf ("timeout expired while starting chaincode %s for transaction %s " , cname , cccid .TxID )
145
145
}
146
146
147
147
if err != nil {
@@ -155,7 +155,7 @@ func (cs *ChaincodeSupport) launchAndWaitForReady(ctx context.Context, cccid *cc
155
155
return nil
156
156
}
157
157
158
- //Stop stops a chaincode if running
158
+ // Stop stops a chaincode if running.
159
159
func (cs * ChaincodeSupport ) Stop (ctx context.Context , cccid * ccprovider.CCContext , cds * pb.ChaincodeDeploymentSpec ) error {
160
160
cname := cccid .GetCanonicalName ()
161
161
defer cs .HandlerRegistry .Deregister (cname )
@@ -256,27 +256,29 @@ func (cs *ChaincodeSupport) Register(stream pb.ChaincodeSupport_RegisterServer)
256
256
}
257
257
258
258
// createCCMessage creates a transaction message.
259
- func createCCMessage (typ pb.ChaincodeMessage_Type , cid string , txid string , cMsg * pb.ChaincodeInput ) (* pb.ChaincodeMessage , error ) {
259
+ func createCCMessage (messageType pb.ChaincodeMessage_Type , cid string , txid string , cMsg * pb.ChaincodeInput ) (* pb.ChaincodeMessage , error ) {
260
260
payload , err := proto .Marshal (cMsg )
261
261
if err != nil {
262
- fmt .Printf (err .Error ())
263
262
return nil , err
264
263
}
265
- return & pb.ChaincodeMessage {Type : typ , Payload : payload , Txid : txid , ChannelId : cid }, nil
264
+ ccmsg := & pb.ChaincodeMessage {
265
+ Type : messageType ,
266
+ Payload : payload ,
267
+ Txid : txid ,
268
+ ChannelId : cid ,
269
+ }
270
+ return ccmsg , nil
266
271
}
267
272
268
- // Execute executes a transaction and waits for it to complete until a timeout value.
269
- func (cs * ChaincodeSupport ) Execute (ctxt context.Context , cccid * ccprovider.CCContext , msg * pb.ChaincodeMessage , timeout time.Duration ) (* pb.ChaincodeMessage , error ) {
270
- chaincodeLogger .Debugf ("Entry" )
271
- defer chaincodeLogger .Debugf ("Exit" )
273
+ // execute executes a transaction and waits for it to complete until a timeout value.
274
+ func (cs * ChaincodeSupport ) execute (ctxt context.Context , cccid * ccprovider.CCContext , msg * pb.ChaincodeMessage , timeout time.Duration ) (* pb.ChaincodeMessage , error ) {
272
275
cname := cccid .GetCanonicalName ()
276
+ chaincodeLogger .Debugf ("canonical name: %s" , cname )
273
277
274
- chaincodeLogger .Debugf ("chaincode canonical name: %s" , cname )
275
- //we expect the chaincode to be running... sanity check
276
278
handler := cs .HandlerRegistry .Handler (cname )
277
279
if handler == nil {
278
- chaincodeLogger .Debugf ("cannot execute- chaincode is not running: %s" , cname )
279
- return nil , errors .Errorf ("cannot execute transaction for %s" , cname )
280
+ chaincodeLogger .Debugf ("chaincode is not running: %s" , cname )
281
+ return nil , errors .Errorf ("unable to invoke chaincode %s" , cname )
280
282
}
281
283
282
284
ccresp , err := handler .Execute (ctxt , cccid , msg , timeout )
@@ -289,17 +291,15 @@ func (cs *ChaincodeSupport) Execute(ctxt context.Context, cccid *ccprovider.CCCo
289
291
290
292
//Execute - execute proposal, return original response of chaincode
291
293
func (cs * ChaincodeSupport ) ExecuteSpec (ctxt context.Context , cccid * ccprovider.CCContext , spec ccprovider.ChaincodeSpecGetter ) (* pb.Response , * pb.ChaincodeEvent , error ) {
292
- var err error
293
- var cds * pb.ChaincodeDeploymentSpec
294
- var ci * pb.ChaincodeInvocationSpec
295
-
296
- //init will call the Init method of a on a chain
297
- cctyp := pb .ChaincodeMessage_INIT
298
- if cds , _ = spec .(* pb.ChaincodeDeploymentSpec ); cds == nil {
299
- if ci , _ = spec .(* pb.ChaincodeInvocationSpec ); ci == nil {
300
- panic ("Execute should be called with deployment or invocation spec" )
301
- }
294
+ var cctyp pb.ChaincodeMessage_Type
295
+
296
+ switch spec .(type ) {
297
+ case * pb.ChaincodeDeploymentSpec : // init
298
+ cctyp = pb .ChaincodeMessage_INIT
299
+ case * pb.ChaincodeInvocationSpec : // invoke
302
300
cctyp = pb .ChaincodeMessage_TRANSACTION
301
+ default :
302
+ return nil , nil , errors .New ("a deployment or invocation spec is required" )
303
303
}
304
304
305
305
cMsg , err := cs .Launch (ctxt , cccid , spec )
@@ -308,101 +308,95 @@ func (cs *ChaincodeSupport) ExecuteSpec(ctxt context.Context, cccid *ccprovider.
308
308
}
309
309
310
310
cMsg .Decorations = cccid .ProposalDecorations
311
-
312
- var ccMsg * pb.ChaincodeMessage
313
- ccMsg , err = createCCMessage (cctyp , cccid .ChainID , cccid .TxID , cMsg )
311
+ ccMsg , err := createCCMessage (cctyp , cccid .ChainID , cccid .TxID , cMsg )
314
312
if err != nil {
315
313
return nil , nil , errors .WithMessage (err , "failed to create chaincode message" )
316
314
}
317
315
318
- resp , err := cs .Execute (ctxt , cccid , ccMsg , cs .executetimeout )
316
+ resp , err := cs .execute (ctxt , cccid , ccMsg , cs .executetimeout )
319
317
if err != nil {
320
- // Rollback transaction
321
- return nil , nil , errors .WithMessage (err , "failed to execute transaction" )
322
- } else if resp == nil {
323
- // Rollback transaction
324
- return nil , nil , errors .Errorf ("failed to receive a response for txid (%s)" , cccid .TxID )
318
+ return nil , nil , errors .WithMessage (err , "failed to execute transaction %s" )
319
+ }
320
+ if resp == nil {
321
+ return nil , nil , errors .Errorf ("nil response from transaction %s" , cccid .TxID )
325
322
}
326
323
327
324
if resp .ChaincodeEvent != nil {
328
325
resp .ChaincodeEvent .ChaincodeId = cccid .Name
329
326
resp .ChaincodeEvent .TxId = cccid .TxID
330
327
}
331
328
332
- if resp .Type == pb .ChaincodeMessage_COMPLETED {
329
+ switch resp .Type {
330
+ case pb .ChaincodeMessage_COMPLETED :
333
331
res := & pb.Response {}
334
- unmarshalErr := proto .Unmarshal (resp .Payload , res )
335
- if unmarshalErr != nil {
336
- return nil , nil , errors .Wrap ( unmarshalErr , fmt . Sprintf ( "failed to unmarshal response for txid (%s) " , cccid .TxID ) )
332
+ err := proto .Unmarshal (resp .Payload , res )
333
+ if err != nil {
334
+ return nil , nil , errors .Wrapf ( err , "failed to unmarshal response for transaction %s " , cccid .TxID )
337
335
}
338
-
339
- // Success
340
336
return res , resp .ChaincodeEvent , nil
341
- } else if resp .Type == pb .ChaincodeMessage_ERROR {
342
- // Rollback transaction
343
- return nil , resp .ChaincodeEvent , errors .Errorf ("transaction returned with failure: %s" , string (resp .Payload ))
344
- }
345
337
346
- //TODO - this should never happen ... a panic is more appropriate but will save that for future
347
- return nil , nil , errors .Errorf ("receive a response for txid (%s) but in invalid state (%d)" , cccid .TxID , resp .Type )
348
- }
338
+ case pb .ChaincodeMessage_ERROR :
339
+ return nil , resp .ChaincodeEvent , errors .Errorf ("transaction returned with failure: %s" , resp .Payload )
349
340
350
- //create a chaincode invocation spec
351
- func createCIS (ccname string , args [][]byte ) (* pb.ChaincodeInvocationSpec , error ) {
352
- var err error
353
- spec := & pb.ChaincodeInvocationSpec {ChaincodeSpec : & pb.ChaincodeSpec {Type : pb .ChaincodeSpec_Type (pb .ChaincodeSpec_Type_value ["GOLANG" ]), ChaincodeId : & pb.ChaincodeID {Name : ccname }, Input : & pb.ChaincodeInput {Args : args }}}
354
- if nil != err {
355
- return nil , err
341
+ default :
342
+ return nil , nil , errors .Errorf ("unexpected response type %d for transaction %s" , resp .Type , cccid .TxID )
356
343
}
357
- return spec , nil
358
344
}
359
345
360
346
// GetCDS retrieves a chaincode deployment spec for the required chaincode
361
347
func (cs * ChaincodeSupport ) GetCDS (ctxt context.Context , txid string , signedProp * pb.SignedProposal , prop * pb.Proposal , chainID string , chaincodeID string ) ([]byte , error ) {
362
348
version := util .GetSysCCVersion ()
363
349
cccid := ccprovider .NewCCContext (chainID , "lscc" , version , txid , true , signedProp , prop )
364
- res , _ , err := cs .ExecuteChaincode (ctxt , cccid , [][]byte {[]byte ("getdepspec" ), []byte (chainID ), []byte (chaincodeID )})
350
+
351
+ args := util .ToChaincodeArgs ("getdepspec" , chainID , chaincodeID )
352
+ res , _ , err := cs .ExecuteChaincode (ctxt , cccid , args )
365
353
if err != nil {
366
- return nil , errors .WithMessage (err , fmt . Sprintf ( "execute getdepspec(%s, %s) of LSCC error " , chainID , chaincodeID ) )
354
+ return nil , errors .Wrapf (err , " getdepspec %s/%s " , chainID , chaincodeID )
367
355
}
368
356
if res .Status != shim .OK {
369
- return nil , errors .Errorf ("get ChaincodeDeploymentSpec for %s/%s from LSCC error : %s" , chaincodeID , chainID , res .Message )
357
+ return nil , errors .Errorf ("getdepspec %s/%s: %s" , chainID , chaincodeID , res .Message )
370
358
}
371
359
372
360
return res .Payload , nil
373
361
}
374
362
375
- // GetChaincodeDefinition returns ccprovider.ChaincodeDefinition for the chaincode with the supplied name
363
+ // GetChaincodeDefinition returns a resourcesconfig.ChaincodeDefinition for the chaincode
364
+ // associated with the provided channel and name.
376
365
func (cs * ChaincodeSupport ) GetChaincodeDefinition (ctxt context.Context , txid string , signedProp * pb.SignedProposal , prop * pb.Proposal , chainID string , chaincodeID string ) (ccprovider.ChaincodeDefinition , error ) {
377
366
version := util .GetSysCCVersion ()
378
367
cccid := ccprovider .NewCCContext (chainID , "lscc" , version , txid , true , signedProp , prop )
379
- res , _ , err := cs .ExecuteChaincode (ctxt , cccid , [][]byte {[]byte ("getccdata" ), []byte (chainID ), []byte (chaincodeID )})
380
- if err == nil {
381
- if res .Status != shim .OK {
382
- return nil , errors .New (res .Message )
383
- }
384
- cd := & ccprovider.ChaincodeData {}
385
- err = proto .Unmarshal (res .Payload , cd )
386
- if err != nil {
387
- return nil , err
388
- }
389
- return cd , nil
368
+
369
+ args := util .ToChaincodeArgs ("getccdata" , chainID , chaincodeID )
370
+ res , _ , err := cs .ExecuteChaincode (ctxt , cccid , args )
371
+ if err != nil {
372
+ return nil , errors .Wrapf (err , "getccdata %s/%s" , chainID , chaincodeID )
373
+ }
374
+ if res .Status != shim .OK {
375
+ return nil , errors .Errorf ("getccdata %s/%s: %s" , chainID , chaincodeID , res .Message )
390
376
}
391
377
392
- return nil , err
378
+ cd := & ccprovider.ChaincodeData {}
379
+ err = proto .Unmarshal (res .Payload , cd )
380
+ if err != nil {
381
+ return nil , errors .Wrap (err , "failed to unmarshal chaincode definition" )
382
+ }
383
+
384
+ return cd , nil
393
385
}
394
386
395
- // ExecuteChaincode executes a given chaincode given chaincode name and arguments
387
+ // ExecuteChaincode invokes chaincode with the provided arguments.
396
388
func (cs * ChaincodeSupport ) ExecuteChaincode (ctxt context.Context , cccid * ccprovider.CCContext , args [][]byte ) (* pb.Response , * pb.ChaincodeEvent , error ) {
397
- var spec * pb.ChaincodeInvocationSpec
398
- var err error
399
- var res * pb.Response
400
- var ccevent * pb.ChaincodeEvent
389
+ invocationSpec := & pb.ChaincodeInvocationSpec {
390
+ ChaincodeSpec : & pb.ChaincodeSpec {
391
+ Type : pb .ChaincodeSpec_GOLANG ,
392
+ ChaincodeId : & pb.ChaincodeID {Name : cccid .Name },
393
+ Input : & pb.ChaincodeInput {Args : args },
394
+ },
395
+ }
401
396
402
- spec , err = createCIS (cccid .Name , args )
403
- res , ccevent , err = cs .ExecuteSpec (ctxt , cccid , spec )
397
+ res , ccevent , err := cs .ExecuteSpec (ctxt , cccid , invocationSpec )
404
398
if err != nil {
405
- err = errors .WithMessage (err , "error executing chaincode" )
399
+ err = errors .WithMessage (err , "error invoking chaincode" )
406
400
chaincodeLogger .Errorf ("%+v" , err )
407
401
return nil , nil , err
408
402
}
0 commit comments