tendermint/config/config.go

253 lines
7.0 KiB
Go
Raw Normal View History

2014-06-06 11:57:22 -07:00
package config
import (
2015-05-12 18:29:00 -07:00
"bufio"
2014-07-01 14:50:24 -07:00
"fmt"
"io/ioutil"
"os"
2015-03-16 23:34:31 -07:00
"path"
2014-07-01 14:50:24 -07:00
"path/filepath"
"strings"
2015-03-16 23:16:26 -07:00
"sync"
2014-06-06 11:57:22 -07:00
2015-01-08 16:40:23 -08:00
flag "github.com/spf13/pflag"
"github.com/tendermint/confer"
)
2014-06-06 11:57:22 -07:00
2015-03-16 23:16:26 -07:00
var app *confer.Config
var appMtx sync.Mutex
func App() *confer.Config {
appMtx.Lock()
defer appMtx.Unlock()
if app == nil {
Init("")
2015-03-16 23:16:26 -07:00
}
return app
}
func SetApp(a *confer.Config) {
appMtx.Lock()
defer appMtx.Unlock()
app = a
}
// NOTE: If you change this, maybe also change initDefaults()
2015-05-12 18:29:00 -07:00
var defaultConfigTmpl = `# This is a TOML config file.
2015-01-08 16:40:23 -08:00
# For more information, see https://github.com/toml-lang/toml
2014-06-06 11:57:22 -07:00
network = "tendermint_testnet_5"
2015-05-12 18:29:00 -07:00
moniker = "__MONIKER__"
node_laddr = "0.0.0.0:46656"
seeds = "goldenalchemist.chaintest.net:46656"
fast_sync = true
db_backend = "leveldb"
log_level = "debug"
rpc_laddr = "0.0.0.0:46657"
2015-01-08 16:40:23 -08:00
`
2015-04-02 20:10:37 -07:00
var DefaultGenesis = `{
2015-05-01 17:26:49 -07:00
"accounts": [
2015-03-16 23:34:31 -07:00
{
2015-05-13 18:39:32 -07:00
"address": "F81CB9ED0A868BD961C4F5BBC0E39B763B89FCB6",
"amount": 690000000000
2015-05-03 23:06:27 -07:00
},
{
2015-05-13 18:39:32 -07:00
"address": "0000000000000000000000000000000000000002",
"amount": 565000000000
},
{
"address": "9E54C9ECA9A3FD5D4496696818DA17A9E17F69DA",
"amount": 525000000000
},
{
"address": "0000000000000000000000000000000000000004",
"amount": 110000000000
2015-03-16 23:34:31 -07:00
}
2015-04-02 20:10:37 -07:00
],
2015-05-01 17:26:49 -07:00
"validators": [
2015-04-18 23:08:02 -07:00
{
2015-05-13 18:39:32 -07:00
"pub_key": [1, "178EC6008A4364508979C70CBF100BD4BCBAA12DDE6251F5F486B4FD09014F06"],
"amount": 5000000000,
2015-05-01 17:26:49 -07:00
"unbond_to": [
2015-05-13 18:39:32 -07:00
{
"address": "93E243AC8A01F723DE353A4FA1ED911529CCB6E5",
"amount": 5000000000
}
2015-04-18 23:08:02 -07:00
]
},
2015-05-03 23:06:27 -07:00
{
2015-05-13 18:39:32 -07:00
"pub_key": [1, "2A77777CC51467DE42350D4A8F34720D527734189BE64C7A930DD169E1FED3C6"],
"amount": 5000000000,
2015-05-03 23:06:27 -07:00
"unbond_to": [
2015-05-13 18:39:32 -07:00
{
"address": "93E243AC8A01F723DE353A4FA1ED911529CCB6E5",
"amount": 5000000000
}
2015-05-03 23:06:27 -07:00
]
},
2015-04-23 21:27:22 -07:00
{
2015-05-13 18:39:32 -07:00
"pub_key": [1, "3718E69D09B11B3AD3FA31AEF07EC416D2AEED241CACE7B0F30AE9803FFB0F08"],
"amount": 5000000000,
2015-05-01 17:26:49 -07:00
"unbond_to": [
2015-05-13 18:39:32 -07:00
{
"address": "93E243AC8A01F723DE353A4FA1ED911529CCB6E5",
"amount": 5000000000
}
2015-04-23 21:27:22 -07:00
]
},
2015-04-02 20:10:37 -07:00
{
2015-05-13 18:39:32 -07:00
"pub_key": [1, "C6B0440DEACD1E4CF1C736CEB8E38E788B700BA2B2045A55CB657A455CF5F889"],
"amount": 5000000000,
2015-05-01 17:26:49 -07:00
"unbond_to": [
2015-05-13 18:39:32 -07:00
{
"address": "93E243AC8A01F723DE353A4FA1ED911529CCB6E5",
"amount": 5000000000
}
2015-04-02 20:10:37 -07:00
]
2015-04-18 23:08:02 -07:00
},
{
2015-05-13 18:39:32 -07:00
"pub_key": [1, "3BA1190D54F91EFBF8B0125F7EC116AD4BA2894B6EE38564A5D5FD3230D91F7B"],
"amount": 5000000000,
2015-05-01 17:26:49 -07:00
"unbond_to": [
2015-05-13 18:39:32 -07:00
{
"address": "93E243AC8A01F723DE353A4FA1ED911529CCB6E5",
"amount": 5000000000
}
2015-04-18 23:08:02 -07:00
]
},
{
2015-05-13 18:39:32 -07:00
"pub_key": [1, "E56663353D01C58A1D4CDB4D14B70C2E3335BE1EBB6C3F697AF7882C03837962"],
"amount": 5000000000,
2015-05-01 17:26:49 -07:00
"unbond_to": [
2015-05-13 18:39:32 -07:00
{
"address": "9E54C9ECA9A3FD5D4496696818DA17A9E17F69DA",
"amount": 5000000000
}
2015-04-18 23:08:02 -07:00
]
2015-04-02 20:10:37 -07:00
}
]
}`
2015-03-16 23:34:31 -07:00
2015-05-12 18:29:00 -07:00
// If not defined in the process args nor config file, then use these defaults.
// NOTE: If you change this, maybe also change defaultConfig
2015-03-16 23:16:26 -07:00
func initDefaults(rootDir string) {
app.SetDefault("network", "tendermint_testnet0")
2015-05-12 19:03:05 -07:00
app.SetDefault("version", "0.2.1")
app.SetDefault("genesis_file", rootDir+"/genesis.json")
app.SetDefault("moniker", "anonymous")
app.SetDefault("node_laddr", "0.0.0.0:46656")
app.SetDefault("seeds", "goldenalchemist.chaintest.net:46656")
app.SetDefault("fast_sync", true)
app.SetDefault("addrbook_file", rootDir+"/addrbook.json")
app.SetDefault("priv_validator_file", rootDir+"/priv_validator.json")
app.SetDefault("db_backend", "leveldb")
app.SetDefault("db_dir", rootDir+"/data")
app.SetDefault("log_level", "info")
app.SetDefault("rpc_laddr", "0.0.0.0:46657")
2015-03-16 23:34:31 -07:00
}
2015-03-16 23:16:26 -07:00
func Init(rootDir string) {
2015-03-16 23:34:31 -07:00
// Get rootdir
if rootDir == "" {
rootDir = os.Getenv("TMROOT")
}
if rootDir == "" {
rootDir = os.Getenv("HOME") + "/.tendermint"
}
2015-03-16 23:34:31 -07:00
configFile := path.Join(rootDir, "config.toml")
genesisFile := path.Join(rootDir, "genesis.json")
// Write default config file if missing.
2015-05-12 18:29:00 -07:00
if !fileExists(configFile) {
// Ask user for moniker
moniker := getInput("Type hostname: ", "anonymous")
defaultConfig := strings.Replace(defaultConfigTmpl, "__MONIKER__", moniker, -1)
writeFile(configFile, defaultConfig)
}
if !fileExists(genesisFile) {
writeFile(genesisFile, DefaultGenesis)
}
2015-01-08 16:40:23 -08:00
// Initialize Config
2015-03-16 23:16:26 -07:00
app = confer.NewConfig()
initDefaults(rootDir)
2015-01-08 16:40:23 -08:00
paths := []string{configFile}
2015-03-16 23:16:26 -07:00
if err := app.ReadPaths(paths...); err != nil {
2015-01-08 16:40:23 -08:00
log.Warn("Error reading configuration", "paths", paths, "error", err)
}
2015-01-08 16:40:23 -08:00
// Confused?
2015-04-23 14:59:12 -07:00
//app.Debug()
2015-03-16 23:16:26 -07:00
}
2015-05-12 18:29:00 -07:00
func getInput(prompt string, defaultValue string) string {
fmt.Print(prompt)
reader := bufio.NewReader(os.Stdin)
line, err := reader.ReadString('\n')
if err != nil {
log.Warn("Error reading stdin", "err", err)
return defaultValue
} else {
line = strings.TrimSpace(line)
if line == "" {
return defaultValue
}
return line
}
}
func fileExists(file string) bool {
_, err := os.Stat(file)
return !os.IsNotExist(err)
}
func writeFile(file, contents string) {
if _, err := os.Stat(file); os.IsNotExist(err) {
if strings.Index(file, "/") != -1 {
err := os.MkdirAll(filepath.Dir(file), 0700)
2015-03-16 23:16:26 -07:00
if err != nil {
fmt.Printf("Could not create directory: %v", err)
os.Exit(1)
}
}
2015-05-12 18:29:00 -07:00
err := ioutil.WriteFile(file, []byte(contents), 0600)
2015-03-16 23:16:26 -07:00
if err != nil {
2015-05-12 18:29:00 -07:00
fmt.Printf("Could not write file: %v", err)
2015-03-16 23:16:26 -07:00
os.Exit(1)
}
2015-05-12 18:29:00 -07:00
fmt.Printf("File written to %v.\n", file)
2015-03-16 23:16:26 -07:00
}
}
func ParseFlags(args []string) {
2015-01-08 16:40:23 -08:00
var flags = flag.NewFlagSet("main", flag.ExitOnError)
var printHelp = false
2015-01-08 16:40:23 -08:00
// Declare flags
flags.BoolVar(&printHelp, "help", false, "Print this help message.")
flags.String("moniker", app.GetString("moniker"), "Node Name")
flags.String("node_laddr", app.GetString("node_laddr"), "Node listen address. (0.0.0.0:0 means any interface, any port)")
flags.String("seeds", app.GetString("seeds"), "Comma delimited seed nodes")
flags.Bool("fast_sync", app.GetBool("fast_sync"), "Fast blockchain syncing")
flags.String("rpc_laddr", app.GetString("rpc_laddr"), "RPC listen address. Port required")
flags.String("log_level", app.GetString("log_level"), "Log level")
2015-01-08 16:40:23 -08:00
flags.Parse(args)
if printHelp {
flags.PrintDefaults()
os.Exit(0)
}
2015-01-08 16:40:23 -08:00
2015-03-16 23:16:26 -07:00
// Merge parsed flag values onto app.
app.BindPFlag("moniker", flags.Lookup("moniker"))
app.BindPFlag("node_laddr", flags.Lookup("node_laddr"))
app.BindPFlag("seeds", flags.Lookup("seeds"))
app.BindPFlag("fast_sync", flags.Lookup("fast_sync"))
app.BindPFlag("rpc_laddr", flags.Lookup("rpc_laddr"))
app.BindPFlag("log_level", flags.Lookup("log_level"))
2015-01-08 16:40:23 -08:00
// Confused?
2015-03-16 23:16:26 -07:00
//app.Debug()
}