@@ -38,12 +38,7 @@ import (
3838 pb "github.com/hyperledger/fabric/protos/peer"
3939)
4040
41- // ChainName is the name of the chain to which this chaincode support belongs to.
42- type ChainName string
43-
4441const (
45- // DefaultChain is the name of the default chain.
46- DefaultChain ChainName = "default"
4742 // DevModeUserRunsChaincode property allows user to run chaincode in development environment
4843 DevModeUserRunsChaincode string = "dev"
4944 chaincodeStartupTimeoutDefault int = 5000
@@ -54,13 +49,12 @@ const (
5449 TXSimulatorKey string = "txsimulatorkey"
5550)
5651
57- // chains is a map between different blockchains and their ChaincodeSupport.
58- //this needs to be a first class, top-level object... for now, lets just have a placeholder
59- var chains map [ChainName ]* ChaincodeSupport
60-
61- func init () {
62- chains = make (map [ChainName ]* ChaincodeSupport )
63- }
52+ //this is basically the singleton that supports the
53+ //entire chaincode framework. It does NOT know about
54+ //chains. Chains are per-proposal entities that are
55+ //setup as part of "join" and go through this object
56+ //via calls to Execute and Deploy chaincodes.
57+ var theChaincodeSupport * ChaincodeSupport
6458
6559//use this for ledger access and make sure TXSimulator is being used
6660func getTxSimulator (context context.Context ) ledger.TxSimulator {
@@ -84,9 +78,8 @@ type runningChaincodes struct {
8478 chaincodeMap map [string ]* chaincodeRTEnv
8579}
8680
87- // GetChain returns the chaincode support for a given chain
88- func GetChain (name ChainName ) * ChaincodeSupport {
89- return chains [name ]
81+ func GetChain () * ChaincodeSupport {
82+ return theChaincodeSupport
9083}
9184
9285//call this under lock
@@ -106,48 +99,47 @@ func (chaincodeSupport *ChaincodeSupport) chaincodeHasBeenLaunched(chaincode str
10699}
107100
108101// NewChaincodeSupport creates a new ChaincodeSupport instance
109- func NewChaincodeSupport (chainname ChainName , getPeerEndpoint func () (* pb.PeerEndpoint , error ), userrunsCC bool , ccstartuptimeout time.Duration ) * ChaincodeSupport {
102+ func NewChaincodeSupport (getPeerEndpoint func () (* pb.PeerEndpoint , error ), userrunsCC bool , ccstartuptimeout time.Duration ) * ChaincodeSupport {
110103 pnid := viper .GetString ("peer.networkId" )
111104 pid := viper .GetString ("peer.id" )
112105
113- s : = & ChaincodeSupport {name : chainname , runningChaincodes : & runningChaincodes {chaincodeMap : make (map [string ]* chaincodeRTEnv )}, peerNetworkID : pnid , peerID : pid }
106+ theChaincodeSupport = & ChaincodeSupport {runningChaincodes : & runningChaincodes {chaincodeMap : make (map [string ]* chaincodeRTEnv )}, peerNetworkID : pnid , peerID : pid }
114107
115108 //initialize global chain
116- chains [chainname ] = s
117109
118110 peerEndpoint , err := getPeerEndpoint ()
119111 if err != nil {
120112 chaincodeLogger .Errorf ("Error getting PeerEndpoint, using peer.address: %s" , err )
121- s .peerAddress = viper .GetString ("peer.address" )
113+ theChaincodeSupport .peerAddress = viper .GetString ("peer.address" )
122114 } else {
123- s .peerAddress = peerEndpoint .Address
115+ theChaincodeSupport .peerAddress = peerEndpoint .Address
124116 }
125- chaincodeLogger .Infof ("Chaincode support using peerAddress: %s\n " , s .peerAddress )
117+ chaincodeLogger .Infof ("Chaincode support using peerAddress: %s\n " , theChaincodeSupport .peerAddress )
126118 //peerAddress = viper.GetString("peer.address")
127- if s .peerAddress == "" {
128- s .peerAddress = peerAddressDefault
119+ if theChaincodeSupport .peerAddress == "" {
120+ theChaincodeSupport .peerAddress = peerAddressDefault
129121 }
130122
131- s .userRunsCC = userrunsCC
123+ theChaincodeSupport .userRunsCC = userrunsCC
132124
133- s .ccStartupTimeout = ccstartuptimeout
125+ theChaincodeSupport .ccStartupTimeout = ccstartuptimeout
134126
135127 //TODO I'm not sure if this needs to be on a per chain basis... too lowel and just needs to be a global default ?
136- s .chaincodeInstallPath = viper .GetString ("chaincode.installpath" )
137- if s .chaincodeInstallPath == "" {
138- s .chaincodeInstallPath = chaincodeInstallPathDefault
128+ theChaincodeSupport .chaincodeInstallPath = viper .GetString ("chaincode.installpath" )
129+ if theChaincodeSupport .chaincodeInstallPath == "" {
130+ theChaincodeSupport .chaincodeInstallPath = chaincodeInstallPathDefault
139131 }
140132
141- s .peerTLS = viper .GetBool ("peer.tls.enabled" )
142- if s .peerTLS {
143- s .peerTLSCertFile = viper .GetString ("peer.tls.cert.file" )
144- s .peerTLSKeyFile = viper .GetString ("peer.tls.key.file" )
145- s .peerTLSSvrHostOrd = viper .GetString ("peer.tls.serverhostoverride" )
133+ theChaincodeSupport .peerTLS = viper .GetBool ("peer.tls.enabled" )
134+ if theChaincodeSupport .peerTLS {
135+ theChaincodeSupport .peerTLSCertFile = viper .GetString ("peer.tls.cert.file" )
136+ theChaincodeSupport .peerTLSKeyFile = viper .GetString ("peer.tls.key.file" )
137+ theChaincodeSupport .peerTLSSvrHostOrd = viper .GetString ("peer.tls.serverhostoverride" )
146138 }
147139
148140 kadef := 0
149141 if ka := viper .GetString ("chaincode.keepalive" ); ka == "" {
150- s .keepalive = time .Duration (kadef ) * time .Second
142+ theChaincodeSupport .keepalive = time .Duration (kadef ) * time .Second
151143 } else {
152144 t , terr := strconv .Atoi (ka )
153145 if terr != nil {
@@ -157,7 +149,7 @@ func NewChaincodeSupport(chainname ChainName, getPeerEndpoint func() (*pb.PeerEn
157149 chaincodeLogger .Debugf ("Turn off keepalive(value %s)" , ka )
158150 t = kadef
159151 }
160- s .keepalive = time .Duration (t ) * time .Second
152+ theChaincodeSupport .keepalive = time .Duration (t ) * time .Second
161153 }
162154
163155 viper .SetEnvPrefix ("CORE" )
@@ -169,13 +161,13 @@ func NewChaincodeSupport(chainname ChainName, getPeerEndpoint func() (*pb.PeerEn
169161 chaincodeLogLevel , err := logging .LogLevel (chaincodeLogLevelString )
170162
171163 if err == nil {
172- s .chaincodeLogLevel = chaincodeLogLevel .String ()
164+ theChaincodeSupport .chaincodeLogLevel = chaincodeLogLevel .String ()
173165 } else {
174166 chaincodeLogger .Warningf ("Chaincode logging level %s is invalid; defaulting to %s" , chaincodeLogLevelString , flogging .DefaultLoggingLevel ().String ())
175- s .chaincodeLogLevel = flogging .DefaultLoggingLevel ().String ()
167+ theChaincodeSupport .chaincodeLogLevel = flogging .DefaultLoggingLevel ().String ()
176168 }
177169
178- return s
170+ return theChaincodeSupport
179171}
180172
181173// // ChaincodeStream standard stream for ChaincodeMessage type.
@@ -186,7 +178,6 @@ func NewChaincodeSupport(chainname ChainName, getPeerEndpoint func() (*pb.PeerEn
186178
187179// ChaincodeSupport responsible for providing interfacing with chaincodes from the Peer.
188180type ChaincodeSupport struct {
189- name ChainName
190181 runningChaincodes * runningChaincodes
191182 peerAddress string
192183 ccStartupTimeout time.Duration
@@ -270,7 +261,7 @@ func (chaincodeSupport *ChaincodeSupport) deregisterHandler(chaincodehandler *Ha
270261}
271262
272263// Based on state of chaincode send either init or ready to move to ready state
273- func (chaincodeSupport * ChaincodeSupport ) sendInitOrReady (context context.Context , txid string , prop * pb.Proposal , chaincode string , initArgs [][]byte , timeout time.Duration ) error {
264+ func (chaincodeSupport * ChaincodeSupport ) sendInitOrReady (context context.Context , chainID string , txid string , prop * pb.Proposal , chaincode string , initArgs [][]byte , timeout time.Duration ) error {
274265 chaincodeSupport .runningChaincodes .Lock ()
275266 //if its in the map, there must be a connected stream...nothing to do
276267 var chrte * chaincodeRTEnv
@@ -284,7 +275,7 @@ func (chaincodeSupport *ChaincodeSupport) sendInitOrReady(context context.Contex
284275
285276 var notfy chan * pb.ChaincodeMessage
286277 var err error
287- if notfy , err = chrte .handler .initOrReady (context , txid , prop , initArgs ); err != nil {
278+ if notfy , err = chrte .handler .initOrReady (context , chainID , txid , prop , initArgs ); err != nil {
288279 return fmt .Errorf ("Error sending %s: %s" , pb .ChaincodeMessage_INIT , err )
289280 }
290281 if notfy != nil {
@@ -442,7 +433,7 @@ func (chaincodeSupport *ChaincodeSupport) Stop(context context.Context, cds *pb.
442433}
443434
444435// Launch will launch the chaincode if not running (if running return nil) and will wait for handler of the chaincode to get into FSM ready state.
445- func (chaincodeSupport * ChaincodeSupport ) Launch (context context.Context , txid string , prop * pb.Proposal , spec interface {}) (* pb.ChaincodeID , * pb.ChaincodeInput , error ) {
436+ func (chaincodeSupport * ChaincodeSupport ) Launch (context context.Context , chainID string , txid string , prop * pb.Proposal , spec interface {}) (* pb.ChaincodeID , * pb.ChaincodeInput , error ) {
446437 //build the chaincode
447438 var cID * pb.ChaincodeID
448439 var cMsg * pb.ChaincodeInput
@@ -499,7 +490,7 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, txid s
499490 var depPayload []byte
500491
501492 //hopefully we are restarting from existing image and the deployed transaction exists
502- depPayload , err = GetCDSFromLCCC (context , txid , prop , string ( DefaultChain ) , chaincode )
493+ depPayload , err = GetCDSFromLCCC (context , txid , prop , chainID , chaincode )
503494 if err != nil {
504495 return cID , cMsg , fmt .Errorf ("Could not get deployment transaction from LCCC for %s - %s" , chaincode , err )
505496 }
@@ -530,7 +521,7 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, txid s
530521
531522 if err == nil {
532523 //send init (if (args)) and wait for ready state
533- err = chaincodeSupport .sendInitOrReady (context , txid , prop , chaincode , initargs , chaincodeSupport .ccStartupTimeout )
524+ err = chaincodeSupport .sendInitOrReady (context , chainID , txid , prop , chaincode , initargs , chaincodeSupport .ccStartupTimeout )
534525 if err != nil {
535526 chaincodeLogger .Errorf ("sending init failed(%s)" , err )
536527 err = fmt .Errorf ("Failed to init chaincode(%s)" , err )
@@ -618,7 +609,7 @@ func createTransactionMessage(txid string, cMsg *pb.ChaincodeInput) (*pb.Chainco
618609}
619610
620611// Execute executes a transaction and waits for it to complete until a timeout value.
621- func (chaincodeSupport * ChaincodeSupport ) Execute (ctxt context.Context , chaincode string , msg * pb.ChaincodeMessage , timeout time.Duration , prop * pb.Proposal ) (* pb.ChaincodeMessage , error ) {
612+ func (chaincodeSupport * ChaincodeSupport ) Execute (ctxt context.Context , chainID string , chaincode string , msg * pb.ChaincodeMessage , timeout time.Duration , prop * pb.Proposal ) (* pb.ChaincodeMessage , error ) {
622613 chaincodeSupport .runningChaincodes .Lock ()
623614 //we expect the chaincode to be running... sanity check
624615 chrte , ok := chaincodeSupport .chaincodeHasBeenLaunched (chaincode )
@@ -631,7 +622,7 @@ func (chaincodeSupport *ChaincodeSupport) Execute(ctxt context.Context, chaincod
631622
632623 var notfy chan * pb.ChaincodeMessage
633624 var err error
634- if notfy , err = chrte .handler .sendExecuteMessage (ctxt , msg , prop ); err != nil {
625+ if notfy , err = chrte .handler .sendExecuteMessage (ctxt , chainID , msg , prop ); err != nil {
635626 return nil , fmt .Errorf ("Error sending %s: %s" , msg .Type .String (), err )
636627 }
637628 var ccresp * pb.ChaincodeMessage
0 commit comments