diff --git a/sweep/sweeper.go b/sweep/sweeper.go index 505292912..cb70a11c3 100644 --- a/sweep/sweeper.go +++ b/sweep/sweeper.go @@ -104,62 +104,61 @@ func (p Params) String() string { type SweepState uint8 const ( - // StateInit is the initial state of a pending input. This is set when - // a new sweeping request for a given input is made. - StateInit SweepState = iota + // Init is the initial state of a pending input. This is set when a new + // sweeping request for a given input is made. + Init SweepState = iota - // StatePendingPublish specifies an input's state where it's already - // been included in a sweeping tx but the tx is not published yet. - // Inputs in this state should not be used for grouping again. - StatePendingPublish + // PendingPublish specifies an input's state where it's already been + // included in a sweeping tx but the tx is not published yet. Inputs + // in this state should not be used for grouping again. + PendingPublish - // StatePublished is the state where the input's sweeping tx has + // Published is the state where the input's sweeping tx has // successfully been published. Inputs in this state can only be // updated via RBF. - StatePublished + Published - // StatePublishFailed is the state when an error is returned from - // publishing the sweeping tx. Inputs in this state can be re-grouped - // in to a new sweeping tx. - StatePublishFailed + // PublishFailed is the state when an error is returned from publishing + // the sweeping tx. Inputs in this state can be re-grouped in to a new + // sweeping tx. + PublishFailed - // StateSwept is the final state of a pending input. This is set when - // the input has been successfully swept. - StateSwept + // Swept is the final state of a pending input. This is set when the + // input has been successfully swept. + Swept - // StateExcluded is the state of a pending input that has been excluded - // and can no longer be swept. For instance, when one of the three - // anchor sweeping transactions confirmed, the remaining two will be - // excluded. - StateExcluded + // Excluded is the state of a pending input that has been excluded and + // can no longer be swept. For instance, when one of the three anchor + // sweeping transactions confirmed, the remaining two will be excluded. + Excluded - // StateFailed is the state when a pending input has too many failed - // publish atttempts or unknown broadcast error is returned. - StateFailed + // Failed is the state when a pending input has too many failed publish + // atttempts or unknown broadcast error is returned. + Failed ) // String gives a human readable text for the sweep states. func (s SweepState) String() string { switch s { - case StateInit: + case Init: return "Init" - case StatePendingPublish: + case PendingPublish: return "PendingPublish" - case StatePublished: + case Published: return "Published" - case StatePublishFailed: + case PublishFailed: return "PublishFailed" - case StateSwept: + case Swept: return "Swept" - case StateExcluded: + case Excluded: return "Excluded" - case StateFailed: + case Failed: return "Failed" default: @@ -231,7 +230,7 @@ func (p *pendingInput) terminated() bool { // If the input has reached a final state, that it's either // been swept, or failed, or excluded, we will remove it from // our sweeper. - case StateFailed, StateSwept, StateExcluded: + case Failed, Swept, Excluded: return true default: @@ -752,7 +751,7 @@ func (s *UtxoSweeper) removeExclusiveGroup(group uint64) { }) // Update the input's state as it can no longer be swept. - input.state = StateExcluded + input.state = Excluded // Remove all unconfirmed transactions from the wallet which // spend the passed outpoint of the same exclusive group. @@ -886,7 +885,7 @@ func (s *UtxoSweeper) markInputsPendingPublish(set InputSet) { } // Update the input's state. - pi.state = StatePendingPublish + pi.state = PendingPublish // Record another publish attempt. pi.publishAttempts++ @@ -923,16 +922,16 @@ func (s *UtxoSweeper) markInputsPublished(tr *TxRecord, } // Valdiate that the input is in an expected state. - if pi.state != StatePendingPublish { + if pi.state != PendingPublish { log.Errorf("Expect input %v to have %v, instead it "+ "has %v", input.PreviousOutPoint, - StatePendingPublish, pi.state) + PendingPublish, pi.state) continue } // Update the input's state. - pi.state = StatePublished + pi.state = Published } return nil @@ -954,9 +953,9 @@ func (s *UtxoSweeper) markInputsPublishFailed(outpoints []wire.OutPoint) { } // Valdiate that the input is in an expected state. - if pi.state != StatePendingPublish { + if pi.state != PendingPublish { log.Errorf("Expect input %v to have %v, instead it "+ - "has %v", op, StatePendingPublish, pi.state) + "has %v", op, PendingPublish, pi.state) continue } @@ -964,7 +963,7 @@ func (s *UtxoSweeper) markInputsPublishFailed(outpoints []wire.OutPoint) { log.Warnf("Failed to publish input %v", op) // Update the input's state. - pi.state = StatePublishFailed + pi.state = PublishFailed } } @@ -1138,7 +1137,7 @@ func (s *UtxoSweeper) handleUpdateReq(req *updateReq) ( // our sweeper. // // TODO(yy): a dedicated state? - pendingInput.state = StateInit + pendingInput.state = Init resultChan := make(chan Result, 1) pendingInput.listeners = append(pendingInput.listeners, resultChan) @@ -1282,11 +1281,11 @@ func (s *UtxoSweeper) decideStateAndRBFInfo(op wire.OutPoint) ( // - for neutrino we don't have a mempool. // - for btcd below v0.24.1 we don't have `gettxspendingprevout`. if tx == nil { - return StateInit, fn.None[RBFInfo]() + return Init, fn.None[RBFInfo]() } // Otherwise the input is already spent in the mempool, so eventually - // we will return StatePublished. + // we will return Published. // // We also need to update the RBF info for this input. If the sweeping // transaction is broadcast by us, we can find the fee info in the @@ -1300,7 +1299,7 @@ func (s *UtxoSweeper) decideStateAndRBFInfo(op wire.OutPoint) ( // pendingInputs. if errors.Is(err, ErrTxNotFound) { log.Warnf("Spending tx %v not found in sweeper store", txid) - return StatePublished, fn.None[RBFInfo]() + return Published, fn.None[RBFInfo]() } // Exit if we get an db error. @@ -1308,7 +1307,7 @@ func (s *UtxoSweeper) decideStateAndRBFInfo(op wire.OutPoint) ( log.Errorf("Unable to get tx %v from sweeper store: %v", txid, err) - return StatePublished, fn.None[RBFInfo]() + return Published, fn.None[RBFInfo]() } // Prepare the fee info and return it. @@ -1318,7 +1317,7 @@ func (s *UtxoSweeper) decideStateAndRBFInfo(op wire.OutPoint) ( FeeRate: chainfee.SatPerKWeight(tr.FeeRate), }) - return StatePublished, rbf + return Published, rbf } // handleExistingInput processes an input that is already known to the sweeper. @@ -1437,7 +1436,7 @@ func (s *UtxoSweeper) markInputsSwept(tx *wire.MsgTx, isOurTx bool) { continue } - input.state = StateSwept + input.state = Swept // Return either a nil or a remote spend result. var err error @@ -1465,7 +1464,7 @@ func (s *UtxoSweeper) markInputsSwept(tx *wire.MsgTx, isOurTx bool) { func (s *UtxoSweeper) markInputFailed(pi *pendingInput, err error) { log.Errorf("Failed to sweep input: %v, error: %v", pi, err) - pi.state = StateFailed + pi.state = Failed // Remove all other inputs in this exclusive group. if pi.params.ExclusiveGroup != nil { @@ -1477,8 +1476,7 @@ func (s *UtxoSweeper) markInputFailed(pi *pendingInput, err error) { // updateSweeperInputs updates the sweeper's internal state and returns a map // of inputs to be swept. It will remove the inputs that are in final states, -// and returns a map of inputs that have either StateInit or -// StatePublishFailed. +// and returns a map of inputs that have either state Init or PublishFailed. func (s *UtxoSweeper) updateSweeperInputs() pendingInputs { // Create a map of inputs to be swept. inputs := make(pendingInputs) @@ -1505,13 +1503,13 @@ func (s *UtxoSweeper) updateSweeperInputs() pendingInputs { // If this input has been included in a sweep tx that's not // published yet, we'd skip this input and wait for the sweep // tx to be published. - if input.state == StatePendingPublish { + if input.state == PendingPublish { continue } // If this input has already been published, we will need to // check the RBF condition before attempting another sweeping. - if input.state == StatePublished { + if input.state == Published { continue } @@ -1666,7 +1664,7 @@ func (s *UtxoSweeper) handleBumpEventTxReplaced(r *BumpResult) error { // // TODO(yy): we may also need to update the inputs in this tx to a new // state. Suppose a replacing tx only spends a subset of the inputs - // here, we'd end up with the rest being marked as `StatePublished` and + // here, we'd end up with the rest being marked as `Published` and // won't be aggregated in the next sweep. Atm it's fine as we always // RBF the same input set. if err := s.cfg.Store.DeleteTx(oldTxid); err != nil { diff --git a/sweep/sweeper_test.go b/sweep/sweeper_test.go index ee143e3d3..6ad927206 100644 --- a/sweep/sweeper_test.go +++ b/sweep/sweeper_test.go @@ -2147,7 +2147,7 @@ func TestMarkInputsPendingPublish(t *testing.T) { inputInit.On("OutPoint").Return(&wire.OutPoint{Index: 1}) s.pendingInputs[*inputInit.OutPoint()] = &pendingInput{ - state: StateInit, + state: Init, } // inputPendingPublish specifies an input that's about to be published. @@ -2157,7 +2157,7 @@ func TestMarkInputsPendingPublish(t *testing.T) { inputPendingPublish.On("OutPoint").Return(&wire.OutPoint{Index: 2}) s.pendingInputs[*inputPendingPublish.OutPoint()] = &pendingInput{ - state: StatePendingPublish, + state: PendingPublish, } // inputTerminated specifies an input that's terminated. @@ -2167,7 +2167,7 @@ func TestMarkInputsPendingPublish(t *testing.T) { inputTerminated.On("OutPoint").Return(&wire.OutPoint{Index: 3}) s.pendingInputs[*inputTerminated.OutPoint()] = &pendingInput{ - state: StateExcluded, + state: Excluded, } // Mark the test inputs. We expect the non-exist input and the @@ -2182,20 +2182,20 @@ func TestMarkInputsPendingPublish(t *testing.T) { require.Len(s.pendingInputs, 3) // We expect the init input's state to become pending publish. - require.Equal(StatePendingPublish, + require.Equal(PendingPublish, s.pendingInputs[*inputInit.OutPoint()].state) // We expect the pending-publish to stay unchanged. - require.Equal(StatePendingPublish, + require.Equal(PendingPublish, s.pendingInputs[*inputPendingPublish.OutPoint()].state) // We expect the terminated to stay unchanged. - require.Equal(StateExcluded, + require.Equal(Excluded, s.pendingInputs[*inputTerminated.OutPoint()].state) } // TestMarkInputsPublished checks that given a list of inputs with different -// states, only the state `StatePendingPublish` will be marked as `Published`. +// states, only the state `PendingPublish` will be marked as `Published`. func TestMarkInputsPublished(t *testing.T) { t.Parallel() @@ -2228,7 +2228,7 @@ func TestMarkInputsPublished(t *testing.T) { PreviousOutPoint: wire.OutPoint{Index: 2}, } s.pendingInputs[inputInit.PreviousOutPoint] = &pendingInput{ - state: StateInit, + state: Init, } // inputPendingPublish specifies an input that's about to be published. @@ -2236,7 +2236,7 @@ func TestMarkInputsPublished(t *testing.T) { PreviousOutPoint: wire.OutPoint{Index: 3}, } s.pendingInputs[inputPendingPublish.PreviousOutPoint] = &pendingInput{ - state: StatePendingPublish, + state: PendingPublish, } // First, check that when an error is returned from db, it's properly @@ -2266,11 +2266,11 @@ func TestMarkInputsPublished(t *testing.T) { require.Len(s.pendingInputs, 2) // We expect the init input's state to stay unchanged. - require.Equal(StateInit, + require.Equal(Init, s.pendingInputs[inputInit.PreviousOutPoint].state) // We expect the pending-publish input's is now marked as published. - require.Equal(StatePublished, + require.Equal(Published, s.pendingInputs[inputPendingPublish.PreviousOutPoint].state) // Assert mocked statements are executed as expected. @@ -2278,7 +2278,7 @@ func TestMarkInputsPublished(t *testing.T) { } // TestMarkInputsPublishFailed checks that given a list of inputs with -// different states, only the state `StatePendingPublish` will be marked as +// different states, only the state `PendingPublish` will be marked as // `PublishFailed`. func TestMarkInputsPublishFailed(t *testing.T) { t.Parallel() @@ -2308,7 +2308,7 @@ func TestMarkInputsPublishFailed(t *testing.T) { PreviousOutPoint: wire.OutPoint{Index: 2}, } s.pendingInputs[inputInit.PreviousOutPoint] = &pendingInput{ - state: StateInit, + state: Init, } // inputPendingPublish specifies an input that's about to be published. @@ -2316,7 +2316,7 @@ func TestMarkInputsPublishFailed(t *testing.T) { PreviousOutPoint: wire.OutPoint{Index: 3}, } s.pendingInputs[inputPendingPublish.PreviousOutPoint] = &pendingInput{ - state: StatePendingPublish, + state: PendingPublish, } // Mark the test inputs. We expect the non-exist input and the @@ -2332,12 +2332,12 @@ func TestMarkInputsPublishFailed(t *testing.T) { require.Len(s.pendingInputs, 2) // We expect the init input's state to stay unchanged. - require.Equal(StateInit, + require.Equal(Init, s.pendingInputs[inputInit.PreviousOutPoint].state) // We expect the pending-publish input's is now marked as publish // failed. - require.Equal(StatePublishFailed, + require.Equal(PublishFailed, s.pendingInputs[inputPendingPublish.PreviousOutPoint].state) // Assert mocked statements are executed as expected. @@ -2345,7 +2345,7 @@ func TestMarkInputsPublishFailed(t *testing.T) { } // TestMarkInputsSwept checks that given a list of inputs with different -// states, only the non-terminal state will be marked as `StateSwept`. +// states, only the non-terminal state will be marked as `Swept`. func TestMarkInputsSwept(t *testing.T) { t.Parallel() @@ -2374,7 +2374,7 @@ func TestMarkInputsSwept(t *testing.T) { PreviousOutPoint: wire.OutPoint{Index: 2}, } s.pendingInputs[inputInit.PreviousOutPoint] = &pendingInput{ - state: StateInit, + state: Init, Input: mockInput, } @@ -2383,7 +2383,7 @@ func TestMarkInputsSwept(t *testing.T) { PreviousOutPoint: wire.OutPoint{Index: 3}, } s.pendingInputs[inputPendingPublish.PreviousOutPoint] = &pendingInput{ - state: StatePendingPublish, + state: PendingPublish, Input: mockInput, } @@ -2392,7 +2392,7 @@ func TestMarkInputsSwept(t *testing.T) { PreviousOutPoint: wire.OutPoint{Index: 4}, } s.pendingInputs[inputTerminated.PreviousOutPoint] = &pendingInput{ - state: StateExcluded, + state: Excluded, Input: mockInput, } @@ -2411,15 +2411,15 @@ func TestMarkInputsSwept(t *testing.T) { require.Len(s.pendingInputs, 3) // We expect the init input's state to become swept. - require.Equal(StateSwept, + require.Equal(Swept, s.pendingInputs[inputInit.PreviousOutPoint].state) // We expect the pending-publish becomes swept. - require.Equal(StateSwept, + require.Equal(Swept, s.pendingInputs[inputPendingPublish.PreviousOutPoint].state) // We expect the terminated to stay unchanged. - require.Equal(StateExcluded, + require.Equal(Excluded, s.pendingInputs[inputTerminated.PreviousOutPoint].state) } @@ -2479,13 +2479,13 @@ func TestUpdateSweeperInputs(t *testing.T) { s := New(nil) // Create a list of inputs using all the states. - input0 := &pendingInput{state: StateInit} - input1 := &pendingInput{state: StatePendingPublish} - input2 := &pendingInput{state: StatePublished} - input3 := &pendingInput{state: StatePublishFailed} - input4 := &pendingInput{state: StateSwept} - input5 := &pendingInput{state: StateExcluded} - input6 := &pendingInput{state: StateFailed} + input0 := &pendingInput{state: Init} + input1 := &pendingInput{state: PendingPublish} + input2 := &pendingInput{state: Published} + input3 := &pendingInput{state: PublishFailed} + input4 := &pendingInput{state: Swept} + input5 := &pendingInput{state: Excluded} + input6 := &pendingInput{state: Failed} // Add the inputs to the sweeper. After the update, we should see the // terminated inputs being removed. @@ -2499,8 +2499,8 @@ func TestUpdateSweeperInputs(t *testing.T) { {Index: 6}: input6, } - // We expect the inputs with `StateSwept`, `StateExcluded`, and - // `StateFailed` to be removed. + // We expect the inputs with `Swept`, `Excluded`, and `Failed` to be + // removed. expectedInputs := map[wire.OutPoint]*pendingInput{ {Index: 0}: input0, {Index: 1}: input1, @@ -2508,8 +2508,8 @@ func TestUpdateSweeperInputs(t *testing.T) { {Index: 3}: input3, } - // We expect only the inputs with `StateInit` and `StatePublishFailed` - // to be returned. + // We expect only the inputs with `Init` and `PublishFailed` to be + // returned. expectedReturn := map[wire.OutPoint]*pendingInput{ {Index: 0}: input0, {Index: 3}: input3, @@ -2556,7 +2556,7 @@ func TestDecideStateAndRBFInfo(t *testing.T) { // RBFInfo. state, rbf := s.decideStateAndRBFInfo(op) require.True(rbf.IsNone()) - require.Equal(StateInit, state) + require.Equal(Init, state) // Mock the mempool lookup to return a tx three times as we are calling // attachAvailableRBFInfo three times. @@ -2570,7 +2570,7 @@ func TestDecideStateAndRBFInfo(t *testing.T) { // Although the db lookup failed, we expect the state to be Published. state, rbf = s.decideStateAndRBFInfo(op) require.True(rbf.IsNone()) - require.Equal(StatePublished, state) + require.Equal(Published, state) // Mock the store to return a db error. dummyErr := errors.New("dummy error") @@ -2579,7 +2579,7 @@ func TestDecideStateAndRBFInfo(t *testing.T) { // Although the db lookup failed, we expect the state to be Published. state, rbf = s.decideStateAndRBFInfo(op) require.True(rbf.IsNone()) - require.Equal(StatePublished, state) + require.Equal(Published, state) // Mock the store to return a record. tr := &TxRecord{ @@ -2600,7 +2600,7 @@ func TestDecideStateAndRBFInfo(t *testing.T) { require.Equal(rbfInfo, rbf) // Assert the state is updated. - require.Equal(StatePublished, state) + require.Equal(Published, state) } // TestMarkInputFailed checks that the input is marked as failed as expected. @@ -2619,7 +2619,7 @@ func TestMarkInputFailed(t *testing.T) { // Create a testing pending input. pi := &pendingInput{ - state: StateInit, + state: Init, Input: mockInput, } @@ -2627,7 +2627,7 @@ func TestMarkInputFailed(t *testing.T) { s.markInputFailed(pi, errors.New("dummy error")) // Assert the state is updated. - require.Equal(t, StateFailed, pi.state) + require.Equal(t, Failed, pi.state) } // TestSweepPendingInputs checks that `sweepPendingInputs` correctly executes @@ -2730,9 +2730,9 @@ func TestHandleBumpEventTxFailed(t *testing.T) { // Construct the initial state for the sweeper. s.pendingInputs = pendingInputs{ - op1: &pendingInput{Input: input1, state: StatePendingPublish}, - op2: &pendingInput{Input: input2, state: StatePendingPublish}, - op3: &pendingInput{Input: input3, state: StatePendingPublish}, + op1: &pendingInput{Input: input1, state: PendingPublish}, + op2: &pendingInput{Input: input2, state: PendingPublish}, + op3: &pendingInput{Input: input3, state: PendingPublish}, } // Create a testing tx that spends the first two inputs. @@ -2756,11 +2756,11 @@ func TestHandleBumpEventTxFailed(t *testing.T) { require.ErrorIs(t, err, errDummy) // Assert the states of the first two inputs are updated. - require.Equal(t, StatePublishFailed, s.pendingInputs[op1].state) - require.Equal(t, StatePublishFailed, s.pendingInputs[op2].state) + require.Equal(t, PublishFailed, s.pendingInputs[op1].state) + require.Equal(t, PublishFailed, s.pendingInputs[op2].state) // Assert the state of the third input is not updated. - require.Equal(t, StatePendingPublish, s.pendingInputs[op3].state) + require.Equal(t, PendingPublish, s.pendingInputs[op3].state) // Assert the non-existing input is not added to the pending inputs. require.NotContains(t, s.pendingInputs, opNotExist) @@ -2789,7 +2789,7 @@ func TestHandleBumpEventTxReplaced(t *testing.T) { // Construct the initial state for the sweeper. s.pendingInputs = pendingInputs{ - op: &pendingInput{Input: inp, state: StatePendingPublish}, + op: &pendingInput{Input: inp, state: PendingPublish}, } // Create a testing tx that spends the input. @@ -2853,7 +2853,7 @@ func TestHandleBumpEventTxReplaced(t *testing.T) { require.NoError(t, err) // Assert the state of the input is updated. - require.Equal(t, StatePublished, s.pendingInputs[op].state) + require.Equal(t, Published, s.pendingInputs[op].state) } // TestHandleBumpEventTxPublished checks that the sweeper correctly handles the @@ -2879,7 +2879,7 @@ func TestHandleBumpEventTxPublished(t *testing.T) { // Construct the initial state for the sweeper. s.pendingInputs = pendingInputs{ - op: &pendingInput{Input: inp, state: StatePendingPublish}, + op: &pendingInput{Input: inp, state: PendingPublish}, } // Create a testing tx that spends the input. @@ -2907,7 +2907,7 @@ func TestHandleBumpEventTxPublished(t *testing.T) { require.NoError(t, err) // Assert the state of the input is updated. - require.Equal(t, StatePublished, s.pendingInputs[op].state) + require.Equal(t, Published, s.pendingInputs[op].state) } // TestMonitorFeeBumpResult checks that the fee bump monitor loop correctly @@ -2931,7 +2931,7 @@ func TestMonitorFeeBumpResult(t *testing.T) { // Construct the initial state for the sweeper. s.pendingInputs = pendingInputs{ - op: &pendingInput{Input: inp, state: StatePendingPublish}, + op: &pendingInput{Input: inp, state: PendingPublish}, } // Create a testing tx that spends the input.