From 1518f1d6b526c1d8bacf7d3ddf43c9d68c37c642 Mon Sep 17 00:00:00 2001 From: jholdstock Date: Fri, 1 Nov 2024 11:51:22 +0000 Subject: [PATCH 1/2] webapi: Remove duplicate code to render admin.html --- internal/webapi/admin.go | 48 ++++++++++++---------------------------- 1 file changed, 14 insertions(+), 34 deletions(-) diff --git a/internal/webapi/admin.go b/internal/webapi/admin.go index 0dd06a78..d52963e5 100644 --- a/internal/webapi/admin.go +++ b/internal/webapi/admin.go @@ -144,6 +144,11 @@ func (w *WebAPI) statusJSON(c *gin.Context) { // adminPage is the handler for "GET /admin". func (w *WebAPI) adminPage(c *gin.Context) { + // Render the admin template with no search result. + w.renderAdmin(c, nil) +} + +func (w *WebAPI) renderAdmin(c *gin.Context, searchResult *searchResult) { cacheData := c.MustGet(cacheKey).(cacheData) missed, err := w.db.GetMissedTickets() @@ -163,6 +168,7 @@ func (w *WebAPI) adminPage(c *gin.Context) { } c.HTML(http.StatusOK, "admin.html", gin.H{ + "SearchResult": searchResult, "WebApiCache": cacheData, "WebApiCfg": w.cfg, "WalletStatus": w.walletStatus(c), @@ -175,8 +181,6 @@ func (w *WebAPI) adminPage(c *gin.Context) { // ticketSearch is the handler for "POST /admin/ticket". The hash param will be // used to retrieve a ticket from the database. func (w *WebAPI) ticketSearch(c *gin.Context) { - cacheData := c.MustGet(cacheKey).(cacheData) - hash := c.PostForm("hash") ticket, found, err := w.db.GetTicketByHash(hash) @@ -230,38 +234,14 @@ func (w *WebAPI) ticketSearch(c *gin.Context) { feeTxDecoded = string(decoded) } - missed, err := w.db.GetMissedTickets() - if err != nil { - w.log.Errorf("db.GetMissedTickets error: %v", err) - c.String(http.StatusInternalServerError, "Error getting missed tickets from db") - return - } - - missed.SortByPurchaseHeight() - - xpubs, err := w.db.AllXPubs() - if err != nil { - w.log.Errorf("db.AllXPubs error: %v", err) - c.String(http.StatusInternalServerError, "Error getting xpubs from db") - return - } - - c.HTML(http.StatusOK, "admin.html", gin.H{ - "SearchResult": searchResult{ - Hash: hash, - Found: found, - Ticket: ticket, - FeeTxDecoded: feeTxDecoded, - AltSignAddrData: altSignAddrData, - VoteChanges: voteChanges, - MaxVoteChanges: w.cfg.MaxVoteChangeRecords, - }, - "WebApiCache": cacheData, - "WebApiCfg": w.cfg, - "WalletStatus": w.walletStatus(c), - "DcrdStatus": w.dcrdStatus(c), - "MissedTickets": missed, - "XPubs": xpubs, + w.renderAdmin(c, &searchResult{ + Hash: hash, + Found: found, + Ticket: ticket, + FeeTxDecoded: feeTxDecoded, + AltSignAddrData: altSignAddrData, + VoteChanges: voteChanges, + MaxVoteChanges: w.cfg.MaxVoteChangeRecords, }) } From 947f5a7ab6d412e10c6d37fc5f31e290de237e40 Mon Sep 17 00:00:00 2001 From: jholdstock Date: Fri, 1 Nov 2024 12:09:03 +0000 Subject: [PATCH 2/2] webapi: Distinguish between current/old xpub Showing current and old pubkeys under separate headers makes the information easier to parse, and also fixes a bug where the current pubkey was showing a retired timestamp of unix epoch. --- internal/webapi/admin.go | 17 ++++++++++++++--- internal/webapi/templates/admin.html | 24 +++++++++++++++++++++--- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/internal/webapi/admin.go b/internal/webapi/admin.go index d52963e5..6af358c6 100644 --- a/internal/webapi/admin.go +++ b/internal/webapi/admin.go @@ -160,13 +160,23 @@ func (w *WebAPI) renderAdmin(c *gin.Context, searchResult *searchResult) { missed.SortByPurchaseHeight() - xpubs, err := w.db.AllXPubs() + currentXPub, err := w.db.FeeXPub() + if err != nil { + w.log.Errorf("db.FeeXPub error: %v", err) + c.String(http.StatusInternalServerError, "Error getting current xpub from db") + return + } + + oldXPubs, err := w.db.AllXPubs() if err != nil { w.log.Errorf("db.AllXPubs error: %v", err) - c.String(http.StatusInternalServerError, "Error getting xpubs from db") + c.String(http.StatusInternalServerError, "Error getting all xpubs from db") return } + // Remove current xpub from the list of old xpubs. + delete(oldXPubs, currentXPub.ID) + c.HTML(http.StatusOK, "admin.html", gin.H{ "SearchResult": searchResult, "WebApiCache": cacheData, @@ -174,7 +184,8 @@ func (w *WebAPI) renderAdmin(c *gin.Context, searchResult *searchResult) { "WalletStatus": w.walletStatus(c), "DcrdStatus": w.dcrdStatus(c), "MissedTickets": missed, - "XPubs": xpubs, + "CurrentXPub": currentXPub, + "OldXPubs": oldXPubs, }) } diff --git a/internal/webapi/templates/admin.html b/internal/webapi/templates/admin.html index f6a7255f..018223cd 100644 --- a/internal/webapi/templates/admin.html +++ b/internal/webapi/templates/admin.html @@ -251,8 +251,26 @@

{{ pluralize (len .MissedTickets) "Missed Ticket" }}

-

All X Pubs

- {{ with .XPubs }} +

Current X Pub

+ + + + + + + + + + + + + +
IDKeyLast Address Index
{{ .CurrentXPub.ID }}{{ .CurrentXPub.Key }}{{ .CurrentXPub.LastUsedIdx }}
+
+ + {{ with .OldXPubs }} +
+

Old X Pubs

@@ -271,8 +289,8 @@

All X Pubs

{{ end }}
ID
- {{ end}}
+ {{ end }}