From 697e8d625539322358c7d03316e90aa97c37cc9d Mon Sep 17 00:00:00 2001 From: Boris Nagaev Date: Mon, 21 Oct 2024 15:35:32 -0300 Subject: [PATCH] sweep: log tx label in GetSweepFee methods Added argument 'label' to GetSweepFee and GetSweepFeeDetails. It is logged together with expected weight, fee and feerate. --- client.go | 3 +++ loopd/log.go | 4 ++++ loopin.go | 4 +++- loopin_test.go | 4 +++- sweep/log.go | 26 ++++++++++++++++++++++++++ sweep/sweeper.go | 20 ++++++++++++++------ 6 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 sweep/log.go diff --git a/client.go b/client.go index f116f6b3..084cfe29 100644 --- a/client.go +++ b/client.go @@ -570,8 +570,11 @@ func (s *Client) getLoopOutSweepFee(ctx context.Context, confTarget int32) ( htlc = swap.QuoteHtlcP2WSH } + label := "loopout-quote" + return s.sweeper.GetSweepFee( ctx, htlc.AddSuccessToEstimator, p2wshAddress, confTarget, + label, ) } diff --git a/loopd/log.go b/loopd/log.go index a4f433f0..6ef5a74b 100644 --- a/loopd/log.go +++ b/loopd/log.go @@ -11,6 +11,7 @@ import ( "github.com/lightninglabs/loop/liquidity" "github.com/lightninglabs/loop/loopdb" "github.com/lightninglabs/loop/notifications" + "github.com/lightninglabs/loop/sweep" "github.com/lightninglabs/loop/sweepbatcher" "github.com/lightningnetwork/lnd" "github.com/lightningnetwork/lnd/build" @@ -52,6 +53,9 @@ func SetupLoggers(root *build.RotatingLogWriter, intercept signal.Interceptor) { lnd.AddSubLogger( root, notifications.Subsystem, intercept, notifications.UseLogger, ) + lnd.AddSubLogger( + root, sweep.Subsystem, intercept, sweep.UseLogger, + ) } // genSubLogger creates a logger for a subsystem. We provide an instance of diff --git a/loopin.go b/loopin.go index b72d1795..21e40dcb 100644 --- a/loopin.go +++ b/loopin.go @@ -1077,10 +1077,12 @@ func (s *loopInSwap) publishTimeoutTx(ctx context.Context, } } + label := fmt.Sprintf("loopin-timeout-%x", s.hash[:6]) + // Calculate sweep tx fee. fee, err := s.sweeper.GetSweepFee( ctx, s.htlc.AddTimeoutToEstimator, s.timeoutAddr, - TimeoutTxConfTarget, + TimeoutTxConfTarget, label, ) if err != nil { return 0, err diff --git a/loopin_test.go b/loopin_test.go index 32f05210..5f403ba8 100644 --- a/loopin_test.go +++ b/loopin_test.go @@ -312,11 +312,13 @@ func handleHtlcExpiry(t *testing.T, ctx *loopInTestContext, inSwap *loopInSwap, // Expect timeout tx to be published. timeoutTx := <-ctx.lnd.TxPublishChannel + label := fmt.Sprintf("loopin-timeout-%x", inSwap.hash[:6]) + // We can just get our sweep fee as we would in the swap code because // our estimate is static. fee, err := inSwap.sweeper.GetSweepFee( context.Background(), inSwap.htlc.AddTimeoutToEstimator, - inSwap.timeoutAddr, TimeoutTxConfTarget, + inSwap.timeoutAddr, TimeoutTxConfTarget, label, ) require.NoError(t, err) cost.Onchain += fee diff --git a/sweep/log.go b/sweep/log.go new file mode 100644 index 00000000..3d0931a4 --- /dev/null +++ b/sweep/log.go @@ -0,0 +1,26 @@ +package sweep + +import ( + "github.com/btcsuite/btclog" + "github.com/lightningnetwork/lnd/build" +) + +// Subsystem defines the sub system name of this package. +const Subsystem = "SWP" + +// log is a logger that is initialized with no output filters. This means the +// package will not perform any logging by default until the caller requests +// it. +var log btclog.Logger + +// The default amount of logging is none. +func init() { + UseLogger(build.NewSubLogger(Subsystem, nil)) +} + +// UseLogger uses a specified Logger to output package logging info. This +// should be used in preference to SetLogWriter if the caller is also using +// btclog. +func UseLogger(logger btclog.Logger) { + log = logger +} diff --git a/sweep/sweeper.go b/sweep/sweeper.go index e6b3df46..edcce025 100644 --- a/sweep/sweeper.go +++ b/sweep/sweeper.go @@ -177,15 +177,15 @@ func (s *Sweeper) CreateSweepTx( // GetSweepFee calculates the required tx fee to spend to P2WKH. It takes a // function that is expected to add the weight of the input to the weight -// estimator. +// estimator. It also takes a label used for logging. func (s *Sweeper) GetSweepFee(ctx context.Context, addInputEstimate func(*input.TxWeightEstimator) error, - destAddr btcutil.Address, sweepConfTarget int32) ( + destAddr btcutil.Address, sweepConfTarget int32, label string) ( btcutil.Amount, error) { // Use GetSweepFeeDetails to get the fee and other unused data. fee, _, _, err := s.GetSweepFeeDetails( - ctx, addInputEstimate, destAddr, sweepConfTarget, + ctx, addInputEstimate, destAddr, sweepConfTarget, label, ) return fee, err @@ -193,10 +193,11 @@ func (s *Sweeper) GetSweepFee(ctx context.Context, // GetSweepFeeDetails calculates the required tx fee to spend to P2WKH. It takes // a function that is expected to add the weight of the input to the weight -// estimator. It returns also the fee rate and transaction weight. +// estimator. It also takes a label used for logging. It returns also the fee +// rate and transaction weight. func (s *Sweeper) GetSweepFeeDetails(ctx context.Context, addInputEstimate func(*input.TxWeightEstimator) error, - destAddr btcutil.Address, sweepConfTarget int32) ( + destAddr btcutil.Address, sweepConfTarget int32, label string) ( btcutil.Amount, chainfee.SatPerKWeight, lntypes.WeightUnit, error) { // Get fee estimate from lnd. @@ -224,7 +225,14 @@ func (s *Sweeper) GetSweepFeeDetails(ctx context.Context, // Find weight. weight := weightEstimate.Weight() - return feeRate.FeeForWeight(weight), feeRate, weight, nil + // Find fee. + fee := feeRate.FeeForWeight(weight) + + log.Debugf("Estimations for a tx (label=%s): weight=%v, fee=%v, "+ + "feerate=%v, sweepConfTarget=%d.", label, weight, fee, feeRate, + sweepConfTarget) + + return fee, feeRate, weight, nil } // AddOutputEstimate adds output to weight estimator.