From a88c261cb156e8ebde0272c457cdb074830d83fc Mon Sep 17 00:00:00 2001 From: jholdstock Date: Mon, 26 Jun 2023 14:35:53 +0100 Subject: [PATCH] webapi: Display decoded fee tx, not just raw bytes When searching for a ticket in the admin interface, use GetRawTransaction RPC to get the decoded transaction and display it as formatted JSON. --- rpc/dcrd.go | 11 ++++++++ webapi/admin.go | 33 ++++++++++++++++++++++ webapi/public/css/vspd.css | 5 ++++ webapi/templates/ticket-search-result.html | 17 ++++++++++- 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/rpc/dcrd.go b/rpc/dcrd.go index 31fe23f..d0975ae 100644 --- a/rpc/dcrd.go +++ b/rpc/dcrd.go @@ -150,6 +150,17 @@ func (c *DcrdRPC) GetRawTransaction(txHash string) (*dcrdtypes.TxRawResult, erro return &resp, nil } +// DecodeRawTransaction uses decoderawtransaction RPC to decode raw transaction bytes. +func (c *DcrdRPC) DecodeRawTransaction(txHex string) (*dcrdtypes.TxRawDecodeResult, error) { + var resp dcrdtypes.TxRawDecodeResult + err := c.Call(c.ctx, "decoderawtransaction", &resp, txHex) + if err != nil { + return nil, err + } + + return &resp, nil +} + // SendRawTransaction uses sendrawtransaction RPC to broadcast a transaction to // the network. It ignores errors caused by duplicate transactions. func (c *DcrdRPC) SendRawTransaction(txHex string) error { diff --git a/webapi/admin.go b/webapi/admin.go index f9c37d4..ab9b2a8 100644 --- a/webapi/admin.go +++ b/webapi/admin.go @@ -5,6 +5,7 @@ package webapi import ( + "encoding/json" "net/http" "github.com/decred/vspd/database" @@ -41,6 +42,7 @@ type searchResult struct { Hash string Found bool Ticket database.Ticket + FeeTxDecoded string AltSignAddrData *database.AltSignAddrData VoteChanges map[uint32]database.VoteChangeRecord MaxVoteChanges int @@ -176,11 +178,42 @@ func (s *Server) ticketSearch(c *gin.Context) { return } + // Decode the fee tx so it can be displayed human-readable. Fee tx hex may + // be null because it is removed from the DB if the tx is already mined and + // confirmed. + var feeTxDecoded string + if ticket.FeeTxHex != "" { + dcrdClient := c.MustGet(dcrdKey).(*rpc.DcrdRPC) + dcrdErr := c.MustGet(dcrdErrorKey) + if dcrdErr != nil { + s.log.Errorf("Could not get dcrd client: %v", dcrdErr.(error)) + c.String(http.StatusInternalServerError, "Could not get dcrd client") + return + } + + resp, err := dcrdClient.DecodeRawTransaction(ticket.FeeTxHex) + if err != nil { + s.log.Errorf("dcrd.DecodeRawTransaction error: %w", err) + c.String(http.StatusInternalServerError, "Error decoding fee transaction") + return + } + + decoded, err := json.Marshal(resp) + if err != nil { + s.log.Errorf("Unmarshal fee tx error: %w", err) + c.String(http.StatusInternalServerError, "Error unmarshalling fee tx") + return + } + + feeTxDecoded = string(decoded) + } + c.HTML(http.StatusOK, "admin.html", gin.H{ "SearchResult": searchResult{ Hash: hash, Found: found, Ticket: ticket, + FeeTxDecoded: feeTxDecoded, AltSignAddrData: altSignAddrData, VoteChanges: voteChanges, MaxVoteChanges: s.cfg.MaxVoteChangeRecords, diff --git a/webapi/public/css/vspd.css b/webapi/public/css/vspd.css index 4ccb6f3..368a324 100644 --- a/webapi/public/css/vspd.css +++ b/webapi/public/css/vspd.css @@ -158,6 +158,11 @@ footer .code { font-size: 12px; } +#ticket-table .small-text { + padding: 10px 16px; + font-size: 12px; +} + #ticket-table pre { color: #3D5873; font-size: 12px; diff --git a/webapi/templates/ticket-search-result.html b/webapi/templates/ticket-search-result.html index 5fc8479..29c200d 100644 --- a/webapi/templates/ticket-search-result.html +++ b/webapi/templates/ticket-search-result.html @@ -75,7 +75,22 @@ Fee Tx - {{ .Ticket.FeeTxHex }} + + {{ if .Ticket.FeeTxHex }} +
+ Raw Bytes +
+ {{ .Ticket.FeeTxHex }} +
+
+
+ Decoded +
+
{{ indentJSON .FeeTxDecoded }}
+
+
+ {{ end }} + Fee Tx Status