Commit Graph

81 Commits

Author SHA1 Message Date
yyforyongyu
658ba445ea
sweep: delay sweeping inputs with future locktimes
This commit fixes an edge case that the sweeper's best known block
height is behind arbitrator's, which may cause an issue when creating
sweeping tx, as we may end up using an old block height from
arbitrator's view.
2024-04-19 21:33:31 +08:00
yyforyongyu
28df2d7327
lnrpc+sweep: make sure public interface takes public types as params
This commit exports and renames the following variable names:
- `PendingInput` is now `PendingInputResponse` as it's responding to a
  request.
- `pendingInput` is now renamed and exported as `SweeperInput`.
- `pendingInputs` is now renamed and exported as `InputsMap`.

This commit is first made from running:
```
gofmt -d -w -r 'PendingInput -> PendingInputResponse' .
gofmt -d -w -r 'pendingInput -> SweeperInput' .
gofmt -d -w -r 'pendingInputs -> InputsMap' .
```
And followed by some docs and variable names fixes.
2024-04-19 21:33:30 +08:00
yyforyongyu
9e7d4b7e0b
sweep: rename pendingInputs to inputs on sweeper
There's no need use the prefix `pending` as the inputs in the sweeper
can only be pending, so it's renamed, also to avoid the confusion with
the type `pendingInputs`.
2024-04-19 21:33:30 +08:00
yyforyongyu
0063770cb7
sweep: remove the prefix used in SweepState types
Don't prefix with `State` we already have information to determine what
the type is. Generated this commit using,
```
gofmt -d -w -r 'StateInit -> Init' .
gofmt -d -w -r 'StatePendingPublish -> PendingPublish' .
gofmt -d -w -r 'StatePublished -> Published' .
gofmt -d -w -r 'StatePublishFailed -> PublishFailed' .
gofmt -d -w -r 'StateSwept -> Swept' .
gofmt -d -w -r 'StateExcluded -> Excluded' .
gofmt -d -w -r 'StateFailed -> Failed' .
```

and some string matching to fix the docs.
2024-04-19 21:33:30 +08:00
yyforyongyu
6d6c544414
sweep: remove RBF related tests
As there will be dedicated new tests for them.
2024-04-19 21:33:28 +08:00
yyforyongyu
21aff324a3
sweeper: fix existing sweeper tests 2024-04-19 21:33:28 +08:00
yyforyongyu
1187b868ad
sweep: introduce Bumper interface to handle RBF
This commit adds a new interface, `Bumper`, to handle RBF for a given
input set. It's responsible for creating the sweeping tx using the input
set, and monitors its confirmation status to decide whether a RBF should
be attempted or not.

We leave implementation details to future commits, and focus on mounting
this `Bumper` interface to our sweeper in this commit.
2024-04-19 21:33:27 +08:00
yyforyongyu
bd5eec8e1f
sweep: refactor markInputsPendingPublish to take InputSet
This commit changes `markInputsPendingPublish` to take `InputSet` only.
This is needed for the following commits as we won't be able to know the
tx being created beforehand, yet we still want to make sure these inputs
won't be grouped to another input set as it complicates our RBF process.
2024-04-19 21:33:27 +08:00
yyforyongyu
6202c59cb3
sweep: change markInputsPublishFailed to take outpoints
This way it's easier to pass values to this method in various callsites.
2024-04-19 21:33:27 +08:00
yyforyongyu
d0a8f27d84
sweep: change MaxInputsPerTx from int to uint32 2024-04-19 21:33:26 +08:00
yyforyongyu
465332f409
multi: deprecate batchwindowduration config option 2024-04-19 21:33:26 +08:00
yyforyongyu
c03509397f
sweep: add mocks and patch unit test for sweepPendingInputs 2024-04-19 21:33:26 +08:00
yyforyongyu
b536e9bd3f
sweep: deepen the interface Aggregator
This commit makes the `ClusterInputs` directly returning the `InputSet`
so the sweeper doesn't know about the existence of `Cluster` interface.
This way we can have a deeper interface as the sweeper only needs to
interact with `Aggregator` only to get the final input sets, leaving the
implementation details being managed by `SimpleAggregator` and future
aggregators.
2024-04-19 21:33:25 +08:00
yyforyongyu
9d5ddf29f3
sweep: add new interface Cluster to manage grouping inputs
This commit adds a new interface `Cluster` to manage cluster-level
inputs grouping. This new interface replaces the `inputCluster` and will
be futher refactored so the sweeper can use a much smaller coin
selection lock.
2024-04-19 21:33:25 +08:00
yyforyongyu
a7e9c08baf
sweep: make sweeper block-driven instead of time-driven
This commit changes the source that drives the state changes in the
sweeper. Previously we used a ticker with default interval of 30s to
trigger sweepings periodically. The assumption is, within this 30s we'd
batch multiple inputs into one transaction to maximize profits. However,
the efficacy of this batch is questionable.

