-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
202 lines (167 loc) · 4.91 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/streamingfast/dummy-blockchain/core"
"github.com/streamingfast/dummy-blockchain/tracer"
)
type Flags struct {
GenesisHeight uint64
GenesisTimeRaw string
GenesisBlockBurst uint64
LogLevel string
StoreDir string
BlockRate int
ServerAddr string
Tracer string
StopHeight uint64
}
func (f *Flags) GenesisTime() (time.Time, error) {
if f.GenesisTimeRaw == "" {
return time.Now(), nil
}
genesisTime, err := time.Parse(time.RFC3339, f.GenesisTimeRaw)
if err != nil {
return time.Time{}, fmt.Errorf("parse %q: %w", f.GenesisTimeRaw, err)
}
return genesisTime, nil
}
var cliOpts Flags
func main() {
root := &cobra.Command{
Use: "dummy-blockchain",
Short: "CLI for the Dummy Chain",
Version: VersionString(),
}
root.SetOutput(os.Stderr)
if err := initFlags(root); err != nil {
logrus.Fatal(err)
}
if err := initLogger(); err != nil {
logrus.Fatal(err)
}
root.AddCommand(
makeInitCommand(),
makeResetCommand(),
makeStartComand(),
)
root.Execute()
}
func initFlags(root *cobra.Command) error {
flags := root.PersistentFlags()
flags.Uint64Var(&cliOpts.GenesisHeight, "genesis-height", 1, "Blockchain genesis height")
flags.StringVar(&cliOpts.GenesisTimeRaw, "genesis-time", "", "Blockchain genesis time in RFC3339 time format, leave empty for current time")
flags.Uint64Var(&cliOpts.GenesisBlockBurst, "genesis-block-burst", 0, "The amount of block to produce when initially starting from genesis block")
flags.StringVar(&cliOpts.LogLevel, "log-level", "info", "Logging level")
flags.StringVar(&cliOpts.StoreDir, "store-dir", "./data", "Directory for storing blockchain state")
flags.IntVar(&cliOpts.BlockRate, "block-rate", 60, "Block production rate (per minute)")
flags.Uint64Var(&cliOpts.StopHeight, "stop-height", 0, "Stop block production at this height")
flags.StringVar(&cliOpts.ServerAddr, "server-addr", "0.0.0.0:8080", "Server address")
flags.StringVar(&cliOpts.Tracer, "tracer", "", "The tracer to use, either <empty>, none or firehose")
return nil
}
func initLogger() error {
level, err := logrus.ParseLevel(cliOpts.LogLevel)
if err != nil {
return err
}
logrus.SetLevel(level)
logrus.SetOutput(os.Stderr)
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
return nil
}
func makeInitCommand() *cobra.Command {
return &cobra.Command{
Use: "init",
Short: "Initialize local blockchain state",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
genesisTime, err := cliOpts.GenesisTime()
if err != nil {
return fmt.Errorf("get genesis time: %w", err)
}
logrus.
WithField("dir", cliOpts.StoreDir).
WithField("genesis_height", cliOpts.GenesisHeight).
WithField("genesis_time", genesisTime).
Info("initializing chain store")
store := core.NewStore(cliOpts.StoreDir, cliOpts.GenesisHeight, genesisTime)
return store.Initialize()
},
}
}
func makeResetCommand() *cobra.Command {
return &cobra.Command{
Use: "reset",
Short: "Reset local blockchain state",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
logrus.WithField("dir", cliOpts.StoreDir).Info("removing chain store")
err := os.RemoveAll(cliOpts.StoreDir)
if err != nil {
logrus.WithError(err).Error("cant remove the chain store directory")
}
return err
},
}
}
func makeStartComand() *cobra.Command {
return &cobra.Command{
Use: "start",
Short: "Start blockchain service",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if cliOpts.BlockRate < 1 {
return errors.New("block rate option must be greater than 1")
}
genesisTime, err := cliOpts.GenesisTime()
if err != nil {
return fmt.Errorf("get genesis time: %w", err)
}
var blockTracer tracer.Tracer
if cliOpts.Tracer == "firehose" {
blockTracer = &tracer.FirehoseTracer{}
}
node := core.NewNode(
cliOpts.StoreDir,
cliOpts.BlockRate,
cliOpts.GenesisHeight,
genesisTime,
cliOpts.GenesisBlockBurst,
cliOpts.StopHeight,
cliOpts.ServerAddr,
blockTracer,
)
if err := node.Initialize(); err != nil {
logrus.WithError(err).Fatal("node failed to initialize")
return err
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
sig := waitForSignal()
logrus.WithField("signal", sig).Info("shutting down")
cancel()
}()
if err := node.Start(ctx); err != nil {
logrus.WithError(err).Fatal("node terminated with error")
} else {
logrus.Info("node terminated")
}
return nil
},
}
}
func waitForSignal() os.Signal {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGTERM)
signal.Notify(sig, syscall.SIGINT)
return <-sig
}