-
Notifications
You must be signed in to change notification settings - Fork 57
/
cometconfig.go
121 lines (97 loc) · 3.67 KB
/
cometconfig.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package cmd
import (
"bytes"
"context"
"os"
"path/filepath"
"testing"
"time"
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/log"
cfg "github.com/cometbft/cometbft/config"
"github.com/spf13/viper"
)
//nolint:gochecknoglobals // Overrides cometbft default moniker for testing.
var testMoniker string
// setMonikerForT sets the test moniker for the duration of the test.
// This is required for deterministic default cometbft config.
func setMonikerForT(t *testing.T) {
t.Helper()
testMoniker = "testmoniker"
t.Cleanup(func() {
testMoniker = ""
})
}
// DefaultCometConfig returns the default cometBFT config.
func DefaultCometConfig(homeDir string) cfg.Config {
conf := cfg.DefaultConfig()
if testMoniker != "" {
conf.Moniker = testMoniker
}
conf.RootDir = homeDir
conf.SetRoot(conf.RootDir)
conf.LogLevel = "error" // Decrease default comet log level, it is super noisy.
conf.TxIndex = &cfg.TxIndexConfig{Indexer: "null"} // Disable tx indexing.
conf.StateSync.DiscoveryTime = time.Second * 10 // Increase discovery time
conf.StateSync.ChunkRequestTimeout = time.Minute // Increase timeout
conf.Mempool.Type = cfg.MempoolTypeNop // Disable cometBFT mempool
conf.ProxyApp = "" // Only support built-in ABCI app supported.
conf.ABCI = "" // Only support built-in ABCI app supported.
conf.Consensus.TimeoutPropose = time.Second // Mitigate slow blocks when proposer inactive (default=3s).
conf.RPC.ListenAddress = "tcp://0.0.0.0:26657" // Halo always run inside docker
return *conf
}
// WriteCometConfig writes the cometBFT config to disk.
// TODO(corevr): Remove this once mempool.type issue is fixed upstream.
func WriteCometConfig(path string, config *cfg.Config) (err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New("failed writing comet config", "err", r)
}
}()
cfg.WriteConfigFile(path, config) // This panics on error
bz, err := os.ReadFile(path)
if err != nil {
return errors.Wrap(err, "read comet config")
}
// Workaround for issue: https://github.com/cometbft/cometbft/pull/4281
bz = bytes.ReplaceAll(bz, []byte(`"flood"`), []byte(`"nop"`))
if err := os.WriteFile(path, bz, 0o644); err != nil {
return errors.Wrap(err, "update comet config")
}
return nil
}
// parseCometConfig parses the cometBFT config from disk and verifies it.
func parseCometConfig(ctx context.Context, homeDir string) (cfg.Config, error) {
const (
file = "config" // CometBFT config files are named config.toml
dir = "config" // CometBFT config files are stored in the config directory
)
v := viper.New()
v.SetConfigName(file)
v.AddConfigPath(filepath.Join(homeDir, dir))
// Attempt to read the cometBFT config file, gracefully ignoring errors
// caused by a config file not being found. Return an error
// if we cannot parse the config file.
if err := v.ReadInConfig(); err != nil {
// It's okay if there isn't a config file
var cfgError viper.ConfigFileNotFoundError
if ok := errors.As(err, &cfgError); !ok {
return cfg.Config{}, errors.Wrap(err, "read comet config")
}
log.Warn(ctx, "No comet config.toml file found, using default config", nil)
}
conf := DefaultCometConfig(homeDir)
if err := v.Unmarshal(&conf); err != nil {
return cfg.Config{}, errors.Wrap(err, "unmarshal comet config")
}
if err := conf.ValidateBasic(); err != nil {
return cfg.Config{}, errors.Wrap(err, "validate comet config")
}
if warnings := conf.CheckDeprecated(); len(warnings) > 0 {
for _, warning := range warnings {
log.Info(ctx, "Deprecated CometBFT config", "usage", warning)
}
}
return conf, nil
}