chanfunding: make coin selection generic

Before this commit the coin selection logic in the chanfunding package
would always assume that there is a P2WSH funding output and potentially
a P2TR change output. But because we want to re-use the coin selection
for things other than just channel funding, we make the logic more
generic by allowing us to specify both the existing weight of the
transaction (the already known, static parts of the TX) as well as the
type of the potential change output we would use.
This commit is contained in:
Oliver Gugger committed 2024-02-23 08:58:09 +01:00
1 parent 9bdddbcc56
commit 4c82fb6cbb
3 files changed
+79 -19

No files matched your search

+14
View File
@@ -308,6 +308,17 @@ func (w *WalletAssembler) ProvisionChannel(r *Request) (Intent, error) {
}
}
// The coin selection algorithm requires to know what
// inputs/outputs are already present in the funding
// transaction and what a change output would look like. Since
// a channel funding is always either a P2WSH or P2TR output,
// we can use just P2WSH here (both of these output types have
// the same length). And we currently don't support specifying a
// change output type, so we always use P2TR.
var fundingOutputWeight input.TxWeightEstimator
fundingOutputWeight.AddP2WSHOutput()
changeType := P2TRChangeAddress
var (
coins []wallet.Coin
selectedCoins []wallet.Coin
@@ -393,6 +404,7 @@ func (w *WalletAssembler) ProvisionChannel(r *Request) (Intent, error) {
r.FeeRate, r.MinFundAmt, r.FundUpToMaxAmt,
reserve, w.cfg.DustLimit, coins,
w.cfg.CoinSelectionStrategy,
fundingOutputWeight, changeType,
)
if err != nil {
return err
@@ -427,6 +439,7 @@ func (w *WalletAssembler) ProvisionChannel(r *Request) (Intent, error) {
err = CoinSelectSubtractFees(
r.FeeRate, r.LocalAmt, dustLimit, coins,
w.cfg.CoinSelectionStrategy,
fundingOutputWeight, changeType,
)
if err != nil {
return err
@@ -440,6 +453,7 @@ func (w *WalletAssembler) ProvisionChannel(r *Request) (Intent, error) {
selectedCoins, changeAmt, err = CoinSelect(
r.FeeRate, r.LocalAmt, dustLimit, coins,
w.cfg.CoinSelectionStrategy,
fundingOutputWeight, changeType,
)
if err != nil {
return err