Skip to content

Commit

Permalink
Merge pull request #103 from algorandfoundation/chore/bootstrap-offer…
Browse files Browse the repository at this point in the history
…-fast-catchup-conditionally

chore: skip questions when already bootstrapped
  • Loading branch information
PhearZero authored Jan 21, 2025
2 parents a808fe2 + 96999f4 commit 84b2494
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 89 deletions.
82 changes: 31 additions & 51 deletions cmd/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ import (
"time"

"github.com/algorandfoundation/nodekit/api"
cmdutils "github.com/algorandfoundation/nodekit/cmd/utils"
"github.com/algorandfoundation/nodekit/cmd/utils/explanations"
"github.com/algorandfoundation/nodekit/internal/algod"
"github.com/algorandfoundation/nodekit/internal/algod/utils"
"github.com/algorandfoundation/nodekit/internal/system"
"github.com/algorandfoundation/nodekit/ui"
"github.com/algorandfoundation/nodekit/ui/app"
"github.com/algorandfoundation/nodekit/ui/bootstrap"
"github.com/algorandfoundation/nodekit/ui/style"
Expand Down Expand Up @@ -63,8 +60,20 @@ var bootstrapCmd = &cobra.Command{
log.Fatal("invalid state, exiting")
}

ctx := context.Background()
httpPkg := new(api.HttpPkg)
// Just launch the TUI if it's already running
if algod.IsInstalled() && algod.IsService() && algod.IsRunning() {
dir, err := algod.GetDataDir("")
if err != nil {
log.Fatal(err)
}
err = runTUI(RootCmd, dir, false)
if err != nil {
log.Fatal(err)
}
return nil
}

// Render the welcome text
r, _ := glamour.NewTermRenderer(
glamour.WithAutoStyle(),
)
Expand All @@ -75,6 +84,7 @@ var bootstrapCmd = &cobra.Command{
}
fmt.Println(out)

// Create the Bootstrap TUI
model := bootstrap.NewModel()
if algod.IsInstalled() {
model.BootstrapMsg.Install = false
Expand All @@ -92,15 +102,16 @@ var bootstrapCmd = &cobra.Command{
}
}
}()

if _, err := p.Run(); err != nil {
log.Fatal(err)
}

// If the pointer is empty, return (should not happen)
if msg == nil {
return nil
}

// User Answer for Install Question
if msg.Install {
log.Warn(style.Yellow.Render(explanations.SudoWarningMsg))

Expand Down Expand Up @@ -128,17 +139,22 @@ var bootstrapCmd = &cobra.Command{
}
}

// Find the data directory automatically
dataDir, err := algod.GetDataDir("")
if err != nil {
return err
}
// Create the client
client, err := algod.GetClient(dataDir)
if err != nil {
return err
}

// User answer for catchup question
if msg.Catchup {
ctx := context.Background()
httpPkg := new(api.HttpPkg)

if err != nil {
return err
}
// Create the client
client, err := algod.GetClient(dataDir)
if err != nil {
return err
}
network, err := utils.GetNetworkFromDataDir(dataDir)
if err != nil {
return err
Expand All @@ -160,42 +176,6 @@ var bootstrapCmd = &cobra.Command{

}

t := new(system.Clock)
// Fetch the state and handle any creation errors
state, stateResponse, err := algod.NewStateModel(ctx, client, httpPkg, false)
cmdutils.WithInvalidResponsesExplanations(err, stateResponse, cmd.UsageString())
cobra.CheckErr(err)

// Construct the TUI Model from the State
m, err := ui.NewViewportViewModel(state, client)
cobra.CheckErr(err)

// Construct the TUI Application
p = tea.NewProgram(
m,
tea.WithAltScreen(),
tea.WithFPS(120),
)

// Watch for State Updates on a separate thread
// TODO: refactor into context aware watcher without callbacks
go func() {
state.Watch(func(status *algod.StateModel, err error) {
if err == nil {
p.Send(state)
}
if err != nil {
p.Send(state)
p.Send(err)
}
}, ctx, t)
}()

// Execute the TUI Application
_, err = p.Run()
if err != nil {
log.Fatal(err)
}
return nil
return runTUI(RootCmd, dataDir, false)
},
}
85 changes: 47 additions & 38 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,44 +56,7 @@ var (
},
Run: func(cmd *cobra.Command, args []string) {
log.SetOutput(cmd.OutOrStdout())
// Create the dependencies
ctx := context.Background()
client, err := algod.GetClient(algodData)
cobra.CheckErr(err)
httpPkg := new(api.HttpPkg)
t := new(system.Clock)
// Fetch the state and handle any creation errors
state, stateResponse, err := algod.NewStateModel(ctx, client, httpPkg, IncentivesDisabled)
utils.WithInvalidResponsesExplanations(err, stateResponse, cmd.UsageString())
cobra.CheckErr(err)

// Construct the TUI Model from the State
m, err := ui.NewViewportViewModel(state, client)
cobra.CheckErr(err)

// Construct the TUI Application
p := tea.NewProgram(
m,
tea.WithAltScreen(),
tea.WithFPS(120),
)

// Watch for State Updates on a separate thread
// TODO: refactor into context aware watcher without callbacks
go func() {
state.Watch(func(status *algod.StateModel, err error) {
if err == nil {
p.Send(state)
}
if err != nil {
p.Send(state)
p.Send(err)
}
}, ctx, t)
}()

// Execute the TUI Application
_, err = p.Run()
err := runTUI(cmd, algodData, IncentivesDisabled)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -152,3 +115,49 @@ func Execute(version string, needsUpgrade bool) error {
NeedsUpgrade = needsUpgrade
return RootCmd.Execute()
}

func runTUI(cmd *cobra.Command, dataDir string, incentivesFlag bool) error {
if cmd == nil {
return fmt.Errorf("cmd is nil")
}
// Create the dependencies
ctx := context.Background()
httpPkg := new(api.HttpPkg)
t := new(system.Clock)
client, err := algod.GetClient(dataDir)
cobra.CheckErr(err)

// Fetch the state and handle any creation errors
state, stateResponse, err := algod.NewStateModel(ctx, client, httpPkg, incentivesFlag)
utils.WithInvalidResponsesExplanations(err, stateResponse, cmd.UsageString())
cobra.CheckErr(err)

// Construct the TUI Model from the State
m, err := ui.NewViewportViewModel(state, client)
cobra.CheckErr(err)

// Construct the TUI Application
p := tea.NewProgram(
m,
tea.WithAltScreen(),
tea.WithFPS(120),
)

// Watch for State Updates on a separate thread
// TODO: refactor into context aware watcher without callbacks
go func() {
state.Watch(func(status *algod.StateModel, err error) {
if err == nil {
p.Send(state)
}
if err != nil {
p.Send(state)
p.Send(err)
}
}, ctx, t)
}()

// Execute the TUI Application
_, err = p.Run()
return err
}

0 comments on commit 84b2494

Please sign in to comment.