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,
ChainIO: walletController,
NetParams: *walletConfig.NetParams,
CoinSelectionStrategy: walletConfig.CoinSelectionStrategy,
}
// The broadcast is already always active for neutrino nodes, so we
@ -808,6 +809,7 @@ func (d *RPCSignerWalletImpl) BuildChainControl(
SecretKeyRing: rpcKeyRing,
ChainIO: walletController,
NetParams: *walletConfig.NetParams,
CoinSelectionStrategy: walletConfig.CoinSelectionStrategy,
}
// 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/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcwallet/wallet"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/chainreg"
acpt "github.com/lightningnetwork/lnd/chanacceptor"
@ -368,6 +369,7 @@ func createTestWallet(cdb *channeldb.ChannelStateDB, netParams *chaincfg.Params,
ChainIO: bio,
FeeEstimator: estimator,
NetParams: *netParams,
CoinSelectionStrategy: wallet.CoinSelectionLargest,
})
if err != nil {
return nil, err

View File

@ -9,6 +9,7 @@ import (
"github.com/btcsuite/btcd/btcutil/txsort"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcwallet/wallet"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain"
)
@ -207,6 +208,10 @@ type WalletConfig struct {
// CoinSource is what the WalletAssembler uses to list/locate coins.
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
// access to the current set of coins returned by the CoinSource.
CoinSelectLocker CoinSelectionLocker

View File

@ -2,6 +2,7 @@ package lnwallet
import (
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcwallet/wallet"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/input"
@ -57,4 +58,8 @@ type Config struct {
// passively rebroadcast transactions in the background until they're
// detected as being confirmed.
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/wire"
"github.com/btcsuite/btcwallet/chain"
"github.com/btcsuite/btcwallet/wallet"
"github.com/btcsuite/btcwallet/walletdb"
_ "github.com/btcsuite/btcwallet/walletdb/bdb"
"github.com/davecgh/go-spew/spew"
@ -357,6 +358,7 @@ func createTestWallet(tempTestDir string, miningNode *rpctest.Harness,
ChainIO: bio,
FeeEstimator: chainfee.NewStaticEstimator(2500, 0),
NetParams: *netParams,
CoinSelectionStrategy: wallet.CoinSelectionLargest,
}
wallet, err := lnwallet.NewLightningWallet(cfg)
@ -2984,18 +2986,22 @@ func testSingleFunderExternalFundingTx(miner *rpctest.Harness,
// Simulating external funding negotiation, we'll now create the
// funding transaction for both parties. Utilizing existing tools,
// 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),
CoinSelectLocker: alice,
CoinLocker: alice,
Signer: alice.Cfg.Signer,
DustLimit: 600,
})
CoinSelectionStrategy: wallet.CoinSelectionLargest,
},
)
// With the chan funder created, we'll now provision a funding intent,
// bind the keys we obtained above, and finally obtain our funding
// transaction and outpoint.
fundingIntent, err := aliceChanFunder.ProvisionChannel(&chanfunding.Request{
fundingIntent, err := aliceChanFunder.ProvisionChannel(
&chanfunding.Request{
LocalAmt: btcutil.Amount(chanAmt),
MinConfs: 1,
FeeRate: 253,
@ -3005,7 +3011,8 @@ func testSingleFunderExternalFundingTx(miner *rpctest.Harness,
lnwallet.DefaultAccountName,
)
},
})
},
)
require.NoError(t, err, "unable to perform coin selection")
// 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,
CoinLocker: l,
Signer: l.Cfg.Signer,
DustLimit: DustLimitForSize(input.P2WSHSize),
DustLimit: DustLimitForSize(
input.P2WSHSize,
),
CoinSelectionStrategy: l.Cfg.CoinSelectionStrategy,
}
req.ChanFunder = chanfunding.NewWalletAssembler(cfg)
} else {