forked from distractedm1nd/da-tui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
381 lines (325 loc) · 10.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package main
import (
"context"
"fmt"
"os"
"sort"
"strconv"
"strings"
"time"
"github.com/celestiaorg/celestia-node/das"
"github.com/celestiaorg/celestia-node/header"
"github.com/celestiaorg/go-header/sync"
"github.com/charmbracelet/bubbles/list"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/celestiaorg/celestia-node/api/rpc/client"
"github.com/charmbracelet/bubbles/progress"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
const (
padding = 2
)
var (
pad = strings.Repeat(" ", padding)
titleStyle = lipgloss.NewStyle()
paginationStyle = list.DefaultStyles().PaginationStyle.PaddingLeft(4)
panelStyle = lipgloss.NewStyle().BorderStyle(lipgloss.NormalBorder())
activePanelStyle = lipgloss.NewStyle().BorderStyle(lipgloss.NormalBorder()).BorderForeground(lipgloss.Color("69"))
docStyle = lipgloss.NewStyle().Margin(1, 1, 1, 1)
)
type panel int
const (
daserPanel panel = iota
headerPanel
syncerPanel
peerPanel
bannedPeerPanel
)
type listItem struct {
title, desc string
}
func (i listItem) Title() string { return i.title }
func (i listItem) Description() string { return i.desc }
func (i listItem) FilterValue() string { return i.title }
func main() {
celestiaClient, err := client.NewClient(
context.TODO(),
os.Args[1],
os.Args[2],
)
if err != nil {
panic(err)
}
headerSub, err := celestiaClient.Header.Subscribe(context.Background())
if err != nil {
panic(err)
}
m := model{
samplingProgress: progress.New(progress.WithDefaultGradient(), progress.WithoutPercentage()),
syncerProgress: progress.New(progress.WithDefaultGradient(), progress.WithoutPercentage()),
client: celestiaClient,
peerList: createList("Peers"),
bannedPeerList: createList("Banned Peers"),
headerList: createList("Incoming Headers"),
headerSub: headerSub,
}
if _, err := tea.NewProgram(&m).Run(); err != nil {
fmt.Println("Oh no!", err)
os.Exit(1)
}
}
func createList(title string) list.Model {
l := list.New(make([]list.Item, 0), list.NewDefaultDelegate(), 0, 0)
l.DisableQuitKeybindings()
l.SetShowStatusBar(false)
l.SetFilteringEnabled(false)
l.Title = title
l.Styles.Title = titleStyle
l.Styles.PaginationStyle = paginationStyle
return l
}
type tickMsg time.Time
type model struct {
client *client.Client
currentStats *das.SamplingStats
samplingProgress progress.Model
syncStats *sync.State
syncerProgress progress.Model
headerSub <-chan *header.ExtendedHeader
headerList list.Model
peerList list.Model
peers []peer.AddrInfo
bannedPeerList list.Model
bannedPeers []peer.AddrInfo
activePanel panel
width, height int
}
func (m *model) Init() tea.Cmd {
return tea.Batch(waitForActivity(m.headerSub), tickCmd())
}
func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case *header.ExtendedHeader:
return m, m.handleIncomingHeader(msg)
case tea.KeyMsg:
switch keypress := msg.String(); keypress {
case "ctrl+c":
return m, tea.Quit
case "tab":
m.activePanel = (m.activePanel + 1) % 5
case "b":
m.handleBanPeer()
case "u":
m.handleUnbanPeer()
}
case tea.WindowSizeMsg:
m.height, m.width = msg.Height, msg.Width
return m, nil
case tickMsg:
updateStats := m.updateStats()
updatePeers := m.updatePeers()
return m, tea.Batch(tickCmd(), updateStats, updatePeers)
// FrameMsg is sent when the samplingProgress bar wants to animate itself
case progress.FrameMsg:
progressModel, cmd := m.samplingProgress.Update(msg)
syncerModel, cmd2 := m.syncerProgress.Update(msg)
m.samplingProgress = progressModel.(progress.Model)
m.syncerProgress = syncerModel.(progress.Model)
return m, tea.Batch(cmd, cmd2)
}
// handle navigation keypresses for the list panels
switch m.activePanel {
case headerPanel:
var cmd tea.Cmd
m.headerList, cmd = m.headerList.Update(msg)
return m, cmd
case peerPanel:
var cmd tea.Cmd
m.peerList, cmd = m.peerList.Update(msg)
return m, cmd
case bannedPeerPanel:
var cmd tea.Cmd
m.bannedPeerList, cmd = m.bannedPeerList.Update(msg)
return m, cmd
default:
return m, nil
}
}
func (m *model) View() string {
h, _ := docStyle.GetFrameSize()
peerW, peerH := m.getPanelDimensions(0.33, 0.33)
headerW, headerH := m.getPanelDimensions(0.66, 0.33)
daserW, daserH := m.getPanelDimensions(0.66, 0.66)
syncerW, syncerH := m.getPanelDimensions(0.33, 0.33)
m.peerList.SetHeight(peerH)
m.peerList.SetWidth(peerW)
m.bannedPeerList.SetHeight(peerH)
m.bannedPeerList.SetWidth(peerW)
m.headerList.SetHeight(headerH)
m.headerList.SetWidth(headerW)
styles := []lipgloss.Style{panelStyle, panelStyle, panelStyle, panelStyle, panelStyle}
styles[m.activePanel] = activePanelStyle
return lipgloss.JoinHorizontal(
lipgloss.Top,
lipgloss.JoinVertical(
lipgloss.Left,
styles[0].Width(daserW).Height(daserH).Render(m.daserPanel(h)),
styles[1].Width(headerW).Height(headerH).Render(m.headerList.View()),
),
lipgloss.JoinVertical(
lipgloss.Right,
styles[2].Width(syncerW).Height(syncerH).Render(m.syncerPanel(h)),
styles[3].Width(peerW).Height(peerH).Render(m.peerList.View()),
styles[4].Width(peerW).Height(peerH).Render(m.bannedPeerList.View()),
),
)
}
func tickCmd() tea.Cmd {
return tea.Tick(time.Second/2, func(t time.Time) tea.Msg {
return tickMsg(t)
})
}
func waitForActivity(sub <-chan *header.ExtendedHeader) tea.Cmd {
return func() tea.Msg {
return <-sub
}
}
func (m *model) handleBanPeer() {
if m.activePanel == peerPanel {
peerID, err := peer.Decode(m.peerList.SelectedItem().(listItem).Title())
if err != nil {
panic(err)
}
err = m.client.P2P.BlockPeer(context.TODO(), peerID)
if err != nil {
panic(err)
}
}
}
func (m *model) handleUnbanPeer() {
if m.activePanel == bannedPeerPanel {
peerID, err := peer.Decode(m.bannedPeerList.SelectedItem().(listItem).Title())
if err != nil {
panic(err)
}
err = m.client.P2P.UnblockPeer(context.TODO(), peerID)
if err != nil {
panic(err)
}
}
}
func (m *model) handleIncomingHeader(header *header.ExtendedHeader) tea.Cmd {
var cmd tea.Cmd
m.headerList.InsertItem(
len(m.headerList.Items()),
listItem{title: strconv.FormatInt(header.Height(), 10), desc: header.Hash().String()},
)
m.headerList, cmd = m.headerList.Update(header)
return tea.Batch(cmd, waitForActivity(m.headerSub))
}
func (m *model) getAddrInfo(peer peer.ID) peer.AddrInfo {
addrInfo, _ := m.client.P2P.PeerInfo(context.TODO(), peer)
sort.Slice(addrInfo.Addrs, func(i, j int) bool {
return addrInfo.Addrs[i].String() < addrInfo.Addrs[j].String()
})
return addrInfo
}
func (m *model) updateStats() tea.Cmd {
stats, err := m.client.DAS.SamplingStats(context.TODO())
if err != nil {
return nil
}
m.currentStats = &stats
syncStats, err := m.client.Header.SyncState(context.TODO())
if err != nil {
return nil
}
m.syncStats = &syncStats
setSamplingProgress := m.samplingProgress.SetPercent(
float64(stats.SampledChainHead) / float64(stats.NetworkHead),
)
setSyncerProgress := m.syncerProgress.SetPercent(
float64(m.syncStats.Height-m.syncStats.FromHeight) / float64(m.syncStats.ToHeight-m.syncStats.FromHeight),
)
return tea.Batch(setSamplingProgress, setSyncerProgress)
}
func (m *model) updatePeers() tea.Cmd {
return tea.Batch(
m.refreshPeerList(&m.bannedPeers, &m.bannedPeerList, m.client.P2P.ListBlockedPeers),
m.refreshPeerList(&m.peers, &m.peerList, m.client.P2P.Peers),
)
}
func (m *model) refreshPeerList(peerList *[]peer.AddrInfo, uiList *list.Model, fetchPeers func(context.Context) ([]peer.ID, error)) tea.Cmd {
peers, err := fetchPeers(context.TODO())
if err != nil {
return nil
}
sort.Slice(peers, func(i, j int) bool {
return peers[i].String() < peers[j].String()
})
if len(peers) != len(*peerList) {
peerListItems := make([]list.Item, 0, len(peers))
for _, peer := range peers {
addrInfo := m.getAddrInfo(peer)
desc := "No multiaddr found"
if len(addrInfo.Addrs) > 0 {
desc = addrInfo.Addrs[0].String()
}
peerListItems = append(peerListItems, listItem{title: addrInfo.String(), desc: desc})
}
return uiList.SetItems(peerListItems)
}
return nil
}
func (m *model) getPanelDimensions(scaleW, scaleH float64) (w, h int) {
h, v := docStyle.GetFrameSize()
return int(float64(m.width)*scaleW) - h, int(float64(m.height)*scaleH) - v
}
func (m *model) syncerPanel(frameHeight int) string {
var syncerPanel string
if m.syncStats == nil {
syncerPanel = "Syncer Stats Loading...\n"
}
if m.syncStats != nil {
fromHeight := strconv.FormatUint(m.syncStats.FromHeight, 10)
toHeight := strconv.FormatUint(m.syncStats.ToHeight, 10)
syncerHeight := strconv.FormatUint(m.syncStats.Height, 10)
m.syncerProgress.Width = (m.width / 3) - frameHeight - 4*padding - 1 - len(fromHeight) - len(toHeight)
syncerPanel = "Syncer Progress: \n\n" +
pad + fromHeight + pad + m.syncerProgress.View() + pad + toHeight + pad + "\n\n" +
"Syncer Height: " + syncerHeight
}
return syncerPanel
}
func (m *model) daserPanel(frameHeight int) string {
var daserPanel string
if m.currentStats == nil {
daserPanel = "\n" +
pad + m.samplingProgress.View()
}
if m.currentStats != nil {
m.samplingProgress.Width = (2 * m.width / 3) - frameHeight - 4*padding - 1 - len(strconv.FormatUint(m.currentStats.NetworkHead, 10))
daserPanel = "DASer Progress: \n" +
pad + "0" + pad + m.samplingProgress.View() + pad + strconv.FormatUint(m.currentStats.NetworkHead, 10) + pad + "\n\n" +
pad + "Sampled Chain Head: " + strconv.FormatUint(m.currentStats.SampledChainHead, 10) + "\n" +
pad + "Network Head: " + strconv.FormatUint(m.currentStats.NetworkHead, 10) + "\n" +
pad + "Head of Catchup: " + strconv.FormatUint(m.currentStats.CatchupHead, 10) + "\n\n" +
m.daserWorkers()
}
return daserPanel
}
func (m *model) daserWorkers() string {
sort.Slice(m.currentStats.Workers, func(i, j int) bool {
return m.currentStats.Workers[i].From < m.currentStats.Workers[j].From
})
workerString := "Workers: \n"
for _, worker := range m.currentStats.Workers {
progressBar := progress.New(progress.WithDefaultGradient(), progress.WithoutPercentage())
from, to := strconv.FormatUint(worker.From, 10), strconv.FormatUint(worker.To, 10)
percentageComplete := float64(worker.Curr-worker.From) / float64(worker.To-worker.From)
workerString += pad + from + pad + progressBar.ViewAs(percentageComplete) + pad + to + "\n"
}
workerString += "\n\n"
return workerString
}