Skip to content

Commit 04ea6dc

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 9c02e71 commit 04ea6dc

2 files changed

Lines changed: 40 additions & 58 deletions

File tree

cmd/dcrdata/internal/explorer/explorerroutes.go

Lines changed: 36 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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

@@ -1408,62 +1408,40 @@ type TreasuryInfo struct {
14081408
Path string
14091409
Limit, Offset int64 // ?n=Limit&start=Offset
14101410
TxnType string // ?txntype=TxnType
1411-
1412-
// TODO: tadd and tspend can be unconfirmed. tspend for a very long time.
1413-
// NumUnconfirmed is the number of unconfirmed txns
1414-
// NumUnconfirmed int64
1415-
// UnconfirmedTxns []*dbtypes.TreasuryTx
1416-
14171411
// Transactions on the current page
14181412
Transactions []*dbtypes.TreasuryTx
14191413
NumTransactions int64 // len(Transactions) but int64 for dumb template
14201414

14211415
Balance *dbtypes.TreasuryBalance
14221416
ConvertedBalance *exchanges.Conversion
14231417
TypeCount int64
1418+
1419+
// tadd and tspend can be unconfirmed. tspend for a very long time.
1420+
Mempool *TreasuryMempoolInfo
1421+
}
1422+
1423+
// TreasuryMempoolInfo holds the treasury-related mempool transactions that are
1424+
// not yet confirmed in a block. It is used to display treasury mempool
1425+
// information on the treasury page.
1426+
type TreasuryMempoolInfo struct {
1427+
NumTSpends int
1428+
NumTAdds int
1429+
TSpends []types.MempoolTx
1430+
TAdds []types.MempoolTx
14241431
}
14251432

