Skip to content

Commit 91b778f

Browse files
committed
Add time until consensus upgrade to status.
1 parent 0f9e585 commit 91b778f

File tree

1 file changed

+45
-15
lines changed

1 file changed

+45
-15
lines changed

tui/internal/bubbles/status/status.go

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,20 @@ type Model struct {
2222
Network messages.NetworkMsg
2323
Err error
2424

25-
style *style.Styles
26-
requestor *messages.Requestor
25+
style *style.Styles
26+
requestor *messages.Requestor
27+
28+
// fast catchup state
2729
progress progress.Model
2830
processedAcctsPct float64
2931
verifiedAcctsPct float64
3032
acquiredBlksPct float64
33+
34+
// round time calculation state
35+
startBlock uint64
36+
startTime time.Time
37+
latestBlock uint64
38+
latestTime time.Time
3139
}
3240

3341
// New creates a status Model.
@@ -47,6 +55,19 @@ func (m Model) Init() tea.Cmd {
4755
)
4856
}
4957

58+
func (m Model) averageBlockTime() time.Duration {
59+
numBlocks := int64(m.latestBlock - m.startBlock)
60+
61+
// Default round time during first seen block
62+
if numBlocks == 0 {
63+
return 4400 * time.Millisecond
64+
}
65+
66+
runtime := m.latestTime.Sub(m.startTime)
67+
dur := runtime.Nanoseconds() / numBlocks
68+
return time.Duration(dur)
69+
}
70+
5071
// Update is part of the tea.Model interface.
5172
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
5273
switch msg := msg.(type) {
@@ -56,6 +77,20 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
5677
return m, tea.Quit
5778
}
5879
m.Status = msg.Status
80+
81+
// Save the times for computing round time
82+
if m.latestBlock < m.Status.LastRound {
83+
m.latestBlock = m.Status.LastRound
84+
m.latestTime = time.Now().Add(-time.Duration(m.Status.TimeSinceLastRound))
85+
86+
// Grab the start time
87+
if m.startBlock == 0 {
88+
m.startBlock = m.Status.LastRound
89+
since := time.Duration(m.Status.TimeSinceLastRound)
90+
m.startTime = time.Now().Add(-since)
91+
}
92+
}
93+
5994
if m.Status.CatchpointTotalAccounts > 0 {
6095
m.processedAcctsPct = float64(m.Status.CatchpointProcessedAccounts) / float64(m.Status.CatchpointTotalAccounts)
6196
m.verifiedAcctsPct = float64(m.Status.CatchpointVerifiedAccounts) / float64(m.Status.CatchpointTotalAccounts)
@@ -65,6 +100,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
65100
m.verifiedAcctsPct = 1
66101
m.acquiredBlksPct = float64(m.Status.CatchpointAcquiredBlocks) / float64(m.Status.CatchpointTotalBlocks)
67102
}
103+
68104
return m, tea.Tick(100*time.Millisecond, func(time.Time) tea.Msg {
69105
return m.requestor.GetStatusCmd()()
70106
})
@@ -89,13 +125,6 @@ func formatVersion(v string) string {
89125
return v[i:]
90126
}
91127

92-
func formatNextVersion(last, next string, round uint64) string {
93-
if last == next {
94-
return "N/A"
95-
}
96-
return strconv.FormatUint(round, 10)
97-
}
98-
99128
func writeProgress(b *strings.Builder, prefix string, progress progress.Model, pct float64) {
100129
b.WriteString(prefix)
101130
b.WriteString(progress.ViewAs(pct))
@@ -114,11 +143,6 @@ func (m Model) View() string {
114143
height := style.TopHeight - 2 - 3 // 3 is the padding/margin/border
115144
// status
116145
if (m.Status != models.NodeStatus{}) {
117-
nextVersion := formatNextVersion(
118-
m.Status.LastVersion,
119-
m.Status.NextVersion,
120-
m.Status.NextVersionRound)
121-
122146
switch {
123147
case m.Status.Catchpoint != "":
124148
// Catchpoint view
@@ -153,11 +177,17 @@ func (m Model) View() string {
153177
builder.WriteString(fmt.Sprintf(" %s\n", bold.Render("No upgrade in progress.")))
154178
height -= 2
155179
} else {
180+
// compute the time until the upgrade round and apply formatting to message
181+
togo := m.Status.NextVersionRound - m.Status.LastRound
182+
timeRemaining := time.Duration(int64(togo) * m.averageBlockTime().Nanoseconds()).Round(roundTo)
183+
remaining := m.style.AccountBlueText.Render(
184+
fmt.Sprintf("%d to go, %s", togo, timeRemaining))
185+
156186
// upgrade in progress
157187
builder.WriteString(fmt.Sprintf("%s\n", bold.Render("Consensus Upgrade Pending")))
158188
builder.WriteString(fmt.Sprintf("Current Protocol: %s\n", formatVersion(m.Status.LastVersion)))
159189
builder.WriteString(fmt.Sprintf("Next Protocol: %s\n", formatVersion(m.Status.NextVersion)))
160-
builder.WriteString(fmt.Sprintf("Upgrade round: %s\n", nextVersion))
190+
builder.WriteString(fmt.Sprintf("Upgrade round: %d (%s)\n", m.Status.NextVersionRound, remaining))
161191
height -= 4
162192
}
163193
}

0 commit comments

Comments
 (0)