@@ -38,12 +38,7 @@ import (
38
38
pb "github.com/hyperledger/fabric/protos/peer"
39
39
)
40
40
41
- // ChainName is the name of the chain to which this chaincode support belongs to.
42
- type ChainName string
43
-
44
41
const (
45
- // DefaultChain is the name of the default chain.
46
- DefaultChain ChainName = "default"
47
42
// DevModeUserRunsChaincode property allows user to run chaincode in development environment
48
43
DevModeUserRunsChaincode string = "dev"
49
44
chaincodeStartupTimeoutDefault int = 5000
@@ -54,13 +49,12 @@ const (
54
49
TXSimulatorKey string = "txsimulatorkey"
55
50
)
56
51
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
64
58
65
59
//use this for ledger access and make sure TXSimulator is being used
66
60
func getTxSimulator (context context.Context ) ledger.TxSimulator {
@@ -84,9 +78,8 @@ type runningChaincodes struct {
84
78
chaincodeMap map [string ]* chaincodeRTEnv
85
79
}
86
80
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
90
83
}
91
84
92
85
//call this under lock
@@ -106,48 +99,47 @@ func (chaincodeSupport *ChaincodeSupport) chaincodeHasBeenLaunched(chaincode str
106
99
}
107
100
108
101
// 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 {
110
103
pnid := viper .GetString ("peer.networkId" )
111
104
pid := viper .GetString ("peer.id" )
112
105
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 }
114
107
115
108
//initialize global chain
116
- chains [chainname ] = s
117
109
118
110
peerEndpoint , err := getPeerEndpoint ()
119
111
if err != nil {
120
112
chaincodeLogger .Errorf ("Error getting PeerEndpoint, using peer.address: %s" , err )
121
- s .peerAddress = viper .GetString ("peer.address" )
113
+ theChaincodeSupport .peerAddress = viper .GetString ("peer.address" )
122
114
} else {
123
- s .peerAddress = peerEndpoint .Address
115
+ theChaincodeSupport .peerAddress = peerEndpoint .Address
124
116
}
125
- chaincodeLogger .Infof ("Chaincode support using peerAddress: %s\n " , s .peerAddress )
117
+ chaincodeLogger .Infof ("Chaincode support using peerAddress: %s\n " , theChaincodeSupport .peerAddress )
126
118
//peerAddress = viper.GetString("peer.address")
127
- if s .peerAddress == "" {
128
- s .peerAddress = peerAddressDefault
119
+ if theChaincodeSupport .peerAddress == "" {
120
+ theChaincodeSupport .peerAddress = peerAddressDefault
129
121
}
130
122
131
- s .userRunsCC = userrunsCC
123
+ theChaincodeSupport .userRunsCC = userrunsCC
132
124
133
- s .ccStartupTimeout = ccstartuptimeout
125
+ theChaincodeSupport .ccStartupTimeout = ccstartuptimeout
134
126
135
127
//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
139
131
}
140
132
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" )
146
138
}
147
139
148
140
kadef := 0
149
141
if ka := viper .GetString ("chaincode.keepalive" ); ka == "" {
150
- s .keepalive = time .Duration (kadef ) * time .Second
142
+ theChaincodeSupport .keepalive = time .Duration (kadef ) * time .Second
151
143
} else {
152
144
t , terr := strconv .Atoi (ka )
153
145
if terr != nil {
@@ -157,7 +149,7 @@ func NewChaincodeSupport(chainname ChainName, getPeerEndpoint func() (*pb.PeerEn
157
149
chaincodeLogger .Debugf ("Turn off keepalive(value %s)" , ka )
158
150
t = kadef
159
151
}
160
- s .keepalive = time .Duration (t ) * time .Second
152
+ theChaincodeSupport .keepalive = time .Duration (t ) * time .Second
161
153
}
162
154
163
155
viper .SetEnvPrefix ("CORE" )
@@ -169,13 +161,13 @@ func NewChaincodeSupport(chainname ChainName, getPeerEndpoint func() (*pb.PeerEn
169
161
chaincodeLogLevel , err := logging .LogLevel (chaincodeLogLevelString )
170
162
171
163
if err == nil {
172
- s .chaincodeLogLevel = chaincodeLogLevel .String ()
164
+ theChaincodeSupport .chaincodeLogLevel = chaincodeLogLevel .String ()
173
165
} else {
174
166
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 ()
176
168
}
177
169
178
- return s
170
+ return theChaincodeSupport
179
171
}
180
172
181
173
// // ChaincodeStream standard stream for ChaincodeMessage type.
@@ -186,7 +178,6 @@ func NewChaincodeSupport(chainname ChainName, getPeerEndpoint func() (*pb.PeerEn
186
178
187
179
// ChaincodeSupport responsible for providing interfacing with chaincodes from the Peer.
188
180
type ChaincodeSupport struct {
189
- name ChainName
190
181
runningChaincodes * runningChaincodes
191
182
peerAddress string
192
183
ccStartupTimeout time.Duration
@@ -270,7 +261,7 @@ func (chaincodeSupport *ChaincodeSupport) deregisterHandler(chaincodehandler *Ha
270
261
}
271
262
272
263
// 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 {
274
265
chaincodeSupport .runningChaincodes .Lock ()
275
266
//if its in the map, there must be a connected stream...nothing to do
276
267
var chrte * chaincodeRTEnv
@@ -284,7 +275,7 @@ func (chaincodeSupport *ChaincodeSupport) sendInitOrReady(context context.Contex
284
275
285
276
var notfy chan * pb.ChaincodeMessage
286
277
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 {
288
279
return fmt .Errorf ("Error sending %s: %s" , pb .ChaincodeMessage_INIT , err )
289
280
}
290
281
if notfy != nil {
@@ -442,7 +433,7 @@ func (chaincodeSupport *ChaincodeSupport) Stop(context context.Context, cds *pb.
442
433
}
443
434
444
435
// 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 ) {
446
437
//build the chaincode
447
438
var cID * pb.ChaincodeID
448
439
var cMsg * pb.ChaincodeInput
@@ -499,7 +490,7 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, txid s
499
490
var depPayload []byte
500
491
501
492
//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 )
503
494
if err != nil {
504
495
return cID , cMsg , fmt .Errorf ("Could not get deployment transaction from LCCC for %s - %s" , chaincode , err )
505
496
}
@@ -530,7 +521,7 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, txid s
530
521
531
522
if err == nil {
532
523
//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 )
534
525
if err != nil {
535
526
chaincodeLogger .Errorf ("sending init failed(%s)" , err )
536
527
err = fmt .Errorf ("Failed to init chaincode(%s)" , err )
@@ -618,7 +609,7 @@ func createTransactionMessage(txid string, cMsg *pb.ChaincodeInput) (*pb.Chainco
618
609
}
619
610
620
611
// 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 ) {
622
613
chaincodeSupport .runningChaincodes .Lock ()
623
614
//we expect the chaincode to be running... sanity check
624
615
chrte , ok := chaincodeSupport .chaincodeHasBeenLaunched (chaincode )
@@ -631,7 +622,7 @@ func (chaincodeSupport *ChaincodeSupport) Execute(ctxt context.Context, chaincod
631
622
632
623
var notfy chan * pb.ChaincodeMessage
633
624
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 {
635
626
return nil , fmt .Errorf ("Error sending %s: %s" , msg .Type .String (), err )
636
627
}
637
628
var ccresp * pb.ChaincodeMessage
0 commit comments