-
Notifications
You must be signed in to change notification settings - Fork 375
/
start.go
319 lines (272 loc) · 7.14 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
package main
import (
"context"
"flag"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/gnolang/gno/gno.land/pkg/gnoland"
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/pkg/gnomod"
"github.com/gnolang/gno/tm2/pkg/amino"
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"
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"
vmm "github.com/gnolang/gno/tm2/pkg/sdk/vm"
"github.com/gnolang/gno/tm2/pkg/std"
)
type startCfg struct {
skipFailingGenesisTxs bool
skipStart bool
genesisBalancesFile string
genesisTxsFile string
chainID string
genesisRemote string
rootDir string
genesisMaxVMCycles int64
config 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, args []string) error {
return execStart(cfg, args, io)
},
)
}
func (c *startCfg) RegisterFlags(fs *flag.FlagSet) {
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",
"./genesis/genesis_balances.txt",
"initial distribution file",
)
fs.StringVar(
&c.genesisTxsFile,
"genesis-txs-file",
"./genesis/genesis_txs.txt",
"initial txs to replay",
)
fs.StringVar(
&c.chainID,
"chainid",
"dev",
"the ID of the chain",
)
fs.StringVar(
&c.rootDir,
"root-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",
"",
"config file (optional)",
)
}
func execStart(c *startCfg, args []string, io *commands.IO) error {
logger := log.NewTMLogger(log.NewSyncWriter(io.Out))
rootDir := c.rootDir
cfg := config.LoadOrMakeConfigWithOptions(rootDir, func(cfg *config.Config) {
cfg.Consensus.CreateEmptyBlocks = true
cfg.Consensus.CreateEmptyBlocksInterval = 0 * time.Second
})
// create priv validator first.
// need it to generate genesis.json
newPrivValKey := cfg.PrivValidatorKeyFile()
newPrivValState := cfg.PrivValidatorStateFile()
priv := privval.LoadOrGenFilePV(newPrivValKey, newPrivValState)
// write genesis file if missing.
genesisFilePath := filepath.Join(rootDir, cfg.Genesis)
if !osm.FileExists(genesisFilePath) {
genDoc := makeGenesisDoc(
priv.GetPubKey(),
c.chainID,
c.genesisBalancesFile,
loadGenesisTxs(c.genesisTxsFile, c.chainID, c.genesisRemote),
)
writeGenesisFile(genDoc, genesisFilePath)
}
// create application and node.
gnoApp, err := gnoland.NewApp(rootDir, 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 {
fmt.Fprintln(io.Err, "'--skip-start' is set. Exiting.")
return nil
}
if err := gnoNode.Start(); err != nil {
return fmt.Errorf("error in start node: %w", err)
}
// run forever
osm.TrapSignal(func() {
if gnoNode.IsRunning() {
_ = gnoNode.Stop()
}
})
select {} // run forever
}
// Makes a local test genesis doc with local privValidator.
func makeGenesisDoc(
pvPub crypto.PubKey,
chainID string,
genesisBalancesFile string,
genesisTxs []std.Tx,
) *bft.GenesisDoc {
gen := &bft.GenesisDoc{}
gen.GenesisTime = time.Now()
gen.ChainID = chainID
gen.ConsensusParams = abci.ConsensusParams{
Block: &abci.BlockParams{
// TODO: update limits.
MaxTxBytes: 1000000, // 1MB,
MaxDataBytes: 2000000, // 2MB,
MaxGas: 10000000, // 10M gas
TimeIotaMS: 100, // 100ms
},
}
gen.Validators = []bft.GenesisValidator{
{
Address: pvPub.Address(),
PubKey: pvPub,
Power: 10,
Name: "testvalidator",
},
}
// Load distribution.
balances := loadGenesisBalances(genesisBalancesFile)
// debug: for _, balance := range balances { fmt.Println(balance) }
// Load initial packages from examples.
test1 := crypto.MustAddressFromString("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
txs := []std.Tx{}
// List initial packages to load from examples.
pkgs, err := gnomod.ListPkgs(filepath.Join("..", "examples"))
if err != nil {
panic(fmt.Errorf("listing gno packages: %w", err))
}
// Sort packages by dependencies.
sortedPkgs, err := pkgs.Sort()
if err != nil {
panic(fmt.Errorf("sorting packages: %w", err))
}
// Filter out draft packages.
nonDraftPkgs := sortedPkgs.GetNonDraftPkgs()
for _, pkg := range nonDraftPkgs {
// open files in directory as MemPackage.
memPkg := gno.ReadMemPackage(pkg.Path(), pkg.Name())
var tx std.Tx
tx.Msgs = []std.Msg{
vmm.MsgAddPackage{
Creator: test1,
Package: memPkg,
Deposit: nil,
},
}
tx.Fee = std.NewFee(50000, std.MustParseCoin("1000000ugnot"))
tx.Signatures = make([]std.Signature, len(tx.GetSigners()))
txs = append(txs, tx)
}
// load genesis txs from file.
txs = append(txs, genesisTxs...)
// construct genesis AppState.
gen.AppState = gnoland.GnoGenesisState{
Balances: balances,
Txs: txs,
}
return gen
}
func writeGenesisFile(gen *bft.GenesisDoc, filePath string) {
err := gen.SaveAs(filePath)
if err != nil {
panic(err)
}
}
func loadGenesisTxs(
path string,
chainID string,
genesisRemote string,
) []std.Tx {
txs := []std.Tx{}
txsBz := osm.MustReadFile(path)
txsLines := strings.Split(string(txsBz), "\n")
for _, txLine := range txsLines {
if txLine == "" {
continue // skip empty line
}
// patch the TX
txLine = strings.ReplaceAll(txLine, "%%CHAINID%%", chainID)
txLine = strings.ReplaceAll(txLine, "%%REMOTE%%", genesisRemote)
var tx std.Tx
amino.MustUnmarshalJSON([]byte(txLine), &tx)
txs = append(txs, tx)
}
return txs
}
func loadGenesisBalances(path string) []string {
// each balance is in the form: g1xxxxxxxxxxxxxxxx=100000ugnot
balances := []string{}
content := osm.MustReadFile(path)
lines := strings.Split(string(content), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
// remove comments.
line = strings.Split(line, "#")[0]
line = strings.TrimSpace(line)
// skip empty lines.
if line == "" {
continue
}
parts := strings.Split(line, "=")
if len(parts) != 2 {
panic("invalid genesis_balance line: " + line)
}
balances = append(balances, line)
}
return balances
}