From dc40662770110fed28549155b5c67151936c9c39 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 7 Jun 2017 17:01:17 -0700 Subject: [PATCH] lnwallet/btcwallet: properly pass in FeeEstimator to btcwallet MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit fixes a bug wherein the wallet would use the default relay fee to craft transactions. On testnet, this might be insufficient or be rejected all together in a mainnet setting. Therefore, we now pass in the FeeEstimator interface and ensure that it’s consulted in order to set the relay fee the wallet will use to craft transactions. Note that this is a hold over until we have true dynamic fee calculation within lnd which can then be extended to the internal wallets. --- chainregistry.go | 7 ++++--- lnwallet/btcwallet/btcwallet.go | 12 ++++++++++-- lnwallet/btcwallet/config.go | 8 +++++++- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/chainregistry.go b/chainregistry.go index d0f26a942..f54cafd3e 100644 --- a/chainregistry.go +++ b/chainregistry.go @@ -83,9 +83,10 @@ func newChainControlFromConfig(cfg *config, chanDB *channeldb.DB) (*chainControl estimator := lnwallet.StaticFeeEstimator{FeeRate: 50} walletConfig := &btcwallet.Config{ - PrivatePass: []byte("hello"), - DataDir: homeChainConfig.ChainDir, - NetParams: activeNetParams.Params, + PrivatePass: []byte("hello"), + DataDir: homeChainConfig.ChainDir, + NetParams: activeNetParams.Params, + FeeEstimator: estimator, } cc := &chainControl{ diff --git a/lnwallet/btcwallet/btcwallet.go b/lnwallet/btcwallet/btcwallet.go index 81c7d342c..1650cb611 100644 --- a/lnwallet/btcwallet/btcwallet.go +++ b/lnwallet/btcwallet/btcwallet.go @@ -108,6 +108,14 @@ func New(cfg Config) (*BtcWallet, error) { return nil, err } + // Using the passed fee estimator, we'll compute the relay fee for all + // transactions made which will be scaled up according to the size of a + // particular transaction. + // + // TODO(roasbeef): hook in dynamic relay fees + relayFee := cfg.FeeEstimator.EstimateFeePerByte(3) * 1000 + wallet.SetRelayFee(btcutil.Amount(relayFee)) + return &BtcWallet{ cfg: &cfg, wallet: wallet, @@ -123,7 +131,7 @@ func New(cfg Config) (*BtcWallet, error) { // // This is a part of the WalletController interface. func (b *BtcWallet) Start() error { - // Establish an RPC connection in additino to starting the goroutines + // Establish an RPC connection in addition to starting the goroutines // in the underlying wallet. if err := b.chain.Start(); err != nil { return err @@ -465,7 +473,7 @@ func (b *BtcWallet) ListTransactionDetails() ([]*lnwallet.TransactionDetail, err txDetails := make([]*lnwallet.TransactionDetail, 0, len(txns.MinedTransactions)+len(txns.UnminedTransactions)) - // For both confirmed and unconfirme dtransactions, create a + // For both confirmed and unconfirmed transactions, create a // TransactionDetail which re-packages the data returned by the base // wallet. for _, blockPackage := range txns.MinedTransactions { diff --git a/lnwallet/btcwallet/config.go b/lnwallet/btcwallet/config.go index ab374de29..532ef3ca1 100644 --- a/lnwallet/btcwallet/config.go +++ b/lnwallet/btcwallet/config.go @@ -3,6 +3,7 @@ package btcwallet import ( "path/filepath" + "github.com/lightningnetwork/lnd/lnwallet" "github.com/roasbeef/btcd/chaincfg" "github.com/roasbeef/btcd/wire" "github.com/roasbeef/btcutil" @@ -67,6 +68,11 @@ type Config struct { // notifications for received funds, etc. ChainSource chain.Interface + // FeeEstimator is an instance of the fee estimator interface which + // will be used by the wallet to dynamically set transaction fees when + // crafting transactions. + FeeEstimator lnwallet.FeeEstimator + // NetParams is the net parameters for the target chain. NetParams *chaincfg.Params } @@ -78,7 +84,7 @@ func networkDir(dataDir string, chainParams *chaincfg.Params) string { // For now, we must always name the testnet data directory as "testnet" // and not "testnet3" or any other version, as the chaincfg testnet3 - // paramaters will likely be switched to being named "testnet3" in the + // parameters will likely be switched to being named "testnet3" in the // future. This is done to future proof that change, and an upgrade // plan to move the testnet3 data directory can be worked out later. if chainParams.Net == wire.TestNet3 {