Make ticket parent mandatory (#218)

* Make ticket parent mandatory

* Reduce scope of request types which arent reused.
This commit is contained in:
Jamie Holdstock 2021-01-04 14:55:21 +00:00 committed by GitHub
parent 861eb1c377
commit c71fbb024b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 37 deletions

View File

@ -20,16 +20,6 @@ import (
"github.com/jrick/wsrpc/v2" "github.com/jrick/wsrpc/v2"
) )
type ticketHashRequest struct {
TicketHash string `json:"tickethash" binding:"required"`
}
type ticketRequest struct {
TicketHex string `json:"tickethex" binding:"required"`
TicketHash string `json:"tickethash" binding:"required"`
ParentHex string `json:"parenthex"`
}
// withSession middleware adds a gorilla session to the request context for // withSession middleware adds a gorilla session to the request context for
// downstream handlers to make use of. Sessions are used by admin pages to // downstream handlers to make use of. Sessions are used by admin pages to
// maintain authentication status. // maintain authentication status.
@ -112,9 +102,11 @@ func withWalletClients(wallets rpc.WalletConnect) gin.HandlerFunc {
} }
} }
// broadcastTicket will parse ticket hash and ticket hex from the request body, // broadcastTicket will ensure that the local dcrd instance is aware of the
// and ensure the local dcrd instance can retrieve information about the ticket. // provided ticket.
// If no info can be found, the ticket hex will be broadcast. // Ticket hash, ticket hex, and parent hex are parsed from the request body and
// validated. They are broadcast to the network using SendRawTransaction if dcrd
// is not aware of them.
func broadcastTicket() gin.HandlerFunc { func broadcastTicket() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
const funcName = "broadcastTicket" const funcName = "broadcastTicket"
@ -130,15 +122,19 @@ func broadcastTicket() gin.HandlerFunc {
c.Request.Body.Close() c.Request.Body.Close()
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(reqBytes)) c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(reqBytes))
// Parse request and ensure ticket hash and hex are included. // Parse request to ensure ticket hash/hex and parent hex are included.
var request ticketRequest var request struct {
TicketHex string `json:"tickethex" binding:"required"`
TicketHash string `json:"tickethash" binding:"required"`
ParentHex string `json:"parenthex" binding:"required"`
}
if err := binding.JSON.BindBody(reqBytes, &request); err != nil { if err := binding.JSON.BindBody(reqBytes, &request); err != nil {
log.Warnf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err) log.Warnf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err)
sendErrorWithMsg(err.Error(), errBadRequest, c) sendErrorWithMsg(err.Error(), errBadRequest, c)
return return
} }
// Ensure the provided hex is a valid ticket. // Ensure the provided ticket hex is a valid ticket.
msgTx, err := decodeTransaction(request.TicketHex) msgTx, err := decodeTransaction(request.TicketHex)
if err != nil { if err != nil {
log.Errorf("%s: Failed to decode ticket hex (ticketHash=%s): %v", funcName, request.TicketHash, err) log.Errorf("%s: Failed to decode ticket hex (ticketHash=%s): %v", funcName, request.TicketHash, err)
@ -162,9 +158,7 @@ func broadcastTicket() gin.HandlerFunc {
return return
} }
dcrdClient := c.MustGet("DcrdClient").(*rpc.DcrdRPC) // Ensure the provided parent hex is a valid tx.
if request.ParentHex != "" {
parentTx, err := decodeTransaction(request.ParentHex) parentTx, err := decodeTransaction(request.ParentHex)
if err != nil { if err != nil {
log.Errorf("%s: Failed to decode parent hex (ticketHash=%s): %v", funcName, request.TicketHash, err) log.Errorf("%s: Failed to decode parent hex (ticketHash=%s): %v", funcName, request.TicketHash, err)
@ -173,12 +167,18 @@ func broadcastTicket() gin.HandlerFunc {
} }
parentHash := parentTx.TxHash() parentHash := parentTx.TxHash()
// Check if local dcrd already knows the parent tx.
dcrdClient := c.MustGet("DcrdClient").(*rpc.DcrdRPC)
_, err = dcrdClient.GetRawTransaction(parentHash.String()) _, err = dcrdClient.GetRawTransaction(parentHash.String())
var e *wsrpc.Error
if err == nil { if err == nil {
// No error means dcrd already knows the parent, we are done here. // No error means dcrd already knows the parent tx, nothing to do.
goto processTicket } else if errors.As(err, &e) && e.Code == rpc.ErrNoTxInfo {
} // ErrNoTxInfo means local dcrd is not aware of the parent. We have
// the hex, so we can broadcast it here.
// Before broadcasting, check that the provided parent hex is
// actually the parent of the ticket.
var found bool var found bool
for _, txIn := range msgTx.TxIn { for _, txIn := range msgTx.TxIn {
if !txIn.PreviousOutPoint.Hash.IsEqual(&parentHash) { if !txIn.PreviousOutPoint.Hash.IsEqual(&parentHash) {
@ -187,11 +187,13 @@ func broadcastTicket() gin.HandlerFunc {
found = true found = true
break break
} }
if !found { if !found {
log.Errorf("%s: Invalid ticket parent (ticketHash=%s)", funcName, request.TicketHash) log.Errorf("%s: Invalid ticket parent (ticketHash=%s)", funcName, request.TicketHash)
sendErrorWithMsg("invalid ticket parent", errBadRequest, c) sendErrorWithMsg("invalid ticket parent", errBadRequest, c)
return return
} }
log.Debugf("%s: Broadcasting parent tx %s (ticketHash=%s)", funcName, parentHash, request.TicketHash) log.Debugf("%s: Broadcasting parent tx %s (ticketHash=%s)", funcName, parentHash, request.TicketHash)
err = dcrdClient.SendRawTransaction(request.ParentHex) err = dcrdClient.SendRawTransaction(request.ParentHex)
if err != nil { if err != nil {
@ -200,11 +202,15 @@ func broadcastTicket() gin.HandlerFunc {
sendError(errCannotBroadcastTicket, c) sendError(errCannotBroadcastTicket, c)
return return
} }
} else {
log.Errorf("%s: dcrd.GetRawTransaction for ticket parent failed (ticketHash=%s): %v",
funcName, request.TicketHash, err)
sendError(errInternalError, c)
return
} }
processTicket: // Check if local dcrd already knows the ticket.
// Use GetRawTransaction to check if local dcrd already knows this
// ticket.
_, err = dcrdClient.GetRawTransaction(request.TicketHash) _, err = dcrdClient.GetRawTransaction(request.TicketHash)
if err == nil { if err == nil {
// No error means dcrd already knows the ticket, we are done here. // No error means dcrd already knows the ticket, we are done here.
@ -213,7 +219,6 @@ func broadcastTicket() gin.HandlerFunc {
// ErrNoTxInfo means local dcrd is not aware of the ticket. We have the // ErrNoTxInfo means local dcrd is not aware of the ticket. We have the
// hex, so we can broadcast it here. // hex, so we can broadcast it here.
var e *wsrpc.Error
if errors.As(err, &e) && e.Code == rpc.ErrNoTxInfo { if errors.As(err, &e) && e.Code == rpc.ErrNoTxInfo {
log.Debugf("%s: Broadcasting ticket (ticketHash=%s)", funcName, request.TicketHash) log.Debugf("%s: Broadcasting ticket (ticketHash=%s)", funcName, request.TicketHash)
err = dcrdClient.SendRawTransaction(request.TicketHex) err = dcrdClient.SendRawTransaction(request.TicketHex)
@ -256,7 +261,9 @@ func vspAuth() gin.HandlerFunc {
c.Set("RequestBytes", reqBytes) c.Set("RequestBytes", reqBytes)
// Parse request and ensure there is a ticket hash included. // Parse request and ensure there is a ticket hash included.
var request ticketHashRequest var request struct {
TicketHash string `json:"tickethash" binding:"required"`
}
if err := binding.JSON.BindBody(reqBytes, &request); err != nil { if err := binding.JSON.BindBody(reqBytes, &request); err != nil {
log.Warnf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err) log.Warnf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err)
sendErrorWithMsg(err.Error(), errBadRequest, c) sendErrorWithMsg(err.Error(), errBadRequest, c)

View File

@ -21,7 +21,7 @@ type feeAddressRequest struct {
Timestamp int64 `json:"timestamp" binding:"required"` Timestamp int64 `json:"timestamp" binding:"required"`
TicketHash string `json:"tickethash" binding:"required"` TicketHash string `json:"tickethash" binding:"required"`
TicketHex string `json:"tickethex" binding:"required"` TicketHex string `json:"tickethex" binding:"required"`
ParentTx string `json:"parenthex"` ParentHex string `json:"parenthex" binding:"required"`
} }
type feeAddressResponse struct { type feeAddressResponse struct {