2018-12-07 09:06:36 +01:00
|
|
|
package sweep
|
|
|
|
|
|
|
|
import (
|
2023-10-24 02:14:52 +02:00
|
|
|
"errors"
|
2018-12-07 09:06:36 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-02-23 14:48:00 +01:00
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
2018-12-07 09:06:36 +01:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
2023-10-24 07:14:21 +02:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2023-10-26 08:27:46 +02:00
|
|
|
"github.com/lightningnetwork/lnd/fn"
|
2019-01-16 15:47:43 +01:00
|
|
|
"github.com/lightningnetwork/lnd/input"
|
2024-06-04 08:00:38 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2019-10-31 03:43:05 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
2024-01-30 20:25:58 +01:00
|
|
|
"github.com/stretchr/testify/mock"
|
2020-09-04 11:28:17 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-12-07 09:06:36 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-01-17 10:21:09 +01:00
|
|
|
errDummy = errors.New("dummy error")
|
2018-12-07 09:06:36 +01:00
|
|
|
|
|
|
|
testPubKey, _ = btcec.ParsePubKey([]byte{
|
|
|
|
0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
|
|
|
|
0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e,
|
|
|
|
0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca,
|
|
|
|
0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0,
|
|
|
|
0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64,
|
|
|
|
0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9,
|
|
|
|
0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56,
|
|
|
|
0xb4, 0x12, 0xa3,
|
2022-02-23 14:48:00 +01:00
|
|
|
})
|
2018-12-07 09:06:36 +01:00
|
|
|
)
|
|
|
|
|
2024-01-11 00:54:32 +01:00
|
|
|
// TestMarkInputsPendingPublish checks that given a list of inputs with
|
|
|
|
// different states, only the non-terminal state will be marked as `Published`.
|
|
|
|
func TestMarkInputsPendingPublish(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Create a test sweeper.
|
2024-02-21 09:00:05 +01:00
|
|
|
s := New(&UtxoSweeperConfig{})
|
|
|
|
|
|
|
|
// Create a mock input set.
|
|
|
|
set := &MockInputSet{}
|
|
|
|
defer set.AssertExpectations(t)
|
2024-01-11 00:54:32 +01:00
|
|
|
|
|
|
|
// Create three testing inputs.
|
|
|
|
//
|
|
|
|
// inputNotExist specifies an input that's not found in the sweeper's
|
|
|
|
// `pendingInputs` map.
|
2024-02-21 09:00:05 +01:00
|
|
|
inputNotExist := &input.MockInput{}
|
|
|
|
defer inputNotExist.AssertExpectations(t)
|
|
|
|
|
2024-03-27 10:07:48 +01:00
|
|
|
inputNotExist.On("OutPoint").Return(wire.OutPoint{Index: 0})
|
2024-01-11 00:54:32 +01:00
|
|
|
|
|
|
|
// inputInit specifies a newly created input.
|
2024-02-21 09:00:05 +01:00
|
|
|
inputInit := &input.MockInput{}
|
|
|
|
defer inputInit.AssertExpectations(t)
|
|
|
|
|
2024-03-27 10:07:48 +01:00
|
|
|
inputInit.On("OutPoint").Return(wire.OutPoint{Index: 1})
|
2024-02-21 09:00:05 +01:00
|
|
|
|
2024-03-27 10:07:48 +01:00
|
|
|
s.inputs[inputInit.OutPoint()] = &SweeperInput{
|
2024-03-17 19:49:58 +01:00
|
|
|
state: Init,
|
2024-01-11 00:54:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// inputPendingPublish specifies an input that's about to be published.
|
2024-02-21 09:00:05 +01:00
|
|
|
inputPendingPublish := &input.MockInput{}
|
|
|
|
defer inputPendingPublish.AssertExpectations(t)
|
|
|
|
|
2024-03-27 10:07:48 +01:00
|
|
|
inputPendingPublish.On("OutPoint").Return(wire.OutPoint{Index: 2})
|
2024-02-21 09:00:05 +01:00
|
|
|
|
2024-03-27 10:07:48 +01:00
|
|
|
s.inputs[inputPendingPublish.OutPoint()] = &SweeperInput{
|
2024-03-17 19:49:58 +01:00
|
|
|
state: PendingPublish,
|
2024-01-11 00:54:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// inputTerminated specifies an input that's terminated.
|
2024-02-21 09:00:05 +01:00
|
|
|
inputTerminated := &input.MockInput{}
|
|
|
|
defer inputTerminated.AssertExpectations(t)
|
2024-01-11 00:54:32 +01:00
|
|
|
|
2024-03-27 10:07:48 +01:00
|
|
|
inputTerminated.On("OutPoint").Return(wire.OutPoint{Index: 3})
|
2024-01-11 00:54:32 +01:00
|
|
|
|
2024-03-27 10:07:48 +01:00
|
|
|
s.inputs[inputTerminated.OutPoint()] = &SweeperInput{
|
2024-03-17 19:49:58 +01:00
|
|
|
state: Excluded,
|
2024-02-21 09:00:05 +01:00
|
|
|
}
|
2024-01-11 00:54:32 +01:00
|
|
|
|
|
|
|
// Mark the test inputs. We expect the non-exist input and the
|
|
|
|
// inputTerminated to be skipped, and the rest to be marked as pending
|
|
|
|
// publish.
|
2024-02-21 09:00:05 +01:00
|
|
|
set.On("Inputs").Return([]input.Input{
|
2024-01-11 00:54:32 +01:00
|
|
|
inputNotExist, inputInit, inputPendingPublish, inputTerminated,
|
|
|
|
})
|
2024-02-21 09:00:05 +01:00
|
|
|
s.markInputsPendingPublish(set)
|
2024-01-11 00:54:32 +01:00
|
|
|
|
|
|
|
// We expect unchanged number of pending inputs.
|
2024-03-17 20:00:58 +01:00
|
|
|
require.Len(s.inputs, 3)
|
2024-01-11 00:54:32 +01:00
|
|
|
|
|
|
|
// We expect the init input's state to become pending publish.
|
2024-03-27 10:07:48 +01:00
|
|
|
require.Equal(PendingPublish, s.inputs[inputInit.OutPoint()].state)
|
2024-01-11 00:54:32 +01:00
|
|
|
|
|
|
|
// We expect the pending-publish to stay unchanged.
|
2024-03-17 19:49:58 +01:00
|
|
|
require.Equal(PendingPublish,
|
2024-03-27 10:07:48 +01:00
|
|
|
s.inputs[inputPendingPublish.OutPoint()].state)
|
2024-01-11 00:54:32 +01:00
|
|
|
|
|
|
|
// We expect the terminated to stay unchanged.
|
2024-03-27 10:07:48 +01:00
|
|
|
require.Equal(Excluded, s.inputs[inputTerminated.OutPoint()].state)
|
2024-01-11 00:54:32 +01:00
|
|
|
}
|
|
|
|
|
2023-10-24 02:14:52 +02:00
|
|
|
// TestMarkInputsPublished checks that given a list of inputs with different
|
2024-03-17 19:49:58 +01:00
|
|
|
// states, only the state `PendingPublish` will be marked as `Published`.
|
2023-10-24 02:14:52 +02:00
|
|
|
func TestMarkInputsPublished(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Create a mock sweeper store.
|
|
|
|
mockStore := NewMockSweeperStore()
|
|
|
|
|
|
|
|
// Create a test TxRecord and a dummy error.
|
|
|
|
dummyTR := &TxRecord{}
|
|
|
|
dummyErr := errors.New("dummy error")
|
|
|
|
|
|
|
|
// Create a test sweeper.
|
|
|
|
s := New(&UtxoSweeperConfig{
|
|
|
|
Store: mockStore,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Create three testing inputs.
|
|
|
|
//
|
|
|
|
// inputNotExist specifies an input that's not found in the sweeper's
|
2024-03-17 20:00:58 +01:00
|
|
|
// `inputs` map.
|
2023-10-24 02:14:52 +02:00
|
|
|
inputNotExist := &wire.TxIn{
|
|
|
|
PreviousOutPoint: wire.OutPoint{Index: 1},
|
|
|
|
}
|
|
|
|
|
|
|
|
// inputInit specifies a newly created input. When marking this as
|
|
|
|
// published, we should see an error log as this input hasn't been
|
|
|
|
// published yet.
|
|
|
|
inputInit := &wire.TxIn{
|
|
|
|
PreviousOutPoint: wire.OutPoint{Index: 2},
|
|
|
|
}
|
2024-03-17 20:20:37 +01:00
|
|
|
s.inputs[inputInit.PreviousOutPoint] = &SweeperInput{
|
2024-03-17 19:49:58 +01:00
|
|
|
state: Init,
|
2023-10-24 02:14:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// inputPendingPublish specifies an input that's about to be published.
|
|
|
|
inputPendingPublish := &wire.TxIn{
|
|
|
|
PreviousOutPoint: wire.OutPoint{Index: 3},
|
|
|
|
}
|
2024-03-17 20:20:37 +01:00
|
|
|
s.inputs[inputPendingPublish.PreviousOutPoint] = &SweeperInput{
|
2024-03-17 19:49:58 +01:00
|
|
|
state: PendingPublish,
|
2023-10-24 02:14:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// First, check that when an error is returned from db, it's properly
|
|
|
|
// returned here.
|
|
|
|
mockStore.On("StoreTx", dummyTR).Return(dummyErr).Once()
|
|
|
|
err := s.markInputsPublished(dummyTR, nil)
|
|
|
|
require.ErrorIs(err, dummyErr)
|
|
|
|
|
|
|
|
// We also expect the record has been marked as published.
|
|
|
|
require.True(dummyTR.Published)
|
|
|
|
|
|
|
|
// Then, check that the target input has will be correctly marked as
|
|
|
|
// published.
|
|
|
|
//
|
|
|
|
// Mock the store to return nil
|
|
|
|
mockStore.On("StoreTx", dummyTR).Return(nil).Once()
|
|
|
|
|
|
|
|
// Mark the test inputs. We expect the non-exist input and the
|
|
|
|
// inputInit to be skipped, and the final input to be marked as
|
|
|
|
// published.
|
|
|
|
err = s.markInputsPublished(dummyTR, []*wire.TxIn{
|
|
|
|
inputNotExist, inputInit, inputPendingPublish,
|
|
|
|
})
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// We expect unchanged number of pending inputs.
|
2024-03-17 20:00:58 +01:00
|
|
|
require.Len(s.inputs, 2)
|
2023-10-24 02:14:52 +02:00
|
|
|
|
|
|
|
// We expect the init input's state to stay unchanged.
|
2024-03-17 19:49:58 +01:00
|
|
|
require.Equal(Init,
|
2024-03-17 20:00:58 +01:00
|
|
|
s.inputs[inputInit.PreviousOutPoint].state)
|
2023-10-24 02:14:52 +02:00
|
|
|
|
|
|
|
// We expect the pending-publish input's is now marked as published.
|
2024-03-17 19:49:58 +01:00
|
|
|
require.Equal(Published,
|
2024-03-17 20:00:58 +01:00
|
|
|
s.inputs[inputPendingPublish.PreviousOutPoint].state)
|
2023-10-24 02:14:52 +02:00
|
|
|
|
|
|
|
// Assert mocked statements are executed as expected.
|
|
|
|
mockStore.AssertExpectations(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestMarkInputsPublishFailed checks that given a list of inputs with
|
2024-04-08 11:46:18 +02:00
|
|
|
// different states, only the state `PendingPublish` and `Published` will be
|
|
|
|
// marked as `PublishFailed`.
|
2023-10-24 02:14:52 +02:00
|
|
|
func TestMarkInputsPublishFailed(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Create a mock sweeper store.
|
|
|
|
mockStore := NewMockSweeperStore()
|
|
|
|
|
|
|
|
// Create a test sweeper.
|
|
|
|
s := New(&UtxoSweeperConfig{
|
|
|
|
Store: mockStore,
|
|
|
|
})
|
|
|
|
|
2024-05-14 14:47:57 +02:00
|
|
|
// Create testing inputs for each state.
|
2023-10-24 02:14:52 +02:00
|
|
|
//
|
|
|
|
// inputNotExist specifies an input that's not found in the sweeper's
|
2024-03-17 20:00:58 +01:00
|
|
|
// `inputs` map.
|
2023-10-24 02:14:52 +02:00
|
|
|
inputNotExist := &wire.TxIn{
|
|
|
|
PreviousOutPoint: wire.OutPoint{Index: 1},
|
|
|
|
}
|
|
|
|
|
|
|
|
// inputInit specifies a newly created input. When marking this as
|
|
|
|
// published, we should see an error log as this input hasn't been
|
|
|
|
// published yet.
|
|
|
|
inputInit := &wire.TxIn{
|
|
|
|
PreviousOutPoint: wire.OutPoint{Index: 2},
|
|
|
|
}
|
2024-03-17 20:20:37 +01:00
|
|
|
s.inputs[inputInit.PreviousOutPoint] = &SweeperInput{
|
2024-03-17 19:49:58 +01:00
|
|
|
state: Init,
|
2023-10-24 02:14:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// inputPendingPublish specifies an input that's about to be published.
|
|
|
|
inputPendingPublish := &wire.TxIn{
|
|
|
|
PreviousOutPoint: wire.OutPoint{Index: 3},
|
|
|
|
}
|
2024-03-17 20:20:37 +01:00
|
|
|
s.inputs[inputPendingPublish.PreviousOutPoint] = &SweeperInput{
|
2024-03-17 19:49:58 +01:00
|
|
|
state: PendingPublish,
|
2023-10-24 02:14:52 +02:00
|
|
|
}
|
|
|
|
|
2024-04-08 11:46:18 +02:00
|
|
|
// inputPublished specifies an input that's published.
|
|
|
|
inputPublished := &wire.TxIn{
|
|
|
|
PreviousOutPoint: wire.OutPoint{Index: 4},
|
|
|
|
}
|
|
|
|
s.inputs[inputPublished.PreviousOutPoint] = &SweeperInput{
|
|
|
|
state: Published,
|
|
|
|
}
|
|
|
|
|
2024-05-14 14:47:57 +02:00
|
|
|
// inputPublishFailed specifies an input that's failed to be published.
|
|
|
|
inputPublishFailed := &wire.TxIn{
|
|
|
|
PreviousOutPoint: wire.OutPoint{Index: 5},
|
|
|
|
}
|
|
|
|
s.inputs[inputPublishFailed.PreviousOutPoint] = &SweeperInput{
|
|
|
|
state: PublishFailed,
|
|
|
|
}
|
|
|
|
|
|
|
|
// inputSwept specifies an input that's swept.
|
|
|
|
inputSwept := &wire.TxIn{
|
|
|
|
PreviousOutPoint: wire.OutPoint{Index: 6},
|
|
|
|
}
|
|
|
|
s.inputs[inputSwept.PreviousOutPoint] = &SweeperInput{
|
|
|
|
state: Swept,
|
|
|
|
}
|
|
|
|
|
|
|
|
// inputExcluded specifies an input that's excluded.
|
|
|
|
inputExcluded := &wire.TxIn{
|
|
|
|
PreviousOutPoint: wire.OutPoint{Index: 7},
|
|
|
|
}
|
|
|
|
s.inputs[inputExcluded.PreviousOutPoint] = &SweeperInput{
|
|
|
|
state: Excluded,
|
|
|
|
}
|
|
|
|
|
|
|
|
// inputFailed specifies an input that's failed.
|
|
|
|
inputFailed := &wire.TxIn{
|
|
|
|
PreviousOutPoint: wire.OutPoint{Index: 8},
|
|
|
|
}
|
|
|
|
s.inputs[inputFailed.PreviousOutPoint] = &SweeperInput{
|
|
|
|
state: Failed,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gather all inputs' outpoints.
|
|
|
|
pendingOps := make([]wire.OutPoint, 0, len(s.inputs)+1)
|
|
|
|
for op := range s.inputs {
|
|
|
|
pendingOps = append(pendingOps, op)
|
|
|
|
}
|
|
|
|
pendingOps = append(pendingOps, inputNotExist.PreviousOutPoint)
|
|
|
|
|
2023-10-24 02:14:52 +02:00
|
|
|
// Mark the test inputs. We expect the non-exist input and the
|
|
|
|
// inputInit to be skipped, and the final input to be marked as
|
|
|
|
// published.
|
2024-05-14 14:47:57 +02:00
|
|
|
s.markInputsPublishFailed(pendingOps)
|
2023-10-24 02:14:52 +02:00
|
|
|
|
|
|
|
// We expect unchanged number of pending inputs.
|
2024-05-14 14:47:57 +02:00
|
|
|
require.Len(s.inputs, 7)
|
2023-10-24 02:14:52 +02:00
|
|
|
|
|
|
|
// We expect the init input's state to stay unchanged.
|
2024-03-17 19:49:58 +01:00
|
|
|
require.Equal(Init,
|
2024-03-17 20:00:58 +01:00
|
|
|
s.inputs[inputInit.PreviousOutPoint].state)
|
2023-10-24 02:14:52 +02:00
|
|
|
|
|
|
|
// We expect the pending-publish input's is now marked as publish
|
|
|
|
// failed.
|
2024-03-17 19:49:58 +01:00
|
|
|
require.Equal(PublishFailed,
|
2024-03-17 20:00:58 +01:00
|
|
|
s.inputs[inputPendingPublish.PreviousOutPoint].state)
|
2023-10-24 02:14:52 +02:00
|
|
|
|
2024-04-08 11:46:18 +02:00
|
|
|
// We expect the published input's is now marked as publish failed.
|
|
|
|
require.Equal(PublishFailed,
|
|
|
|
s.inputs[inputPublished.PreviousOutPoint].state)
|
|
|
|
|
2024-05-14 14:47:57 +02:00
|
|
|
// We expect the publish failed input to stay unchanged.
|
|
|
|
require.Equal(PublishFailed,
|
|
|
|
s.inputs[inputPublishFailed.PreviousOutPoint].state)
|
|
|
|
|
|
|
|
// We expect the swept input to stay unchanged.
|
|
|
|
require.Equal(Swept, s.inputs[inputSwept.PreviousOutPoint].state)
|
|
|
|
|
|
|
|
// We expect the excluded input to stay unchanged.
|
|
|
|
require.Equal(Excluded, s.inputs[inputExcluded.PreviousOutPoint].state)
|
|
|
|
|
|
|
|
// We expect the failed input to stay unchanged.
|
|
|
|
require.Equal(Failed, s.inputs[inputFailed.PreviousOutPoint].state)
|
|
|
|
|
2023-10-24 02:14:52 +02:00
|
|
|
// Assert mocked statements are executed as expected.
|
|
|
|
mockStore.AssertExpectations(t)
|
|
|
|
}
|
2023-10-24 07:14:21 +02:00
|
|
|
|
2024-01-11 00:54:32 +01:00
|
|
|
// TestMarkInputsSwept checks that given a list of inputs with different
|
2024-03-17 19:49:58 +01:00
|
|
|
// states, only the non-terminal state will be marked as `Swept`.
|
2024-01-11 00:54:32 +01:00
|
|
|
func TestMarkInputsSwept(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Create a mock input.
|
|
|
|
mockInput := &input.MockInput{}
|
|
|
|
defer mockInput.AssertExpectations(t)
|
|
|
|
|
|
|
|
// Mock the `OutPoint` to return a dummy outpoint.
|
2024-03-27 10:07:48 +01:00
|
|
|
mockInput.On("OutPoint").Return(wire.OutPoint{Hash: chainhash.Hash{1}})
|
2024-01-11 00:54:32 +01:00
|
|
|
|
|
|
|
// Create a test sweeper.
|
|
|
|
s := New(&UtxoSweeperConfig{})
|
|
|
|
|
|
|
|
// Create three testing inputs.
|
|
|
|
//
|
|
|
|
// inputNotExist specifies an input that's not found in the sweeper's
|
2024-03-17 20:00:58 +01:00
|
|
|
// `inputs` map.
|
2024-01-11 00:54:32 +01:00
|
|
|
inputNotExist := &wire.TxIn{
|
|
|
|
PreviousOutPoint: wire.OutPoint{Index: 1},
|
|
|
|
}
|
|
|
|
|
|
|
|
// inputInit specifies a newly created input.
|
|
|
|
inputInit := &wire.TxIn{
|
|
|
|
PreviousOutPoint: wire.OutPoint{Index: 2},
|
|
|
|
}
|
2024-03-17 20:20:37 +01:00
|
|
|
s.inputs[inputInit.PreviousOutPoint] = &SweeperInput{
|
2024-03-17 19:49:58 +01:00
|
|
|
state: Init,
|
2024-01-11 00:54:32 +01:00
|
|
|
Input: mockInput,
|
|
|
|
}
|
|
|
|
|
|
|
|
// inputPendingPublish specifies an input that's about to be published.
|
|
|
|
inputPendingPublish := &wire.TxIn{
|
|
|
|
PreviousOutPoint: wire.OutPoint{Index: 3},
|
|
|
|
}
|
2024-03-17 20:20:37 +01:00
|
|
|
s.inputs[inputPendingPublish.PreviousOutPoint] = &SweeperInput{
|
2024-03-17 19:49:58 +01:00
|
|
|
state: PendingPublish,
|
2024-01-11 00:54:32 +01:00
|
|
|
Input: mockInput,
|
|
|
|
}
|
|
|
|
|
|
|
|
// inputTerminated specifies an input that's terminated.
|
|
|
|
inputTerminated := &wire.TxIn{
|
|
|
|
PreviousOutPoint: wire.OutPoint{Index: 4},
|
|
|
|
}
|
2024-03-17 20:20:37 +01:00
|
|
|
s.inputs[inputTerminated.PreviousOutPoint] = &SweeperInput{
|
2024-03-17 19:49:58 +01:00
|
|
|
state: Excluded,
|
2024-01-11 00:54:32 +01:00
|
|
|
Input: mockInput,
|
|
|
|
}
|
|
|
|
|
|
|
|
tx := &wire.MsgTx{
|
|
|
|
TxIn: []*wire.TxIn{
|
|
|
|
inputNotExist, inputInit,
|
|
|
|
inputPendingPublish, inputTerminated,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the test inputs. We expect the inputTerminated to be skipped,
|
|
|
|
// and the rest to be marked as swept.
|
|
|
|
s.markInputsSwept(tx, true)
|
|
|
|
|
|
|
|
// We expect unchanged number of pending inputs.
|
2024-03-17 20:00:58 +01:00
|
|
|
require.Len(s.inputs, 3)
|
2024-01-11 00:54:32 +01:00
|
|
|
|
|
|
|
// We expect the init input's state to become swept.
|
2024-03-17 19:49:58 +01:00
|
|
|
require.Equal(Swept,
|
2024-03-17 20:00:58 +01:00
|
|
|
s.inputs[inputInit.PreviousOutPoint].state)
|
2024-01-11 00:54:32 +01:00
|
|
|
|
|
|
|
// We expect the pending-publish becomes swept.
|
2024-03-17 19:49:58 +01:00
|
|
|
require.Equal(Swept,
|
2024-03-17 20:00:58 +01:00
|
|
|
s.inputs[inputPendingPublish.PreviousOutPoint].state)
|
2024-01-11 00:54:32 +01:00
|
|
|
|
|
|
|
// We expect the terminated to stay unchanged.
|
2024-03-17 19:49:58 +01:00
|
|
|
require.Equal(Excluded,
|
2024-03-17 20:00:58 +01:00
|
|
|
s.inputs[inputTerminated.PreviousOutPoint].state)
|
2024-01-11 00:54:32 +01:00
|
|
|
}
|
|
|
|
|
2023-10-24 07:14:21 +02:00
|
|
|
// TestMempoolLookup checks that the method `mempoolLookup` works as expected.
|
|
|
|
func TestMempoolLookup(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Create a test outpoint.
|
|
|
|
op := wire.OutPoint{Index: 1}
|
|
|
|
|
|
|
|
// Create a mock mempool watcher.
|
|
|
|
mockMempool := chainntnfs.NewMockMempoolWatcher()
|
2024-02-24 05:21:54 +01:00
|
|
|
defer mockMempool.AssertExpectations(t)
|
2023-10-24 07:14:21 +02:00
|
|
|
|
|
|
|
// Create a test sweeper without a mempool.
|
|
|
|
s := New(&UtxoSweeperConfig{})
|
|
|
|
|
2024-02-24 05:21:54 +01:00
|
|
|
// Since we don't have a mempool, we expect the call to return a
|
|
|
|
// fn.None indicating it's not found.
|
|
|
|
tx := s.mempoolLookup(op)
|
|
|
|
require.True(tx.IsNone())
|
2023-10-24 07:14:21 +02:00
|
|
|
|
|
|
|
// Re-create the sweeper with the mocked mempool watcher.
|
|
|
|
s = New(&UtxoSweeperConfig{
|
|
|
|
Mempool: mockMempool,
|
|
|
|
})
|
|
|
|
|
2024-02-24 05:21:54 +01:00
|
|
|
// Mock the mempool watcher to return not found.
|
|
|
|
mockMempool.On("LookupInputMempoolSpend", op).Return(
|
|
|
|
fn.None[wire.MsgTx]()).Once()
|
2023-10-24 07:14:21 +02:00
|
|
|
|
2024-02-24 05:21:54 +01:00
|
|
|
// We expect a fn.None tx to be returned.
|
|
|
|
tx = s.mempoolLookup(op)
|
|
|
|
require.True(tx.IsNone())
|
2023-10-24 07:14:21 +02:00
|
|
|
|
2024-02-24 05:21:54 +01:00
|
|
|
// Mock the mempool to return a spending tx.
|
|
|
|
dummyTx := wire.MsgTx{}
|
|
|
|
mockMempool.On("LookupInputMempoolSpend", op).Return(
|
|
|
|
fn.Some(dummyTx)).Once()
|
2023-10-24 07:14:21 +02:00
|
|
|
|
|
|
|
// Calling the loopup again, we expect the dummyTx to be returned.
|
2024-02-24 05:21:54 +01:00
|
|
|
tx = s.mempoolLookup(op)
|
|
|
|
require.False(tx.IsNone())
|
|
|
|
require.Equal(dummyTx, tx.UnsafeFromSome())
|
2023-10-24 07:14:21 +02:00
|
|
|
}
|
2023-10-24 07:47:14 +02:00
|
|
|
|
|
|
|
// TestUpdateSweeperInputs checks that the method `updateSweeperInputs` will
|
|
|
|
// properly update the inputs based on their states.
|
|
|
|
func TestUpdateSweeperInputs(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Create a test sweeper.
|
|
|
|
s := New(nil)
|
|
|
|
|
2024-03-20 09:48:58 +01:00
|
|
|
// Create mock inputs.
|
|
|
|
inp1 := &input.MockInput{}
|
|
|
|
defer inp1.AssertExpectations(t)
|
|
|
|
inp2 := &input.MockInput{}
|
|
|
|
defer inp2.AssertExpectations(t)
|
2024-04-08 09:45:03 +02:00
|
|
|
inp3 := &input.MockInput{}
|
|
|
|
defer inp3.AssertExpectations(t)
|
2024-03-20 09:48:58 +01:00
|
|
|
|
2023-10-24 07:47:14 +02:00
|
|
|
// Create a list of inputs using all the states.
|
2024-03-20 09:48:58 +01:00
|
|
|
//
|
|
|
|
// Mock the input to have a locktime that's matured so it will be
|
|
|
|
// returned.
|
|
|
|
inp1.On("RequiredLockTime").Return(
|
|
|
|
uint32(s.currentHeight), false).Once()
|
2024-04-08 09:45:03 +02:00
|
|
|
inp1.On("BlocksToMaturity").Return(uint32(0)).Once()
|
|
|
|
inp1.On("HeightHint").Return(uint32(s.currentHeight)).Once()
|
2024-03-20 09:48:58 +01:00
|
|
|
input0 := &SweeperInput{state: Init, Input: inp1}
|
|
|
|
|
|
|
|
// These inputs won't hit RequiredLockTime so we won't mock.
|
|
|
|
input1 := &SweeperInput{state: PendingPublish, Input: inp1}
|
|
|
|
input2 := &SweeperInput{state: Published, Input: inp1}
|
|
|
|
|
|
|
|
// Mock the input to have a locktime that's matured so it will be
|
|
|
|
// returned.
|
|
|
|
inp1.On("RequiredLockTime").Return(
|
|
|
|
uint32(s.currentHeight), false).Once()
|
2024-04-08 09:45:03 +02:00
|
|
|
inp1.On("BlocksToMaturity").Return(uint32(0)).Once()
|
|
|
|
inp1.On("HeightHint").Return(uint32(s.currentHeight)).Once()
|
2024-03-20 09:48:58 +01:00
|
|
|
input3 := &SweeperInput{state: PublishFailed, Input: inp1}
|
|
|
|
|
|
|
|
// These inputs won't hit RequiredLockTime so we won't mock.
|
|
|
|
input4 := &SweeperInput{state: Swept, Input: inp1}
|
|
|
|
input5 := &SweeperInput{state: Excluded, Input: inp1}
|
|
|
|
input6 := &SweeperInput{state: Failed, Input: inp1}
|
|
|
|
|
|
|
|
// Mock the input to have a locktime in the future so it will NOT be
|
|
|
|
// returned.
|
|
|
|
inp2.On("RequiredLockTime").Return(
|
|
|
|
uint32(s.currentHeight+1), true).Once()
|
|
|
|
input7 := &SweeperInput{state: Init, Input: inp2}
|
2023-10-24 07:47:14 +02:00
|
|
|
|
2024-04-08 09:45:03 +02:00
|
|
|
// Mock the input to have a CSV expiry in the future so it will NOT be
|
|
|
|
// returned.
|
|
|
|
inp3.On("RequiredLockTime").Return(
|
|
|
|
uint32(s.currentHeight), false).Once()
|
|
|
|
inp3.On("BlocksToMaturity").Return(uint32(2)).Once()
|
|
|
|
inp3.On("HeightHint").Return(uint32(s.currentHeight)).Once()
|
|
|
|
input8 := &SweeperInput{state: Init, Input: inp3}
|
|
|
|
|
2023-10-24 07:47:14 +02:00
|
|
|
// Add the inputs to the sweeper. After the update, we should see the
|
|
|
|
// terminated inputs being removed.
|
2024-03-17 20:20:37 +01:00
|
|
|
s.inputs = map[wire.OutPoint]*SweeperInput{
|
2023-10-24 07:47:14 +02:00
|
|
|
{Index: 0}: input0,
|
|
|
|
{Index: 1}: input1,
|
|
|
|
{Index: 2}: input2,
|
|
|
|
{Index: 3}: input3,
|
|
|
|
{Index: 4}: input4,
|
|
|
|
{Index: 5}: input5,
|
|
|
|
{Index: 6}: input6,
|
2024-03-20 09:48:58 +01:00
|
|
|
{Index: 7}: input7,
|
2024-04-08 09:45:03 +02:00
|
|
|
{Index: 8}: input8,
|
2023-10-24 07:47:14 +02:00
|
|
|
}
|
|
|
|
|
2024-03-17 19:49:58 +01:00
|
|
|
// We expect the inputs with `Swept`, `Excluded`, and `Failed` to be
|
|
|
|
// removed.
|
2024-03-17 20:20:37 +01:00
|
|
|
expectedInputs := map[wire.OutPoint]*SweeperInput{
|
2023-10-24 07:47:14 +02:00
|
|
|
{Index: 0}: input0,
|
|
|
|
{Index: 1}: input1,
|
|
|
|
{Index: 2}: input2,
|
|
|
|
{Index: 3}: input3,
|
2024-03-20 09:48:58 +01:00
|
|
|
{Index: 7}: input7,
|
2024-04-08 09:45:03 +02:00
|
|
|
{Index: 8}: input8,
|
2023-10-24 07:47:14 +02:00
|
|
|
}
|
|
|
|
|
2024-03-17 19:49:58 +01:00
|
|
|
// We expect only the inputs with `Init` and `PublishFailed` to be
|
|
|
|
// returned.
|
2024-03-17 20:20:37 +01:00
|
|
|
expectedReturn := map[wire.OutPoint]*SweeperInput{
|
2023-10-24 07:47:14 +02:00
|
|
|
{Index: 0}: input0,
|
|
|
|
{Index: 3}: input3,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the sweeper inputs.
|
|
|
|
inputs := s.updateSweeperInputs()
|
|
|
|
|
|
|
|
// Assert the returned inputs are as expected.
|
|
|
|
require.Equal(expectedReturn, inputs)
|
|
|
|
|
|
|
|
// Assert the sweeper inputs are as expected.
|
2024-03-17 20:00:58 +01:00
|
|
|
require.Equal(expectedInputs, s.inputs)
|
2023-10-24 07:47:14 +02:00
|
|
|
}
|
2023-10-26 08:27:46 +02:00
|
|
|
|
2024-03-20 00:31:41 +01:00
|
|
|
// TestDecideStateAndRBFInfo checks that the expected state and RBFInfo are
|
|
|
|
// returned based on whether this input can be found both in mempool and the
|
|
|
|
// sweeper store.
|
|
|
|
func TestDecideStateAndRBFInfo(t *testing.T) {
|
2023-10-26 08:27:46 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Create a test outpoint.
|
|
|
|
op := wire.OutPoint{Index: 1}
|
|
|
|
|
|
|
|
// Create a mock mempool watcher and a mock sweeper store.
|
|
|
|
mockMempool := chainntnfs.NewMockMempoolWatcher()
|
2024-02-24 05:21:54 +01:00
|
|
|
defer mockMempool.AssertExpectations(t)
|
2023-10-26 08:27:46 +02:00
|
|
|
mockStore := NewMockSweeperStore()
|
2024-02-24 05:21:54 +01:00
|
|
|
defer mockStore.AssertExpectations(t)
|
2023-10-26 08:27:46 +02:00
|
|
|
|
|
|
|
// Create a test sweeper.
|
|
|
|
s := New(&UtxoSweeperConfig{
|
|
|
|
Store: mockStore,
|
|
|
|
Mempool: mockMempool,
|
|
|
|
})
|
|
|
|
|
2024-02-24 05:21:54 +01:00
|
|
|
// First, mock the mempool to return false.
|
|
|
|
mockMempool.On("LookupInputMempoolSpend", op).Return(
|
|
|
|
fn.None[wire.MsgTx]()).Once()
|
2023-10-26 08:27:46 +02:00
|
|
|
|
2024-03-20 00:31:41 +01:00
|
|
|
// Since the mempool lookup failed, we exepect state Init and no
|
|
|
|
// RBFInfo.
|
|
|
|
state, rbf := s.decideStateAndRBFInfo(op)
|
|
|
|
require.True(rbf.IsNone())
|
2024-03-17 19:49:58 +01:00
|
|
|
require.Equal(Init, state)
|
2023-10-26 08:27:46 +02:00
|
|
|
|
2024-02-24 05:21:54 +01:00
|
|
|
// Mock the mempool lookup to return a tx three times as we are calling
|
|
|
|
// attachAvailableRBFInfo three times.
|
|
|
|
tx := wire.MsgTx{}
|
|
|
|
mockMempool.On("LookupInputMempoolSpend", op).Return(
|
|
|
|
fn.Some(tx)).Times(3)
|
2023-10-26 08:27:46 +02:00
|
|
|
|
|
|
|
// Mock the store to return an error saying the tx cannot be found.
|
|
|
|
mockStore.On("GetTx", tx.TxHash()).Return(nil, ErrTxNotFound).Once()
|
|
|
|
|
2024-03-20 00:31:41 +01:00
|
|
|
// Although the db lookup failed, we expect the state to be Published.
|
|
|
|
state, rbf = s.decideStateAndRBFInfo(op)
|
|
|
|
require.True(rbf.IsNone())
|
2024-03-17 19:49:58 +01:00
|
|
|
require.Equal(Published, state)
|
2023-10-26 08:27:46 +02:00
|
|
|
|
|
|
|
// Mock the store to return a db error.
|
2024-02-24 05:21:54 +01:00
|
|
|
dummyErr := errors.New("dummy error")
|
2023-10-26 08:27:46 +02:00
|
|
|
mockStore.On("GetTx", tx.TxHash()).Return(nil, dummyErr).Once()
|
|
|
|
|
2024-03-20 00:31:41 +01:00
|
|
|
// Although the db lookup failed, we expect the state to be Published.
|
|
|
|
state, rbf = s.decideStateAndRBFInfo(op)
|
|
|
|
require.True(rbf.IsNone())
|
2024-03-17 19:49:58 +01:00
|
|
|
require.Equal(Published, state)
|
2023-10-26 08:27:46 +02:00
|
|
|
|
|
|
|
// Mock the store to return a record.
|
|
|
|
tr := &TxRecord{
|
|
|
|
Fee: 100,
|
|
|
|
FeeRate: 100,
|
|
|
|
}
|
|
|
|
mockStore.On("GetTx", tx.TxHash()).Return(tr, nil).Once()
|
|
|
|
|
|
|
|
// Call the method again.
|
2024-03-20 00:31:41 +01:00
|
|
|
state, rbf = s.decideStateAndRBFInfo(op)
|
2023-10-26 08:27:46 +02:00
|
|
|
|
2024-03-20 00:31:41 +01:00
|
|
|
// Assert that the RBF info is returned.
|
2023-10-26 08:27:46 +02:00
|
|
|
rbfInfo := fn.Some(RBFInfo{
|
|
|
|
Txid: tx.TxHash(),
|
|
|
|
Fee: btcutil.Amount(tr.Fee),
|
|
|
|
FeeRate: chainfee.SatPerKWeight(tr.FeeRate),
|
|
|
|
})
|
2024-03-20 00:31:41 +01:00
|
|
|
require.Equal(rbfInfo, rbf)
|
2023-10-26 08:27:46 +02:00
|
|
|
|
|
|
|
// Assert the state is updated.
|
2024-03-17 19:49:58 +01:00
|
|
|
require.Equal(Published, state)
|
2023-10-26 08:27:46 +02:00
|
|
|
}
|
2024-02-27 20:10:49 +01:00
|
|
|
|
|
|
|
// TestMarkInputFailed checks that the input is marked as failed as expected.
|
|
|
|
func TestMarkInputFailed(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Create a mock input.
|
|
|
|
mockInput := &input.MockInput{}
|
|
|
|
defer mockInput.AssertExpectations(t)
|
|
|
|
|
|
|
|
// Mock the `OutPoint` to return a dummy outpoint.
|
2024-03-27 10:07:48 +01:00
|
|
|
mockInput.On("OutPoint").Return(wire.OutPoint{Hash: chainhash.Hash{1}})
|
2024-02-27 20:10:49 +01:00
|
|
|
|
|
|
|
// Create a test sweeper.
|
|
|
|
s := New(&UtxoSweeperConfig{})
|
|
|
|
|
|
|
|
// Create a testing pending input.
|
2024-03-17 20:20:37 +01:00
|
|
|
pi := &SweeperInput{
|
2024-03-17 19:49:58 +01:00
|
|
|
state: Init,
|
2024-02-27 20:10:49 +01:00
|
|
|
Input: mockInput,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the method under test.
|
|
|
|
s.markInputFailed(pi, errors.New("dummy error"))
|
|
|
|
|
|
|
|
// Assert the state is updated.
|
2024-03-17 19:49:58 +01:00
|
|
|
require.Equal(t, Failed, pi.state)
|
2024-02-27 20:10:49 +01:00
|
|
|
}
|
2024-01-30 20:25:58 +01:00
|
|
|
|
|
|
|
// TestSweepPendingInputs checks that `sweepPendingInputs` correctly executes
|
|
|
|
// its workflow based on the returned values from the interfaces.
|
|
|
|
func TestSweepPendingInputs(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Create a mock wallet and aggregator.
|
|
|
|
wallet := &MockWallet{}
|
2024-01-17 10:21:09 +01:00
|
|
|
defer wallet.AssertExpectations(t)
|
|
|
|
|
2024-01-30 20:25:58 +01:00
|
|
|
aggregator := &mockUtxoAggregator{}
|
2024-01-17 10:21:09 +01:00
|
|
|
defer aggregator.AssertExpectations(t)
|
|
|
|
|
|
|
|
publisher := &MockBumper{}
|
|
|
|
defer publisher.AssertExpectations(t)
|
2024-01-30 20:25:58 +01:00
|
|
|
|
|
|
|
// Create a test sweeper.
|
|
|
|
s := New(&UtxoSweeperConfig{
|
|
|
|
Wallet: wallet,
|
|
|
|
Aggregator: aggregator,
|
2024-01-17 10:21:09 +01:00
|
|
|
Publisher: publisher,
|
2024-06-04 08:00:38 +02:00
|
|
|
GenSweepScript: func() fn.Result[lnwallet.AddrWithKey] {
|
|
|
|
//nolint:lll
|
|
|
|
return fn.Ok(lnwallet.AddrWithKey{
|
|
|
|
DeliveryAddress: testPubKey.SerializeCompressed(),
|
|
|
|
})
|
2024-01-17 10:21:09 +01:00
|
|
|
},
|
2024-03-18 21:45:05 +01:00
|
|
|
NoDeadlineConfTarget: uint32(DefaultDeadlineDelta),
|
2024-01-30 20:25:58 +01:00
|
|
|
})
|
|
|
|
|
2024-03-27 12:22:02 +01:00
|
|
|
// Set a current height to test the deadline override.
|
|
|
|
s.currentHeight = testHeight
|
|
|
|
|
2024-01-30 20:25:58 +01:00
|
|
|
// Create an input set that needs wallet inputs.
|
|
|
|
setNeedWallet := &MockInputSet{}
|
2024-01-17 10:21:09 +01:00
|
|
|
defer setNeedWallet.AssertExpectations(t)
|
2024-01-30 20:25:58 +01:00
|
|
|
|
|
|
|
// Mock this set to ask for wallet input.
|
|
|
|
setNeedWallet.On("NeedWalletInput").Return(true).Once()
|
|
|
|
setNeedWallet.On("AddWalletInputs", wallet).Return(nil).Once()
|
|
|
|
|
|
|
|
// Mock the wallet to require the lock once.
|
|
|
|
wallet.On("WithCoinSelectLock", mock.Anything).Return(nil).Once()
|
|
|
|
|
|
|
|
// Create an input set that doesn't need wallet inputs.
|
|
|
|
normalSet := &MockInputSet{}
|
2024-01-17 10:21:09 +01:00
|
|
|
defer normalSet.AssertExpectations(t)
|
|
|
|
|
2024-01-30 20:25:58 +01:00
|
|
|
normalSet.On("NeedWalletInput").Return(false).Once()
|
|
|
|
|
|
|
|
// Mock the methods used in `sweep`. This is not important for this
|
|
|
|
// unit test.
|
2024-05-20 16:02:13 +02:00
|
|
|
setNeedWallet.On("Inputs").Return(nil).Maybe()
|
2024-03-27 12:22:02 +01:00
|
|
|
setNeedWallet.On("DeadlineHeight").Return(testHeight).Once()
|
2024-01-17 10:21:09 +01:00
|
|
|
setNeedWallet.On("Budget").Return(btcutil.Amount(1)).Once()
|
2024-04-11 11:08:36 +02:00
|
|
|
setNeedWallet.On("StartingFeeRate").Return(
|
|
|
|
fn.None[chainfee.SatPerKWeight]()).Once()
|
2024-05-20 16:02:13 +02:00
|
|
|
normalSet.On("Inputs").Return(nil).Maybe()
|
2024-03-27 12:22:02 +01:00
|
|
|
normalSet.On("DeadlineHeight").Return(testHeight).Once()
|
2024-01-17 10:21:09 +01:00
|
|
|
normalSet.On("Budget").Return(btcutil.Amount(1)).Once()
|
2024-04-11 11:08:36 +02:00
|
|
|
normalSet.On("StartingFeeRate").Return(
|
|
|
|
fn.None[chainfee.SatPerKWeight]()).Once()
|
2024-01-30 20:25:58 +01:00
|
|
|
|
|
|
|
// Make pending inputs for testing. We don't need real values here as
|
|
|
|
// the returned clusters are mocked.
|
2024-03-17 20:20:37 +01:00
|
|
|
pis := make(InputsMap)
|
2024-01-30 20:25:58 +01:00
|
|
|
|
|
|
|
// Mock the aggregator to return the mocked input sets.
|
2024-04-15 21:15:34 +02:00
|
|
|
aggregator.On("ClusterInputs", pis).Return([]InputSet{
|
2024-01-30 20:25:58 +01:00
|
|
|
setNeedWallet, normalSet,
|
|
|
|
})
|
|
|
|
|
2024-01-17 10:21:09 +01:00
|
|
|
// Mock `Broadcast` to return an error. This should cause the
|
2024-01-30 20:25:58 +01:00
|
|
|
// `createSweepTx` inside `sweep` to fail. This is done so we can
|
|
|
|
// terminate the method early as we are only interested in testing the
|
|
|
|
// workflow in `sweepPendingInputs`. We don't need to test `sweep` here
|
|
|
|
// as it should be tested in its own unit test.
|
2024-01-17 10:21:09 +01:00
|
|
|
dummyErr := errors.New("dummy error")
|
|
|
|
publisher.On("Broadcast", mock.Anything).Return(nil, dummyErr).Twice()
|
2024-01-30 20:25:58 +01:00
|
|
|
|
|
|
|
// Call the method under test.
|
|
|
|
s.sweepPendingInputs(pis)
|
2024-01-17 10:21:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestHandleBumpEventTxFailed checks that the sweeper correctly handles the
|
|
|
|
// case where the bump event tx fails to be published.
|
|
|
|
func TestHandleBumpEventTxFailed(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Create a test sweeper.
|
|
|
|
s := New(&UtxoSweeperConfig{})
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Create four testing outpoints.
|
|
|
|
op1 = wire.OutPoint{Hash: chainhash.Hash{1}}
|
|
|
|
op2 = wire.OutPoint{Hash: chainhash.Hash{2}}
|
|
|
|
op3 = wire.OutPoint{Hash: chainhash.Hash{3}}
|
|
|
|
opNotExist = wire.OutPoint{Hash: chainhash.Hash{4}}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Create three mock inputs.
|
|
|
|
input1 := &input.MockInput{}
|
|
|
|
defer input1.AssertExpectations(t)
|
|
|
|
|
|
|
|
input2 := &input.MockInput{}
|
|
|
|
defer input2.AssertExpectations(t)
|
|
|
|
|
|
|
|
input3 := &input.MockInput{}
|
|
|
|
defer input3.AssertExpectations(t)
|
|
|
|
|
|
|
|
// Construct the initial state for the sweeper.
|
2024-03-17 20:20:37 +01:00
|
|
|
s.inputs = InputsMap{
|
|
|
|
op1: &SweeperInput{Input: input1, state: PendingPublish},
|
|
|
|
op2: &SweeperInput{Input: input2, state: PendingPublish},
|
|
|
|
op3: &SweeperInput{Input: input3, state: PendingPublish},
|
2024-01-17 10:21:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a testing tx that spends the first two inputs.
|
|
|
|
tx := &wire.MsgTx{
|
|
|
|
TxIn: []*wire.TxIn{
|
|
|
|
{PreviousOutPoint: op1},
|
|
|
|
{PreviousOutPoint: op2},
|
|
|
|
{PreviousOutPoint: opNotExist},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a testing bump result.
|
|
|
|
br := &BumpResult{
|
|
|
|
Tx: tx,
|
|
|
|
Event: TxFailed,
|
|
|
|
Err: errDummy,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the method under test.
|
|
|
|
err := s.handleBumpEvent(br)
|
|
|
|
require.ErrorIs(t, err, errDummy)
|
|
|
|
|
|
|
|
// Assert the states of the first two inputs are updated.
|
2024-03-17 20:00:58 +01:00
|
|
|
require.Equal(t, PublishFailed, s.inputs[op1].state)
|
|
|
|
require.Equal(t, PublishFailed, s.inputs[op2].state)
|
2024-01-17 10:21:09 +01:00
|
|
|
|
|
|
|
// Assert the state of the third input is not updated.
|
2024-03-17 20:00:58 +01:00
|
|
|
require.Equal(t, PendingPublish, s.inputs[op3].state)
|
2024-01-17 10:21:09 +01:00
|
|
|
|
|
|
|
// Assert the non-existing input is not added to the pending inputs.
|
2024-03-17 20:00:58 +01:00
|
|
|
require.NotContains(t, s.inputs, opNotExist)
|
2024-01-17 10:21:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestHandleBumpEventTxReplaced checks that the sweeper correctly handles the
|
|
|
|
// case where the bump event tx is replaced.
|
|
|
|
func TestHandleBumpEventTxReplaced(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Create a mock store.
|
|
|
|
store := &MockSweeperStore{}
|
|
|
|
defer store.AssertExpectations(t)
|
|
|
|
|
2024-03-21 13:34:43 +01:00
|
|
|
// Create a mock wallet.
|
|
|
|
wallet := &MockWallet{}
|
|
|
|
defer wallet.AssertExpectations(t)
|
|
|
|
|
2024-01-17 10:21:09 +01:00
|
|
|
// Create a test sweeper.
|
|
|
|
s := New(&UtxoSweeperConfig{
|
2024-03-21 13:34:43 +01:00
|
|
|
Store: store,
|
|
|
|
Wallet: wallet,
|
2024-01-17 10:21:09 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
// Create a testing outpoint.
|
|
|
|
op := wire.OutPoint{Hash: chainhash.Hash{1}}
|
|
|
|
|
|
|
|
// Create a mock input.
|
|
|
|
inp := &input.MockInput{}
|
|
|
|
defer inp.AssertExpectations(t)
|
|
|
|
|
|
|
|
// Construct the initial state for the sweeper.
|
2024-03-17 20:20:37 +01:00
|
|
|
s.inputs = InputsMap{
|
|
|
|
op: &SweeperInput{Input: inp, state: PendingPublish},
|
2024-01-17 10:21:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a testing tx that spends the input.
|
|
|
|
tx := &wire.MsgTx{
|
|
|
|
LockTime: 1,
|
|
|
|
TxIn: []*wire.TxIn{
|
|
|
|
{PreviousOutPoint: op},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a replacement tx.
|
|
|
|
replacementTx := &wire.MsgTx{
|
|
|
|
LockTime: 2,
|
|
|
|
TxIn: []*wire.TxIn{
|
|
|
|
{PreviousOutPoint: op},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a testing bump result.
|
|
|
|
br := &BumpResult{
|
|
|
|
Tx: replacementTx,
|
|
|
|
ReplacedTx: tx,
|
|
|
|
Event: TxReplaced,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mock the store to return an error.
|
|
|
|
dummyErr := errors.New("dummy error")
|
|
|
|
store.On("GetTx", tx.TxHash()).Return(nil, dummyErr).Once()
|
|
|
|
|
|
|
|
// Call the method under test and assert the error is returned.
|
|
|
|
err := s.handleBumpEventTxReplaced(br)
|
|
|
|
require.ErrorIs(t, err, dummyErr)
|
|
|
|
|
|
|
|
// Mock the store to return the old tx record.
|
|
|
|
store.On("GetTx", tx.TxHash()).Return(&TxRecord{
|
|
|
|
Txid: tx.TxHash(),
|
|
|
|
}, nil).Once()
|
|
|
|
|
2024-03-21 13:34:43 +01:00
|
|
|
// We expect to cancel rebroadcasting the replaced tx.
|
|
|
|
wallet.On("CancelRebroadcast", tx.TxHash()).Once()
|
|
|
|
|
2024-01-17 10:21:09 +01:00
|
|
|
// Mock an error returned when deleting the old tx record.
|
|
|
|
store.On("DeleteTx", tx.TxHash()).Return(dummyErr).Once()
|
|
|
|
|
|
|
|
// Call the method under test and assert the error is returned.
|
|
|
|
err = s.handleBumpEventTxReplaced(br)
|
|
|
|
require.ErrorIs(t, err, dummyErr)
|
|
|
|
|
|
|
|
// Mock the store to return the old tx record and delete it without
|
|
|
|
// error.
|
|
|
|
store.On("GetTx", tx.TxHash()).Return(&TxRecord{
|
|
|
|
Txid: tx.TxHash(),
|
|
|
|
}, nil).Once()
|
|
|
|
store.On("DeleteTx", tx.TxHash()).Return(nil).Once()
|
|
|
|
|
|
|
|
// Mock the store to save the new tx record.
|
|
|
|
store.On("StoreTx", &TxRecord{
|
|
|
|
Txid: replacementTx.TxHash(),
|
|
|
|
Published: true,
|
|
|
|
}).Return(nil).Once()
|
|
|
|
|
2024-03-21 13:34:43 +01:00
|
|
|
// We expect to cancel rebroadcasting the replaced tx.
|
|
|
|
wallet.On("CancelRebroadcast", tx.TxHash()).Once()
|
|
|
|
|
2024-01-17 10:21:09 +01:00
|
|
|
// Call the method under test.
|
|
|
|
err = s.handleBumpEventTxReplaced(br)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Assert the state of the input is updated.
|
2024-03-17 20:00:58 +01:00
|
|
|
require.Equal(t, Published, s.inputs[op].state)
|
2024-01-17 10:21:09 +01:00
|
|
|
}
|
2024-01-30 20:25:58 +01:00
|
|
|
|
2024-01-17 10:21:09 +01:00
|
|
|
// TestHandleBumpEventTxPublished checks that the sweeper correctly handles the
|
|
|
|
// case where the bump event tx is published.
|
|
|
|
func TestHandleBumpEventTxPublished(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Create a mock store.
|
|
|
|
store := &MockSweeperStore{}
|
|
|
|
defer store.AssertExpectations(t)
|
|
|
|
|
|
|
|
// Create a test sweeper.
|
|
|
|
s := New(&UtxoSweeperConfig{
|
|
|
|
Store: store,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Create a testing outpoint.
|
|
|
|
op := wire.OutPoint{Hash: chainhash.Hash{1}}
|
|
|
|
|
|
|
|
// Create a mock input.
|
|
|
|
inp := &input.MockInput{}
|
|
|
|
defer inp.AssertExpectations(t)
|
|
|
|
|
|
|
|
// Construct the initial state for the sweeper.
|
2024-03-17 20:20:37 +01:00
|
|
|
s.inputs = InputsMap{
|
|
|
|
op: &SweeperInput{Input: inp, state: PendingPublish},
|
2024-01-17 10:21:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a testing tx that spends the input.
|
|
|
|
tx := &wire.MsgTx{
|
|
|
|
LockTime: 1,
|
|
|
|
TxIn: []*wire.TxIn{
|
|
|
|
{PreviousOutPoint: op},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a testing bump result.
|
|
|
|
br := &BumpResult{
|
|
|
|
Tx: tx,
|
|
|
|
Event: TxPublished,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mock the store to save the new tx record.
|
|
|
|
store.On("StoreTx", &TxRecord{
|
|
|
|
Txid: tx.TxHash(),
|
|
|
|
Published: true,
|
|
|
|
}).Return(nil).Once()
|
|
|
|
|
|
|
|
// Call the method under test.
|
|
|
|
err := s.handleBumpEventTxPublished(br)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Assert the state of the input is updated.
|
2024-03-17 20:00:58 +01:00
|
|
|
require.Equal(t, Published, s.inputs[op].state)
|
2024-01-17 10:21:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestMonitorFeeBumpResult checks that the fee bump monitor loop correctly
|
|
|
|
// exits when the sweeper is stopped, the tx is confirmed or failed.
|
|
|
|
func TestMonitorFeeBumpResult(t *testing.T) {
|
|
|
|
// Create a mock store.
|
|
|
|
store := &MockSweeperStore{}
|
|
|
|
defer store.AssertExpectations(t)
|
|
|
|
|
2024-03-21 13:34:43 +01:00
|
|
|
// Create a mock wallet.
|
|
|
|
wallet := &MockWallet{}
|
|
|
|
defer wallet.AssertExpectations(t)
|
|
|
|
|
2024-01-17 10:21:09 +01:00
|
|
|
// Create a test sweeper.
|
|
|
|
s := New(&UtxoSweeperConfig{
|
2024-03-21 13:34:43 +01:00
|
|
|
Store: store,
|
|
|
|
Wallet: wallet,
|
2024-01-17 10:21:09 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
// Create a testing outpoint.
|
|
|
|
op := wire.OutPoint{Hash: chainhash.Hash{1}}
|
|
|
|
|
|
|
|
// Create a mock input.
|
|
|
|
inp := &input.MockInput{}
|
|
|
|
defer inp.AssertExpectations(t)
|
|
|
|
|
|
|
|
// Construct the initial state for the sweeper.
|
2024-03-17 20:20:37 +01:00
|
|
|
s.inputs = InputsMap{
|
|
|
|
op: &SweeperInput{Input: inp, state: PendingPublish},
|
2024-01-17 10:21:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a testing tx that spends the input.
|
|
|
|
tx := &wire.MsgTx{
|
|
|
|
LockTime: 1,
|
|
|
|
TxIn: []*wire.TxIn{
|
|
|
|
{PreviousOutPoint: op},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
setupResultChan func() <-chan *BumpResult
|
|
|
|
shouldExit bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// When a tx confirmed event is received, we expect to
|
|
|
|
// exit the monitor loop.
|
|
|
|
name: "tx confirmed",
|
|
|
|
// We send a result with TxConfirmed event to the
|
|
|
|
// result channel.
|
|
|
|
setupResultChan: func() <-chan *BumpResult {
|
|
|
|
// Create a result chan.
|
|
|
|
resultChan := make(chan *BumpResult, 1)
|
|
|
|
resultChan <- &BumpResult{
|
|
|
|
Tx: tx,
|
|
|
|
Event: TxConfirmed,
|
|
|
|
Fee: 10000,
|
|
|
|
FeeRate: 100,
|
|
|
|
}
|
|
|
|
|
2024-03-21 13:34:43 +01:00
|
|
|
// We expect to cancel rebroadcasting the tx
|
|
|
|
// once confirmed.
|
|
|
|
wallet.On("CancelRebroadcast",
|
|
|
|
tx.TxHash()).Once()
|
|
|
|
|
2024-01-17 10:21:09 +01:00
|
|
|
return resultChan
|
|
|
|
},
|
|
|
|
shouldExit: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// When a tx failed event is received, we expect to
|
|
|
|
// exit the monitor loop.
|
|
|
|
name: "tx failed",
|
|
|
|
// We send a result with TxConfirmed event to the
|
|
|
|
// result channel.
|
|
|
|
setupResultChan: func() <-chan *BumpResult {
|
|
|
|
// Create a result chan.
|
|
|
|
resultChan := make(chan *BumpResult, 1)
|
|
|
|
resultChan <- &BumpResult{
|
|
|
|
Tx: tx,
|
|
|
|
Event: TxFailed,
|
|
|
|
Err: errDummy,
|
|
|
|
}
|
|
|
|
|
2024-03-21 13:34:43 +01:00
|
|
|
// We expect to cancel rebroadcasting the tx
|
|
|
|
// once failed.
|
|
|
|
wallet.On("CancelRebroadcast",
|
|
|
|
tx.TxHash()).Once()
|
|
|
|
|
2024-01-17 10:21:09 +01:00
|
|
|
return resultChan
|
|
|
|
},
|
|
|
|
shouldExit: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// When processing non-confirmed events, the monitor
|
|
|
|
// should not exit.
|
|
|
|
name: "no exit on normal event",
|
|
|
|
// We send a result with TxPublished and mock the
|
|
|
|
// method `StoreTx` to return nil.
|
|
|
|
setupResultChan: func() <-chan *BumpResult {
|
|
|
|
// Create a result chan.
|
|
|
|
resultChan := make(chan *BumpResult, 1)
|
|
|
|
resultChan <- &BumpResult{
|
|
|
|
Tx: tx,
|
|
|
|
Event: TxPublished,
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultChan
|
|
|
|
},
|
|
|
|
shouldExit: false,
|
|
|
|
}, {
|
|
|
|
// When the sweeper is shutting down, the monitor loop
|
|
|
|
// should exit.
|
|
|
|
name: "exit on sweeper shutdown",
|
|
|
|
// We don't send anything but quit the sweeper.
|
|
|
|
setupResultChan: func() <-chan *BumpResult {
|
|
|
|
close(s.quit)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
shouldExit: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
// Setup the testing result channel.
|
|
|
|
resultChan := tc.setupResultChan()
|
|
|
|
|
|
|
|
// Create a done chan that's used to signal the monitor
|
|
|
|
// has exited.
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
|
|
|
s.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
s.monitorFeeBumpResult(resultChan)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// The monitor is expected to exit, we check it's done
|
|
|
|
// in one second or fail.
|
|
|
|
if tc.shouldExit {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
require.Fail(t, "monitor not exited")
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// The monitor should not exit, check it doesn't close
|
|
|
|
// the `done` channel within one second.
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
require.Fail(t, "monitor exited")
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2024-01-30 20:25:58 +01:00
|
|
|
}
|