multi: add coin selection strategy to channel funding

With this commit we prepare for the lnwallet channel funding logic to be
aware of the config-level coin selection strategy by adding it to the
wallet config.
This commit is contained in:
Oliver Gugger 2024-02-06 12:25:45 +01:00
parent 935e550da6
commit cbc11dac8f
No known key found for this signature in database
GPG Key ID: 8E4256593F177720
6 changed files with 74 additions and 50 deletions

View File

@ -694,6 +694,7 @@ func (d *DefaultWalletImpl) BuildChainControl(
SecretKeyRing: keyRing, SecretKeyRing: keyRing,
ChainIO: walletController, ChainIO: walletController,
NetParams: *walletConfig.NetParams, NetParams: *walletConfig.NetParams,
CoinSelectionStrategy: walletConfig.CoinSelectionStrategy,
} }
// The broadcast is already always active for neutrino nodes, so we // The broadcast is already always active for neutrino nodes, so we
@ -808,6 +809,7 @@ func (d *RPCSignerWalletImpl) BuildChainControl(
SecretKeyRing: rpcKeyRing, SecretKeyRing: rpcKeyRing,
ChainIO: walletController, ChainIO: walletController,
NetParams: *walletConfig.NetParams, NetParams: *walletConfig.NetParams,
CoinSelectionStrategy: walletConfig.CoinSelectionStrategy,
} }
// We've created the wallet configuration now, so we can finish // We've created the wallet configuration now, so we can finish

View File

@ -20,6 +20,7 @@ import (
"github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcwallet/wallet"
"github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/chainreg" "github.com/lightningnetwork/lnd/chainreg"
acpt "github.com/lightningnetwork/lnd/chanacceptor" acpt "github.com/lightningnetwork/lnd/chanacceptor"
@ -368,6 +369,7 @@ func createTestWallet(cdb *channeldb.ChannelStateDB, netParams *chaincfg.Params,
ChainIO: bio, ChainIO: bio,
FeeEstimator: estimator, FeeEstimator: estimator,
NetParams: *netParams, NetParams: *netParams,
CoinSelectionStrategy: wallet.CoinSelectionLargest,
}) })
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -9,6 +9,7 @@ import (
"github.com/btcsuite/btcd/btcutil/txsort" "github.com/btcsuite/btcd/btcutil/txsort"
"github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcwallet/wallet"
"github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/keychain"
) )
@ -207,6 +208,10 @@ type WalletConfig struct {
// CoinSource is what the WalletAssembler uses to list/locate coins. // CoinSource is what the WalletAssembler uses to list/locate coins.
CoinSource CoinSource CoinSource CoinSource
// CoinSelectionStrategy is the strategy that is used for selecting
// coins when funding a transaction.
CoinSelectionStrategy wallet.CoinSelectionStrategy
// CoinSelectionLocker allows the WalletAssembler to gain exclusive // CoinSelectionLocker allows the WalletAssembler to gain exclusive
// access to the current set of coins returned by the CoinSource. // access to the current set of coins returned by the CoinSource.
CoinSelectLocker CoinSelectionLocker CoinSelectLocker CoinSelectionLocker

View File

@ -2,6 +2,7 @@ package lnwallet
import ( import (
"github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcwallet/wallet"
"github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/input"
@ -57,4 +58,8 @@ type Config struct {
// passively rebroadcast transactions in the background until they're // passively rebroadcast transactions in the background until they're
// detected as being confirmed. // detected as being confirmed.
Rebroadcaster Rebroadcaster Rebroadcaster Rebroadcaster
// CoinSelectionStrategy is the strategy that is used for selecting
// coins when funding a transaction.
CoinSelectionStrategy wallet.CoinSelectionStrategy
} }

View File

@ -27,6 +27,7 @@ import (
"github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcwallet/chain" "github.com/btcsuite/btcwallet/chain"
"github.com/btcsuite/btcwallet/wallet"
"github.com/btcsuite/btcwallet/walletdb" "github.com/btcsuite/btcwallet/walletdb"
_ "github.com/btcsuite/btcwallet/walletdb/bdb" _ "github.com/btcsuite/btcwallet/walletdb/bdb"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
@ -357,6 +358,7 @@ func createTestWallet(tempTestDir string, miningNode *rpctest.Harness,
ChainIO: bio, ChainIO: bio,
FeeEstimator: chainfee.NewStaticEstimator(2500, 0), FeeEstimator: chainfee.NewStaticEstimator(2500, 0),
NetParams: *netParams, NetParams: *netParams,
CoinSelectionStrategy: wallet.CoinSelectionLargest,
} }
wallet, err := lnwallet.NewLightningWallet(cfg) wallet, err := lnwallet.NewLightningWallet(cfg)
@ -2984,18 +2986,22 @@ func testSingleFunderExternalFundingTx(miner *rpctest.Harness,
// Simulating external funding negotiation, we'll now create the // Simulating external funding negotiation, we'll now create the
// funding transaction for both parties. Utilizing existing tools, // funding transaction for both parties. Utilizing existing tools,
// we'll create a new chanfunding.Assembler hacked by Alice's wallet. // we'll create a new chanfunding.Assembler hacked by Alice's wallet.
aliceChanFunder := chanfunding.NewWalletAssembler(chanfunding.WalletConfig{ aliceChanFunder := chanfunding.NewWalletAssembler(
chanfunding.WalletConfig{
CoinSource: lnwallet.NewCoinSource(alice), CoinSource: lnwallet.NewCoinSource(alice),
CoinSelectLocker: alice, CoinSelectLocker: alice,
CoinLocker: alice, CoinLocker: alice,
Signer: alice.Cfg.Signer, Signer: alice.Cfg.Signer,
DustLimit: 600, DustLimit: 600,
}) CoinSelectionStrategy: wallet.CoinSelectionLargest,
},
)
// With the chan funder created, we'll now provision a funding intent, // With the chan funder created, we'll now provision a funding intent,
// bind the keys we obtained above, and finally obtain our funding // bind the keys we obtained above, and finally obtain our funding
// transaction and outpoint. // transaction and outpoint.
fundingIntent, err := aliceChanFunder.ProvisionChannel(&chanfunding.Request{ fundingIntent, err := aliceChanFunder.ProvisionChannel(
&chanfunding.Request{
LocalAmt: btcutil.Amount(chanAmt), LocalAmt: btcutil.Amount(chanAmt),
MinConfs: 1, MinConfs: 1,
FeeRate: 253, FeeRate: 253,
@ -3005,7 +3011,8 @@ func testSingleFunderExternalFundingTx(miner *rpctest.Harness,
lnwallet.DefaultAccountName, lnwallet.DefaultAccountName,
) )
}, },
}) },
)
require.NoError(t, err, "unable to perform coin selection") require.NoError(t, err, "unable to perform coin selection")
// With our intent created, we'll instruct it to finalize the funding // With our intent created, we'll instruct it to finalize the funding

View File

@ -852,7 +852,10 @@ func (l *LightningWallet) handleFundingReserveRequest(req *InitFundingReserveMsg
CoinSelectLocker: l, CoinSelectLocker: l,
CoinLocker: l, CoinLocker: l,
Signer: l.Cfg.Signer, Signer: l.Cfg.Signer,
DustLimit: DustLimitForSize(input.P2WSHSize), DustLimit: DustLimitForSize(
input.P2WSHSize,
),
CoinSelectionStrategy: l.Cfg.CoinSelectionStrategy,
} }
req.ChanFunder = chanfunding.NewWalletAssembler(cfg) req.ChanFunder = chanfunding.NewWalletAssembler(cfg)
} else { } else {