-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (68 loc) · 1.86 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/spf13/viper"
abciclient "github.com/tendermint/tendermint/abci/client"
abcitypes "github.com/tendermint/tendermint/abci/types"
tmconfig "github.com/tendermint/tendermint/config"
tmlog "github.com/tendermint/tendermint/libs/log"
tmservice "github.com/tendermint/tendermint/libs/service"
tmnode "github.com/tendermint/tendermint/node"
)
var configFile string
func init() {
flag.StringVar(&configFile, "config", "./tmhome/config/config.toml", "Path to config.toml")
}
func main() {
app := NewKVStoreApplication()
flag.Parse()
node, err := newTendermint(app, configFile)
if err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(2)
}
node.Start()
defer func() {
node.Stop()
node.Wait()
}()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
}
func newTendermint(app abcitypes.Application, configFile string) (tmservice.Service, error) {
// read config
config := tmconfig.DefaultValidatorConfig()
config.SetRoot(filepath.Dir(filepath.Dir(configFile)))
viper.SetConfigFile(configFile)
if err := viper.ReadInConfig(); err != nil {
return nil, fmt.Errorf("viper failed to read config file: %w", err)
}
if err := viper.Unmarshal(config); err != nil {
return nil, fmt.Errorf("viper failed to unmarshal config: %w", err)
}
if err := config.ValidateBasic(); err != nil {
return nil, fmt.Errorf("config is invalid: %w", err)
}
// create logger
logger, err := tmlog.NewDefaultLogger(tmlog.LogFormatPlain, config.LogLevel, false)
if err != nil {
return nil, fmt.Errorf("failed to create logger: %w", err)
}
// create node
node, err := tmnode.New(
config,
logger,
abciclient.NewLocalCreator(app),
nil,
)
if err != nil {
return nil, fmt.Errorf("failed to create new Tendermint node: %w", err)
}
return node, nil
}