Skip to content

Commit

Permalink
Merge pull request #16 from algorandfoundation/fix/algod-on-crash
Browse files Browse the repository at this point in the history
fix: improve resilience and error handling
  • Loading branch information
PhearZero authored Oct 29, 2024
2 parents fde8e8a + d0b7806 commit 0f0dc2c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 11 deletions.
9 changes: 5 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 = `
Expand Down Expand Up @@ -43,6 +42,7 @@ var (
cobra.CheckErr(err)

partkeys, err := internal.GetPartKeys(context.Background(), client)
cobra.CheckErr(err)

state := internal.StateModel{
Status: internal.StatusModel{
Expand Down Expand Up @@ -80,6 +80,7 @@ var (
p.Send(state)
}
if err != nil {
p.Send(state)
p.Send(err)
}
}, context.Background(), client)
Expand Down
3 changes: 2 additions & 1 deletion cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 8 additions & 1 deletion internal/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
21 changes: 17 additions & 4 deletions internal/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"context"
"errors"
"time"

"github.com/algorandfoundation/hack-tui/api"
)
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
4 changes: 4 additions & 0 deletions ui/viewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 0f0dc2c

Please sign in to comment.