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 committed 2024-02-23 08:58:07 +01:00
1 parent 935e550da6
commit cbc11dac8f
6 files changed
+74 -50

No files matched your search

+18 -16
View File
@@ -686,14 +686,15 @@ func (d *DefaultWalletImpl) BuildChainControl(
// Create, and start the lnwallet, which handles the core payment // Create, and start the lnwallet, which handles the core payment
// channel logic, and exposes control via proxy state machines. // channel logic, and exposes control via proxy state machines.
lnWalletConfig := lnwallet.Config{ lnWalletConfig := lnwallet.Config{
Database: partialChainControl.Cfg.ChanStateDB, Database: partialChainControl.Cfg.ChanStateDB,
Notifier: partialChainControl.ChainNotifier, Notifier: partialChainControl.ChainNotifier,
WalletController: walletController, WalletController: walletController,
Signer: walletController, Signer: walletController,
FeeEstimator: partialChainControl.FeeEstimator, FeeEstimator: partialChainControl.FeeEstimator,
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
@@ -800,14 +801,15 @@ func (d *RPCSignerWalletImpl) BuildChainControl(
// Create, and start the lnwallet, which handles the core payment // Create, and start the lnwallet, which handles the core payment
// channel logic, and exposes control via proxy state machines. // channel logic, and exposes control via proxy state machines.
lnWalletConfig := lnwallet.Config{ lnWalletConfig := lnwallet.Config{
Database: partialChainControl.Cfg.ChanStateDB, Database: partialChainControl.Cfg.ChanStateDB,
Notifier: partialChainControl.ChainNotifier, Notifier: partialChainControl.ChainNotifier,
WalletController: rpcKeyRing, WalletController: rpcKeyRing,
Signer: rpcKeyRing, Signer: rpcKeyRing,
FeeEstimator: partialChainControl.FeeEstimator, FeeEstimator: partialChainControl.FeeEstimator,
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
+10 -8
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"
@@ -360,14 +361,15 @@ func createTestWallet(cdb *channeldb.ChannelStateDB, netParams *chaincfg.Params,
estimator chainfee.Estimator) (*lnwallet.LightningWallet, error) { estimator chainfee.Estimator) (*lnwallet.LightningWallet, error) {
wallet, err := lnwallet.NewLightningWallet(lnwallet.Config{ wallet, err := lnwallet.NewLightningWallet(lnwallet.Config{
Database: cdb, Database: cdb,
Notifier: notifier, Notifier: notifier,
SecretKeyRing: keyRing, SecretKeyRing: keyRing,
WalletController: wc, WalletController: wc,
Signer: signer, Signer: signer,
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
+5
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
+5
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
} }
+32 -25
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"
@@ -349,14 +350,15 @@ func createTestWallet(tempTestDir string, miningNode *rpctest.Harness,
} }
cfg := lnwallet.Config{ cfg := lnwallet.Config{
Database: fullDB.ChannelStateDB(), Database: fullDB.ChannelStateDB(),
Notifier: notifier, Notifier: notifier,
SecretKeyRing: keyRing, SecretKeyRing: keyRing,
WalletController: wc, WalletController: wc,
Signer: signer, Signer: signer,
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,28 +2986,33 @@ 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(
CoinSource: lnwallet.NewCoinSource(alice), chanfunding.WalletConfig{
CoinSelectLocker: alice, CoinSource: lnwallet.NewCoinSource(alice),
CoinLocker: alice, CoinSelectLocker: alice,
Signer: alice.Cfg.Signer, CoinLocker: alice,
DustLimit: 600, Signer: alice.Cfg.Signer,
}) 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(
LocalAmt: btcutil.Amount(chanAmt), &chanfunding.Request{
MinConfs: 1, LocalAmt: btcutil.Amount(chanAmt),
FeeRate: 253, MinConfs: 1,
ChangeAddr: func() (btcutil.Address, error) { FeeRate: 253,
return alice.NewAddress( ChangeAddr: func() (btcutil.Address, error) {
lnwallet.WitnessPubKey, true, return alice.NewAddress(
lnwallet.DefaultAccountName, lnwallet.WitnessPubKey, true,
) 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
+4 -1
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 {