|
| 1 | +package loop |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/btcsuite/btcd/btcutil" |
| 8 | + "github.com/btcsuite/btcd/chaincfg" |
| 9 | + "github.com/btcsuite/btcd/txscript" |
| 10 | + "github.com/lightninglabs/loop/loopdb" |
| 11 | + "github.com/lightninglabs/loop/swap" |
| 12 | + sweeperpkg "github.com/lightninglabs/loop/sweeper" |
| 13 | + "github.com/lightninglabs/loop/utils" |
| 14 | + "github.com/lightningnetwork/lnd/input" |
| 15 | + "github.com/lightningnetwork/lnd/lntypes" |
| 16 | + "github.com/lightningnetwork/lnd/lnwallet/chainfee" |
| 17 | +) |
| 18 | + |
| 19 | +// sweepFeeGetter calculates sweep's confTarget and fee in sats. |
| 20 | +type sweepFeeGetter interface { |
| 21 | + // SweepFeeRate calculates sweep's confTarget and fee in sats. |
| 22 | + // It takes high priority case into account. |
| 23 | + SweepFeeRate(ctx context.Context, amt btcutil.Amount, |
| 24 | + addInputToEstimator func(e *input.TxWeightEstimator) error, |
| 25 | + destAddr btcutil.Address, cltvExpiry, height int32, |
| 26 | + label string) (int32, btcutil.Amount, chainfee.SatPerKWeight, |
| 27 | + lntypes.WeightUnit, error) |
| 28 | +} |
| 29 | + |
| 30 | +// loopOutFetcher provides the loop out swap with the given hash. |
| 31 | +type loopOutFetcher interface { |
| 32 | + // FetchLoopOutSwap returns the loop out swap with the given hash. |
| 33 | + FetchLoopOutSwap(ctx context.Context, |
| 34 | + hash lntypes.Hash) (*loopdb.LoopOut, error) |
| 35 | +} |
| 36 | + |
| 37 | +// heightGetter returns current height known to the swap server. |
| 38 | +type heightGetter func() int32 |
| 39 | + |
| 40 | +// loopOutSweepFeerateProvider provides sweepbatcher with the info about swap's |
| 41 | +// current feerate for loop-out sweep. |
| 42 | +type loopOutSweepFeerateProvider struct { |
| 43 | + // sweepFeeGetter incorporates the logic of determining swap feerate. |
| 44 | + sweepFeeGetter sweepFeeGetter |
| 45 | + |
| 46 | + // loopOutFetcher loads LoopOut from DB by swap hash. |
| 47 | + loopOutFetcher loopOutFetcher |
| 48 | + |
| 49 | + // chainParams are the chain parameters of the chain that is used by |
| 50 | + // swaps. |
| 51 | + chainParams *chaincfg.Params |
| 52 | + |
| 53 | + // getHeight returns current height known to the swap server. |
| 54 | + getHeight heightGetter |
| 55 | +} |
| 56 | + |
| 57 | +// newLoopOutSweepFeerateProvider builds and returns new instance of |
| 58 | +// loopOutSweepFeerateProvider. |
| 59 | +func newLoopOutSweepFeerateProvider(sweeper sweeperpkg.Sweeper, |
| 60 | + loopOutFetcher loopOutFetcher, chainParams *chaincfg.Params, |
| 61 | + getHeight heightGetter) (*loopOutSweepFeerateProvider, error) { |
| 62 | + |
| 63 | + // Initialize sweep fee provider for loop-out's. |
| 64 | + sweepFeeProvider := &sweeperpkg.SweepFeeProvider{ |
| 65 | + Sweeper: sweeper, |
| 66 | + |
| 67 | + DefaultSweepConfTargetDelta: DefaultSweepConfTargetDelta, |
| 68 | + UrgentSweepConfTarget: urgentSweepConfTarget, |
| 69 | + DefaultSweepConfTargetFactor: 1.0, |
| 70 | + UrgentSweepConfTargetFactor: urgentSweepConfTargetFactor, |
| 71 | + HighPrioSweepAmount: highPrioSweepAmount, |
| 72 | + HighPrioFeePPM: highPrioFeePPM, |
| 73 | + } |
| 74 | + |
| 75 | + if err := sweepFeeProvider.Validate(); err != nil { |
| 76 | + return nil, fmt.Errorf("sweep fee provider failed: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + return &loopOutSweepFeerateProvider{ |
| 80 | + sweepFeeGetter: sweepFeeProvider, |
| 81 | + loopOutFetcher: loopOutFetcher, |
| 82 | + chainParams: chainParams, |
| 83 | + getHeight: getHeight, |
| 84 | + }, nil |
| 85 | +} |
| 86 | + |
| 87 | +// GetMinFeeRate returns minimum required feerate for a sweep by swap hash. |
| 88 | +func (p *loopOutSweepFeerateProvider) GetMinFeeRate(ctx context.Context, |
| 89 | + swapHash lntypes.Hash) (chainfee.SatPerKWeight, error) { |
| 90 | + |
| 91 | + _, feeRate, err := p.GetConfTargetAndFeeRate(ctx, swapHash) |
| 92 | + |
| 93 | + return feeRate, err |
| 94 | +} |
| 95 | + |
| 96 | +// GetConfTargetAndFeeRate returns conf target and minimum required feerate |
| 97 | +// for a sweep by swap hash. |
| 98 | +func (p *loopOutSweepFeerateProvider) GetConfTargetAndFeeRate( |
| 99 | + ctx context.Context, swapHash lntypes.Hash) (int32, |
| 100 | + chainfee.SatPerKWeight, error) { |
| 101 | + |
| 102 | + // Load the loop-out from DB. |
| 103 | + loopOut, err := p.loopOutFetcher.FetchLoopOutSwap(ctx, swapHash) |
| 104 | + if err != nil { |
| 105 | + return 0, 0, fmt.Errorf("failed to load swap %x from DB: %w", |
| 106 | + swapHash[:6], err) |
| 107 | + } |
| 108 | + |
| 109 | + contract := loopOut.Contract |
| 110 | + if contract == nil { |
| 111 | + return 0, 0, fmt.Errorf("loop-out %x has nil Contract", |
| 112 | + swapHash[:6]) |
| 113 | + } |
| 114 | + |
| 115 | + // Determine if we can keyspend. |
| 116 | + htlcVersion := utils.GetHtlcScriptVersion(contract.ProtocolVersion) |
| 117 | + canKeyspend := htlcVersion >= swap.HtlcV3 |
| 118 | + |
| 119 | + // Find addInputToEstimator function. |
| 120 | + var addInputToEstimator func(e *input.TxWeightEstimator) error |
| 121 | + if canKeyspend { |
| 122 | + // Assume the server is cooperative and we produce keyspend. |
| 123 | + addInputToEstimator = func(e *input.TxWeightEstimator) error { |
| 124 | + e.AddTaprootKeySpendInput(txscript.SigHashDefault) |
| 125 | + return nil |
| 126 | + } |
| 127 | + } else { |
| 128 | + // Get the HTLC script for our swap. |
| 129 | + htlc, err := utils.GetHtlc( |
| 130 | + swapHash, &contract.SwapContract, p.chainParams, |
| 131 | + ) |
| 132 | + if err != nil { |
| 133 | + return 0, 0, fmt.Errorf("failed to get HTLC: %w", err) |
| 134 | + } |
| 135 | + addInputToEstimator = htlc.AddSuccessToEstimator |
| 136 | + } |
| 137 | + |
| 138 | + // Transaction weight might be important for feeRate, in case of high |
| 139 | + // priority proportional fee, so we accurately assess the size of input. |
| 140 | + // The size of output is almost the same for all types, so use P2TR. |
| 141 | + var destAddr *btcutil.AddressTaproot |
| 142 | + |
| 143 | + // Get current height. |
| 144 | + height := p.getHeight() |
| 145 | + if height == 0 { |
| 146 | + return 0, 0, fmt.Errorf("got zero best block height") |
| 147 | + } |
| 148 | + |
| 149 | + // Construct the label. |
| 150 | + label := fmt.Sprintf("loopout-sweep-%x", swapHash[:6]) |
| 151 | + |
| 152 | + // Estimate confTarget and feeRate. |
| 153 | + confTarget, _, feeRate, _, err := p.sweepFeeGetter.SweepFeeRate( |
| 154 | + ctx, contract.AmountRequested, addInputToEstimator, destAddr, |
| 155 | + contract.CltvExpiry, height, label, |
| 156 | + ) |
| 157 | + if err != nil { |
| 158 | + return 0, 0, fmt.Errorf("failed to determine "+ |
| 159 | + "confTarget and feeRate for swap hash %x: %w", |
| 160 | + swapHash[:6], err) |
| 161 | + } |
| 162 | + |
| 163 | + log.Debugf("Estimated for swap %x: feeRate=%s, confTarget=%d.", |
| 164 | + swapHash[:6], feeRate, confTarget) |
| 165 | + |
| 166 | + return confTarget, feeRate, nil |
| 167 | +} |
0 commit comments