Skip to content

Commit d5b3062

Browse files
review: refactor errors strings and return values
1 parent 6ab9252 commit d5b3062

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

webapi/helpers.go

+14-22
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/decred/dcrd/dcrutil/v4"
1717
"github.com/decred/dcrd/wire"
1818
"github.com/decred/vspd/rpc"
19-
"github.com/gin-gonic/gin"
2019
)
2120

2221
func currentVoteVersion(params *chaincfg.Params) uint32 {
@@ -106,22 +105,22 @@ func validPolicyOption(policy string) error {
106105
}
107106
}
108107

109-
func validateSignature(hash, commitmentAddress, signature, message string, c *gin.Context) error {
108+
func validateSignature(hash, commitmentAddress, signature, message string) error {
110109
firstErr := dcrutil.VerifyMessage(commitmentAddress, signature, message, cfg.NetParams)
111110
if firstErr != nil {
112111
// Don't return an error straight away if sig validation fails -
113112
// first check if we have an alternate sign address for this ticket.
114113
altSigData, err := db.AltSignAddrData(hash)
115114
if err != nil {
116-
return fmt.Errorf("db.AltSignAddrData failed (ticketHash=%s): %v", hash, err)
115+
return fmt.Errorf("db.AltSignAddrData failed: %v", err)
117116
}
118117

119118
// If we have no alternate sign address, or if validating with the
120119
// alt sign addr fails, return an error to the client.
121120
if altSigData == nil || dcrutil.VerifyMessage(altSigData.AltSignAddr, signature, message, cfg.NetParams) != nil {
122-
return fmt.Errorf("Bad signature (clientIP=%s, ticketHash=%s)", c.ClientIP(), hash)
121+
return fmt.Errorf("bad signature")
123122
}
124-
return firstErr
123+
125124
}
126125
return nil
127126
}
@@ -152,55 +151,48 @@ func isValidTicket(tx *wire.MsgTx) error {
152151
// validateTicketHash ensures the provided ticket hash is a valid ticket hash.
153152
// A ticket hash should be 64 chars (MaxHashStringSize) and should parse into
154153
// a chainhash.Hash without error.
155-
func validateTicketHash(c *gin.Context, hash string) (bool, error) {
154+
func validateTicketHash(hash string) error {
156155
if len(hash) != chainhash.MaxHashStringSize {
157-
return false, fmt.Errorf("Incorrect hash length (clientIP=%s): got %d, expected %d", c.ClientIP(), len(hash), chainhash.MaxHashStringSize)
156+
return fmt.Errorf("incorrect hash length: got %d, expected %d", len(hash), chainhash.MaxHashStringSize)
158157

159158
}
160159
_, err := chainhash.NewHashFromStr(hash)
161160
if err != nil {
162-
return false, fmt.Errorf("Invalid hash (clientIP=%s): %v", c.ClientIP(), err)
161+
return fmt.Errorf("invalid hash: %v", err)
163162

164163
}
165164

166-
return true, nil
165+
return nil
167166
}
168167

169168
// getCommitmentAddress gets the commitment address of the provided ticket hash
170169
// from the chain.
171-
func getCommitmentAddress(c *gin.Context, hash string) (string, bool, error) {
170+
func getCommitmentAddress(hash string, dcrdClient *rpc.DcrdRPC) (string, error) {
172171
var commitmentAddress string
173-
dcrdClient := c.MustGet(dcrdKey).(*rpc.DcrdRPC)
174-
dcrdErr := c.MustGet(dcrdErrorKey)
175-
if dcrdErr != nil {
176-
return commitmentAddress, false, fmt.Errorf("could not get dcrd client: %v", dcrdErr.(error))
177-
178-
}
179-
180172
resp, err := dcrdClient.GetRawTransaction(hash)
181173
if err != nil {
182-
return commitmentAddress, false, fmt.Errorf("dcrd.GetRawTransaction for ticket failed (ticketHash=%s): %v", hash, err)
174+
return commitmentAddress, fmt.Errorf("dcrd.GetRawTransaction for ticket failed: %v", err)
183175

184176
}
185177

186178
msgTx, err := decodeTransaction(resp.Hex)
187179
if err != nil {
188-
return commitmentAddress, false, fmt.Errorf("Failed to decode ticket hex (ticketHash=%s): %v", hash, err)
180+
return commitmentAddress, fmt.Errorf("Failed to decode ticket hex: %v", err)
189181

190182
}
191183

192184
err = isValidTicket(msgTx)
193185
if err != nil {
194-
return commitmentAddress, true, fmt.Errorf("Invalid ticket (clientIP=%s, ticketHash=%s): %v", c.ClientIP(), hash, err)
186+
return commitmentAddress, fmt.Errorf("Invalid ticket: %w", errInvalidTicket)
195187

196188
}
197189

198190
addr, err := stake.AddrFromSStxPkScrCommitment(msgTx.TxOut[1].PkScript, cfg.NetParams)
199191
if err != nil {
200-
return commitmentAddress, false, fmt.Errorf("AddrFromSStxPkScrCommitment error (ticketHash=%s): %v", hash, err)
192+
return commitmentAddress, fmt.Errorf("AddrFromSStxPkScrCommitment error: %v", err)
201193

202194
}
203195

204196
commitmentAddress = addr.String()
205-
return commitmentAddress, false, nil
197+
return commitmentAddress, nil
206198
}

webapi/middleware.go

+17-9
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,9 @@ func vspAuth() gin.HandlerFunc {
285285
hash := request.TicketHash
286286

287287
// Before hitting the db or any RPC, ensure this is a valid ticket hash.
288-
validticket, err := validateTicketHash(c, hash)
289-
if !validticket {
290-
log.Errorf("%s: %v", funcName, err)
288+
err = validateTicketHash(hash)
289+
if err != nil {
290+
log.Errorf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err)
291291
sendErrorWithMsg("invalid ticket hash", errBadRequest, c)
292292
return
293293
}
@@ -303,34 +303,42 @@ func vspAuth() gin.HandlerFunc {
303303
// If the ticket was found in the database, we already know its
304304
// commitment address. Otherwise we need to get it from the chain.
305305
var commitmentAddress string
306-
var isInvalid bool
306+
dcrdClient := c.MustGet(dcrdKey).(*rpc.DcrdRPC)
307+
dcrdErr := c.MustGet(dcrdErrorKey)
308+
if dcrdErr != nil {
309+
log.Errorf("%s: could not get dcrd client: %v", funcName, dcrdErr.(error))
310+
sendError(errInternalError, c)
311+
return
312+
}
307313

308314
if ticketFound {
309315
commitmentAddress = ticket.CommitmentAddress
310316
} else {
311-
commitmentAddress, isInvalid, err = getCommitmentAddress(c, hash)
317+
commitmentAddress, err = getCommitmentAddress(hash, dcrdClient)
312318
if err != nil {
313-
if isInvalid {
319+
var apiErr *apiError
320+
if errors.Is(err, apiErr) {
314321
sendError(errInvalidTicket, c)
315322
} else {
316323
sendError(errInternalError, c)
317324
}
318-
log.Errorf("%s: %v", funcName, err)
325+
log.Errorf("%s: (clientIP: %s, ticketHash: %s): %v", funcName, err)
319326
return
320327
}
321328
}
322329

323330
// Ensure a signature is provided.
324331
signature := c.GetHeader("VSP-Client-Signature")
325332
if signature == "" {
333+
log.Warnf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err)
326334
sendErrorWithMsg("no VSP-Client-Signature header", errBadRequest, c)
327335
return
328336
}
329337

330338
// Validate request signature to ensure ticket ownership.
331-
err = validateSignature(hash, commitmentAddress, signature, string(reqBytes), c)
339+
err = validateSignature(hash, commitmentAddress, signature, string(reqBytes))
332340
if err != nil {
333-
log.Errorf("%s: %v", funcName, err)
341+
log.Errorf("%s: Bad signature (clientIP=%s, ticketHash=%s): %v", funcName, err)
334342
sendError(errBadSignature, c)
335343
return
336344
}

0 commit comments

Comments
 (0)