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.
This commit is contained in:
yyforyongyu committed 2025-02-20 14:40:53 +08:00
1 parent f614e7aed9
commit 4f469de18e
2 files changed
+23 -19

No files matched your search

+17 -18
View File
@@ -544,7 +544,7 @@ func (t *TxPublisher) createRBFCompliantTx(
for { for {
// Create a new tx with the given fee rate and check its // Create a new tx with the given fee rate and check its
// mempool acceptance. // mempool acceptance.
sweepCtx, err := t.createAndCheckTx(r.req, f) sweepCtx, err := t.createAndCheckTx(r)
switch { switch {
case err == nil: case err == nil:
@@ -607,8 +607,9 @@ func (t *TxPublisher) createRBFCompliantTx(
// script, and the fee rate. In addition, it validates the tx's mempool // script, and the fee rate. In addition, it validates the tx's mempool
// acceptance before returning a tx that can be published directly, along with // acceptance before returning a tx that can be published directly, along with
// its fee. // its fee.
func (t *TxPublisher) createAndCheckTx(req *BumpRequest, func (t *TxPublisher) createAndCheckTx(r *monitorRecord) (*sweepTxCtx, error) {
f FeeFunction) (*sweepTxCtx, error) { req := r.req
f := r.feeFunction
// Create the sweep tx with max fee rate of 0 as the fee function // Create the sweep tx with max fee rate of 0 as the fee function
// guarantees the fee rate used here won't exceed the max fee rate. // guarantees the fee rate used here won't exceed the max fee rate.
@@ -1025,27 +1026,31 @@ func (t *TxPublisher) handleTxConfirmed(r *monitorRecord) {
// handleInitialTxError takes the error from `initializeTx` and decides the // handleInitialTxError takes the error from `initializeTx` and decides the
// bump event. It will construct a BumpResult and handles it. // bump event. It will construct a BumpResult and handles it.
func (t *TxPublisher) handleInitialTxError(requestID uint64, err error) { func (t *TxPublisher) handleInitialTxError(r *monitorRecord, err error) {
// We now decide what type of event to send. // Create a bump result to be sent to the sweeper.
var event BumpEvent result := &BumpResult{
Err: err,
requestID: r.requestID,
}
// We now decide what type of event to send.
switch { switch {
// When the error is due to a dust output, we'll send a TxFailed so // When the error is due to a dust output, we'll send a TxFailed so
// these inputs can be retried with a different group in the next // these inputs can be retried with a different group in the next
// block. // block.
case errors.Is(err, ErrTxNoOutput): case errors.Is(err, ErrTxNoOutput):
event = TxFailed result.Event = TxFailed
// When the error is due to budget being used up, we'll send a TxFailed // When the error is due to budget being used up, we'll send a TxFailed
// so these inputs can be retried with a different group in the next // so these inputs can be retried with a different group in the next
// block. // block.
case errors.Is(err, ErrMaxPosition): case errors.Is(err, ErrMaxPosition):
event = TxFailed result.Event = TxFailed
// When the error is due to zero fee rate delta, we'll send a TxFailed // When the error is due to zero fee rate delta, we'll send a TxFailed
// so these inputs can be retried in the next block. // so these inputs can be retried in the next block.
case errors.Is(err, ErrZeroFeeRateDelta): case errors.Is(err, ErrZeroFeeRateDelta):
event = TxFailed result.Event = TxFailed
// Otherwise this is not a fee-related error and the tx cannot be // Otherwise this is not a fee-related error and the tx cannot be
// retried. In that case we will fail ALL the inputs in this tx, which // 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) {
// TODO(yy): Find out which input is causing the failure and fail that // TODO(yy): Find out which input is causing the failure and fail that
// one only. // one only.
default: default:
event = TxFatal result.Event = TxFatal
}
result := &BumpResult{
Event: event,
Err: err,
requestID: requestID,
} }
t.handleResult(result) t.handleResult(result)
@@ -1089,7 +1088,7 @@ func (t *TxPublisher) handleInitialBroadcast(r *monitorRecord) {
log.Errorf("Initial broadcast failed: %v", err) log.Errorf("Initial broadcast failed: %v", err)
// We now handle the initialization error and exit. // We now handle the initialization error and exit.
t.handleInitialTxError(r.requestID, err) t.handleInitialTxError(r, err)
return return
} }
@@ -1261,7 +1260,7 @@ func (t *TxPublisher) createAndPublishTx(
// NOTE: The fee function is expected to have increased its returned // NOTE: The fee function is expected to have increased its returned
// fee rate after calling the SkipFeeBump method. So we can use it // fee rate after calling the SkipFeeBump method. So we can use it
// directly here. // directly here.
sweepCtx, err := t.createAndCheckTx(r.req, r.feeFunction) sweepCtx, err := t.createAndCheckTx(r)
// If there's an error creating the replacement tx, we need to abort the // If there's an error creating the replacement tx, we need to abort the
// flow and handle it. // flow and handle it.
+6 -1
View File
@@ -504,9 +504,14 @@ func TestCreateAndCheckTx(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
tc := tc tc := tc
r := &monitorRecord{
req: tc.req,
feeFunction: m.feeFunc,
}
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
// Call the method under test. // Call the method under test.
_, err := tp.createAndCheckTx(tc.req, m.feeFunc) _, err := tp.createAndCheckTx(r)
// Check the result is as expected. // Check the result is as expected.
require.ErrorIs(t, err, tc.expectedErr) require.ErrorIs(t, err, tc.expectedErr)