2019-03-07 02:22:46 +00:00
|
|
|
package loop
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2020-07-14 12:54:06 +00:00
|
|
|
"fmt"
|
2020-02-21 09:27:57 +00:00
|
|
|
"strings"
|
2019-03-06 20:13:50 +00:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2022-06-24 11:57:42 +00:00
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
|
|
|
"github.com/btcsuite/btcd/btcec/v2/schnorr"
|
2022-03-14 12:36:02 +00:00
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
2024-04-17 17:13:01 +00:00
|
|
|
"github.com/lightninglabs/aperture/l402"
|
2020-06-17 20:25:57 +00:00
|
|
|
"github.com/lightninglabs/lndclient"
|
2019-03-07 04:32:24 +00:00
|
|
|
"github.com/lightninglabs/loop/loopdb"
|
|
|
|
"github.com/lightninglabs/loop/swap"
|
2019-03-06 23:29:44 +00:00
|
|
|
"github.com/lightninglabs/loop/sweep"
|
2024-01-18 14:18:36 +00:00
|
|
|
"github.com/lightninglabs/loop/sweepbatcher"
|
2023-11-14 00:52:39 +00:00
|
|
|
"github.com/lightninglabs/loop/utils"
|
2023-11-13 13:50:10 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2021-10-19 22:05:59 +00:00
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
2023-08-24 23:41:45 +00:00
|
|
|
"google.golang.org/grpc"
|
2021-07-19 08:01:33 +00:00
|
|
|
"google.golang.org/grpc/status"
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrSwapFeeTooHigh is returned when the swap invoice amount is too
|
|
|
|
// high.
|
|
|
|
ErrSwapFeeTooHigh = errors.New("swap fee too high")
|
|
|
|
|
|
|
|
// ErrPrepayAmountTooHigh is returned when the prepay invoice amount is
|
|
|
|
// too high.
|
|
|
|
ErrPrepayAmountTooHigh = errors.New("prepay amount too high")
|
|
|
|
|
|
|
|
// ErrSwapAmountTooLow is returned when the requested swap amount is
|
|
|
|
// less than the server minimum.
|
|
|
|
ErrSwapAmountTooLow = errors.New("swap amount too low")
|
|
|
|
|
|
|
|
// ErrSwapAmountTooHigh is returned when the requested swap amount is
|
|
|
|
// more than the server maximum.
|
|
|
|
ErrSwapAmountTooHigh = errors.New("swap amount too high")
|
|
|
|
|
|
|
|
// ErrExpiryTooFar is returned when the server proposes an expiry that
|
|
|
|
// is too soon for us.
|
|
|
|
ErrExpiryTooFar = errors.New("swap expiry too far")
|
|
|
|
|
2023-11-27 12:46:53 +00:00
|
|
|
// ErrInsufficientBalance indicates insufficient confirmed balance to
|
|
|
|
// publish a swap.
|
|
|
|
ErrInsufficientBalance = errors.New("insufficient confirmed balance")
|
|
|
|
|
2019-11-27 12:36:48 +00:00
|
|
|
// serverRPCTimeout is the maximum time a gRPC request to the server
|
|
|
|
// should be allowed to take.
|
2019-03-06 20:13:50 +00:00
|
|
|
serverRPCTimeout = 30 * time.Second
|
|
|
|
|
2019-11-27 12:36:48 +00:00
|
|
|
// globalCallTimeout is the maximum time any call of the client to the
|
|
|
|
// server is allowed to take, including the time it may take to get
|
2024-04-17 18:57:59 +00:00
|
|
|
// and pay for an L402 token.
|
2024-04-17 17:13:01 +00:00
|
|
|
globalCallTimeout = serverRPCTimeout + l402.PaymentTimeout
|
2019-11-27 12:36:48 +00:00
|
|
|
|
2021-05-10 14:55:53 +00:00
|
|
|
// probeTimeout is the maximum time until a probe is allowed to take.
|
|
|
|
probeTimeout = 3 * time.Minute
|
|
|
|
|
2024-01-18 14:18:36 +00:00
|
|
|
repushDelay = 1 * time.Second
|
2020-02-21 09:27:57 +00:00
|
|
|
|
|
|
|
// MinerFeeEstimationFailed is a magic number that is returned in a
|
|
|
|
// quote call as the miner fee if the fee estimation in lnd's wallet
|
|
|
|
// failed because of insufficient funds.
|
|
|
|
MinerFeeEstimationFailed btcutil.Amount = -1
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
2019-03-07 02:22:46 +00:00
|
|
|
// Client performs the client side part of swaps. This interface exists to be
|
|
|
|
// able to implement a stub.
|
2019-03-06 20:13:50 +00:00
|
|
|
type Client struct {
|
|
|
|
started uint32 // To be used atomically.
|
|
|
|
errChan chan error
|
|
|
|
|
2023-11-13 13:50:10 +00:00
|
|
|
// abandonChans allows for accessing a swap's abandon channel by
|
|
|
|
// providing its swap hash. This map is used to look up the abandon
|
|
|
|
// channel of a swap if the client requests to abandon it.
|
|
|
|
abandonChans map[lntypes.Hash]chan struct{}
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
lndServices *lndclient.LndServices
|
|
|
|
sweeper *sweep.Sweeper
|
|
|
|
executor *executor
|
|
|
|
|
|
|
|
resumeReady chan struct{}
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
|
|
|
clientConfig
|
|
|
|
}
|
|
|
|
|
2020-04-27 12:03:23 +00:00
|
|
|
// ClientConfig is the exported configuration structure that is required to
|
|
|
|
// instantiate the loop client.
|
|
|
|
type ClientConfig struct {
|
|
|
|
// ServerAddress is the loop server to connect to.
|
|
|
|
ServerAddress string
|
|
|
|
|
|
|
|
// ProxyAddress is the SOCKS proxy that should be used to establish the
|
|
|
|
// connection.
|
|
|
|
ProxyAddress string
|
|
|
|
|
2020-06-15 09:04:37 +00:00
|
|
|
// SwapServerNoTLS skips TLS for the swap server connection when set.
|
|
|
|
SwapServerNoTLS bool
|
2020-04-27 12:03:23 +00:00
|
|
|
|
|
|
|
// TLSPathServer is the path to the TLS certificate that is required to
|
|
|
|
// connect to the server.
|
|
|
|
TLSPathServer string
|
|
|
|
|
|
|
|
// Lnd is an instance of the lnd proxy.
|
|
|
|
Lnd *lndclient.LndServices
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2024-04-17 18:57:59 +00:00
|
|
|
// MaxL402Cost is the maximum price we are willing to pay to the server
|
2020-04-27 12:03:23 +00:00
|
|
|
// for the token.
|
2024-04-17 18:57:59 +00:00
|
|
|
MaxL402Cost btcutil.Amount
|
2020-04-27 12:03:23 +00:00
|
|
|
|
2024-04-17 18:57:59 +00:00
|
|
|
// MaxL402Fee is the maximum that we are willing to pay in routing fees
|
2020-04-27 12:03:23 +00:00
|
|
|
// to obtain the token.
|
2024-04-17 18:57:59 +00:00
|
|
|
MaxL402Fee btcutil.Amount
|
2020-04-27 12:06:08 +00:00
|
|
|
|
|
|
|
// LoopOutMaxParts defines the maximum number of parts that may be used
|
|
|
|
// for a loop out swap. When greater than one, a multi-part payment may
|
|
|
|
// be attempted.
|
|
|
|
LoopOutMaxParts uint32
|
2021-12-01 14:18:10 +00:00
|
|
|
|
|
|
|
// TotalPaymentTimeout is the total amount of time until we time out
|
|
|
|
// off-chain payments (used in loop out).
|
|
|
|
TotalPaymentTimeout time.Duration
|
|
|
|
|
|
|
|
// MaxPaymentRetries is the maximum times we retry an off-chain payment
|
|
|
|
// (used in loop out).
|
|
|
|
MaxPaymentRetries int
|
2020-04-27 12:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient returns a new instance to initiate swaps with.
|
2023-05-19 13:09:38 +00:00
|
|
|
func NewClient(dbDir string, loopDB loopdb.SwapStore,
|
2024-01-18 14:18:36 +00:00
|
|
|
sweeperDb sweepbatcher.BatcherStore, cfg *ClientConfig) (
|
|
|
|
*Client, func(), error) {
|
2023-05-19 13:09:38 +00:00
|
|
|
|
2024-04-17 18:57:59 +00:00
|
|
|
l402Store, err := l402.NewFileStore(dbDir)
|
2019-11-08 09:37:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2024-04-17 18:57:59 +00:00
|
|
|
swapServerClient, err := newSwapServerClient(cfg, l402Store)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
config := &clientConfig{
|
2020-04-27 12:03:23 +00:00
|
|
|
LndServices: cfg.Lnd,
|
2019-03-06 20:13:50 +00:00
|
|
|
Server: swapServerClient,
|
2023-05-19 13:09:38 +00:00
|
|
|
Store: loopDB,
|
2023-08-24 23:41:45 +00:00
|
|
|
Conn: swapServerClient.conn,
|
2024-04-17 18:57:59 +00:00
|
|
|
L402Store: l402Store,
|
2019-03-06 20:13:50 +00:00
|
|
|
CreateExpiryTimer: func(d time.Duration) <-chan time.Time {
|
|
|
|
return time.NewTimer(d).C
|
|
|
|
},
|
2021-03-11 10:20:11 +00:00
|
|
|
LoopOutMaxParts: cfg.LoopOutMaxParts,
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sweeper := &sweep.Sweeper{
|
2020-04-27 12:03:23 +00:00
|
|
|
Lnd: cfg.Lnd,
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2024-01-18 14:18:36 +00:00
|
|
|
verifySchnorrSig := func(pubKey *btcec.PublicKey, hash, sig []byte) error {
|
|
|
|
schnorrSig, err := schnorr.ParseSignature(sig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !schnorrSig.Verify(hash, pubKey) {
|
|
|
|
return fmt.Errorf("invalid signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
batcher := sweepbatcher.NewBatcher(
|
|
|
|
cfg.Lnd.WalletKit, cfg.Lnd.ChainNotifier, cfg.Lnd.Signer,
|
|
|
|
swapServerClient.MultiMuSig2SignSweep, verifySchnorrSig,
|
|
|
|
cfg.Lnd.ChainParams, sweeperDb, loopDB,
|
|
|
|
)
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
executor := newExecutor(&executorConfig{
|
2021-12-01 14:18:10 +00:00
|
|
|
lnd: cfg.Lnd,
|
2023-05-19 13:09:38 +00:00
|
|
|
store: loopDB,
|
2021-12-01 14:18:10 +00:00
|
|
|
sweeper: sweeper,
|
2024-01-18 14:18:36 +00:00
|
|
|
batcher: batcher,
|
2021-12-01 14:18:10 +00:00
|
|
|
createExpiryTimer: config.CreateExpiryTimer,
|
|
|
|
loopOutMaxParts: cfg.LoopOutMaxParts,
|
|
|
|
totalPaymentTimeout: cfg.TotalPaymentTimeout,
|
|
|
|
maxPaymentRetries: cfg.MaxPaymentRetries,
|
|
|
|
cancelSwap: swapServerClient.CancelLoopOutSwap,
|
2024-01-18 14:18:36 +00:00
|
|
|
verifySchnorrSig: verifySchnorrSig,
|
2019-03-06 20:13:50 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
client := &Client{
|
|
|
|
errChan: make(chan error),
|
|
|
|
clientConfig: *config,
|
2020-04-27 12:03:23 +00:00
|
|
|
lndServices: cfg.Lnd,
|
2019-03-06 20:13:50 +00:00
|
|
|
sweeper: sweeper,
|
|
|
|
executor: executor,
|
|
|
|
resumeReady: make(chan struct{}),
|
2023-11-13 13:50:10 +00:00
|
|
|
abandonChans: make(map[lntypes.Hash]chan struct{}),
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup := func() {
|
2020-07-16 10:01:02 +00:00
|
|
|
swapServerClient.stop()
|
2023-05-19 13:09:38 +00:00
|
|
|
loopDB.Close()
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return client, cleanup, nil
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:41:45 +00:00
|
|
|
// GetConn returns the gRPC connection to the server.
|
|
|
|
func (s *Client) GetConn() *grpc.ClientConn {
|
|
|
|
return s.clientConfig.Conn
|
|
|
|
}
|
|
|
|
|
2019-04-11 07:06:59 +00:00
|
|
|
// FetchSwaps returns all loop in and out swaps currently in the database.
|
2023-05-16 15:40:52 +00:00
|
|
|
func (s *Client) FetchSwaps(ctx context.Context) ([]*SwapInfo, error) {
|
|
|
|
loopOutSwaps, err := s.Store.FetchLoopOutSwaps(ctx)
|
2019-04-11 07:06:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-16 15:40:52 +00:00
|
|
|
loopInSwaps, err := s.Store.FetchLoopInSwaps(ctx)
|
2019-04-11 07:06:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
swaps := make([]*SwapInfo, 0, len(loopInSwaps)+len(loopOutSwaps))
|
|
|
|
|
|
|
|
for _, swp := range loopOutSwaps {
|
2023-09-28 11:16:01 +00:00
|
|
|
swp := swp
|
|
|
|
|
2022-04-24 20:59:59 +00:00
|
|
|
swapInfo := &SwapInfo{
|
|
|
|
SwapType: swap.TypeOut,
|
|
|
|
SwapContract: swp.Contract.SwapContract,
|
|
|
|
SwapStateData: swp.State(),
|
|
|
|
SwapHash: swp.Hash,
|
|
|
|
LastUpdate: swp.LastUpdateTime(),
|
|
|
|
}
|
|
|
|
|
2023-11-14 00:52:39 +00:00
|
|
|
htlc, err := utils.GetHtlc(
|
2022-11-16 18:01:28 +00:00
|
|
|
swp.Hash, &swp.Contract.SwapContract,
|
|
|
|
s.lndServices.ChainParams,
|
2019-04-11 07:06:59 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-16 18:01:28 +00:00
|
|
|
switch htlc.OutputType {
|
|
|
|
case swap.HtlcP2WSH:
|
2022-04-24 20:59:59 +00:00
|
|
|
swapInfo.HtlcAddressP2WSH = htlc.Address
|
2022-11-16 18:01:28 +00:00
|
|
|
|
|
|
|
case swap.HtlcP2TR:
|
|
|
|
swapInfo.HtlcAddressP2TR = htlc.Address
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, swap.ErrInvalidOutputType
|
2022-04-24 20:59:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
swaps = append(swaps, swapInfo)
|
2019-04-11 07:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, swp := range loopInSwaps {
|
2023-09-28 11:16:01 +00:00
|
|
|
swp := swp
|
|
|
|
|
2022-04-24 20:59:41 +00:00
|
|
|
swapInfo := &SwapInfo{
|
|
|
|
SwapType: swap.TypeIn,
|
|
|
|
SwapContract: swp.Contract.SwapContract,
|
|
|
|
SwapStateData: swp.State(),
|
|
|
|
SwapHash: swp.Hash,
|
|
|
|
LastUpdate: swp.LastUpdateTime(),
|
2019-04-11 07:06:59 +00:00
|
|
|
}
|
|
|
|
|
2023-11-14 00:52:39 +00:00
|
|
|
htlc, err := utils.GetHtlc(
|
2022-11-16 18:01:28 +00:00
|
|
|
swp.Hash, &swp.Contract.SwapContract,
|
|
|
|
s.lndServices.ChainParams,
|
2020-04-24 15:35:32 +00:00
|
|
|
)
|
2022-11-16 18:01:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-24 20:59:41 +00:00
|
|
|
|
2022-11-16 18:01:28 +00:00
|
|
|
switch htlc.OutputType {
|
|
|
|
case swap.HtlcP2WSH:
|
|
|
|
swapInfo.HtlcAddressP2WSH = htlc.Address
|
2022-04-24 20:59:41 +00:00
|
|
|
|
2022-11-16 18:01:28 +00:00
|
|
|
case swap.HtlcP2TR:
|
|
|
|
swapInfo.HtlcAddressP2TR = htlc.Address
|
2022-04-24 20:59:41 +00:00
|
|
|
|
2022-11-16 18:01:28 +00:00
|
|
|
default:
|
|
|
|
return nil, swap.ErrInvalidOutputType
|
2020-04-24 15:35:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-24 20:59:41 +00:00
|
|
|
swaps = append(swaps, swapInfo)
|
2019-04-11 07:06:59 +00:00
|
|
|
}
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-04-11 07:06:59 +00:00
|
|
|
return swaps, nil
|
2019-03-12 15:10:37 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
// Run is a blocking call that executes all swaps. Any pending swaps are
|
2019-03-07 02:22:46 +00:00
|
|
|
// restored from persistent storage and resumed. Subsequent updates will be
|
|
|
|
// sent through the passed in statusChan. The function can be terminated by
|
|
|
|
// cancelling the context.
|
2023-08-10 12:51:38 +00:00
|
|
|
func (s *Client) Run(ctx context.Context, statusChan chan<- SwapInfo) error {
|
2019-03-06 20:13:50 +00:00
|
|
|
if !atomic.CompareAndSwapUint32(&s.started, 0, 1) {
|
|
|
|
return errors.New("swap client can only be started once")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log connected node.
|
2021-05-19 11:35:36 +00:00
|
|
|
log.Infof("Connected to lnd node '%v' with pubkey %s (version %s)",
|
2020-04-22 13:48:34 +00:00
|
|
|
s.lndServices.NodeAlias, s.lndServices.NodePubkey,
|
|
|
|
lndclient.VersionString(s.lndServices.Version))
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2023-08-10 12:51:38 +00:00
|
|
|
// Setup main context used for cancellation.
|
2019-03-06 20:13:50 +00:00
|
|
|
mainCtx, mainCancel := context.WithCancel(ctx)
|
|
|
|
defer mainCancel()
|
|
|
|
|
|
|
|
// Query store before starting event loop to prevent new swaps from
|
|
|
|
// being treated as swaps that need to be resumed.
|
2023-05-16 15:40:52 +00:00
|
|
|
pendingLoopOutSwaps, err := s.Store.FetchLoopOutSwaps(mainCtx)
|
2019-03-12 15:10:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-16 15:40:52 +00:00
|
|
|
pendingLoopInSwaps, err := s.Store.FetchLoopInSwaps(mainCtx)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start goroutine to deliver all pending swaps to the main loop.
|
|
|
|
s.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer s.wg.Done()
|
|
|
|
|
2019-03-12 15:10:37 +00:00
|
|
|
s.resumeSwaps(mainCtx, pendingLoopOutSwaps, pendingLoopInSwaps)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2023-08-10 12:51:38 +00:00
|
|
|
// Signal that new requests can be accepted. Otherwise, the new
|
2019-03-06 20:13:50 +00:00
|
|
|
// swap could already have been added to the store and read in
|
|
|
|
// this goroutine as being a swap that needs to be resumed.
|
|
|
|
// Resulting in two goroutines executing the same swap.
|
|
|
|
close(s.resumeReady)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Main event loop.
|
2023-11-13 13:50:10 +00:00
|
|
|
err = s.executor.run(mainCtx, statusChan, s.abandonChans)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
// Consider canceled as happy flow.
|
2023-11-13 13:50:10 +00:00
|
|
|
if errors.Is(err, context.Canceled) {
|
2019-03-06 20:13:50 +00:00
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Errorf("Swap client terminating: %v", err)
|
2019-03-06 20:13:50 +00:00
|
|
|
} else {
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Info("Swap client terminating")
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel all remaining active goroutines.
|
|
|
|
mainCancel()
|
|
|
|
|
|
|
|
// Wait for all to finish.
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Debug("Wait for executor to finish")
|
2019-03-06 20:13:50 +00:00
|
|
|
s.executor.waitFinished()
|
|
|
|
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Debug("Wait for goroutines to finish")
|
2019-03-06 20:13:50 +00:00
|
|
|
s.wg.Wait()
|
|
|
|
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Info("Swap client terminated")
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// resumeSwaps restarts all pending swaps from the provided list.
|
|
|
|
func (s *Client) resumeSwaps(ctx context.Context,
|
2019-03-12 15:10:37 +00:00
|
|
|
loopOutSwaps []*loopdb.LoopOut, loopInSwaps []*loopdb.LoopIn) {
|
|
|
|
|
2020-06-08 10:53:07 +00:00
|
|
|
swapCfg := newSwapConfig(s.lndServices, s.Store, s.Server)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-03-12 15:10:37 +00:00
|
|
|
for _, pend := range loopOutSwaps {
|
2019-05-15 11:55:41 +00:00
|
|
|
if pend.State().State.Type() != loopdb.StateTypePending {
|
2019-03-06 20:13:50 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-12-15 15:43:38 +00:00
|
|
|
swap, err := resumeLoopOutSwap(swapCfg, pend)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Errorf("resuming loop out swap: %v", err)
|
2019-03-12 15:10:37 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
s.executor.initiateSwap(ctx, swap)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pend := range loopInSwaps {
|
2019-05-15 11:55:41 +00:00
|
|
|
if pend.State().State.Type() != loopdb.StateTypePending {
|
2019-03-12 15:10:37 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
swap, err := resumeLoopInSwap(ctx, swapCfg, pend)
|
|
|
|
if err != nil {
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Errorf("resuming loop in swap: %v", err)
|
2019-03-06 20:13:50 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-11-21 16:20:36 +00:00
|
|
|
// Store the swap's abandon channel so that the client can
|
|
|
|
// abandon the swap by providing the swap hash.
|
|
|
|
s.executor.Lock()
|
|
|
|
s.abandonChans[swap.hash] = swap.abandonChan
|
|
|
|
s.executor.Unlock()
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
s.executor.initiateSwap(ctx, swap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
// LoopOut initiates a loop out swap. It blocks until the swap is initiation
|
2019-03-07 02:22:46 +00:00
|
|
|
// with the swap server is completed (typically this takes only a short amount
|
|
|
|
// of time). From there on further status information can be acquired through
|
|
|
|
// the status channel returned from the Run call.
|
2019-03-06 20:13:50 +00:00
|
|
|
//
|
2019-03-07 04:32:24 +00:00
|
|
|
// When the call returns, the swap has been persisted and will be resumed
|
|
|
|
// automatically after restarts.
|
2019-03-06 20:13:50 +00:00
|
|
|
//
|
|
|
|
// The return value is a hash that uniquely identifies the new swap.
|
2019-03-07 04:32:24 +00:00
|
|
|
func (s *Client) LoopOut(globalCtx context.Context,
|
2020-06-30 11:45:12 +00:00
|
|
|
request *OutRequest) (*LoopOutSwapInfo, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2020-05-19 07:54:27 +00:00
|
|
|
log.Infof("LoopOut %v to %v (channels: %v)",
|
|
|
|
request.Amount, request.DestAddr, request.OutgoingChanSet,
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if err := s.waitForInitialized(globalCtx); err != nil {
|
2020-06-30 11:45:12 +00:00
|
|
|
return nil, err
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 12:54:06 +00:00
|
|
|
// Calculate htlc expiry height.
|
2023-06-20 18:52:07 +00:00
|
|
|
terms, err := s.Server.GetLoopOutTerms(globalCtx, request.Initiator)
|
2020-07-14 12:54:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
initiationHeight := s.executor.height()
|
2020-07-14 12:54:06 +00:00
|
|
|
request.Expiry, err = s.getExpiry(
|
|
|
|
initiationHeight, terms, request.SweepConfTarget,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new swap object for this swap.
|
2020-06-08 10:53:07 +00:00
|
|
|
swapCfg := newSwapConfig(s.lndServices, s.Store, s.Server)
|
2020-06-30 12:06:36 +00:00
|
|
|
initResult, err := newLoopOutSwap(
|
2019-03-06 20:13:50 +00:00
|
|
|
globalCtx, swapCfg, initiationHeight, request,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2020-06-30 11:45:12 +00:00
|
|
|
return nil, err
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
2020-06-30 12:06:36 +00:00
|
|
|
swap := initResult.swap
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
// Post swap to the main loop.
|
|
|
|
s.executor.initiateSwap(globalCtx, swap)
|
|
|
|
|
|
|
|
// Return hash so that the caller can identify this swap in the updates
|
|
|
|
// stream.
|
2020-06-30 11:45:12 +00:00
|
|
|
return &LoopOutSwapInfo{
|
2022-04-24 20:59:59 +00:00
|
|
|
SwapHash: swap.hash,
|
|
|
|
HtlcAddress: swap.htlc.Address,
|
|
|
|
ServerMessage: initResult.serverMessage,
|
2020-06-30 11:45:12 +00:00
|
|
|
}, nil
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 12:54:06 +00:00
|
|
|
// getExpiry returns an absolute expiry height based on the sweep confirmation
|
|
|
|
// target, constrained by the server terms.
|
|
|
|
func (s *Client) getExpiry(height int32, terms *LoopOutTerms,
|
|
|
|
confTarget int32) (int32, error) {
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case confTarget < terms.MinCltvDelta:
|
|
|
|
return height + terms.MinCltvDelta, nil
|
|
|
|
|
|
|
|
case confTarget > terms.MaxCltvDelta:
|
|
|
|
return 0, fmt.Errorf("confirmation target %v exceeds maximum "+
|
|
|
|
"server cltv delta of %v", confTarget,
|
|
|
|
terms.MaxCltvDelta)
|
|
|
|
}
|
|
|
|
|
|
|
|
return height + confTarget, nil
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
// LoopOutQuote takes a LoopOut amount and returns a break down of estimated
|
2019-03-07 02:22:46 +00:00
|
|
|
// costs for the client. Both the swap server and the on-chain fee estimator
|
|
|
|
// are queried to get to build the quote response.
|
2019-03-07 04:32:24 +00:00
|
|
|
func (s *Client) LoopOutQuote(ctx context.Context,
|
|
|
|
request *LoopOutQuoteRequest) (*LoopOutQuote, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2023-06-20 18:52:07 +00:00
|
|
|
terms, err := s.Server.GetLoopOutTerms(ctx, request.Initiator)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.Amount < terms.MinSwapAmount {
|
|
|
|
return nil, ErrSwapAmountTooLow
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.Amount > terms.MaxSwapAmount {
|
|
|
|
return nil, ErrSwapAmountTooHigh
|
|
|
|
}
|
|
|
|
|
2020-07-14 12:54:06 +00:00
|
|
|
height := s.executor.height()
|
|
|
|
expiry, err := s.getExpiry(height, terms, request.SweepConfTarget)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-07 12:53:18 +00:00
|
|
|
quote, err := s.Server.GetLoopOutQuote(
|
2020-07-14 12:54:06 +00:00
|
|
|
ctx, request.Amount, expiry, request.SwapPublicationDeadline,
|
2023-06-20 18:52:07 +00:00
|
|
|
request.Initiator,
|
2020-01-07 12:53:18 +00:00
|
|
|
)
|
2019-10-08 20:28:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Infof("Offchain swap destination: %x", quote.SwapPaymentDest)
|
2019-10-08 20:28:20 +00:00
|
|
|
|
2022-05-16 16:37:53 +00:00
|
|
|
minerFee, err := s.getLoopOutSweepFee(ctx, request.SweepConfTarget)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &LoopOutQuote{
|
|
|
|
SwapFee: quote.SwapFee,
|
|
|
|
MinerFee: minerFee,
|
|
|
|
PrepayAmount: quote.PrepayAmount,
|
|
|
|
SwapPaymentDest: quote.SwapPaymentDest,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getLoopOutSweepFee is a helper method to estimate the loop out htlc sweep
|
|
|
|
// fee to a p2wsh address.
|
|
|
|
func (s *Client) getLoopOutSweepFee(ctx context.Context, confTarget int32) (
|
|
|
|
btcutil.Amount, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-07-30 11:37:47 +00:00
|
|
|
// Generate dummy p2wsh address for fee estimation. The p2wsh address
|
|
|
|
// type is chosen because it adds the most weight of all output types
|
|
|
|
// and we want the quote to return a worst case value.
|
|
|
|
wsh := [32]byte{}
|
|
|
|
p2wshAddress, err := btcutil.NewAddressWitnessScriptHash(
|
|
|
|
wsh[:], s.lndServices.ChainParams,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2022-05-16 16:37:53 +00:00
|
|
|
return 0, err
|
2019-07-30 11:37:47 +00:00
|
|
|
}
|
|
|
|
|
2023-11-14 00:52:39 +00:00
|
|
|
scriptVersion := utils.GetHtlcScriptVersion(
|
2022-05-16 16:37:53 +00:00
|
|
|
loopdb.CurrentProtocolVersion(),
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
2022-05-16 16:37:53 +00:00
|
|
|
|
|
|
|
htlc := swap.QuoteHtlcP2TR
|
|
|
|
if scriptVersion != swap.HtlcV3 {
|
|
|
|
htlc = swap.QuoteHtlcP2WSH
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 16:37:53 +00:00
|
|
|
return s.sweeper.GetSweepFee(
|
|
|
|
ctx, htlc.AddSuccessToEstimator, p2wshAddress, confTarget,
|
|
|
|
)
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
// LoopOutTerms returns the terms on which the server executes swaps.
|
2023-06-20 18:52:07 +00:00
|
|
|
func (s *Client) LoopOutTerms(ctx context.Context, initiator string) (
|
2019-03-07 04:32:24 +00:00
|
|
|
*LoopOutTerms, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2023-06-20 18:52:07 +00:00
|
|
|
return s.Server.GetLoopOutTerms(ctx, initiator)
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// waitForInitialized for swaps to be resumed and executor ready.
|
|
|
|
func (s *Client) waitForInitialized(ctx context.Context) error {
|
|
|
|
select {
|
|
|
|
case <-s.executor.ready:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-s.resumeReady:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-03-12 15:10:37 +00:00
|
|
|
|
|
|
|
// LoopIn initiates a loop in swap.
|
|
|
|
func (s *Client) LoopIn(globalCtx context.Context,
|
2020-04-29 16:30:44 +00:00
|
|
|
request *LoopInRequest) (*LoopInSwapInfo, error) {
|
2019-03-12 15:10:37 +00:00
|
|
|
|
2020-02-11 12:25:03 +00:00
|
|
|
log.Infof("Loop in %v (last hop: %v)",
|
2019-03-12 15:10:37 +00:00
|
|
|
request.Amount,
|
2020-02-11 12:25:03 +00:00
|
|
|
request.LastHop,
|
2019-03-12 15:10:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if err := s.waitForInitialized(globalCtx); err != nil {
|
2020-04-29 16:30:44 +00:00
|
|
|
return nil, err
|
2019-03-12 15:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new swap object for this swap.
|
|
|
|
initiationHeight := s.executor.height()
|
2020-06-08 10:53:07 +00:00
|
|
|
swapCfg := newSwapConfig(s.lndServices, s.Store, s.Server)
|
2020-06-30 12:06:36 +00:00
|
|
|
initResult, err := newLoopInSwap(
|
2020-06-08 10:53:07 +00:00
|
|
|
globalCtx, swapCfg, initiationHeight, request,
|
2019-03-12 15:10:37 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2020-04-29 16:30:44 +00:00
|
|
|
return nil, err
|
2019-03-12 15:10:37 +00:00
|
|
|
}
|
2020-06-30 12:06:36 +00:00
|
|
|
swap := initResult.swap
|
2019-03-12 15:10:37 +00:00
|
|
|
|
2023-11-13 13:50:10 +00:00
|
|
|
s.executor.Lock()
|
|
|
|
s.abandonChans[swap.hash] = swap.abandonChan
|
|
|
|
s.executor.Unlock()
|
|
|
|
|
2019-03-12 15:10:37 +00:00
|
|
|
// Post swap to the main loop.
|
|
|
|
s.executor.initiateSwap(globalCtx, swap)
|
|
|
|
|
|
|
|
// Return hash so that the caller can identify this swap in the updates
|
|
|
|
// stream.
|
2020-04-29 16:30:44 +00:00
|
|
|
swapInfo := &LoopInSwapInfo{
|
2022-04-24 20:59:41 +00:00
|
|
|
SwapHash: swap.hash,
|
|
|
|
ServerMessage: initResult.serverMessage,
|
2020-04-29 16:30:44 +00:00
|
|
|
}
|
2022-04-24 20:59:41 +00:00
|
|
|
|
|
|
|
if loopdb.CurrentProtocolVersion() < loopdb.ProtocolVersionHtlcV3 {
|
|
|
|
swapInfo.HtlcAddressP2WSH = swap.htlcP2WSH.Address
|
|
|
|
} else {
|
|
|
|
swapInfo.HtlcAddressP2TR = swap.htlcP2TR.Address
|
|
|
|
}
|
|
|
|
|
2020-04-29 16:30:44 +00:00
|
|
|
return swapInfo, nil
|
2019-03-12 15:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoopInQuote takes an amount and returns a break down of estimated
|
|
|
|
// costs for the client. Both the swap server and the on-chain fee estimator are
|
|
|
|
// queried to get to build the quote response.
|
|
|
|
func (s *Client) LoopInQuote(ctx context.Context,
|
|
|
|
request *LoopInQuoteRequest) (*LoopInQuote, error) {
|
|
|
|
|
|
|
|
// Retrieve current server terms to calculate swap fee.
|
2023-06-20 18:52:07 +00:00
|
|
|
terms, err := s.Server.GetLoopInTerms(ctx, request.Initiator)
|
2019-03-12 15:10:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check amount limits.
|
|
|
|
if request.Amount < terms.MinSwapAmount {
|
|
|
|
return nil, ErrSwapAmountTooLow
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.Amount > terms.MaxSwapAmount {
|
|
|
|
return nil, ErrSwapAmountTooHigh
|
|
|
|
}
|
|
|
|
|
2021-10-19 22:05:59 +00:00
|
|
|
// Private and routehints are mutually exclusive as setting private
|
|
|
|
// means we retrieve our own routehints from the connected node.
|
|
|
|
if len(request.RouteHints) != 0 && request.Private {
|
|
|
|
return nil, fmt.Errorf("private and route_hints both set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.Private {
|
|
|
|
// If last_hop is set, we'll only add channels with peers
|
|
|
|
// set to the last_hop parameter
|
|
|
|
includeNodes := make(map[route.Vertex]struct{})
|
|
|
|
if request.LastHop != nil {
|
|
|
|
includeNodes[*request.LastHop] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Because the Private flag is set, we'll generate our own
|
|
|
|
// set of hop hints and use that
|
|
|
|
request.RouteHints, err = SelectHopHints(
|
|
|
|
ctx, s.lndServices, request.Amount, DefaultMaxHopHints, includeNodes,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 14:55:53 +00:00
|
|
|
quote, err := s.Server.GetLoopInQuote(
|
|
|
|
ctx, request.Amount, s.lndServices.NodePubkey, request.LastHop,
|
2023-06-20 18:52:07 +00:00
|
|
|
request.RouteHints, request.Initiator,
|
2021-05-10 14:55:53 +00:00
|
|
|
)
|
2019-10-08 20:28:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
swapFee := quote.SwapFee
|
2019-03-12 15:10:37 +00:00
|
|
|
|
2019-09-23 08:08:34 +00:00
|
|
|
// We don't calculate the on-chain fee if the HTLC is going to be
|
|
|
|
// published externally.
|
|
|
|
if request.ExternalHtlc {
|
|
|
|
return &LoopInQuote{
|
|
|
|
SwapFee: swapFee,
|
|
|
|
MinerFee: 0,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-10-08 20:28:20 +00:00
|
|
|
|
2020-02-21 09:27:57 +00:00
|
|
|
// Get estimate for miner fee. If estimating the miner fee for the
|
|
|
|
// requested amount is not possible because lnd's wallet cannot
|
|
|
|
// construct a sample TX, we just return zero instead of failing the
|
|
|
|
// quote. The user interface should inform the user that fee estimation
|
|
|
|
// was not possible.
|
|
|
|
//
|
|
|
|
// TODO(guggero): Thread through error code from lnd to avoid string
|
|
|
|
// matching.
|
2022-05-16 16:38:50 +00:00
|
|
|
minerFee, err := s.estimateFee(
|
2019-03-12 15:10:37 +00:00
|
|
|
ctx, request.Amount, request.HtlcConfTarget,
|
|
|
|
)
|
2020-02-21 09:27:57 +00:00
|
|
|
if err != nil && strings.Contains(err.Error(), "insufficient funds") {
|
|
|
|
return &LoopInQuote{
|
|
|
|
SwapFee: swapFee,
|
|
|
|
MinerFee: MinerFeeEstimationFailed,
|
|
|
|
CltvDelta: quote.CltvDelta,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-03-12 15:10:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &LoopInQuote{
|
2019-10-08 20:28:20 +00:00
|
|
|
SwapFee: swapFee,
|
|
|
|
MinerFee: minerFee,
|
|
|
|
CltvDelta: quote.CltvDelta,
|
2019-03-12 15:10:37 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-05-16 16:38:50 +00:00
|
|
|
// estimateFee is a helper method to estimate the total fee for paying the
|
|
|
|
// passed amount with the given conf target. It'll assume taproot destination
|
|
|
|
// if the protocol version indicates that we're using taproot htlcs.
|
|
|
|
func (s *Client) estimateFee(ctx context.Context, amt btcutil.Amount,
|
|
|
|
confTarget int32) (btcutil.Amount, error) {
|
|
|
|
|
|
|
|
var (
|
|
|
|
address btcutil.Address
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
// Generate a dummy address for fee estimation.
|
|
|
|
witnessProg := [32]byte{}
|
|
|
|
|
2023-11-14 00:52:39 +00:00
|
|
|
scriptVersion := utils.GetHtlcScriptVersion(
|
2022-05-16 16:38:50 +00:00
|
|
|
loopdb.CurrentProtocolVersion(),
|
|
|
|
)
|
|
|
|
|
|
|
|
if scriptVersion != swap.HtlcV3 {
|
|
|
|
address, err = btcutil.NewAddressWitnessScriptHash(
|
|
|
|
witnessProg[:], s.lndServices.ChainParams,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
address, err = btcutil.NewAddressTaproot(
|
|
|
|
witnessProg[:], s.lndServices.ChainParams,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.lndServices.Client.EstimateFee(ctx, address, amt, confTarget)
|
|
|
|
}
|
|
|
|
|
2019-03-12 15:10:37 +00:00
|
|
|
// LoopInTerms returns the terms on which the server executes swaps.
|
2023-06-20 18:52:07 +00:00
|
|
|
func (s *Client) LoopInTerms(ctx context.Context, initiator string) (
|
2019-03-12 15:10:37 +00:00
|
|
|
*LoopInTerms, error) {
|
|
|
|
|
2023-06-20 18:52:07 +00:00
|
|
|
return s.Server.GetLoopInTerms(ctx, initiator)
|
2019-03-12 15:10:37 +00:00
|
|
|
}
|
2021-07-19 08:01:33 +00:00
|
|
|
|
|
|
|
// wrapGrpcError wraps the non-nil error provided with a message providing
|
|
|
|
// additional context, preserving the grpc code returned with the original
|
|
|
|
// error. If the original error has no grpc code, then codes.Unknown is used.
|
|
|
|
func wrapGrpcError(message string, err error) error {
|
|
|
|
// Since our error is non-nil, we don't need to worry about a nil
|
|
|
|
// grpcStatus, we'll just get an unknown one if no code was passed back.
|
|
|
|
grpcStatus, _ := status.FromError(err)
|
|
|
|
|
|
|
|
return status.Error(
|
|
|
|
grpcStatus.Code(), fmt.Sprintf("%v: %v", message,
|
|
|
|
grpcStatus.Message()),
|
|
|
|
)
|
|
|
|
}
|
2021-05-10 14:55:53 +00:00
|
|
|
|
|
|
|
// Probe asks the server to probe a route to us given a requested amount and
|
|
|
|
// last hop. The server is free to discard frequent request to avoid abuse or if
|
|
|
|
// there's been a recent probe to us for the same amount.
|
|
|
|
func (s *Client) Probe(ctx context.Context, req *ProbeRequest) error {
|
|
|
|
return s.Server.Probe(
|
|
|
|
ctx, req.Amount, s.lndServices.NodePubkey, req.LastHop,
|
|
|
|
req.RouteHints,
|
|
|
|
)
|
|
|
|
}
|
2023-11-13 13:50:10 +00:00
|
|
|
|
|
|
|
// AbandonSwap sends a signal on the abandon channel of the swap identified by
|
|
|
|
// the passed swap hash. This will cause the swap to abandon itself.
|
|
|
|
func (s *Client) AbandonSwap(ctx context.Context,
|
|
|
|
req *AbandonSwapRequest) error {
|
|
|
|
|
|
|
|
if req == nil {
|
|
|
|
return errors.New("no request provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
s.executor.Lock()
|
|
|
|
defer s.executor.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case s.abandonChans[req.SwapHash] <- struct{}{}:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
// This is to avoid writing to a full channel.
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|