diff --git a/cmd/root.go b/cmd/root.go index 6861bff9..6ad5f43c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,10 +3,6 @@ package cmd import ( "context" "encoding/json" - "io" - "os" - "strings" - "github.com/algorandfoundation/hack-tui/api" "github.com/algorandfoundation/hack-tui/internal" "github.com/algorandfoundation/hack-tui/ui" @@ -15,6 +11,9 @@ import ( "github.com/oapi-codegen/oapi-codegen/v2/pkg/securityprovider" "github.com/spf13/cobra" "github.com/spf13/viper" + "io" + "os" + "strings" ) const BANNER = ` @@ -43,6 +42,7 @@ var ( cobra.CheckErr(err) partkeys, err := internal.GetPartKeys(context.Background(), client) + cobra.CheckErr(err) state := internal.StateModel{ Status: internal.StatusModel{ @@ -80,6 +80,7 @@ var ( p.Send(state) } if err != nil { + p.Send(state) p.Send(err) } }, context.Background(), client) diff --git a/cmd/root_test.go b/cmd/root_test.go index 2f9cda89..0b7b8b7e 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -8,7 +8,8 @@ import ( // Test the stub root command func Test_ExecuteRootCommand(t *testing.T) { - viper.Set("server", "https://mainnet-api.4160.nodely.dev:443") + viper.Set("token", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") + viper.Set("server", "http://localhost:8080") // Execute err := rootCmd.Execute() diff --git a/internal/block.go b/internal/block.go index d5b7e2ea..7f839a4e 100644 --- a/internal/block.go +++ b/internal/block.go @@ -18,7 +18,7 @@ func GetBlock(ctx context.Context, client *api.ClientWithResponses, round uint64 } if block.StatusCode() != 200 { - return nil, errors.New("invalid status code") + return nil, errors.New(block.Status()) } return block.JSON200.Block, nil @@ -44,12 +44,19 @@ func GetBlockMetrics(ctx context.Context, client *api.ClientWithResponses, round if err != nil { return nil, err } + if a.StatusCode() != 200 { + return nil, errors.New(a.Status()) + } b, err := client.GetBlockWithResponse(ctx, int(round)-window, &api.GetBlockParams{ Format: &format, }) if err != nil { return nil, err } + if b.StatusCode() != 200 { + return nil, errors.New(b.Status()) + } + // Push to the transactions count list aTimestamp := time.Duration(a.JSON200.Block["ts"].(float64)) * time.Second bTimestamp := time.Duration(b.JSON200.Block["ts"].(float64)) * time.Second diff --git a/internal/state.go b/internal/state.go index 374f5a84..6cae09ea 100644 --- a/internal/state.go +++ b/internal/state.go @@ -3,6 +3,7 @@ package internal import ( "context" "errors" + "time" "github.com/algorandfoundation/hack-tui/api" ) @@ -17,6 +18,14 @@ type StateModel struct { Watching bool } +func (s *StateModel) waitAfterError(err error, cb func(model *StateModel, err error)) { + if err != nil { + s.Status.State = "DOWN" + cb(nil, err) + time.Sleep(time.Second * 3) + } +} + // TODO: allow context to handle loop func (s *StateModel) Watch(cb func(model *StateModel, err error), ctx context.Context, client *api.ClientWithResponses) { s.Watching = true @@ -36,14 +45,17 @@ func (s *StateModel) Watch(cb func(model *StateModel, err error), ctx context.Co break } status, err := client.WaitForBlockWithResponse(ctx, int(lastRound)) + s.waitAfterError(err, cb) if err != nil { - cb(nil, err) + continue } if status.StatusCode() != 200 { - cb(nil, errors.New(status.Status())) - return + s.waitAfterError(errors.New(status.Status()), cb) + continue } + s.Status.State = "Unknown" + // Update Status s.Status.Update(status.JSON200.LastRound, status.JSON200.CatchupTime, status.JSON200.UpgradeNodeVote) @@ -53,8 +65,9 @@ func (s *StateModel) Watch(cb func(model *StateModel, err error), ctx context.Co // Run Round Averages and RX/TX every 5 rounds if s.Status.LastRound%5 == 0 { bm, err := GetBlockMetrics(ctx, client, s.Status.LastRound, s.Metrics.Window) + s.waitAfterError(err, cb) if err != nil { - cb(nil, err) + continue } s.Metrics.RoundTime = bm.AvgTime s.Metrics.TPS = bm.TPS diff --git a/main_test.go b/main_test.go index e17e723f..40521d59 100644 --- a/main_test.go +++ b/main_test.go @@ -6,6 +6,7 @@ import ( ) func Test_Main(t *testing.T) { - viper.Set("server", "https://mainnet-api.4160.nodely.dev") + viper.Set("token", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") + viper.Set("server", "http://localhost:8080") main() } diff --git a/ui/viewport.go b/ui/viewport.go index 14bf3239..02f07f7b 100644 --- a/ui/viewport.go +++ b/ui/viewport.go @@ -83,6 +83,10 @@ func (m ViewportViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.errorMsg = &strMsg // When the state updates case internal.StateModel: + if m.errorMsg != nil { + m.errorMsg = nil + m.page = AccountsPage + } m.Data = &msg // Navigate to the transaction page when a partkey is selected case *api.ParticipationKey: