Skip to content

Commit 4f469de

Browse files
committed
sweep: refactor handleInitialTxError and createAndCheckTx
This commit refactors `handleInitialTxError` and `createAndCheckTx` to take a `monitorRecord` param, which prepares for the following commit where we start handling missing inputs.
1 parent f614e7a commit 4f469de

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

sweep/fee_bumper.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ func (t *TxPublisher) createRBFCompliantTx(
544544
for {
545545
// Create a new tx with the given fee rate and check its
546546
// mempool acceptance.
547-
sweepCtx, err := t.createAndCheckTx(r.req, f)
547+
sweepCtx, err := t.createAndCheckTx(r)
548548

549549
switch {
550550
case err == nil:
@@ -607,8 +607,9 @@ func (t *TxPublisher) createRBFCompliantTx(
607607
// script, and the fee rate. In addition, it validates the tx's mempool
608608
// acceptance before returning a tx that can be published directly, along with
609609
// its fee.
610-
func (t *TxPublisher) createAndCheckTx(req *BumpRequest,
611-
f FeeFunction) (*sweepTxCtx, error) {
610+
func (t *TxPublisher) createAndCheckTx(r *monitorRecord) (*sweepTxCtx, error) {
611+
req := r.req
612+
f := r.feeFunction
612613

613614
// Create the sweep tx with max fee rate of 0 as the fee function
614615
// guarantees the fee rate used here won't exceed the max fee rate.
@@ -1025,27 +1026,31 @@ func (t *TxPublisher) handleTxConfirmed(r *monitorRecord) {
10251026

10261027
// handleInitialTxError takes the error from `initializeTx` and decides the
10271028
// bump event. It will construct a BumpResult and handles it.
1028-
func (t *TxPublisher) handleInitialTxError(requestID uint64, err error) {
1029-
// We now decide what type of event to send.
1030-
var event BumpEvent
1029+
func (t *TxPublisher) handleInitialTxError(r *monitorRecord, err error) {
1030+
// Create a bump result to be sent to the sweeper.
1031+
result := &BumpResult{
1032+
Err: err,
1033+
requestID: r.requestID,
1034+
}
10311035

1036+
// We now decide what type of event to send.
10321037
switch {
10331038
// When the error is due to a dust output, we'll send a TxFailed so
10341039
// these inputs can be retried with a different group in the next
10351040
// block.
10361041
case errors.Is(err, ErrTxNoOutput):
1037-
event = TxFailed
1042+
result.Event = TxFailed
10381043

10391044
// When the error is due to budget being used up, we'll send a TxFailed
10401045
// so these inputs can be retried with a different group in the next
10411046
// block.
10421047
case errors.Is(err, ErrMaxPosition):
1043-
event = TxFailed
1048+
result.Event = TxFailed
10441049

10451050
// When the error is due to zero fee rate delta, we'll send a TxFailed
10461051
// so these inputs can be retried in the next block.
10471052
case errors.Is(err, ErrZeroFeeRateDelta):
1048-
event = TxFailed
1053+
result.Event = TxFailed
10491054

10501055
// Otherwise this is not a fee-related error and the tx cannot be
10511056
// retried. In that case we will fail ALL the inputs in this tx, which
@@ -1055,13 +1060,7 @@ func (t *TxPublisher) handleInitialTxError(requestID uint64, err error) {
10551060
// TODO(yy): Find out which input is causing the failure and fail that
10561061
// one only.
10571062
default:
1058-
event = TxFatal
1059-
}
1060-
1061-
result := &BumpResult{
1062-
Event: event,
1063-
Err: err,
1064-
requestID: requestID,
1063+
result.Event = TxFatal
10651064
}
10661065

10671066
t.handleResult(result)
@@ -1089,7 +1088,7 @@ func (t *TxPublisher) handleInitialBroadcast(r *monitorRecord) {
10891088
log.Errorf("Initial broadcast failed: %v", err)
10901089

10911090
// We now handle the initialization error and exit.
1092-
t.handleInitialTxError(r.requestID, err)
1091+
t.handleInitialTxError(r, err)
10931092

10941093
return
10951094
}
@@ -1261,7 +1260,7 @@ func (t *TxPublisher) createAndPublishTx(
12611260
// NOTE: The fee function is expected to have increased its returned
12621261
// fee rate after calling the SkipFeeBump method. So we can use it
12631262
// directly here.
1264-
sweepCtx, err := t.createAndCheckTx(r.req, r.feeFunction)
1263+
sweepCtx, err := t.createAndCheckTx(r)
12651264

12661265
// If there's an error creating the replacement tx, we need to abort the
12671266
// flow and handle it.

sweep/fee_bumper_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,14 @@ func TestCreateAndCheckTx(t *testing.T) {
504504
for _, tc := range testCases {
505505
tc := tc
506506

507+
r := &monitorRecord{
508+
req: tc.req,
509+
feeFunction: m.feeFunc,
510+
}
511+
507512
t.Run(tc.name, func(t *testing.T) {
508513
// Call the method under test.
509-
_, err := tp.createAndCheckTx(tc.req, m.feeFunc)
514+
_, err := tp.createAndCheckTx(r)
510515

511516
// Check the result is as expected.
512517
require.ErrorIs(t, err, tc.expectedErr)

0 commit comments

Comments
 (0)