Remove global logger from webapi.
This commit is contained in:
parent
7f72caafe7
commit
ebcfe0e5e9
@ -46,14 +46,14 @@ type searchResult struct {
|
||||
MaxVoteChanges int
|
||||
}
|
||||
|
||||
func dcrdStatus(c *gin.Context) DcrdStatus {
|
||||
func (s *Server) dcrdStatus(c *gin.Context) DcrdStatus {
|
||||
hostname := c.MustGet(dcrdHostKey).(string)
|
||||
status := DcrdStatus{Host: hostname}
|
||||
|
||||
dcrdClient := c.MustGet(dcrdKey).(*rpc.DcrdRPC)
|
||||
dcrdErr := c.MustGet(dcrdErrorKey)
|
||||
if dcrdErr != nil {
|
||||
log.Errorf("could not get dcrd client: %v", dcrdErr.(error))
|
||||
s.log.Errorf("Could not get dcrd client: %v", dcrdErr.(error))
|
||||
return status
|
||||
}
|
||||
|
||||
@ -61,7 +61,7 @@ func dcrdStatus(c *gin.Context) DcrdStatus {
|
||||
|
||||
bestBlock, err := dcrdClient.GetBestBlockHeader()
|
||||
if err != nil {
|
||||
log.Errorf("could not get dcrd best block header: %v", err)
|
||||
s.log.Errorf("Could not get dcrd best block header: %v", err)
|
||||
status.BestBlockError = true
|
||||
return status
|
||||
}
|
||||
@ -71,7 +71,7 @@ func dcrdStatus(c *gin.Context) DcrdStatus {
|
||||
return status
|
||||
}
|
||||
|
||||
func walletStatus(c *gin.Context) map[string]WalletStatus {
|
||||
func (s *Server) walletStatus(c *gin.Context) map[string]WalletStatus {
|
||||
walletClients := c.MustGet(walletsKey).([]*rpc.WalletRPC)
|
||||
failedWalletClients := c.MustGet(failedWalletsKey).([]string)
|
||||
|
||||
@ -81,7 +81,7 @@ func walletStatus(c *gin.Context) map[string]WalletStatus {
|
||||
|
||||
walletInfo, err := v.WalletInfo()
|
||||
if err != nil {
|
||||
log.Errorf("dcrwallet.WalletInfo error (wallet=%s): %v", v.String(), err)
|
||||
s.log.Errorf("dcrwallet.WalletInfo error (wallet=%s): %v", v.String(), err)
|
||||
ws.InfoError = true
|
||||
} else {
|
||||
ws.DaemonConnected = walletInfo.DaemonConnected
|
||||
@ -92,7 +92,7 @@ func walletStatus(c *gin.Context) map[string]WalletStatus {
|
||||
|
||||
height, err := v.GetBestBlockHeight()
|
||||
if err != nil {
|
||||
log.Errorf("dcrwallet.GetBestBlockHeight error (wallet=%s): %v", v.String(), err)
|
||||
s.log.Errorf("dcrwallet.GetBestBlockHeight error (wallet=%s): %v", v.String(), err)
|
||||
ws.BestBlockError = true
|
||||
} else {
|
||||
ws.BestBlockHeight = height
|
||||
@ -112,7 +112,7 @@ func walletStatus(c *gin.Context) map[string]WalletStatus {
|
||||
func (s *Server) statusJSON(c *gin.Context) {
|
||||
httpStatus := http.StatusOK
|
||||
|
||||
wallets := walletStatus(c)
|
||||
wallets := s.walletStatus(c)
|
||||
|
||||
// Respond with HTTP status 500 if any voting wallets have issues.
|
||||
for _, wallet := range wallets {
|
||||
@ -127,7 +127,7 @@ func (s *Server) statusJSON(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
dcrd := dcrdStatus(c)
|
||||
dcrd := s.dcrdStatus(c)
|
||||
|
||||
// Respond with HTTP status 500 if dcrd has issues.
|
||||
if !dcrd.Connected || dcrd.BestBlockError {
|
||||
@ -145,8 +145,8 @@ func (s *Server) adminPage(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "admin.html", gin.H{
|
||||
"WebApiCache": s.cache.getData(),
|
||||
"WebApiCfg": s.cfg,
|
||||
"WalletStatus": walletStatus(c),
|
||||
"DcrdStatus": dcrdStatus(c),
|
||||
"WalletStatus": s.walletStatus(c),
|
||||
"DcrdStatus": s.dcrdStatus(c),
|
||||
})
|
||||
}
|
||||
|
||||
@ -157,21 +157,21 @@ func (s *Server) ticketSearch(c *gin.Context) {
|
||||
|
||||
ticket, found, err := s.db.GetTicketByHash(hash)
|
||||
if err != nil {
|
||||
log.Errorf("db.GetTicketByHash error (ticketHash=%s): %v", hash, err)
|
||||
s.log.Errorf("db.GetTicketByHash error (ticketHash=%s): %v", hash, err)
|
||||
c.String(http.StatusInternalServerError, "Error getting ticket from db")
|
||||
return
|
||||
}
|
||||
|
||||
voteChanges, err := s.db.GetVoteChanges(hash)
|
||||
if err != nil {
|
||||
log.Errorf("db.GetVoteChanges error (ticketHash=%s): %v", hash, err)
|
||||
s.log.Errorf("db.GetVoteChanges error (ticketHash=%s): %v", hash, err)
|
||||
c.String(http.StatusInternalServerError, "Error getting vote changes from db")
|
||||
return
|
||||
}
|
||||
|
||||
altSignAddrData, err := s.db.AltSignAddrData(hash)
|
||||
if err != nil {
|
||||
log.Errorf("db.AltSignAddrData error (ticketHash=%s): %v", hash, err)
|
||||
s.log.Errorf("db.AltSignAddrData error (ticketHash=%s): %v", hash, err)
|
||||
c.String(http.StatusInternalServerError, "Error getting alt sig from db")
|
||||
return
|
||||
}
|
||||
@ -187,8 +187,8 @@ func (s *Server) ticketSearch(c *gin.Context) {
|
||||
},
|
||||
"WebApiCache": s.cache.getData(),
|
||||
"WebApiCfg": s.cfg,
|
||||
"WalletStatus": walletStatus(c),
|
||||
"DcrdStatus": dcrdStatus(c),
|
||||
"WalletStatus": s.walletStatus(c),
|
||||
"DcrdStatus": s.dcrdStatus(c),
|
||||
})
|
||||
}
|
||||
|
||||
@ -198,7 +198,7 @@ func (s *Server) adminLogin(c *gin.Context) {
|
||||
password := c.PostForm("password")
|
||||
|
||||
if password != s.cfg.AdminPass {
|
||||
log.Warnf("Failed login attempt from %s", c.ClientIP())
|
||||
s.log.Warnf("Failed login attempt from %s", c.ClientIP())
|
||||
c.HTML(http.StatusUnauthorized, "login.html", gin.H{
|
||||
"WebApiCache": s.cache.getData(),
|
||||
"WebApiCfg": s.cfg,
|
||||
@ -207,13 +207,13 @@ func (s *Server) adminLogin(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
setAdminStatus(true, c)
|
||||
s.setAdminStatus(true, c)
|
||||
}
|
||||
|
||||
// adminLogout is the handler for "POST /admin/logout". The current session will
|
||||
// have its admin authentication removed.
|
||||
func (s *Server) adminLogout(c *gin.Context) {
|
||||
setAdminStatus(nil, c)
|
||||
s.setAdminStatus(nil, c)
|
||||
}
|
||||
|
||||
// downloadDatabaseBackup is the handler for "GET /backup". A binary
|
||||
@ -221,7 +221,7 @@ func (s *Server) adminLogout(c *gin.Context) {
|
||||
func (s *Server) downloadDatabaseBackup(c *gin.Context) {
|
||||
err := s.db.BackupDB(c.Writer)
|
||||
if err != nil {
|
||||
log.Errorf("Error backing up database: %v", err)
|
||||
s.log.Errorf("Error backing up database: %v", err)
|
||||
// Don't write any http body here because Content-Length has already
|
||||
// been set in db.BackupDB. Status is enough to indicate an error.
|
||||
c.Status(http.StatusInternalServerError)
|
||||
@ -230,12 +230,12 @@ func (s *Server) downloadDatabaseBackup(c *gin.Context) {
|
||||
|
||||
// setAdminStatus stores the authentication status of the current session and
|
||||
// redirects the client to GET /admin.
|
||||
func setAdminStatus(admin interface{}, c *gin.Context) {
|
||||
func (s *Server) setAdminStatus(admin interface{}, c *gin.Context) {
|
||||
session := c.MustGet(sessionKey).(*sessions.Session)
|
||||
session.Values["admin"] = admin
|
||||
err := session.Save(c.Request, c.Writer)
|
||||
if err != nil {
|
||||
log.Errorf("Error saving session: %v", err)
|
||||
s.log.Errorf("Error saving session: %v", err)
|
||||
c.String(http.StatusInternalServerError, "Error saving session")
|
||||
return
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/decred/dcrd/dcrutil/v4"
|
||||
"github.com/decred/slog"
|
||||
)
|
||||
|
||||
func addressURL(blockExplorerURL string) func(string) string {
|
||||
@ -39,15 +40,17 @@ func stripWss(input string) string {
|
||||
return input
|
||||
}
|
||||
|
||||
func indentJSON(input string) template.HTML {
|
||||
var indented bytes.Buffer
|
||||
err := json.Indent(&indented, []byte(input), "<br/>", " ")
|
||||
if err != nil {
|
||||
log.Errorf("Failed to indent JSON: %w", err)
|
||||
return template.HTML(input)
|
||||
}
|
||||
func indentJSON(log slog.Logger) func(string) template.HTML {
|
||||
return func(input string) template.HTML {
|
||||
var indented bytes.Buffer
|
||||
err := json.Indent(&indented, []byte(input), "<br/>", " ")
|
||||
if err != nil {
|
||||
log.Errorf("Failed to indent JSON: %w", err)
|
||||
return template.HTML(input)
|
||||
}
|
||||
|
||||
return template.HTML(indented.String())
|
||||
return template.HTML(indented.String())
|
||||
}
|
||||
}
|
||||
|
||||
func atomsToDCR(atoms int64) string {
|
||||
|
||||
@ -83,7 +83,7 @@ func (s *Server) feeAddress(c *gin.Context) {
|
||||
dcrdClient := c.MustGet(dcrdKey).(*rpc.DcrdRPC)
|
||||
dcrdErr := c.MustGet(dcrdErrorKey)
|
||||
if dcrdErr != nil {
|
||||
log.Errorf("%s: could not get dcrd client: %v", funcName, dcrdErr.(error))
|
||||
s.log.Errorf("%s: Could not get dcrd client: %v", funcName, dcrdErr.(error))
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
}
|
||||
@ -96,7 +96,7 @@ func (s *Server) feeAddress(c *gin.Context) {
|
||||
|
||||
var request feeAddressRequest
|
||||
if err := binding.JSON.BindBody(reqBytes, &request); err != nil {
|
||||
log.Warnf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err)
|
||||
s.log.Warnf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err)
|
||||
s.sendErrorWithMsg(err.Error(), errBadRequest, c)
|
||||
return
|
||||
}
|
||||
@ -108,7 +108,7 @@ func (s *Server) feeAddress(c *gin.Context) {
|
||||
(ticket.FeeTxStatus == database.FeeReceieved ||
|
||||
ticket.FeeTxStatus == database.FeeBroadcast ||
|
||||
ticket.FeeTxStatus == database.FeeConfirmed) {
|
||||
log.Warnf("%s: Fee tx already received (clientIP=%s, ticketHash=%s)",
|
||||
s.log.Warnf("%s: Fee tx already received (clientIP=%s, ticketHash=%s)",
|
||||
funcName, c.ClientIP(), ticket.Hash)
|
||||
s.sendError(errFeeAlreadyReceived, c)
|
||||
return
|
||||
@ -117,7 +117,7 @@ func (s *Server) feeAddress(c *gin.Context) {
|
||||
// Get ticket details.
|
||||
rawTicket, err := dcrdClient.GetRawTransaction(ticketHash)
|
||||
if err != nil {
|
||||
log.Errorf("%s: dcrd.GetRawTransaction for ticket failed (ticketHash=%s): %v", funcName, ticketHash, err)
|
||||
s.log.Errorf("%s: dcrd.GetRawTransaction for ticket failed (ticketHash=%s): %v", funcName, ticketHash, err)
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
}
|
||||
@ -125,12 +125,12 @@ func (s *Server) feeAddress(c *gin.Context) {
|
||||
// Ensure this ticket is eligible to vote at some point in the future.
|
||||
canVote, err := canTicketVote(rawTicket, dcrdClient, s.cfg.NetParams)
|
||||
if err != nil {
|
||||
log.Errorf("%s: canTicketVote error (ticketHash=%s): %v", funcName, ticketHash, err)
|
||||
s.log.Errorf("%s: canTicketVote error (ticketHash=%s): %v", funcName, ticketHash, err)
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
}
|
||||
if !canVote {
|
||||
log.Warnf("%s: Unvotable ticket (clientIP=%s, ticketHash=%s)",
|
||||
s.log.Warnf("%s: Unvotable ticket (clientIP=%s, ticketHash=%s)",
|
||||
funcName, c.ClientIP(), ticketHash)
|
||||
s.sendError(errTicketCannotVote, c)
|
||||
return
|
||||
@ -144,7 +144,7 @@ func (s *Server) feeAddress(c *gin.Context) {
|
||||
if ticket.FeeExpired() {
|
||||
newFee, err := s.getCurrentFee(dcrdClient)
|
||||
if err != nil {
|
||||
log.Errorf("%s: getCurrentFee error (ticketHash=%s): %v", funcName, ticket.Hash, err)
|
||||
s.log.Errorf("%s: getCurrentFee error (ticketHash=%s): %v", funcName, ticket.Hash, err)
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
}
|
||||
@ -153,12 +153,12 @@ func (s *Server) feeAddress(c *gin.Context) {
|
||||
|
||||
err = s.db.UpdateTicket(ticket)
|
||||
if err != nil {
|
||||
log.Errorf("%s: db.UpdateTicket error, failed to update fee expiry (ticketHash=%s): %v",
|
||||
s.log.Errorf("%s: db.UpdateTicket error, failed to update fee expiry (ticketHash=%s): %v",
|
||||
funcName, ticket.Hash, err)
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
}
|
||||
log.Debugf("%s: Expired fee updated (newFeeAmt=%s, ticketHash=%s)",
|
||||
s.log.Debugf("%s: Expired fee updated (newFeeAmt=%s, ticketHash=%s)",
|
||||
funcName, newFee, ticket.Hash)
|
||||
}
|
||||
s.sendJSONResponse(feeAddressResponse{
|
||||
@ -177,14 +177,14 @@ func (s *Server) feeAddress(c *gin.Context) {
|
||||
|
||||
fee, err := s.getCurrentFee(dcrdClient)
|
||||
if err != nil {
|
||||
log.Errorf("%s: getCurrentFee error (ticketHash=%s): %v", funcName, ticketHash, err)
|
||||
s.log.Errorf("%s: getCurrentFee error (ticketHash=%s): %v", funcName, ticketHash, err)
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
}
|
||||
|
||||
newAddress, newAddressIdx, err := s.getNewFeeAddress()
|
||||
if err != nil {
|
||||
log.Errorf("%s: getNewFeeAddress error (ticketHash=%s): %v", funcName, ticketHash, err)
|
||||
s.log.Errorf("%s: getNewFeeAddress error (ticketHash=%s): %v", funcName, ticketHash, err)
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
}
|
||||
@ -215,12 +215,12 @@ func (s *Server) feeAddress(c *gin.Context) {
|
||||
|
||||
err = s.db.InsertNewTicket(dbTicket)
|
||||
if err != nil {
|
||||
log.Errorf("%s: db.InsertNewTicket failed (ticketHash=%s): %v", funcName, ticketHash, err)
|
||||
s.log.Errorf("%s: db.InsertNewTicket failed (ticketHash=%s): %v", funcName, ticketHash, err)
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
}
|
||||
|
||||
log.Debugf("%s: Fee address created for new ticket: (tktConfirmed=%t, feeAddrIdx=%d, "+
|
||||
s.log.Debugf("%s: Fee address created for new ticket: (tktConfirmed=%t, feeAddrIdx=%d, "+
|
||||
"feeAddr=%s, feeAmt=%s, ticketHash=%s)",
|
||||
funcName, confirmed, newAddressIdx, newAddress, fee, ticketHash)
|
||||
|
||||
|
||||
@ -21,7 +21,7 @@ import (
|
||||
// withSession middleware adds a gorilla session to the request context for
|
||||
// downstream handlers to make use of. Sessions are used by admin pages to
|
||||
// maintain authentication status.
|
||||
func withSession(store *sessions.CookieStore) gin.HandlerFunc {
|
||||
func (s *Server) withSession(store *sessions.CookieStore) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
session, err := store.Get(c.Request, "vspd-session")
|
||||
if err != nil {
|
||||
@ -29,18 +29,18 @@ func withSession(store *sessions.CookieStore) gin.HandlerFunc {
|
||||
// common during development (eg. when using the test harness) but
|
||||
// it should not occur in production.
|
||||
if strings.Contains(err.Error(), "securecookie: the value is not valid") {
|
||||
log.Warn("Cookie secret has changed. Generating new session.")
|
||||
s.log.Warn("Cookie secret has changed. Generating new session.")
|
||||
|
||||
// Persist the newly generated session.
|
||||
err = store.Save(c.Request, c.Writer, session)
|
||||
if err != nil {
|
||||
log.Errorf("Error saving session: %v", err)
|
||||
s.log.Errorf("Error saving session: %v", err)
|
||||
c.String(http.StatusInternalServerError, "Error saving session")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
} else {
|
||||
log.Errorf("Session error: %v", err)
|
||||
s.log.Errorf("Session error: %v", err)
|
||||
c.String(http.StatusInternalServerError, "Error getting session")
|
||||
c.Abort()
|
||||
return
|
||||
@ -69,7 +69,7 @@ func (s *Server) requireAdmin(c *gin.Context) {
|
||||
|
||||
// withDcrdClient middleware adds a dcrd client to the request context for
|
||||
// downstream handlers to make use of.
|
||||
func withDcrdClient(dcrd rpc.DcrdConnect) gin.HandlerFunc {
|
||||
func (s *Server) withDcrdClient(dcrd rpc.DcrdConnect) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
client, hostname, err := dcrd.Client()
|
||||
// Don't handle the error here, add it to the context and let downstream
|
||||
@ -83,13 +83,13 @@ func withDcrdClient(dcrd rpc.DcrdConnect) gin.HandlerFunc {
|
||||
// withWalletClients middleware attempts to add voting wallet clients to the
|
||||
// request context for downstream handlers to make use of. Downstream handlers
|
||||
// must handle the case where no wallet clients are connected.
|
||||
func withWalletClients(wallets rpc.WalletConnect) gin.HandlerFunc {
|
||||
func (s *Server) withWalletClients(wallets rpc.WalletConnect) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
clients, failedConnections := wallets.Clients()
|
||||
if len(clients) == 0 {
|
||||
log.Error("Could not connect to any wallets")
|
||||
s.log.Error("Could not connect to any wallets")
|
||||
} else if len(failedConnections) > 0 {
|
||||
log.Errorf("Failed to connect to %d wallet(s), proceeding with only %d",
|
||||
s.log.Errorf("Failed to connect to %d wallet(s), proceeding with only %d",
|
||||
len(failedConnections), len(clients))
|
||||
}
|
||||
c.Set(walletsKey, clients)
|
||||
@ -120,7 +120,7 @@ func (s *Server) broadcastTicket(c *gin.Context) {
|
||||
// Read request bytes.
|
||||
reqBytes, err := drainAndReplaceBody(c.Request)
|
||||
if err != nil {
|
||||
log.Warnf("%s: Error reading request (clientIP=%s): %v", funcName, c.ClientIP(), err)
|
||||
s.log.Warnf("%s: Error reading request (clientIP=%s): %v", funcName, c.ClientIP(), err)
|
||||
s.sendErrorWithMsg(err.Error(), errBadRequest, c)
|
||||
return
|
||||
}
|
||||
@ -132,7 +132,7 @@ func (s *Server) broadcastTicket(c *gin.Context) {
|
||||
ParentHex string `json:"parenthex" binding:"required"`
|
||||
}
|
||||
if err := binding.JSON.BindBody(reqBytes, &request); err != nil {
|
||||
log.Warnf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err)
|
||||
s.log.Warnf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err)
|
||||
s.sendErrorWithMsg(err.Error(), errBadRequest, c)
|
||||
return
|
||||
}
|
||||
@ -140,7 +140,7 @@ func (s *Server) broadcastTicket(c *gin.Context) {
|
||||
// Ensure the provided ticket hex is a valid ticket.
|
||||
msgTx, err := decodeTransaction(request.TicketHex)
|
||||
if err != nil {
|
||||
log.Errorf("%s: Failed to decode ticket hex (ticketHash=%s): %v",
|
||||
s.log.Errorf("%s: Failed to decode ticket hex (ticketHash=%s): %v",
|
||||
funcName, request.TicketHash, err)
|
||||
s.sendErrorWithMsg("cannot decode ticket hex", errBadRequest, c)
|
||||
return
|
||||
@ -148,7 +148,7 @@ func (s *Server) broadcastTicket(c *gin.Context) {
|
||||
|
||||
err = isValidTicket(msgTx)
|
||||
if err != nil {
|
||||
log.Warnf("%s: Invalid ticket (clientIP=%s, ticketHash=%s): %v",
|
||||
s.log.Warnf("%s: Invalid ticket (clientIP=%s, ticketHash=%s): %v",
|
||||
funcName, c.ClientIP(), request.TicketHash, err)
|
||||
s.sendError(errInvalidTicket, c)
|
||||
return
|
||||
@ -156,7 +156,7 @@ func (s *Server) broadcastTicket(c *gin.Context) {
|
||||
|
||||
// Ensure hex matches hash.
|
||||
if msgTx.TxHash().String() != request.TicketHash {
|
||||
log.Warnf("%s: Ticket hex/hash mismatch (clientIP=%s, ticketHash=%s)",
|
||||
s.log.Warnf("%s: Ticket hex/hash mismatch (clientIP=%s, ticketHash=%s)",
|
||||
funcName, c.ClientIP(), request.TicketHash)
|
||||
s.sendErrorWithMsg("ticket hex does not match hash", errBadRequest, c)
|
||||
return
|
||||
@ -165,7 +165,7 @@ func (s *Server) broadcastTicket(c *gin.Context) {
|
||||
// Ensure the provided parent hex is a valid tx.
|
||||
parentTx, err := decodeTransaction(request.ParentHex)
|
||||
if err != nil {
|
||||
log.Errorf("%s: Failed to decode parent hex (ticketHash=%s): %v", funcName, request.TicketHash, err)
|
||||
s.log.Errorf("%s: Failed to decode parent hex (ticketHash=%s): %v", funcName, request.TicketHash, err)
|
||||
s.sendErrorWithMsg("cannot decode parent hex", errBadRequest, c)
|
||||
return
|
||||
}
|
||||
@ -175,7 +175,7 @@ func (s *Server) broadcastTicket(c *gin.Context) {
|
||||
dcrdClient := c.MustGet(dcrdKey).(*rpc.DcrdRPC)
|
||||
dcrdErr := c.MustGet(dcrdErrorKey)
|
||||
if dcrdErr != nil {
|
||||
log.Errorf("%s: could not get dcrd client: %v", funcName, dcrdErr.(error))
|
||||
s.log.Errorf("%s: Could not get dcrd client: %v", funcName, dcrdErr.(error))
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
}
|
||||
@ -200,22 +200,22 @@ func (s *Server) broadcastTicket(c *gin.Context) {
|
||||
}
|
||||
|
||||
if !found {
|
||||
log.Errorf("%s: Invalid ticket parent (ticketHash=%s)", funcName, request.TicketHash)
|
||||
s.log.Errorf("%s: Invalid ticket parent (ticketHash=%s)", funcName, request.TicketHash)
|
||||
s.sendErrorWithMsg("invalid ticket parent", errBadRequest, c)
|
||||
return
|
||||
}
|
||||
|
||||
log.Debugf("%s: Broadcasting parent tx %s (ticketHash=%s)", funcName, parentHash, request.TicketHash)
|
||||
s.log.Debugf("%s: Broadcasting parent tx %s (ticketHash=%s)", funcName, parentHash, request.TicketHash)
|
||||
err = dcrdClient.SendRawTransaction(request.ParentHex)
|
||||
if err != nil {
|
||||
log.Errorf("%s: dcrd.SendRawTransaction for parent tx failed (ticketHash=%s): %v",
|
||||
s.log.Errorf("%s: dcrd.SendRawTransaction for parent tx failed (ticketHash=%s): %v",
|
||||
funcName, request.TicketHash, err)
|
||||
s.sendError(errCannotBroadcastTicket, c)
|
||||
return
|
||||
}
|
||||
|
||||
} else {
|
||||
log.Errorf("%s: dcrd.GetRawTransaction for ticket parent failed (ticketHash=%s): %v",
|
||||
s.log.Errorf("%s: dcrd.GetRawTransaction for ticket parent failed (ticketHash=%s): %v",
|
||||
funcName, request.TicketHash, err)
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
@ -231,16 +231,16 @@ func (s *Server) broadcastTicket(c *gin.Context) {
|
||||
// ErrNoTxInfo means local dcrd is not aware of the ticket. We have the
|
||||
// hex, so we can broadcast it here.
|
||||
if errors.As(err, &e) && e.Code == rpc.ErrNoTxInfo {
|
||||
log.Debugf("%s: Broadcasting ticket (ticketHash=%s)", funcName, request.TicketHash)
|
||||
s.log.Debugf("%s: Broadcasting ticket (ticketHash=%s)", funcName, request.TicketHash)
|
||||
err = dcrdClient.SendRawTransaction(request.TicketHex)
|
||||
if err != nil {
|
||||
log.Errorf("%s: dcrd.SendRawTransaction for ticket failed (ticketHash=%s): %v",
|
||||
s.log.Errorf("%s: dcrd.SendRawTransaction for ticket failed (ticketHash=%s): %v",
|
||||
funcName, request.TicketHash, err)
|
||||
s.sendError(errCannotBroadcastTicket, c)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
log.Errorf("%s: dcrd.GetRawTransaction for ticket failed (ticketHash=%s): %v",
|
||||
s.log.Errorf("%s: dcrd.GetRawTransaction for ticket failed (ticketHash=%s): %v",
|
||||
funcName, request.TicketHash, err)
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
@ -260,7 +260,7 @@ func (s *Server) vspAuth(c *gin.Context) {
|
||||
// Read request bytes.
|
||||
reqBytes, err := drainAndReplaceBody(c.Request)
|
||||
if err != nil {
|
||||
log.Warnf("%s: Error reading request (clientIP=%s): %v", funcName, c.ClientIP(), err)
|
||||
s.log.Warnf("%s: Error reading request (clientIP=%s): %v", funcName, c.ClientIP(), err)
|
||||
s.sendErrorWithMsg(err.Error(), errBadRequest, c)
|
||||
return
|
||||
}
|
||||
@ -274,7 +274,7 @@ func (s *Server) vspAuth(c *gin.Context) {
|
||||
TicketHash string `json:"tickethash" binding:"required"`
|
||||
}
|
||||
if err := binding.JSON.BindBody(reqBytes, &request); err != nil {
|
||||
log.Warnf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err)
|
||||
s.log.Warnf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err)
|
||||
s.sendErrorWithMsg(err.Error(), errBadRequest, c)
|
||||
return
|
||||
}
|
||||
@ -283,7 +283,7 @@ func (s *Server) vspAuth(c *gin.Context) {
|
||||
// Before hitting the db or any RPC, ensure this is a valid ticket hash.
|
||||
err = validateTicketHash(hash)
|
||||
if err != nil {
|
||||
log.Errorf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err)
|
||||
s.log.Errorf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err)
|
||||
s.sendErrorWithMsg("invalid ticket hash", errBadRequest, c)
|
||||
return
|
||||
}
|
||||
@ -291,7 +291,7 @@ func (s *Server) vspAuth(c *gin.Context) {
|
||||
// Check if this ticket already appears in the database.
|
||||
ticket, ticketFound, err := s.db.GetTicketByHash(hash)
|
||||
if err != nil {
|
||||
log.Errorf("%s: db.GetTicketByHash error (ticketHash=%s): %v", funcName, hash, err)
|
||||
s.log.Errorf("%s: db.GetTicketByHash error (ticketHash=%s): %v", funcName, hash, err)
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
}
|
||||
@ -301,7 +301,7 @@ func (s *Server) vspAuth(c *gin.Context) {
|
||||
dcrdClient := c.MustGet(dcrdKey).(*rpc.DcrdRPC)
|
||||
dcrdErr := c.MustGet(dcrdErrorKey)
|
||||
if dcrdErr != nil {
|
||||
log.Errorf("%s: could not get dcrd client: %v", funcName, dcrdErr.(error))
|
||||
s.log.Errorf("%s: Could not get dcrd client: %v", funcName, dcrdErr.(error))
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
}
|
||||
@ -329,7 +329,7 @@ func (s *Server) vspAuth(c *gin.Context) {
|
||||
// Ensure a signature is provided.
|
||||
signature := c.GetHeader("VSP-Client-Signature")
|
||||
if signature == "" {
|
||||
log.Warnf("%s: No VSP-Client-Signature header (clientIP=%s)", funcName, c.ClientIP())
|
||||
s.log.Warnf("%s: No VSP-Client-Signature header (clientIP=%s)", funcName, c.ClientIP())
|
||||
s.sendErrorWithMsg("no VSP-Client-Signature header", errBadRequest, c)
|
||||
return
|
||||
}
|
||||
@ -337,7 +337,7 @@ func (s *Server) vspAuth(c *gin.Context) {
|
||||
// Validate request signature to ensure ticket ownership.
|
||||
err = validateSignature(hash, commitmentAddress, signature, string(reqBytes), s.db, s.cfg.NetParams)
|
||||
if err != nil {
|
||||
log.Errorf("%s: Couldn't validate signature (clientIP=%s, ticketHash=%s): %v",
|
||||
s.log.Errorf("%s: Couldn't validate signature (clientIP=%s, ticketHash=%s): %v",
|
||||
funcName, c.ClientIP(), hash, err)
|
||||
s.sendError(errBadSignature, c)
|
||||
return
|
||||
|
||||
@ -29,7 +29,7 @@ func (s *Server) payFee(c *gin.Context) {
|
||||
dcrdClient := c.MustGet(dcrdKey).(*rpc.DcrdRPC)
|
||||
dcrdErr := c.MustGet(dcrdErrorKey)
|
||||
if dcrdErr != nil {
|
||||
log.Errorf("%s: could not get dcrd client: %v", funcName, dcrdErr.(error))
|
||||
s.log.Errorf("%s: Could not get dcrd client: %v", funcName, dcrdErr.(error))
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
}
|
||||
@ -41,14 +41,14 @@ func (s *Server) payFee(c *gin.Context) {
|
||||
}
|
||||
|
||||
if !knownTicket {
|
||||
log.Warnf("%s: Unknown ticket (clientIP=%s)", funcName, c.ClientIP())
|
||||
s.log.Warnf("%s: Unknown ticket (clientIP=%s)", funcName, c.ClientIP())
|
||||
s.sendError(errUnknownTicket, c)
|
||||
return
|
||||
}
|
||||
|
||||
var request payFeeRequest
|
||||
if err := binding.JSON.BindBody(reqBytes, &request); err != nil {
|
||||
log.Warnf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err)
|
||||
s.log.Warnf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err)
|
||||
s.sendErrorWithMsg(err.Error(), errBadRequest, c)
|
||||
return
|
||||
}
|
||||
@ -57,7 +57,7 @@ func (s *Server) payFee(c *gin.Context) {
|
||||
if ticket.FeeTxStatus == database.FeeReceieved ||
|
||||
ticket.FeeTxStatus == database.FeeBroadcast ||
|
||||
ticket.FeeTxStatus == database.FeeConfirmed {
|
||||
log.Warnf("%s: Fee tx already received (clientIP=%s, ticketHash=%s)",
|
||||
s.log.Warnf("%s: Fee tx already received (clientIP=%s, ticketHash=%s)",
|
||||
funcName, c.ClientIP(), ticket.Hash)
|
||||
s.sendError(errFeeAlreadyReceived, c)
|
||||
return
|
||||
@ -66,7 +66,7 @@ func (s *Server) payFee(c *gin.Context) {
|
||||
// Get ticket details.
|
||||
rawTicket, err := dcrdClient.GetRawTransaction(ticket.Hash)
|
||||
if err != nil {
|
||||
log.Errorf("%s: dcrd.GetRawTransaction for ticket failed (ticketHash=%s): %v", funcName, ticket.Hash, err)
|
||||
s.log.Errorf("%s: dcrd.GetRawTransaction for ticket failed (ticketHash=%s): %v", funcName, ticket.Hash, err)
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
}
|
||||
@ -74,12 +74,12 @@ func (s *Server) payFee(c *gin.Context) {
|
||||
// Ensure this ticket is eligible to vote at some point in the future.
|
||||
canVote, err := canTicketVote(rawTicket, dcrdClient, s.cfg.NetParams)
|
||||
if err != nil {
|
||||
log.Errorf("%s: canTicketVote error (ticketHash=%s): %v", funcName, ticket.Hash, err)
|
||||
s.log.Errorf("%s: canTicketVote error (ticketHash=%s): %v", funcName, ticket.Hash, err)
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
}
|
||||
if !canVote {
|
||||
log.Warnf("%s: Unvotable ticket (clientIP=%s, ticketHash=%s)",
|
||||
s.log.Warnf("%s: Unvotable ticket (clientIP=%s, ticketHash=%s)",
|
||||
funcName, c.ClientIP(), ticket.Hash)
|
||||
s.sendError(errTicketCannotVote, c)
|
||||
return
|
||||
@ -87,7 +87,7 @@ func (s *Server) payFee(c *gin.Context) {
|
||||
|
||||
// Respond early if the fee for this ticket is expired.
|
||||
if ticket.FeeExpired() {
|
||||
log.Warnf("%s: Expired payfee request (clientIP=%s, ticketHash=%s)",
|
||||
s.log.Warnf("%s: Expired payfee request (clientIP=%s, ticketHash=%s)",
|
||||
funcName, c.ClientIP(), ticket.Hash)
|
||||
s.sendError(errFeeExpired, c)
|
||||
return
|
||||
@ -97,7 +97,7 @@ func (s *Server) payFee(c *gin.Context) {
|
||||
votingKey := request.VotingKey
|
||||
votingWIF, err := dcrutil.DecodeWIF(votingKey, s.cfg.NetParams.PrivateKeyID)
|
||||
if err != nil {
|
||||
log.Warnf("%s: Failed to decode WIF (clientIP=%s, ticketHash=%s): %v",
|
||||
s.log.Warnf("%s: Failed to decode WIF (clientIP=%s, ticketHash=%s): %v",
|
||||
funcName, c.ClientIP(), ticket.Hash, err)
|
||||
s.sendError(errInvalidPrivKey, c)
|
||||
return
|
||||
@ -110,7 +110,7 @@ func (s *Server) payFee(c *gin.Context) {
|
||||
err = validConsensusVoteChoices(s.cfg.NetParams, currentVoteVersion(s.cfg.NetParams), request.VoteChoices)
|
||||
if err != nil {
|
||||
validVoteChoices = false
|
||||
log.Warnf("%s: Invalid consensus vote choices (clientIP=%s, ticketHash=%s): %v",
|
||||
s.log.Warnf("%s: Invalid consensus vote choices (clientIP=%s, ticketHash=%s): %v",
|
||||
funcName, c.ClientIP(), ticket.Hash, err)
|
||||
}
|
||||
|
||||
@ -118,7 +118,7 @@ func (s *Server) payFee(c *gin.Context) {
|
||||
err = validTreasuryPolicy(request.TreasuryPolicy)
|
||||
if err != nil {
|
||||
validTreasury = false
|
||||
log.Warnf("%s: Invalid treasury policy (clientIP=%s, ticketHash=%s): %v",
|
||||
s.log.Warnf("%s: Invalid treasury policy (clientIP=%s, ticketHash=%s): %v",
|
||||
funcName, c.ClientIP(), ticket.Hash, err)
|
||||
}
|
||||
|
||||
@ -126,14 +126,14 @@ func (s *Server) payFee(c *gin.Context) {
|
||||
err = validTSpendPolicy(request.TSpendPolicy)
|
||||
if err != nil {
|
||||
validTSpend = false
|
||||
log.Warnf("%s: Invalid tspend policy (clientIP=%s, ticketHash=%s): %v",
|
||||
s.log.Warnf("%s: Invalid tspend policy (clientIP=%s, ticketHash=%s): %v",
|
||||
funcName, c.ClientIP(), ticket.Hash, err)
|
||||
}
|
||||
|
||||
// Validate FeeTx.
|
||||
feeTx, err := decodeTransaction(request.FeeTx)
|
||||
if err != nil {
|
||||
log.Warnf("%s: Failed to decode fee tx hex (clientIP=%s, ticketHash=%s): %v",
|
||||
s.log.Warnf("%s: Failed to decode fee tx hex (clientIP=%s, ticketHash=%s): %v",
|
||||
funcName, c.ClientIP(), ticket.Hash, err)
|
||||
s.sendError(errInvalidFeeTx, c)
|
||||
return
|
||||
@ -141,7 +141,7 @@ func (s *Server) payFee(c *gin.Context) {
|
||||
|
||||
err = blockchain.CheckTransactionSanity(feeTx, s.cfg.NetParams)
|
||||
if err != nil {
|
||||
log.Warnf("%s: Fee tx failed sanity check (clientIP=%s, ticketHash=%s): %v",
|
||||
s.log.Warnf("%s: Fee tx failed sanity check (clientIP=%s, ticketHash=%s): %v",
|
||||
funcName, c.ClientIP(), ticket.Hash, err)
|
||||
s.sendError(errInvalidFeeTx, c)
|
||||
return
|
||||
@ -150,7 +150,7 @@ func (s *Server) payFee(c *gin.Context) {
|
||||
// Decode fee address to get its payment script details.
|
||||
feeAddr, err := stdaddr.DecodeAddress(ticket.FeeAddress, s.cfg.NetParams)
|
||||
if err != nil {
|
||||
log.Errorf("%s: Failed to decode fee address (ticketHash=%s): %v",
|
||||
s.log.Errorf("%s: Failed to decode fee address (ticketHash=%s): %v",
|
||||
funcName, ticket.Hash, err)
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
@ -170,7 +170,7 @@ func (s *Server) payFee(c *gin.Context) {
|
||||
|
||||
// Confirm a fee payment was found.
|
||||
if feePaid == 0 {
|
||||
log.Warnf("%s: Fee tx did not include expected payment (ticketHash=%s, feeAddress=%s, clientIP=%s)",
|
||||
s.log.Warnf("%s: Fee tx did not include expected payment (ticketHash=%s, feeAddress=%s, clientIP=%s)",
|
||||
funcName, ticket.Hash, ticket.FeeAddress, c.ClientIP())
|
||||
s.sendErrorWithMsg(
|
||||
fmt.Sprintf("feetx did not include any payments for fee address %s", ticket.FeeAddress),
|
||||
@ -181,7 +181,7 @@ func (s *Server) payFee(c *gin.Context) {
|
||||
// Confirm fee payment is equal to or larger than the minimum expected.
|
||||
minFee := dcrutil.Amount(ticket.FeeAmount)
|
||||
if feePaid < minFee {
|
||||
log.Warnf("%s: Fee too small (ticketHash=%s, clientIP=%s): was %s, expected minimum %s",
|
||||
s.log.Warnf("%s: Fee too small (ticketHash=%s, clientIP=%s): was %s, expected minimum %s",
|
||||
funcName, ticket.Hash, c.ClientIP(), feePaid, minFee)
|
||||
s.sendError(errFeeTooSmall, c)
|
||||
return
|
||||
@ -191,7 +191,7 @@ func (s *Server) payFee(c *gin.Context) {
|
||||
pkHash := stdaddr.Hash160(votingWIF.PubKey())
|
||||
wifAddr, err := stdaddr.NewAddressPubKeyHashEcdsaSecp256k1V0(pkHash, s.cfg.NetParams)
|
||||
if err != nil {
|
||||
log.Errorf("%s: Failed to get voting address from WIF (ticketHash=%s, clientIP=%s): %v",
|
||||
s.log.Errorf("%s: Failed to get voting address from WIF (ticketHash=%s, clientIP=%s): %v",
|
||||
funcName, ticket.Hash, c.ClientIP(), err)
|
||||
s.sendError(errInvalidPrivKey, c)
|
||||
return
|
||||
@ -202,7 +202,7 @@ func (s *Server) payFee(c *gin.Context) {
|
||||
// Decode ticket transaction to get its voting rights script.
|
||||
ticketTx, err := decodeTransaction(rawTicket.Hex)
|
||||
if err != nil {
|
||||
log.Warnf("%s: Failed to decode ticket hex (ticketHash=%s): %v",
|
||||
s.log.Warnf("%s: Failed to decode ticket hex (ticketHash=%s): %v",
|
||||
funcName, ticket.Hash, err)
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
@ -214,7 +214,7 @@ func (s *Server) payFee(c *gin.Context) {
|
||||
// Ensure provided voting WIF matches the actual voting address of the
|
||||
// ticket. Both script and script version should match.
|
||||
if actualScriptVer != wantScriptVer || !bytes.Equal(actualScript, wantScript) {
|
||||
log.Warnf("%s: Voting address does not match provided private key: (ticketHash=%s)",
|
||||
s.log.Warnf("%s: Voting address does not match provided private key: (ticketHash=%s)",
|
||||
funcName, ticket.Hash)
|
||||
s.sendErrorWithMsg("voting address does not match provided private key",
|
||||
errInvalidPrivKey, c)
|
||||
@ -244,19 +244,19 @@ func (s *Server) payFee(c *gin.Context) {
|
||||
|
||||
err = s.db.UpdateTicket(ticket)
|
||||
if err != nil {
|
||||
log.Errorf("%s: db.UpdateTicket error, failed to set fee tx (ticketHash=%s): %v",
|
||||
s.log.Errorf("%s: db.UpdateTicket error, failed to set fee tx (ticketHash=%s): %v",
|
||||
funcName, ticket.Hash, err)
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
}
|
||||
|
||||
log.Debugf("%s: Fee tx received for ticket (minExpectedFee=%v, feePaid=%v, ticketHash=%s)",
|
||||
s.log.Debugf("%s: Fee tx received for ticket (minExpectedFee=%v, feePaid=%v, ticketHash=%s)",
|
||||
funcName, minFee, feePaid, ticket.Hash)
|
||||
|
||||
if ticket.Confirmed {
|
||||
err = dcrdClient.SendRawTransaction(request.FeeTx)
|
||||
if err != nil {
|
||||
log.Errorf("%s: dcrd.SendRawTransaction for fee tx failed (ticketHash=%s): %v",
|
||||
s.log.Errorf("%s: dcrd.SendRawTransaction for fee tx failed (ticketHash=%s): %v",
|
||||
funcName, ticket.Hash, err)
|
||||
|
||||
ticket.FeeTxStatus = database.FeeError
|
||||
@ -270,7 +270,7 @@ func (s *Server) payFee(c *gin.Context) {
|
||||
|
||||
err = s.db.UpdateTicket(ticket)
|
||||
if err != nil {
|
||||
log.Errorf("%s: db.UpdateTicket error, failed to set fee tx error (ticketHash=%s): %v",
|
||||
s.log.Errorf("%s: db.UpdateTicket error, failed to set fee tx error (ticketHash=%s): %v",
|
||||
funcName, ticket.Hash, err)
|
||||
}
|
||||
|
||||
@ -281,13 +281,13 @@ func (s *Server) payFee(c *gin.Context) {
|
||||
|
||||
err = s.db.UpdateTicket(ticket)
|
||||
if err != nil {
|
||||
log.Errorf("%s: db.UpdateTicket error, failed to set fee tx as broadcast (ticketHash=%s): %v",
|
||||
s.log.Errorf("%s: db.UpdateTicket error, failed to set fee tx as broadcast (ticketHash=%s): %v",
|
||||
funcName, ticket.Hash, err)
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
}
|
||||
|
||||
log.Debugf("%s: Fee tx broadcast for ticket (ticketHash=%s, feeHash=%s)",
|
||||
s.log.Debugf("%s: Fee tx broadcast for ticket (ticketHash=%s, feeHash=%s)",
|
||||
funcName, ticket.Hash, ticket.FeeTxHash)
|
||||
}
|
||||
|
||||
@ -307,6 +307,6 @@ func (s *Server) payFee(c *gin.Context) {
|
||||
ResponseSignature: respSig,
|
||||
})
|
||||
if err != nil {
|
||||
log.Errorf("%s: Failed to store vote change record (ticketHash=%s): %v", err)
|
||||
s.log.Errorf("%s: Failed to store vote change record (ticketHash=%s): %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,7 +33,7 @@ func (s *Server) setAltSignAddr(c *gin.Context) {
|
||||
dcrdClient := c.MustGet(dcrdKey).(Node)
|
||||
dcrdErr := c.MustGet(dcrdErrorKey)
|
||||
if dcrdErr != nil {
|
||||
log.Errorf("%s: could not get dcrd client: %v", funcName, dcrdErr.(error))
|
||||
s.log.Errorf("%s: Could not get dcrd client: %v", funcName, dcrdErr.(error))
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
}
|
||||
@ -46,7 +46,7 @@ func (s *Server) setAltSignAddr(c *gin.Context) {
|
||||
|
||||
var request setAltSignAddrRequest
|
||||
if err := binding.JSON.BindBody(reqBytes, &request); err != nil {
|
||||
log.Warnf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err)
|
||||
s.log.Warnf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err)
|
||||
s.sendErrorWithMsg(err.Error(), errBadRequest, c)
|
||||
return
|
||||
}
|
||||
@ -55,13 +55,13 @@ func (s *Server) setAltSignAddr(c *gin.Context) {
|
||||
|
||||
currentData, err := s.db.AltSignAddrData(ticketHash)
|
||||
if err != nil {
|
||||
log.Errorf("%s: db.AltSignAddrData (ticketHash=%s): %v", funcName, ticketHash, err)
|
||||
s.log.Errorf("%s: db.AltSignAddrData (ticketHash=%s): %v", funcName, ticketHash, err)
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
}
|
||||
if currentData != nil {
|
||||
msg := "alternate sign address data already exists"
|
||||
log.Warnf("%s: %s (ticketHash=%s)", funcName, msg, ticketHash)
|
||||
s.log.Warnf("%s: %s (ticketHash=%s)", funcName, msg, ticketHash)
|
||||
s.sendErrorWithMsg(msg, errBadRequest, c)
|
||||
return
|
||||
|
||||
@ -70,12 +70,12 @@ func (s *Server) setAltSignAddr(c *gin.Context) {
|
||||
// Fail fast if the pubkey doesn't decode properly.
|
||||
addr, err := stdaddr.DecodeAddressV0(altSignAddr, s.cfg.NetParams)
|
||||
if err != nil {
|
||||
log.Warnf("%s: Alt sign address cannot be decoded (clientIP=%s): %v", funcName, c.ClientIP(), err)
|
||||
s.log.Warnf("%s: Alt sign address cannot be decoded (clientIP=%s): %v", funcName, c.ClientIP(), err)
|
||||
s.sendErrorWithMsg(err.Error(), errBadRequest, c)
|
||||
return
|
||||
}
|
||||
if _, ok := addr.(*stdaddr.AddressPubKeyHashEcdsaSecp256k1V0); !ok {
|
||||
log.Warnf("%s: Alt sign address is unexpected type (clientIP=%s, type=%T)", funcName, c.ClientIP(), addr)
|
||||
s.log.Warnf("%s: Alt sign address is unexpected type (clientIP=%s, type=%T)", funcName, c.ClientIP(), addr)
|
||||
s.sendErrorWithMsg("wrong type for alternate signing address", errBadRequest, c)
|
||||
return
|
||||
}
|
||||
@ -83,7 +83,7 @@ func (s *Server) setAltSignAddr(c *gin.Context) {
|
||||
// Get ticket details.
|
||||
rawTicket, err := dcrdClient.GetRawTransaction(ticketHash)
|
||||
if err != nil {
|
||||
log.Errorf("%s: dcrd.GetRawTransaction for ticket failed (ticketHash=%s): %v", funcName, ticketHash, err)
|
||||
s.log.Errorf("%s: dcrd.GetRawTransaction for ticket failed (ticketHash=%s): %v", funcName, ticketHash, err)
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
}
|
||||
@ -91,12 +91,12 @@ func (s *Server) setAltSignAddr(c *gin.Context) {
|
||||
// Ensure this ticket is eligible to vote at some point in the future.
|
||||
canVote, err := canTicketVote(rawTicket, dcrdClient, s.cfg.NetParams)
|
||||
if err != nil {
|
||||
log.Errorf("%s: canTicketVote error (ticketHash=%s): %v", funcName, ticketHash, err)
|
||||
s.log.Errorf("%s: canTicketVote error (ticketHash=%s): %v", funcName, ticketHash, err)
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
}
|
||||
if !canVote {
|
||||
log.Warnf("%s: unvotable ticket (clientIP=%s, ticketHash=%s)",
|
||||
s.log.Warnf("%s: unvotable ticket (clientIP=%s, ticketHash=%s)",
|
||||
funcName, c.ClientIP(), ticketHash)
|
||||
s.sendError(errTicketCannotVote, c)
|
||||
return
|
||||
@ -118,10 +118,10 @@ func (s *Server) setAltSignAddr(c *gin.Context) {
|
||||
|
||||
err = s.db.InsertAltSignAddr(ticketHash, data)
|
||||
if err != nil {
|
||||
log.Errorf("%s: db.InsertAltSignAddr error (ticketHash=%s): %v",
|
||||
s.log.Errorf("%s: db.InsertAltSignAddr error (ticketHash=%s): %v",
|
||||
funcName, ticketHash, err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Debugf("%s: New alt sign address set for ticket: (ticketHash=%s)", funcName, ticketHash)
|
||||
s.log.Debugf("%s: New alt sign address set for ticket: (ticketHash=%s)", funcName, ticketHash)
|
||||
}
|
||||
|
||||
@ -86,6 +86,7 @@ func TestMain(m *testing.M) {
|
||||
cfg: cfg,
|
||||
signPrivKey: signPrivKey,
|
||||
db: db,
|
||||
log: log,
|
||||
}
|
||||
|
||||
// Run tests.
|
||||
|
||||
@ -33,13 +33,13 @@ func (s *Server) setVoteChoices(c *gin.Context) {
|
||||
}
|
||||
|
||||
if !knownTicket {
|
||||
log.Warnf("%s: Unknown ticket (clientIP=%s)", funcName, c.ClientIP())
|
||||
s.log.Warnf("%s: Unknown ticket (clientIP=%s)", funcName, c.ClientIP())
|
||||
s.sendError(errUnknownTicket, c)
|
||||
return
|
||||
}
|
||||
|
||||
if ticket.FeeTxStatus == database.NoFee {
|
||||
log.Warnf("%s: No fee tx for ticket (clientIP=%s, ticketHash=%s)",
|
||||
s.log.Warnf("%s: No fee tx for ticket (clientIP=%s, ticketHash=%s)",
|
||||
funcName, c.ClientIP(), ticket.Hash)
|
||||
s.sendError(errFeeNotReceived, c)
|
||||
return
|
||||
@ -47,7 +47,7 @@ func (s *Server) setVoteChoices(c *gin.Context) {
|
||||
|
||||
// Only allow vote choices to be updated for live/immature tickets.
|
||||
if ticket.Outcome != "" {
|
||||
log.Warnf("%s: Ticket not eligible to vote (clientIP=%s, ticketHash=%s)",
|
||||
s.log.Warnf("%s: Ticket not eligible to vote (clientIP=%s, ticketHash=%s)",
|
||||
funcName, c.ClientIP(), ticket.Hash)
|
||||
s.sendErrorWithMsg(fmt.Sprintf("ticket not eligible to vote (status=%s)", ticket.Outcome),
|
||||
errTicketCannotVote, c)
|
||||
@ -56,7 +56,7 @@ func (s *Server) setVoteChoices(c *gin.Context) {
|
||||
|
||||
var request setVoteChoicesRequest
|
||||
if err := binding.JSON.BindBody(reqBytes, &request); err != nil {
|
||||
log.Warnf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err)
|
||||
s.log.Warnf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err)
|
||||
s.sendErrorWithMsg(err.Error(), errBadRequest, c)
|
||||
return
|
||||
}
|
||||
@ -65,7 +65,7 @@ func (s *Server) setVoteChoices(c *gin.Context) {
|
||||
// vote change requests. This is to prevent requests from being replayed.
|
||||
previousChanges, err := s.db.GetVoteChanges(ticket.Hash)
|
||||
if err != nil {
|
||||
log.Errorf("%s: db.GetVoteChanges error (ticketHash=%s): %v",
|
||||
s.log.Errorf("%s: db.GetVoteChanges error (ticketHash=%s): %v",
|
||||
funcName, ticket.Hash, err)
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
@ -77,14 +77,14 @@ func (s *Server) setVoteChoices(c *gin.Context) {
|
||||
}
|
||||
err := json.Unmarshal([]byte(change.Request), &prevReq)
|
||||
if err != nil {
|
||||
log.Errorf("%s: Could not unmarshal vote change record (ticketHash=%s): %v",
|
||||
s.log.Errorf("%s: Could not unmarshal vote change record (ticketHash=%s): %v",
|
||||
funcName, ticket.Hash, err)
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
}
|
||||
|
||||
if request.Timestamp <= prevReq.Timestamp {
|
||||
log.Warnf("%s: Request uses invalid timestamp, %d is not greater "+
|
||||
s.log.Warnf("%s: Request uses invalid timestamp, %d is not greater "+
|
||||
"than %d (ticketHash=%s)",
|
||||
funcName, request.Timestamp, prevReq.Timestamp, ticket.Hash)
|
||||
s.sendError(errInvalidTimestamp, c)
|
||||
@ -96,7 +96,7 @@ func (s *Server) setVoteChoices(c *gin.Context) {
|
||||
|
||||
err = validConsensusVoteChoices(s.cfg.NetParams, currentVoteVersion(s.cfg.NetParams), request.VoteChoices)
|
||||
if err != nil {
|
||||
log.Warnf("%s: Invalid consensus vote choices (clientIP=%s, ticketHash=%s): %v",
|
||||
s.log.Warnf("%s: Invalid consensus vote choices (clientIP=%s, ticketHash=%s): %v",
|
||||
funcName, c.ClientIP(), ticket.Hash, err)
|
||||
s.sendErrorWithMsg(err.Error(), errInvalidVoteChoices, c)
|
||||
return
|
||||
@ -104,14 +104,14 @@ func (s *Server) setVoteChoices(c *gin.Context) {
|
||||
|
||||
err = validTreasuryPolicy(request.TreasuryPolicy)
|
||||
if err != nil {
|
||||
log.Warnf("%s: Invalid treasury policy (clientIP=%s, ticketHash=%s): %v",
|
||||
s.log.Warnf("%s: Invalid treasury policy (clientIP=%s, ticketHash=%s): %v",
|
||||
funcName, c.ClientIP(), ticket.Hash, err)
|
||||
s.sendErrorWithMsg(err.Error(), errInvalidVoteChoices, c)
|
||||
}
|
||||
|
||||
err = validTSpendPolicy(request.TSpendPolicy)
|
||||
if err != nil {
|
||||
log.Warnf("%s: Invalid tspend policy (clientIP=%s, ticketHash=%s): %v",
|
||||
s.log.Warnf("%s: Invalid tspend policy (clientIP=%s, ticketHash=%s): %v",
|
||||
funcName, c.ClientIP(), ticket.Hash, err)
|
||||
s.sendErrorWithMsg(err.Error(), errInvalidVoteChoices, c)
|
||||
}
|
||||
@ -133,7 +133,7 @@ func (s *Server) setVoteChoices(c *gin.Context) {
|
||||
|
||||
err = s.db.UpdateTicket(ticket)
|
||||
if err != nil {
|
||||
log.Errorf("%s: db.UpdateTicket error, failed to set consensus vote choices (ticketHash=%s): %v",
|
||||
s.log.Errorf("%s: db.UpdateTicket error, failed to set consensus vote choices (ticketHash=%s): %v",
|
||||
funcName, ticket.Hash, err)
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
@ -151,7 +151,7 @@ func (s *Server) setVoteChoices(c *gin.Context) {
|
||||
for agenda, choice := range ticket.VoteChoices {
|
||||
err = walletClient.SetVoteChoice(agenda, choice, ticket.Hash)
|
||||
if err != nil {
|
||||
log.Errorf("%s: dcrwallet.SetVoteChoice failed (wallet=%s, ticketHash=%s): %v",
|
||||
s.log.Errorf("%s: dcrwallet.SetVoteChoice failed (wallet=%s, ticketHash=%s): %v",
|
||||
funcName, walletClient.String(), ticket.Hash, err)
|
||||
}
|
||||
}
|
||||
@ -160,7 +160,7 @@ func (s *Server) setVoteChoices(c *gin.Context) {
|
||||
for tspend, policy := range ticket.TSpendPolicy {
|
||||
err = walletClient.SetTSpendPolicy(tspend, policy, ticket.Hash)
|
||||
if err != nil {
|
||||
log.Errorf("%s: dcrwallet.SetTSpendPolicy failed (wallet=%s, ticketHash=%s): %v",
|
||||
s.log.Errorf("%s: dcrwallet.SetTSpendPolicy failed (wallet=%s, ticketHash=%s): %v",
|
||||
funcName, walletClient.String(), ticket.Hash, err)
|
||||
}
|
||||
}
|
||||
@ -169,14 +169,14 @@ func (s *Server) setVoteChoices(c *gin.Context) {
|
||||
for key, policy := range ticket.TreasuryPolicy {
|
||||
err = walletClient.SetTreasuryPolicy(key, policy, ticket.Hash)
|
||||
if err != nil {
|
||||
log.Errorf("%s: dcrwallet.SetTreasuryPolicy failed (wallet=%s, ticketHash=%s): %v",
|
||||
s.log.Errorf("%s: dcrwallet.SetTreasuryPolicy failed (wallet=%s, ticketHash=%s): %v",
|
||||
funcName, walletClient.String(), ticket.Hash, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.Debugf("%s: Vote choices updated (ticketHash=%s)", funcName, ticket.Hash)
|
||||
s.log.Debugf("%s: Vote choices updated (ticketHash=%s)", funcName, ticket.Hash)
|
||||
|
||||
// Send success response to client.
|
||||
resp, respSig := s.sendJSONResponse(setVoteChoicesResponse{
|
||||
@ -194,6 +194,6 @@ func (s *Server) setVoteChoices(c *gin.Context) {
|
||||
ResponseSignature: respSig,
|
||||
})
|
||||
if err != nil {
|
||||
log.Errorf("%s: Failed to store vote change record (ticketHash=%s): %v", err)
|
||||
s.log.Errorf("%s: Failed to store vote change record (ticketHash=%s): %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,14 +22,14 @@ func (s *Server) ticketStatus(c *gin.Context) {
|
||||
reqBytes := c.MustGet(requestBytesKey).([]byte)
|
||||
|
||||
if !knownTicket {
|
||||
log.Warnf("%s: Unknown ticket (clientIP=%s)", funcName, c.ClientIP())
|
||||
s.log.Warnf("%s: Unknown ticket (clientIP=%s)", funcName, c.ClientIP())
|
||||
s.sendError(errUnknownTicket, c)
|
||||
return
|
||||
}
|
||||
|
||||
var request ticketStatusRequest
|
||||
if err := binding.JSON.BindBody(reqBytes, &request); err != nil {
|
||||
log.Warnf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err)
|
||||
s.log.Warnf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err)
|
||||
s.sendErrorWithMsg(err.Error(), errBadRequest, c)
|
||||
return
|
||||
}
|
||||
@ -37,7 +37,7 @@ func (s *Server) ticketStatus(c *gin.Context) {
|
||||
// Get altSignAddress from database
|
||||
altSignAddrData, err := s.db.AltSignAddrData(ticket.Hash)
|
||||
if err != nil {
|
||||
log.Errorf("%s: db.AltSignAddrData error (ticketHash=%s): %v", funcName, ticket.Hash, err)
|
||||
s.log.Errorf("%s: db.AltSignAddrData error (ticketHash=%s): %v", funcName, ticket.Hash, err)
|
||||
s.sendError(errInternalError, c)
|
||||
return
|
||||
}
|
||||
|
||||
@ -18,6 +18,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/decred/dcrd/chaincfg/v3"
|
||||
"github.com/decred/slog"
|
||||
"github.com/decred/vspd/database"
|
||||
"github.com/decred/vspd/rpc"
|
||||
"github.com/dustin/go-humanize"
|
||||
@ -66,6 +67,7 @@ const (
|
||||
type Server struct {
|
||||
cfg Config
|
||||
db *database.VspDatabase
|
||||
log slog.Logger
|
||||
addrGen *addressGenerator
|
||||
cache *cache
|
||||
signPrivKey ed25519.PrivateKey
|
||||
@ -78,6 +80,7 @@ func Start(shutdownCtx context.Context, requestShutdown func(), shutdownWg *sync
|
||||
s := &Server{
|
||||
cfg: config,
|
||||
db: vdb,
|
||||
log: log,
|
||||
}
|
||||
|
||||
var err error
|
||||
@ -202,7 +205,7 @@ func (s *Server) router(cookieSecret []byte, dcrd rpc.DcrdConnect, wallets rpc.W
|
||||
"blockURL": blockURL(s.cfg.BlockExplorerURL),
|
||||
"dateTime": dateTime,
|
||||
"stripWss": stripWss,
|
||||
"indentJSON": indentJSON,
|
||||
"indentJSON": indentJSON(s.log),
|
||||
"atomsToDCR": atomsToDCR,
|
||||
"float32ToPercent": float32ToPercent,
|
||||
"comma": humanize.Comma,
|
||||
@ -231,32 +234,32 @@ func (s *Server) router(cookieSecret []byte, dcrd rpc.DcrdConnect, wallets rpc.W
|
||||
|
||||
api := router.Group("/api/v3")
|
||||
api.GET("/vspinfo", s.vspInfo)
|
||||
api.POST("/setaltsignaddr", withDcrdClient(dcrd), s.broadcastTicket, s.vspAuth, s.setAltSignAddr)
|
||||
api.POST("/feeaddress", withDcrdClient(dcrd), s.broadcastTicket, s.vspAuth, s.feeAddress)
|
||||
api.POST("/ticketstatus", withDcrdClient(dcrd), s.vspAuth, s.ticketStatus)
|
||||
api.POST("/payfee", withDcrdClient(dcrd), s.vspAuth, s.payFee)
|
||||
api.POST("/setvotechoices", withDcrdClient(dcrd), withWalletClients(wallets), s.vspAuth, s.setVoteChoices)
|
||||
api.POST("/setaltsignaddr", s.withDcrdClient(dcrd), s.broadcastTicket, s.vspAuth, s.setAltSignAddr)
|
||||
api.POST("/feeaddress", s.withDcrdClient(dcrd), s.broadcastTicket, s.vspAuth, s.feeAddress)
|
||||
api.POST("/ticketstatus", s.withDcrdClient(dcrd), s.vspAuth, s.ticketStatus)
|
||||
api.POST("/payfee", s.withDcrdClient(dcrd), s.vspAuth, s.payFee)
|
||||
api.POST("/setvotechoices", s.withDcrdClient(dcrd), s.withWalletClients(wallets), s.vspAuth, s.setVoteChoices)
|
||||
|
||||
// Website routes.
|
||||
|
||||
router.GET("", s.homepage)
|
||||
|
||||
login := router.Group("/admin").Use(
|
||||
withSession(cookieStore),
|
||||
s.withSession(cookieStore),
|
||||
)
|
||||
login.POST("", s.adminLogin)
|
||||
|
||||
admin := router.Group("/admin").Use(
|
||||
withWalletClients(wallets), withSession(cookieStore), s.requireAdmin,
|
||||
s.withWalletClients(wallets), s.withSession(cookieStore), s.requireAdmin,
|
||||
)
|
||||
admin.GET("", withDcrdClient(dcrd), s.adminPage)
|
||||
admin.POST("/ticket", withDcrdClient(dcrd), s.ticketSearch)
|
||||
admin.GET("", s.withDcrdClient(dcrd), s.adminPage)
|
||||
admin.POST("/ticket", s.withDcrdClient(dcrd), s.ticketSearch)
|
||||
admin.GET("/backup", s.downloadDatabaseBackup)
|
||||
admin.POST("/logout", s.adminLogout)
|
||||
|
||||
// Require Basic HTTP Auth on /admin/status endpoint.
|
||||
basic := router.Group("/admin").Use(
|
||||
withDcrdClient(dcrd), withWalletClients(wallets), gin.BasicAuth(gin.Accounts{
|
||||
s.withDcrdClient(dcrd), s.withWalletClients(wallets), gin.BasicAuth(gin.Accounts{
|
||||
"admin": s.cfg.AdminPass,
|
||||
}),
|
||||
)
|
||||
@ -271,7 +274,7 @@ func (s *Server) router(cookieSecret []byte, dcrd rpc.DcrdConnect, wallets rpc.W
|
||||
func (s *Server) sendJSONResponse(resp interface{}, c *gin.Context) (string, string) {
|
||||
dec, err := json.Marshal(resp)
|
||||
if err != nil {
|
||||
log.Errorf("JSON marshal error: %v", err)
|
||||
s.log.Errorf("JSON marshal error: %v", err)
|
||||
s.sendError(errInternalError, c)
|
||||
return "", ""
|
||||
}
|
||||
@ -305,7 +308,7 @@ func (s *Server) sendErrorWithMsg(msg string, e apiError, c *gin.Context) {
|
||||
// Try to sign the error response. If it fails, send it without a signature.
|
||||
dec, err := json.Marshal(resp)
|
||||
if err != nil {
|
||||
log.Warnf("Sending error response without signature: %v", err)
|
||||
s.log.Warnf("Sending error response without signature: %v", err)
|
||||
} else {
|
||||
sig := ed25519.Sign(s.signPrivKey, dec)
|
||||
c.Writer.Header().Set("VSP-Server-Signature", base64.StdEncoding.EncodeToString(sig))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user