Skip to content

Commit

Permalink
gui: Show pool payments.
Browse files Browse the repository at this point in the history
Pending and archived payments to the pool are now displayed on the admin page.
  • Loading branch information
jholdstock committed Sep 17, 2020
1 parent 89827b5 commit a41ea95
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 83 deletions.
11 changes: 11 additions & 0 deletions gui/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package gui
import (
"net/http"

"github.com/decred/dcrpool/pool"
"github.com/gorilla/csrf"
"github.com/gorilla/sessions"
)
Expand All @@ -17,6 +18,8 @@ type adminPageData struct {
HeaderData headerData
PoolStatsData poolStatsData
ConnectedClients map[string][]*client
ArchivedPayments []*archivedPayment
PendingPayments []*pendingPayment
}

// adminPage is the handler for "GET /admin". If the current session is
Expand All @@ -32,6 +35,12 @@ func (ui *GUI) adminPage(w http.ResponseWriter, r *http.Request) {

clients := ui.cache.getClients()

// Get the 10 most recent pending payments.
_, pendingPmts, _ := ui.cache.getPendingPayments(0, 9, pool.PoolFeesK)

// Get the 10 most recent archived payments.
_, archivedPmts, _ := ui.cache.getArchivedPayments(0, 9, pool.PoolFeesK)

pageData := adminPageData{
HeaderData: headerData{
CSRF: csrf.TemplateField(r),
Expand All @@ -48,6 +57,8 @@ func (ui *GUI) adminPage(w http.ResponseWriter, r *http.Request) {
SoloPool: ui.cfg.SoloPool,
},
ConnectedClients: clients,
PendingPayments: pendingPmts,
ArchivedPayments: archivedPmts,
}

ui.renderTemplate(w, "admin", pageData)
Expand Down
39 changes: 39 additions & 0 deletions gui/assets/public/js/admin-pagination.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
$.fn["pagination"].defaults.locator = "data";
$.fn["pagination"].defaults.totalNumberLocator = function(response) { return response.count; };
$.fn["pagination"].defaults.nextText = '<div class="pagination-arrow pagination-arrow-right"></div>';
$.fn["pagination"].defaults.prevText = '<div class="pagination-arrow pagination-arrow-left"></div>';
$.fn["pagination"].defaults.hideWhenLessThanOnePage = true;

if ($('#pending-payments-page-select').length) {
$('#pending-payments-page-select').pagination({
dataSource: "/admin/payments/pending",
callback: function (data) {
var html = '';
if (data.length > 0) {
$.each(data, function (_, item) {
html += '<tr><td><a href="' + item.workheighturl + '" rel="noopener noreferrer">' + item.workheight + '</a></td><td>' + item.createdon + '</td><td>' + item.amount + '</td></tr>';
});
} else {
html += '<tr><td colspan="100%"><span class="no-data">No pending payments</span></td></tr>';
}
$('#pending-payments-table').html(html);
}
});
};

if ($('#archived-payments-page-select').length) {
$('#archived-payments-page-select').pagination({
dataSource: "/admin/payments/archived",
callback: function (data) {
var html = '';
if (data.length > 0) {
$.each(data, function (_, item) {
html += '<tr><td><a href="' + item.workheighturl + '" rel="noopener noreferrer">' + item.workheight + '</a></td><td><a href="' + item.paidheighturl + '" rel="noopener noreferrer">' + item.paidheight + '</a></td><td>' + item.createdon + '</td><td>' + item.amount + '</td><td><a href="' + item.txurl + '" rel="noopener noreferrer">' + item.txid + '</a></td></tr>';
});
} else {
html += '<tr><td colspan="100%"><span class="no-data">No received payments</span></td></tr>';
}
$('#archived-payments-table').html(html);
}
});
};
77 changes: 1 addition & 76 deletions gui/assets/templates/account.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,84 +35,9 @@ <h1>Account Information</h1>


<div class="container">

{{ if .PendingPayments }}
<div class="row">
<div class="col-12 p-3">
<div class="block__content">

<h1>Pending Payments</h1>

<table class="table">
<thead>
<tr>
<th>Work Height</th>
<th>Created On</th>
<th>Amount</th>
</tr>
</thead>
<tbody id="pending-payments-table">
{{ range .PendingPayments }}
<tr>
<td><a href="{{ .WorkHeightURL }}"
rel="noopener noreferrer">{{ .WorkHeight }}</a></td>
<td>{{ .CreatedOn }}</td>
<td>{{ .Amount }}</td>
</tr>
{{end}}
</tbody>
</table>

<div id="pending-payments-page-select" class="page-select"></div>

</div>
</div>
</div>
{{end}}
{{template "payments" . }}

<div class="row">
<div class="col-12 p-3">
<div class="block__content">
<h1>Payments Received</h1>

<table class="table">
<thead>
<tr>
<th>Work Height</th>
<th>Payment Height</th>
<th>Created On</th>
<th>Amount</th>
<th>Tx ID</th>
</tr>
</thead>
<tbody id="archived-payments-table">
{{ range .ArchivedPayments }}
<tr>
<td><a href="{{ .WorkHeightURL }}"
rel="noopener noreferrer">{{ .WorkHeight }}</a></td>
<td><a href="{{ .PaidHeightURL }}"
rel="noopener noreferrer">{{ .PaidHeight }}</a></td>
<td>{{ .CreatedOn }}</td>
<td>{{ .Amount }}</td>
<td><a href="{{ .TxURL }}"
rel="noopener noreferrer">{{ .TxID }}</a>
</td>
</tr>
{{else}}
<tr>
<td colspan="100%"><span class="no-data">No payments received by account</span></td>
</tr>
{{end}}
</tbody>
</table>

<div id="archived-payments-page-select" class="page-select"></div>

</div>
</div>

</div>

<div class="row">

<div class="col-lg-6 col-12 p-3">
Expand Down
3 changes: 3 additions & 0 deletions gui/assets/templates/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ <h1>All Connected Miners</h1>

</div>

{{template "payments" . }}

</div>

<script src='/assets/js/socket.js'></script>
<script src='/assets/js/admin-pagination.js'></script>

</body>
</html>
Expand Down
80 changes: 80 additions & 0 deletions gui/assets/templates/payments.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{{define "payments"}}

{{ if .PendingPayments }}
<div class="row">
<div class="col-12 p-3">
<div class="block__content">

<h1>Pending Payments</h1>

<table class="table">
<thead>
<tr>
<th>Work Height</th>
<th>Created On</th>
<th>Amount</th>
</tr>
</thead>
<tbody id="pending-payments-table">
{{ range .PendingPayments }}
<tr>
<td><a href="{{ .WorkHeightURL }}"
rel="noopener noreferrer">{{ .WorkHeight }}</a></td>
<td>{{ .CreatedOn }}</td>
<td>{{ .Amount }}</td>
</tr>
{{end}}
</tbody>
</table>

<div id="pending-payments-page-select" class="page-select"></div>

</div>
</div>
</div>
{{end}}

<div class="row">
<div class="col-12 p-3">
<div class="block__content">
<h1>Payments Received</h1>

<table class="table">
<thead>
<tr>
<th>Work Height</th>
<th>Payment Height</th>
<th>Created On</th>
<th>Amount</th>
<th>Tx ID</th>
</tr>
</thead>
<tbody id="archived-payments-table">
{{ range .ArchivedPayments }}
<tr>
<td><a href="{{ .WorkHeightURL }}"
rel="noopener noreferrer">{{ .WorkHeight }}</a></td>
<td><a href="{{ .PaidHeightURL }}"
rel="noopener noreferrer">{{ .PaidHeight }}</a></td>
<td>{{ .CreatedOn }}</td>
<td>{{ .Amount }}</td>
<td><a href="{{ .TxURL }}"
rel="noopener noreferrer">{{ .TxID }}</a>
</td>
</tr>
{{else}}
<tr>
<td colspan="100%"><span class="no-data">No payments received</span></td>
</tr>
{{end}}
</tbody>
</table>

<div id="archived-payments-page-select" class="page-select"></div>

</div>
</div>

</div>

{{end}}
4 changes: 4 additions & 0 deletions gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ func (ui *GUI) route() {
guiRouter.HandleFunc("/account/{accountID}/payments/pending", ui.paginatedPendingPaymentsByAccount).Methods("GET")
guiRouter.HandleFunc("/account/{accountID}/payments/archived", ui.paginatedArchivedPaymentsByAccount).Methods("GET")

// Paginated endpoints which require admin authentication.
guiRouter.HandleFunc("/admin/payments/pending", ui.paginatedPendingPoolPayments).Methods("GET")
guiRouter.HandleFunc("/admin/payments/archived", ui.paginatedArchivedPoolPayments).Methods("GET")

// Websocket endpoint allows the GUI to receive updated values.
guiRouter.HandleFunc("/ws", ui.websocketServer.registerClient).Methods("GET")
}
Expand Down
70 changes: 70 additions & 0 deletions gui/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"net/http"
"strconv"

"github.com/decred/dcrpool/pool"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
)

type paginationPayload struct {
Expand Down Expand Up @@ -212,3 +214,71 @@ func (ui *GUI) paginatedArchivedPaymentsByAccount(w http.ResponseWriter, r *http
Data: payments,
})
}