At a high level, we can put our inputs into two categories - one that's
forced, and one that's not. For forced inputs, we should sweep them
immediately as the force flag indicates they are very urgent, eg,
CPFPing the force closing tx. For non-forced inputs, such as anchors
or HTLCs with CLTV that's far away, we can wait to sweep them till a new
block comes in and triggers the sweeping process.

Eventually, all inputs will be deadline-aware, and the sweeper will
consult our fee bumper about the most economical fee rate to be used for
a given deadline. Since the deadlines here are blockstamp, it's also
easier to manage them if the sweeper is also using blockstamp instead of
timestamp.
2024-04-19 21:33:24 +08:00
yyforyongyu
df4e51e2e0
sweep: refactor attachAvailableRBFInfo to decideStateAndRBFInfo
Thus this method `decideStateAndRBFInfo` won't touch the state changes
of a given input.
2024-04-19 21:33:24 +08:00
yyforyongyu
5fce91caf9
chainntnfs+sweep: add LookupInputMempoolSpend and use it in the
sweeper

This commit implements a new method, `LookupInputMempoolSpend` to do
lookups in the mempool. This method is useful in the case when we only
want to know whether an input is already been spent in the mempool by
the time we call.
2024-04-19 21:33:24 +08:00
yyforyongyu
b5e4384e24
lnd+sweep: remove unused NextAttemptDeltaFunc 2024-04-19 21:33:23 +08:00
yyforyongyu
fd922942a7
sweep: patch unit tests for markInputsSwept and markInputsPendingPublish
Now that the refactor is done, we start patching unit tests for these
two methods. Minor changes are also made based on the feedback from the
tests.
2024-04-19 21:33:23 +08:00
yyforyongyu
34b6a3d718
sweep: add method markInputFailed 2024-04-19 21:33:23 +08:00
yyforyongyu
a8f5a09dea
sweep: don't give up an input based on number of attempts
This commit removes the logic where we remove an input when it's been
published more than 10 times. This is needed as in our future fee
bumper, we might start with a low fee and rebroadcast the same input for
hundred of blocks.
2024-04-19 21:33:23 +08:00
yyforyongyu
8b876be3b6
sweep: add fee info for published inputs
This commit attaches RBFInfo to an input before it's been published or
it's already been published.
2024-04-19 21:33:22 +08:00
yyforyongyu
a263d68fb9
sweep: delete pending inputs based on their states
This commit uniforms and put the deletion of pending inputs in a single
point.
2024-04-19 21:33:22 +08:00
yyforyongyu
47478718d4
multi: query mempool spend when a new input is received
This commit changes how a new input sweep request is handled - now we
will query the mempool and see if it's already been spent. If so, we'll
update its state as we may need to RBF this input.
2024-04-19 21:33:22 +08:00
yyforyongyu
6a2e3fb203
sweep: make pending input stateful
This commit starts tracking the state of a pending input so it's easier
to control the sweeping flow and provide RBF awareness in the future.
2024-04-19 21:33:22 +08:00
yyforyongyu
1870caf39c
sweep+lnd: introduce UtxoAggregator to handle clustering inputs
This commit refactors the grouping logic into a new interface
`UtxoAggregator`, which makes it easier to write tests and opens
possibility for future customized clustering strategies.

The old clustering logic is kept as and moved into `SimpleAggregator`.
2024-04-19 21:33:21 +08:00
yyforyongyu
3bcac318eb
sweep+lnrpc: add new interface FeePreference
This commit adds a new interface `FeePreference` which makes it easier
to write unit tests and allows more customized implementation in
following commits.
2024-04-19 21:33:21 +08:00
yyforyongyu
530eed92a0
multi: rename FeePreference to FeeEstimateInfo
Results from running,
```
gofmt -d -w -r 'FeePreference -> FeeEstimateInfo' .
```
2024-04-19 21:33:21 +08:00
yyforyongyu
18b06b7303
sweep: replace feeRateForPreference with Estimate
This commit refactors the sweeper so the method `feeRateForPreference`
is now moved to `FeePreference`, which makes our following refactor
easier to handle.
2024-04-19 21:33:20 +08:00
yyforyongyu
84a6fdcda3
sweep+contractcourt: track best height in UtxoSweeper
Thus we can use shorter method signatures. In doing so we also remove an
old TODO in one use case of `CreateSweepTx`.
2024-04-19 21:33:20 +08:00
yyforyongyu
f13a3a8053
sweep: use testify/mock for MockSweeperStore 2024-04-19 21:33:20 +08:00
Thabokani
55072bfd5e
sweep+lntest: fix typos 2024-01-02 19:23:30 +08:00
yyforyongyu
dba4c8e5ad
trivial: fix typo and nits 2023-10-23 13:01:52 +08:00
yyforyongyu
92837621ec
sweep: remove possible RBF when creating sweeping tx for new inputs
This commit changes how we create the input sets which are used to
construct the sweeping transactions. Assume the sweeper has two inputs,
one is new and one is retried, we'd end up having two transactions,
- tx1: which spends both the new and old inputs.
- tx2: which spends the new inputs only.
When publishing these txes, depending on which one gets into the mempool
first, the other one will be viewed as an RBF for the first one since
they both spending the same input(the new input).

