@@ -19,6 +19,7 @@ package chaincode
1919import (
2020 "fmt"
2121 "io"
22+ "path/filepath"
2223 "strconv"
2324 "sync"
2425 "time"
@@ -105,6 +106,8 @@ func (chaincodeSupport *ChaincodeSupport) chaincodeHasBeenLaunched(chaincode str
105106
106107// NewChaincodeSupport creates a new ChaincodeSupport instance
107108func NewChaincodeSupport (getPeerEndpoint func () (* pb.PeerEndpoint , error ), userrunsCC bool , ccstartuptimeout time.Duration ) * ChaincodeSupport {
109+ ccprovider .SetChaincodesPath (viper .GetString ("peer.fileSystemPath" ) + string (filepath .Separator ) + "chaincodes" )
110+
108111 pnid := viper .GetString ("peer.networkId" )
109112 pid := viper .GetString ("peer.id" )
110113
@@ -265,8 +268,8 @@ func (chaincodeSupport *ChaincodeSupport) deregisterHandler(chaincodehandler *Ha
265268 return nil
266269}
267270
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 {
270273 canName := cccid .GetCanonicalName ()
271274 chaincodeSupport .runningChaincodes .Lock ()
272275 //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
281284
282285 var notfy chan * pb.ChaincodeMessage
283286 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 {
285288 return fmt .Errorf ("Error sending %s: %s" , pb .ChaincodeMessage_INIT , err )
286289 }
287290 if notfy != nil {
@@ -394,7 +397,7 @@ func (chaincodeSupport *ChaincodeSupport) launchAndWaitForRegister(ctxt context.
394397
395398 vmtype , _ := chaincodeSupport .getVMType (cds )
396399
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 }
398401
399402 ipcCtxt := context .WithValue (ctxt , ccintf .GetCCHandlerKey (), chaincodeSupport )
400403
@@ -437,7 +440,7 @@ func (chaincodeSupport *ChaincodeSupport) Stop(context context.Context, cccid *c
437440 }
438441
439442 //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 }
441444 // The line below is left for debugging. It replaces the line above to keep
442445 // the chaincode container around to give you a chance to get data
443446 //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
470473 var cID * pb.ChaincodeID
471474 var cMsg * pb.ChaincodeInput
472475 var cLang pb.ChaincodeSpec_Type
473- var initargs [][]byte
474476
475477 var cds * pb.ChaincodeDeploymentSpec
476478 var ci * pb.ChaincodeInvocationSpec
@@ -483,7 +485,6 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, cccid
483485 cID = cds .ChaincodeSpec .ChaincodeID
484486 cMsg = cds .ChaincodeSpec .Input
485487 cLang = cds .ChaincodeSpec .Type
486- initargs = cMsg .Args
487488 } else {
488489 cID = ci .ChaincodeSpec .ChaincodeID
489490 cMsg = ci .ChaincodeSpec .Input
@@ -548,6 +549,20 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, cccid
548549
549550 //launch container if it is a System container or not in dev mode
550551 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+ }
551566
552567 builder := func () (io.Reader , error ) { return platforms .GenerateDockerBuild (cds ) }
553568 err = chaincodeSupport .launchAndWaitForRegister (context , cccid , cds , cLang , builder )
@@ -558,8 +573,8 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, cccid
558573 }
559574
560575 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 )
563578 if err != nil {
564579 chaincodeLogger .Errorf ("sending init failed(%s)" , err )
565580 err = fmt .Errorf ("Failed to init chaincode(%s)" , err )
@@ -585,50 +600,6 @@ func (chaincodeSupport *ChaincodeSupport) getVMType(cds *pb.ChaincodeDeploymentS
585600 return container .DOCKER , nil
586601}
587602
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-
632603// HandleChaincodeStream implements ccintf.HandleChaincodeStream for all vms to call with appropriate stream
633604func (chaincodeSupport * ChaincodeSupport ) HandleChaincodeStream (ctxt context.Context , stream ccintf.ChaincodeStream ) error {
634605 return HandleChaincodeStream (chaincodeSupport , ctxt , stream )
@@ -639,14 +610,14 @@ func (chaincodeSupport *ChaincodeSupport) Register(stream pb.ChaincodeSupport_Re
639610 return chaincodeSupport .HandleChaincodeStream (stream .Context (), stream )
640611}
641612
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 ) {
644615 payload , err := proto .Marshal (cMsg )
645616 if err != nil {
646617 fmt .Printf (err .Error ())
647618 return nil , err
648619 }
649- return & pb.ChaincodeMessage {Type : pb . ChaincodeMessage_TRANSACTION , Payload : payload , Txid : txid }, nil
620+ return & pb.ChaincodeMessage {Type : typ , Payload : payload , Txid : txid }, nil
650621}
651622
652623// Execute executes a transaction and waits for it to complete until a timeout value.
0 commit comments