// paginatedArchivedPoolPayments is the handler for "GET
// /admin/payments/archived". It uses parameters pageNumber, pageSize and
// accountID to prepare a json payload describing payments made to the pool, as
// well as the total count of all paid payments.
// Returns an error if the current session is not authenticated as an admin.
func (ui *GUI) paginatedArchivedPoolPayments(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value(sessionKey).(*sessions.Session)

if session.Values["IsAdmin"] != true {
log.Warn("Unauthorized access")
w.WriteHeader(http.StatusUnauthorized)
return
}

first, last, err := getPaginationParams(r)
if err != nil {
log.Warn(err)
w.WriteHeader(http.StatusBadRequest)
return
}

count, payments, err := ui.cache.getArchivedPayments(first, last, pool.PoolFeesK)
if err != nil {
log.Warn(err)
w.WriteHeader(http.StatusBadRequest)
return
}

sendJSONResponse(w, paginationPayload{
Count: count,
Data: payments,
})
}

// paginatedPendingPoolPayments is the handler for "GET
// /admin/payments/pending". It uses parameters pageNumber, pageSize and
// accountID to prepare a json payload describing unpaid payments due to the
// pool, as well as the total count of all unpaid payments.
// Returns an error if the current session is not authenticated as an admin.
func (ui *GUI) paginatedPendingPoolPayments(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value(sessionKey).(*sessions.Session)

if session.Values["IsAdmin"] != true {
log.Warn("Unauthorized access")
w.WriteHeader(http.StatusUnauthorized)
return
}

first, last, err := getPaginationParams(r)
if err != nil {
log.Warn(err)
w.WriteHeader(http.StatusBadRequest)
return
}

count, payments, err := ui.cache.getPendingPayments(first, last, pool.PoolFeesK)
if err != nil {
log.Warn(err)
w.WriteHeader(http.StatusBadRequest)
return
}

sendJSONResponse(w, paginationPayload{
Count: count,
Data: payments,
})
}
4 changes: 2 additions & 2 deletions pool/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ var (
soloPool = []byte("solopool")
// csrfSecret is the CSRF secret key.
csrfSecret = []byte("csrfsecret")
// poolFeesK is the key used to track pool fee payouts.
poolFeesK = "fees"
// PoolFeesK is the key used to track pool fee payouts.
PoolFeesK = "fees"
// backup is the database backup file name.
backupFile = "backup.kv"
)
Expand Down
4 changes: 2 additions & 2 deletions pool/paymentmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ func (pm *PaymentMgr) calculatePayments(ratios map[string]*big.Rat, source *Paym
}

// Add a payout entry for pool fees.
feePayment := NewPayment(poolFeesK, source, fee, height, estMaturity)
feePayment := NewPayment(PoolFeesK, source, fee, height, estMaturity)
payments = append(payments, feePayment)

return payments, feePayment.CreatedOn, nil
Expand Down Expand Up @@ -825,7 +825,7 @@ func (pm *PaymentMgr) payDividends(ctx context.Context, height uint32) error {

// Generate the outputs paying dividends and fees.
for _, pmt := range set {
if pmt.Account == poolFeesK {
if pmt.Account == PoolFeesK {
_, ok := outputs[feeAddr.String()]
if !ok {
outputs[feeAddr.String()] = pmt.Amount
Expand Down
Loading

0 comments on commit a41ea95

Please sign in to comment.