Skip to content

Commit 9bce561

Browse files
author
Srinivasan Muralidharan
committed
[FAB-4751] allow provisioning separate chaincode server
The chaincodeListenAddress property allows chaincode connections to use a listener other than the peer's listener. With this, chaincode streams need not be bound to the peers endpoint allowing more network options for protecting it. The e2e_cli sample uses the chaincodeListenAddress to excercise the separate chaincode listener (on 7052 but on the same IP address as the that peer's listener uses). patch 1 . comment changes (thanks, Binh) Change-Id: Ic0943be6739ef0401e12686721d3e0472479580c Signed-off-by: Srinivasan Muralidharan <[email protected]>
1 parent 69d40c3 commit 9bce561

File tree

7 files changed

+104
-9
lines changed

7 files changed

+104
-9
lines changed

core/chaincode/chaincode_support.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (chaincodeSupport *ChaincodeSupport) chaincodeHasBeenLaunched(chaincode str
120120
}
121121

122122
// NewChaincodeSupport creates a new ChaincodeSupport instance
123-
func NewChaincodeSupport(getPeerEndpoint func() (*pb.PeerEndpoint, error), userrunsCC bool, ccstartuptimeout time.Duration) *ChaincodeSupport {
123+
func NewChaincodeSupport(getCCEndpoint func() (*pb.PeerEndpoint, error), userrunsCC bool, ccstartuptimeout time.Duration) *ChaincodeSupport {
124124
ccprovider.SetChaincodesPath(config.GetPath("peer.fileSystemPath") + string(filepath.Separator) + "chaincodes")
125125

126126
pnid := viper.GetString("peer.networkId")
@@ -130,12 +130,12 @@ func NewChaincodeSupport(getPeerEndpoint func() (*pb.PeerEndpoint, error), userr
130130

131131
//initialize global chain
132132

133-
peerEndpoint, err := getPeerEndpoint()
133+
ccEndpoint, err := getCCEndpoint()
134134
if err != nil {
135-
chaincodeLogger.Errorf("Error getting PeerEndpoint, using peer.address: %s", err)
136-
theChaincodeSupport.peerAddress = viper.GetString("peer.address")
135+
chaincodeLogger.Errorf("Error getting chaincode endpoint, using chaincode.peerAddress: %s", err)
136+
theChaincodeSupport.peerAddress = viper.GetString("chaincode.peerAddress")
137137
} else {
138-
theChaincodeSupport.peerAddress = peerEndpoint.Address
138+
theChaincodeSupport.peerAddress = ccEndpoint.Address
139139
}
140140
chaincodeLogger.Infof("Chaincode support using peerAddress: %s\n", theChaincodeSupport.peerAddress)
141141
//peerAddress = viper.GetString("peer.address")

core/comm/connection_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ func TestConnection_Correct(t *testing.T) {
7676
func TestConnection_WrongAddress(t *testing.T) {
7777
testutil.SetupTestConfig()
7878
viper.Set("ledger.blockchain.deploy-system-chaincode", "false")
79-
peerAddress := GetPeerTestingAddress("7052")
79+
//some random port
80+
peerAddress := GetPeerTestingAddress("10287")
8081
var tmpConn *grpc.ClientConn
8182
var err error
8283
if TLSEnabled() {

examples/e2e_cli/base/docker-compose-base.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ services:
3939
environment:
4040
- CORE_PEER_ID=peer0.org1.example.com
4141
- CORE_PEER_ADDRESS=peer0.org1.example.com:7051
42+
- CORE_PEER_CHAINCODELISTENADDRESS=peer0.org1.example.com:7052
4243
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.example.com:7051
4344
- CORE_PEER_LOCALMSPID=Org1MSP
4445
volumes:
@@ -47,6 +48,7 @@ services:
4748
- ../crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls:/etc/hyperledger/fabric/tls
4849
ports:
4950
- 7051:7051
51+
- 7052:7052
5052
- 7053:7053
5153

5254
peer1.org1.example.com:
@@ -57,6 +59,7 @@ services:
5759
environment:
5860
- CORE_PEER_ID=peer1.org1.example.com
5961
- CORE_PEER_ADDRESS=peer1.org1.example.com:7051
62+
- CORE_PEER_CHAINCODELISTENADDRESS=peer1.org1.example.com:7052
6063
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.org1.example.com:7051
6164
- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org1.example.com:7051
6265
- CORE_PEER_LOCALMSPID=Org1MSP
@@ -67,6 +70,7 @@ services:
6770

6871
ports:
6972
- 8051:7051
73+
- 8052:7052
7074
- 8053:7053
7175

7276
peer0.org2.example.com:
@@ -77,6 +81,7 @@ services:
7781
environment:
7882
- CORE_PEER_ID=peer0.org2.example.com
7983
- CORE_PEER_ADDRESS=peer0.org2.example.com:7051
84+
- CORE_PEER_CHAINCODELISTENADDRESS=peer0.org2.example.com:7052
8085
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org2.example.com:7051
8186
- CORE_PEER_LOCALMSPID=Org2MSP
8287
volumes:
@@ -85,6 +90,7 @@ services:
8590
- ../crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls:/etc/hyperledger/fabric/tls
8691
ports:
8792
- 9051:7051
93+
- 9052:7052
8894
- 9053:7053
8995

9096
peer1.org2.example.com:
@@ -95,6 +101,7 @@ services:
95101
environment:
96102
- CORE_PEER_ID=peer1.org2.example.com
97103
- CORE_PEER_ADDRESS=peer1.org2.example.com:7051
104+
- CORE_PEER_CHAINCODELISTENADDRESS=peer1.org2.example.com:7052
98105
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.org2.example.com:7051
99106
- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org2.example.com:7051
100107
- CORE_PEER_LOCALMSPID=Org2MSP
@@ -104,4 +111,5 @@ services:
104111
- ../crypto-config/peerOrganizations/org2.example.com/peers/peer1.org2.example.com/tls:/etc/hyperledger/fabric/tls
105112
ports:
106113
- 10051:7051
114+
- 10052:7052
107115
- 10053:7053

peer/node/start.go

+72-3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ import (
4040
"google.golang.org/grpc/grpclog"
4141
)
4242

43+
//function used by chaincode support
44+
type ccEndpointFunc func() (*pb.PeerEndpoint, error)
45+
4346
var chaincodeDevMode bool
4447
var peerDefaultChain bool
4548
var orderingEndpoint string
@@ -128,7 +131,9 @@ func serve(args []string) error {
128131
// enable the cache of chaincode info
129132
ccprovider.EnableCCInfoCache()
130133

131-
registerChaincodeSupport(peerServer.Server())
134+
ccSrv, ccEpFunc := createChaincodeServer(peerServer, listenAddr)
135+
registerChaincodeSupport(ccSrv.Server(), ccEpFunc)
136+
go ccSrv.Start()
132137

133138
logger.Debugf("Running peer")
134139

@@ -247,10 +252,52 @@ func serve(args []string) error {
247252
return <-serve
248253
}
249254

255+
//create a CC listener using peer.chaincodeListenAddress (and if that's not set use peer.peerAddress)
256+
func createChaincodeServer(peerServer comm.GRPCServer, peerListenAddress string) (comm.GRPCServer, ccEndpointFunc) {
257+
cclistenAddress := viper.GetString("peer.chaincodeListenAddress")
258+
259+
var srv comm.GRPCServer
260+
var ccEpFunc ccEndpointFunc
261+
262+
//use the chaincode address endpoint function..
263+
//three cases
264+
// - peer.chaincodeListenAddress not specied (use peer's server)
265+
// - peer.chaincodeListenAddress identical to peer.listenAddress (use peer's server)
266+
// - peer.chaincodeListenAddress different and specified (create chaincode server)
267+
if cclistenAddress == "" {
268+
//...but log a warning
269+
logger.Warningf("peer.chaincodeListenAddress is not set, use peer.listenAddress %s", peerListenAddress)
270+
271+
//we are using peer address, use peer endpoint
272+
ccEpFunc = peer.GetPeerEndpoint
273+
srv = peerServer
274+
} else if cclistenAddress == peerListenAddress {
275+
//using peer's endpoint...log a warning
276+
logger.Warningf("peer.chaincodeListenAddress is identical to peer.listenAddress %s", cclistenAddress)
277+
278+
//we are using peer address, use peer endpoint
279+
ccEpFunc = peer.GetPeerEndpoint
280+
srv = peerServer
281+
} else {
282+
config, err := peer.GetSecureConfig()
283+
if err != nil {
284+
panic(err)
285+
}
286+
287+
srv, err = comm.NewGRPCServer(cclistenAddress, config)
288+
if err != nil {
289+
panic(err)
290+
}
291+
ccEpFunc = getChaincodeAddressEndpoint
292+
}
293+
294+
return srv, ccEpFunc
295+
}
296+
250297
//NOTE - when we implment JOIN we will no longer pass the chainID as param
251298
//The chaincode support will come up without registering system chaincodes
252299
//which will be registered only during join phase.
253-
func registerChaincodeSupport(grpcServer *grpc.Server) {
300+
func registerChaincodeSupport(grpcServer *grpc.Server, ccEpFunc ccEndpointFunc) {
254301
//get user mode
255302
userRunsCC := chaincode.IsDevMode()
256303

@@ -263,14 +310,36 @@ func registerChaincodeSupport(grpcServer *grpc.Server) {
263310
logger.Debugf("Chaincode startup timeout value set to %s", ccStartupTimeout)
264311
}
265312

266-
ccSrv := chaincode.NewChaincodeSupport(peer.GetPeerEndpoint, userRunsCC, ccStartupTimeout)
313+
ccSrv := chaincode.NewChaincodeSupport(ccEpFunc, userRunsCC, ccStartupTimeout)
267314

268315
//Now that chaincode is initialized, register all system chaincodes.
269316
scc.RegisterSysCCs()
270317

271318
pb.RegisterChaincodeSupportServer(grpcServer, ccSrv)
272319
}
273320

321+
func getChaincodeAddressEndpoint() (*pb.PeerEndpoint, error) {
322+
//need this for the ID to create chaincode endpoint
323+
peerEndpoint, err := peer.GetPeerEndpoint()
324+
if err != nil {
325+
return nil, err
326+
}
327+
328+
ccendpoint := viper.GetString("peer.chaincodeListenAddress")
329+
if ccendpoint == "" {
330+
return nil, fmt.Errorf("peer.chaincodeListenAddress not specified")
331+
}
332+
333+
if _, _, err = net.SplitHostPort(ccendpoint); err != nil {
334+
return nil, err
335+
}
336+
337+
return &pb.PeerEndpoint{
338+
Id: peerEndpoint.Id,
339+
Address: ccendpoint,
340+
}, nil
341+
}
342+
274343
func createEventHubServer(secureConfig comm.SecureServerConfig) (comm.GRPCServer, error) {
275344
var lis net.Listener
276345
var err error

peer/node/start_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
func TestStartCmd(t *testing.T) {
3333
viper.Set("peer.address", "0.0.0.0:6051")
3434
viper.Set("peer.listenAddress", "0.0.0.0:6051")
35+
viper.Set("peer.chaincodeListenAddress", "0.0.0.0:6052")
3536
viper.Set("peer.fileSystemPath", "/tmp/hyperledger/test")
3637
viper.Set("chaincode.executetimeout", "30s")
3738
overrideLogModules := []string{"msp", "gossip", "ledger", "cauthdsl", "policies", "grpc"}

sampleconfig/core.yaml

+15
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ peer:
5757
# The Address at local network interface this Peer will listen on.
5858
# By default, it will listen on all network interfaces
5959
listenAddress: 0.0.0.0:7051
60+
61+
# The endpoint this peer uses to listen for inbound chaincode connections.
62+
#
63+
# If chaincodeListenAddress is commented out or equals listenAddress, listenAddress will
64+
# be used for chaincode connections. Otherwise a new listener different from peer's listener
65+
# on listenAddress will be used.
66+
#
67+
# The chaincode connection does not support TLS-mutual auth. Having a separate listener for
68+
# the chaincode environment helps isolate the chaincode enviroment for enhanced security.
69+
#
70+
# chaincodeListenAddress: 127.0.0.1:7052
71+
6072
# When used as peer config, represents the endpoint to other peers in the same organization
6173
# for peers in other organization, see gossip.externalEndpoint
6274
# When used as cli config, will mean the peer node's endpoint to interact with
@@ -270,6 +282,9 @@ vm:
270282
#
271283
###############################################################################
272284
chaincode:
285+
# This is used if chaincode endpoint resolution fails with the chaincodeListenAddress
286+
# property
287+
peerAddress:
273288

274289
# The id is used by the Chaincode stub to register the executing Chaincode
275290
# ID with the Peerand is generally supplied through ENV variables

unit-test/docker-compose.yml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ vp:
88
log_driver: none
99
expose:
1010
- 7051
11+
- 7052
1112
environment:
1213
- CORE_PEER_ADDRESSAUTODETECT=true
1314
volumes:

0 commit comments

Comments
 (0)