Skip to content

Commit

Permalink
api: add optional parent transaction to feeaddress
Browse files Browse the repository at this point in the history
  • Loading branch information
dajohi committed Nov 19, 2020
1 parent 0f657eb commit 03cc671
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
32 changes: 32 additions & 0 deletions webapi/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type ticketHashRequest struct {
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
Expand Down Expand Up @@ -163,6 +164,37 @@ func broadcastTicket() gin.HandlerFunc {

dcrdClient := c.MustGet("DcrdClient").(*rpc.DcrdRPC)

if request.ParentHex != "" {
parentTx, err := decodeTransaction(request.ParentHex)
if err != nil {
log.Errorf("%s: Failed to decode parent hex (ticketHash=%s): %v", funcName, request.TicketHash, err)
sendErrorWithMsg("cannot decode parent hex", errBadRequest, c)
return
}
parentHash := parentTx.TxHash()
var found bool
for _, txIn := range msgTx.TxIn {
if !txIn.PreviousOutPoint.Hash.IsEqual(&parentHash) {
continue
}
found = true
break
}
if !found {
log.Errorf("%s: Invalid ticket parent (ticketHash=%s)", funcName, request.TicketHash)
sendErrorWithMsg("invalid ticket parent", errBadRequest, c)
return
}
log.Debugf("%s: Broadcasting parent tx %s (ticketHash=%s)", funcName, parentHash, request.TicketHash)
err = dcrdClient.SendRawTransaction(request.TicketHex)
if err != nil {
log.Errorf("%s: dcrd.SendRawTransaction for parent tx failed (ticketHash=%s): %v",
funcName, request.TicketHash, err)
sendError(errCannotBroadcastTicket, c)
return
}
}

// Use GetRawTransaction to check if local dcrd already knows this
// ticket.
_, err = dcrdClient.GetRawTransaction(request.TicketHash)
Expand Down
1 change: 1 addition & 0 deletions webapi/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type feeAddressRequest struct {
Timestamp int64 `json:"timestamp" binding:"required"`
TicketHash string `json:"tickethash" binding:"required"`
TicketHex string `json:"tickethex" binding:"required"`
ParentTx string `json:"parenthex"`
}

type feeAddressResponse struct {
Expand Down

0 comments on commit 03cc671

Please sign in to comment.