This is now fixed by only attempt to publish the second tx when there
isn't a first tx - when there is a tx1, it means the new inputs are
already used in this tx along with retried inputs, hence there's no need
to publish tx2 which spends the new inputs only.
2023-10-23 10:48:51 +08:00
yyforyongyu
7de4186766
sweep: simplify polling logic in sweeper
This commit attempts to make the polling logic in sweeper more linear.
Previously, the sweep's timer is reset/restarted in multiple places,
such as when a new input comes in, or a new block comes in, or a
previous input being spent, making it difficult to follow. We now remove
the old timer and replaces it with a simple polling logic - we will
schedule sweeps every 5s(default), and if there's no input to be swept,
we'd skip, just like the previous `scheduleSweep` does.
It's also worthy noting that, although `scheduleSweep` triggers the
timer to tick, by the time we do the actual sweep in `sweepCluster`,
conditions may have changed. This is now also fixed because we only have
one place to create the clusters and sweeps.
2023-10-23 10:24:58 +08:00
yyforyongyu
4ba09098d1
sweep+lnd: move ticker creation into sweeper 2023-10-23 10:24:58 +08:00
yyforyongyu
551102a0f3
sweep: fix make lint 2023-10-13 17:00:53 +08:00
yyforyongyu
34e0c7b5e0
input+sweep: make sure input with no fee rate is not added to cluster
This commit makes sure an input is only added to the cluster when it has
successfully estimated its fee rate. Previously, when an error is
returned from `feeRateForPreference`, we'd still add this input to the
cluster, resulting a **lower** fee rates being used because when
averaging the fee rates, we'd think this input has zero fee rate
specified.

An unit test is patched to make the method `clusterByLockTime` more
robust.
2023-10-13 17:00:53 +08:00
yyforyongyu
497f421009
sweep: patch unit test for feeRateForPreference 2023-10-13 17:00:53 +08:00
yyforyongyu
82053970ef
lnd+sweep: move DetermineFeePerKw into config
This commit makes `DetermineFeePerKw` configurable on sweeper so it's
easier to write unit tests for it.
2023-10-13 14:44:20 +08:00
yyforyongyu
24fa35ec80 multi: make sure CPFP won't exceed max allowed fee rate
This commit updates the `fee()` method in `weightEstimator` to make sure
when doing CPFP we are not exceeding the max allowed fee rate. In order
to use the max fee rate, we need to modify several methods to pass the
configured value to the estimator.
2023-10-06 16:34:47 -07:00
yyforyongyu
258fe7999b sweep+itest: change MaxFeeRate to use SatPerVbyte 2023-10-06 16:34:47 -07:00
ziggie
07502a8fb0
sweep: Remove publishing last-tx logic.
We remove the publishing of the last published sweep tx during the
startup of the sweeper. This republishing can lead to situations
where funds of the default wallet might be locked for neutrino
backend clients.
Moreover all related tests are removed as well.
2023-08-15 10:00:29 +02:00
eugene
6ec2826f6c
sweep: account for all script types in craftSweepTx
With this change, transactions created via craftSweepTx will be
standard. Previously, p2wsh/p2pkh scripts passed in via SendCoins would
be weighted as p2wpkh scripts. With a feerate of 1 sat/vbyte,
transactions returned would be non-standard. Luckily, the critical
sweeper subsystem only used p2wpkh scripts so this only affected
callers from the rpcserver.

Also added is an integration test that fails if SendCoins manages
to generate a non-standard transaction. All script types are now
accounted for in getWeightEstimate, which now errors if an unknown
script type is passed in.
2022-08-08 15:33:00 -04:00
Tommy Volk
9a10c80bcb multi: move many t.Fatalf calls to require.NoError 2022-06-17 04:26:55 +00:00
Oliver Gugger
c73cf03a55
multi: add p2tr keyspend wallet support 2022-03-24 18:02:38 +01:00
Oliver Gugger
f130eddb92
multi: use prev output fetcher where possible 2022-03-24 18:02:37 +01:00
Oliver Gugger
7dfe4018ce
multi: use btcd's btcec/v2 and btcutil modules
This commit was previously split into the following parts to ease
review:
 - 2d746f68: replace imports
 - 4008f0fd: use ecdsa.Signature
 - 849e33d1: remove btcec.S256()
 - b8f6ebbd: use v2 library correctly
 - fa80bca9: bump go modules
2022-03-09 19:02:37 +01:00
Oliver Gugger
895a2e497b
multi: formatting and comment fixes 2022-02-10 11:02:02 +01:00
eugene
fdcd726f9a
multi: replace DefaultDustLimit with script-specific DustLimitForSize
This commit updates call-sites to use the proper dust limits for
various script types. This also updates the default dust limit used
in the funding flow to be 354 satoshis instead of 573 satoshis.
2021-09-29 13:33:10 -04:00