@@ -19,6 +19,7 @@ package chaincode
19
19
import (
20
20
"fmt"
21
21
"io"
22
+ "path/filepath"
22
23
"strconv"
23
24
"sync"
24
25
"time"
@@ -105,6 +106,8 @@ func (chaincodeSupport *ChaincodeSupport) chaincodeHasBeenLaunched(chaincode str
105
106
106
107
// NewChaincodeSupport creates a new ChaincodeSupport instance
107
108
func NewChaincodeSupport (getPeerEndpoint func () (* pb.PeerEndpoint , error ), userrunsCC bool , ccstartuptimeout time.Duration ) * ChaincodeSupport {
109
+ ccprovider .SetChaincodesPath (viper .GetString ("peer.fileSystemPath" ) + string (filepath .Separator ) + "chaincodes" )
110
+
108
111
pnid := viper .GetString ("peer.networkId" )
109
112
pid := viper .GetString ("peer.id" )
110
113
@@ -265,8 +268,8 @@ func (chaincodeSupport *ChaincodeSupport) deregisterHandler(chaincodehandler *Ha
265
268
return nil
266
269
}
267
270
268
- // Based on state of chaincode send either init or ready to move to ready state
269
- func (chaincodeSupport * ChaincodeSupport ) sendInitOrReady (context context.Context , cccid * ccprovider.CCContext , initArgs [][] byte , timeout time.Duration ) error {
271
+ // send ready to move to ready state
272
+ func (chaincodeSupport * ChaincodeSupport ) sendReady (context context.Context , cccid * ccprovider.CCContext , timeout time.Duration ) error {
270
273
canName := cccid .GetCanonicalName ()
271
274
chaincodeSupport .runningChaincodes .Lock ()
272
275
//if its in the map, there must be a connected stream...nothing to do
@@ -281,7 +284,7 @@ func (chaincodeSupport *ChaincodeSupport) sendInitOrReady(context context.Contex
281
284
282
285
var notfy chan * pb.ChaincodeMessage
283
286
var err error
284
- if notfy , err = chrte .handler .initOrReady (context , cccid .ChainID , cccid .TxID , cccid .Proposal , initArgs ); err != nil {
287
+ if notfy , err = chrte .handler .ready (context , cccid .ChainID , cccid .TxID , cccid .Proposal ); err != nil {
285
288
return fmt .Errorf ("Error sending %s: %s" , pb .ChaincodeMessage_INIT , err )
286
289
}
287
290
if notfy != nil {
@@ -394,7 +397,7 @@ func (chaincodeSupport *ChaincodeSupport) launchAndWaitForRegister(ctxt context.
394
397
395
398
vmtype , _ := chaincodeSupport .getVMType (cds )
396
399
397
- sir := container.StartImageReq {CCID : ccintf.CCID {ChaincodeSpec : cds .ChaincodeSpec , NetworkID : chaincodeSupport .peerNetworkID , PeerID : chaincodeSupport .peerID , ChainID : cccid . ChainID , Version : cccid .Version }, Builder : builder , Args : args , Env : env }
400
+ sir := container.StartImageReq {CCID : ccintf.CCID {ChaincodeSpec : cds .ChaincodeSpec , NetworkID : chaincodeSupport .peerNetworkID , PeerID : chaincodeSupport .peerID , Version : cccid .Version }, Builder : builder , Args : args , Env : env }
398
401
399
402
ipcCtxt := context .WithValue (ctxt , ccintf .GetCCHandlerKey (), chaincodeSupport )
400
403
@@ -437,7 +440,7 @@ func (chaincodeSupport *ChaincodeSupport) Stop(context context.Context, cccid *c
437
440
}
438
441
439
442
//stop the chaincode
440
- sir := container.StopImageReq {CCID : ccintf.CCID {ChaincodeSpec : cds .ChaincodeSpec , NetworkID : chaincodeSupport .peerNetworkID , PeerID : chaincodeSupport .peerID , ChainID : cccid . ChainID , Version : cccid .Version }, Timeout : 0 }
443
+ sir := container.StopImageReq {CCID : ccintf.CCID {ChaincodeSpec : cds .ChaincodeSpec , NetworkID : chaincodeSupport .peerNetworkID , PeerID : chaincodeSupport .peerID , Version : cccid .Version }, Timeout : 0 }
441
444
// The line below is left for debugging. It replaces the line above to keep
442
445
// the chaincode container around to give you a chance to get data
443
446
//sir := container.StopImageReq{CCID: ccintf.CCID{ChaincodeSpec: cds.ChaincodeSpec, NetworkID: chaincodeSupport.peerNetworkID, PeerID: chaincodeSupport.peerID, ChainID: cccid.ChainID, Version: cccid.Version}, Timeout: 0, Dontremove: true}
@@ -470,7 +473,6 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, cccid
470
473
var cID * pb.ChaincodeID
471
474
var cMsg * pb.ChaincodeInput
472
475
var cLang pb.ChaincodeSpec_Type
473
- var initargs [][]byte
474
476
475
477
var cds * pb.ChaincodeDeploymentSpec
476
478
var ci * pb.ChaincodeInvocationSpec
@@ -483,7 +485,6 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, cccid
483
485
cID = cds .ChaincodeSpec .ChaincodeID
484
486
cMsg = cds .ChaincodeSpec .Input
485
487
cLang = cds .ChaincodeSpec .Type
486
- initargs = cMsg .Args
487
488
} else {
488
489
cID = ci .ChaincodeSpec .ChaincodeID
489
490
cMsg = ci .ChaincodeSpec .Input
@@ -548,6 +549,20 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, cccid
548
549
549
550
//launch container if it is a System container or not in dev mode
550
551
if (! chaincodeSupport .userRunsCC || cds .ExecEnv == pb .ChaincodeDeploymentSpec_SYSTEM ) && (chrte == nil || chrte .handler == nil ) {
552
+ //whether we deploying, upgrading or launching a chaincode we now have a
553
+ //deployment package. If lauching, we got it from LCCC and has gone through
554
+ //ccprovider.GetChaincodeFromFS
555
+ if cds .CodePackage == nil {
556
+ //no code bytes for these situations
557
+ if ! (chaincodeSupport .userRunsCC || cds .ExecEnv == pb .ChaincodeDeploymentSpec_SYSTEM ) {
558
+ _ , cdsfs , err := ccprovider .GetChaincodeFromFS (cID .Name , cID .Version )
559
+ if err != nil {
560
+ return cID , cMsg , err
561
+ }
562
+ cds .CodePackage = cdsfs .CodePackage
563
+ chaincodeLogger .Debugf ("launchAndWaitForRegister fetched %d from file system" , len (cds .CodePackage ), err )
564
+ }
565
+ }
551
566
552
567
builder := func () (io.Reader , error ) { return platforms .GenerateDockerBuild (cds ) }
553
568
err = chaincodeSupport .launchAndWaitForRegister (context , cccid , cds , cLang , builder )
@@ -558,8 +573,8 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, cccid
558
573
}
559
574
560
575
if err == nil {
561
- //send init (if (args)) and wait for ready state
562
- err = chaincodeSupport .sendInitOrReady (context , cccid , initargs , chaincodeSupport .ccStartupTimeout )
576
+ //launch will set the chaincode in Ready state
577
+ err = chaincodeSupport .sendReady (context , cccid , chaincodeSupport .ccStartupTimeout )
563
578
if err != nil {
564
579
chaincodeLogger .Errorf ("sending init failed(%s)" , err )
565
580
err = fmt .Errorf ("Failed to init chaincode(%s)" , err )
@@ -585,50 +600,6 @@ func (chaincodeSupport *ChaincodeSupport) getVMType(cds *pb.ChaincodeDeploymentS
585
600
return container .DOCKER , nil
586
601
}
587
602
588
- // Deploy deploys the chaincode if not in development mode where user is running the chaincode.
589
- func (chaincodeSupport * ChaincodeSupport ) Deploy (context context.Context , cccid * ccprovider.CCContext , cds * pb.ChaincodeDeploymentSpec ) (* pb.ChaincodeDeploymentSpec , error ) {
590
- cLang := cds .ChaincodeSpec .Type
591
- canName := cccid .GetCanonicalName ()
592
-
593
- if chaincodeSupport .userRunsCC {
594
- chaincodeLogger .Debug ("user runs chaincode, not deploying chaincode" )
595
- return nil , nil
596
- }
597
-
598
- chaincodeSupport .runningChaincodes .Lock ()
599
- //if its in the map, there must be a connected stream...and we are trying to build the code ?!
600
- if _ , ok := chaincodeSupport .chaincodeHasBeenLaunched (canName ); ok {
601
- chaincodeLogger .Debugf ("deploy ?!! there's a chaincode with that name running: %s" , canName )
602
- chaincodeSupport .runningChaincodes .Unlock ()
603
- return cds , fmt .Errorf ("deploy attempted but a chaincode with same name running %s" , canName )
604
- }
605
- chaincodeSupport .runningChaincodes .Unlock ()
606
-
607
- args , envs , err := chaincodeSupport .getArgsAndEnv (cccid , cLang )
608
- if err != nil {
609
- return cds , fmt .Errorf ("error getting args for chaincode %s" , err )
610
- }
611
-
612
- targz , err := platforms .GenerateDockerBuild (cds )
613
- if err != nil {
614
- return cds , fmt .Errorf ("error converting CodePackage to Docker: %s" , err )
615
- }
616
-
617
- cir := & container.CreateImageReq {CCID : ccintf.CCID {ChaincodeSpec : cds .ChaincodeSpec , NetworkID : chaincodeSupport .peerNetworkID , PeerID : chaincodeSupport .peerID , ChainID : cccid .ChainID , Version : cccid .Version }, Args : args , Reader : targz , Env : envs }
618
-
619
- vmtype , _ := chaincodeSupport .getVMType (cds )
620
-
621
- chaincodeLogger .Debugf ("deploying chaincode %s(networkid:%s,peerid:%s)" , canName , chaincodeSupport .peerNetworkID , chaincodeSupport .peerID )
622
-
623
- //create image and create container
624
- resp , err2 := container .VMCProcess (context , vmtype , cir )
625
- if err2 != nil || (resp != nil && resp .(container.VMCResp ).Err != nil ) {
626
- err = fmt .Errorf ("Error creating image: %s" , err2 )
627
- }
628
-
629
- return cds , err
630
- }
631
-
632
603
// HandleChaincodeStream implements ccintf.HandleChaincodeStream for all vms to call with appropriate stream
633
604
func (chaincodeSupport * ChaincodeSupport ) HandleChaincodeStream (ctxt context.Context , stream ccintf.ChaincodeStream ) error {
634
605
return HandleChaincodeStream (chaincodeSupport , ctxt , stream )
@@ -639,14 +610,14 @@ func (chaincodeSupport *ChaincodeSupport) Register(stream pb.ChaincodeSupport_Re
639
610
return chaincodeSupport .HandleChaincodeStream (stream .Context (), stream )
640
611
}
641
612
642
- // createTransactionMessage creates a transaction message.
643
- func createTransactionMessage ( txid string , cMsg * pb.ChaincodeInput ) (* pb.ChaincodeMessage , error ) {
613
+ // createCCMessage creates a transaction message.
614
+ func createCCMessage ( typ pb. ChaincodeMessage_Type , txid string , cMsg * pb.ChaincodeInput ) (* pb.ChaincodeMessage , error ) {
644
615
payload , err := proto .Marshal (cMsg )
645
616
if err != nil {
646
617
fmt .Printf (err .Error ())
647
618
return nil , err
648
619
}
649
- return & pb.ChaincodeMessage {Type : pb . ChaincodeMessage_TRANSACTION , Payload : payload , Txid : txid }, nil
620
+ return & pb.ChaincodeMessage {Type : typ , Payload : payload , Txid : txid }, nil
650
621
}
651
622
652
623
// Execute executes a transaction and waits for it to complete until a timeout value.
0 commit comments