Skip to content

Commit dd22b2a

Browse files
committed
[release-v1.3] webapi: Wait for unknown outputs to propagate.
If broadcasting parent transaction of a ticket fails because it references unknown outputs, there is a good chance that waiting a few seconds will resolve the issue because the ancestor transactions will propagate through the network and reach the mempool of the local dcrd instance.
1 parent 22d0411 commit dd22b2a

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

internal/webapi/middleware.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,37 @@ func (w *WebAPI) broadcastTicket(c *gin.Context) {
275275
w.log.Debugf("%s: Broadcasting parent tx %s (ticketHash=%s)", funcName, parentHash, request.TicketHash)
276276
err = dcrdClient.SendRawTransaction(request.ParentHex)
277277
if err != nil {
278-
w.log.Errorf("%s: dcrd.SendRawTransaction for parent tx failed (ticketHash=%s): %v",
279-
funcName, request.TicketHash, err)
280-
w.sendError(types.ErrCannotBroadcastTicket, c)
281-
return
278+
// Unknown output errors have special handling because they
279+
// could be resolved by waiting for network propagation. Any
280+
// other errors are returned to client immediately.
281+
if !strings.Contains(err.Error(), rpc.ErrUnknownOutputs) {
282+
w.log.Errorf("%s: dcrd.SendRawTransaction for parent tx failed (ticketHash=%s): %v",
283+
funcName, request.TicketHash, err)
284+
w.sendError(types.ErrCannotBroadcastTicket, c)
285+
return
286+
}
287+
288+
w.log.Debugf("%s: Parent tx references an unknown output, waiting for it in mempool (ticketHash=%s)",
289+
funcName, request.TicketHash)
290+
291+
txBroadcast := func() bool {
292+
// Wait for 1 second and try again, max 7 attempts.
293+
for i := 0; i < 7; i++ {
294+
time.Sleep(1 * time.Second)
295+
err := dcrdClient.SendRawTransaction(request.ParentHex)
296+
if err == nil {
297+
return true
298+
}
299+
}
300+
return false
301+
}()
302+
303+
if !txBroadcast {
304+
w.log.Errorf("%s: Failed to broadcast parent tx, waiting didn't help (ticketHash=%s)",
305+
funcName, request.TicketHash)
306+
w.sendError(types.ErrCannotBroadcastTicket, c)
307+
return
308+
}
282309
}
283310

284311
} else {

0 commit comments

Comments
 (0)