github.com/inklabsfoundation/inkchain@v0.17.1-0.20181025012015-c3cef8062f19/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  	"fmt"
    22  	"net"
    23  	"strconv"
    24  	"time"
    25  
    26  	"github.com/inklabsfoundation/inkchain/core/config"
    27  	"github.com/inklabsfoundation/inkchain/gossip/api"
    28  	"github.com/inklabsfoundation/inkchain/gossip/gossip"
    29  	"github.com/inklabsfoundation/inkchain/gossip/identity"
    30  	"github.com/inklabsfoundation/inkchain/gossip/util"
    31  	"github.com/spf13/viper"
    32  	"google.golang.org/grpc"
    33  )
    34  
    35  // This file is used to bootstrap a gossip instance and/or leader election service instance
    36  
    37  func newConfig(selfEndpoint string, externalEndpoint string, bootPeers ...string) (*gossip.Config, error) {
    38  	_, p, err := net.SplitHostPort(selfEndpoint)
    39  
    40  	if err != nil {
    41  		return nil, fmt.Errorf("misconfigured endpoint %s, the error is %s", selfEndpoint, err)
    42  	}
    43  
    44  	port, err := strconv.ParseInt(p, 10, 64)
    45  	if err != nil {
    46  		return nil, fmt.Errorf("misconfigured endpoint %s, failed to parse port number due to %s", selfEndpoint, err)
    47  	}
    48  
    49  	var cert *tls.Certificate
    50  	if viper.GetBool("peer.tls.enabled") {
    51  		certTmp, err := tls.LoadX509KeyPair(config.GetPath("peer.tls.cert.file"), config.GetPath("peer.tls.key.file"))
    52  		if err != nil {
    53  			return nil, fmt.Errorf("failed to load certificates because of %s", err)
    54  		}
    55  		cert = &certTmp
    56  	}
    57  
    58  	return &gossip.Config{
    59  		BindPort:                   int(port),
    60  		BootstrapPeers:             bootPeers,
    61  		ID:                         selfEndpoint,
    62  		MaxBlockCountToStore:       util.GetIntOrDefault("peer.gossip.maxBlockCountToStore", 100),
    63  		MaxPropagationBurstLatency: util.GetDurationOrDefault("peer.gossip.maxPropagationBurstLatency", 10*time.Millisecond),
    64  		MaxPropagationBurstSize:    util.GetIntOrDefault("peer.gossip.maxPropagationBurstSize", 10),
    65  		PropagateIterations:        util.GetIntOrDefault("peer.gossip.propagateIterations", 1),
    66  		PropagatePeerNum:           util.GetIntOrDefault("peer.gossip.propagatePeerNum", 3),
    67  		PullInterval:               util.GetDurationOrDefault("peer.gossip.pullInterval", 4*time.Second),
    68  		PullPeerNum:                util.GetIntOrDefault("peer.gossip.pullPeerNum", 3),
    69  		InternalEndpoint:           selfEndpoint,
    70  		ExternalEndpoint:           externalEndpoint,
    71  		PublishCertPeriod:          util.GetDurationOrDefault("peer.gossip.publishCertPeriod", 10*time.Second),
    72  		RequestStateInfoInterval:   util.GetDurationOrDefault("peer.gossip.requestStateInfoInterval", 4*time.Second),
    73  		PublishStateInfoInterval:   util.GetDurationOrDefault("peer.gossip.publishStateInfoInterval", 4*time.Second),
    74  		SkipBlockVerification:      viper.GetBool("peer.gossip.skipBlockVerification"),
    75  		TLSServerCert:              cert,
    76  	}, nil
    77  }
    78  
    79  // NewGossipComponent creates a gossip component that attaches itself to the given gRPC server
    80  func NewGossipComponent(peerIdentity []byte, endpoint string, s *grpc.Server,
    81  	secAdv api.SecurityAdvisor, cryptSvc api.MessageCryptoService, idMapper identity.Mapper,
    82  	secureDialOpts api.PeerSecureDialOpts, bootPeers ...string) (gossip.Gossip, error) {
    83  
    84  	externalEndpoint := viper.GetString("peer.gossip.externalEndpoint")
    85  
    86  	conf, err := newConfig(endpoint, externalEndpoint, bootPeers...)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  	gossipInstance := gossip.NewGossipService(conf, s, secAdv, cryptSvc, idMapper,
    91  		peerIdentity, secureDialOpts)
    92  
    93  	return gossipInstance, nil
    94  }