2020-01-03 13:01:31 +00:00
|
|
|
package loopd
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-10 14:55:53 +00:00
|
|
|
"encoding/hex"
|
2019-03-12 15:10:37 +00:00
|
|
|
"errors"
|
2019-03-06 20:13:50 +00:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
2020-01-31 12:57:21 +00:00
|
|
|
"sync"
|
2019-11-14 09:35:32 +00:00
|
|
|
"time"
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2021-05-10 14:55:53 +00:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2021-03-08 08:52:57 +00:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
2020-09-01 07:58:09 +00:00
|
|
|
"github.com/btcsuite/btcutil"
|
2020-06-17 20:25:57 +00:00
|
|
|
"github.com/lightninglabs/lndclient"
|
2019-03-07 04:32:24 +00:00
|
|
|
"github.com/lightninglabs/loop"
|
2020-10-12 11:34:52 +00:00
|
|
|
"github.com/lightninglabs/loop/labels"
|
2020-09-01 07:58:09 +00:00
|
|
|
"github.com/lightninglabs/loop/liquidity"
|
2019-03-07 04:32:24 +00:00
|
|
|
"github.com/lightninglabs/loop/loopdb"
|
2021-12-13 13:28:38 +00:00
|
|
|
clientrpc "github.com/lightninglabs/loop/looprpc"
|
2020-09-01 07:58:09 +00:00
|
|
|
"github.com/lightninglabs/loop/swap"
|
2021-12-13 13:31:37 +00:00
|
|
|
looprpc "github.com/lightninglabs/loop/swapserverrpc"
|
2020-09-01 07:58:09 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2020-09-30 10:34:10 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
2020-09-01 07:58:09 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
"github.com/lightningnetwork/lnd/queue"
|
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
2021-05-10 14:55:53 +00:00
|
|
|
"github.com/lightningnetwork/lnd/zpay32"
|
2021-02-08 07:38:23 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
2019-06-25 18:41:45 +00:00
|
|
|
const (
|
|
|
|
completedSwapsCount = 5
|
|
|
|
|
|
|
|
// minConfTarget is the minimum confirmation target we'll allow clients
|
|
|
|
// to specify. This is driven by the minimum confirmation target allowed
|
|
|
|
// by the backing fee estimator.
|
|
|
|
minConfTarget = 2
|
|
|
|
)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2021-03-08 07:50:17 +00:00
|
|
|
var (
|
2021-03-08 08:52:57 +00:00
|
|
|
// errIncorrectChain is returned when the format of the
|
|
|
|
// destination address provided does not match the active chain.
|
|
|
|
errIncorrectChain = errors.New("invalid address format for the " +
|
|
|
|
"active chain")
|
|
|
|
|
2021-03-08 07:50:17 +00:00
|
|
|
// errConfTargetTooLow is returned when the chosen confirmation target
|
|
|
|
// is below the allowed minimum.
|
|
|
|
errConfTargetTooLow = errors.New("confirmation target too low")
|
2021-03-11 10:20:11 +00:00
|
|
|
|
|
|
|
// errBalanceTooLow is returned when the loop out amount can't be
|
|
|
|
// satisfied given total balance of the selection of channels to loop
|
|
|
|
// out on.
|
|
|
|
errBalanceTooLow = errors.New(
|
|
|
|
"channel balance too low for loop out amount",
|
|
|
|
)
|
2021-03-08 07:50:17 +00:00
|
|
|
)
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
// swapClientServer implements the grpc service exposed by loopd.
|
2019-03-06 20:13:50 +00:00
|
|
|
type swapClientServer struct {
|
2021-07-29 11:32:53 +00:00
|
|
|
// Required by the grpc-gateway/v2 library for forward compatibility.
|
2021-12-13 13:28:38 +00:00
|
|
|
clientrpc.UnimplementedSwapClientServer
|
|
|
|
clientrpc.UnimplementedDebugServer
|
2021-07-29 11:32:53 +00:00
|
|
|
|
2020-10-15 11:49:05 +00:00
|
|
|
network lndclient.Network
|
2020-01-31 12:57:21 +00:00
|
|
|
impl *loop.Client
|
2020-09-01 07:58:08 +00:00
|
|
|
liquidityMgr *liquidity.Manager
|
2020-01-31 12:57:21 +00:00
|
|
|
lnd *lndclient.LndServices
|
|
|
|
swaps map[lntypes.Hash]loop.SwapInfo
|
|
|
|
subscribers map[int]chan<- interface{}
|
|
|
|
statusChan chan loop.SwapInfo
|
|
|
|
nextSubscriberID int
|
|
|
|
swapsLock sync.Mutex
|
2020-05-15 10:17:59 +00:00
|
|
|
mainCtx context.Context
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
// LoopOut initiates an loop out swap with the given parameters. The call
|
2019-03-06 20:13:50 +00:00
|
|
|
// returns after the swap has been set up with the swap server. From that point
|
2019-03-07 04:32:24 +00:00
|
|
|
// onwards, progress can be tracked via the LoopOutStatus stream that is
|
2019-03-06 20:13:50 +00:00
|
|
|
// returned from Monitor().
|
2019-03-07 04:32:24 +00:00
|
|
|
func (s *swapClientServer) LoopOut(ctx context.Context,
|
2021-12-13 13:28:38 +00:00
|
|
|
in *clientrpc.LoopOutRequest) (
|
|
|
|
*clientrpc.SwapResponse, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Infof("Loop out request received")
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
var sweepAddr btcutil.Address
|
|
|
|
if in.Dest == "" {
|
|
|
|
// Generate sweep address if none specified.
|
|
|
|
var err error
|
|
|
|
sweepAddr, err = s.lnd.WalletKit.NextAddr(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("NextAddr error: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var err error
|
2019-03-25 10:06:16 +00:00
|
|
|
sweepAddr, err = btcutil.DecodeAddress(
|
|
|
|
in.Dest, s.lnd.ChainParams,
|
|
|
|
)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("decode address: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 08:52:57 +00:00
|
|
|
sweepConfTarget, err := validateLoopOutRequest(
|
2021-03-11 10:20:11 +00:00
|
|
|
ctx, s.lnd.Client, s.lnd.ChainParams, in, sweepAddr,
|
|
|
|
s.impl.LoopOutMaxParts,
|
2021-03-08 08:52:57 +00:00
|
|
|
)
|
2021-03-08 07:50:17 +00:00
|
|
|
if err != nil {
|
2020-10-12 11:34:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
req := &loop.OutRequest{
|
2019-03-06 20:13:50 +00:00
|
|
|
Amount: btcutil.Amount(in.Amt),
|
|
|
|
DestAddr: sweepAddr,
|
|
|
|
MaxMinerFee: btcutil.Amount(in.MaxMinerFee),
|
|
|
|
MaxPrepayAmount: btcutil.Amount(in.MaxPrepayAmt),
|
|
|
|
MaxPrepayRoutingFee: btcutil.Amount(in.MaxPrepayRoutingFee),
|
|
|
|
MaxSwapRoutingFee: btcutil.Amount(in.MaxSwapRoutingFee),
|
|
|
|
MaxSwapFee: btcutil.Amount(in.MaxSwapFee),
|
2019-06-25 18:41:49 +00:00
|
|
|
SweepConfTarget: sweepConfTarget,
|
2020-08-04 14:37:24 +00:00
|
|
|
HtlcConfirmations: in.HtlcConfirmations,
|
2019-11-14 09:35:32 +00:00
|
|
|
SwapPublicationDeadline: time.Unix(
|
|
|
|
int64(in.SwapPublicationDeadline), 0,
|
|
|
|
),
|
2020-11-06 09:43:04 +00:00
|
|
|
Label: in.Label,
|
|
|
|
Initiator: in.Initiator,
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
2020-05-19 08:21:13 +00:00
|
|
|
|
|
|
|
switch {
|
2021-07-14 15:21:54 +00:00
|
|
|
case in.LoopOutChannel != 0 && len(in.OutgoingChanSet) > 0: // nolint:staticcheck
|
2020-05-19 08:21:13 +00:00
|
|
|
return nil, errors.New("loop_out_channel and outgoing_" +
|
|
|
|
"chan_ids are mutually exclusive")
|
|
|
|
|
2021-07-14 15:21:54 +00:00
|
|
|
case in.LoopOutChannel != 0: // nolint:staticcheck
|
|
|
|
req.OutgoingChanSet = loopdb.ChannelSet{in.LoopOutChannel} // nolint:staticcheck
|
2020-05-19 08:21:13 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
req.OutgoingChanSet = in.OutgoingChanSet
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
2020-05-19 08:21:13 +00:00
|
|
|
|
2020-06-30 11:45:12 +00:00
|
|
|
info, err := s.impl.LoopOut(ctx, req)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Errorf("LoopOut: %v", err)
|
2019-03-06 20:13:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 13:28:38 +00:00
|
|
|
return &clientrpc.SwapResponse{
|
2020-06-30 11:45:12 +00:00
|
|
|
Id: info.SwapHash.String(),
|
|
|
|
IdBytes: info.SwapHash[:],
|
|
|
|
HtlcAddress: info.HtlcAddressP2WSH.String(),
|
|
|
|
HtlcAddressP2Wsh: info.HtlcAddressP2WSH.String(),
|
2020-06-30 12:10:33 +00:00
|
|
|
ServerMessage: info.ServerMessage,
|
2019-03-06 20:13:50 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
func (s *swapClientServer) marshallSwap(loopSwap *loop.SwapInfo) (
|
2021-12-13 13:28:38 +00:00
|
|
|
*clientrpc.SwapStatus, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2020-07-30 07:09:27 +00:00
|
|
|
var (
|
2021-12-13 13:28:38 +00:00
|
|
|
state clientrpc.SwapState
|
|
|
|
failureReason = clientrpc.FailureReason_FAILURE_REASON_NONE
|
2020-07-30 07:09:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Set our state var for non-failure states. If we get a failure, we
|
|
|
|
// will update our failure reason. To remain backwards compatible with
|
|
|
|
// previous versions where we squashed all failure reasons to a single
|
|
|
|
// failure state, we set a failure reason for all our different failure
|
|
|
|
// states, and set our failed state for all of them.
|
2019-03-07 04:32:24 +00:00
|
|
|
switch loopSwap.State {
|
|
|
|
case loopdb.StateInitiated:
|
2021-12-13 13:28:38 +00:00
|
|
|
state = clientrpc.SwapState_INITIATED
|
2020-07-30 07:09:27 +00:00
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
case loopdb.StatePreimageRevealed:
|
2021-12-13 13:28:38 +00:00
|
|
|
state = clientrpc.SwapState_PREIMAGE_REVEALED
|
2020-07-30 07:09:27 +00:00
|
|
|
|
2019-03-12 15:10:37 +00:00
|
|
|
case loopdb.StateHtlcPublished:
|
2021-12-13 13:28:38 +00:00
|
|
|
state = clientrpc.SwapState_HTLC_PUBLISHED
|
2020-07-30 07:09:27 +00:00
|
|
|
|
2019-04-02 08:51:51 +00:00
|
|
|
case loopdb.StateInvoiceSettled:
|
2021-12-13 13:28:38 +00:00
|
|
|
state = clientrpc.SwapState_INVOICE_SETTLED
|
2020-07-30 07:09:27 +00:00
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
case loopdb.StateSuccess:
|
2021-12-13 13:28:38 +00:00
|
|
|
state = clientrpc.SwapState_SUCCESS
|
2020-07-30 07:09:27 +00:00
|
|
|
|
|
|
|
case loopdb.StateFailOffchainPayments:
|
2021-12-13 13:28:38 +00:00
|
|
|
failureReason = clientrpc.FailureReason_FAILURE_REASON_OFFCHAIN
|
2020-07-30 07:09:27 +00:00
|
|
|
|
|
|
|
case loopdb.StateFailTimeout:
|
2021-12-13 13:28:38 +00:00
|
|
|
failureReason = clientrpc.FailureReason_FAILURE_REASON_TIMEOUT
|
2020-07-30 07:09:27 +00:00
|
|
|
|
|
|
|
case loopdb.StateFailSweepTimeout:
|
2021-12-13 13:28:38 +00:00
|
|
|
failureReason = clientrpc.FailureReason_FAILURE_REASON_SWEEP_TIMEOUT
|
2020-07-30 07:09:27 +00:00
|
|
|
|
|
|
|
case loopdb.StateFailInsufficientValue:
|
2021-12-13 13:28:38 +00:00
|
|
|
failureReason = clientrpc.FailureReason_FAILURE_REASON_INSUFFICIENT_VALUE
|
2020-07-30 07:09:27 +00:00
|
|
|
|
|
|
|
case loopdb.StateFailTemporary:
|
2021-12-13 13:28:38 +00:00
|
|
|
failureReason = clientrpc.FailureReason_FAILURE_REASON_TEMPORARY
|
2020-07-30 07:09:27 +00:00
|
|
|
|
|
|
|
case loopdb.StateFailIncorrectHtlcAmt:
|
2021-12-13 13:28:38 +00:00
|
|
|
failureReason = clientrpc.FailureReason_FAILURE_REASON_INCORRECT_AMOUNT
|
2020-07-30 07:09:27 +00:00
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
default:
|
2020-07-30 07:09:27 +00:00
|
|
|
return nil, fmt.Errorf("unknown swap state: %v", loopSwap.State)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a failure reason, we have a failure state, so should use
|
|
|
|
// our catchall failed state.
|
2021-12-13 13:28:38 +00:00
|
|
|
if failureReason != clientrpc.FailureReason_FAILURE_REASON_NONE {
|
|
|
|
state = clientrpc.SwapState_FAILED
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 13:28:38 +00:00
|
|
|
var swapType clientrpc.SwapType
|
2020-04-24 15:35:32 +00:00
|
|
|
var htlcAddress, htlcAddressP2WSH, htlcAddressNP2WSH string
|
|
|
|
|
2019-03-12 15:10:37 +00:00
|
|
|
switch loopSwap.SwapType {
|
2019-10-09 10:36:16 +00:00
|
|
|
case swap.TypeIn:
|
2021-12-13 13:28:38 +00:00
|
|
|
swapType = clientrpc.SwapType_LOOP_IN
|
2020-04-28 15:30:20 +00:00
|
|
|
htlcAddressP2WSH = loopSwap.HtlcAddressP2WSH.EncodeAddress()
|
|
|
|
|
2020-04-24 15:35:32 +00:00
|
|
|
if loopSwap.ExternalHtlc {
|
2020-04-28 15:30:20 +00:00
|
|
|
htlcAddressNP2WSH = loopSwap.HtlcAddressNP2WSH.EncodeAddress()
|
|
|
|
htlcAddress = htlcAddressNP2WSH
|
|
|
|
} else {
|
|
|
|
htlcAddress = htlcAddressP2WSH
|
2020-04-24 15:35:32 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 10:36:16 +00:00
|
|
|
case swap.TypeOut:
|
2021-12-13 13:28:38 +00:00
|
|
|
swapType = clientrpc.SwapType_LOOP_OUT
|
2020-04-24 15:35:32 +00:00
|
|
|
htlcAddressP2WSH = loopSwap.HtlcAddressP2WSH.EncodeAddress()
|
|
|
|
htlcAddress = htlcAddressP2WSH
|
|
|
|
|
2019-03-12 15:10:37 +00:00
|
|
|
default:
|
|
|
|
return nil, errors.New("unknown swap type")
|
|
|
|
}
|
|
|
|
|
2021-12-13 13:28:38 +00:00
|
|
|
return &clientrpc.SwapStatus{
|
2020-04-24 15:35:32 +00:00
|
|
|
Amt: int64(loopSwap.AmountRequested),
|
|
|
|
Id: loopSwap.SwapHash.String(),
|
|
|
|
IdBytes: loopSwap.SwapHash[:],
|
|
|
|
State: state,
|
2020-07-30 07:09:27 +00:00
|
|
|
FailureReason: failureReason,
|
2020-04-24 15:35:32 +00:00
|
|
|
InitiationTime: loopSwap.InitiationTime.UnixNano(),
|
|
|
|
LastUpdateTime: loopSwap.LastUpdate.UnixNano(),
|
|
|
|
HtlcAddress: htlcAddress,
|
|
|
|
HtlcAddressP2Wsh: htlcAddressP2WSH,
|
|
|
|
HtlcAddressNp2Wsh: htlcAddressNP2WSH,
|
|
|
|
Type: swapType,
|
|
|
|
CostServer: int64(loopSwap.Cost.Server),
|
|
|
|
CostOnchain: int64(loopSwap.Cost.Onchain),
|
|
|
|
CostOffchain: int64(loopSwap.Cost.Offchain),
|
2020-07-28 09:21:28 +00:00
|
|
|
Label: loopSwap.Label,
|
2019-03-06 20:13:50 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Monitor will return a stream of swap updates for currently active swaps.
|
2021-12-13 13:28:38 +00:00
|
|
|
func (s *swapClientServer) Monitor(in *clientrpc.MonitorRequest,
|
|
|
|
server clientrpc.SwapClient_MonitorServer) error {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Infof("Monitor request received")
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
send := func(info loop.SwapInfo) error {
|
2019-03-06 20:13:50 +00:00
|
|
|
rpcSwap, err := s.marshallSwap(&info)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return server.Send(rpcSwap)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a notification queue for this subscriber.
|
|
|
|
queue := queue.NewConcurrentQueue(20)
|
|
|
|
queue.Start()
|
|
|
|
|
|
|
|
// Add this subscriber to the global subscriber list. Also create a
|
|
|
|
// snapshot of all pending and completed swaps within the lock, to
|
|
|
|
// prevent subscribers from receiving duplicate updates.
|
2020-01-31 12:57:21 +00:00
|
|
|
s.swapsLock.Lock()
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2020-01-31 12:57:21 +00:00
|
|
|
id := s.nextSubscriberID
|
|
|
|
s.nextSubscriberID++
|
|
|
|
s.subscribers[id] = queue.ChanIn()
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
var pendingSwaps, completedSwaps []loop.SwapInfo
|
2020-01-31 12:57:21 +00:00
|
|
|
for _, swap := range s.swaps {
|
2019-03-07 04:32:24 +00:00
|
|
|
if swap.State.Type() == loopdb.StateTypePending {
|
2019-03-06 20:13:50 +00:00
|
|
|
pendingSwaps = append(pendingSwaps, swap)
|
|
|
|
} else {
|
|
|
|
completedSwaps = append(completedSwaps, swap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-31 12:57:21 +00:00
|
|
|
s.swapsLock.Unlock()
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
defer func() {
|
2020-01-31 12:57:21 +00:00
|
|
|
s.swapsLock.Lock()
|
|
|
|
delete(s.subscribers, id)
|
|
|
|
s.swapsLock.Unlock()
|
2020-04-01 17:04:50 +00:00
|
|
|
queue.Stop()
|
2019-03-06 20:13:50 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Sort completed swaps new to old.
|
|
|
|
sort.Slice(completedSwaps, func(i, j int) bool {
|
|
|
|
return completedSwaps[i].LastUpdate.After(
|
|
|
|
completedSwaps[j].LastUpdate,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Discard all but top x latest.
|
|
|
|
if len(completedSwaps) > completedSwapsCount {
|
|
|
|
completedSwaps = completedSwaps[:completedSwapsCount]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Concatenate both sets.
|
|
|
|
filteredSwaps := append(pendingSwaps, completedSwaps...)
|
|
|
|
|
|
|
|
// Sort again, but this time old to new.
|
|
|
|
sort.Slice(filteredSwaps, func(i, j int) bool {
|
|
|
|
return filteredSwaps[i].LastUpdate.Before(
|
|
|
|
filteredSwaps[j].LastUpdate,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Return swaps to caller.
|
|
|
|
for _, swap := range filteredSwaps {
|
|
|
|
if err := send(swap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// As long as the client is connected, keep passing through swap
|
|
|
|
// updates.
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case queueItem, ok := <-queue.ChanOut():
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
swap := queueItem.(loop.SwapInfo)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err := send(swap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-15 10:17:59 +00:00
|
|
|
|
|
|
|
// The client cancels the subscription.
|
2019-03-06 20:13:50 +00:00
|
|
|
case <-server.Context().Done():
|
|
|
|
return nil
|
2020-05-15 10:17:59 +00:00
|
|
|
|
|
|
|
// The server is shutting down.
|
|
|
|
case <-s.mainCtx.Done():
|
|
|
|
return fmt.Errorf("server is shutting down")
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-31 12:57:22 +00:00
|
|
|
// ListSwaps returns a list of all currently known swaps and their current
|
|
|
|
// status.
|
|
|
|
func (s *swapClientServer) ListSwaps(_ context.Context,
|
2021-12-13 13:28:38 +00:00
|
|
|
_ *clientrpc.ListSwapsRequest) (*clientrpc.ListSwapsResponse, error) {
|
2020-01-31 12:57:22 +00:00
|
|
|
|
|
|
|
var (
|
2021-12-13 13:28:38 +00:00
|
|
|
rpcSwaps = make([]*clientrpc.SwapStatus, len(s.swaps))
|
2020-01-31 12:57:22 +00:00
|
|
|
idx = 0
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2020-04-30 13:48:58 +00:00
|
|
|
s.swapsLock.Lock()
|
|
|
|
defer s.swapsLock.Unlock()
|
|
|
|
|
2020-01-31 12:57:22 +00:00
|
|
|
// We can just use the server's in-memory cache as that contains the
|
|
|
|
// most up-to-date state including temporary failures which aren't
|
|
|
|
// persisted to disk. The swaps field is a map, that's why we need an
|
|
|
|
// additional index.
|
|
|
|
for _, swp := range s.swaps {
|
|
|
|
swp := swp
|
|
|
|
rpcSwaps[idx], err = s.marshallSwap(&swp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
idx++
|
|
|
|
}
|
2021-12-13 13:28:38 +00:00
|
|
|
return &clientrpc.ListSwapsResponse{Swaps: rpcSwaps}, nil
|
2020-01-31 12:57:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SwapInfo returns all known details about a single swap.
|
|
|
|
func (s *swapClientServer) SwapInfo(_ context.Context,
|
2021-12-13 13:28:38 +00:00
|
|
|
req *clientrpc.SwapInfoRequest) (*clientrpc.SwapStatus, error) {
|
2020-01-31 12:57:22 +00:00
|
|
|
|
|
|
|
swapHash, err := lntypes.MakeHash(req.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing swap hash: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just return the server's in-memory cache here too as we also want to
|
|
|
|
// return temporary failures to the client.
|
|
|
|
swp, ok := s.swaps[swapHash]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("swap with hash %s not found", req.Id)
|
|
|
|
}
|
|
|
|
return s.marshallSwap(&swp)
|
|
|
|
}
|
|
|
|
|
2019-03-12 22:34:45 +00:00
|
|
|
// LoopOutTerms returns the terms that the server enforces for loop out swaps.
|
|
|
|
func (s *swapClientServer) LoopOutTerms(ctx context.Context,
|
2021-12-13 13:28:38 +00:00
|
|
|
req *clientrpc.TermsRequest) (*clientrpc.OutTermsResponse, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Infof("Loop out terms request received")
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
terms, err := s.impl.LoopOutTerms(ctx)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Errorf("Terms request: %v", err)
|
2019-03-06 20:13:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 13:28:38 +00:00
|
|
|
return &clientrpc.OutTermsResponse{
|
2019-03-06 20:13:50 +00:00
|
|
|
MinSwapAmount: int64(terms.MinSwapAmount),
|
|
|
|
MaxSwapAmount: int64(terms.MaxSwapAmount),
|
2020-07-15 11:00:27 +00:00
|
|
|
MinCltvDelta: terms.MinCltvDelta,
|
|
|
|
MaxCltvDelta: terms.MaxCltvDelta,
|
2019-03-06 20:13:50 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-12 22:34:45 +00:00
|
|
|
// LoopOutQuote returns a quote for a loop out swap with the provided
|
|
|
|
// parameters.
|
|
|
|
func (s *swapClientServer) LoopOutQuote(ctx context.Context,
|
2021-12-13 13:28:38 +00:00
|
|
|
req *clientrpc.QuoteRequest) (*clientrpc.OutQuoteResponse, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-06-25 18:41:49 +00:00
|
|
|
confTarget, err := validateConfTarget(
|
|
|
|
req.ConfTarget, loop.DefaultSweepConfTarget,
|
|
|
|
)
|
2019-06-25 18:41:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-07 04:32:24 +00:00
|
|
|
quote, err := s.impl.LoopOutQuote(ctx, &loop.LoopOutQuoteRequest{
|
2019-03-06 20:13:50 +00:00
|
|
|
Amount: btcutil.Amount(req.Amt),
|
2019-06-25 18:41:45 +00:00
|
|
|
SweepConfTarget: confTarget,
|
2020-01-07 12:52:42 +00:00
|
|
|
SwapPublicationDeadline: time.Unix(
|
|
|
|
int64(req.SwapPublicationDeadline), 0,
|
|
|
|
),
|
2019-03-06 20:13:50 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-08 20:28:20 +00:00
|
|
|
|
2021-12-13 13:28:38 +00:00
|
|
|
return &clientrpc.OutQuoteResponse{
|
2020-07-14 13:20:37 +00:00
|
|
|
HtlcSweepFeeSat: int64(quote.MinerFee),
|
|
|
|
PrepayAmtSat: int64(quote.PrepayAmount),
|
|
|
|
SwapFeeSat: int64(quote.SwapFee),
|
2019-10-08 20:28:20 +00:00
|
|
|
SwapPaymentDest: quote.SwapPaymentDest[:],
|
2021-03-04 09:39:00 +00:00
|
|
|
ConfTarget: confTarget,
|
2019-03-06 20:13:50 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2019-03-12 15:10:37 +00:00
|
|
|
|
|
|
|
// GetTerms returns the terms that the server enforces for swaps.
|
2020-07-14 13:13:55 +00:00
|
|
|
func (s *swapClientServer) GetLoopInTerms(ctx context.Context,
|
2021-12-13 13:28:38 +00:00
|
|
|
req *clientrpc.TermsRequest) (*clientrpc.InTermsResponse, error) {
|
2019-03-12 15:10:37 +00:00
|
|
|
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Infof("Loop in terms request received")
|
2019-03-12 15:10:37 +00:00
|
|
|
|
|
|
|
terms, err := s.impl.LoopInTerms(ctx)
|
|
|
|
if err != nil {
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Errorf("Terms request: %v", err)
|
2019-03-12 15:10:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 13:28:38 +00:00
|
|
|
return &clientrpc.InTermsResponse{
|
2019-03-12 15:10:37 +00:00
|
|
|
MinSwapAmount: int64(terms.MinSwapAmount),
|
|
|
|
MaxSwapAmount: int64(terms.MaxSwapAmount),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetQuote returns a quote for a swap with the provided parameters.
|
|
|
|
func (s *swapClientServer) GetLoopInQuote(ctx context.Context,
|
2021-12-13 13:28:38 +00:00
|
|
|
req *clientrpc.QuoteRequest) (*clientrpc.InQuoteResponse, error) {
|
2019-03-12 15:10:37 +00:00
|
|
|
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Infof("Loop in quote request received")
|
2019-03-12 15:10:37 +00:00
|
|
|
|
2020-04-15 07:10:21 +00:00
|
|
|
htlcConfTarget, err := validateLoopInRequest(
|
|
|
|
req.ConfTarget, req.ExternalHtlc,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-19 22:05:59 +00:00
|
|
|
var (
|
|
|
|
routeHints [][]zpay32.HopHint
|
|
|
|
lastHop *route.Vertex
|
|
|
|
)
|
|
|
|
|
2021-07-28 16:11:45 +00:00
|
|
|
if req.LoopInLastHop != nil {
|
|
|
|
lastHopVertex, err := route.NewVertexFromBytes(
|
|
|
|
req.LoopInLastHop,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lastHop = &lastHopVertex
|
|
|
|
}
|
|
|
|
|
2021-10-19 22:05:59 +00:00
|
|
|
if len(req.LoopInRouteHints) != 0 {
|
|
|
|
routeHints, err = unmarshallRouteHints(req.LoopInRouteHints)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-28 16:11:45 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 15:10:37 +00:00
|
|
|
quote, err := s.impl.LoopInQuote(ctx, &loop.LoopInQuoteRequest{
|
|
|
|
Amount: btcutil.Amount(req.Amt),
|
2020-04-15 07:10:21 +00:00
|
|
|
HtlcConfTarget: htlcConfTarget,
|
2019-09-23 08:08:34 +00:00
|
|
|
ExternalHtlc: req.ExternalHtlc,
|
2021-07-28 16:11:45 +00:00
|
|
|
LastHop: lastHop,
|
|
|
|
RouteHints: routeHints,
|
2021-10-19 22:05:59 +00:00
|
|
|
Private: req.Private,
|
2019-03-12 15:10:37 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-19 22:05:59 +00:00
|
|
|
|
2021-12-13 13:28:38 +00:00
|
|
|
return &clientrpc.InQuoteResponse{
|
2020-07-14 13:20:37 +00:00
|
|
|
HtlcPublishFeeSat: int64(quote.MinerFee),
|
|
|
|
SwapFeeSat: int64(quote.SwapFee),
|
2021-03-04 09:39:00 +00:00
|
|
|
ConfTarget: htlcConfTarget,
|
2019-03-12 15:10:37 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-05-10 14:55:53 +00:00
|
|
|
// unmarshallRouteHints unmarshalls a list of route hints.
|
2021-12-13 13:31:37 +00:00
|
|
|
func unmarshallRouteHints(rpcRouteHints []*looprpc.RouteHint) (
|
2021-05-10 14:55:53 +00:00
|
|
|
[][]zpay32.HopHint, error) {
|
|
|
|
|
|
|
|
routeHints := make([][]zpay32.HopHint, 0, len(rpcRouteHints))
|
|
|
|
for _, rpcRouteHint := range rpcRouteHints {
|
|
|
|
routeHint := make(
|
|
|
|
[]zpay32.HopHint, 0, len(rpcRouteHint.HopHints),
|
|
|
|
)
|
|
|
|
for _, rpcHint := range rpcRouteHint.HopHints {
|
|
|
|
hint, err := unmarshallHopHint(rpcHint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
routeHint = append(routeHint, hint)
|
|
|
|
}
|
|
|
|
routeHints = append(routeHints, routeHint)
|
|
|
|
}
|
|
|
|
|
|
|
|
return routeHints, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshallHopHint unmarshalls a single hop hint.
|
2021-12-13 13:31:37 +00:00
|
|
|
func unmarshallHopHint(rpcHint *looprpc.HopHint) (zpay32.HopHint, error) {
|
2021-05-10 14:55:53 +00:00
|
|
|
pubBytes, err := hex.DecodeString(rpcHint.NodeId)
|
|
|
|
if err != nil {
|
|
|
|
return zpay32.HopHint{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pubkey, err := btcec.ParsePubKey(pubBytes, btcec.S256())
|
|
|
|
if err != nil {
|
|
|
|
return zpay32.HopHint{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return zpay32.HopHint{
|
|
|
|
NodeID: pubkey,
|
|
|
|
ChannelID: rpcHint.ChanId,
|
|
|
|
FeeBaseMSat: rpcHint.FeeBaseMsat,
|
|
|
|
FeeProportionalMillionths: rpcHint.FeeProportionalMillionths,
|
|
|
|
CLTVExpiryDelta: uint16(rpcHint.CltvExpiryDelta),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Probe requests the server to probe the client's node to test inbound
|
|
|
|
// liquidity.
|
|
|
|
func (s *swapClientServer) Probe(ctx context.Context,
|
2021-12-13 13:28:38 +00:00
|
|
|
req *clientrpc.ProbeRequest) (*clientrpc.ProbeResponse, error) {
|
2021-05-10 14:55:53 +00:00
|
|
|
|
|
|
|
log.Infof("Probe request received")
|
|
|
|
|
|
|
|
var lastHop *route.Vertex
|
|
|
|
if req.LastHop != nil {
|
|
|
|
lastHopVertex, err := route.NewVertexFromBytes(req.LastHop)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lastHop = &lastHopVertex
|
|
|
|
}
|
|
|
|
|
|
|
|
routeHints, err := unmarshallRouteHints(req.RouteHints)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.impl.Probe(ctx, &loop.ProbeRequest{
|
|
|
|
Amount: btcutil.Amount(req.Amt),
|
|
|
|
LastHop: lastHop,
|
|
|
|
RouteHints: routeHints,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 13:28:38 +00:00
|
|
|
return &clientrpc.ProbeResponse{}, nil
|
2021-05-10 14:55:53 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 15:10:37 +00:00
|
|
|
func (s *swapClientServer) LoopIn(ctx context.Context,
|
2021-12-13 13:28:38 +00:00
|
|
|
in *clientrpc.LoopInRequest) (
|
|
|
|
*clientrpc.SwapResponse, error) {
|
2019-03-12 15:10:37 +00:00
|
|
|
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Infof("Loop in request received")
|
2019-03-12 15:10:37 +00:00
|
|
|
|
2020-04-15 07:10:21 +00:00
|
|
|
htlcConfTarget, err := validateLoopInRequest(
|
|
|
|
in.HtlcConfTarget, in.ExternalHtlc,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-10-12 11:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the label is valid.
|
|
|
|
if err := labels.Validate(in.Label); err != nil {
|
|
|
|
return nil, err
|
2020-04-15 07:10:21 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 22:05:59 +00:00
|
|
|
routeHints, err := unmarshallRouteHints(in.RouteHints)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-12 15:10:37 +00:00
|
|
|
req := &loop.LoopInRequest{
|
|
|
|
Amount: btcutil.Amount(in.Amt),
|
|
|
|
MaxMinerFee: btcutil.Amount(in.MaxMinerFee),
|
|
|
|
MaxSwapFee: btcutil.Amount(in.MaxSwapFee),
|
2020-04-15 07:10:21 +00:00
|
|
|
HtlcConfTarget: htlcConfTarget,
|
2019-03-28 12:29:21 +00:00
|
|
|
ExternalHtlc: in.ExternalHtlc,
|
2020-07-28 09:21:28 +00:00
|
|
|
Label: in.Label,
|
2020-11-06 09:43:04 +00:00
|
|
|
Initiator: in.Initiator,
|
2021-10-19 22:05:59 +00:00
|
|
|
Private: in.Private,
|
|
|
|
RouteHints: routeHints,
|
2019-03-12 15:10:37 +00:00
|
|
|
}
|
2020-02-11 12:25:03 +00:00
|
|
|
if in.LastHop != nil {
|
|
|
|
lastHop, err := route.NewVertexFromBytes(in.LastHop)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.LastHop = &lastHop
|
2019-03-12 15:10:37 +00:00
|
|
|
}
|
2020-04-29 16:30:44 +00:00
|
|
|
swapInfo, err := s.impl.LoopIn(ctx, req)
|
2019-03-12 15:10:37 +00:00
|
|
|
if err != nil {
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Errorf("Loop in: %v", err)
|
2019-03-12 15:10:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 13:28:38 +00:00
|
|
|
response := &clientrpc.SwapResponse{
|
2020-05-12 16:51:42 +00:00
|
|
|
Id: swapInfo.SwapHash.String(),
|
|
|
|
IdBytes: swapInfo.SwapHash[:],
|
|
|
|
HtlcAddressP2Wsh: swapInfo.HtlcAddressP2WSH.String(),
|
2020-06-30 12:10:33 +00:00
|
|
|
ServerMessage: swapInfo.ServerMessage,
|
2020-04-24 15:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.ExternalHtlc {
|
2020-05-12 16:51:42 +00:00
|
|
|
response.HtlcAddressNp2Wsh = swapInfo.HtlcAddressNP2WSH.String()
|
2021-07-14 15:21:54 +00:00
|
|
|
response.HtlcAddress = response.HtlcAddressNp2Wsh // nolint:staticcheck
|
2020-05-12 16:51:42 +00:00
|
|
|
} else {
|
2021-07-14 15:21:54 +00:00
|
|
|
response.HtlcAddress = response.HtlcAddressP2Wsh // nolint:staticcheck
|
2020-04-24 15:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
2019-03-12 15:10:37 +00:00
|
|
|
}
|
2019-06-25 18:41:45 +00:00
|
|
|
|
2019-11-15 12:57:03 +00:00
|
|
|
// GetLsatTokens returns all tokens that are contained in the LSAT token store.
|
|
|
|
func (s *swapClientServer) GetLsatTokens(ctx context.Context,
|
2021-12-13 13:28:38 +00:00
|
|
|
_ *clientrpc.TokensRequest) (*clientrpc.TokensResponse, error) {
|
2019-11-15 12:57:03 +00:00
|
|
|
|
|
|
|
log.Infof("Get LSAT tokens request received")
|
|
|
|
|
|
|
|
tokens, err := s.impl.LsatStore.AllTokens()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 13:28:38 +00:00
|
|
|
rpcTokens := make([]*clientrpc.LsatToken, len(tokens))
|
2019-11-15 12:57:03 +00:00
|
|
|
idx := 0
|
|
|
|
for key, token := range tokens {
|
|
|
|
macBytes, err := token.BaseMacaroon().MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-13 13:28:38 +00:00
|
|
|
rpcTokens[idx] = &clientrpc.LsatToken{
|
2019-11-15 12:57:03 +00:00
|
|
|
BaseMacaroon: macBytes,
|
|
|
|
PaymentHash: token.PaymentHash[:],
|
|
|
|
PaymentPreimage: token.Preimage[:],
|
|
|
|
AmountPaidMsat: int64(token.AmountPaid),
|
|
|
|
RoutingFeePaidMsat: int64(token.RoutingFeePaid),
|
|
|
|
TimeCreated: token.TimeCreated.Unix(),
|
|
|
|
Expired: !token.IsValid(),
|
|
|
|
StorageName: key,
|
|
|
|
}
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
|
2021-12-13 13:28:38 +00:00
|
|
|
return &clientrpc.TokensResponse{Tokens: rpcTokens}, nil
|
2019-11-15 12:57:03 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 07:58:09 +00:00
|
|
|
// GetLiquidityParams gets our current liquidity manager's parameters.
|
|
|
|
func (s *swapClientServer) GetLiquidityParams(_ context.Context,
|
2021-12-13 13:28:38 +00:00
|
|
|
_ *clientrpc.GetLiquidityParamsRequest) (*clientrpc.LiquidityParameters,
|
2020-09-01 07:58:09 +00:00
|
|
|
error) {
|
|
|
|
|
|
|
|
cfg := s.liquidityMgr.GetParameters()
|
|
|
|
|
2021-02-16 11:31:51 +00:00
|
|
|
totalRules := len(cfg.ChannelRules) + len(cfg.PeerRules)
|
|
|
|
|
2021-12-13 13:28:38 +00:00
|
|
|
rpcCfg := &clientrpc.LiquidityParameters{
|
2021-03-02 12:42:02 +00:00
|
|
|
SweepConfTarget: cfg.SweepConfTarget,
|
|
|
|
FailureBackoffSec: uint64(cfg.FailureBackOff.Seconds()),
|
|
|
|
Autoloop: cfg.Autoloop,
|
|
|
|
AutoloopBudgetSat: uint64(cfg.AutoFeeBudget),
|
|
|
|
AutoMaxInFlight: uint64(cfg.MaxAutoInFlight),
|
2020-09-01 07:58:09 +00:00
|
|
|
Rules: make(
|
2021-12-13 13:28:38 +00:00
|
|
|
[]*clientrpc.LiquidityRule, 0, totalRules,
|
2020-09-01 07:58:09 +00:00
|
|
|
),
|
2021-12-15 07:11:46 +00:00
|
|
|
MinSwapAmount: uint64(cfg.ClientRestrictions.Minimum),
|
|
|
|
MaxSwapAmount: uint64(cfg.ClientRestrictions.Maximum),
|
|
|
|
HtlcConfTarget: cfg.HtlcConfTarget,
|
2020-09-01 07:58:09 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 12:42:04 +00:00
|
|
|
switch f := cfg.FeeLimit.(type) {
|
|
|
|
case *liquidity.FeeCategoryLimit:
|
|
|
|
satPerByte := f.SweepFeeRateLimit.FeePerKVByte() / 1000
|
|
|
|
|
|
|
|
rpcCfg.SweepFeeRateSatPerVbyte = uint64(satPerByte)
|
2021-03-02 12:42:02 +00:00
|
|
|
|
2021-03-02 12:42:04 +00:00
|
|
|
rpcCfg.MaxMinerFeeSat = uint64(f.MaximumMinerFee)
|
|
|
|
rpcCfg.MaxSwapFeePpm = f.MaximumSwapFeePPM
|
|
|
|
rpcCfg.MaxRoutingFeePpm = f.MaximumRoutingFeePPM
|
|
|
|
rpcCfg.MaxPrepayRoutingFeePpm = f.MaximumPrepayRoutingFeePPM
|
|
|
|
rpcCfg.MaxPrepaySat = uint64(f.MaximumPrepay)
|
2021-03-02 12:42:02 +00:00
|
|
|
|
2021-03-02 12:42:04 +00:00
|
|
|
case *liquidity.FeePortion:
|
|
|
|
rpcCfg.FeePpm = f.PartsPerMillion
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown fee limit: %T", cfg.FeeLimit)
|
|
|
|
}
|
2021-03-02 12:42:02 +00:00
|
|
|
|
2020-10-12 11:34:56 +00:00
|
|
|
// Zero golang time is different to a zero unix time, so we only set
|
|
|
|
// our start date if it is non-zero.
|
|
|
|
if !cfg.AutoFeeStartDate.IsZero() {
|
2021-02-03 06:54:48 +00:00
|
|
|
rpcCfg.AutoloopBudgetStartSec = uint64(
|
2020-10-12 11:34:56 +00:00
|
|
|
cfg.AutoFeeStartDate.Unix(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:58:09 +00:00
|
|
|
for channel, rule := range cfg.ChannelRules {
|
2021-02-16 11:31:51 +00:00
|
|
|
rpcRule := newRPCRule(channel.ToUint64(), nil, rule)
|
|
|
|
rpcCfg.Rules = append(rpcCfg.Rules, rpcRule)
|
|
|
|
}
|
2020-09-01 07:58:09 +00:00
|
|
|
|
2021-02-16 11:31:51 +00:00
|
|
|
for peer, rule := range cfg.PeerRules {
|
2021-02-16 11:31:52 +00:00
|
|
|
peer := peer
|
2021-02-16 11:31:51 +00:00
|
|
|
rpcRule := newRPCRule(0, peer[:], rule)
|
2020-09-01 07:58:09 +00:00
|
|
|
rpcCfg.Rules = append(rpcCfg.Rules, rpcRule)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rpcCfg, nil
|
|
|
|
}
|
|
|
|
|
2021-02-16 11:31:51 +00:00
|
|
|
func newRPCRule(channelID uint64, peer []byte,
|
2021-12-13 13:28:38 +00:00
|
|
|
rule *liquidity.SwapRule) *clientrpc.LiquidityRule {
|
2021-02-16 11:31:51 +00:00
|
|
|
|
2021-12-13 13:28:38 +00:00
|
|
|
rpcRule := &clientrpc.LiquidityRule{
|
2021-02-16 11:31:51 +00:00
|
|
|
ChannelId: channelID,
|
|
|
|
Pubkey: peer,
|
2021-12-13 13:28:38 +00:00
|
|
|
Type: clientrpc.LiquidityRuleType_THRESHOLD,
|
2021-02-16 11:31:51 +00:00
|
|
|
IncomingThreshold: uint32(rule.MinimumIncoming),
|
|
|
|
OutgoingThreshold: uint32(rule.MinimumOutgoing),
|
2021-12-13 13:28:38 +00:00
|
|
|
SwapType: clientrpc.SwapType_LOOP_OUT,
|
2021-02-16 11:31:51 +00:00
|
|
|
}
|
2021-11-30 11:18:28 +00:00
|
|
|
|
|
|
|
if rule.Type == swap.TypeIn {
|
2021-12-13 13:28:38 +00:00
|
|
|
rpcRule.SwapType = clientrpc.SwapType_LOOP_IN
|
2021-11-30 11:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rpcRule
|
2021-02-16 11:31:51 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 07:58:09 +00:00
|
|
|
// SetLiquidityParams attempts to set our current liquidity manager's
|
|
|
|
// parameters.
|
2020-12-01 09:18:31 +00:00
|
|
|
func (s *swapClientServer) SetLiquidityParams(ctx context.Context,
|
2021-12-13 13:28:38 +00:00
|
|
|
in *clientrpc.SetLiquidityParamsRequest) (*clientrpc.SetLiquidityParamsResponse,
|
2020-09-01 07:58:09 +00:00
|
|
|
error) {
|
|
|
|
|
2021-03-02 12:42:04 +00:00
|
|
|
feeLimit, err := rpcToFee(in.Parameters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-02 12:42:02 +00:00
|
|
|
|
2020-09-01 07:58:09 +00:00
|
|
|
params := liquidity.Parameters{
|
2021-03-02 12:42:02 +00:00
|
|
|
FeeLimit: feeLimit,
|
|
|
|
SweepConfTarget: in.Parameters.SweepConfTarget,
|
2020-09-30 10:34:10 +00:00
|
|
|
FailureBackOff: time.Duration(in.Parameters.FailureBackoffSec) *
|
|
|
|
time.Second,
|
2021-02-03 06:54:48 +00:00
|
|
|
Autoloop: in.Parameters.Autoloop,
|
|
|
|
AutoFeeBudget: btcutil.Amount(in.Parameters.AutoloopBudgetSat),
|
2020-10-12 11:34:56 +00:00
|
|
|
MaxAutoInFlight: int(in.Parameters.AutoMaxInFlight),
|
2020-09-01 07:58:09 +00:00
|
|
|
ChannelRules: make(
|
2021-11-30 11:18:27 +00:00
|
|
|
map[lnwire.ShortChannelID]*liquidity.SwapRule,
|
2021-02-16 11:31:51 +00:00
|
|
|
),
|
|
|
|
PeerRules: make(
|
2021-11-30 11:18:27 +00:00
|
|
|
map[route.Vertex]*liquidity.SwapRule,
|
2020-09-01 07:58:09 +00:00
|
|
|
),
|
2020-12-01 10:20:38 +00:00
|
|
|
ClientRestrictions: liquidity.Restrictions{
|
|
|
|
Minimum: btcutil.Amount(in.Parameters.MinSwapAmount),
|
|
|
|
Maximum: btcutil.Amount(in.Parameters.MaxSwapAmount),
|
|
|
|
},
|
2021-12-15 07:11:46 +00:00
|
|
|
HtlcConfTarget: in.Parameters.HtlcConfTarget,
|
2020-09-01 07:58:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 11:34:56 +00:00
|
|
|
// Zero unix time is different to zero golang time.
|
2021-02-03 06:54:48 +00:00
|
|
|
if in.Parameters.AutoloopBudgetStartSec != 0 {
|
2020-10-12 11:34:56 +00:00
|
|
|
params.AutoFeeStartDate = time.Unix(
|
2021-02-03 06:54:48 +00:00
|
|
|
int64(in.Parameters.AutoloopBudgetStartSec), 0,
|
2020-10-12 11:34:56 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:58:09 +00:00
|
|
|
for _, rule := range in.Parameters.Rules {
|
2021-02-16 11:31:51 +00:00
|
|
|
peerRule := rule.Pubkey != nil
|
|
|
|
chanRule := rule.ChannelId != 0
|
2020-09-01 07:58:09 +00:00
|
|
|
|
2021-02-16 11:31:51 +00:00
|
|
|
liquidityRule, err := rpcToRule(rule)
|
2020-09-01 07:58:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-16 11:31:51 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case peerRule && chanRule:
|
|
|
|
return nil, fmt.Errorf("cannot set channel: %v and "+
|
|
|
|
"peer: %v fields in rule", rule.ChannelId,
|
|
|
|
rule.Pubkey)
|
|
|
|
|
|
|
|
case peerRule:
|
|
|
|
pubkey, err := route.NewVertexFromBytes(rule.Pubkey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := params.PeerRules[pubkey]; ok {
|
|
|
|
return nil, fmt.Errorf("multiple rules set "+
|
|
|
|
"for peer: %v", pubkey)
|
|
|
|
}
|
|
|
|
|
|
|
|
params.PeerRules[pubkey] = liquidityRule
|
|
|
|
|
|
|
|
case chanRule:
|
|
|
|
shortID := lnwire.NewShortChanIDFromInt(rule.ChannelId)
|
|
|
|
|
|
|
|
if _, ok := params.ChannelRules[shortID]; ok {
|
|
|
|
return nil, fmt.Errorf("multiple rules set "+
|
|
|
|
"for channel: %v", shortID)
|
|
|
|
}
|
|
|
|
|
|
|
|
params.ChannelRules[shortID] = liquidityRule
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errors.New("please set channel id or " +
|
|
|
|
"pubkey for rule")
|
|
|
|
}
|
2020-09-01 07:58:09 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 09:18:31 +00:00
|
|
|
if err := s.liquidityMgr.SetParameters(ctx, params); err != nil {
|
2020-09-01 07:58:09 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 13:28:38 +00:00
|
|
|
return &clientrpc.SetLiquidityParamsResponse{}, nil
|
2020-09-01 07:58:09 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 12:42:04 +00:00
|
|
|
// rpcToFee converts the values provided over rpc to a fee limit interface,
|
|
|
|
// failing if an inconsistent set of fields are set.
|
2021-12-13 13:28:38 +00:00
|
|
|
func rpcToFee(req *clientrpc.LiquidityParameters) (liquidity.FeeLimit,
|
2021-03-02 12:42:04 +00:00
|
|
|
error) {
|
|
|
|
|
|
|
|
// Check which fee limit type we have values set for. If any fields
|
|
|
|
// relevant to our individual categories are set, we count that type
|
|
|
|
// as set.
|
|
|
|
isFeePPM := req.FeePpm != 0
|
|
|
|
isCategories := req.MaxSwapFeePpm != 0 || req.MaxRoutingFeePpm != 0 ||
|
|
|
|
req.MaxPrepayRoutingFeePpm != 0 || req.MaxMinerFeeSat != 0 ||
|
|
|
|
req.MaxPrepaySat != 0 || req.SweepFeeRateSatPerVbyte != 0
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case isFeePPM && isCategories:
|
|
|
|
return nil, errors.New("set either fee ppm, or individual " +
|
|
|
|
"fee categories")
|
|
|
|
case isFeePPM:
|
|
|
|
return liquidity.NewFeePortion(req.FeePpm), nil
|
|
|
|
|
|
|
|
case isCategories:
|
|
|
|
satPerVbyte := chainfee.SatPerKVByte(
|
|
|
|
req.SweepFeeRateSatPerVbyte * 1000,
|
|
|
|
)
|
|
|
|
|
|
|
|
return liquidity.NewFeeCategoryLimit(
|
|
|
|
req.MaxSwapFeePpm,
|
|
|
|
req.MaxRoutingFeePpm,
|
|
|
|
req.MaxPrepayRoutingFeePpm,
|
|
|
|
btcutil.Amount(req.MaxMinerFeeSat),
|
|
|
|
btcutil.Amount(req.MaxPrepaySat),
|
|
|
|
satPerVbyte.FeePerKWeight(),
|
|
|
|
), nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errors.New("no fee categories set")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:58:09 +00:00
|
|
|
// rpcToRule switches on rpc rule type to convert to our rule interface.
|
2021-12-13 13:28:38 +00:00
|
|
|
func rpcToRule(rule *clientrpc.LiquidityRule) (*liquidity.SwapRule, error) {
|
2021-11-30 11:18:28 +00:00
|
|
|
swapType := swap.TypeOut
|
2021-12-13 13:28:38 +00:00
|
|
|
if rule.SwapType == clientrpc.SwapType_LOOP_IN {
|
2021-11-30 11:18:28 +00:00
|
|
|
swapType = swap.TypeIn
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:58:09 +00:00
|
|
|
switch rule.Type {
|
2021-12-13 13:28:38 +00:00
|
|
|
case clientrpc.LiquidityRuleType_UNKNOWN:
|
2020-09-01 07:58:09 +00:00
|
|
|
return nil, fmt.Errorf("rule type field must be set")
|
|
|
|
|
2021-12-13 13:28:38 +00:00
|
|
|
case clientrpc.LiquidityRuleType_THRESHOLD:
|
2021-11-30 11:18:27 +00:00
|
|
|
return &liquidity.SwapRule{
|
|
|
|
ThresholdRule: liquidity.NewThresholdRule(
|
|
|
|
int(rule.IncomingThreshold),
|
|
|
|
int(rule.OutgoingThreshold),
|
|
|
|
),
|
2021-11-30 11:18:28 +00:00
|
|
|
Type: swapType,
|
2021-11-30 11:18:27 +00:00
|
|
|
}, nil
|
2020-09-01 07:58:09 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown rule: %T", rule)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-09-03 08:36:44 +00:00
|
|
|
// SuggestSwaps provides a list of suggested swaps based on lnd's current
|
|
|
|
// channel balances and rules set by the liquidity manager.
|
|
|
|
func (s *swapClientServer) SuggestSwaps(ctx context.Context,
|
2021-12-13 13:28:38 +00:00
|
|
|
_ *clientrpc.SuggestSwapsRequest) (*clientrpc.SuggestSwapsResponse, error) {
|
2020-09-03 08:36:44 +00:00
|
|
|
|
2021-02-08 07:39:02 +00:00
|
|
|
suggestions, err := s.liquidityMgr.SuggestSwaps(ctx, false)
|
2021-02-08 07:38:23 +00:00
|
|
|
switch err {
|
|
|
|
case liquidity.ErrNoRules:
|
|
|
|
return nil, status.Error(codes.FailedPrecondition, err.Error())
|
|
|
|
|
|
|
|
case nil:
|
|
|
|
|
|
|
|
default:
|
2020-09-03 08:36:44 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-15 07:10:28 +00:00
|
|
|
resp := &clientrpc.SuggestSwapsResponse{
|
|
|
|
LoopOut: make(
|
|
|
|
[]*clientrpc.LoopOutRequest, len(suggestions.OutSwaps),
|
|
|
|
),
|
|
|
|
LoopIn: make(
|
|
|
|
[]*clientrpc.LoopInRequest, len(suggestions.InSwaps),
|
|
|
|
),
|
|
|
|
}
|
2020-09-03 08:36:44 +00:00
|
|
|
|
2021-12-15 07:10:28 +00:00
|
|
|
for i, swap := range suggestions.OutSwaps {
|
|
|
|
resp.LoopOut[i] = &clientrpc.LoopOutRequest{
|
2020-09-30 10:34:02 +00:00
|
|
|
Amt: int64(swap.Amount),
|
|
|
|
OutgoingChanSet: swap.OutgoingChanSet,
|
|
|
|
MaxSwapFee: int64(swap.MaxSwapFee),
|
|
|
|
MaxMinerFee: int64(swap.MaxMinerFee),
|
|
|
|
MaxPrepayAmt: int64(swap.MaxPrepayAmount),
|
|
|
|
MaxSwapRoutingFee: int64(swap.MaxSwapRoutingFee),
|
|
|
|
MaxPrepayRoutingFee: int64(swap.MaxPrepayRoutingFee),
|
|
|
|
SweepConfTarget: swap.SweepConfTarget,
|
2021-12-15 07:10:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, swap := range suggestions.InSwaps {
|
|
|
|
loopIn := &clientrpc.LoopInRequest{
|
|
|
|
Amt: int64(swap.Amount),
|
|
|
|
MaxSwapFee: int64(swap.MaxSwapFee),
|
|
|
|
MaxMinerFee: int64(swap.MaxMinerFee),
|
|
|
|
HtlcConfTarget: swap.HtlcConfTarget,
|
|
|
|
}
|
|
|
|
|
|
|
|
if swap.LastHop != nil {
|
|
|
|
loopIn.LastHop = swap.LastHop[:]
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.LoopIn[i] = loopIn
|
2020-09-03 08:36:44 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 07:39:03 +00:00
|
|
|
for id, reason := range suggestions.DisqualifiedChans {
|
|
|
|
autoloopReason, err := rpcAutoloopReason(reason)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 13:28:38 +00:00
|
|
|
exclChan := &clientrpc.Disqualified{
|
2021-02-08 07:39:03 +00:00
|
|
|
Reason: autoloopReason,
|
|
|
|
ChannelId: id.ToUint64(),
|
|
|
|
}
|
2021-02-16 11:31:51 +00:00
|
|
|
|
2021-12-15 07:10:28 +00:00
|
|
|
resp.Disqualified = append(resp.Disqualified, exclChan)
|
2021-02-16 11:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for pubkey, reason := range suggestions.DisqualifiedPeers {
|
|
|
|
autoloopReason, err := rpcAutoloopReason(reason)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 13:28:38 +00:00
|
|
|
exclChan := &clientrpc.Disqualified{
|
2021-02-16 11:31:51 +00:00
|
|
|
Reason: autoloopReason,
|
|
|
|
Pubkey: pubkey[:],
|
|
|
|
}
|
|
|
|
|
2021-12-15 07:10:28 +00:00
|
|
|
resp.Disqualified = append(resp.Disqualified, exclChan)
|
2021-02-08 07:39:03 +00:00
|
|
|
}
|
|
|
|
|
2021-12-15 07:10:28 +00:00
|
|
|
return resp, nil
|
2020-09-03 08:36:44 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 13:28:38 +00:00
|
|
|
func rpcAutoloopReason(reason liquidity.Reason) (clientrpc.AutoReason, error) {
|
2021-02-08 07:39:03 +00:00
|
|
|
switch reason {
|
|
|
|
case liquidity.ReasonNone:
|
2021-12-13 13:28:38 +00:00
|
|
|
return clientrpc.AutoReason_AUTO_REASON_UNKNOWN, nil
|
2021-02-08 07:39:03 +00:00
|
|
|
|
|
|
|
case liquidity.ReasonBudgetNotStarted:
|
2021-12-13 13:28:38 +00:00
|
|
|
return clientrpc.AutoReason_AUTO_REASON_BUDGET_NOT_STARTED, nil
|
2021-02-08 07:39:03 +00:00
|
|
|
|
|
|
|
case liquidity.ReasonSweepFees:
|
2021-12-13 13:28:38 +00:00
|
|
|
return clientrpc.AutoReason_AUTO_REASON_SWEEP_FEES, nil
|
2021-02-08 07:39:03 +00:00
|
|
|
|
|
|
|
case liquidity.ReasonBudgetElapsed:
|
2021-12-13 13:28:38 +00:00
|
|
|
return clientrpc.AutoReason_AUTO_REASON_BUDGET_ELAPSED, nil
|
2021-02-08 07:39:03 +00:00
|
|
|
|
|
|
|
case liquidity.ReasonInFlight:
|
2021-12-13 13:28:38 +00:00
|
|
|
return clientrpc.AutoReason_AUTO_REASON_IN_FLIGHT, nil
|
2021-02-08 07:39:03 +00:00
|
|
|
|
|
|
|
case liquidity.ReasonSwapFee:
|
2021-12-13 13:28:38 +00:00
|
|
|
return clientrpc.AutoReason_AUTO_REASON_SWAP_FEE, nil
|
2021-02-08 07:39:03 +00:00
|
|
|
|
|
|
|
case liquidity.ReasonMinerFee:
|
2021-12-13 13:28:38 +00:00
|
|
|
return clientrpc.AutoReason_AUTO_REASON_MINER_FEE, nil
|
2021-02-08 07:39:03 +00:00
|
|
|
|
|
|
|
case liquidity.ReasonPrepay:
|
2021-12-13 13:28:38 +00:00
|
|
|
return clientrpc.AutoReason_AUTO_REASON_PREPAY, nil
|
2021-02-08 07:39:03 +00:00
|
|
|
|
|
|
|
case liquidity.ReasonFailureBackoff:
|
2021-12-13 13:28:38 +00:00
|
|
|
return clientrpc.AutoReason_AUTO_REASON_FAILURE_BACKOFF, nil
|
2021-02-08 07:39:03 +00:00
|
|
|
|
|
|
|
case liquidity.ReasonLoopOut:
|
2021-12-13 13:28:38 +00:00
|
|
|
return clientrpc.AutoReason_AUTO_REASON_LOOP_OUT, nil
|
2021-02-08 07:39:03 +00:00
|
|
|
|
|
|
|
case liquidity.ReasonLoopIn:
|
2021-12-13 13:28:38 +00:00
|
|
|
return clientrpc.AutoReason_AUTO_REASON_LOOP_IN, nil
|
2021-02-08 07:39:03 +00:00
|
|
|
|
|
|
|
case liquidity.ReasonLiquidityOk:
|
2021-12-13 13:28:38 +00:00
|
|
|
return clientrpc.AutoReason_AUTO_REASON_LIQUIDITY_OK, nil
|
2021-02-08 07:39:03 +00:00
|
|
|
|
|
|
|
case liquidity.ReasonBudgetInsufficient:
|
2021-12-13 13:28:38 +00:00
|
|
|
return clientrpc.AutoReason_AUTO_REASON_BUDGET_INSUFFICIENT, nil
|
2021-02-08 07:39:03 +00:00
|
|
|
|
2021-03-02 12:42:03 +00:00
|
|
|
case liquidity.ReasonFeePPMInsufficient:
|
2021-12-13 13:28:38 +00:00
|
|
|
return clientrpc.AutoReason_AUTO_REASON_SWAP_FEE, nil
|
2021-03-02 12:42:03 +00:00
|
|
|
|
2021-02-08 07:39:03 +00:00
|
|
|
default:
|
|
|
|
return 0, fmt.Errorf("unknown autoloop reason: %v", reason)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-31 12:57:21 +00:00
|
|
|
// processStatusUpdates reads updates on the status channel and processes them.
|
|
|
|
//
|
|
|
|
// NOTE: This must run inside a goroutine as it blocks until the main context
|
|
|
|
// shuts down.
|
|
|
|
func (s *swapClientServer) processStatusUpdates(mainCtx context.Context) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// On updates, refresh the server's in-memory state and inform
|
|
|
|
// subscribers about the changes.
|
|
|
|
case swp := <-s.statusChan:
|
|
|
|
s.swapsLock.Lock()
|
|
|
|
s.swaps[swp.SwapHash] = swp
|
|
|
|
|
|
|
|
for _, subscriber := range s.subscribers {
|
|
|
|
select {
|
|
|
|
case subscriber <- swp:
|
|
|
|
case <-mainCtx.Done():
|
2020-04-01 17:04:50 +00:00
|
|
|
s.swapsLock.Unlock()
|
2020-01-31 12:57:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.swapsLock.Unlock()
|
|
|
|
|
|
|
|
// Server is shutting down.
|
|
|
|
case <-mainCtx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-25 18:41:45 +00:00
|
|
|
// validateConfTarget ensures the given confirmation target is valid. If one
|
|
|
|
// isn't specified (0 value), then the default target is used.
|
|
|
|
func validateConfTarget(target, defaultTarget int32) (int32, error) {
|
|
|
|
switch {
|
2020-04-15 07:10:21 +00:00
|
|
|
case target == 0:
|
|
|
|
return defaultTarget, nil
|
|
|
|
|
2019-06-25 18:41:45 +00:00
|
|
|
// Ensure the target respects our minimum threshold.
|
|
|
|
case target < minConfTarget:
|
2021-03-08 07:50:17 +00:00
|
|
|
return 0, fmt.Errorf("%w: A confirmation target of at "+
|
|
|
|
"least %v must be provided", errConfTargetTooLow,
|
|
|
|
minConfTarget)
|
2019-06-25 18:41:45 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return target, nil
|
|
|
|
}
|
|
|
|
}
|
2020-04-15 07:10:21 +00:00
|
|
|
|
|
|
|
// validateLoopInRequest fails if the mutually exclusive conf target and
|
|
|
|
// external parameters are both set.
|
|
|
|
func validateLoopInRequest(htlcConfTarget int32, external bool) (int32, error) {
|
|
|
|
// If the htlc is going to be externally set, the htlcConfTarget should
|
|
|
|
// not be set, because it has no relevance when the htlc is external.
|
|
|
|
if external && htlcConfTarget != 0 {
|
|
|
|
return 0, errors.New("external and htlc conf target cannot " +
|
|
|
|
"both be set")
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the htlc is being externally published, we do not need to set a
|
|
|
|
// confirmation target.
|
|
|
|
if external {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return validateConfTarget(htlcConfTarget, loop.DefaultHtlcConfTarget)
|
|
|
|
}
|
2021-03-08 07:50:17 +00:00
|
|
|
|
2021-03-08 08:52:57 +00:00
|
|
|
// validateLoopOutRequest validates the confirmation target, destination
|
2021-03-11 10:20:11 +00:00
|
|
|
// address and label of the loop out request. It also checks that the requested
|
|
|
|
// loop amount is valid given the available balance.
|
|
|
|
func validateLoopOutRequest(ctx context.Context, lnd lndclient.LightningClient,
|
2021-12-13 13:28:38 +00:00
|
|
|
chainParams *chaincfg.Params, req *clientrpc.LoopOutRequest,
|
2021-03-11 10:20:11 +00:00
|
|
|
sweepAddr btcutil.Address, maxParts uint32) (int32, error) {
|
|
|
|
|
2021-03-08 08:52:57 +00:00
|
|
|
// Check that the provided destination address has the correct format
|
|
|
|
// for the active network.
|
|
|
|
if !sweepAddr.IsForNet(chainParams) {
|
|
|
|
return 0, fmt.Errorf("%w: Current active network is %s",
|
|
|
|
errIncorrectChain, chainParams.Name)
|
|
|
|
}
|
|
|
|
|
2021-03-08 07:50:17 +00:00
|
|
|
// Check that the label is valid.
|
2021-03-11 10:20:11 +00:00
|
|
|
if err := labels.Validate(req.Label); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2021-10-24 08:08:23 +00:00
|
|
|
channels, err := lnd.ListChannels(ctx, false, false)
|
2021-03-11 10:20:11 +00:00
|
|
|
if err != nil {
|
2021-03-08 07:50:17 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2021-03-11 10:20:11 +00:00
|
|
|
unlimitedChannels := len(req.OutgoingChanSet) == 0
|
|
|
|
outgoingChanSetMap := make(map[uint64]bool)
|
|
|
|
for _, chanID := range req.OutgoingChanSet {
|
|
|
|
outgoingChanSetMap[chanID] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var activeChannelSet []lndclient.ChannelInfo
|
|
|
|
for _, c := range channels {
|
|
|
|
// Don't bother looking at inactive channels.
|
|
|
|
if !c.Active {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no outgoing channel set was specified then all active
|
|
|
|
// channels are considered. However, if a channel set was
|
|
|
|
// specified then only the specified channels are considered.
|
|
|
|
if unlimitedChannels || outgoingChanSetMap[c.ChannelID] {
|
|
|
|
activeChannelSet = append(activeChannelSet, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if the loop out request is theoretically possible given
|
|
|
|
// the amount requested, the maximum possible routing fees,
|
|
|
|
// the available channel set and the fact that equal splitting is
|
|
|
|
// used for MPP.
|
|
|
|
requiredBalance := btcutil.Amount(req.Amt + req.MaxSwapRoutingFee)
|
|
|
|
isRoutable, _ := hasBandwidth(activeChannelSet, requiredBalance,
|
|
|
|
int(maxParts))
|
|
|
|
if !isRoutable {
|
|
|
|
return 0, fmt.Errorf("%w: Requested swap amount of %d "+
|
|
|
|
"sats along with the maximum routing fee of %d sats "+
|
|
|
|
"is more than what can be routed given current state "+
|
|
|
|
"of the channel set", errBalanceTooLow, req.Amt,
|
|
|
|
req.MaxSwapRoutingFee)
|
|
|
|
}
|
|
|
|
|
|
|
|
return validateConfTarget(
|
|
|
|
req.SweepConfTarget, loop.DefaultSweepConfTarget,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// hasBandwidth simulates the MPP splitting logic that will be used by LND when
|
|
|
|
// attempting to route the payment. This function is used to evaluate if a
|
|
|
|
// payment will be routable given the splitting logic used by LND.
|
|
|
|
// It returns true if the amount is routable given the channel set and the
|
|
|
|
// maximum number of shards allowed. If the amount is routable then the number
|
|
|
|
// of shards used is also returned. This function makes an assumption that the
|
|
|
|
// minimum loop amount divided by max parts will not be less than the minimum
|
|
|
|
// shard amount. If the MPP logic changes, then this function should be updated.
|
|
|
|
func hasBandwidth(channels []lndclient.ChannelInfo, amt btcutil.Amount,
|
|
|
|
maxParts int) (bool, int) {
|
|
|
|
|
|
|
|
scratch := make([]btcutil.Amount, len(channels))
|
|
|
|
var totalBandwidth btcutil.Amount
|
|
|
|
for i, channel := range channels {
|
|
|
|
scratch[i] = channel.LocalBalance
|
|
|
|
totalBandwidth += channel.LocalBalance
|
|
|
|
}
|
|
|
|
|
|
|
|
if totalBandwidth < amt {
|
|
|
|
return false, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
split := amt
|
|
|
|
for shard := 0; shard <= maxParts; {
|
|
|
|
paid := false
|
|
|
|
for i := 0; i < len(scratch); i++ {
|
|
|
|
if scratch[i] >= split {
|
|
|
|
scratch[i] -= split
|
|
|
|
amt -= split
|
|
|
|
paid = true
|
|
|
|
shard++
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if amt == 0 {
|
|
|
|
return true, shard
|
|
|
|
}
|
|
|
|
|
|
|
|
if !paid {
|
|
|
|
split /= 2
|
|
|
|
} else {
|
|
|
|
split = amt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, 0
|
2021-03-08 07:50:17 +00:00
|
|
|
}
|