@@ -57,9 +57,6 @@ const (
57
57
defaultChaincodePort = 7052
58
58
)
59
59
60
- //function used by chaincode support
61
- type ccEndpointFunc func () (* pb.PeerEndpoint , error )
62
-
63
60
var chaincodeDevMode bool
64
61
var orderingEndpoint string
65
62
@@ -161,8 +158,11 @@ func serve(args []string) error {
161
158
if err != nil {
162
159
logger .Panic ("Failed creating authentication layer:" , err )
163
160
}
164
- ccSrv , ccEpFunc := createChaincodeServer (ca .CertBytes (), peerHost )
165
- registerChaincodeSupport (ccSrv , ccEpFunc , ca )
161
+ ccSrv , ccEndpoint , err := createChaincodeServer (ca .CertBytes (), peerHost )
162
+ if err != nil {
163
+ logger .Panicf ("Failed to create chaincode server: %s" , err )
164
+ }
165
+ registerChaincodeSupport (ccSrv , ccEndpoint , ca )
166
166
go ccSrv .Start ()
167
167
168
168
logger .Debugf ("Running peer" )
@@ -303,20 +303,32 @@ func serve(args []string) error {
303
303
}
304
304
305
305
//create a CC listener using peer.chaincodeListenAddress (and if that's not set use peer.peerAddress)
306
- func createChaincodeServer (caCert []byte , peerHostname string ) (comm.GRPCServer , ccEndpointFunc ) {
306
+ func createChaincodeServer (caCert []byte , peerHostname string ) (srv comm.GRPCServer , ccEndpoint string , err error ) {
307
+ // before potentially setting chaincodeListenAddress, compute chaincode endpoint at first
308
+ ccEndpoint , err = computeChaincodeEndpoint (peerHostname )
309
+ if err != nil {
310
+ if chaincode .IsDevMode () {
311
+ // if any error for dev mode, we use 0.0.0.0:7052
312
+ ccEndpoint = fmt .Sprintf ("%s:%d" , "0.0.0.0" , defaultChaincodePort )
313
+ logger .Warningf ("use %s as chaincode endpoint because of error in computeChaincodeEndpoint: %s" , ccEndpoint , err )
314
+ } else {
315
+ // for non-dev mode, we have to return error
316
+ logger .Errorf ("Error computing chaincode endpoint: %s" , err )
317
+ return nil , "" , err
318
+ }
319
+ }
320
+
307
321
cclistenAddress := viper .GetString (chaincodeListenAddrKey )
308
322
if cclistenAddress == "" {
309
323
cclistenAddress = fmt .Sprintf ("%s:%d" , peerHostname , defaultChaincodePort )
310
324
logger .Warningf ("%s is not set, using %s" , chaincodeListenAddrKey , cclistenAddress )
311
325
viper .Set (chaincodeListenAddrKey , cclistenAddress )
312
326
}
313
327
314
- var srv comm.GRPCServer
315
- var ccEpFunc ccEndpointFunc
316
-
317
328
config , err := peer .GetServerConfig ()
318
329
if err != nil {
319
- panic (err )
330
+ logger .Errorf ("Error getting server config: %s" , err )
331
+ return nil , "" , err
320
332
}
321
333
322
334
if config .SecOpts .UseTLS {
@@ -334,32 +346,17 @@ func createChaincodeServer(caCert []byte, peerHostname string) (comm.GRPCServer,
334
346
335
347
srv , err = comm .NewGRPCServer (cclistenAddress , config )
336
348
if err != nil {
337
- panic (err )
338
- }
339
-
340
- ccEpFunc = func () (* pb.PeerEndpoint , error ) {
341
- //need this for the ID to create chaincode endpoint
342
- peerEndpoint , err := peer .GetPeerEndpoint ()
343
- if err != nil {
344
- return nil , err
345
- }
346
-
347
- ccEndpoint , err := computeChaincodeEndpoint (peerHostname )
348
- if err != nil {
349
- return nil , err
350
- }
351
-
352
- return & pb.PeerEndpoint {
353
- Id : peerEndpoint .Id ,
354
- Address : ccEndpoint ,
355
- }, nil
349
+ logger .Errorf ("Error creating GRPC server: %s" , err )
350
+ return nil , "" , err
356
351
}
357
352
358
- return srv , ccEpFunc
353
+ return srv , ccEndpoint , nil
359
354
}
360
355
356
+ // computeChaincodeEndpoint will utilize chaincode address, chaincode listen
357
+ // address (these two are from viper) and peer address to compute chaincode endpoint.
361
358
// There could be following cases of computing chaincode endpoint:
362
- // Case A: if chaincodeAddrKey is set, use it
359
+ // Case A: if chaincodeAddrKey is set, use it if not "0.0.0.0" (or "::")
363
360
// Case B: else if chaincodeListenAddrKey is set and not "0.0.0.0" or ("::"), use it
364
361
// Case C: else use peer address if not "0.0.0.0" (or "::")
365
362
// Case D: else return error
@@ -381,15 +378,14 @@ func computeChaincodeEndpoint(peerHostname string) (ccEndpoint string, err error
381
378
logger .Errorf ("ChaincodeAddress and chaincodeListenAddress are nil and peerIP is %s" , peerIp )
382
379
return "" , errors .New ("invalid endpoint for chaincode to connect" )
383
380
}
381
+
384
382
// use peerAddress:defaultChaincodePort
385
383
ccEndpoint = fmt .Sprintf ("%s:%d" , peerHostname , defaultChaincodePort )
386
384
387
385
} else {
388
386
// Case B: chaincodeListenAddrKey is set
389
387
host , port , err := net .SplitHostPort (ccEndpoint )
390
388
if err != nil {
391
- // and the listener was brought up above...
392
- // so this should really not happen.. just a paranoid check
393
389
logger .Errorf ("ChaincodeAddress is nil and fail to split chaincodeListenAddress: %s" , err )
394
390
return "" , err
395
391
}
@@ -407,13 +403,20 @@ func computeChaincodeEndpoint(peerHostname string) (ccEndpoint string, err error
407
403
}
408
404
ccEndpoint = fmt .Sprintf ("%s:%s" , peerHostname , port )
409
405
}
406
+
410
407
}
411
408
412
409
} else {
413
410
// Case A: the chaincodeAddrKey is set
414
- if _ , _ , err = net .SplitHostPort (ccEndpoint ); err != nil {
411
+ if host , _ , err : = net .SplitHostPort (ccEndpoint ); err != nil {
415
412
logger .Errorf ("Fail to split chaincodeAddress: %s" , err )
416
413
return "" , err
414
+ } else {
415
+ ccIP := net .ParseIP (host )
416
+ if ccIP != nil && ccIP .IsUnspecified () {
417
+ logger .Errorf ("ChaincodeAddress' IP cannot be %s in non-dev mode" , ccIP )
418
+ return "" , errors .New ("invalid endpoint for chaincode to connect" )
419
+ }
417
420
}
418
421
}
419
422
@@ -424,7 +427,7 @@ func computeChaincodeEndpoint(peerHostname string) (ccEndpoint string, err error
424
427
//NOTE - when we implement JOIN we will no longer pass the chainID as param
425
428
//The chaincode support will come up without registering system chaincodes
426
429
//which will be registered only during join phase.
427
- func registerChaincodeSupport (grpcServer comm.GRPCServer , ccEpFunc ccEndpointFunc , ca accesscontrol.CA ) {
430
+ func registerChaincodeSupport (grpcServer comm.GRPCServer , ccEndpoint string , ca accesscontrol.CA ) {
428
431
//get user mode
429
432
userRunsCC := chaincode .IsDevMode ()
430
433
@@ -437,7 +440,7 @@ func registerChaincodeSupport(grpcServer comm.GRPCServer, ccEpFunc ccEndpointFun
437
440
logger .Debugf ("Chaincode startup timeout value set to %s" , ccStartupTimeout )
438
441
}
439
442
440
- ccSrv := chaincode .NewChaincodeSupport (ccEpFunc , userRunsCC , ccStartupTimeout , ca )
443
+ ccSrv := chaincode .NewChaincodeSupport (ccEndpoint , userRunsCC , ccStartupTimeout , ca )
441
444
442
445
//Now that chaincode is initialized, register all system chaincodes.
443
446
scc .RegisterSysCCs ()
0 commit comments