-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathrun_l1_node.go
561 lines (474 loc) · 13.2 KB
/
run_l1_node.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
package weaveinit
import (
"fmt"
"os"
"path/filepath"
tea "github.com/charmbracelet/bubbletea"
"github.com/initia-labs/weave/utils"
)
type RunL1NodeNetworkSelect struct {
utils.Selector[L1NodeNetworkOption]
state *RunL1NodeState
}
type L1NodeNetworkOption string
const (
Mainnet L1NodeNetworkOption = "Mainnet"
Testnet L1NodeNetworkOption = "Testnet"
Local L1NodeNetworkOption = "Local"
)
func NewRunL1NodeNetworkSelect(state *RunL1NodeState) *RunL1NodeNetworkSelect {
return &RunL1NodeNetworkSelect{
Selector: utils.Selector[L1NodeNetworkOption]{
Options: []L1NodeNetworkOption{
Mainnet,
Testnet,
Local,
},
},
state: state,
}
}
func (m *RunL1NodeNetworkSelect) Init() tea.Cmd {
return nil
}
func (m *RunL1NodeNetworkSelect) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
selected, cmd := m.Select(msg)
if selected != nil {
m.state.network = string(*selected)
switch *selected {
case Mainnet, Testnet:
return NewExistingAppChecker(m.state), utils.DoTick()
case Local:
return NewRunL1NodeVersionInput(m.state), nil
}
return m, tea.Quit
}
return m, cmd
}
func (m *RunL1NodeNetworkSelect) View() string {
view := "? Which network will your node participate in?\n"
for i, option := range m.Options {
if i == m.Cursor {
view += "(■) " + string(option) + "\n"
} else {
view += "( ) " + string(option) + "\n"
}
}
return view + "\nPress Enter to select, or q to quit."
}
type RunL1NodeVersionInput struct {
utils.TextInput
state *RunL1NodeState
}
func NewRunL1NodeVersionInput(state *RunL1NodeState) *RunL1NodeVersionInput {
return &RunL1NodeVersionInput{
TextInput: utils.NewTextInput(),
state: state,
}
}
func (m *RunL1NodeVersionInput) Init() tea.Cmd {
return nil
}
func (m *RunL1NodeVersionInput) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
input, cmd, done := m.TextInput.Update(msg)
if done {
m.state.initiadVersion = input.Text
return NewRunL1NodeChainIdInput(m.state), nil
}
m.TextInput = input
return m, cmd
}
func (m *RunL1NodeVersionInput) View() string {
return fmt.Sprintf("? Please specify the initiad version\n> %s\n", m.TextInput.View())
}
type RunL1NodeChainIdInput struct {
utils.TextInput
state *RunL1NodeState
}
func NewRunL1NodeChainIdInput(state *RunL1NodeState) *RunL1NodeChainIdInput {
return &RunL1NodeChainIdInput{
TextInput: utils.NewTextInput(),
state: state,
}
}
func (m *RunL1NodeChainIdInput) Init() tea.Cmd {
return nil
}
func (m *RunL1NodeChainIdInput) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
input, cmd, done := m.TextInput.Update(msg)
if done {
m.state.chainId = input.Text
return NewRunL1NodeMonikerInput(m.state), nil
}
m.TextInput = input
return m, cmd
}
func (m *RunL1NodeChainIdInput) View() string {
return fmt.Sprintf("? Please specify the chain ID\n> %s\n", m.TextInput.View())
}
type RunL1NodeMonikerInput struct {
utils.TextInput
state *RunL1NodeState
}
func NewRunL1NodeMonikerInput(state *RunL1NodeState) *RunL1NodeMonikerInput {
return &RunL1NodeMonikerInput{
TextInput: utils.NewTextInput(),
state: state,
}
}
func (m *RunL1NodeMonikerInput) Init() tea.Cmd {
return nil
}
func (m *RunL1NodeMonikerInput) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
input, cmd, done := m.TextInput.Update(msg)
if done {
m.state.moniker = input.Text
return NewExistingAppChecker(m.state), utils.DoTick()
}
m.TextInput = input
return m, cmd
}
func (m *RunL1NodeMonikerInput) View() string {
return fmt.Sprintf("? Please specify the moniker\n> %s\n", m.TextInput.View())
}
type ExistingAppChecker struct {
state *RunL1NodeState
}
func NewExistingAppChecker(state *RunL1NodeState) *ExistingAppChecker {
return &ExistingAppChecker{
state: state,
}
}
func (m *ExistingAppChecker) Init() tea.Cmd {
return utils.DoTick()
}
func (m *ExistingAppChecker) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.(type) {
case utils.TickMsg:
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Printf("[error] Failed to get user home directory: %v\n", err)
return m, tea.Quit
}
initiaConfigPath := filepath.Join(homeDir, utils.InitiaConfigDirectory)
appTomlPath := filepath.Join(initiaConfigPath, "app.toml")
configTomlPath := filepath.Join(initiaConfigPath, "config.toml")
if !utils.FileOrFolderExists(configTomlPath) || !utils.FileOrFolderExists(appTomlPath) {
m.state.existingApp = false
return NewMinGasPriceInput(m.state), nil
} else {
m.state.existingApp = true
return NewExistingAppReplaceSelect(m.state), nil
}
default:
return m, nil
}
}
func (m *ExistingAppChecker) View() string {
return "Checking for existing Initia app..."
}
type ExistingAppReplaceSelect struct {
utils.Selector[ExistingAppReplaceOption]
state *RunL1NodeState
}
type ExistingAppReplaceOption string
const (
UseCurrentApp ExistingAppReplaceOption = "Use current files"
ReplaceApp ExistingAppReplaceOption = "Replace"
)
func NewExistingAppReplaceSelect(state *RunL1NodeState) *ExistingAppReplaceSelect {
return &ExistingAppReplaceSelect{
Selector: utils.Selector[ExistingAppReplaceOption]{
Options: []ExistingAppReplaceOption{
UseCurrentApp,
ReplaceApp,
},
},
state: state,
}
}
func (m *ExistingAppReplaceSelect) Init() tea.Cmd {
return nil
}
func (m *ExistingAppReplaceSelect) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
selected, cmd := m.Select(msg)
if selected != nil {
switch *selected {
case UseCurrentApp:
m.state.replaceExistingApp = false
return NewExistingGenesisChecker(m.state), utils.DoTick()
case ReplaceApp:
m.state.replaceExistingApp = true
return NewMinGasPriceInput(m.state), nil
}
return m, tea.Quit
}
return m, cmd
}
func (m *ExistingAppReplaceSelect) View() string {
view := "? Existing config/app.toml and config/config.toml detected. Would you like to use the current files or replace them\n"
for i, option := range m.Options {
if i == m.Cursor {
view += "(■) " + string(option) + "\n"
} else {
view += "( ) " + string(option) + "\n"
}
}
return view + "\nPress Enter to select, or q to quit."
}
type MinGasPriceInput struct {
utils.TextInput
state *RunL1NodeState
}
func NewMinGasPriceInput(state *RunL1NodeState) *MinGasPriceInput {
return &MinGasPriceInput{
TextInput: utils.NewTextInput(),
state: state,
}
}
func (m *MinGasPriceInput) Init() tea.Cmd {
return nil
}
func (m *MinGasPriceInput) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
input, cmd, done := m.TextInput.Update(msg)
if done {
m.state.minGasPrice = input.Text
return NewEnableFeaturesCheckbox(m.state), nil
}
m.TextInput = input
return m, cmd
}
func (m *MinGasPriceInput) View() string {
preText := ""
if !m.state.existingApp {
preText += "i There is no config/app.toml or config/config.toml available. You will need to enter the required information to proceed.\n\n"
}
return fmt.Sprintf("%s? Please specify min-gas-price (uinit)\n> %s\n", preText, m.TextInput.View())
}
type EnableFeaturesCheckbox struct {
utils.CheckBox[EnableFeaturesOption]
state *RunL1NodeState
}
type EnableFeaturesOption string
const (
LCD EnableFeaturesOption = "LCD API"
gRPC EnableFeaturesOption = "gRPC"
)
func NewEnableFeaturesCheckbox(state *RunL1NodeState) *EnableFeaturesCheckbox {
return &EnableFeaturesCheckbox{
CheckBox: *utils.NewCheckBox([]EnableFeaturesOption{LCD, gRPC}),
state: state,
}
}
func (m *EnableFeaturesCheckbox) Init() tea.Cmd {
return nil
}
func (m *EnableFeaturesCheckbox) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cb, cmd, done := m.Select(msg)
if done {
for idx, isSelected := range cb.Selected {
if isSelected {
switch cb.Options[idx] {
case LCD:
m.state.enableLCD = true
case gRPC:
m.state.enableGRPC = true
}
}
}
return NewSeedsInput(m.state), nil
}
return m, cmd
}
func (m *EnableFeaturesCheckbox) View() string {
view := "? Would you like to enable the following options?\n"
view += m.CheckBox.View()
return view + "\nUse arrow-keys. Space to select. Return to submit, or q to quit."
}
type SeedsInput struct {
utils.TextInput
state *RunL1NodeState
}
func NewSeedsInput(state *RunL1NodeState) *SeedsInput {
return &SeedsInput{
TextInput: utils.NewTextInput(),
state: state,
}
}
func (m *SeedsInput) Init() tea.Cmd {
return nil
}
func (m *SeedsInput) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
input, cmd, done := m.TextInput.Update(msg)
if done {
m.state.seeds = input.Text
return NewPersistentPeersInput(m.state), nil
}
m.TextInput = input
return m, cmd
}
func (m *SeedsInput) View() string {
return fmt.Sprintf("? Please specify seeds\n> %s\n", m.TextInput.View())
}
type PersistentPeersInput struct {
utils.TextInput
state *RunL1NodeState
}
func NewPersistentPeersInput(state *RunL1NodeState) *PersistentPeersInput {
return &PersistentPeersInput{
TextInput: utils.NewTextInput(),
state: state,
}
}
func (m *PersistentPeersInput) Init() tea.Cmd {
return nil
}
func (m *PersistentPeersInput) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
input, cmd, done := m.TextInput.Update(msg)
if done {
m.state.persistentPeers = input.Text
return NewExistingGenesisChecker(m.state), utils.DoTick()
}
m.TextInput = input
return m, cmd
}
func (m *PersistentPeersInput) View() string {
return fmt.Sprintf("? Please specify persistent_peers\n> %s\n", m.TextInput.View())
}
type ExistingGenesisChecker struct {
state *RunL1NodeState
}
func NewExistingGenesisChecker(state *RunL1NodeState) *ExistingGenesisChecker {
return &ExistingGenesisChecker{
state: state,
}
}
func (m *ExistingGenesisChecker) Init() tea.Cmd {
return utils.DoTick()
}
func (m *ExistingGenesisChecker) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.(type) {
case utils.TickMsg:
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Printf("[error] Failed to get user home directory: %v\n", err)
return m, tea.Quit
}
initiaConfigPath := filepath.Join(homeDir, utils.InitiaConfigDirectory)
genesisFilePath := filepath.Join(initiaConfigPath, "genesis.json")
if !utils.FileOrFolderExists(genesisFilePath) {
m.state.existingGenesis = false
if m.state.network == string(Local) {
newLoader := NewInitializingAppLoading(m.state)
return newLoader, newLoader.Init()
}
return NewGenesisEndpointInput(m.state), nil
} else {
m.state.existingGenesis = true
return NewExistingGenesisReplaceSelect(m.state), nil
}
default:
return m, nil
}
}
func (m *ExistingGenesisChecker) View() string {
return "Checking for existing Initia genesis file..."
}
type ExistingGenesisReplaceSelect struct {
utils.Selector[ExistingGenesisReplaceOption]
state *RunL1NodeState
}
type ExistingGenesisReplaceOption string
const (
UseCurrentGenesis ExistingGenesisReplaceOption = "Use current one"
ReplaceGenesis ExistingGenesisReplaceOption = "Replace"
)
func NewExistingGenesisReplaceSelect(state *RunL1NodeState) *ExistingGenesisReplaceSelect {
return &ExistingGenesisReplaceSelect{
Selector: utils.Selector[ExistingGenesisReplaceOption]{
Options: []ExistingGenesisReplaceOption{
UseCurrentGenesis,
ReplaceGenesis,
},
},
state: state,
}
}
func (m *ExistingGenesisReplaceSelect) Init() tea.Cmd {
return nil
}
func (m *ExistingGenesisReplaceSelect) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
selected, cmd := m.Select(msg)
if selected != nil {
switch *selected {
case UseCurrentGenesis:
newLoader := NewInitializingAppLoading(m.state)
return newLoader, newLoader.Init()
case ReplaceGenesis:
return NewGenesisEndpointInput(m.state), nil
}
return m, tea.Quit
}
return m, cmd
}
func (m *ExistingGenesisReplaceSelect) View() string {
view := "? Existing config/genesis.json detected. Would you like to use the current one or replace it?\n"
for i, option := range m.Options {
if i == m.Cursor {
view += "(■) " + string(option) + "\n"
} else {
view += "( ) " + string(option) + "\n"
}
}
return view + "\nPress Enter to select, or q to quit."
}
type GenesisEndpointInput struct {
utils.TextInput
state *RunL1NodeState
}
func NewGenesisEndpointInput(state *RunL1NodeState) *GenesisEndpointInput {
return &GenesisEndpointInput{
TextInput: utils.NewTextInput(),
state: state,
}
}
func (m *GenesisEndpointInput) Init() tea.Cmd {
return nil
}
func (m *GenesisEndpointInput) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
input, cmd, done := m.TextInput.Update(msg)
if done {
m.state.genesisEndpoint = input.Text
newLoader := NewInitializingAppLoading(m.state)
return newLoader, newLoader.Init()
}
m.TextInput = input
return m, cmd
}
func (m *GenesisEndpointInput) View() string {
preText := ""
if !m.state.existingGenesis {
preText += "i There is no config/genesis.json available. You will need to enter the required information to proceed.\n\n"
}
return fmt.Sprintf("%s? Please specify the endpoint to fetch genesis.json\n> %s\n", preText, m.TextInput.View())
}
type InitializingAppLoading struct {
utils.Loading
state *RunL1NodeState
}
func NewInitializingAppLoading(state *RunL1NodeState) *InitializingAppLoading {
return &InitializingAppLoading{
Loading: utils.NewLoading("Initializing Initia App..."),
state: state,
}
}
func (m *InitializingAppLoading) Init() tea.Cmd {
return m.Loading.Init()
}
func (m *InitializingAppLoading) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
md, cmd := m.Loading.Update(msg)
return md, cmd
}
func (m *InitializingAppLoading) View() string {
return m.Loading.View()
}