14261433
// TreasuryPage is the page handler for the "/treasury" path
14271434
func (exp *explorerUI) TreasuryPage(w http.ResponseWriter, r *http.Request) {
1428-
ctx := context.WithValue(r.Context(), ctxAddress, exp.pageData.HomeInfo.DevAddress)
1429-
r = r.WithContext(ctx)
1430-
if queryVals := r.URL.Query(); queryVals.Get("txntype") == "" {
1431-
queryVals.Set("txntype", "tspend")
1432-
r.URL.RawQuery = queryVals.Encode()
1433-
}
1434-
1435-
limitN := defaultAddressRows
1436-
if nParam := r.URL.Query().Get("n"); nParam != "" {
1437-
val, err := strconv.ParseUint(nParam, 10, 64)
1438-
if err != nil {
1439-
exp.StatusPage(w, defaultErrorCode, "invalid n value", "", ExpStatusError)
1440-
return
1441-
}
1442-
if int64(val) > MaxTreasuryRows {
1443-
log.Warnf("TreasuryPage: requested up to %d address rows, "+
1444-
"limiting to %d", limitN, MaxTreasuryRows)
1445-
limitN = MaxTreasuryRows
1446-
} else {
1447-
limitN = int64(val)
1448-
}
1449-
}
1435+
ctx := r.Context()
14501436

1451-
// Number of txns to skip (OFFSET in database query). For UX reasons, the
1452-
// "start" URL query parameter is used.
1453-
var offset int64
1454-
if startParam := r.URL.Query().Get("start"); startParam != "" {
1455-
val, err := strconv.ParseUint(startParam, 10, 64)
1456-
if err != nil {
1457-
exp.StatusPage(w, defaultErrorCode, "invalid start value", "", ExpStatusError)
1458-
return
1459-
}
1460-
offset = int64(val)
1437+
// Grab the URL query parameters
1438+
txType, txTypeStr, limitN, offset, err := parseTreasuryParams(r)
1439+
if err != nil {
1440+
log.Errorf("TreasuryPage request error: %v", err)
1441+
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
1442+
return
14611443
}
14621444

1463-
// Transaction types to show.
1464-
txTypeStr := r.URL.Query().Get("txntype")
1465-
txType := parseTreasuryTransactionType(txTypeStr)
1466-
14671445
txns, err := exp.dataSource.TreasuryTxns(ctx, limitN, offset, txType)
14681446
if exp.timeoutErrorPage(w, err, "TreasuryTxns") {
14691447
return
@@ -1477,6 +1455,7 @@ func (exp *explorerUI) TreasuryPage(w http.ResponseWriter, r *http.Request) {
14771455
exp.pageData.RUnlock()
14781456

14791457
typeCount := treasuryTypeCount(treasuryBalance, txType)
1458+
inv := exp.MempoolInventory()
14801459

14811460
treasuryData := &TreasuryInfo{
14821461
Net: exp.ChainParams.Net.String(),
@@ -1489,6 +1468,12 @@ func (exp *explorerUI) TreasuryPage(w http.ResponseWriter, r *http.Request) {
14891468
Transactions: txns,
14901469
Balance: treasuryBalance,
14911470
TypeCount: typeCount,
1471+
Mempool: &TreasuryMempoolInfo{
1472+
NumTSpends: inv.NumTSpends,
1473+
NumTAdds: inv.NumTAdds,
1474+
TSpends: inv.TSpends,
1475+
TAdds: inv.TAdds,
1476+
},
14921477
}
14931478

14941479
xcBot := exp.xcBot
@@ -1497,7 +1482,7 @@ func (exp *explorerUI) TreasuryPage(w http.ResponseWriter, r *http.Request) {
14971482
}
14981483

14991484
// Execute the HTML template.
1500-
linkTemplate := fmt.Sprintf("/treasury?start=%%d&n=%d&txntype=%v", limitN, txType)
1485+
linkTemplate := fmt.Sprintf("/treasury?start=%%d&n=%d&txntype=%s", limitN, txTypeStr)
15011486
pageData := struct {
15021487
*CommonPageData
15031488
Data *TreasuryInfo
@@ -1704,7 +1689,7 @@ func (exp *explorerUI) TreasuryTable(w http.ResponseWriter, r *http.Request) {
17041689
ctx := r.Context()
17051690

17061691
// Grab the URL query parameters
1707-
txType, limitN, offset, err := parseTreasuryParams(r)
1692+
txType, txTypeStr, limitN, offset, err := parseTreasuryParams(r)
17081693
if err != nil {
17091694
log.Errorf("TreasuryTable request error: %v", err)
17101695
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
@@ -1723,7 +1708,7 @@ func (exp *explorerUI) TreasuryTable(w http.ResponseWriter, r *http.Request) {
17231708
bal := exp.pageData.HomeInfo.TreasuryBalance
17241709
exp.pageData.RUnlock()
17251710

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

17281713
response := struct {
17291714
TxnCount int64 `json:"tx_count"`
@@ -1816,9 +1801,9 @@ func parseTreasuryTransactionType(txnTypeStr string) (txType stake.TxType) {
18161801

18171802
// parseTreasuryParams parses the tx filters for the treasury page. Used by both
18181803
// TreasuryPage and TreasuryTable.
1819-
func parseTreasuryParams(r *http.Request) (txType stake.TxType, limitN, offsetAddrOuts int64, err error) {
1820-
tType, limitN, offsetAddrOuts, err := parsePaginationParams(r)
1821-
txType = parseTreasuryTransactionType(tType)
1804+
func parseTreasuryParams(r *http.Request) (txType stake.TxType, txTypeStr string, limitN, offsetAddrOuts int64, err error) {
1805+
txTypeStr, limitN, offsetAddrOuts, err = parsePaginationParams(r)
1806+
txType = parseTreasuryTransactionType(txTypeStr)
18221807
return
18231808
}
18241809

@@ -1830,7 +1815,6 @@ func parsePaginationParams(r *http.Request) (txnType string, limitN, offset int6
18301815
limitN = defaultAddressRows
18311816

18321817
if nParam := r.URL.Query().Get("n"); nParam != "" {
1833-
18341818
var val uint64
18351819
val, err = strconv.ParseUint(nParam, 10, 64)
18361820
if err != nil {
@@ -1859,9 +1843,8 @@ func parsePaginationParams(r *http.Request) (txnType string, limitN, offset int6
18591843
}
18601844

18611845
// Transaction types to show.
1862-
txnType = r.URL.Query().Get("txntype")
1863-
if txnType == "" {
1864-
txnType = "all"
1846+
if txnType = r.URL.Query().Get("txntype"); txnType == "" {
1847+
txnType = "tspend" // Default to tspend
18651848
}
18661849

18671850
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)