github.com/tri-stone/burrow@v0.25.0/consensus/tendermint/config.go (about)

     1  package tendermint
     2  
     3  import (
     4  	"math"
     5  	"net/url"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/hyperledger/burrow/consensus/abci"
    10  	tmConfig "github.com/tendermint/tendermint/config"
    11  )
    12  
    13  // Burrow's view on Tendermint's config. Since we operate as a Tendermint harness not all configuration values
    14  // are applicable, we may not allow some values to specified, or we may not allow some to be set independently.
    15  // So this serves as a layer of indirection over Tendermint's real config that we derive from ours.
    16  type BurrowTendermintConfig struct {
    17  	Enabled bool
    18  	// Initial peers we connect to for peer exchange
    19  	Seeds string
    20  	// Whether this node should crawl the network looking for new peers - disconnecting to peers after it has shared addresses
    21  	SeedMode bool
    22  	// Peers to which we automatically connect
    23  	PersistentPeers string
    24  	ListenAddress   string
    25  	// Optional external that nodes may provide with their NodeInfo
    26  	ExternalAddress string
    27  	// Set true for strict address routability rules
    28  	// Set false for private or local networks
    29  	AddrBookStrict bool
    30  	Moniker        string
    31  	// Peers ID or address this node is authorize to sync with
    32  	AuthorizedPeers string
    33  	// EmptyBlocks mode and possible interval between empty blocks in seconds
    34  	CreateEmptyBlocks         bool
    35  	CreateEmptyBlocksInterval time.Duration
    36  }
    37  
    38  func DefaultBurrowTendermintConfig() *BurrowTendermintConfig {
    39  	tmDefaultConfig := tmConfig.DefaultConfig()
    40  	return &BurrowTendermintConfig{
    41  		Enabled:                   true,
    42  		ListenAddress:             tmDefaultConfig.P2P.ListenAddress,
    43  		ExternalAddress:           tmDefaultConfig.P2P.ExternalAddress,
    44  		CreateEmptyBlocks:         tmDefaultConfig.Consensus.CreateEmptyBlocks,
    45  		CreateEmptyBlocksInterval: tmDefaultConfig.Consensus.CreateEmptyBlocksInterval,
    46  	}
    47  }
    48  
    49  func (btc *BurrowTendermintConfig) Config(rootDir string, timeoutFactor float64) *tmConfig.Config {
    50  	conf := tmConfig.DefaultConfig()
    51  	// We expose Tendermint config as required, but try to give fewer levers to pull where possible
    52  	if btc != nil {
    53  		conf.RootDir = rootDir
    54  		conf.Mempool.RootDir = rootDir
    55  		conf.Consensus.RootDir = rootDir
    56  
    57  		// Consensus
    58  		conf.Consensus.CreateEmptyBlocks = btc.CreateEmptyBlocks
    59  		conf.Consensus.CreateEmptyBlocksInterval = btc.CreateEmptyBlocksInterval
    60  		// Assume Tendermint has some mutually consistent values, assume scaling them linearly makes sense
    61  		conf.Consensus.TimeoutPropose = scaleTimeout(timeoutFactor, conf.Consensus.TimeoutPropose)
    62  		conf.Consensus.TimeoutProposeDelta = scaleTimeout(timeoutFactor, conf.Consensus.TimeoutProposeDelta)
    63  		conf.Consensus.TimeoutPrevote = scaleTimeout(timeoutFactor, conf.Consensus.TimeoutPrevote)
    64  		conf.Consensus.TimeoutPrevoteDelta = scaleTimeout(timeoutFactor, conf.Consensus.TimeoutPrevoteDelta)
    65  		conf.Consensus.TimeoutPrecommit = scaleTimeout(timeoutFactor, conf.Consensus.TimeoutPrecommit)
    66  		conf.Consensus.TimeoutPrecommitDelta = scaleTimeout(timeoutFactor, conf.Consensus.TimeoutPrecommitDelta)
    67  		conf.Consensus.TimeoutCommit = scaleTimeout(timeoutFactor, conf.Consensus.TimeoutCommit)
    68  
    69  		// P2P
    70  		conf.Moniker = btc.Moniker
    71  		conf.P2P.RootDir = rootDir
    72  		conf.P2P.Seeds = btc.Seeds
    73  		conf.P2P.SeedMode = btc.SeedMode
    74  		conf.P2P.PersistentPeers = btc.PersistentPeers
    75  		conf.P2P.ListenAddress = btc.ListenAddress
    76  		conf.P2P.ExternalAddress = btc.ExternalAddress
    77  		conf.P2P.AddrBookStrict = btc.AddrBookStrict
    78  		// We use this in tests and I am not aware of a strong reason to reject nodes on the same IP with different ports
    79  		conf.P2P.AllowDuplicateIP = true
    80  
    81  		// Unfortunately this stops metrics from being used at all
    82  		conf.Instrumentation.Prometheus = false
    83  		conf.FilterPeers = btc.AuthorizedPeers != ""
    84  	}
    85  	// Disable Tendermint RPC
    86  	conf.RPC.ListenAddress = ""
    87  	return conf
    88  }
    89  
    90  func (btc *BurrowTendermintConfig) DefaultAuthorizedPeersProvider() abci.PeersFilterProvider {
    91  	var authorizedPeersID, authorizedPeersAddress []string
    92  
    93  	authorizedPeersAddrOrID := strings.Split(btc.AuthorizedPeers, ",")
    94  	for _, authorizedPeerAddrOrID := range authorizedPeersAddrOrID {
    95  		_, err := url.Parse(authorizedPeerAddrOrID)
    96  		isNodeAddress := err != nil
    97  		if isNodeAddress {
    98  			authorizedPeersAddress = append(authorizedPeersAddress, authorizedPeerAddrOrID)
    99  		} else {
   100  			authorizedPeersID = append(authorizedPeersID, authorizedPeerAddrOrID)
   101  		}
   102  	}
   103  
   104  	return func() ([]string, []string) {
   105  		return authorizedPeersID, authorizedPeersAddress
   106  	}
   107  }
   108  
   109  func scaleTimeout(factor float64, timeout time.Duration) time.Duration {
   110  	if factor == 0 {
   111  		return timeout
   112  	}
   113  	return time.Duration(math.Round(factor * float64(timeout)))
   114  }