-
Notifications
You must be signed in to change notification settings - Fork 389
/
Copy pathstart.go
397 lines (338 loc) · 9.3 KB
/
start.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package main
import (
"context"
"errors"
"flag"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/gnolang/gno/gno.land/pkg/gnoland"
"github.com/gnolang/gno/gno.land/pkg/log"
"github.com/gnolang/gno/gnovm/pkg/gnoenv"
abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
"github.com/gnolang/gno/tm2/pkg/bft/config"
"github.com/gnolang/gno/tm2/pkg/bft/node"
"github.com/gnolang/gno/tm2/pkg/bft/privval"
"github.com/gnolang/gno/tm2/pkg/bft/state/eventstore/file"
"github.com/gnolang/gno/tm2/pkg/bft/state/eventstore/null"
eventstorecfg "github.com/gnolang/gno/tm2/pkg/bft/state/eventstore/types"
bft "github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/crypto"
osm "github.com/gnolang/gno/tm2/pkg/os"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/gno/tm2/pkg/telemetry"
"go.uber.org/zap/zapcore"
)
var startGraphic = strings.ReplaceAll(`
__ __
___ ____ ___ / /__ ____ ___/ /
/ _ '/ _ \/ _ \_ / / _ '/ _ \/ _ /
\_, /_//_/\___(_)_/\_,_/_//_/\_,_/
/___/
`, "'", "`")
type startCfg struct {
gnoRootDir string
skipFailingGenesisTxs bool
skipStart bool
genesisBalancesFile string
genesisTxsFile string
genesisFile string
chainID string
genesisRemote string
dataDir string
genesisMaxVMCycles int64
config string
txEventStoreType string
txEventStorePath string
nodeConfigPath string
logLevel string
logFormat string
}
func newStartCmd(io commands.IO) *commands.Command {
cfg := &startCfg{}
return commands.NewCommand(
commands.Metadata{
Name: "start",
ShortUsage: "start [flags]",
ShortHelp: "run the full node",
},
cfg,
func(_ context.Context, _ []string) error {
return execStart(cfg, io)
},
)
}
func (c *startCfg) RegisterFlags(fs *flag.FlagSet) {
gnoroot := gnoenv.RootDir()
defaultGenesisBalancesFile := filepath.Join(gnoroot, "gno.land", "genesis", "genesis_balances.txt")
defaultGenesisTxsFile := filepath.Join(gnoroot, "gno.land", "genesis", "genesis_txs.jsonl")
fs.BoolVar(
&c.skipFailingGenesisTxs,
"skip-failing-genesis-txs",
false,
"don't panic when replaying invalid genesis txs",
)
fs.BoolVar(
&c.skipStart,
"skip-start",
false,
"quit after initialization, don't start the node",
)
fs.StringVar(
&c.genesisBalancesFile,
"genesis-balances-file",
defaultGenesisBalancesFile,
"initial distribution file",
)
fs.StringVar(
&c.genesisTxsFile,
"genesis-txs-file",
defaultGenesisTxsFile,
"initial txs to replay",
)
fs.StringVar(
&c.genesisFile,
"genesis",
"genesis.json",
"the path to the genesis.json",
)
fs.StringVar(
&c.chainID,
"chainid",
"dev",
"the ID of the chain",
)
fs.StringVar(
&c.gnoRootDir,
"gnoroot-dir",
gnoroot,
"the root directory of the gno repository",
)
fs.StringVar(
&c.dataDir,
"data-dir",
"gnoland-data",
"the path to the node's data directory",
)
fs.StringVar(
&c.genesisRemote,
"genesis-remote",
"localhost:26657",
"replacement for '%%REMOTE%%' in genesis",
)
fs.Int64Var(
&c.genesisMaxVMCycles,
"genesis-max-vm-cycles",
10_000_000,
"set maximum allowed vm cycles per operation. Zero means no limit.",
)
fs.StringVar(
&c.config,
flagConfigFlag,
"",
"the flag config file (optional)",
)
fs.StringVar(
&c.nodeConfigPath,
"config-path",
"",
"the node TOML config file path (optional)",
)
fs.StringVar(
&c.txEventStoreType,
"tx-event-store-type",
null.EventStoreType,
fmt.Sprintf(
"type of transaction event store [%s]",
strings.Join(
[]string{
null.EventStoreType,
file.EventStoreType,
},
", ",
),
),
)
fs.StringVar(
&c.txEventStorePath,
"tx-event-store-path",
"",
fmt.Sprintf("path for the file tx event store (required if event store is '%s')", file.EventStoreType),
)
fs.StringVar(
&c.logLevel,
"log-level",
zapcore.DebugLevel.String(),
"log level for the gnoland node,",
)
fs.StringVar(
&c.logFormat,
"log-format",
log.ConsoleFormat.String(),
"log format for the gnoland node",
)
}
func execStart(c *startCfg, io commands.IO) error {
// Get the absolute path to the node's data directory
nodeDir, err := filepath.Abs(c.dataDir)
if err != nil {
return fmt.Errorf("unable to get absolute path for data directory, %w", err)
}
// Get the absolute path to the node's genesis.json
genesisPath, err := filepath.Abs(c.genesisFile)
if err != nil {
return fmt.Errorf("unable to get absolute path for the genesis.json, %w", err)
}
var (
cfg *config.Config
loadCfgErr error
)
// Set the node configuration
if c.nodeConfigPath != "" {
// Load the node configuration
// from the specified path
cfg, loadCfgErr = config.LoadConfigFile(c.nodeConfigPath)
} else {
// Load the default node configuration
cfg, loadCfgErr = config.LoadOrMakeConfigWithOptions(nodeDir)
}
if loadCfgErr != nil {
return fmt.Errorf("unable to load node configuration, %w", loadCfgErr)
}
// Initialize the log level
logLevel, err := zapcore.ParseLevel(c.logLevel)
if err != nil {
return fmt.Errorf("unable to parse log level, %w", err)
}
// Initialize the log format
logFormat := log.Format(strings.ToLower(c.logFormat))
// Initialize the zap logger
zapLogger := log.GetZapLoggerFn(logFormat)(io.Out(), logLevel)
// Wrap the zap logger
logger := log.ZapLoggerToSlog(zapLogger)
// Initialize telemetry
telemetry.Init(*cfg.Telemetry)
// Write genesis file if missing.
// NOTE: this will be dropped in a PR that resolves issue #1886:
// https://github.com/gnolang/gno/issues/1886
if !osm.FileExists(genesisPath) {
// Create priv validator first.
// Need it to generate genesis.json
newPrivValKey := cfg.PrivValidatorKeyFile()
newPrivValState := cfg.PrivValidatorStateFile()
priv := privval.LoadOrGenFilePV(newPrivValKey, newPrivValState)
pk := priv.GetPubKey()
// Generate genesis.json file
if err := generateGenesisFile(genesisPath, pk, c); err != nil {
return fmt.Errorf("unable to generate genesis file: %w", err)
}
}
// Initialize the indexer config
txEventStoreCfg, err := getTxEventStoreConfig(c)
if err != nil {
return fmt.Errorf("unable to parse indexer config, %w", err)
}
cfg.TxEventStore = txEventStoreCfg
// Create application and node.
gnoApp, err := gnoland.NewApp(nodeDir, c.skipFailingGenesisTxs, logger, c.genesisMaxVMCycles)
if err != nil {
return fmt.Errorf("error in creating new app: %w", err)
}
cfg.LocalApp = gnoApp
if logFormat != log.JSONFormat {
io.Println(startGraphic)
}
gnoNode, err := node.DefaultNewNode(cfg, genesisPath, logger)
if err != nil {
return fmt.Errorf("error in creating node: %w", err)
}
if c.skipStart {
io.ErrPrintln("'--skip-start' is set. Exiting.")
return nil
}
if err := gnoNode.Start(); err != nil {
return fmt.Errorf("error in start node: %w", err)
}
osm.TrapSignal(func() {
if gnoNode.IsRunning() {
_ = gnoNode.Stop()
}
// Sync the logger before exiting
_ = zapLogger.Sync()
})
// Run forever
select {}
}
func generateGenesisFile(genesisFile string, pk crypto.PubKey, c *startCfg) error {
gen := &bft.GenesisDoc{}
gen.GenesisTime = time.Now()
gen.ChainID = c.chainID
gen.ConsensusParams = abci.ConsensusParams{
Block: &abci.BlockParams{
// TODO: update limits.
MaxTxBytes: 1_000_000, // 1MB,
MaxDataBytes: 2_000_000, // 2MB,
MaxGas: 10_0000_00, // 10M gas
TimeIotaMS: 100, // 100ms
},
}
gen.Validators = []bft.GenesisValidator{
{
Address: pk.Address(),
PubKey: pk,
Power: 10,
Name: "testvalidator",
},
}
// Load balances files
balances, err := gnoland.LoadGenesisBalancesFile(c.genesisBalancesFile)
if err != nil {
return fmt.Errorf("unable to load genesis balances file %q: %w", c.genesisBalancesFile, err)
}
// Load examples folder
examplesDir := filepath.Join(c.gnoRootDir, "examples")
test1 := crypto.MustAddressFromString("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
defaultFee := std.NewFee(50000, std.MustParseCoin("1000000ugnot"))
pkgsTxs, err := gnoland.LoadPackagesFromDir(examplesDir, test1, defaultFee, nil)
if err != nil {
return fmt.Errorf("unable to load examples folder: %w", err)
}
// Load Genesis TXs
genesisTxs, err := gnoland.LoadGenesisTxsFile(c.genesisTxsFile, c.chainID, c.genesisRemote)
if err != nil {
return fmt.Errorf("unable to load genesis txs file: %w", err)
}
genesisTxs = append(pkgsTxs, genesisTxs...)
// Construct genesis AppState.
gen.AppState = gnoland.GnoGenesisState{
Balances: balances,
Txs: genesisTxs,
}
// Write genesis state
if err := gen.SaveAs(genesisFile); err != nil {
return fmt.Errorf("unable to write genesis file %q: %w", genesisFile, err)
}
return nil
}
// getTxEventStoreConfig constructs an event store config from provided user options
func getTxEventStoreConfig(c *startCfg) (*eventstorecfg.Config, error) {
var cfg *eventstorecfg.Config
switch c.txEventStoreType {
case file.EventStoreType:
if c.txEventStorePath == "" {
return nil, errors.New("unspecified file transaction indexer path")
}
// Fill out the configuration
cfg = &eventstorecfg.Config{
EventStoreType: file.EventStoreType,
Params: map[string]any{
file.Path: c.txEventStorePath,
},
}
default:
cfg = eventstorecfg.DefaultEventStoreConfig()
}
return cfg, nil
}