github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/gossip/integration/integration.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package integration
    18  
    19  import (
    20  	"crypto/tls"
    21  	"strconv"
    22  	"strings"
    23  	"time"
    24  
    25  	"github.com/hyperledger/fabric/core/config"
    26  	"github.com/hyperledger/fabric/gossip/api"
    27  	"github.com/hyperledger/fabric/gossip/gossip"
    28  	"github.com/hyperledger/fabric/gossip/identity"
    29  	"github.com/hyperledger/fabric/gossip/util"
    30  	"github.com/spf13/viper"
    31  	"google.golang.org/grpc"
    32  )
    33  
    34  // This file is used to bootstrap a gossip instance and/or leader election service instance
    35  
    36  func newConfig(selfEndpoint string, externalEndpoint string, bootPeers ...string) *gossip.Config {
    37  	port, err := strconv.ParseInt(strings.Split(selfEndpoint, ":")[1], 10, 64)
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  
    42  	var cert *tls.Certificate
    43  	if viper.GetBool("peer.tls.enabled") {
    44  		certTmp, err := tls.LoadX509KeyPair(config.GetPath("peer.tls.cert.file"), config.GetPath("peer.tls.key.file"))
    45  		if err != nil {
    46  			panic(err)
    47  		}
    48  		cert = &certTmp
    49  	}
    50  
    51  	return &gossip.Config{
    52  		BindPort:                   int(port),
    53  		BootstrapPeers:             bootPeers,
    54  		ID:                         selfEndpoint,
    55  		MaxBlockCountToStore:       util.GetIntOrDefault("peer.gossip.maxBlockCountToStore", 100),
    56  		MaxPropagationBurstLatency: util.GetDurationOrDefault("peer.gossip.maxPropagationBurstLatency", 10*time.Millisecond),
    57  		MaxPropagationBurstSize:    util.GetIntOrDefault("peer.gossip.maxPropagationBurstSize", 10),
    58  		PropagateIterations:        util.GetIntOrDefault("peer.gossip.propagateIterations", 1),
    59  		PropagatePeerNum:           util.GetIntOrDefault("peer.gossip.propagatePeerNum", 3),
    60  		PullInterval:               util.GetDurationOrDefault("peer.gossip.pullInterval", 4*time.Second),
    61  		PullPeerNum:                util.GetIntOrDefault("peer.gossip.pullPeerNum", 3),
    62  		InternalEndpoint:           selfEndpoint,
    63  		ExternalEndpoint:           externalEndpoint,
    64  		PublishCertPeriod:          util.GetDurationOrDefault("peer.gossip.publishCertPeriod", 10*time.Second),
    65  		RequestStateInfoInterval:   util.GetDurationOrDefault("peer.gossip.requestStateInfoInterval", 4*time.Second),
    66  		PublishStateInfoInterval:   util.GetDurationOrDefault("peer.gossip.publishStateInfoInterval", 4*time.Second),
    67  		SkipBlockVerification:      viper.GetBool("peer.gossip.skipBlockVerification"),
    68  		TLSServerCert:              cert,
    69  	}
    70  }
    71  
    72  // NewGossipComponent creates a gossip component that attaches itself to the given gRPC server
    73  func NewGossipComponent(peerIdentity []byte, endpoint string, s *grpc.Server,
    74  	secAdv api.SecurityAdvisor, cryptSvc api.MessageCryptoService, idMapper identity.Mapper,
    75  	secureDialOpts api.PeerSecureDialOpts, bootPeers ...string) gossip.Gossip {
    76  
    77  	externalEndpoint := viper.GetString("peer.gossip.externalEndpoint")
    78  
    79  	conf := newConfig(endpoint, externalEndpoint, bootPeers...)
    80  	gossipInstance := gossip.NewGossipService(conf, s, secAdv, cryptSvc, idMapper,
    81  		peerIdentity, secureDialOpts)
    82  
    83  	return gossipInstance
    84  }