-
Notifications
You must be signed in to change notification settings - Fork 388
/
start.go
339 lines (290 loc) · 7.91 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
package main
import (
"context"
"errors"
"flag"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/gnolang/gno/gno.land/pkg/gnoland"
"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"
"github.com/gnolang/gno/tm2/pkg/log"
osm "github.com/gnolang/gno/tm2/pkg/os"
"github.com/gnolang/gno/tm2/pkg/std"
)
type startCfg struct {
gnoRootDir string
skipFailingGenesisTxs bool
skipStart bool
genesisBalancesFile string
genesisTxsFile string
chainID string
genesisRemote string
dataDir string
genesisMaxVMCycles int64
config string
txEventStoreType string
txEventStorePath string
nodeConfigPath 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.txt")
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.chainID,
"chainid",
"dev",
"the ID of the chain",
)
fs.StringVar(
&c.gnoRootDir,
"gnoroot-dir",
gnoroot,
"the root directory of the gno repository",
)
// XXX: Use home directory for this
fs.StringVar(
&c.dataDir,
"data-dir",
"testdir",
"directory for config and data",
)
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,
"config",
"",
"the flag config file (optional)",
)
fs.StringVar(
&c.nodeConfigPath,
"tm2-node-config",
"",
"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),
)
// XXX(deprecated): use data-dir instead
fs.StringVar(
&c.dataDir,
"root-dir",
"testdir",
"deprecated: use data-dir instead - directory for config and data",
)
}
func execStart(c *startCfg, io commands.IO) error {
logger := log.NewTMLogger(log.NewSyncWriter(io.Out()))
dataDir := c.dataDir
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(dataDir, nil)
}
if loadCfgErr != nil {
return fmt.Errorf("unable to load node configuration, %w", loadCfgErr)
}
// Write genesis file if missing.
genesisFilePath := filepath.Join(dataDir, cfg.Genesis)
if !osm.FileExists(genesisFilePath) {
// 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(genesisFilePath, 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(dataDir, c.skipFailingGenesisTxs, logger, c.genesisMaxVMCycles)
if err != nil {
return fmt.Errorf("error in creating new app: %w", err)
}
cfg.LocalApp = gnoApp
gnoNode, err := node.DefaultNewNode(cfg, logger)
if err != nil {
return fmt.Errorf("error in creating node: %w", err)
}
fmt.Fprintln(io.Err(), "Node created.")
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()
}
})
// 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
}