-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
78 lines (63 loc) · 2.07 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package config
import (
"errors"
"os"
"path/filepath"
"time"
"github.com/spf13/viper"
cfg "github.com/tendermint/tendermint/config"
"github.com/ci123chain/ci123chain/pkg/util"
)
var ErrConfigNotFound = errors.New("config not found")
var ErrGetConfig = errors.New("config not found")
//_____________________________________________________________________
// Configuration structure for command functions that share configuration.
// For example: init, init gen-tx and testnet commands need similar input and run the same code
// Storage for init gen-tx command input parameters
type GenTx struct {
Name string
CliRoot string
Overwrite bool
IP string
}
func SaveConfig(c *cfg.Config) {
configDir := filepath.Join(c.RootDir, "config")
configFilePath := filepath.Join(configDir, "config.toml")
c.Instrumentation.Prometheus = true
c.Consensus.TimeoutPropose = 3 * time.Second
c.Consensus.TimeoutCommit = 3 * time.Second
c.RPC.ListenAddress = "tcp://0.0.0.0:26657"
cfg.EnsureRoot(c.RootDir)
cfg.WriteConfigFile(configFilePath, c)
}
func CreateConfig(moniker, root string) (*cfg.Config, error) {
c := cfg.DefaultConfig()
c.SetRoot(root)
c.Moniker = moniker
c.P2P.RecvRate = 5120000
c.P2P.SendRate = 5120000
//c.Consensus.TimeoutCommit = 3 * time.Second
c.TxIndex.Indexer = "kv"
return c, unmarshalWithViper(viper.GetViper(), c)
}
func GetConfig(root string) (*cfg.Config, error) {
configDir := filepath.Join(root, "config")
configFilePath := filepath.Join(configDir, "config.toml")
if _, err := os.Stat(configFilePath); err != nil && !os.IsExist(err) {
return nil, ErrConfigNotFound
}
viper.SetConfigFile(configFilePath)
err := viper.ReadInConfig()
c := new(cfg.Config)
err = unmarshalWithViper(viper.GetViper(), c)
return c, err
}
func unmarshalWithViper(vp *viper.Viper, c *cfg.Config) error {
// you can configure tedermint params via environment variables.
// TM_PARAMS="consensus.timeout_commit=3000,instrumentation.prometheus=true" ./liamd start
util.SetEnvToViper(vp, "TM_PARAMS")
if err := vp.Unmarshal(c); err != nil {
return err
}
return nil
}