@@ -22,12 +22,20 @@ type Model struct {
22
22
Network messages.NetworkMsg
23
23
Err error
24
24
25
- style * style.Styles
26
- requestor * messages.Requestor
25
+ style * style.Styles
26
+ requestor * messages.Requestor
27
+
28
+ // fast catchup state
27
29
progress progress.Model
28
30
processedAcctsPct float64
29
31
verifiedAcctsPct float64
30
32
acquiredBlksPct float64
33
+
34
+ // round time calculation state
35
+ startBlock uint64
36
+ startTime time.Time
37
+ latestBlock uint64
38
+ latestTime time.Time
31
39
}
32
40
33
41
// New creates a status Model.
@@ -47,6 +55,19 @@ func (m Model) Init() tea.Cmd {
47
55
)
48
56
}
49
57
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
+
50
71
// Update is part of the tea.Model interface.
51
72
func (m Model ) Update (msg tea.Msg ) (tea.Model , tea.Cmd ) {
52
73
switch msg := msg .(type ) {
@@ -56,6 +77,20 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
56
77
return m , tea .Quit
57
78
}
58
79
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
+
59
94
if m .Status .CatchpointTotalAccounts > 0 {
60
95
m .processedAcctsPct = float64 (m .Status .CatchpointProcessedAccounts ) / float64 (m .Status .CatchpointTotalAccounts )
61
96
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) {
65
100
m .verifiedAcctsPct = 1
66
101
m .acquiredBlksPct = float64 (m .Status .CatchpointAcquiredBlocks ) / float64 (m .Status .CatchpointTotalBlocks )
67
102
}
103
+
68
104
return m , tea .Tick (100 * time .Millisecond , func (time.Time ) tea.Msg {
69
105
return m .requestor .GetStatusCmd ()()
70
106
})
@@ -89,13 +125,6 @@ func formatVersion(v string) string {
89
125
return v [i :]
90
126
}
91
127
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
-
99
128
func writeProgress (b * strings.Builder , prefix string , progress progress.Model , pct float64 ) {
100
129
b .WriteString (prefix )
101
130
b .WriteString (progress .ViewAs (pct ))
@@ -114,11 +143,6 @@ func (m Model) View() string {
114
143
height := style .TopHeight - 2 - 3 // 3 is the padding/margin/border
115
144
// status
116
145
if (m .Status != models.NodeStatus {}) {
117
- nextVersion := formatNextVersion (
118
- m .Status .LastVersion ,
119
- m .Status .NextVersion ,
120
- m .Status .NextVersionRound )
121
-
122
146
switch {
123
147
case m .Status .Catchpoint != "" :
124
148
// Catchpoint view
@@ -153,11 +177,17 @@ func (m Model) View() string {
153
177
builder .WriteString (fmt .Sprintf (" %s\n " , bold .Render ("No upgrade in progress." )))
154
178
height -= 2
155
179
} 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
+
156
186
// upgrade in progress
157
187
builder .WriteString (fmt .Sprintf ("%s\n " , bold .Render ("Consensus Upgrade Pending" )))
158
188
builder .WriteString (fmt .Sprintf ("Current Protocol: %s\n " , formatVersion (m .Status .LastVersion )))
159
189
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 ))
161
191
height -= 4
162
192
}
163
193
}
0 commit comments