-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
143 lines (119 loc) · 3.32 KB
/
main.go
File metadata and controls
143 lines (119 loc) · 3.32 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"context"
"flag"
"fmt"
"log/slog"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/cockroachdb/pebble"
"github.com/cometbft/cometbft/v2/privval"
"github.com/cometbft/cometbft/v2/proxy"
"golang.org/x/sync/errgroup"
cfg "github.com/cometbft/cometbft/v2/config"
cmtflags "github.com/cometbft/cometbft/v2/libs/cli/flags"
cmtlog "github.com/cometbft/cometbft/v2/libs/log"
cmtnode "github.com/cometbft/cometbft/v2/node"
node "github.com/meterio/supernova/node"
"github.com/meterio/supernova/types"
"github.com/spf13/viper"
)
var homeDir string
func init() {
flag.StringVar(&homeDir, "cmt-home", "", "Path to the CometBFT config directory (if empty, uses $HOME/.supernova)")
}
var (
logger = cmtlog.NewLogger(os.Stdout).With("module", "priv_val")
)
func main() {
flag.Parse()
if homeDir == "" {
homeDir = os.ExpandEnv("$HOME/.supernova")
}
config := cfg.DefaultConfig()
config.SetRoot(homeDir)
viper.SetConfigFile(fmt.Sprintf("%s/%s", homeDir, "config/config.toml"))
if err := viper.ReadInConfig(); err != nil {
slog.Error("Reading config", "err", err)
}
if err := viper.Unmarshal(config); err != nil {
slog.Error("Decoding config", "err", err)
}
if err := config.ValidateBasic(); err != nil {
slog.Error("Invalid configuration data", "err", err)
}
dbPath := filepath.Join(homeDir, "badger")
db, err := pebble.Open(dbPath, &pebble.Options{})
if err != nil {
slog.Error("Opening database", "err", err)
}
defer func() {
if err := db.Close(); err != nil {
slog.Error("Closing database", "err", err)
}
}()
app := NewKVStoreApplication(db)
pv := privval.LoadFilePV(
config.PrivValidatorKeyFile(),
config.PrivValidatorStateFile(),
)
nodeKey, err := types.LoadNodeKey(config.NodeKeyFile())
if err != nil {
slog.Error("failed to load node's key", "nodeKeyFile", config.NodeKeyFile(), "err", err)
}
logger := cmtlog.NewLogger(os.Stdout)
logger, err = cmtflags.ParseLogLevel(config.LogLevel, logger, cfg.DefaultLogLevel)
if err != nil {
slog.Error("failed to parse log level", "err", err)
}
ctx, cancelFn := context.WithCancel(context.TODO())
// config.LogLevel = "debug" // default is info
node, err := node.NewNode(
ctx,
config,
pv,
nodeKey,
proxy.NewLocalClientCreator(app),
cmtnode.DefaultGenesisDocProviderFunc(config),
cfg.DefaultDBProvider,
cmtnode.DefaultMetricsProvider(config.Instrumentation),
logger,
)
if err != nil {
slog.Error("Creating node", "err", err)
}
node.Start()
defer func() {
cancelFn()
// node.Stop()
// node.Wait()
}()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
}
// ListenForQuitSignals listens for SIGINT and SIGTERM. When a signal is received,
// the cleanup function is called, indicating the caller can gracefully exit or
// return.
//
// Note, the blocking behavior of this depends on the block argument.
// The caller must ensure the corresponding context derived from the cancelFn is used correctly.
func ListenForQuitSignals(g *errgroup.Group, block bool, cancelFn context.CancelFunc, logger slog.Logger) {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
f := func() {
sig := <-sigCh
cancelFn()
logger.Info("caught signal", "signal", sig.String())
}
if block {
g.Go(func() error {
f()
return nil
})
} else {
go f()
}
}