2020-09-01 07:58:08 +00:00
|
|
|
// Package liquidity is responsible for monitoring our node's liquidity. It
|
|
|
|
// allows setting of a liquidity rule which describes the desired liquidity
|
|
|
|
// balance on a per-channel basis.
|
2020-09-30 10:34:07 +00:00
|
|
|
//
|
|
|
|
// Swap suggestions are limited to channels that are not currently being used
|
|
|
|
// for a pending swap. If we are currently processing an unrestricted swap (ie,
|
|
|
|
// a loop out with no outgoing channel targets set or a loop in with no last
|
|
|
|
// hop set), we will not suggest any swaps because these swaps will shift the
|
|
|
|
// balances of our channels in ways we can't predict.
|
2020-09-30 10:34:08 +00:00
|
|
|
//
|
|
|
|
// Fee restrictions are placed on swap suggestions to ensure that we only
|
|
|
|
// suggest swaps that fit the configured fee preferences.
|
|
|
|
// - Sweep Fee Rate Limit: the maximum sat/vByte fee estimate for our sweep
|
|
|
|
// transaction to confirm within our configured number of confirmations
|
|
|
|
// that we will suggest swaps for.
|
2020-09-30 10:34:09 +00:00
|
|
|
// - Maximum Swap Fee PPM: the maximum server fee, expressed as parts per
|
|
|
|
// million of the full swap amount
|
|
|
|
// - Maximum Routing Fee PPM: the maximum off-chain routing fees for the swap
|
|
|
|
// invoice, expressed as parts per million of the swap amount.
|
|
|
|
// - Maximum Prepay Routing Fee PPM: the maximum off-chain routing fees for the
|
|
|
|
// swap prepayment, expressed as parts per million of the prepay amount.
|
|
|
|
// - Maximum Prepay: the maximum now-show fee, expressed in satoshis. This
|
|
|
|
// amount is only payable in the case where the swap server broadcasts a htlc
|
|
|
|
// and the client fails to sweep the preimage.
|
|
|
|
// - Maximum miner fee: the maximum miner fee we are willing to pay to sweep the
|
|
|
|
// on chain htlc. Note that the client will use current fee estimates to
|
|
|
|
// sweep, so this value acts more as a sanity check in the case of a large fee
|
|
|
|
// spike.
|
|
|
|
//
|
|
|
|
// The maximum fee per-swap is calculated as follows:
|
|
|
|
// (swap amount * serverPPM/1e6) + miner fee + (swap amount * routingPPM/1e6)
|
|
|
|
// + (prepay amount * prepayPPM/1e6).
|
2020-09-01 07:58:08 +00:00
|
|
|
package liquidity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-30 10:34:09 +00:00
|
|
|
"errors"
|
2020-09-01 07:58:08 +00:00
|
|
|
"fmt"
|
2020-10-12 11:34:54 +00:00
|
|
|
"sort"
|
2020-09-01 07:58:08 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2020-09-30 10:34:08 +00:00
|
|
|
"time"
|
2020-09-01 07:58:08 +00:00
|
|
|
|
2020-09-30 10:34:02 +00:00
|
|
|
"github.com/btcsuite/btcutil"
|
2020-09-03 08:36:43 +00:00
|
|
|
"github.com/lightninglabs/lndclient"
|
2020-09-30 10:34:02 +00:00
|
|
|
"github.com/lightninglabs/loop"
|
2020-10-12 11:34:54 +00:00
|
|
|
"github.com/lightninglabs/loop/labels"
|
2020-09-30 10:34:02 +00:00
|
|
|
"github.com/lightninglabs/loop/loopdb"
|
2021-02-03 06:54:50 +00:00
|
|
|
"github.com/lightninglabs/loop/swap"
|
2020-09-30 10:34:05 +00:00
|
|
|
"github.com/lightningnetwork/lnd/clock"
|
2020-12-09 09:54:22 +00:00
|
|
|
"github.com/lightningnetwork/lnd/funding"
|
2020-09-30 10:34:08 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
2020-09-01 07:58:08 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2020-09-30 10:34:07 +00:00
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
2020-10-12 11:34:55 +00:00
|
|
|
"github.com/lightningnetwork/lnd/ticker"
|
2020-09-01 07:58:08 +00:00
|
|
|
)
|
|
|
|
|
2020-09-30 10:34:02 +00:00
|
|
|
const (
|
2020-09-30 10:34:08 +00:00
|
|
|
// defaultFailureBackoff is the default amount of time we backoff if
|
|
|
|
// a channel is part of a temporarily failed swap.
|
|
|
|
defaultFailureBackoff = time.Hour * 24
|
|
|
|
|
2020-09-30 10:34:02 +00:00
|
|
|
// FeeBase is the base that we use to express fees.
|
|
|
|
FeeBase = 1e6
|
|
|
|
|
|
|
|
// defaultSwapFeePPM is the default limit we place on swap fees,
|
|
|
|
// expressed as parts per million of swap volume, 0.5%.
|
|
|
|
defaultSwapFeePPM = 5000
|
|
|
|
|
|
|
|
// defaultRoutingFeePPM is the default limit we place on routing fees
|
|
|
|
// for the swap invoice, expressed as parts per million of swap volume,
|
|
|
|
// 1%.
|
|
|
|
defaultRoutingFeePPM = 10000
|
|
|
|
|
|
|
|
// defaultRoutingFeePPM is the default limit we place on routing fees
|
|
|
|
// for the prepay invoice, expressed as parts per million of prepay
|
|
|
|
// volume, 0.5%.
|
|
|
|
defaultPrepayRoutingFeePPM = 5000
|
|
|
|
|
|
|
|
// defaultMaximumMinerFee is the default limit we place on miner fees
|
2020-09-30 10:34:09 +00:00
|
|
|
// per swap. We apply a multiplier to this default fee to guard against
|
|
|
|
// the case where we have broadcast the preimage, then fees spike and
|
|
|
|
// we need to sweep the preimage.
|
|
|
|
defaultMaximumMinerFee = 15000 * 100
|
2020-09-30 10:34:02 +00:00
|
|
|
|
|
|
|
// defaultMaximumPrepay is the default limit we place on prepay
|
|
|
|
// invoices.
|
|
|
|
defaultMaximumPrepay = 30000
|
2020-09-30 10:34:08 +00:00
|
|
|
|
|
|
|
// defaultSweepFeeRateLimit is the default limit we place on estimated
|
|
|
|
// sweep fees, (750 * 4 /1000 = 3 sat/vByte).
|
|
|
|
defaultSweepFeeRateLimit = chainfee.SatPerKWeight(750)
|
2020-10-12 11:34:54 +00:00
|
|
|
|
|
|
|
// defaultMaxInFlight is the default number of in-flight automatically
|
|
|
|
// dispatched swaps we allow. Note that this does not enable automated
|
|
|
|
// swaps itself (because we want non-zero values to be expressed in
|
|
|
|
// suggestions as a dry-run).
|
2020-10-12 11:34:55 +00:00
|
|
|
defaultMaxInFlight = 1
|
|
|
|
|
2021-02-03 06:54:49 +00:00
|
|
|
// DefaultAutoloopTicker is the default amount of time between automated
|
|
|
|
// swap checks.
|
|
|
|
DefaultAutoloopTicker = time.Minute * 10
|
2020-11-06 09:43:05 +00:00
|
|
|
|
|
|
|
// autoloopSwapInitiator is the value we send in the initiator field of
|
|
|
|
// a swap request when issuing an automatic swap.
|
|
|
|
autoloopSwapInitiator = "autoloop"
|
2020-09-30 10:34:02 +00:00
|
|
|
)
|
|
|
|
|
2020-09-01 07:58:08 +00:00
|
|
|
var (
|
2020-10-12 11:34:54 +00:00
|
|
|
// defaultBudget is the default autoloop budget we set. This budget will
|
|
|
|
// only be used for automatically dispatched swaps if autoloop is
|
|
|
|
// explicitly enabled, so we are happy to set a non-zero value here. The
|
|
|
|
// amount chosen simply uses the current defaults to provide budget for
|
|
|
|
// a single swap. We don't have a swap amount to calculate our maximum
|
|
|
|
// routing fee, so we use 0.16 BTC for now.
|
|
|
|
defaultBudget = defaultMaximumMinerFee +
|
2020-12-09 09:54:22 +00:00
|
|
|
ppmToSat(funding.MaxBtcFundingAmount, defaultSwapFeePPM) +
|
2020-10-12 11:34:54 +00:00
|
|
|
ppmToSat(defaultMaximumPrepay, defaultPrepayRoutingFeePPM) +
|
2020-12-09 09:54:22 +00:00
|
|
|
ppmToSat(funding.MaxBtcFundingAmount, defaultRoutingFeePPM)
|
2020-10-12 11:34:54 +00:00
|
|
|
|
2020-09-30 10:34:06 +00:00
|
|
|
// defaultParameters contains the default parameters that we start our
|
|
|
|
// liquidity manger with.
|
|
|
|
defaultParameters = Parameters{
|
2020-10-12 11:34:54 +00:00
|
|
|
AutoFeeBudget: defaultBudget,
|
2020-10-12 11:34:54 +00:00
|
|
|
MaxAutoInFlight: defaultMaxInFlight,
|
2020-09-30 10:34:09 +00:00
|
|
|
ChannelRules: make(map[lnwire.ShortChannelID]*ThresholdRule),
|
2021-02-16 11:31:51 +00:00
|
|
|
PeerRules: make(map[route.Vertex]*ThresholdRule),
|
2020-09-30 10:34:09 +00:00
|
|
|
FailureBackOff: defaultFailureBackoff,
|
|
|
|
SweepFeeRateLimit: defaultSweepFeeRateLimit,
|
|
|
|
SweepConfTarget: loop.DefaultSweepConfTarget,
|
|
|
|
MaximumSwapFeePPM: defaultSwapFeePPM,
|
|
|
|
MaximumRoutingFeePPM: defaultRoutingFeePPM,
|
|
|
|
MaximumPrepayRoutingFeePPM: defaultPrepayRoutingFeePPM,
|
|
|
|
MaximumMinerFee: defaultMaximumMinerFee,
|
|
|
|
MaximumPrepay: defaultMaximumPrepay,
|
2020-09-30 10:34:06 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 07:58:08 +00:00
|
|
|
// ErrZeroChannelID is returned if we get a rule for a 0 channel ID.
|
|
|
|
ErrZeroChannelID = fmt.Errorf("zero channel ID not allowed")
|
2020-09-30 10:34:08 +00:00
|
|
|
|
|
|
|
// ErrInvalidSweepFeeRateLimit is returned if an invalid sweep fee limit
|
|
|
|
// is set.
|
|
|
|
ErrInvalidSweepFeeRateLimit = fmt.Errorf("sweep fee rate limit must "+
|
|
|
|
"be > %v sat/vByte",
|
|
|
|
satPerKwToSatPerVByte(chainfee.AbsoluteFeePerKwFloor))
|
2020-09-30 10:34:09 +00:00
|
|
|
|
|
|
|
// ErrZeroMinerFee is returned if a zero maximum miner fee is set.
|
|
|
|
ErrZeroMinerFee = errors.New("maximum miner fee must be non-zero")
|
|
|
|
|
|
|
|
// ErrZeroSwapFeePPM is returned if a zero server fee ppm is set.
|
|
|
|
ErrZeroSwapFeePPM = errors.New("swap fee PPM must be non-zero")
|
|
|
|
|
|
|
|
// ErrZeroRoutingPPM is returned if a zero routing fee ppm is set.
|
|
|
|
ErrZeroRoutingPPM = errors.New("routing fee PPM must be non-zero")
|
|
|
|
|
|
|
|
// ErrZeroPrepayPPM is returned if a zero prepay routing fee ppm is set.
|
|
|
|
ErrZeroPrepayPPM = errors.New("prepay routing fee PPM must be non-zero")
|
|
|
|
|
|
|
|
// ErrZeroPrepay is returned if a zero maximum prepay is set.
|
|
|
|
ErrZeroPrepay = errors.New("maximum prepay must be non-zero")
|
2020-10-12 11:34:54 +00:00
|
|
|
|
|
|
|
// ErrNegativeBudget is returned if a negative swap budget is set.
|
|
|
|
ErrNegativeBudget = errors.New("swap budget must be >= 0")
|
2020-10-12 11:34:54 +00:00
|
|
|
|
|
|
|
// ErrZeroInFlight is returned is a zero in flight swaps value is set.
|
|
|
|
ErrZeroInFlight = errors.New("max in flight swaps must be >=0")
|
2020-12-01 09:18:31 +00:00
|
|
|
|
|
|
|
// ErrMinimumExceedsMaximumAmt is returned when the minimum configured
|
|
|
|
// swap amount is more than the maximum.
|
|
|
|
ErrMinimumExceedsMaximumAmt = errors.New("minimum swap amount " +
|
|
|
|
"exceeds maximum")
|
|
|
|
|
|
|
|
// ErrMaxExceedsServer is returned if the maximum swap amount set is
|
|
|
|
// more than the server offers.
|
|
|
|
ErrMaxExceedsServer = errors.New("maximum swap amount is more than " +
|
|
|
|
"server maximum")
|
|
|
|
|
|
|
|
// ErrMinLessThanServer is returned if the minimum swap amount set is
|
|
|
|
// less than the server minimum.
|
|
|
|
ErrMinLessThanServer = errors.New("minimum swap amount is less than " +
|
|
|
|
"server minimum")
|
2021-02-08 07:38:23 +00:00
|
|
|
|
|
|
|
// ErrNoRules is returned when no rules are set for swap suggestions.
|
|
|
|
ErrNoRules = errors.New("no rules set for autoloop")
|
2021-02-16 11:31:51 +00:00
|
|
|
|
|
|
|
// ErrExclusiveRules is returned when a set of rules that may not be
|
|
|
|
// set together are specified.
|
|
|
|
ErrExclusiveRules = errors.New("channel and peer rules must be " +
|
|
|
|
"exclusive")
|
2020-09-01 07:58:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config contains the external functionality required to run the
|
|
|
|
// liquidity manager.
|
|
|
|
type Config struct {
|
2021-02-03 06:54:49 +00:00
|
|
|
// AutoloopTicker determines how often we should check whether we want
|
|
|
|
// to dispatch an automated swap. We use a force ticker so that we can
|
|
|
|
// trigger autoloop in itests.
|
|
|
|
AutoloopTicker *ticker.Force
|
2020-10-12 11:34:55 +00:00
|
|
|
|
2021-02-03 06:54:50 +00:00
|
|
|
// Restrictions returns the restrictions that the server applies to
|
|
|
|
// swaps.
|
|
|
|
Restrictions func(ctx context.Context, swapType swap.Type) (
|
|
|
|
*Restrictions, error)
|
2020-09-03 08:36:43 +00:00
|
|
|
|
2020-09-30 10:34:02 +00:00
|
|
|
// Lnd provides us with access to lnd's rpc servers.
|
|
|
|
Lnd *lndclient.LndServices
|
2020-09-30 10:34:05 +00:00
|
|
|
|
2020-09-30 10:34:07 +00:00
|
|
|
// ListLoopOut returns all of the loop our swaps stored on disk.
|
|
|
|
ListLoopOut func() ([]*loopdb.LoopOut, error)
|
|
|
|
|
|
|
|
// ListLoopIn returns all of the loop in swaps stored on disk.
|
|
|
|
ListLoopIn func() ([]*loopdb.LoopIn, error)
|
|
|
|
|
2020-09-30 10:34:09 +00:00
|
|
|
// LoopOutQuote gets swap fee, estimated miner fee and prepay amount for
|
|
|
|
// a loop out swap.
|
|
|
|
LoopOutQuote func(ctx context.Context,
|
|
|
|
request *loop.LoopOutQuoteRequest) (*loop.LoopOutQuote, error)
|
|
|
|
|
2020-10-12 11:34:55 +00:00
|
|
|
// LoopOut dispatches a loop out.
|
|
|
|
LoopOut func(ctx context.Context, request *loop.OutRequest) (
|
|
|
|
*loop.LoopOutSwapInfo, error)
|
|
|
|
|
2020-09-30 10:34:05 +00:00
|
|
|
// Clock allows easy mocking of time in unit tests.
|
|
|
|
Clock clock.Clock
|
2020-09-30 10:34:08 +00:00
|
|
|
|
|
|
|
// MinimumConfirmations is the minimum number of confirmations we allow
|
|
|
|
// setting for sweep target.
|
|
|
|
MinimumConfirmations int32
|
2020-09-01 07:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parameters is a set of parameters provided by the user which guide
|
|
|
|
// how we assess liquidity.
|
|
|
|
type Parameters struct {
|
2021-02-03 06:54:47 +00:00
|
|
|
// Autoloop enables automatic dispatch of swaps.
|
|
|
|
Autoloop bool
|
2020-10-12 11:34:55 +00:00
|
|
|
|
2020-10-12 11:34:54 +00:00
|
|
|
// AutoFeeBudget is the total amount we allow to be spent on
|
|
|
|
// automatically dispatched swaps. Once this budget has been used, we
|
|
|
|
// will stop dispatching swaps until the budget is increased or the
|
|
|
|
// start date is moved.
|
|
|
|
AutoFeeBudget btcutil.Amount
|
|
|
|
|
|
|
|
// AutoFeeStartDate is the date from which we will include automatically
|
|
|
|
// dispatched swaps in our current budget, inclusive.
|
|
|
|
AutoFeeStartDate time.Time
|
|
|
|
|
2020-10-12 11:34:54 +00:00
|
|
|
// MaxAutoInFlight is the maximum number of in-flight automatically
|
|
|
|
// dispatched swaps we allow.
|
|
|
|
MaxAutoInFlight int
|
|
|
|
|
2020-09-30 10:34:08 +00:00
|
|
|
// FailureBackOff is the amount of time that we require passes after a
|
|
|
|
// channel has been part of a failed loop out swap before we suggest
|
|
|
|
// using it again.
|
|
|
|
// TODO(carla): add exponential backoff
|
|
|
|
FailureBackOff time.Duration
|
|
|
|
|
2020-09-30 10:34:08 +00:00
|
|
|
// SweepFeeRateLimit is the limit that we place on our estimated sweep
|
|
|
|
// fee. A swap will not be suggested if estimated fee rate is above this
|
|
|
|
// value.
|
|
|
|
SweepFeeRateLimit chainfee.SatPerKWeight
|
|
|
|
|
|
|
|
// SweepConfTarget is the number of blocks we aim to confirm our sweep
|
|
|
|
// transaction in. This value affects the on chain fees we will pay.
|
|
|
|
SweepConfTarget int32
|
|
|
|
|
2020-09-30 10:34:09 +00:00
|
|
|
// MaximumPrepay is the maximum prepay amount we are willing to pay per
|
|
|
|
// swap.
|
|
|
|
MaximumPrepay btcutil.Amount
|
|
|
|
|
|
|
|
// MaximumSwapFeePPM is the maximum server fee we are willing to pay per
|
|
|
|
// swap expressed as parts per million of the swap volume.
|
|
|
|
MaximumSwapFeePPM int
|
|
|
|
|
|
|
|
// MaximumRoutingFeePPM is the maximum off-chain routing fee we
|
|
|
|
// are willing to pay for off chain invoice routing fees per swap,
|
|
|
|
// expressed as parts per million of the swap amount.
|
|
|
|
MaximumRoutingFeePPM int
|
|
|
|
|
|
|
|
// MaximumPrepayRoutingFeePPM is the maximum off-chain routing fee we
|
|
|
|
// are willing to pay for off chain prepay routing fees per swap,
|
|
|
|
// expressed as parts per million of the prepay amount.
|
|
|
|
MaximumPrepayRoutingFeePPM int
|
|
|
|
|
|
|
|
// MaximumMinerFee is the maximum on chain fee that we cap our miner
|
|
|
|
// fee at in case where we need to claim on chain because we have
|
|
|
|
// revealed the preimage, but fees have spiked. We will not initiate a
|
|
|
|
// swap if we estimate that the sweep cost will be above our sweep
|
|
|
|
// fee limit, and we use fee estimates at time of sweep to set our fees,
|
|
|
|
// so this is just a sane cap covering the special case where we need to
|
|
|
|
// sweep during a fee spike.
|
|
|
|
MaximumMinerFee btcutil.Amount
|
|
|
|
|
2020-12-01 09:18:31 +00:00
|
|
|
// ClientRestrictions are the restrictions placed on swap size by the
|
|
|
|
// client.
|
|
|
|
ClientRestrictions Restrictions
|
|
|
|
|
2020-09-01 07:58:08 +00:00
|
|
|
// ChannelRules maps a short channel ID to a rule that describes how we
|
2021-02-16 11:31:51 +00:00
|
|
|
// would like liquidity to be managed. These rules and PeerRules are
|
|
|
|
// exclusively set to prevent overlap between peer and channel rules.
|
2020-09-01 07:58:08 +00:00
|
|
|
ChannelRules map[lnwire.ShortChannelID]*ThresholdRule
|
2021-02-16 11:31:51 +00:00
|
|
|
|
|
|
|
// PeerRules maps a peer's pubkey to a rule that applies to all the
|
|
|
|
// channels that we have with the peer collectively. These rules and
|
|
|
|
// ChannelRules are exclusively set to prevent overlap between peer
|
|
|
|
// and channel rules map to avoid ambiguity.
|
|
|
|
PeerRules map[route.Vertex]*ThresholdRule
|
2020-09-01 07:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the string representation of our parameters.
|
|
|
|
func (p Parameters) String() string {
|
2021-02-16 11:31:51 +00:00
|
|
|
ruleList := make([]string, 0, len(p.ChannelRules)+len(p.PeerRules))
|
2020-09-01 07:58:08 +00:00
|
|
|
|
|
|
|
for channel, rule := range p.ChannelRules {
|
2021-02-16 11:31:51 +00:00
|
|
|
ruleList = append(
|
|
|
|
ruleList, fmt.Sprintf("Channel: %v: %v", channel, rule),
|
2020-09-01 07:58:08 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-02-16 11:31:51 +00:00
|
|
|
for peer, rule := range p.PeerRules {
|
|
|
|
ruleList = append(
|
|
|
|
ruleList, fmt.Sprintf("Peer: %v: %v", peer, rule),
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("rules: %v, failure backoff: %v, sweep "+
|
2020-09-30 10:34:09 +00:00
|
|
|
"fee rate limit: %v, sweep conf target: %v, maximum prepay: "+
|
|
|
|
"%v, maximum miner fee: %v, maximum swap fee ppm: %v, maximum "+
|
2020-10-12 11:34:54 +00:00
|
|
|
"routing fee ppm: %v, maximum prepay routing fee ppm: %v, "+
|
2020-12-01 09:18:31 +00:00
|
|
|
"auto budget: %v, budget start: %v, max auto in flight: %v, "+
|
|
|
|
"minimum swap size=%v, maximum swap size=%v",
|
2021-02-16 11:31:51 +00:00
|
|
|
strings.Join(ruleList, ","), p.FailureBackOff,
|
2020-09-30 10:34:09 +00:00
|
|
|
p.SweepFeeRateLimit, p.SweepConfTarget, p.MaximumPrepay,
|
|
|
|
p.MaximumMinerFee, p.MaximumSwapFeePPM,
|
|
|
|
p.MaximumRoutingFeePPM, p.MaximumPrepayRoutingFeePPM,
|
2020-12-01 09:18:31 +00:00
|
|
|
p.AutoFeeBudget, p.AutoFeeStartDate, p.MaxAutoInFlight,
|
|
|
|
p.ClientRestrictions.Minimum, p.ClientRestrictions.Maximum)
|
2020-09-01 07:58:08 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 11:31:51 +00:00
|
|
|
// haveRules returns a boolean indicating whether we have any rules configured.
|
|
|
|
func (p Parameters) haveRules() bool {
|
|
|
|
if len(p.ChannelRules) != 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p.PeerRules) != 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate checks whether a set of parameters is valid. Our set of currently
|
|
|
|
// open channels are required to check that there is no overlap between the
|
|
|
|
// rules set on a per-peer level, and those set for specific channels. We can't
|
|
|
|
// allow both, because then we're trying to cater for two separate liquidity
|
|
|
|
// goals on the same channel. Since we use short channel ID, we don't need to
|
|
|
|
// worry about pending channels (users would need to work very hard to get the
|
|
|
|
// short channel ID for a pending channel). Likewise, we don't care about closed
|
|
|
|
// channels, since there is no action that may occur on them, and we want to
|
|
|
|
// allow peer-level rules to be set once a channel which had a specific rule
|
|
|
|
// has been closed. It takes the minimum confirmations we allow for sweep
|
|
|
|
// confirmation target as a parameter.
|
|
|
|
// TODO(carla): prune channels that have been closed from rules.
|
|
|
|
func (p Parameters) validate(minConfs int32, openChans []lndclient.ChannelInfo,
|
|
|
|
server *Restrictions) error {
|
|
|
|
|
|
|
|
// First, we check that the rules on a per peer and per channel do not
|
|
|
|
// overlap, since this could lead to contractions.
|
|
|
|
for _, channel := range openChans {
|
|
|
|
// If we don't have a rule for the peer, there's no way we have
|
|
|
|
// an overlap between this peer and the channel.
|
|
|
|
_, ok := p.PeerRules[channel.PubKeyBytes]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
shortID := lnwire.NewShortChanIDFromInt(channel.ChannelID)
|
|
|
|
_, ok = p.ChannelRules[shortID]
|
|
|
|
if ok {
|
|
|
|
log.Debugf("Rules for peer: %v and its channel: %v "+
|
|
|
|
"can't both be set", channel.PubKeyBytes, shortID)
|
|
|
|
|
|
|
|
return ErrExclusiveRules
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:58:08 +00:00
|
|
|
for channel, rule := range p.ChannelRules {
|
|
|
|
if channel.ToUint64() == 0 {
|
|
|
|
return ErrZeroChannelID
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rule.validate(); err != nil {
|
|
|
|
return fmt.Errorf("channel: %v has invalid rule: %v",
|
|
|
|
channel.ToUint64(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 11:31:51 +00:00
|
|
|
for peer, rule := range p.PeerRules {
|
|
|
|
if err := rule.validate(); err != nil {
|
|
|
|
return fmt.Errorf("peer: %v has invalid rule: %v",
|
|
|
|
peer, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 10:34:08 +00:00
|
|
|
// Check that our sweep limit is above our minimum fee rate. We use
|
|
|
|
// absolute fee floor rather than kw floor because we will allow users
|
|
|
|
// to specify fee rate is sat/vByte and want to allow 1 sat/vByte.
|
|
|
|
if p.SweepFeeRateLimit < chainfee.AbsoluteFeePerKwFloor {
|
|
|
|
return ErrInvalidSweepFeeRateLimit
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that our confirmation target is above our required minimum.
|
|
|
|
if p.SweepConfTarget < minConfs {
|
|
|
|
return fmt.Errorf("confirmation target must be at least: %v",
|
|
|
|
minConfs)
|
|
|
|
}
|
|
|
|
|
2020-09-30 10:34:09 +00:00
|
|
|
// Check that we have non-zero fee limits.
|
|
|
|
if p.MaximumSwapFeePPM == 0 {
|
|
|
|
return ErrZeroSwapFeePPM
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.MaximumRoutingFeePPM == 0 {
|
|
|
|
return ErrZeroRoutingPPM
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.MaximumPrepayRoutingFeePPM == 0 {
|
|
|
|
return ErrZeroPrepayPPM
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.MaximumPrepay == 0 {
|
|
|
|
return ErrZeroPrepay
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.MaximumMinerFee == 0 {
|
|
|
|
return ErrZeroMinerFee
|
|
|
|
}
|
|
|
|
|
2020-10-12 11:34:54 +00:00
|
|
|
if p.AutoFeeBudget < 0 {
|
|
|
|
return ErrNegativeBudget
|
|
|
|
}
|
|
|
|
|
2020-10-12 11:34:54 +00:00
|
|
|
if p.MaxAutoInFlight <= 0 {
|
|
|
|
return ErrZeroInFlight
|
|
|
|
}
|
|
|
|
|
2020-12-01 09:18:31 +00:00
|
|
|
err := validateRestrictions(server, &p.ClientRestrictions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// validateRestrictions checks that client restrictions fall within the server's
|
|
|
|
// restrictions.
|
|
|
|
func validateRestrictions(server, client *Restrictions) error {
|
|
|
|
zeroMin := client.Minimum == 0
|
|
|
|
zeroMax := client.Maximum == 0
|
|
|
|
|
|
|
|
if zeroMin && zeroMax {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a non-zero maximum, we need to ensure it is greater than
|
|
|
|
// our minimum (which is fine if min is zero), and does not exceed the
|
|
|
|
// server's maximum.
|
|
|
|
if !zeroMax {
|
|
|
|
if client.Minimum > client.Maximum {
|
|
|
|
return ErrMinimumExceedsMaximumAmt
|
|
|
|
}
|
|
|
|
|
|
|
|
if client.Maximum > server.Maximum {
|
|
|
|
return ErrMaxExceedsServer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if zeroMin {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the client set a minimum, ensure it is at least equal to the
|
|
|
|
// server's limit.
|
|
|
|
if client.Minimum < server.Minimum {
|
|
|
|
return ErrMinLessThanServer
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:58:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manager contains a set of desired liquidity rules for our channel
|
|
|
|
// balances.
|
|
|
|
type Manager struct {
|
|
|
|
// cfg contains the external functionality we require to determine our
|
|
|
|
// current liquidity balance.
|
|
|
|
cfg *Config
|
|
|
|
|
|
|
|
// params is the set of parameters we are currently using. These may be
|
|
|
|
// updated at runtime.
|
|
|
|
params Parameters
|
|
|
|
|
|
|
|
// paramsLock is a lock for our current set of parameters.
|
|
|
|
paramsLock sync.Mutex
|
|
|
|
}
|
|
|
|
|
2020-10-12 11:34:55 +00:00
|
|
|
// Run periodically checks whether we should automatically dispatch a loop out.
|
2020-10-12 11:34:56 +00:00
|
|
|
// We run this loop even if automated swaps are not currently enabled rather
|
|
|
|
// than managing starting and stopping the ticker as our parameters are updated.
|
2020-10-12 11:34:55 +00:00
|
|
|
func (m *Manager) Run(ctx context.Context) error {
|
2021-02-03 06:54:49 +00:00
|
|
|
m.cfg.AutoloopTicker.Resume()
|
|
|
|
defer m.cfg.AutoloopTicker.Stop()
|
2020-10-12 11:34:55 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2021-02-03 06:54:49 +00:00
|
|
|
case <-m.cfg.AutoloopTicker.Ticks():
|
2021-02-08 07:38:23 +00:00
|
|
|
err := m.autoloop(ctx)
|
|
|
|
switch err {
|
|
|
|
case ErrNoRules:
|
|
|
|
log.Debugf("No rules configured for autoloop")
|
|
|
|
|
|
|
|
case nil:
|
|
|
|
|
|
|
|
default:
|
2020-10-12 11:34:55 +00:00
|
|
|
log.Errorf("autoloop failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:58:08 +00:00
|
|
|
// NewManager creates a liquidity manager which has no rules set.
|
|
|
|
func NewManager(cfg *Config) *Manager {
|
|
|
|
return &Manager{
|
|
|
|
cfg: cfg,
|
2020-09-30 10:34:06 +00:00
|
|
|
params: defaultParameters,
|
2020-09-01 07:58:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetParameters returns a copy of our current parameters.
|
|
|
|
func (m *Manager) GetParameters() Parameters {
|
|
|
|
m.paramsLock.Lock()
|
|
|
|
defer m.paramsLock.Unlock()
|
|
|
|
|
|
|
|
return cloneParameters(m.params)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetParameters updates our current set of parameters if the new parameters
|
|
|
|
// provided are valid.
|
2020-12-01 09:18:31 +00:00
|
|
|
func (m *Manager) SetParameters(ctx context.Context, params Parameters) error {
|
2021-02-03 06:54:50 +00:00
|
|
|
restrictions, err := m.cfg.Restrictions(ctx, swap.TypeOut)
|
2020-12-01 09:18:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-16 11:31:51 +00:00
|
|
|
channels, err := m.cfg.Lnd.Client.ListChannels(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = params.validate(m.cfg.MinimumConfirmations, channels, restrictions)
|
2020-12-01 09:18:31 +00:00
|
|
|
if err != nil {
|
2020-09-01 07:58:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.paramsLock.Lock()
|
|
|
|
defer m.paramsLock.Unlock()
|
|
|
|
|
|
|
|
m.params = cloneParameters(params)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cloneParameters creates a deep clone of a parameters struct so that callers
|
|
|
|
// cannot mutate our parameters. Although our parameters struct itself is not
|
|
|
|
// a reference, we still need to clone the contents of maps.
|
|
|
|
func cloneParameters(params Parameters) Parameters {
|
2020-09-30 10:34:08 +00:00
|
|
|
paramCopy := params
|
|
|
|
paramCopy.ChannelRules = make(
|
|
|
|
map[lnwire.ShortChannelID]*ThresholdRule,
|
|
|
|
len(params.ChannelRules),
|
|
|
|
)
|
2020-09-01 07:58:08 +00:00
|
|
|
|
|
|
|
for channel, rule := range params.ChannelRules {
|
|
|
|
ruleCopy := *rule
|
|
|
|
paramCopy.ChannelRules[channel] = &ruleCopy
|
|
|
|
}
|
|
|
|
|
2021-02-16 11:31:51 +00:00
|
|
|
paramCopy.PeerRules = make(
|
|
|
|
map[route.Vertex]*ThresholdRule,
|
|
|
|
len(params.PeerRules),
|
|
|
|
)
|
|
|
|
|
|
|
|
for peer, rule := range params.PeerRules {
|
|
|
|
ruleCopy := *rule
|
|
|
|
paramCopy.PeerRules[peer] = &ruleCopy
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:58:08 +00:00
|
|
|
return paramCopy
|
|
|
|
}
|
2020-09-03 08:36:43 +00:00
|
|
|
|
2020-10-12 11:34:55 +00:00
|
|
|
// autoloop gets a set of suggested swaps and dispatches them automatically if
|
|
|
|
// we have automated looping enabled.
|
|
|
|
func (m *Manager) autoloop(ctx context.Context) error {
|
2021-02-08 07:39:02 +00:00
|
|
|
suggestion, err := m.SuggestSwaps(ctx, true)
|
2020-10-12 11:34:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-08 07:39:02 +00:00
|
|
|
for _, swap := range suggestion.OutSwaps {
|
2021-02-03 06:54:51 +00:00
|
|
|
// If we don't actually have dispatch of swaps enabled, log
|
|
|
|
// suggestions.
|
|
|
|
if !m.params.Autoloop {
|
|
|
|
log.Debugf("recommended autoloop: %v sats over "+
|
|
|
|
"%v", swap.Amount, swap.OutgoingChanSet)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-10-12 11:34:55 +00:00
|
|
|
// Create a copy of our range var so that we can reference it.
|
|
|
|
swap := swap
|
|
|
|
loopOut, err := m.cfg.LoopOut(ctx, &swap)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("loop out automatically dispatched: hash: %v, "+
|
|
|
|
"address: %v", loopOut.SwapHash,
|
|
|
|
loopOut.HtlcAddressP2WSH)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-15 11:49:05 +00:00
|
|
|
// ForceAutoLoop force-ticks our auto-out ticker.
|
|
|
|
func (m *Manager) ForceAutoLoop(ctx context.Context) error {
|
|
|
|
select {
|
2021-02-03 06:54:49 +00:00
|
|
|
case m.cfg.AutoloopTicker.Force <- m.cfg.Clock.Now():
|
2020-10-15 11:49:05 +00:00
|
|
|
return nil
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 07:39:02 +00:00
|
|
|
// Suggestions provides a set of suggested swaps, and the set of channels that
|
|
|
|
// were excluded from consideration.
|
|
|
|
type Suggestions struct {
|
|
|
|
// OutSwaps is the set of loop out swaps that we suggest executing.
|
|
|
|
OutSwaps []loop.OutRequest
|
|
|
|
|
|
|
|
// DisqualifiedChans maps the set of channels that we do not recommend
|
|
|
|
// swaps on to the reason that we did not recommend a swap.
|
|
|
|
DisqualifiedChans map[lnwire.ShortChannelID]Reason
|
2021-02-16 11:31:51 +00:00
|
|
|
|
|
|
|
// Disqualified peers maps the set of peers that we do not recommend
|
|
|
|
// swaps for to the reason that they were excluded.
|
|
|
|
DisqualifiedPeers map[route.Vertex]Reason
|
2021-02-08 07:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newSuggestions() *Suggestions {
|
|
|
|
return &Suggestions{
|
|
|
|
DisqualifiedChans: make(map[lnwire.ShortChannelID]Reason),
|
2021-02-16 11:31:51 +00:00
|
|
|
DisqualifiedPeers: make(map[route.Vertex]Reason),
|
2021-02-08 07:39:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 11:31:50 +00:00
|
|
|
func (s *Suggestions) addSwap(swap swapSuggestion) error {
|
|
|
|
out, ok := swap.(*loopOutSwapSuggestion)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unexpected swap type: %T", swap)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.OutSwaps = append(s.OutSwaps, out.OutRequest)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-08 07:39:02 +00:00
|
|
|
// singleReasonSuggestion is a helper function which returns a set of
|
|
|
|
// suggestions where all of our rules are disqualified due to a reason that
|
|
|
|
// applies to all of them (such as being out of budget).
|
|
|
|
func (m *Manager) singleReasonSuggestion(reason Reason) *Suggestions {
|
|
|
|
resp := newSuggestions()
|
|
|
|
|
|
|
|
for id := range m.params.ChannelRules {
|
|
|
|
resp.DisqualifiedChans[id] = reason
|
|
|
|
}
|
|
|
|
|
2021-02-16 11:31:51 +00:00
|
|
|
for peer := range m.params.PeerRules {
|
|
|
|
resp.DisqualifiedPeers[peer] = reason
|
|
|
|
}
|
|
|
|
|
2021-02-08 07:39:02 +00:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2020-09-03 08:36:43 +00:00
|
|
|
// SuggestSwaps returns a set of swap suggestions based on our current liquidity
|
|
|
|
// balance for the set of rules configured for the manager, failing if there are
|
2021-02-03 06:54:47 +00:00
|
|
|
// no rules set. It takes an autoloop boolean that indicates whether the
|
2020-10-12 11:34:55 +00:00
|
|
|
// suggestions are being used for our internal autolooper. This boolean is used
|
|
|
|
// to determine the information we add to our swap suggestion and whether we
|
|
|
|
// return any suggestions.
|
2021-02-03 06:54:47 +00:00
|
|
|
func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
|
2021-02-08 07:39:02 +00:00
|
|
|
*Suggestions, error) {
|
2020-09-03 08:36:43 +00:00
|
|
|
|
|
|
|
m.paramsLock.Lock()
|
|
|
|
defer m.paramsLock.Unlock()
|
|
|
|
|
|
|
|
// If we have no rules set, exit early to avoid unnecessary calls to
|
|
|
|
// lnd and the server.
|
2021-02-16 11:31:51 +00:00
|
|
|
if !m.params.haveRules() {
|
2021-02-08 07:38:23 +00:00
|
|
|
return nil, ErrNoRules
|
2020-09-03 08:36:43 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 11:34:54 +00:00
|
|
|
// If our start date is in the future, we interpret this as meaning that
|
|
|
|
// we should start using our budget at this date. This means that we
|
|
|
|
// have no budget for the present, so we just return.
|
|
|
|
if m.params.AutoFeeStartDate.After(m.cfg.Clock.Now()) {
|
|
|
|
log.Debugf("autoloop fee budget start time: %v is in "+
|
|
|
|
"the future", m.params.AutoFeeStartDate)
|
|
|
|
|
2021-02-08 07:39:02 +00:00
|
|
|
return m.singleReasonSuggestion(ReasonBudgetNotStarted), nil
|
2020-10-12 11:34:54 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 10:34:08 +00:00
|
|
|
// Before we get any swap suggestions, we check what the current fee
|
|
|
|
// estimate is to sweep within our target number of confirmations. If
|
|
|
|
// This fee exceeds the fee limit we have set, we will not suggest any
|
|
|
|
// swaps at present.
|
|
|
|
estimate, err := m.cfg.Lnd.WalletKit.EstimateFee(
|
|
|
|
ctx, m.params.SweepConfTarget,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if estimate > m.params.SweepFeeRateLimit {
|
|
|
|
log.Debugf("Current fee estimate to sweep within: %v blocks "+
|
|
|
|
"%v sat/vByte exceeds limit of: %v sat/vByte",
|
|
|
|
m.params.SweepConfTarget,
|
|
|
|
satPerKwToSatPerVByte(estimate),
|
|
|
|
satPerKwToSatPerVByte(m.params.SweepFeeRateLimit))
|
|
|
|
|
2021-02-08 07:39:02 +00:00
|
|
|
return m.singleReasonSuggestion(ReasonSweepFees), nil
|
2020-09-30 10:34:08 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 09:18:31 +00:00
|
|
|
// Get the current server side restrictions, combined with the client
|
|
|
|
// set restrictions, if any.
|
2021-02-03 06:54:50 +00:00
|
|
|
restrictions, err := m.getSwapRestrictions(ctx, swap.TypeOut)
|
2020-09-03 08:36:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-30 10:34:07 +00:00
|
|
|
// List our current set of swaps so that we can determine which channels
|
|
|
|
// are already being utilized by swaps. Note that these calls may race
|
|
|
|
// with manual initiation of swaps.
|
|
|
|
loopOut, err := m.cfg.ListLoopOut()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
loopIn, err := m.cfg.ListLoopIn()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-12 11:34:54 +00:00
|
|
|
// Get a summary of our existing swaps so that we can check our autoloop
|
|
|
|
// budget.
|
|
|
|
summary, err := m.checkExistingAutoLoops(ctx, loopOut)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if summary.totalFees() >= m.params.AutoFeeBudget {
|
|
|
|
log.Debugf("autoloop fee budget: %v exhausted, %v spent on "+
|
|
|
|
"completed swaps, %v reserved for ongoing swaps "+
|
|
|
|
"(upper limit)",
|
|
|
|
m.params.AutoFeeBudget, summary.spentFees,
|
|
|
|
summary.pendingFees)
|
|
|
|
|
2021-02-08 07:39:02 +00:00
|
|
|
return m.singleReasonSuggestion(ReasonBudgetElapsed), nil
|
2020-10-12 11:34:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 11:34:54 +00:00
|
|
|
// If we have already reached our total allowed number of in flight
|
|
|
|
// swaps, we do not suggest any more at the moment.
|
|
|
|
allowedSwaps := m.params.MaxAutoInFlight - summary.inFlightCount
|
|
|
|
if allowedSwaps <= 0 {
|
|
|
|
log.Debugf("%v autoloops allowed, %v in flight",
|
|
|
|
m.params.MaxAutoInFlight, summary.inFlightCount)
|
2021-02-08 07:39:02 +00:00
|
|
|
|
|
|
|
return m.singleReasonSuggestion(ReasonInFlight), nil
|
2020-10-12 11:34:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 07:39:01 +00:00
|
|
|
channels, err := m.cfg.Lnd.Client.ListChannels(ctx)
|
2020-09-03 08:36:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-16 11:31:51 +00:00
|
|
|
peerChannels := make(map[route.Vertex]*balances)
|
|
|
|
for _, channel := range channels {
|
|
|
|
bal, ok := peerChannels[channel.PubKeyBytes]
|
|
|
|
if !ok {
|
|
|
|
bal = &balances{}
|
|
|
|
}
|
|
|
|
|
|
|
|
chanID := lnwire.NewShortChanIDFromInt(channel.ChannelID)
|
|
|
|
bal.channels = append(bal.channels, chanID)
|
|
|
|
bal.capacity += channel.Capacity
|
|
|
|
bal.incoming += channel.RemoteBalance
|
|
|
|
bal.outgoing += channel.LocalBalance
|
|
|
|
bal.pubkey = channel.PubKeyBytes
|
|
|
|
|
|
|
|
peerChannels[channel.PubKeyBytes] = bal
|
|
|
|
}
|
|
|
|
|
2021-02-08 07:39:01 +00:00
|
|
|
// Get a summary of the channels and peers that are not eligible due
|
|
|
|
// to ongoing swaps.
|
|
|
|
traffic := m.currentSwapTraffic(loopOut, loopIn)
|
|
|
|
|
2021-02-08 07:39:02 +00:00
|
|
|
var (
|
2021-02-16 11:31:51 +00:00
|
|
|
suggestions []swapSuggestion
|
|
|
|
resp = newSuggestions()
|
2021-02-08 07:39:02 +00:00
|
|
|
)
|
2021-02-08 07:39:01 +00:00
|
|
|
|
2021-02-16 11:31:51 +00:00
|
|
|
for peer, balances := range peerChannels {
|
|
|
|
rule, haveRule := m.params.PeerRules[peer]
|
|
|
|
if !haveRule {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
suggestion, err := m.suggestSwap(
|
|
|
|
ctx, traffic, balances, rule, restrictions, autoloop,
|
|
|
|
)
|
|
|
|
var reasonErr *reasonError
|
|
|
|
if errors.As(err, &reasonErr) {
|
|
|
|
resp.DisqualifiedPeers[peer] = reasonErr.reason
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
suggestions = append(suggestions, suggestion)
|
|
|
|
}
|
|
|
|
|
2021-02-08 07:39:01 +00:00
|
|
|
for _, channel := range channels {
|
|
|
|
balance := newBalances(channel)
|
|
|
|
|
2021-02-16 11:31:51 +00:00
|
|
|
channelID := lnwire.NewShortChanIDFromInt(channel.ChannelID)
|
|
|
|
rule, ok := m.params.ChannelRules[channelID]
|
2020-09-03 08:36:43 +00:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-02-16 11:31:49 +00:00
|
|
|
suggestion, err := m.suggestSwap(
|
|
|
|
ctx, traffic, balance, rule, restrictions, autoloop,
|
2020-09-30 10:34:09 +00:00
|
|
|
)
|
|
|
|
|
2021-02-16 11:31:49 +00:00
|
|
|
var reasonErr *reasonError
|
|
|
|
if errors.As(err, &reasonErr) {
|
2021-02-16 11:31:51 +00:00
|
|
|
resp.DisqualifiedChans[channelID] = reasonErr.reason
|
2020-09-30 10:34:09 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-10-12 11:34:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-16 11:31:49 +00:00
|
|
|
|
2021-02-16 11:31:50 +00:00
|
|
|
suggestions = append(suggestions, suggestion)
|
2020-09-03 08:36:43 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 07:39:02 +00:00
|
|
|
// If we have no swaps to execute after we have applied all of our
|
|
|
|
// limits, just return our set of disqualified swaps.
|
2020-10-12 11:34:54 +00:00
|
|
|
if len(suggestions) == 0 {
|
2021-02-08 07:39:02 +00:00
|
|
|
return resp, nil
|
2020-10-12 11:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sort suggestions by amount in descending order.
|
|
|
|
sort.SliceStable(suggestions, func(i, j int) bool {
|
2021-02-16 11:31:50 +00:00
|
|
|
return suggestions[i].amount() > suggestions[j].amount()
|
2020-10-12 11:34:54 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Run through our suggested swaps in descending order of amount and
|
|
|
|
// return all of the swaps which will fit within our remaining budget.
|
2021-02-08 07:39:02 +00:00
|
|
|
available := m.params.AutoFeeBudget - summary.totalFees()
|
|
|
|
|
|
|
|
// setReason is a helper that adds a swap's channels to our disqualified
|
|
|
|
// list with the reason provided.
|
2021-02-16 11:31:50 +00:00
|
|
|
setReason := func(reason Reason, swap swapSuggestion) {
|
|
|
|
for _, channel := range swap.channels() {
|
|
|
|
_, ok := m.params.ChannelRules[channel]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2021-02-08 07:39:02 +00:00
|
|
|
|
2021-02-16 11:31:50 +00:00
|
|
|
resp.DisqualifiedChans[channel] = reason
|
2021-02-08 07:39:02 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-12 11:34:54 +00:00
|
|
|
|
|
|
|
for _, swap := range suggestions {
|
2021-02-08 07:39:02 +00:00
|
|
|
swap := swap
|
|
|
|
|
|
|
|
// If we do not have enough funds available, or we hit our
|
|
|
|
// in flight limit, we record this value for the rest of the
|
|
|
|
// swaps.
|
|
|
|
var reason Reason
|
|
|
|
switch {
|
|
|
|
case available == 0:
|
|
|
|
reason = ReasonBudgetInsufficient
|
|
|
|
|
|
|
|
case len(resp.OutSwaps) == allowedSwaps:
|
|
|
|
reason = ReasonInFlight
|
|
|
|
}
|
|
|
|
|
|
|
|
if reason != ReasonNone {
|
|
|
|
setReason(reason, swap)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-02-16 11:31:50 +00:00
|
|
|
fees := swap.fees()
|
2020-10-12 11:34:54 +00:00
|
|
|
|
|
|
|
// If the maximum fee we expect our swap to use is less than the
|
|
|
|
// amount we have available, we add it to our set of swaps that
|
|
|
|
// fall within the budget and decrement our available amount.
|
|
|
|
if fees <= available {
|
|
|
|
available -= fees
|
2021-02-16 11:31:50 +00:00
|
|
|
|
|
|
|
if err := resp.addSwap(swap); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-08 07:39:02 +00:00
|
|
|
} else {
|
|
|
|
setReason(ReasonBudgetInsufficient, swap)
|
2020-10-12 11:34:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 07:39:02 +00:00
|
|
|
return resp, nil
|
2020-09-03 08:36:43 +00:00
|
|
|
}
|
2020-09-30 10:34:02 +00:00
|
|
|
|
2021-02-16 11:31:49 +00:00
|
|
|
// suggestSwap checks whether we can currently perform a swap, and creates a
|
|
|
|
// swap request for the rule provided.
|
|
|
|
func (m *Manager) suggestSwap(ctx context.Context, traffic *swapTraffic,
|
|
|
|
balance *balances, rule *ThresholdRule, restrictions *Restrictions,
|
2021-02-16 11:31:50 +00:00
|
|
|
autoloop bool) (swapSuggestion, error) {
|
2021-02-16 11:31:49 +00:00
|
|
|
|
|
|
|
// Check whether we can perform a swap.
|
2021-02-16 11:31:51 +00:00
|
|
|
err := traffic.maySwap(balance.pubkey, balance.channels)
|
2021-02-16 11:31:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can have nil suggestions in the case where no action is
|
|
|
|
// required, so we skip over them.
|
|
|
|
amount := rule.swapAmount(balance, restrictions)
|
|
|
|
if amount == 0 {
|
|
|
|
return nil, newReasonError(ReasonLiquidityOk)
|
|
|
|
}
|
|
|
|
|
2021-02-16 11:31:50 +00:00
|
|
|
swap, err := m.loopOutSwap(ctx, amount, balance, autoloop)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &loopOutSwapSuggestion{
|
|
|
|
OutRequest: *swap,
|
|
|
|
}, nil
|
2021-02-16 11:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// loopOutSwap creates a loop out swap with the amount provided for the balance
|
|
|
|
// described by the balance set provided. A reason that indicates whether we
|
|
|
|
// can swap is returned. If this value is not ReasonNone, there is no possible
|
|
|
|
// swap and the loop out request returned will be nil.
|
|
|
|
func (m *Manager) loopOutSwap(ctx context.Context, amount btcutil.Amount,
|
|
|
|
balance *balances, autoloop bool) (*loop.OutRequest, error) {
|
|
|
|
|
|
|
|
quote, err := m.cfg.LoopOutQuote(
|
|
|
|
ctx, &loop.LoopOutQuoteRequest{
|
|
|
|
Amount: amount,
|
|
|
|
SweepConfTarget: m.params.SweepConfTarget,
|
|
|
|
SwapPublicationDeadline: m.cfg.Clock.Now(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("quote for suggestion: %v, swap fee: %v, "+
|
|
|
|
"miner fee: %v, prepay: %v", amount, quote.SwapFee,
|
|
|
|
quote.MinerFee, quote.PrepayAmount)
|
|
|
|
|
|
|
|
// Check that the estimated fees for the suggested swap are
|
|
|
|
// below the fee limits configured by the manager.
|
|
|
|
feeReason := m.checkFeeLimits(quote, amount)
|
|
|
|
if feeReason != ReasonNone {
|
|
|
|
return nil, newReasonError(feeReason)
|
|
|
|
}
|
|
|
|
|
|
|
|
outRequest, err := m.makeLoopOutRequest(
|
|
|
|
ctx, amount, balance, quote, autoloop,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &outRequest, nil
|
|
|
|
}
|
|
|
|
|
2021-02-03 06:54:50 +00:00
|
|
|
// getSwapRestrictions queries the server for its latest swap size restrictions,
|
|
|
|
// validates client restrictions (if present) against these values and merges
|
|
|
|
// the client's custom requirements with the server's limits to produce a single
|
|
|
|
// set of limitations for our swap.
|
|
|
|
func (m *Manager) getSwapRestrictions(ctx context.Context, swapType swap.Type) (
|
|
|
|
*Restrictions, error) {
|
2020-12-01 09:18:31 +00:00
|
|
|
|
2021-02-03 06:54:50 +00:00
|
|
|
restrictions, err := m.cfg.Restrictions(ctx, swapType)
|
2020-12-01 09:18:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// It is possible that the server has updated its restrictions since
|
|
|
|
// we validated our client restrictions, so we validate again to ensure
|
|
|
|
// that our restrictions are within the server's bounds.
|
|
|
|
err = validateRestrictions(restrictions, &m.params.ClientRestrictions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If our minimum is more than the server's minimum, we set it.
|
|
|
|
if m.params.ClientRestrictions.Minimum > restrictions.Minimum {
|
|
|
|
restrictions.Minimum = m.params.ClientRestrictions.Minimum
|
|
|
|
}
|
|
|
|
|
|
|
|
// If our maximum set and is less than the server's maximum, we set it.
|
|
|
|
if m.params.ClientRestrictions.Maximum != 0 &&
|
|
|
|
m.params.ClientRestrictions.Maximum < restrictions.Maximum {
|
|
|
|
|
|
|
|
restrictions.Maximum = m.params.ClientRestrictions.Maximum
|
|
|
|
}
|
|
|
|
|
|
|
|
return restrictions, nil
|
|
|
|
}
|
|
|
|
|
2020-09-30 10:34:09 +00:00
|
|
|
// makeLoopOutRequest creates a loop out request from a suggestion. Since we
|
|
|
|
// do not get any information about our off-chain routing fees when we request
|
|
|
|
// a quote, we just set our prepay and route maximum fees directly from the
|
|
|
|
// amounts we expect to route. The estimation we use elsewhere is the repo is
|
|
|
|
// route-independent, which is a very poor estimation so we don't bother with
|
|
|
|
// checking against this inaccurate constant. We use the exact prepay amount
|
|
|
|
// and swap fee given to us by the server, but use our maximum miner fee anyway
|
2020-10-12 11:34:55 +00:00
|
|
|
// to give us some leeway when performing the swap. We take an auto-out which
|
|
|
|
// determines whether we set a label identifying this swap as automatically
|
|
|
|
// dispatched, and decides whether we set a sweep address (we don't bother for
|
|
|
|
// non-auto requests, because the client api will set it anyway).
|
|
|
|
func (m *Manager) makeLoopOutRequest(ctx context.Context,
|
2021-02-16 11:31:48 +00:00
|
|
|
amount btcutil.Amount, balance *balances, quote *loop.LoopOutQuote,
|
2021-02-03 06:54:47 +00:00
|
|
|
autoloop bool) (loop.OutRequest, error) {
|
2020-09-30 10:34:09 +00:00
|
|
|
|
2020-09-30 10:34:02 +00:00
|
|
|
prepayMaxFee := ppmToSat(
|
2020-09-30 10:34:09 +00:00
|
|
|
quote.PrepayAmount, m.params.MaximumPrepayRoutingFeePPM,
|
2020-09-30 10:34:02 +00:00
|
|
|
)
|
|
|
|
|
2021-02-16 11:31:48 +00:00
|
|
|
routeMaxFee := ppmToSat(amount, m.params.MaximumRoutingFeePPM)
|
2020-09-30 10:34:02 +00:00
|
|
|
|
2021-02-16 11:31:51 +00:00
|
|
|
var chanSet loopdb.ChannelSet
|
|
|
|
for _, channel := range balance.channels {
|
|
|
|
chanSet = append(chanSet, channel.ToUint64())
|
|
|
|
}
|
|
|
|
|
2020-10-12 11:34:55 +00:00
|
|
|
request := loop.OutRequest{
|
2021-02-16 11:31:51 +00:00
|
|
|
Amount: amount,
|
|
|
|
OutgoingChanSet: chanSet,
|
2020-09-30 10:34:02 +00:00
|
|
|
MaxPrepayRoutingFee: prepayMaxFee,
|
|
|
|
MaxSwapRoutingFee: routeMaxFee,
|
2020-09-30 10:34:09 +00:00
|
|
|
MaxMinerFee: m.params.MaximumMinerFee,
|
|
|
|
MaxSwapFee: quote.SwapFee,
|
|
|
|
MaxPrepayAmount: quote.PrepayAmount,
|
2020-09-30 10:34:08 +00:00
|
|
|
SweepConfTarget: m.params.SweepConfTarget,
|
2020-11-06 09:43:05 +00:00
|
|
|
Initiator: autoloopSwapInitiator,
|
2020-09-30 10:34:02 +00:00
|
|
|
}
|
2020-10-12 11:34:55 +00:00
|
|
|
|
2021-02-03 06:54:47 +00:00
|
|
|
if autoloop {
|
2021-02-03 06:54:50 +00:00
|
|
|
request.Label = labels.AutoloopLabel(swap.TypeOut)
|
2020-10-12 11:34:55 +00:00
|
|
|
|
|
|
|
addr, err := m.cfg.Lnd.WalletKit.NextAddr(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return loop.OutRequest{}, err
|
|
|
|
}
|
|
|
|
request.DestAddr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
return request, nil
|
2020-09-30 10:34:02 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 11:34:54 +00:00
|
|
|
// worstCaseOutFees calculates the largest possible fees for a loop out swap,
|
|
|
|
// comparing the fees for a successful swap to the cost when the client pays
|
|
|
|
// the prepay because they failed to sweep the on chain htlc. This is unlikely,
|
|
|
|
// because we expect clients to be online to sweep, but we want to account for
|
|
|
|
// every outcome so we include it.
|
|
|
|
func worstCaseOutFees(prepayRouting, swapRouting, swapFee, minerFee,
|
|
|
|
prepayAmount btcutil.Amount) btcutil.Amount {
|
|
|
|
|
|
|
|
var (
|
|
|
|
successFees = prepayRouting + minerFee + swapFee + swapRouting
|
|
|
|
noShowFees = prepayRouting + prepayAmount
|
|
|
|
)
|
|
|
|
|
|
|
|
if noShowFees > successFees {
|
|
|
|
return noShowFees
|
|
|
|
}
|
|
|
|
|
|
|
|
return successFees
|
|
|
|
}
|
|
|
|
|
|
|
|
// existingAutoLoopSummary provides a summary of the existing autoloops which
|
|
|
|
// were dispatched during our current budget period.
|
|
|
|
type existingAutoLoopSummary struct {
|
|
|
|
// spentFees is the amount we have spent on completed swaps.
|
|
|
|
spentFees btcutil.Amount
|
|
|
|
|
|
|
|
// pendingFees is the worst-case amount of fees we could spend on in
|
|
|
|
// flight autoloops.
|
|
|
|
pendingFees btcutil.Amount
|
2020-10-12 11:34:54 +00:00
|
|
|
|
|
|
|
// inFlightCount is the total number of automated swaps that are
|
|
|
|
// currently in flight. Note that this may race with swap completion,
|
|
|
|
// but not with initiation of new automated swaps, this is ok, because
|
|
|
|
// it can only lead to dispatching fewer swaps than we could have (not
|
|
|
|
// too many).
|
|
|
|
inFlightCount int
|
2020-10-12 11:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// totalFees returns the total amount of fees that automatically dispatched
|
|
|
|
// swaps may consume.
|
|
|
|
func (e *existingAutoLoopSummary) totalFees() btcutil.Amount {
|
|
|
|
return e.spentFees + e.pendingFees
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkExistingAutoLoops calculates the total amount that has been spent by
|
|
|
|
// automatically dispatched swaps that have completed, and the worst-case fee
|
2020-10-12 11:34:54 +00:00
|
|
|
// total for our set of ongoing, automatically dispatched swaps as well as a
|
|
|
|
// current in-flight count.
|
2020-10-12 11:34:54 +00:00
|
|
|
func (m *Manager) checkExistingAutoLoops(ctx context.Context,
|
|
|
|
loopOuts []*loopdb.LoopOut) (*existingAutoLoopSummary, error) {
|
|
|
|
|
|
|
|
var summary existingAutoLoopSummary
|
|
|
|
|
|
|
|
for _, out := range loopOuts {
|
2021-02-03 06:54:50 +00:00
|
|
|
if out.Contract.Label != labels.AutoloopLabel(swap.TypeOut) {
|
2020-10-12 11:34:54 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a pending swap, we are uncertain of the fees that
|
|
|
|
// it will end up paying. We use the worst-case estimate based
|
|
|
|
// on the maximum values we set for each fee category. This will
|
|
|
|
// likely over-estimate our fees (because we probably won't
|
|
|
|
// spend our maximum miner amount). If a swap is not pending,
|
|
|
|
// it has succeeded or failed so we just record our actual fees
|
|
|
|
// for the swap provided that the swap completed after our
|
|
|
|
// budget start date.
|
|
|
|
if out.State().State.Type() == loopdb.StateTypePending {
|
2020-10-12 11:34:54 +00:00
|
|
|
summary.inFlightCount++
|
|
|
|
|
2020-10-12 11:34:54 +00:00
|
|
|
prepay, err := m.cfg.Lnd.Client.DecodePaymentRequest(
|
|
|
|
ctx, out.Contract.PrepayInvoice,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
summary.pendingFees += worstCaseOutFees(
|
|
|
|
out.Contract.MaxPrepayRoutingFee,
|
|
|
|
out.Contract.MaxSwapRoutingFee,
|
|
|
|
out.Contract.MaxSwapFee,
|
|
|
|
out.Contract.MaxMinerFee,
|
|
|
|
mSatToSatoshis(prepay.Value),
|
|
|
|
)
|
|
|
|
} else if !out.LastUpdateTime().Before(m.params.AutoFeeStartDate) {
|
|
|
|
summary.spentFees += out.State().Cost.Total()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &summary, nil
|
|
|
|
}
|
|
|
|
|
2021-02-08 07:39:01 +00:00
|
|
|
// currentSwapTraffic examines our existing swaps and returns a summary of the
|
|
|
|
// current activity which can be used to determine whether we should perform
|
|
|
|
// any swaps.
|
|
|
|
func (m *Manager) currentSwapTraffic(loopOut []*loopdb.LoopOut,
|
|
|
|
loopIn []*loopdb.LoopIn) *swapTraffic {
|
2020-09-30 10:34:07 +00:00
|
|
|
|
2021-02-08 07:39:01 +00:00
|
|
|
traffic := newSwapTraffic()
|
2020-09-30 10:34:07 +00:00
|
|
|
|
2020-09-30 10:34:08 +00:00
|
|
|
// Failure cutoff is the most recent failure timestamp we will still
|
|
|
|
// consider a channel eligible. Any channels involved in swaps that have
|
|
|
|
// failed since this point will not be considered.
|
|
|
|
failureCutoff := m.cfg.Clock.Now().Add(m.params.FailureBackOff * -1)
|
|
|
|
|
2020-09-30 10:34:07 +00:00
|
|
|
for _, out := range loopOut {
|
|
|
|
var (
|
|
|
|
state = out.State().State
|
|
|
|
chanSet = out.Contract.OutgoingChanSet
|
|
|
|
)
|
|
|
|
|
2020-09-30 10:34:08 +00:00
|
|
|
// If a loop out swap failed due to off chain payment after our
|
|
|
|
// failure cutoff, we add all of its channels to a set of
|
|
|
|
// recently failed channels. It is possible that not all of
|
|
|
|
// these channels were used for the swap, but we play it safe
|
|
|
|
// and back off for all of them.
|
|
|
|
//
|
|
|
|
// We only backoff for off temporary failures. In the case of
|
|
|
|
// chain payment failures, our swap failed to route and we do
|
|
|
|
// not want to repeatedly try to route through bad channels
|
|
|
|
// which remain unbalanced because they cannot route a swap, so
|
|
|
|
// we backoff.
|
|
|
|
if state == loopdb.StateFailOffchainPayments {
|
|
|
|
failedAt := out.LastUpdate().Time
|
|
|
|
|
|
|
|
if failedAt.After(failureCutoff) {
|
|
|
|
for _, id := range chanSet {
|
|
|
|
chanID := lnwire.NewShortChanIDFromInt(
|
|
|
|
id,
|
|
|
|
)
|
|
|
|
|
2021-02-08 07:39:01 +00:00
|
|
|
traffic.failedLoopOut[chanID] = failedAt
|
2020-09-30 10:34:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 10:34:07 +00:00
|
|
|
// Skip completed swaps, they can't affect our channel balances.
|
2020-09-30 10:34:08 +00:00
|
|
|
// Swaps that fail temporarily are considered to be in a pending
|
|
|
|
// state, so we will also check that channels being used by
|
|
|
|
// these swaps. This is important, because a temporarily failed
|
|
|
|
// swap could be re-dispatched on restart, affecting our
|
|
|
|
// balances.
|
2020-09-30 10:34:07 +00:00
|
|
|
if state.Type() != loopdb.StateTypePending {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range chanSet {
|
|
|
|
chanID := lnwire.NewShortChanIDFromInt(id)
|
2021-02-08 07:39:01 +00:00
|
|
|
traffic.ongoingLoopOut[chanID] = true
|
2020-09-30 10:34:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, in := range loopIn {
|
|
|
|
// Skip completed swaps, they can't affect our channel balances.
|
|
|
|
if in.State().State.Type() != loopdb.StateTypePending {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-02-08 07:38:59 +00:00
|
|
|
// Skip over swaps that may come through any peer.
|
2020-09-30 10:34:07 +00:00
|
|
|
if in.Contract.LastHop == nil {
|
2021-02-08 07:38:59 +00:00
|
|
|
continue
|
2020-09-30 10:34:07 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 07:39:01 +00:00
|
|
|
traffic.ongoingLoopIn[*in.Contract.LastHop] = true
|
2020-09-30 10:34:07 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 07:39:01 +00:00
|
|
|
return traffic
|
|
|
|
}
|
2020-09-30 10:34:07 +00:00
|
|
|
|
2021-02-08 07:39:01 +00:00
|
|
|
// swapTraffic contains a summary of our current and previously failed swaps.
|
|
|
|
type swapTraffic struct {
|
|
|
|
ongoingLoopOut map[lnwire.ShortChannelID]bool
|
|
|
|
ongoingLoopIn map[route.Vertex]bool
|
|
|
|
failedLoopOut map[lnwire.ShortChannelID]time.Time
|
|
|
|
}
|
2020-09-30 10:34:07 +00:00
|
|
|
|
2021-02-08 07:39:01 +00:00
|
|
|
func newSwapTraffic() *swapTraffic {
|
|
|
|
return &swapTraffic{
|
|
|
|
ongoingLoopOut: make(map[lnwire.ShortChannelID]bool),
|
|
|
|
ongoingLoopIn: make(map[route.Vertex]bool),
|
|
|
|
failedLoopOut: make(map[lnwire.ShortChannelID]time.Time),
|
|
|
|
}
|
|
|
|
}
|
2020-09-30 10:34:08 +00:00
|
|
|
|
2021-02-08 07:39:01 +00:00
|
|
|
// maySwap returns a boolean that indicates whether we may perform a swap for a
|
|
|
|
// peer and its set of channels.
|
|
|
|
func (s *swapTraffic) maySwap(peer route.Vertex,
|
2021-02-16 11:31:51 +00:00
|
|
|
channels []lnwire.ShortChannelID) error {
|
2020-09-30 10:34:08 +00:00
|
|
|
|
2021-02-16 11:31:51 +00:00
|
|
|
for _, chanID := range channels {
|
|
|
|
lastFail, recentFail := s.failedLoopOut[chanID]
|
|
|
|
if recentFail {
|
|
|
|
log.Debugf("Channel: %v not eligible for suggestions, was "+
|
|
|
|
"part of a failed swap at: %v", chanID, lastFail)
|
2020-09-30 10:34:07 +00:00
|
|
|
|
2021-02-16 11:31:51 +00:00
|
|
|
return newReasonError(ReasonFailureBackoff)
|
|
|
|
}
|
2020-09-30 10:34:07 +00:00
|
|
|
|
2021-02-16 11:31:51 +00:00
|
|
|
if s.ongoingLoopOut[chanID] {
|
|
|
|
log.Debugf("Channel: %v not eligible for suggestions, "+
|
|
|
|
"ongoing loop out utilizing channel", chanID)
|
2020-09-30 10:34:07 +00:00
|
|
|
|
2021-02-16 11:31:51 +00:00
|
|
|
return newReasonError(ReasonLoopOut)
|
|
|
|
}
|
2021-02-08 07:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if s.ongoingLoopIn[peer] {
|
|
|
|
log.Debugf("Peer: %x not eligible for suggestions ongoing "+
|
|
|
|
"loop in utilizing peer", peer)
|
2020-09-30 10:34:07 +00:00
|
|
|
|
2021-02-16 11:31:49 +00:00
|
|
|
return newReasonError(ReasonLoopIn)
|
2020-09-30 10:34:07 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 11:31:49 +00:00
|
|
|
return nil
|
2020-09-30 10:34:07 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 10:34:09 +00:00
|
|
|
// checkFeeLimits takes a set of fees for a swap and checks whether they exceed
|
|
|
|
// our swap limits.
|
|
|
|
func (m *Manager) checkFeeLimits(quote *loop.LoopOutQuote,
|
2021-02-08 07:39:02 +00:00
|
|
|
swapAmt btcutil.Amount) Reason {
|
2020-09-30 10:34:09 +00:00
|
|
|
|
|
|
|
maxFee := ppmToSat(swapAmt, m.params.MaximumSwapFeePPM)
|
|
|
|
|
|
|
|
if quote.SwapFee > maxFee {
|
2021-02-08 07:39:02 +00:00
|
|
|
log.Debugf("quoted swap fee: %v > maximum swap fee: %v",
|
2020-09-30 10:34:09 +00:00
|
|
|
quote.SwapFee, maxFee)
|
2021-02-08 07:39:02 +00:00
|
|
|
|
|
|
|
return ReasonSwapFee
|
2020-09-30 10:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if quote.MinerFee > m.params.MaximumMinerFee {
|
2021-02-08 07:39:02 +00:00
|
|
|
log.Debugf("quoted miner fee: %v > maximum miner "+
|
2020-09-30 10:34:09 +00:00
|
|
|
"fee: %v", quote.MinerFee, m.params.MaximumMinerFee)
|
2021-02-08 07:39:02 +00:00
|
|
|
|
|
|
|
return ReasonMinerFee
|
2020-09-30 10:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if quote.PrepayAmount > m.params.MaximumPrepay {
|
2021-02-08 07:39:02 +00:00
|
|
|
log.Debugf("quoted prepay: %v > maximum prepay: %v",
|
2020-09-30 10:34:09 +00:00
|
|
|
quote.PrepayAmount, m.params.MaximumPrepay)
|
2021-02-08 07:39:02 +00:00
|
|
|
|
|
|
|
return ReasonPrepay
|
2020-09-30 10:34:09 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 07:39:02 +00:00
|
|
|
return ReasonNone
|
2020-09-30 10:34:09 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 10:34:08 +00:00
|
|
|
// satPerKwToSatPerVByte converts sat per kWeight to sat per vByte.
|
|
|
|
func satPerKwToSatPerVByte(satPerKw chainfee.SatPerKWeight) int64 {
|
|
|
|
return int64(satPerKw.FeePerKVByte() / 1000)
|
|
|
|
}
|
|
|
|
|
2020-09-30 10:34:02 +00:00
|
|
|
// ppmToSat takes an amount and a measure of parts per million for the amount
|
|
|
|
// and returns the amount that the ppm represents.
|
|
|
|
func ppmToSat(amount btcutil.Amount, ppm int) btcutil.Amount {
|
|
|
|
return btcutil.Amount(uint64(amount) * uint64(ppm) / FeeBase)
|
|
|
|
}
|
2020-10-12 11:34:54 +00:00
|
|
|
|
|
|
|
func mSatToSatoshis(amount lnwire.MilliSatoshi) btcutil.Amount {
|
|
|
|
return btcutil.Amount(amount / 1000)
|
|
|
|
}
|