github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/consensus/ipbft/config/tendermint/config.go (about)

     1  package tendermint
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strings"
     7  
     8  	. "github.com/intfoundation/go-common"
     9  	cfg "github.com/intfoundation/go-config"
    10  )
    11  
    12  const (
    13  	defaultDataDir         = "data"
    14  	defaultConfigFileName  = "config.toml"
    15  	defaultGenesisJSONName = "genesis.json"
    16  )
    17  
    18  func getTMRoot(rootDir string) string {
    19  	if rootDir == "" {
    20  		rootDir = os.Getenv("TMHOME")
    21  	}
    22  	if rootDir == "" {
    23  		// deprecated, use TMHOME (TODO: remove in TM 0.11.0)
    24  		rootDir = os.Getenv("TMROOT")
    25  	}
    26  	if rootDir == "" {
    27  		rootDir = os.Getenv("HOME") + "/.ipbft"
    28  	}
    29  	return rootDir
    30  }
    31  
    32  func initTMRoot(rootDir, chainId string) {
    33  	rootDir = getTMRoot(rootDir)
    34  	EnsureDir(rootDir, 0700)
    35  	EnsureDir(filepath.Join(rootDir, chainId, defaultDataDir), 0700)
    36  
    37  	configFilePath := filepath.Join(rootDir, defaultConfigFileName)
    38  
    39  	// Write default config file if missing.
    40  	if !FileExists(configFilePath) {
    41  		// Ask user for moniker
    42  		// moniker := cfg.Prompt("Type hostname: ", "anonymous")
    43  		MustWriteFile(configFilePath, []byte(defaultConfig("anonymous")), 0644)
    44  	}
    45  }
    46  
    47  func GetConfig(rootDir, chainId string) cfg.Config {
    48  	rootDir = getTMRoot(rootDir)
    49  	initTMRoot(rootDir, chainId)
    50  
    51  	configFilePath := filepath.Join(rootDir, defaultConfigFileName)
    52  	mapConfig, err := cfg.ReadMapConfigFromFile(configFilePath)
    53  	if err != nil {
    54  		Exit(Fmt("Could not read config: %v", err))
    55  	}
    56  
    57  	// Set defaults or panic
    58  	if mapConfig.IsSet("chain_id") {
    59  		Exit("Cannot set 'chain_id' via config.toml")
    60  	}
    61  	if mapConfig.IsSet("revision_file") {
    62  		Exit("Cannot set 'revision_file' via config.toml. It must match what's in the Makefile")
    63  	}
    64  	mapConfig.SetRequired("chain_id") // blows up if you try to use it before setting.
    65  	mapConfig.SetDefault("chain_id", chainId)
    66  	mapConfig.SetDefault("genesis_file", filepath.Join(rootDir, chainId, "genesis.json"))
    67  	mapConfig.SetDefault("int_genesis_file", filepath.Join(rootDir, chainId, "int_genesis.json"))
    68  	mapConfig.SetDefault("keystore", filepath.Join(rootDir, chainId, "keystore"))
    69  	//mapConfig.SetDefault("proxy_app", "tcp://127.0.0.1:46658")
    70  	//mapConfig.SetDefault("abci", "socket")
    71  	//mapConfig.Set("proxy_app", calcAppAddr())
    72  	//mapConfig.Set("abci", defaultAbci())
    73  	mapConfig.SetDefault("moniker", "anonymous")
    74  	mapConfig.SetDefault("node_laddr", "tcp://0.0.0.0:46656")
    75  	mapConfig.SetDefault("seeds", "")
    76  	// mapConfig.SetDefault("seeds", "goldenalchemist.chaintest.net:46656")
    77  	mapConfig.SetDefault("fast_sync", true)
    78  	mapConfig.SetDefault("skip_upnp", false)
    79  	mapConfig.SetDefault("addrbook_file", filepath.Join(rootDir, chainId, "addrbook.json"))
    80  	mapConfig.SetDefault("addrbook_strict", true) // disable to allow connections locally
    81  	mapConfig.SetDefault("pex_reactor", false)    // enable for peer exchange
    82  	mapConfig.SetDefault("priv_validator_file", filepath.Join(rootDir, chainId, "priv_validator.json"))
    83  	mapConfig.SetDefault("priv_validator_file_root", filepath.Join(rootDir, chainId, "priv_validator"))
    84  	mapConfig.SetDefault("db_dir", filepath.Join(rootDir, chainId, defaultDataDir))
    85  	//mapConfig.SetDefault("rpc_laddr", "tcp://0.0.0.0:46657")
    86  	//mapConfig.SetDefault("rpc_laddr", calcRpcAddr())
    87  	mapConfig.SetDefault("grpc_laddr", "")
    88  	mapConfig.SetDefault("prof_laddr", "")
    89  	mapConfig.SetDefault("revision_file", filepath.Join(rootDir, chainId, "revision"))
    90  	mapConfig.SetDefault("cs_wal_file", filepath.Join(rootDir, chainId, defaultDataDir, "cs.wal", "wal"))
    91  	mapConfig.SetDefault("cs_wal_light", false)
    92  	mapConfig.SetDefault("filter_peers", false)
    93  
    94  	mapConfig.SetDefault("block_size", 10000)      // max number of txs
    95  	mapConfig.SetDefault("block_part_size", 65536) // part size 64K
    96  	mapConfig.SetDefault("disable_data_hash", false)
    97  
    98  	// all timeouts are in ms
    99  	mapConfig.SetDefault("timeout_handshake", 10000)
   100  	mapConfig.SetDefault("timeout_wait_for_miner_block", 2000)
   101  	mapConfig.SetDefault("timeout_propose", 1500)
   102  	mapConfig.SetDefault("timeout_propose_delta", 500)
   103  	mapConfig.SetDefault("timeout_prevote", 2000)
   104  	mapConfig.SetDefault("timeout_prevote_delta", 1000)
   105  	mapConfig.SetDefault("timeout_precommit", 2000)
   106  	mapConfig.SetDefault("timeout_precommit_delta", 1000)
   107  	mapConfig.SetDefault("timeout_commit", 3000)
   108  
   109  	// make progress asap (no `timeout_commit`) on full precommit votes
   110  	mapConfig.SetDefault("skip_timeout_commit", false)
   111  	mapConfig.SetDefault("mempool_recheck", true)
   112  	mapConfig.SetDefault("mempool_recheck_empty", true)
   113  	mapConfig.SetDefault("mempool_broadcast", true)
   114  	mapConfig.SetDefault("mempool_wal_dir", filepath.Join(rootDir, chainId, defaultDataDir, "mempool.wal"))
   115  
   116  	//mapConfig.SetDefault("tx_index", "kv")
   117  
   118  	// EmptyBlocks mode and possible interval between empty blocks
   119  	//mapConfig.SetDefault("create_empty_blocks", true)
   120  	//mapConfig.SetDefault("create_empty_blocks_interval", 0)
   121  
   122  	return mapConfig
   123  }
   124  
   125  var defaultConfigTmpl = `# This is a TOML config file.
   126  # For more information, see https://github.com/toml-lang/toml
   127  #proxy_app = "tcp://127.0.0.1:46658"
   128  moniker = "__MONIKER__"
   129  node_laddr = "tcp://0.0.0.0:46656"
   130  seeds = ""
   131  fast_sync = true
   132  #rpc_laddr = "tcp://0.0.0.0:46657"
   133  `
   134  
   135  func defaultConfig(moniker string) (defaultConfig string) {
   136  	defaultConfig = strings.Replace(defaultConfigTmpl, "__MONIKER__", moniker, -1)
   137  	return
   138  }