Skip to content

Commit db8319d

Browse files
committed
sweep: add method handleReplacementTxError
This is a minor refactor so the `createAndPublishTx` flow becomes more clear, also prepares for the following commit where we start to handle missing inputs.
1 parent 4281894 commit db8319d

File tree

1 file changed

+49
-39
lines changed

1 file changed

+49
-39
lines changed

sweep/fee_bumper.go

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,46 +1254,10 @@ func (t *TxPublisher) createAndPublishTx(
12541254
// directly here.
12551255
sweepCtx, err := t.createAndCheckTx(r.req, r.feeFunction)
12561256

1257-
// If the error is fee related, we will return no error and let the fee
1258-
// bumper retry it at next block.
1259-
//
1260-
// NOTE: we can check the RBF error here and ask the fee function to
1261-
// recalculate the fee rate. However, this would defeat the purpose of
1262-
// using a deadline based fee function:
1263-
// - if the deadline is far away, there's no rush to RBF the tx.
1264-
// - if the deadline is close, we expect the fee function to give us a
1265-
// higher fee rate. If the fee rate cannot satisfy the RBF rules, it
1266-
// means the budget is not enough.
1267-
if errors.Is(err, chain.ErrInsufficientFee) ||
1268-
errors.Is(err, lnwallet.ErrMempoolFee) {
1269-
1270-
log.Debugf("Failed to bump tx %v: %v", oldTx.TxHash(), err)
1271-
return fn.None[BumpResult]()
1272-
}
1273-
1274-
// If the error is not fee related, we will return a `TxFailed` event
1275-
// so this input can be retried.
1257+
// If there's an error creating the replacement tx, we need to abort the
1258+
// flow and handle it.
12761259
if err != nil {
1277-
// If the tx doesn't not have enought budget, we will return a
1278-
// result so the sweeper can handle it by re-clustering the
1279-
// utxos.
1280-
if errors.Is(err, ErrNotEnoughBudget) {
1281-
log.Warnf("Fail to fee bump tx %v: %v", oldTx.TxHash(),
1282-
err)
1283-
} else {
1284-
// Otherwise, an unexpected error occurred, we will
1285-
// fail the tx and let the sweeper retry the whole
1286-
// process.
1287-
log.Errorf("Failed to bump tx %v: %v", oldTx.TxHash(),
1288-
err)
1289-
}
1290-
1291-
return fn.Some(BumpResult{
1292-
Event: TxFailed,
1293-
Tx: oldTx,
1294-
Err: err,
1295-
requestID: r.requestID,
1296-
})
1260+
return t.handleReplacementTxError(r, oldTx, err)
12971261
}
12981262

12991263
// The tx has been created without any errors, we now register a new
@@ -1786,3 +1750,49 @@ func prepareSweepTx(inputs []input.Input, changePkScript lnwallet.AddrWithKey,
17861750

17871751
return txFee, changeOutsOpt, locktimeOpt, nil
17881752
}
1753+
1754+
// handleReplacementTxError handles the error returned from creating the
1755+
// replacement tx. It returns a BumpResult that should be notified to the
1756+
// sweeper.
1757+
func (t *TxPublisher) handleReplacementTxError(r *monitorRecord,
1758+
oldTx *wire.MsgTx, err error) fn.Option[BumpResult] {
1759+
1760+
// If the error is fee related, we will return no error and let the fee
1761+
// bumper retry it at next block.
1762+
//
1763+
// NOTE: we can check the RBF error here and ask the fee function to
1764+
// recalculate the fee rate. However, this would defeat the purpose of
1765+
// using a deadline based fee function:
1766+
// - if the deadline is far away, there's no rush to RBF the tx.
1767+
// - if the deadline is close, we expect the fee function to give us a
1768+
// higher fee rate. If the fee rate cannot satisfy the RBF rules, it
1769+
// means the budget is not enough.
1770+
if errors.Is(err, chain.ErrInsufficientFee) ||
1771+
errors.Is(err, lnwallet.ErrMempoolFee) {
1772+
1773+
log.Debugf("Failed to bump tx %v: %v", oldTx.TxHash(), err)
1774+
return fn.None[BumpResult]()
1775+
}
1776+
1777+
// If the error is not fee related, we will return a `TxFailed` event
1778+
// so this input can be retried.
1779+
result := fn.Some(BumpResult{
1780+
Event: TxFailed,
1781+
Tx: oldTx,
1782+
Err: err,
1783+
requestID: r.requestID,
1784+
})
1785+
1786+
// If the tx doesn't not have enought budget, we will return a result so
1787+
// the sweeper can handle it by re-clustering the utxos.
1788+
if errors.Is(err, ErrNotEnoughBudget) {
1789+
log.Warnf("Fail to fee bump tx %v: %v", oldTx.TxHash(), err)
1790+
return result
1791+
}
1792+
1793+
// Otherwise, an unexpected error occurred, we will log an error and let
1794+
// the sweeper retry the whole process.
1795+
log.Errorf("Failed to bump tx %v: %v", oldTx.TxHash(), err)
1796+
1797+
return result
1798+
}

0 commit comments

Comments
 (0)