Skip to content

Commit b3b2988

Browse files
multi: set linkTemplate txntype correctly and refactor
This commit fixes a bug where the int value of treasury tx types was used to construct page links instead of its string value. As a result, page navigation on the treasury page did not function correctly because the string value is expected to return correct table values. Others: - Replace duplicated code used in parsing URL params in *explorerUI.TreasuryPage with improved parseTreasuryParams helper function. - Remove unused code from when *explorerUI.AddressPage was used to display treasury data. - Remove resolved TODO and refactor how treasury mempool data is passed to the HTML template.
 Signed-off-by: Philemon Ukane <ukanephilemon@gmail.com>
1 parent a09dfc6 commit b3b2988

2 files changed

Lines changed: 39 additions & 60 deletions

File tree

cmd/dcrdata/internal/explorer/explorerroutes.go

Lines changed: 35 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
// Copyright (c) 2018-2024, The Decred developers
1+
// Copyright (c) 2018-2025, The Decred developers
22
// Copyright (c) 2017, The dcrdata developers
33
// See LICENSE for details.
44

55
package explorer
66

77
import (
8-
"context"
98
"encoding/hex"
109
"encoding/json"
1110
"errors"
@@ -1389,62 +1388,38 @@ type TreasuryInfo struct {
13891388
Path string
13901389
Limit, Offset int64 // ?n=Limit&start=Offset
13911390
TxnType string // ?txntype=TxnType
1392-
1393-
// TODO: tadd and tspend can be unconfirmed. tspend for a very long time.
1394-
// NumUnconfirmed is the number of unconfirmed txns
1395-
// NumUnconfirmed int64
1396-
// UnconfirmedTxns []*dbtypes.TreasuryTx
1397-
13981391
// Transactions on the current page
13991392
Transactions []*dbtypes.TreasuryTx
14001393
NumTransactions int64 // len(Transactions) but int64 for dumb template
14011394

14021395
Balance *dbtypes.TreasuryBalance
14031396
ConvertedBalance *exchanges.Conversion
14041397
TypeCount int64
1398+
1399+
// tadd and tspend can be unconfirmed. tspend for a very long time.
1400+
Mempool *TreasuryMempoolInfo
1401+
}
1402+
1403+
// TreasuryMempoolInfo holds the treasury-related mempool transactions that are
1404+
// not yet confirmed in a block. It is used to display treasury mempool
1405+
// information on the treasury page.
1406+
type TreasuryMempoolInfo struct {
1407+
NumTSpends int
1408+
NumTAdds int
1409+
TSpends []types.MempoolTx
1410+
TAdds []types.MempoolTx
14051411
}
14061412

14071413
// TreasuryPage is the page handler for the "/treasury" path
14081414
func (exp *explorerUI) TreasuryPage(w http.ResponseWriter, r *http.Request) {
1409-
ctx := context.WithValue(r.Context(), ctxAddress, exp.pageData.HomeInfo.DevAddress)
1410-
r = r.WithContext(ctx)
1411-
if queryVals := r.URL.Query(); queryVals.Get("txntype") == "" {
1412-
queryVals.Set("txntype", "tspend")
1413-
r.URL.RawQuery = queryVals.Encode()
1414-
}
1415-
1416-
limitN := defaultAddressRows
1417-
if nParam := r.URL.Query().Get("n"); nParam != "" {
1418-
val, err := strconv.ParseUint(nParam, 10, 64)
1419-
if err != nil {
1420-
exp.StatusPage(w, defaultErrorCode, "invalid n value", "", ExpStatusError)
1421-
return
1422-
}
1423-
if int64(val) > MaxTreasuryRows {
1424-
log.Warnf("TreasuryPage: requested up to %d address rows, "+
1425-
"limiting to %d", limitN, MaxTreasuryRows)
1426-
limitN = MaxTreasuryRows
1427-
} else {
1428-
limitN = int64(val)
1429-
}
1430-
}
1431-
1432-
// Number of txns to skip (OFFSET in database query). For UX reasons, the
1433-
// "start" URL query parameter is used.
1434-
var offset int64
1435-
if startParam := r.URL.Query().Get("start"); startParam != "" {
1436-
val, err := strconv.ParseUint(startParam, 10, 64)
1437-
if err != nil {
1438-
exp.StatusPage(w, defaultErrorCode, "invalid start value", "", ExpStatusError)
1439-
return
1440-
}
1441-
offset = int64(val)
1415+
// Grab the URL query parameters
1416+
txType, txTypeStr, limitN, offset, err := parseTreasuryParams(r)
1417+
if err != nil {
1418+
log.Errorf("TreasuryPage request error: %v", err)
1419+
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
1420+
return
14421421
}
14431422

1444-
// Transaction types to show.
1445-
txTypeStr := r.URL.Query().Get("txntype")
1446-
txType := parseTreasuryTransactionType(txTypeStr)
1447-
14481423
txns, err := exp.dataSource.TreasuryTxns(limitN, offset, txType)
14491424
if exp.timeoutErrorPage(w, err, "TreasuryTxns") {
14501425
return
@@ -1458,6 +1433,7 @@ func (exp *explorerUI) TreasuryPage(w http.ResponseWriter, r *http.Request) {
14581433
exp.pageData.RUnlock()
14591434

14601435
typeCount := treasuryTypeCount(treasuryBalance, txType)
1436+
inv := exp.MempoolInventory()
14611437

14621438
treasuryData := &TreasuryInfo{
14631439
Net: exp.ChainParams.Net.String(),
@@ -1470,6 +1446,12 @@ func (exp *explorerUI) TreasuryPage(w http.ResponseWriter, r *http.Request) {
14701446
Transactions: txns,
14711447
Balance: treasuryBalance,
14721448
TypeCount: typeCount,
1449+
Mempool: &TreasuryMempoolInfo{
1450+
NumTSpends: inv.NumTSpends,
1451+
NumTAdds: inv.NumTAdds,
1452+
TSpends: inv.TSpends,
1453+
TAdds: inv.TAdds,
1454+
},
14731455
}
14741456

14751457
xcBot := exp.xcBot
@@ -1478,7 +1460,7 @@ func (exp *explorerUI) TreasuryPage(w http.ResponseWriter, r *http.Request) {
14781460
}
14791461

14801462
// Execute the HTML template.
1481-
linkTemplate := fmt.Sprintf("/treasury?start=%%d&n=%d&txntype=%v", limitN, txType)
1463+
linkTemplate := fmt.Sprintf("/treasury?start=%%d&n=%d&txntype=%s", limitN, txTypeStr)
14821464
pageData := struct {
14831465
*CommonPageData
14841466
Data *TreasuryInfo
@@ -1679,7 +1661,7 @@ func (exp *explorerUI) AddressTable(w http.ResponseWriter, r *http.Request) {
16791661
// TreasuryTable is the handler for the "/treasurytable" path.
16801662
func (exp *explorerUI) TreasuryTable(w http.ResponseWriter, r *http.Request) {
16811663
// Grab the URL query parameters
1682-
txType, limitN, offset, err := parseTreasuryParams(r)
1664+
txType, txTypeStr, limitN, offset, err := parseTreasuryParams(r)
16831665
if err != nil {
16841666
log.Errorf("TreasuryTable request error: %v", err)
16851667
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
@@ -1698,7 +1680,7 @@ func (exp *explorerUI) TreasuryTable(w http.ResponseWriter, r *http.Request) {
16981680
bal := exp.pageData.HomeInfo.TreasuryBalance
16991681
exp.pageData.RUnlock()
17001682

1701-
linkTemplate := "/treasury" + "?start=%d&n=" + strconv.FormatInt(limitN, 10) + "&txntype=" + fmt.Sprintf("%v", txType)
1683+
linkTemplate := "/treasury" + "?start=%d&n=" + strconv.FormatInt(limitN, 10) + "&txntype=" + fmt.Sprintf("%s", txTypeStr)
17021684

17031685
response := struct {
17041686
TxnCount int64 `json:"tx_count"`
@@ -1791,9 +1773,9 @@ func parseTreasuryTransactionType(txnTypeStr string) (txType stake.TxType) {
17911773

17921774
// parseTreasuryParams parses the tx filters for the treasury page. Used by both
17931775
// TreasuryPage and TreasuryTable.
1794-
func parseTreasuryParams(r *http.Request) (txType stake.TxType, limitN, offsetAddrOuts int64, err error) {
1795-
tType, limitN, offsetAddrOuts, err := parsePaginationParams(r)
1796-
txType = parseTreasuryTransactionType(tType)
1776+
func parseTreasuryParams(r *http.Request) (txType stake.TxType, txTypeStr string, limitN, offsetAddrOuts int64, err error) {
1777+
txTypeStr, limitN, offsetAddrOuts, err = parsePaginationParams(r)
1778+
txType = parseTreasuryTransactionType(txTypeStr)
17971779
return
17981780
}
17991781

@@ -1805,7 +1787,6 @@ func parsePaginationParams(r *http.Request) (txnType string, limitN, offset int6
18051787
limitN = defaultAddressRows
18061788

18071789
if nParam := r.URL.Query().Get("n"); nParam != "" {
1808-
18091790
var val uint64
18101791
val, err = strconv.ParseUint(nParam, 10, 64)
18111792
if err != nil {
@@ -1834,9 +1815,8 @@ func parsePaginationParams(r *http.Request) (txnType string, limitN, offset int6
18341815
}
18351816

18361817
// Transaction types to show.
1837-
txnType = r.URL.Query().Get("txntype")
1838-
if txnType == "" {
1839-
txnType = "all"
1818+
if txnType = r.URL.Query().Get("txntype"); txnType == "" {
1819+
txnType = "tspend" // Default to tspend
18401820
}
18411821

18421822
return

cmd/dcrdata/views/treasury.tmpl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<html lang="en">
44
{{template "html-head" headData .CommonPageData "Decred Decentralized Treasury"}}
55
{{template "navbar" . }}
6-
{{- $mempool := .Mempool -}}
76
{{- with .Data}}
87
{{- $bal := .Balance -}}
98
{{- $TxnCount := $bal.TxCount}}
@@ -158,8 +157,8 @@
158157
</tr>
159158
</thead>
160159
<tbody>
161-
{{if gt $mempool.NumTSpends 0 -}}
162-
{{- range $mempool.TSpends -}}
160+
{{if gt .Mempool.NumTSpends 0 -}}
161+
{{- range .Mempool.TSpends -}}
163162
<tr>
164163
<td class="break-word clipboard">
165164
<a class="hash lh1rem" href="/tx/{{.Hash}}" title="{{.Hash}}">{{.Hash}}</a>
@@ -180,7 +179,7 @@
180179
</table>
181180
</div>
182181
</div>
183-
{{if gt $mempool.NumTAdds 0 -}}{{- /* this will be rare, so only show the section header and table if needed */ -}}
182+
{{if gt .Mempool.NumTAdds 0 -}}{{- /* this will be rare, so only show the section header and table if needed */ -}}
184183
<div class="row">
185184
<div class="col-sm-24">
186185
<div class="me-auto h4 col-24">Unconfirmed Treasury Adds</div>
@@ -193,7 +192,7 @@
193192
</tr>
194193
</thead>
195194
<tbody>
196-
{{range $mempool.TAdds -}}
195+
{{range .Mempool.TAdds -}}
197196
<tr>
198197
<td class="break-word clipboard">
199198
<a class="hash lh1rem" href="/tx/{{.Hash}}">{{.Hash}}</a>

0 commit comments

Comments
 (0)