github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/gossip/integration/integration.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package integration
     8  
     9  import (
    10  	"crypto/tls"
    11  	"fmt"
    12  	"net"
    13  	"strconv"
    14  	"time"
    15  
    16  	"github.com/hyperledger/fabric/core/config"
    17  	"github.com/hyperledger/fabric/gossip/api"
    18  	"github.com/hyperledger/fabric/gossip/gossip"
    19  	"github.com/hyperledger/fabric/gossip/identity"
    20  	"github.com/hyperledger/fabric/gossip/util"
    21  	"github.com/spf13/viper"
    22  	"google.golang.org/grpc"
    23  )
    24  
    25  // This file is used to bootstrap a gossip instance and/or leader election service instance
    26  
    27  func newConfig(selfEndpoint string, externalEndpoint string, bootPeers ...string) (*gossip.Config, error) {
    28  	_, p, err := net.SplitHostPort(selfEndpoint)
    29  
    30  	if err != nil {
    31  		return nil, fmt.Errorf("misconfigured endpoint %s, the error is %s", selfEndpoint, err)
    32  	}
    33  
    34  	port, err := strconv.ParseInt(p, 10, 64)
    35  	if err != nil {
    36  		return nil, fmt.Errorf("misconfigured endpoint %s, failed to parse port number due to %s", selfEndpoint, err)
    37  	}
    38  
    39  	var cert *tls.Certificate
    40  	if viper.GetBool("peer.tls.enabled") {
    41  		certTmp, err := tls.LoadX509KeyPair(config.GetPath("peer.tls.cert.file"), config.GetPath("peer.tls.key.file"))
    42  		if err != nil {
    43  			return nil, fmt.Errorf("failed to load certificates because of %s", err)
    44  		}
    45  		cert = &certTmp
    46  	}
    47  
    48  	return &gossip.Config{
    49  		BindPort:                   int(port),
    50  		BootstrapPeers:             bootPeers,
    51  		ID:                         selfEndpoint,
    52  		MaxBlockCountToStore:       util.GetIntOrDefault("peer.gossip.maxBlockCountToStore", 100),
    53  		MaxPropagationBurstLatency: util.GetDurationOrDefault("peer.gossip.maxPropagationBurstLatency", 10*time.Millisecond),
    54  		MaxPropagationBurstSize:    util.GetIntOrDefault("peer.gossip.maxPropagationBurstSize", 10),
    55  		PropagateIterations:        util.GetIntOrDefault("peer.gossip.propagateIterations", 1),
    56  		PropagatePeerNum:           util.GetIntOrDefault("peer.gossip.propagatePeerNum", 3),
    57  		PullInterval:               util.GetDurationOrDefault("peer.gossip.pullInterval", 4*time.Second),
    58  		PullPeerNum:                util.GetIntOrDefault("peer.gossip.pullPeerNum", 3),
    59  		InternalEndpoint:           selfEndpoint,
    60  		ExternalEndpoint:           externalEndpoint,
    61  		PublishCertPeriod:          util.GetDurationOrDefault("peer.gossip.publishCertPeriod", 10*time.Second),
    62  		RequestStateInfoInterval:   util.GetDurationOrDefault("peer.gossip.requestStateInfoInterval", 4*time.Second),
    63  		PublishStateInfoInterval:   util.GetDurationOrDefault("peer.gossip.publishStateInfoInterval", 4*time.Second),
    64  		SkipBlockVerification:      viper.GetBool("peer.gossip.skipBlockVerification"),
    65  		TLSServerCert:              cert,
    66  	}, nil
    67  }
    68  
    69  // NewGossipComponent creates a gossip component that attaches itself to the given gRPC server
    70  func NewGossipComponent(peerIdentity []byte, endpoint string, s *grpc.Server,
    71  	secAdv api.SecurityAdvisor, cryptSvc api.MessageCryptoService, idMapper identity.Mapper,
    72  	secureDialOpts api.PeerSecureDialOpts, bootPeers ...string) (gossip.Gossip, error) {
    73  
    74  	externalEndpoint := viper.GetString("peer.gossip.externalEndpoint")
    75  
    76  	conf, err := newConfig(endpoint, externalEndpoint, bootPeers...)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  	gossipInstance := gossip.NewGossipService(conf, s, secAdv, cryptSvc, idMapper,
    81  		peerIdentity, secureDialOpts)
    82  
    83  	return gossipInstance, nil
    84  }