Skip to content

Commit 8028eb4

Browse files
committed
[FAB-7108] Refactor ccEpFunc to string
The main purpose of function ccEpFunc is to compute chaincode endpoint and return error if needed. It will be better to do all checks and computation during the node start up phase. The CR will do all the necessary checks in node/start.go and panic if unexpected error. Therefore, it will pass only the chaincode endpoint value (as string) to the follow up function. Also, the CR moves panic from createChaincodeServer to serve function and revises UT to cover all chaincode endpoint computation cases. Change-Id: Ie4802472f0dd04c3a3da1e1ac6e30f90463fb0b2 Signed-off-by: Boliang Chen <cblsjtu@gmail.com>
1 parent 6b41e7e commit 8028eb4

File tree

7 files changed

+101
-184
lines changed

7 files changed

+101
-184
lines changed

core/chaincode/chaincode_support.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (chaincodeSupport *ChaincodeSupport) launchStarted(chaincode string) bool {
135135
}
136136

137137
// NewChaincodeSupport creates a new ChaincodeSupport instance
138-
func NewChaincodeSupport(getCCEndpoint func() (*pb.PeerEndpoint, error), userrunsCC bool, ccstartuptimeout time.Duration, ca accesscontrol.CA) pb.ChaincodeSupportServer {
138+
func NewChaincodeSupport(ccEndpoint string, userrunsCC bool, ccstartuptimeout time.Duration, ca accesscontrol.CA) pb.ChaincodeSupportServer {
139139
ccprovider.SetChaincodesPath(config.GetPath("peer.fileSystemPath") + string(filepath.Separator) + "chaincodes")
140140
pnid := viper.GetString("peer.networkId")
141141
pid := viper.GetString("peer.id")
@@ -148,21 +148,7 @@ func NewChaincodeSupport(getCCEndpoint func() (*pb.PeerEndpoint, error), userrun
148148
}
149149

150150
theChaincodeSupport.auth = accesscontrol.NewAuthenticator(theChaincodeSupport, ca)
151-
152-
ccEndpoint, err := getCCEndpoint()
153-
if err != nil {
154-
// getCCEndpoint has already done necessary checks,
155-
// therefore it will panic if any error returns and it's in dev mode.
156-
// peerAddressDefault is acceptable only in dev mode.
157-
if IsDevMode() {
158-
chaincodeLogger.Errorf("Error getting chaincode endpoint in dev mode, using %s: %+v", peerAddressDefault, err)
159-
theChaincodeSupport.peerAddress = peerAddressDefault
160-
} else {
161-
chaincodeLogger.Panicf("Error getting chaincode endpoint: %+v", err)
162-
}
163-
} else {
164-
theChaincodeSupport.peerAddress = ccEndpoint.Address
165-
}
151+
theChaincodeSupport.peerAddress = ccEndpoint
166152
chaincodeLogger.Infof("Chaincode support using peerAddress: %s\n", theChaincodeSupport.peerAddress)
167153

168154
theChaincodeSupport.userRunsCC = userrunsCC

core/chaincode/chaincode_support_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,9 @@ func initMockPeer(chainIDs ...string) error {
150150

151151
peer.MockSetMSPIDGetter(mspGetter)
152152

153-
getPeerEndpoint := func() (*pb.PeerEndpoint, error) {
154-
return &pb.PeerEndpoint{Id: &pb.PeerID{Name: "testpeer"}}, nil
155-
}
156-
157153
ccStartupTimeout := time.Duration(10) * time.Second
158154
ca, _ := accesscontrol.NewCA()
159-
NewChaincodeSupport(getPeerEndpoint, false, ccStartupTimeout, ca)
155+
NewChaincodeSupport("0.0.0.0:7052", false, ccStartupTimeout, ca)
160156
theChaincodeSupport.executetimeout = time.Duration(1) * time.Second
161157

162158
// Mock policy checker

core/chaincode/exectransaction_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,9 @@ func initPeer(chainIDs ...string) (net.Listener, error) {
9898
return nil, fmt.Errorf("Error starting peer listener %s", err)
9999
}
100100

101-
getPeerEndpoint := func() (*pb.PeerEndpoint, error) {
102-
return &pb.PeerEndpoint{Id: &pb.PeerID{Name: "testpeer"}, Address: peerAddress}, nil
103-
}
104-
105101
ccStartupTimeout := time.Duration(3) * time.Minute
106102
ca, _ := accesscontrol.NewCA()
107-
pb.RegisterChaincodeSupportServer(grpcServer, NewChaincodeSupport(getPeerEndpoint, false, ccStartupTimeout, ca))
103+
pb.RegisterChaincodeSupportServer(grpcServer, NewChaincodeSupport(peerAddress, false, ccStartupTimeout, ca))
108104

109105
// Mock policy checker
110106
policy.RegisterPolicyCheckerFactory(&mockPolicyCheckerFactory{})

core/chaincode/systemchaincode_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,9 @@ func initSysCCTests() (*oldSysCCInfo, net.Listener, error) {
6767
return nil, nil, err
6868
}
6969

70-
getPeerEndpoint := func() (*pb.PeerEndpoint, error) {
71-
return &pb.PeerEndpoint{Id: &pb.PeerID{Name: "testpeer"}, Address: peerAddress}, nil
72-
}
73-
7470
ccStartupTimeout := time.Duration(5000) * time.Millisecond
7571
ca, _ := accesscontrol.NewCA()
76-
pb.RegisterChaincodeSupportServer(grpcServer, NewChaincodeSupport(getPeerEndpoint, false, ccStartupTimeout, ca))
72+
pb.RegisterChaincodeSupportServer(grpcServer, NewChaincodeSupport(peerAddress, false, ccStartupTimeout, ca))
7773

7874
go grpcServer.Serve(lis)
7975

core/scc/cscc/configure_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,10 @@ func TestConfigerInvokeJoinChainCorrectParams(t *testing.T) {
197197
stub := shim.NewMockStub("PeerConfiger", e)
198198

199199
peerEndpoint := "localhost:13611"
200-
getPeerEndpoint := func() (*pb.PeerEndpoint, error) {
201-
return &pb.PeerEndpoint{Id: &pb.PeerID{Name: "cscctestpeer"}, Address: peerEndpoint}, nil
202-
}
200+
203201
ccStartupTimeout := time.Duration(30000) * time.Millisecond
204202
ca, _ := accesscontrol.NewCA()
205-
chaincode.NewChaincodeSupport(getPeerEndpoint, false, ccStartupTimeout, ca)
203+
chaincode.NewChaincodeSupport(peerEndpoint, false, ccStartupTimeout, ca)
206204

207205
// Init the policy checker
208206
policyManagerGetter := &policymocks.MockChannelPolicyManagerGetter{

peer/node/start.go

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ const (
5757
defaultChaincodePort = 7052
5858
)
5959

60-
//function used by chaincode support
61-
type ccEndpointFunc func() (*pb.PeerEndpoint, error)
62-
6360
var chaincodeDevMode bool
6461
var orderingEndpoint string
6562

@@ -161,8 +158,11 @@ func serve(args []string) error {
161158
if err != nil {
162159
logger.Panic("Failed creating authentication layer:", err)
163160
}
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)
166166
go ccSrv.Start()
167167

168168
logger.Debugf("Running peer")
@@ -303,20 +303,32 @@ func serve(args []string) error {
303303
}
304304

305305
//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+
307321
cclistenAddress := viper.GetString(chaincodeListenAddrKey)
308322
if cclistenAddress == "" {
309323
cclistenAddress = fmt.Sprintf("%s:%d", peerHostname, defaultChaincodePort)
310324
logger.Warningf("%s is not set, using %s", chaincodeListenAddrKey, cclistenAddress)
311325
viper.Set(chaincodeListenAddrKey, cclistenAddress)
312326
}
313327

314-
var srv comm.GRPCServer
315-
var ccEpFunc ccEndpointFunc
316-
317328
config, err := peer.GetServerConfig()
318329
if err != nil {
319-
panic(err)
330+
logger.Errorf("Error getting server config: %s", err)
331+
return nil, "", err
320332
}
321333

322334
if config.SecOpts.UseTLS {
@@ -334,32 +346,17 @@ func createChaincodeServer(caCert []byte, peerHostname string) (comm.GRPCServer,
334346

335347
srv, err = comm.NewGRPCServer(cclistenAddress, config)
336348
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
356351
}
357352

358-
return srv, ccEpFunc
353+
return srv, ccEndpoint, nil
359354
}
360355

356+
// computeChaincodeEndpoint will utilize chaincode address, chaincode listen
357+
// address (these two are from viper) and peer address to compute chaincode endpoint.
361358
// 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 "::")
363360
// Case B: else if chaincodeListenAddrKey is set and not "0.0.0.0" or ("::"), use it
364361
// Case C: else use peer address if not "0.0.0.0" (or "::")
365362
// Case D: else return error
@@ -381,15 +378,14 @@ func computeChaincodeEndpoint(peerHostname string) (ccEndpoint string, err error
381378
logger.Errorf("ChaincodeAddress and chaincodeListenAddress are nil and peerIP is %s", peerIp)
382379
return "", errors.New("invalid endpoint for chaincode to connect")
383380
}
381+
384382
// use peerAddress:defaultChaincodePort
385383
ccEndpoint = fmt.Sprintf("%s:%d", peerHostname, defaultChaincodePort)
386384

387385
} else {
388386
// Case B: chaincodeListenAddrKey is set
389387
host, port, err := net.SplitHostPort(ccEndpoint)
390388
if err != nil {
391-
// and the listener was brought up above...
392-
// so this should really not happen.. just a paranoid check
393389
logger.Errorf("ChaincodeAddress is nil and fail to split chaincodeListenAddress: %s", err)
394390
return "", err
395391
}
@@ -407,13 +403,20 @@ func computeChaincodeEndpoint(peerHostname string) (ccEndpoint string, err error
407403
}
408404
ccEndpoint = fmt.Sprintf("%s:%s", peerHostname, port)
409405
}
406+
410407
}
411408

412409
} else {
413410
// Case A: the chaincodeAddrKey is set
414-
if _, _, err = net.SplitHostPort(ccEndpoint); err != nil {
411+
if host, _, err := net.SplitHostPort(ccEndpoint); err != nil {
415412
logger.Errorf("Fail to split chaincodeAddress: %s", err)
416413
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+
}
417420
}
418421
}
419422

@@ -424,7 +427,7 @@ func computeChaincodeEndpoint(peerHostname string) (ccEndpoint string, err error
424427
//NOTE - when we implement JOIN we will no longer pass the chainID as param
425428
//The chaincode support will come up without registering system chaincodes
426429
//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) {
428431
//get user mode
429432
userRunsCC := chaincode.IsDevMode()
430433

@@ -437,7 +440,7 @@ func registerChaincodeSupport(grpcServer comm.GRPCServer, ccEpFunc ccEndpointFun
437440
logger.Debugf("Chaincode startup timeout value set to %s", ccStartupTimeout)
438441
}
439442

440-
ccSrv := chaincode.NewChaincodeSupport(ccEpFunc, userRunsCC, ccStartupTimeout, ca)
443+
ccSrv := chaincode.NewChaincodeSupport(ccEndpoint, userRunsCC, ccStartupTimeout, ca)
441444

442445
//Now that chaincode is initialized, register all system chaincodes.
443446
scc.RegisterSysCCs()

0 commit comments

Comments
 (0)