Skip to content

Commit

Permalink
feat(ui): handle fast catchup
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Nov 27, 2024
1 parent 0b57899 commit d0ad886
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
12 changes: 11 additions & 1 deletion internal/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ func (s *StateModel) Watch(cb func(model *StateModel, err error), ctx context.Co
if !s.Watching {
break
}

if s.Status.State == FastCatchupState {
time.Sleep(time.Second * 10)
err := s.Status.Fetch(ctx, client, new(HttpPkg))
if err != nil {
cb(nil, err)
}
continue
}

status, err := client.WaitForBlockWithResponse(ctx, int(lastRound))
s.waitAfterError(err, cb)
if err != nil {
Expand All @@ -65,7 +75,7 @@ func (s *StateModel) Watch(cb func(model *StateModel, err error), ctx context.Co
s.Status.State = "Unknown"

// Update Status
s.Status.Update(status.JSON200.LastRound, status.JSON200.CatchupTime, status.JSON200.UpgradeNodeVote)
s.Status.Update(status.JSON200.LastRound, status.JSON200.CatchupTime, status.JSON200.CatchpointAcquiredBlocks, status.JSON200.UpgradeNodeVote)

// Fetch Keys
s.UpdateKeys()
Expand Down
15 changes: 10 additions & 5 deletions internal/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
type State string

const (
SyncingState State = "SYNCING"
StableState State = "RUNNING"
FastCatchupState State = "FAST-CATCHUP"
SyncingState State = "SYNCING"
StableState State = "RUNNING"
)

// StatusModel represents a status response from algod.Status
Expand All @@ -28,10 +29,14 @@ type StatusModel struct {
func (m *StatusModel) String() string {
return fmt.Sprintf("\nLastRound: %d\n", m.LastRound)
}
func (m *StatusModel) Update(lastRound int, catchupTime int, upgradeNodeVote *bool) {
func (m *StatusModel) Update(lastRound int, catchupTime int, aquiredBlocks *int, upgradeNodeVote *bool) {
m.LastRound = uint64(lastRound)
if catchupTime > 0 {
m.State = SyncingState
if aquiredBlocks != nil {
m.State = FastCatchupState
} else {
m.State = SyncingState
}
} else {
m.State = StableState
}
Expand Down Expand Up @@ -72,6 +77,6 @@ func (m *StatusModel) Fetch(ctx context.Context, client api.ClientWithResponsesI
return fmt.Errorf("Status code %d: %s", s.StatusCode(), s.Status())
}

m.Update(s.JSON200.LastRound, s.JSON200.CatchupTime, s.JSON200.UpgradeNodeVote)
m.Update(s.JSON200.LastRound, s.JSON200.CatchupTime, s.JSON200.CatchpointAcquiredBlocks, s.JSON200.UpgradeNodeVote)
return nil
}
2 changes: 1 addition & 1 deletion ui/pages/accounts/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (m ViewModel) makeRows() *[]table.Row {

for key := range m.Data.Accounts {
expires := m.Data.Accounts[key].Expires.String()
if m.Data.Status.State == internal.SyncingState {
if m.Data.Status.State != internal.StableState {
expires = "SYNCING"
}
if !m.Data.Accounts[key].Expires.After(time.Now().Add(-(time.Hour * 24 * 365 * 50))) {
Expand Down
12 changes: 9 additions & 3 deletions ui/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,20 @@ func (m StatusViewModel) View() string {
}
beginning := style.Blue.Render(" Latest Round: ") + strconv.Itoa(int(m.Data.Status.LastRound))

end := style.Yellow.Render(strings.ToUpper(string(m.Data.Status.State))) + " "
var end string
switch m.Data.Status.State {
case internal.StableState:
end = style.Green.Render(strings.ToUpper(string(m.Data.Status.State))) + " "
default:
end = style.Yellow.Render(strings.ToUpper(string(m.Data.Status.State))) + " "
}
middle := strings.Repeat(" ", max(0, size-(lipgloss.Width(beginning)+lipgloss.Width(end)+2)))

// Last Round
row1 := lipgloss.JoinHorizontal(lipgloss.Left, beginning, middle, end)

roundTime := fmt.Sprintf("%.2fs", float64(m.Data.Metrics.RoundTime)/float64(time.Second))
if m.Data.Status.State == internal.SyncingState {
if m.Data.Status.State != internal.StableState {
roundTime = "--"
}
beginning = style.Blue.Render(" Round time: ") + roundTime
Expand All @@ -98,7 +104,7 @@ func (m StatusViewModel) View() string {
row2 := lipgloss.JoinHorizontal(lipgloss.Left, beginning, middle, end)

tps := fmt.Sprintf("%.2f", m.Data.Metrics.TPS)
if m.Data.Status.State == internal.SyncingState {
if m.Data.Status.State != internal.StableState {
tps = "--"
}
beginning = style.Blue.Render(" TPS: ") + tps
Expand Down
8 changes: 4 additions & 4 deletions ui/viewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (m ViewportViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.String() {
case "g":
// Only open modal when it is closed and not syncing
if !m.modal.Open && m.Data.Status.State != internal.SyncingState && m.Data.Metrics.RoundTime > 0 {
if !m.modal.Open && m.Data.Status.State == internal.StableState && m.Data.Metrics.RoundTime > 0 {
address := ""
selected := m.accountsPage.SelectedAccount()
if selected != nil {
Expand All @@ -86,7 +86,7 @@ func (m ViewportViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Address: address,
Type: app.GenerateModal,
})
} else if m.Data.Status.State == internal.SyncingState || m.Data.Metrics.RoundTime == 0 {
} else if m.Data.Status.State != internal.StableState || m.Data.Metrics.RoundTime == 0 {
genErr := errors.New("Please wait for more data to sync before generating a key")
m.modal, cmd = m.modal.HandleMessage(genErr)
cmds = append(cmds, cmd)
Expand Down Expand Up @@ -124,10 +124,10 @@ func (m ViewportViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.TerminalWidth = msg.Width
m.TerminalHeight = msg.Height
m.PageWidth = msg.Width
m.PageHeight = max(0, msg.Height-lipgloss.Height(m.headerView())-1)
m.PageHeight = max(0, msg.Height-lipgloss.Height(m.headerView()))

modalMsg := tea.WindowSizeMsg{
Width: m.PageWidth - 2,
Width: m.PageWidth,
Height: m.PageHeight,
}

Expand Down

0 comments on commit d0ad886

Please sign in to comment.