sweep: log tx label in GetSweepFee methods

Added argument 'label' to GetSweepFee and GetSweepFeeDetails. It is logged
together with expected weight, fee and feerate.
This commit is contained in:
Boris Nagaev 2024-10-21 15:35:32 -03:00
parent f8c1607154
commit 697e8d6255
No known key found for this signature in database
6 changed files with 53 additions and 8 deletions

View file

@ -570,8 +570,11 @@ func (s *Client) getLoopOutSweepFee(ctx context.Context, confTarget int32) (
htlc = swap.QuoteHtlcP2WSH htlc = swap.QuoteHtlcP2WSH
} }
label := "loopout-quote"
return s.sweeper.GetSweepFee( return s.sweeper.GetSweepFee(
ctx, htlc.AddSuccessToEstimator, p2wshAddress, confTarget, ctx, htlc.AddSuccessToEstimator, p2wshAddress, confTarget,
label,
) )
} }

View file

@ -11,6 +11,7 @@ import (
"github.com/lightninglabs/loop/liquidity" "github.com/lightninglabs/loop/liquidity"
"github.com/lightninglabs/loop/loopdb" "github.com/lightninglabs/loop/loopdb"
"github.com/lightninglabs/loop/notifications" "github.com/lightninglabs/loop/notifications"
"github.com/lightninglabs/loop/sweep"
"github.com/lightninglabs/loop/sweepbatcher" "github.com/lightninglabs/loop/sweepbatcher"
"github.com/lightningnetwork/lnd" "github.com/lightningnetwork/lnd"
"github.com/lightningnetwork/lnd/build" "github.com/lightningnetwork/lnd/build"
@ -52,6 +53,9 @@ func SetupLoggers(root *build.RotatingLogWriter, intercept signal.Interceptor) {
lnd.AddSubLogger( lnd.AddSubLogger(
root, notifications.Subsystem, intercept, notifications.UseLogger, 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 // genSubLogger creates a logger for a subsystem. We provide an instance of

View file

@ -1077,10 +1077,12 @@ func (s *loopInSwap) publishTimeoutTx(ctx context.Context,
} }
} }
label := fmt.Sprintf("loopin-timeout-%x", s.hash[:6])
// Calculate sweep tx fee. // Calculate sweep tx fee.
fee, err := s.sweeper.GetSweepFee( fee, err := s.sweeper.GetSweepFee(
ctx, s.htlc.AddTimeoutToEstimator, s.timeoutAddr, ctx, s.htlc.AddTimeoutToEstimator, s.timeoutAddr,
TimeoutTxConfTarget, TimeoutTxConfTarget, label,
) )
if err != nil { if err != nil {
return 0, err return 0, err

View file

@ -312,11 +312,13 @@ func handleHtlcExpiry(t *testing.T, ctx *loopInTestContext, inSwap *loopInSwap,
// Expect timeout tx to be published. // Expect timeout tx to be published.
timeoutTx := <-ctx.lnd.TxPublishChannel 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 // We can just get our sweep fee as we would in the swap code because
// our estimate is static. // our estimate is static.
fee, err := inSwap.sweeper.GetSweepFee( fee, err := inSwap.sweeper.GetSweepFee(
context.Background(), inSwap.htlc.AddTimeoutToEstimator, context.Background(), inSwap.htlc.AddTimeoutToEstimator,
inSwap.timeoutAddr, TimeoutTxConfTarget, inSwap.timeoutAddr, TimeoutTxConfTarget, label,
) )
require.NoError(t, err) require.NoError(t, err)
cost.Onchain += fee cost.Onchain += fee

26
sweep/log.go Normal file
View file

@ -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
}

View file

@ -177,15 +177,15 @@ func (s *Sweeper) CreateSweepTx(
// GetSweepFee calculates the required tx fee to spend to P2WKH. It takes a // 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 // 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, func (s *Sweeper) GetSweepFee(ctx context.Context,
addInputEstimate func(*input.TxWeightEstimator) error, addInputEstimate func(*input.TxWeightEstimator) error,
destAddr btcutil.Address, sweepConfTarget int32) ( destAddr btcutil.Address, sweepConfTarget int32, label string) (
btcutil.Amount, error) { btcutil.Amount, error) {
// Use GetSweepFeeDetails to get the fee and other unused data. // Use GetSweepFeeDetails to get the fee and other unused data.
fee, _, _, err := s.GetSweepFeeDetails( fee, _, _, err := s.GetSweepFeeDetails(
ctx, addInputEstimate, destAddr, sweepConfTarget, ctx, addInputEstimate, destAddr, sweepConfTarget, label,
) )
return fee, err 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 // 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 // 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, func (s *Sweeper) GetSweepFeeDetails(ctx context.Context,
addInputEstimate func(*input.TxWeightEstimator) error, addInputEstimate func(*input.TxWeightEstimator) error,
destAddr btcutil.Address, sweepConfTarget int32) ( destAddr btcutil.Address, sweepConfTarget int32, label string) (
btcutil.Amount, chainfee.SatPerKWeight, lntypes.WeightUnit, error) { btcutil.Amount, chainfee.SatPerKWeight, lntypes.WeightUnit, error) {
// Get fee estimate from lnd. // Get fee estimate from lnd.
@ -224,7 +225,14 @@ func (s *Sweeper) GetSweepFeeDetails(ctx context.Context,
// Find weight. // Find weight.
weight := weightEstimate.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. // AddOutputEstimate adds output to weight estimator.