2019-03-07 02:22:46 +00:00
|
|
|
package loop
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
import (
|
2022-07-27 18:33:07 +00:00
|
|
|
"bytes"
|
2019-03-06 20:13:50 +00:00
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
2020-04-14 09:07:06 +00:00
|
|
|
"errors"
|
2019-03-06 20:13:50 +00:00
|
|
|
"fmt"
|
2021-05-24 06:46:03 +00:00
|
|
|
"math"
|
2020-07-16 10:01:02 +00:00
|
|
|
"sync"
|
2019-03-06 20:13:50 +00:00
|
|
|
"time"
|
|
|
|
|
2022-06-24 11:57:42 +00:00
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
2022-05-05 09:59:31 +00:00
|
|
|
"github.com/btcsuite/btcd/btcec/v2/schnorr/musig2"
|
2022-03-14 12:36:02 +00:00
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
2020-06-23 09:49:43 +00:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2022-05-05 09:59:31 +00:00
|
|
|
"github.com/btcsuite/btcd/txscript"
|
2019-03-06 20:13:50 +00:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2020-06-17 20:25:57 +00:00
|
|
|
"github.com/lightninglabs/lndclient"
|
2020-10-15 07:15:25 +00:00
|
|
|
"github.com/lightninglabs/loop/labels"
|
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"
|
2019-03-06 20:13:50 +00:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2020-04-14 09:07:06 +00:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2022-05-05 09:59:31 +00:00
|
|
|
"github.com/lightningnetwork/lnd/input"
|
2020-04-14 09:07:06 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2019-03-06 20:13:50 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2021-05-24 06:46:03 +00:00
|
|
|
"github.com/lightningnetwork/lnd/zpay32"
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
2022-05-05 09:59:31 +00:00
|
|
|
const (
|
|
|
|
// loopInternalHops indicate the number of hops that a loop out swap
|
|
|
|
// makes in the server's off-chain infrastructure. We are ok reporting
|
|
|
|
// failure distances from the server up until this point, because every
|
|
|
|
// swap takes these two hops, so surfacing this information does not
|
|
|
|
// identify the client in any way. After this point, the client does not
|
|
|
|
// report failure distances, so that sender-privacy is preserved.
|
|
|
|
loopInternalHops = 2
|
|
|
|
|
|
|
|
// We'll try to sweep with MuSig2 at most 10 times. If that fails we'll
|
|
|
|
// fail back to using standard scriptspend sweep.
|
|
|
|
maxMusigSweepRetries = 10
|
|
|
|
)
|
2021-05-24 06:46:03 +00:00
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
var (
|
2019-03-07 04:32:24 +00:00
|
|
|
// MinLoopOutPreimageRevealDelta configures the minimum number of
|
|
|
|
// remaining blocks before htlc expiry required to reveal preimage.
|
2019-06-25 18:41:49 +00:00
|
|
|
MinLoopOutPreimageRevealDelta int32 = 20
|
|
|
|
|
|
|
|
// DefaultSweepConfTarget is the default confirmation target we'll use
|
|
|
|
// when sweeping on-chain HTLCs.
|
2020-08-24 20:40:28 +00:00
|
|
|
DefaultSweepConfTarget int32 = 9
|
2019-06-25 18:41:57 +00:00
|
|
|
|
2020-04-15 07:10:21 +00:00
|
|
|
// DefaultHtlcConfTarget is the default confirmation target we'll use
|
|
|
|
// for on-chain htlcs published by the swap client for Loop In.
|
|
|
|
DefaultHtlcConfTarget int32 = 6
|
|
|
|
|
2019-06-25 18:41:57 +00:00
|
|
|
// DefaultSweepConfTargetDelta is the delta of blocks from a Loop Out
|
|
|
|
// swap's expiration height at which we begin to use the default sweep
|
|
|
|
// confirmation target.
|
|
|
|
//
|
|
|
|
// TODO(wilmer): tune?
|
2019-07-30 11:22:15 +00:00
|
|
|
DefaultSweepConfTargetDelta = DefaultSweepConfTarget * 2
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
// loopOutSwap contains all the in-memory state related to a pending loop out
|
2019-03-06 20:13:50 +00:00
|
|
|
// swap.
|
2019-03-07 04:32:24 +00:00
|
|
|
type loopOutSwap struct {
|
2019-03-06 20:13:50 +00:00
|
|
|
swapKit
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
loopdb.LoopOutContract
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2020-04-29 17:47:27 +00:00
|
|
|
executeConfig
|
|
|
|
|
|
|
|
htlc *swap.Htlc
|
|
|
|
|
2020-06-23 09:49:43 +00:00
|
|
|
// htlcTxHash is the confirmed htlc tx id.
|
|
|
|
htlcTxHash *chainhash.Hash
|
|
|
|
|
2021-12-01 15:50:54 +00:00
|
|
|
swapInvoicePaymentAddr [32]byte
|
|
|
|
|
2021-05-24 06:40:13 +00:00
|
|
|
swapPaymentChan chan paymentResult
|
|
|
|
prePaymentChan chan paymentResult
|
2020-07-16 10:01:02 +00:00
|
|
|
|
|
|
|
wg sync.WaitGroup
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// executeConfig contains extra configuration to execute the swap.
|
|
|
|
type executeConfig struct {
|
2023-12-03 05:15:51 +00:00
|
|
|
sweeper *sweep.Sweeper
|
|
|
|
statusChan chan<- SwapInfo
|
|
|
|
blockEpochChan <-chan interface{}
|
|
|
|
timerFactory func(time.Duration) <-chan time.Time
|
|
|
|
loopOutMaxParts uint32
|
|
|
|
totalPaymentTimeout time.Duration
|
|
|
|
maxPaymentRetries int
|
|
|
|
cancelSwap func(context.Context, *outCancelDetails) error
|
|
|
|
verifySchnorrSig func(pubKey *btcec.PublicKey, hash, sig []byte) error
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2020-06-30 12:06:36 +00:00
|
|
|
// loopOutInitResult contains information about a just-initiated loop out swap.
|
|
|
|
type loopOutInitResult struct {
|
2020-06-30 12:10:33 +00:00
|
|
|
swap *loopOutSwap
|
|
|
|
serverMessage string
|
2020-06-30 12:06:36 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
// newLoopOutSwap initiates a new swap with the server and returns a
|
2019-03-06 20:13:50 +00:00
|
|
|
// corresponding swap object.
|
2019-03-07 04:32:24 +00:00
|
|
|
func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig,
|
2020-06-30 12:06:36 +00:00
|
|
|
currentHeight int32, request *OutRequest) (*loopOutInitResult, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
// Generate random preimage.
|
|
|
|
var swapPreimage [32]byte
|
|
|
|
if _, err := rand.Read(swapPreimage[:]); err != nil {
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Error("Cannot generate preimage")
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
swapHash := lntypes.Hash(sha256.Sum256(swapPreimage[:]))
|
|
|
|
|
|
|
|
// Derive a receiver key for this swap.
|
|
|
|
keyDesc, err := cfg.lnd.WalletKit.DeriveNextKey(
|
2019-03-07 04:32:24 +00:00
|
|
|
globalCtx, swap.KeyFamily,
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var receiverKey [33]byte
|
|
|
|
copy(receiverKey[:], keyDesc.PubKey.SerializeCompressed())
|
|
|
|
|
|
|
|
// Post the swap parameters to the swap server. The response contains
|
|
|
|
// the server revocation key and the swap and prepay invoices.
|
2020-07-14 12:54:06 +00:00
|
|
|
log.Infof("Initiating swap request at height %v: amt=%v, expiry=%v",
|
|
|
|
currentHeight, request.Amount, request.Expiry)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-11-14 09:35:32 +00:00
|
|
|
// The swap deadline will be given to the server for it to use as the
|
|
|
|
// latest swap publication time.
|
|
|
|
swapResp, err := cfg.server.NewLoopOutSwap(
|
2020-07-14 12:54:06 +00:00
|
|
|
globalCtx, swapHash, request.Amount, request.Expiry,
|
2020-11-06 09:43:03 +00:00
|
|
|
receiverKey, request.SwapPublicationDeadline, request.Initiator,
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2021-07-19 08:01:33 +00:00
|
|
|
return nil, wrapGrpcError("cannot initiate swap", err)
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 09:35:32 +00:00
|
|
|
err = validateLoopOutContract(
|
2022-12-15 15:43:38 +00:00
|
|
|
cfg.lnd, request, swapHash, swapResp,
|
2019-11-14 09:35:32 +00:00
|
|
|
)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-19 07:54:27 +00:00
|
|
|
// Check channel set for duplicates.
|
|
|
|
chanSet, err := loopdb.NewChannelSet(request.OutgoingChanSet)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-04 14:37:24 +00:00
|
|
|
// If a htlc confirmation target was not provided, we use the default
|
|
|
|
// number of confirmations. We overwrite this value rather than failing
|
|
|
|
// it because the field is a new addition to the rpc, and we don't want
|
|
|
|
// to break older clients that are not aware of this new field.
|
|
|
|
confs := uint32(request.HtlcConfirmations)
|
|
|
|
if confs == 0 {
|
|
|
|
confs = loopdb.DefaultLoopOutHtlcConfirmations
|
|
|
|
}
|
|
|
|
|
2019-11-14 09:35:32 +00:00
|
|
|
// Instantiate a struct that contains all required data to start the
|
|
|
|
// swap.
|
2019-03-06 20:13:50 +00:00
|
|
|
initiationTime := time.Now()
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
contract := loopdb.LoopOutContract{
|
2019-11-14 09:35:32 +00:00
|
|
|
SwapInvoice: swapResp.swapInvoice,
|
|
|
|
DestAddr: request.DestAddr,
|
|
|
|
MaxSwapRoutingFee: request.MaxSwapRoutingFee,
|
|
|
|
SweepConfTarget: request.SweepConfTarget,
|
2020-08-04 14:37:24 +00:00
|
|
|
HtlcConfirmations: confs,
|
2019-11-14 09:35:32 +00:00
|
|
|
PrepayInvoice: swapResp.prepayInvoice,
|
|
|
|
MaxPrepayRoutingFee: request.MaxPrepayRoutingFee,
|
|
|
|
SwapPublicationDeadline: request.SwapPublicationDeadline,
|
2019-03-07 04:32:24 +00:00
|
|
|
SwapContract: loopdb.SwapContract{
|
2019-03-12 15:09:57 +00:00
|
|
|
InitiationHeight: currentHeight,
|
|
|
|
InitiationTime: initiationTime,
|
2023-01-05 17:16:05 +00:00
|
|
|
HtlcKeys: loopdb.HtlcKeys{
|
|
|
|
SenderScriptKey: swapResp.senderKey,
|
|
|
|
SenderInternalPubKey: swapResp.senderKey,
|
|
|
|
ReceiverScriptKey: receiverKey,
|
|
|
|
ReceiverInternalPubKey: receiverKey,
|
|
|
|
ClientScriptKeyLocator: keyDesc.KeyLocator,
|
|
|
|
},
|
|
|
|
Preimage: swapPreimage,
|
|
|
|
AmountRequested: request.Amount,
|
|
|
|
CltvExpiry: request.Expiry,
|
|
|
|
MaxMinerFee: request.MaxMinerFee,
|
|
|
|
MaxSwapFee: request.MaxSwapFee,
|
|
|
|
Label: request.Label,
|
|
|
|
ProtocolVersion: loopdb.CurrentProtocolVersion(),
|
2019-03-06 20:13:50 +00:00
|
|
|
},
|
2020-05-19 07:54:27 +00:00
|
|
|
OutgoingChanSet: chanSet,
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 17:47:27 +00:00
|
|
|
swapKit := newSwapKit(
|
2022-04-24 20:59:59 +00:00
|
|
|
swapHash, swap.TypeOut, cfg, &contract.SwapContract,
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
2020-04-29 17:47:27 +00:00
|
|
|
|
|
|
|
swapKit.lastUpdateTime = initiationTime
|
|
|
|
|
|
|
|
// Create the htlc.
|
2022-11-16 18:01:28 +00:00
|
|
|
htlc, err := GetHtlc(
|
|
|
|
swapKit.hash, swapKit.contract, swapKit.lnd.ChainParams,
|
|
|
|
)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-29 17:47:27 +00:00
|
|
|
// Log htlc address for debugging.
|
2022-11-16 18:01:28 +00:00
|
|
|
swapKit.log.Infof("Htlc address (%s): %v", htlc.OutputType,
|
|
|
|
htlc.Address)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2021-12-01 15:50:54 +00:00
|
|
|
// Obtain the payment addr since we'll need it later for routing plugin
|
|
|
|
// recommendation and possibly for cancel.
|
|
|
|
paymentAddr, err := obtainSwapPaymentAddr(contract.SwapInvoice, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
swap := &loopOutSwap{
|
2021-12-01 15:50:54 +00:00
|
|
|
LoopOutContract: contract,
|
|
|
|
swapKit: *swapKit,
|
|
|
|
htlc: htlc,
|
|
|
|
swapInvoicePaymentAddr: *paymentAddr,
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
// Persist the data before exiting this function, so that the caller
|
|
|
|
// can trust that this swap will be resumed on restart.
|
2023-05-16 15:40:52 +00:00
|
|
|
err = cfg.store.CreateLoopOut(globalCtx, swapHash, &swap.LoopOutContract)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot store swap: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-06-30 10:55:50 +00:00
|
|
|
if swapResp.serverMessage != "" {
|
|
|
|
swap.log.Infof("Server message: %v", swapResp.serverMessage)
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:06:36 +00:00
|
|
|
return &loopOutInitResult{
|
2020-06-30 12:10:33 +00:00
|
|
|
swap: swap,
|
|
|
|
serverMessage: swapResp.serverMessage,
|
2020-06-30 12:06:36 +00:00
|
|
|
}, nil
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
// resumeLoopOutSwap returns a swap object representing a pending swap that has
|
2019-03-06 20:13:50 +00:00
|
|
|
// been restored from the database.
|
2022-12-15 15:43:38 +00:00
|
|
|
func resumeLoopOutSwap(cfg *swapConfig, pend *loopdb.LoopOut,
|
|
|
|
) (*loopOutSwap, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
hash := lntypes.Hash(sha256.Sum256(pend.Contract.Preimage[:]))
|
|
|
|
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Infof("Resuming loop out swap %v", hash)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2020-04-29 17:47:27 +00:00
|
|
|
swapKit := newSwapKit(
|
2022-04-24 20:59:59 +00:00
|
|
|
hash, swap.TypeOut, cfg, &pend.Contract.SwapContract,
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
2020-04-29 17:47:27 +00:00
|
|
|
|
|
|
|
// Create the htlc.
|
2022-11-16 18:01:28 +00:00
|
|
|
htlc, err := GetHtlc(
|
|
|
|
swapKit.hash, swapKit.contract, swapKit.lnd.ChainParams,
|
|
|
|
)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-29 17:47:27 +00:00
|
|
|
// Log htlc address for debugging.
|
|
|
|
swapKit.log.Infof("Htlc address: %v", htlc.Address)
|
|
|
|
|
2021-12-01 15:50:54 +00:00
|
|
|
// Obtain the payment addr since we'll need it later for routing plugin
|
|
|
|
// recommendation and possibly for cancel.
|
|
|
|
paymentAddr, err := obtainSwapPaymentAddr(
|
|
|
|
pend.Contract.SwapInvoice, cfg,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-29 17:47:27 +00:00
|
|
|
// Create the swap.
|
2019-03-07 04:32:24 +00:00
|
|
|
swap := &loopOutSwap{
|
2021-12-01 15:50:54 +00:00
|
|
|
LoopOutContract: *pend.Contract,
|
|
|
|
swapKit: *swapKit,
|
|
|
|
htlc: htlc,
|
|
|
|
swapInvoicePaymentAddr: *paymentAddr,
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lastUpdate := pend.LastUpdate()
|
|
|
|
if lastUpdate == nil {
|
|
|
|
swap.lastUpdateTime = pend.Contract.InitiationTime
|
|
|
|
} else {
|
|
|
|
swap.state = lastUpdate.State
|
|
|
|
swap.lastUpdateTime = lastUpdate.Time
|
2020-06-23 09:49:43 +00:00
|
|
|
swap.htlcTxHash = lastUpdate.HtlcTxHash
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return swap, nil
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:50:54 +00:00
|
|
|
// obtainSwapPaymentAddr will retrieve the payment addr from the passed invoice.
|
|
|
|
func obtainSwapPaymentAddr(swapInvoice string, cfg *swapConfig) (
|
|
|
|
*[32]byte, error) {
|
|
|
|
|
|
|
|
swapPayReq, err := zpay32.Decode(
|
|
|
|
swapInvoice, cfg.lnd.ChainParams,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if swapPayReq.PaymentAddr == nil {
|
|
|
|
return nil, fmt.Errorf("expected payment address for invoice")
|
|
|
|
}
|
|
|
|
|
|
|
|
return swapPayReq.PaymentAddr, nil
|
|
|
|
}
|
|
|
|
|
2020-04-29 17:47:27 +00:00
|
|
|
// sendUpdate reports an update to the swap state.
|
|
|
|
func (s *loopOutSwap) sendUpdate(ctx context.Context) error {
|
|
|
|
info := s.swapInfo()
|
|
|
|
s.log.Infof("Loop out swap state: %v", info.State)
|
|
|
|
|
2020-04-24 15:35:32 +00:00
|
|
|
info.HtlcAddressP2WSH = s.htlc.Address
|
2020-04-29 17:47:27 +00:00
|
|
|
|
2022-05-27 09:14:16 +00:00
|
|
|
// In order to avoid potentially dangerous ownership sharing
|
|
|
|
// we copy the outgoing channel set.
|
|
|
|
if s.OutgoingChanSet != nil {
|
|
|
|
outgoingChanSet := make(loopdb.ChannelSet, len(s.OutgoingChanSet))
|
|
|
|
copy(outgoingChanSet[:], s.OutgoingChanSet[:])
|
|
|
|
|
|
|
|
info.OutgoingChanSet = outgoingChanSet
|
|
|
|
}
|
|
|
|
|
2020-04-29 17:47:27 +00:00
|
|
|
select {
|
|
|
|
case s.statusChan <- *info:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
// execute starts/resumes the swap. It is a thin wrapper around
|
|
|
|
// executeAndFinalize to conveniently handle the error case.
|
2019-03-07 04:32:24 +00:00
|
|
|
func (s *loopOutSwap) execute(mainCtx context.Context,
|
2019-03-06 20:13:50 +00:00
|
|
|
cfg *executeConfig, height int32) error {
|
|
|
|
|
2020-07-16 10:01:02 +00:00
|
|
|
defer s.wg.Wait()
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
s.executeConfig = *cfg
|
|
|
|
s.height = height
|
|
|
|
|
2020-07-16 10:01:02 +00:00
|
|
|
// Create context for our state subscription which we will cancel once
|
|
|
|
// swap execution has completed, ensuring that we kill the subscribe
|
|
|
|
// goroutine.
|
|
|
|
subCtx, cancel := context.WithCancel(mainCtx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
s.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer s.wg.Done()
|
|
|
|
subscribeAndLogUpdates(
|
|
|
|
subCtx, s.hash, s.log, s.server.SubscribeLoopOutUpdates,
|
|
|
|
)
|
|
|
|
}()
|
|
|
|
|
2019-04-04 10:20:45 +00:00
|
|
|
// Execute swap.
|
2019-03-06 20:13:50 +00:00
|
|
|
err := s.executeAndFinalize(mainCtx)
|
|
|
|
|
|
|
|
// If an unexpected error happened, report a temporary failure.
|
2019-03-07 04:32:24 +00:00
|
|
|
// Otherwise for example a connection error could lead to abandoning
|
|
|
|
// the swap permanently and losing funds.
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
s.log.Errorf("Swap error: %v", err)
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
s.state = loopdb.StateFailTemporary
|
|
|
|
|
|
|
|
// If we cannot send out this update, there is nothing we can
|
|
|
|
// do.
|
2019-03-06 20:13:50 +00:00
|
|
|
_ = s.sendUpdate(mainCtx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// executeAndFinalize executes a swap and awaits the definitive outcome of the
|
|
|
|
// offchain payments. When this method returns, the swap outcome is final.
|
2019-03-07 04:32:24 +00:00
|
|
|
func (s *loopOutSwap) executeAndFinalize(globalCtx context.Context) error {
|
2019-03-06 20:13:50 +00:00
|
|
|
// Announce swap by sending out an initial update.
|
|
|
|
err := s.sendUpdate(globalCtx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute swap. When this call returns, the swap outcome is final, but
|
|
|
|
// it may be that there are still off-chain payments pending.
|
|
|
|
err = s.executeSwap(globalCtx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check.
|
2019-03-07 04:32:24 +00:00
|
|
|
if s.state.Type() == loopdb.StateTypePending {
|
2019-03-06 20:13:50 +00:00
|
|
|
return fmt.Errorf("swap in non-final state %v", s.state)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until all offchain payments have completed. If payments have
|
|
|
|
// already completed early, their channels have been set to nil.
|
|
|
|
s.log.Infof("Wait for server pulling off-chain payment(s)")
|
|
|
|
for s.swapPaymentChan != nil || s.prePaymentChan != nil {
|
|
|
|
select {
|
|
|
|
case result := <-s.swapPaymentChan:
|
|
|
|
s.swapPaymentChan = nil
|
2021-05-24 06:40:13 +00:00
|
|
|
|
|
|
|
err := s.handlePaymentResult(result)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.failure() != nil {
|
2019-03-06 20:13:50 +00:00
|
|
|
// Server didn't pull the swap payment.
|
|
|
|
s.log.Infof("Swap payment failed: %v",
|
2021-05-24 06:40:13 +00:00
|
|
|
result.failure())
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
case result := <-s.prePaymentChan:
|
|
|
|
s.prePaymentChan = nil
|
2021-05-24 06:40:13 +00:00
|
|
|
|
|
|
|
err := s.handlePaymentResult(result)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.failure() != nil {
|
2019-03-06 20:13:50 +00:00
|
|
|
// Server didn't pull the prepayment.
|
|
|
|
s.log.Infof("Prepayment failed: %v",
|
2021-05-24 06:40:13 +00:00
|
|
|
result.failure())
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-globalCtx.Done():
|
|
|
|
return globalCtx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark swap completed in store.
|
|
|
|
s.log.Infof("Swap completed: %v "+
|
2019-05-15 12:01:27 +00:00
|
|
|
"(final cost: server %v, onchain %v, offchain %v)",
|
2019-03-06 20:13:50 +00:00
|
|
|
s.state,
|
|
|
|
s.cost.Server,
|
|
|
|
s.cost.Onchain,
|
2019-05-15 12:01:27 +00:00
|
|
|
s.cost.Offchain,
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return s.persistState(globalCtx)
|
|
|
|
}
|
|
|
|
|
2021-05-24 06:40:13 +00:00
|
|
|
func (s *loopOutSwap) handlePaymentResult(result paymentResult) error {
|
|
|
|
switch {
|
|
|
|
// If our result has a non-nil error, our status will be nil. In this
|
|
|
|
// case the payment failed so we do not need to take any action.
|
|
|
|
case result.err != nil:
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case result.status.State == lnrpc.Payment_SUCCEEDED:
|
|
|
|
s.cost.Server += result.status.Value.ToSatoshis()
|
|
|
|
s.cost.Offchain += result.status.Fee.ToSatoshis()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case result.status.State == lnrpc.Payment_FAILED:
|
|
|
|
return nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unexpected state: %v", result.status.State)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
// executeSwap executes the swap, but returns as soon as the swap outcome is
|
|
|
|
// final. At that point, there may still be pending off-chain payment(s).
|
2019-03-07 04:32:24 +00:00
|
|
|
func (s *loopOutSwap) executeSwap(globalCtx context.Context) error {
|
2019-03-06 20:13:50 +00:00
|
|
|
// We always pay both invoices (again). This is currently the only way
|
|
|
|
// to sort of resume payments.
|
|
|
|
//
|
|
|
|
// TODO: We shouldn't pay the invoices if it is already too late to
|
|
|
|
// start the swap. But because we don't know if we already fired the
|
|
|
|
// payments in a previous run, we cannot just abandon here.
|
|
|
|
s.payInvoices(globalCtx)
|
|
|
|
|
|
|
|
// Wait for confirmation of the on-chain htlc by watching for a tx
|
|
|
|
// producing the swap script output.
|
|
|
|
txConf, err := s.waitForConfirmedHtlc(globalCtx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no error and no confirmation, the swap is aborted without an
|
|
|
|
// error. The swap state has been updated to a final state.
|
|
|
|
if txConf == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Off-chain payments can be canceled here. Most probably the HTLC
|
|
|
|
// is accepted by the server, but in case there are not for whatever
|
|
|
|
// reason, we don't need to have mission control start another payment
|
|
|
|
// attempt.
|
|
|
|
|
|
|
|
// Retrieve outpoint for sweep.
|
2019-03-07 04:32:24 +00:00
|
|
|
htlcOutpoint, htlcValue, err := swap.GetScriptOutput(
|
2019-04-04 10:20:45 +00:00
|
|
|
txConf.Tx, s.htlc.PkScript,
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.log.Infof("Htlc value: %v", htlcValue)
|
|
|
|
|
|
|
|
// Verify amount if preimage hasn't been revealed yet.
|
2019-03-07 04:32:24 +00:00
|
|
|
if s.state != loopdb.StatePreimageRevealed && htlcValue < s.AmountRequested {
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Warnf("Swap amount too low, expected %v but received %v",
|
2019-03-06 20:13:50 +00:00
|
|
|
s.AmountRequested, htlcValue)
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
s.state = loopdb.StateFailInsufficientValue
|
2019-03-06 20:13:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to spend htlc and continue (rbf) until a spend has confirmed.
|
2022-05-05 09:59:31 +00:00
|
|
|
spendDetails, err := s.waitForHtlcSpendConfirmed(
|
|
|
|
globalCtx, *htlcOutpoint, htlcValue,
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-12 12:09:28 +00:00
|
|
|
// If spend details are nil, we resolved the swap without waiting for
|
|
|
|
// its spend, so we can exit.
|
|
|
|
if spendDetails == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:22:46 +00:00
|
|
|
// Inspect witness stack to see if it is a success transaction. We
|
|
|
|
// don't just try to match with the hash of our sweep tx, because it
|
|
|
|
// may be swept by a different (fee) sweep tx from a previous run.
|
2019-03-07 04:32:24 +00:00
|
|
|
htlcInput, err := swap.GetTxInputByOutpoint(
|
2019-03-06 20:13:50 +00:00
|
|
|
spendDetails.SpendingTx, htlcOutpoint,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sweepSuccessful := s.htlc.IsSuccessWitness(htlcInput.Witness)
|
|
|
|
if sweepSuccessful {
|
|
|
|
s.cost.Server -= htlcValue
|
|
|
|
|
|
|
|
s.cost.Onchain = htlcValue -
|
|
|
|
btcutil.Amount(spendDetails.SpendingTx.TxOut[0].Value)
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
s.state = loopdb.StateSuccess
|
2019-03-06 20:13:50 +00:00
|
|
|
} else {
|
2019-03-07 04:32:24 +00:00
|
|
|
s.state = loopdb.StateFailSweepTimeout
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// persistState updates the swap state and sends out an update notification.
|
2019-03-07 04:32:24 +00:00
|
|
|
func (s *loopOutSwap) persistState(ctx context.Context) error {
|
2019-03-06 20:13:50 +00:00
|
|
|
updateTime := time.Now()
|
|
|
|
|
|
|
|
s.lastUpdateTime = updateTime
|
|
|
|
|
|
|
|
// Update state in store.
|
2019-05-15 11:55:41 +00:00
|
|
|
err := s.store.UpdateLoopOut(
|
2023-05-16 15:40:52 +00:00
|
|
|
ctx, s.hash, updateTime,
|
2019-05-15 11:55:41 +00:00
|
|
|
loopdb.SwapStateData{
|
2020-06-23 09:49:43 +00:00
|
|
|
State: s.state,
|
|
|
|
Cost: s.cost,
|
|
|
|
HtlcTxHash: s.htlcTxHash,
|
2019-05-15 11:55:41 +00:00
|
|
|
},
|
|
|
|
)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send out swap update
|
|
|
|
return s.sendUpdate(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// payInvoices pays both swap invoices.
|
2019-03-07 04:32:24 +00:00
|
|
|
func (s *loopOutSwap) payInvoices(ctx context.Context) {
|
2019-03-06 20:13:50 +00:00
|
|
|
// Pay the swap invoice.
|
|
|
|
s.log.Infof("Sending swap payment %v", s.SwapInvoice)
|
2020-05-19 06:52:38 +00:00
|
|
|
|
2021-12-01 15:50:54 +00:00
|
|
|
// Ask the server if it recommends using a routing plugin.
|
|
|
|
pluginType, err := s.swapKit.server.RecommendRoutingPlugin(
|
|
|
|
ctx, s.swapInfo().SwapHash, s.swapInvoicePaymentAddr,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Warnf("Server couldn't recommend routing plugin: %v", err)
|
|
|
|
pluginType = RoutingPluginNone
|
|
|
|
} else {
|
|
|
|
s.log.Infof("Server recommended routing plugin: %v", pluginType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the recommended routing plugin.
|
2020-04-14 09:07:06 +00:00
|
|
|
s.swapPaymentChan = s.payInvoice(
|
2020-05-19 07:54:27 +00:00
|
|
|
ctx, s.SwapInvoice, s.MaxSwapRoutingFee,
|
2022-02-17 19:36:28 +00:00
|
|
|
s.LoopOutContract.OutgoingChanSet, pluginType, true,
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
2021-12-01 16:04:40 +00:00
|
|
|
// Pay the prepay invoice. Won't use the routing plugin here as the
|
|
|
|
// prepay is trivially small and shouldn't normally need any help.
|
2019-03-06 20:13:50 +00:00
|
|
|
s.log.Infof("Sending prepayment %v", s.PrepayInvoice)
|
2020-04-14 09:07:06 +00:00
|
|
|
s.prePaymentChan = s.payInvoice(
|
2019-03-06 20:13:50 +00:00
|
|
|
ctx, s.PrepayInvoice, s.MaxPrepayRoutingFee,
|
2022-02-17 19:36:28 +00:00
|
|
|
nil, RoutingPluginNone, false,
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-05-24 06:40:13 +00:00
|
|
|
// paymentResult contains the response for a failed or settled payment, and
|
|
|
|
// any errors that occurred if the payment unexpectedly failed.
|
|
|
|
type paymentResult struct {
|
|
|
|
status lndclient.PaymentStatus
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// failure returns the error we encountered trying to dispatch a payment result,
|
|
|
|
// if any.
|
|
|
|
func (p paymentResult) failure() error {
|
|
|
|
if p.err != nil {
|
|
|
|
return p.err
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.status.State == lnrpc.Payment_SUCCEEDED {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("payment failed: %v", p.status.FailureReason)
|
|
|
|
}
|
|
|
|
|
2020-04-14 09:07:06 +00:00
|
|
|
// payInvoice pays a single invoice.
|
|
|
|
func (s *loopOutSwap) payInvoice(ctx context.Context, invoice string,
|
2021-12-01 15:50:54 +00:00
|
|
|
maxFee btcutil.Amount, outgoingChanIds loopdb.ChannelSet,
|
2022-02-17 19:36:28 +00:00
|
|
|
pluginType RoutingPluginType,
|
|
|
|
reportPluginResult bool) chan paymentResult {
|
2020-04-14 09:07:06 +00:00
|
|
|
|
2021-05-24 06:40:13 +00:00
|
|
|
resultChan := make(chan paymentResult)
|
|
|
|
sendResult := func(result paymentResult) {
|
|
|
|
select {
|
|
|
|
case resultChan <- result:
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
}
|
2020-04-14 09:07:06 +00:00
|
|
|
|
|
|
|
go func() {
|
2021-05-24 06:40:13 +00:00
|
|
|
var result paymentResult
|
2020-04-14 09:07:06 +00:00
|
|
|
|
|
|
|
status, err := s.payInvoiceAsync(
|
2021-12-01 15:50:54 +00:00
|
|
|
ctx, invoice, maxFee, outgoingChanIds, pluginType,
|
2022-02-17 19:36:28 +00:00
|
|
|
reportPluginResult,
|
2020-04-14 09:07:06 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2021-05-24 06:40:13 +00:00
|
|
|
result.err = err
|
|
|
|
sendResult(result)
|
|
|
|
return
|
2020-04-14 09:07:06 +00:00
|
|
|
}
|
|
|
|
|
2021-05-24 06:40:13 +00:00
|
|
|
// If our payment failed or succeeded, our status should be
|
|
|
|
// non-nil.
|
|
|
|
switch status.State {
|
|
|
|
case lnrpc.Payment_FAILED, lnrpc.Payment_SUCCEEDED:
|
|
|
|
result.status = *status
|
|
|
|
|
|
|
|
default:
|
|
|
|
result.err = fmt.Errorf("unexpected payment state: %v",
|
|
|
|
status.State)
|
2020-04-14 09:07:06 +00:00
|
|
|
}
|
2021-05-24 06:40:13 +00:00
|
|
|
|
|
|
|
sendResult(result)
|
2020-04-14 09:07:06 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return resultChan
|
|
|
|
}
|
|
|
|
|
|
|
|
// payInvoiceAsync is the asynchronously executed part of paying an invoice.
|
|
|
|
func (s *loopOutSwap) payInvoiceAsync(ctx context.Context,
|
2020-05-19 07:54:27 +00:00
|
|
|
invoice string, maxFee btcutil.Amount,
|
2022-02-17 19:36:28 +00:00
|
|
|
outgoingChanIds loopdb.ChannelSet, pluginType RoutingPluginType,
|
|
|
|
reportPluginResult bool) (*lndclient.PaymentStatus, error) {
|
2020-04-14 09:07:06 +00:00
|
|
|
|
|
|
|
// Extract hash from payment request. Unfortunately the request
|
|
|
|
// components aren't available directly.
|
|
|
|
chainParams := s.lnd.ChainParams
|
2021-12-01 16:04:40 +00:00
|
|
|
target, routeHints, hash, amt, err := swap.DecodeInvoice(
|
|
|
|
chainParams, invoice,
|
|
|
|
)
|
2021-12-01 15:50:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
maxRetries := 1
|
2023-12-03 05:15:51 +00:00
|
|
|
paymentTimeout := s.executeConfig.totalPaymentTimeout
|
2021-12-01 14:18:10 +00:00
|
|
|
|
2021-12-01 15:50:54 +00:00
|
|
|
// Attempt to acquire and initialize the routing plugin.
|
|
|
|
routingPlugin, err := AcquireRoutingPlugin(
|
2021-12-01 16:04:40 +00:00
|
|
|
ctx, pluginType, *s.lnd, target, routeHints, amt,
|
2021-12-01 15:50:54 +00:00
|
|
|
)
|
2020-04-14 09:07:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:50:54 +00:00
|
|
|
if routingPlugin != nil {
|
|
|
|
s.log.Infof("Acquired routing plugin %v for payment %v",
|
|
|
|
pluginType, hash.String())
|
|
|
|
|
2021-12-01 14:18:10 +00:00
|
|
|
maxRetries = s.executeConfig.maxPaymentRetries
|
|
|
|
paymentTimeout /= time.Duration(maxRetries)
|
2021-12-01 15:50:54 +00:00
|
|
|
defer ReleaseRoutingPlugin(ctx)
|
|
|
|
}
|
|
|
|
|
2020-04-14 09:07:06 +00:00
|
|
|
req := lndclient.SendPaymentRequest{
|
|
|
|
MaxFee: maxFee,
|
|
|
|
Invoice: invoice,
|
2020-05-19 06:52:38 +00:00
|
|
|
OutgoingChanIds: outgoingChanIds,
|
2020-04-14 09:07:06 +00:00
|
|
|
Timeout: paymentTimeout,
|
2020-04-27 12:06:08 +00:00
|
|
|
MaxParts: s.executeConfig.loopOutMaxParts,
|
2020-04-14 09:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup state of the swap payment.
|
2021-12-01 15:50:54 +00:00
|
|
|
payCtx, cancel := context.WithCancel(ctx)
|
2020-04-14 09:07:06 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2021-12-01 15:50:54 +00:00
|
|
|
start := time.Now()
|
|
|
|
paymentStatus, attempts, err := s.sendPaymentWithRetry(
|
|
|
|
payCtx, hash, &req, maxRetries, routingPlugin, pluginType,
|
2020-04-14 09:07:06 +00:00
|
|
|
)
|
2021-12-01 15:50:54 +00:00
|
|
|
|
|
|
|
dt := time.Since(start)
|
|
|
|
paymentSuccess := err == nil &&
|
|
|
|
paymentStatus.State == lnrpc.Payment_SUCCEEDED
|
|
|
|
|
2022-02-17 19:36:28 +00:00
|
|
|
if reportPluginResult {
|
|
|
|
// If the plugin couldn't be acquired then override the reported
|
|
|
|
// plugin type to RoutingPluginNone.
|
|
|
|
reportType := pluginType
|
|
|
|
if routingPlugin == nil {
|
|
|
|
reportType = RoutingPluginNone
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.swapKit.server.ReportRoutingResult(
|
|
|
|
ctx, s.swapInfo().SwapHash, s.swapInvoicePaymentAddr,
|
|
|
|
reportType, paymentSuccess, int32(attempts),
|
|
|
|
dt.Milliseconds(),
|
|
|
|
); err != nil {
|
|
|
|
s.log.Warnf("Failed to report routing result: %v", err)
|
|
|
|
}
|
2021-12-01 15:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return paymentStatus, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendPaymentWithRetry will send the payment, optionally with the passed
|
|
|
|
// routing plugin retrying at most maxRetries times.
|
|
|
|
func (s *loopOutSwap) sendPaymentWithRetry(ctx context.Context,
|
|
|
|
hash lntypes.Hash, req *lndclient.SendPaymentRequest, maxRetries int,
|
|
|
|
routingPlugin RoutingPlugin, pluginType RoutingPluginType) (
|
|
|
|
*lndclient.PaymentStatus, int, error) {
|
|
|
|
|
|
|
|
tryCount := 1
|
|
|
|
for {
|
|
|
|
s.log.Infof("Payment (%v) try count %v/%v (plugin=%v)",
|
|
|
|
hash.String(), tryCount, maxRetries,
|
|
|
|
pluginType.String())
|
|
|
|
|
|
|
|
if routingPlugin != nil {
|
|
|
|
if err := routingPlugin.BeforePayment(
|
|
|
|
ctx, tryCount, maxRetries,
|
|
|
|
); err != nil {
|
|
|
|
return nil, tryCount, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
paymentStatus, err := s.awaitSendPayment(ctx, hash, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, tryCount, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Payment has succeeded, we can return here.
|
|
|
|
if paymentStatus.State == lnrpc.Payment_SUCCEEDED {
|
|
|
|
return paymentStatus, tryCount, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retry if the payment has timed out, or return here.
|
|
|
|
if tryCount > maxRetries || paymentStatus.FailureReason !=
|
|
|
|
lnrpc.PaymentFailureReason_FAILURE_REASON_TIMEOUT {
|
|
|
|
|
|
|
|
return paymentStatus, tryCount, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tryCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *loopOutSwap) awaitSendPayment(ctx context.Context, hash lntypes.Hash,
|
|
|
|
req *lndclient.SendPaymentRequest) (*lndclient.PaymentStatus, error) {
|
|
|
|
|
|
|
|
payStatusChan, payErrChan, err := s.lnd.Router.SendPayment(ctx, *req)
|
2020-04-14 09:07:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// Payment advanced to the next state.
|
|
|
|
case payState := <-payStatusChan:
|
2020-04-14 12:35:56 +00:00
|
|
|
s.log.Infof("Payment %v: %v", hash, payState)
|
2020-04-14 09:07:06 +00:00
|
|
|
|
|
|
|
switch payState.State {
|
|
|
|
case lnrpc.Payment_SUCCEEDED:
|
|
|
|
return &payState, nil
|
|
|
|
|
|
|
|
case lnrpc.Payment_FAILED:
|
2021-05-24 06:40:13 +00:00
|
|
|
return &payState, nil
|
2020-04-14 09:07:06 +00:00
|
|
|
|
|
|
|
case lnrpc.Payment_IN_FLIGHT:
|
|
|
|
// Continue waiting for final state.
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errors.New("unknown payment state")
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:50:54 +00:00
|
|
|
// Abort the swap in case of an error. An unknown
|
|
|
|
// payment error from TrackPayment is no longer expected
|
|
|
|
// here.
|
2020-04-14 09:07:06 +00:00
|
|
|
case err := <-payErrChan:
|
|
|
|
if err != channeldb.ErrAlreadyPaid {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
payStatusChan, payErrChan, err =
|
2021-12-01 15:50:54 +00:00
|
|
|
s.lnd.Router.TrackPayment(ctx, hash)
|
2020-04-14 09:07:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
// waitForConfirmedHtlc waits for a confirmed htlc to appear on the chain. In
|
|
|
|
// case we haven't revealed the preimage yet, it also monitors block height and
|
|
|
|
// off-chain payment failure.
|
2019-03-07 04:32:24 +00:00
|
|
|
func (s *loopOutSwap) waitForConfirmedHtlc(globalCtx context.Context) (
|
2019-03-06 20:13:50 +00:00
|
|
|
*chainntnfs.TxConfirmation, error) {
|
|
|
|
|
|
|
|
// Wait for confirmation of the on-chain htlc by watching for a tx
|
|
|
|
// producing the swap script output.
|
|
|
|
s.log.Infof(
|
2020-08-04 18:28:06 +00:00
|
|
|
"Register %v conf ntfn for swap script on chain (hh=%v)",
|
|
|
|
s.HtlcConfirmations, s.InitiationHeight,
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
2020-06-23 09:49:43 +00:00
|
|
|
// If we've revealed the preimage in a previous run, we expect to have
|
|
|
|
// recorded the htlc tx hash. We use this to re-register for
|
|
|
|
// confirmation, to be sure that we'll keep tracking the same htlc. For
|
|
|
|
// older swaps, this field may not be populated even though the preimage
|
|
|
|
// has already been revealed.
|
|
|
|
if s.state == loopdb.StatePreimageRevealed && s.htlcTxHash == nil {
|
|
|
|
s.log.Warnf("No htlc tx hash available, registering with " +
|
|
|
|
"just the pkscript")
|
|
|
|
}
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
ctx, cancel := context.WithCancel(globalCtx)
|
|
|
|
defer cancel()
|
|
|
|
htlcConfChan, htlcErrChan, err :=
|
|
|
|
s.lnd.ChainNotifier.RegisterConfirmationsNtfn(
|
2020-08-04 18:28:06 +00:00
|
|
|
ctx, s.htlcTxHash, s.htlc.PkScript,
|
|
|
|
int32(s.HtlcConfirmations), s.InitiationHeight,
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var txConf *chainntnfs.TxConfirmation
|
2019-03-07 04:32:24 +00:00
|
|
|
if s.state == loopdb.StateInitiated {
|
2019-03-06 20:13:50 +00:00
|
|
|
// Check if it is already too late to start this swap. If we
|
|
|
|
// already revealed the preimage, this check is irrelevant and
|
|
|
|
// we need to sweep in any case.
|
|
|
|
maxPreimageRevealHeight := s.CltvExpiry -
|
2019-03-07 04:32:24 +00:00
|
|
|
MinLoopOutPreimageRevealDelta
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
checkMaxRevealHeightExceeded := func() bool {
|
|
|
|
s.log.Infof("Checking preimage reveal height %v "+
|
|
|
|
"exceeded (height %v)",
|
|
|
|
maxPreimageRevealHeight, s.height)
|
|
|
|
|
|
|
|
if s.height <= maxPreimageRevealHeight {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
s.log.Infof("Max preimage reveal height %v "+
|
|
|
|
"exceeded (height %v)",
|
|
|
|
maxPreimageRevealHeight, s.height)
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
s.state = loopdb.StateFailTimeout
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// First check, because after resume we may otherwise reveal the
|
|
|
|
// preimage after the max height (depending on order in which
|
|
|
|
// events are received in the select loop below).
|
|
|
|
if checkMaxRevealHeightExceeded() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
s.log.Infof("Waiting for either htlc on-chain confirmation or " +
|
2022-05-06 15:33:19 +00:00
|
|
|
"off-chain payment failure")
|
2019-03-06 20:13:50 +00:00
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// If the swap payment fails, abandon the swap. We may
|
|
|
|
// have lost the prepayment.
|
|
|
|
case result := <-s.swapPaymentChan:
|
|
|
|
s.swapPaymentChan = nil
|
2021-05-24 06:40:13 +00:00
|
|
|
|
|
|
|
err := s.handlePaymentResult(result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.failure() != nil {
|
2019-03-06 20:13:50 +00:00
|
|
|
s.log.Infof("Failed swap payment: %v",
|
2021-05-24 06:40:13 +00:00
|
|
|
result.failure())
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2021-05-24 06:46:03 +00:00
|
|
|
s.failOffChain(
|
|
|
|
ctx, paymentTypeInvoice,
|
|
|
|
result.status,
|
|
|
|
)
|
2019-03-06 20:13:50 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the prepay fails, abandon the swap. Because we
|
|
|
|
// didn't reveal the preimage, the swap payment will be
|
|
|
|
// canceled or time out.
|
|
|
|
case result := <-s.prePaymentChan:
|
|
|
|
s.prePaymentChan = nil
|
2021-05-24 06:40:13 +00:00
|
|
|
|
|
|
|
err := s.handlePaymentResult(result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.failure() != nil {
|
2019-03-06 20:13:50 +00:00
|
|
|
s.log.Infof("Failed prepayment: %v",
|
2021-05-24 06:40:13 +00:00
|
|
|
result.failure())
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2021-05-24 06:46:03 +00:00
|
|
|
s.failOffChain(
|
|
|
|
ctx, paymentTypeInvoice,
|
|
|
|
result.status,
|
|
|
|
)
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unexpected error on the confirm channel happened,
|
|
|
|
// abandon the swap.
|
|
|
|
case err := <-htlcErrChan:
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
// Htlc got confirmed, continue to sweeping.
|
|
|
|
case htlcConfNtfn := <-htlcConfChan:
|
|
|
|
txConf = htlcConfNtfn
|
|
|
|
break loop
|
|
|
|
|
|
|
|
// New block is received. Recheck max reveal height.
|
|
|
|
case notification := <-s.blockEpochChan:
|
|
|
|
s.height = notification.(int32)
|
|
|
|
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Infof("Received block %v", s.height)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
if checkMaxRevealHeightExceeded() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client quit.
|
|
|
|
case <-globalCtx.Done():
|
|
|
|
return nil, globalCtx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.log.Infof("Swap script confirmed on chain")
|
|
|
|
} else {
|
|
|
|
s.log.Infof("Retrieving htlc onchain")
|
|
|
|
select {
|
|
|
|
case err := <-htlcErrChan:
|
|
|
|
return nil, err
|
|
|
|
case htlcConfNtfn := <-htlcConfChan:
|
|
|
|
txConf = htlcConfNtfn
|
|
|
|
case <-globalCtx.Done():
|
|
|
|
return nil, globalCtx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 09:49:43 +00:00
|
|
|
htlcTxHash := txConf.Tx.TxHash()
|
|
|
|
s.log.Infof("Htlc tx %v at height %v", htlcTxHash, txConf.BlockHeight)
|
|
|
|
|
|
|
|
s.htlcTxHash = &htlcTxHash
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
return txConf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// waitForHtlcSpendConfirmed waits for the htlc to be spent either by our own
|
|
|
|
// sweep or a server revocation tx. During this process, this function will try
|
|
|
|
// to spend the htlc every block by calling spendFunc.
|
|
|
|
//
|
|
|
|
// TODO: Improve retry/fee increase mechanism. Once in the mempool, server can
|
|
|
|
// sweep offchain. So we must make sure we sweep successfully before on-chain
|
|
|
|
// timeout.
|
2019-03-07 04:32:24 +00:00
|
|
|
func (s *loopOutSwap) waitForHtlcSpendConfirmed(globalCtx context.Context,
|
2022-05-05 09:59:31 +00:00
|
|
|
htlcOutpoint wire.OutPoint, htlcValue btcutil.Amount) (
|
|
|
|
*chainntnfs.SpendDetail, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
// Register the htlc spend notification.
|
|
|
|
ctx, cancel := context.WithCancel(globalCtx)
|
|
|
|
defer cancel()
|
|
|
|
spendChan, spendErr, err := s.lnd.ChainNotifier.RegisterSpendNtfn(
|
2022-05-05 09:59:31 +00:00
|
|
|
ctx, &htlcOutpoint, s.htlc.PkScript, s.InitiationHeight,
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("register spend ntfn: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-06-02 07:31:39 +00:00
|
|
|
// Track our payment status so that we can detect whether our off chain
|
|
|
|
// htlc is settled. We track this information to determine whether it is
|
|
|
|
// necessary to continue trying to push our preimage to the server.
|
|
|
|
trackChan, trackErrChan, err := s.lnd.Router.TrackPayment(
|
|
|
|
ctx, s.hash,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("track payment: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-05-05 09:59:31 +00:00
|
|
|
var (
|
|
|
|
// paymentComplete tracks whether our payment is complete, and
|
|
|
|
// is used to decide whether we need to push our preimage to
|
|
|
|
// the server.
|
|
|
|
paymentComplete bool
|
|
|
|
// musigSweepTryCount tracts the number of cooperative, MuSig2
|
|
|
|
// sweep attempts.
|
|
|
|
musigSweepTryCount int
|
|
|
|
// musigSweepSuccess tracks whether at least one MuSig2 sweep
|
|
|
|
// txn was successfully published to the mempool.
|
|
|
|
musigSweepSuccess bool
|
|
|
|
)
|
2020-06-02 07:31:39 +00:00
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
timerChan := s.timerFactory(republishDelay)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// Htlc spend, break loop.
|
|
|
|
case spendDetails := <-spendChan:
|
2022-05-05 09:59:31 +00:00
|
|
|
s.log.Infof("Htlc spend by tx: %v",
|
|
|
|
spendDetails.SpenderTxHash)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
return spendDetails, nil
|
|
|
|
|
|
|
|
// Spend notification error.
|
|
|
|
case err := <-spendErr:
|
|
|
|
return nil, err
|
|
|
|
|
2020-06-02 07:31:39 +00:00
|
|
|
// Receive status updates for our payment so that we can detect
|
|
|
|
// whether we've successfully pushed our preimage.
|
|
|
|
case status, ok := <-trackChan:
|
|
|
|
// If our channel has been closed, indicating that the
|
|
|
|
// server is finished providing updates because the
|
|
|
|
// payment has reached a terminal state, we replace
|
|
|
|
// the closed channel with nil so that we will no longer
|
|
|
|
// listen on it.
|
|
|
|
if !ok {
|
|
|
|
trackChan = nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if status.State == lnrpc.Payment_SUCCEEDED {
|
|
|
|
s.log.Infof("Off chain payment succeeded")
|
|
|
|
|
|
|
|
paymentComplete = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we receive a track payment error that indicates that the
|
|
|
|
// server stream is complete, we ignore it because we want to
|
|
|
|
// continue this loop beyond the completion of the payment.
|
|
|
|
case err, ok := <-trackErrChan:
|
|
|
|
// If our channel has been closed, indicating that the
|
|
|
|
// server is finished providing updates because the
|
|
|
|
// payment has reached a terminal state, we replace
|
|
|
|
// the closed channel with nil so that we will no longer
|
|
|
|
// listen on it.
|
|
|
|
if !ok {
|
|
|
|
trackErrChan = nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, if we receive a non-nil error, we return
|
|
|
|
// it.
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
// New block arrived, update height and restart the republish
|
|
|
|
// timer.
|
|
|
|
case notification := <-s.blockEpochChan:
|
|
|
|
s.height = notification.(int32)
|
|
|
|
timerChan = s.timerFactory(republishDelay)
|
|
|
|
|
|
|
|
// Some time after start or after arrival of a new block, try
|
|
|
|
// to spend again.
|
|
|
|
case <-timerChan:
|
2022-05-05 09:59:31 +00:00
|
|
|
if IsTaprootSwap(&s.SwapContract) {
|
|
|
|
// sweepConfTarget will return false if the
|
|
|
|
// preimage is not revealed yet but the conf
|
|
|
|
// target is closer than 20 blocks. In this case
|
|
|
|
// to be sure we won't attempt to sweep at all
|
|
|
|
// and we won't reveal the preimage either.
|
|
|
|
_, canSweep := s.sweepConfTarget()
|
|
|
|
if !canSweep {
|
|
|
|
s.log.Infof("Aborting swap, timed " +
|
|
|
|
"out on-chain")
|
|
|
|
|
|
|
|
s.state = loopdb.StateFailTimeout
|
|
|
|
err := s.persistState(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("unable to persist " +
|
|
|
|
"state")
|
|
|
|
}
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2022-05-05 09:59:31 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// When using taproot HTLCs we're pushing the
|
|
|
|
// preimage before attempting to sweep. This
|
|
|
|
// way the server will know that the swap will
|
|
|
|
// go through and we'll be able to MuSig2
|
|
|
|
// cosign our sweep transaction. In the worst
|
|
|
|
// case if the server is uncooperative for any
|
|
|
|
// reason we can still sweep using scriptpath
|
|
|
|
// spend.
|
|
|
|
err = s.setStatePreimageRevealed(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !paymentComplete {
|
|
|
|
// Push the preimage for as long as the
|
|
|
|
// server is able to settle the swap
|
|
|
|
// invoice. So that we can continue
|
|
|
|
// with the MuSig2 sweep afterwards.
|
|
|
|
s.pushPreimage(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now attempt to publish a MuSig2 sweep txn.
|
|
|
|
// Only attempt at most maxMusigSweepRetires
|
|
|
|
// times to still leave time for an emergency
|
|
|
|
// script path sweep.
|
|
|
|
if musigSweepTryCount < maxMusigSweepRetries {
|
|
|
|
success := s.sweepMuSig2(
|
|
|
|
ctx, htlcOutpoint, htlcValue,
|
|
|
|
)
|
|
|
|
if !success {
|
|
|
|
musigSweepTryCount++
|
|
|
|
} else {
|
|
|
|
// Mark that we had a sweep
|
|
|
|
// that was successful. There's
|
|
|
|
// no need for the script spend
|
|
|
|
// now we can just keep pushing
|
|
|
|
// new sweeps to bump the fee.
|
|
|
|
musigSweepSuccess = true
|
|
|
|
}
|
|
|
|
} else if !musigSweepSuccess {
|
|
|
|
// Attempt to script path sweep. If the
|
|
|
|
// sweep fails, we can't do any better
|
|
|
|
// than go on and try again later as
|
2023-12-03 05:15:51 +00:00
|
|
|
// the preimage is already revealed and
|
2022-05-05 09:59:31 +00:00
|
|
|
// the server settled the swap payment.
|
|
|
|
// From the server's point of view the
|
|
|
|
// swap is succeeded at this point so
|
|
|
|
// we are free to retry as long as we
|
|
|
|
// want.
|
|
|
|
err := s.sweep(
|
|
|
|
ctx, htlcOutpoint, htlcValue,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Failed to publish "+
|
|
|
|
"non-cooperative "+
|
|
|
|
"sweep: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2021-05-12 12:09:28 +00:00
|
|
|
|
2022-05-05 09:59:31 +00:00
|
|
|
// If the result of our spend func was that the
|
|
|
|
// swap has reached a final state, then we
|
|
|
|
// return nil spend details, because there is
|
|
|
|
// no further action required for this swap.
|
|
|
|
if s.state.Type() != loopdb.StateTypePending {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err := s.sweep(ctx, htlcOutpoint, htlcValue)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the result of our spend func was that the
|
|
|
|
// swap has reached a final state, then we
|
|
|
|
// return nil spend details, because there is no
|
|
|
|
// further action required for this swap.
|
|
|
|
if s.state.Type() != loopdb.StateTypePending {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If our off chain payment is not yet complete,
|
|
|
|
// we try to push our preimage to the server.
|
|
|
|
if !paymentComplete {
|
|
|
|
s.pushPreimage(ctx)
|
|
|
|
}
|
2020-06-02 07:31:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
// Context canceled.
|
|
|
|
case <-globalCtx.Done():
|
|
|
|
return nil, globalCtx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 07:31:39 +00:00
|
|
|
// pushPreimage pushes our preimage to the server if we have already revealed
|
|
|
|
// our preimage on chain with a sweep attempt.
|
|
|
|
func (s *loopOutSwap) pushPreimage(ctx context.Context) {
|
|
|
|
// If we have not yet revealed our preimage through a sweep, we do not
|
|
|
|
// push the preimage because we may choose to never sweep if fees are
|
|
|
|
// too high.
|
|
|
|
if s.state != loopdb.StatePreimageRevealed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.log.Infof("Pushing preimage to server")
|
|
|
|
|
|
|
|
// Push the preimage to the server, just log server errors since we rely
|
|
|
|
// on our payment state rather than the server response to judge the
|
|
|
|
// outcome of our preimage push.
|
|
|
|
if err := s.server.PushLoopOutPreimage(ctx, s.Preimage); err != nil {
|
|
|
|
s.log.Warnf("Could not push preimage: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 06:46:03 +00:00
|
|
|
// failOffChain updates a swap's state when it has failed due to a routing
|
|
|
|
// failure and notifies the server of the failure.
|
|
|
|
func (s *loopOutSwap) failOffChain(ctx context.Context, paymentType paymentType,
|
|
|
|
status lndclient.PaymentStatus) {
|
|
|
|
|
|
|
|
// Set our state to failed off chain timeout.
|
|
|
|
s.state = loopdb.StateFailOffchainPayments
|
|
|
|
|
|
|
|
details := &outCancelDetails{
|
|
|
|
hash: s.hash,
|
2021-12-01 15:50:54 +00:00
|
|
|
paymentAddr: s.swapInvoicePaymentAddr,
|
2021-05-24 06:46:03 +00:00
|
|
|
metadata: routeCancelMetadata{
|
|
|
|
paymentType: paymentType,
|
|
|
|
failureReason: status.FailureReason,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, htlc := range status.Htlcs {
|
|
|
|
if htlc.Status != lnrpc.HTLCAttempt_FAILED {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if htlc.Route == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(htlc.Route.Hops) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if htlc.Failure == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
failureIdx := htlc.Failure.FailureSourceIndex
|
|
|
|
hops := uint32(len(htlc.Route.Hops))
|
|
|
|
|
|
|
|
// We really don't expect a failure index that is greater than
|
|
|
|
// our number of hops. This is because failure index is zero
|
|
|
|
// based, where a value of zero means that the payment failed
|
|
|
|
// at the client's node, and a value = len(hops) means that it
|
|
|
|
// failed at the last node in the route. We don't want to
|
|
|
|
// underflow so we check and log a warning if this happens.
|
|
|
|
if failureIdx > hops {
|
|
|
|
s.log.Warnf("Htlc attempt failure index > hops",
|
|
|
|
failureIdx, hops)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the number of hops from the server that we failed at
|
|
|
|
// to the set of attempts that we will report to the server.
|
|
|
|
distance := hops - failureIdx
|
|
|
|
|
|
|
|
// In the case that our swap failed in the network at large,
|
|
|
|
// rather than the loop server's internal infrastructure, we
|
|
|
|
// don't want to disclose and information about distance from
|
|
|
|
// the server, so we set maxUint32 to represent failure in
|
|
|
|
// "the network at large" rather than due to the server's
|
|
|
|
// liquidity.
|
|
|
|
if distance > loopInternalHops {
|
|
|
|
distance = math.MaxUint32
|
|
|
|
}
|
|
|
|
|
|
|
|
details.metadata.attempts = append(
|
|
|
|
details.metadata.attempts, distance,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.log.Infof("Canceling swap: %v payment failed: %v, %v attempts",
|
|
|
|
paymentType, details.metadata.failureReason,
|
|
|
|
len(details.metadata.attempts))
|
|
|
|
|
|
|
|
// Report to server, it's not critical if this doesn't go through.
|
|
|
|
if err := s.cancelSwap(ctx, details); err != nil {
|
|
|
|
s.log.Warnf("Could not report failure: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 09:59:31 +00:00
|
|
|
// createMuSig2SweepTxn creates a taproot keyspend sweep transaction and
|
|
|
|
// attempts to cooperate with the server to create a MuSig2 signature witness.
|
|
|
|
func (s *loopOutSwap) createMuSig2SweepTxn(
|
|
|
|
ctx context.Context, htlcOutpoint wire.OutPoint,
|
|
|
|
htlcValue btcutil.Amount, fee btcutil.Amount) (*wire.MsgTx, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2022-05-05 09:59:31 +00:00
|
|
|
// First assemble our taproot keyspend sweep transaction and get the
|
|
|
|
// sig hash.
|
2022-07-08 12:33:06 +00:00
|
|
|
sweepTx, sweepTxPsbt, sigHash, err := s.sweeper.CreateUnsignedTaprootKeySpendSweepTx(
|
2022-05-05 09:59:31 +00:00
|
|
|
ctx, uint32(s.height), s.htlc, htlcOutpoint, htlcValue, fee,
|
|
|
|
s.DestAddr,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 18:01:07 +00:00
|
|
|
var (
|
2023-07-19 14:19:24 +00:00
|
|
|
signers [][]byte
|
|
|
|
muSig2Version input.MuSig2Version
|
2023-01-09 18:01:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Depending on the MuSig2 version we either pass 32 byte Schnorr
|
|
|
|
// public keys or normal 33 byte public keys.
|
|
|
|
if s.ProtocolVersion >= loopdb.ProtocolVersionMuSig2 {
|
2023-07-19 14:19:24 +00:00
|
|
|
muSig2Version = input.MuSig2Version100RC2
|
2023-01-09 18:01:07 +00:00
|
|
|
signers = [][]byte{
|
|
|
|
s.HtlcKeys.SenderInternalPubKey[:],
|
|
|
|
s.HtlcKeys.ReceiverInternalPubKey[:],
|
|
|
|
}
|
|
|
|
} else {
|
2023-07-19 14:19:24 +00:00
|
|
|
muSig2Version = input.MuSig2Version040
|
2023-01-09 18:01:07 +00:00
|
|
|
signers = [][]byte{
|
|
|
|
s.HtlcKeys.SenderInternalPubKey[1:],
|
|
|
|
s.HtlcKeys.ReceiverInternalPubKey[1:],
|
|
|
|
}
|
2023-01-09 15:36:52 +00:00
|
|
|
}
|
2022-05-05 09:59:31 +00:00
|
|
|
|
2023-01-09 18:01:07 +00:00
|
|
|
htlcScript, ok := s.htlc.HtlcScript.(*swap.HtlcScriptV3)
|
2022-05-05 09:59:31 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("non taproot htlc")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we're creating a local MuSig2 session using the receiver key's
|
|
|
|
// key locator and the htlc's root hash.
|
|
|
|
musig2SessionInfo, err := s.lnd.Signer.MuSig2CreateSession(
|
2023-07-19 14:19:24 +00:00
|
|
|
ctx, muSig2Version, &s.HtlcKeys.ClientScriptKeyLocator, signers,
|
2023-01-09 18:01:07 +00:00
|
|
|
lndclient.MuSig2TaprootTweakOpt(htlcScript.RootHash[:], false),
|
2022-05-05 09:59:31 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the session active, we can now send the server our public nonce
|
|
|
|
// and the sig hash, so that it can create it's own MuSig2 session and
|
|
|
|
// return the server side nonce and partial signature.
|
|
|
|
serverNonce, serverSig, err := s.swapKit.server.MuSig2SignSweep(
|
|
|
|
ctx, s.SwapContract.ProtocolVersion, s.hash,
|
|
|
|
s.swapInvoicePaymentAddr, musig2SessionInfo.PublicNonce[:],
|
2022-07-08 12:33:06 +00:00
|
|
|
sweepTxPsbt,
|
2022-05-05 09:59:31 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var serverPublicNonce [musig2.PubNonceSize]byte
|
|
|
|
copy(serverPublicNonce[:], serverNonce)
|
|
|
|
|
|
|
|
// Register the server's nonce before attempting to create our partial
|
|
|
|
// signature.
|
|
|
|
haveAllNonces, err := s.lnd.Signer.MuSig2RegisterNonces(
|
|
|
|
ctx, musig2SessionInfo.SessionID,
|
|
|
|
[][musig2.PubNonceSize]byte{serverPublicNonce},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check that we have all the nonces.
|
|
|
|
if !haveAllNonces {
|
|
|
|
return nil, fmt.Errorf("invalid MuSig2 session: nonces missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
var digest [32]byte
|
|
|
|
copy(digest[:], sigHash)
|
|
|
|
|
|
|
|
// Since our MuSig2 session has all nonces, we can now create the local
|
|
|
|
// partial signature by signing the sig hash.
|
|
|
|
_, err = s.lnd.Signer.MuSig2Sign(
|
|
|
|
ctx, musig2SessionInfo.SessionID, digest, false,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now combine the partial signatures to use the final combined
|
|
|
|
// signature in the sweep transaction's witness.
|
|
|
|
haveAllSigs, finalSig, err := s.lnd.Signer.MuSig2CombineSig(
|
|
|
|
ctx, musig2SessionInfo.SessionID, [][]byte{serverSig},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !haveAllSigs {
|
|
|
|
return nil, fmt.Errorf("failed to combine signatures")
|
|
|
|
}
|
|
|
|
|
|
|
|
// To be sure that we're good, parse and validate that the combined
|
|
|
|
// signature is indeed valid for the sig hash and the internal pubkey.
|
2022-06-24 11:57:42 +00:00
|
|
|
err = s.executeConfig.verifySchnorrSig(
|
2023-01-09 18:01:07 +00:00
|
|
|
htlcScript.TaprootKey, sigHash, finalSig,
|
2022-06-24 11:57:42 +00:00
|
|
|
)
|
2022-05-05 09:59:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we know the signature is correct, we can fill it in to our
|
|
|
|
// witness.
|
|
|
|
sweepTx.TxIn[0].Witness = wire.TxWitness{
|
|
|
|
finalSig,
|
|
|
|
}
|
|
|
|
|
|
|
|
return sweepTx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sweepConfTarget returns the confirmation target for the htlc sweep or false
|
|
|
|
// if we're too late.
|
|
|
|
func (s *loopOutSwap) sweepConfTarget() (int32, bool) {
|
2021-05-12 12:09:28 +00:00
|
|
|
remainingBlocks := s.CltvExpiry - s.height
|
|
|
|
blocksToLastReveal := remainingBlocks - MinLoopOutPreimageRevealDelta
|
|
|
|
preimageRevealed := s.state == loopdb.StatePreimageRevealed
|
|
|
|
|
|
|
|
// If we have not revealed our preimage, and we don't have time left
|
|
|
|
// to sweep the swap, we abandon the swap because we can no longer
|
|
|
|
// sweep the success path (without potentially having to compete with
|
|
|
|
// the server's timeout sweep), and we have not had any coins pulled
|
|
|
|
// off-chain.
|
|
|
|
if blocksToLastReveal <= 0 && !preimageRevealed {
|
|
|
|
s.log.Infof("Preimage can no longer be safely revealed: "+
|
|
|
|
"expires at: %v, current height: %v", s.CltvExpiry,
|
|
|
|
s.height)
|
|
|
|
|
|
|
|
s.state = loopdb.StateFailTimeout
|
2022-05-05 09:59:31 +00:00
|
|
|
return 0, false
|
2021-05-12 12:09:28 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 18:41:57 +00:00
|
|
|
// Calculate the transaction fee based on the confirmation target
|
|
|
|
// required to sweep the HTLC before the timeout. We'll use the
|
|
|
|
// confirmation target provided by the client unless we've come too
|
|
|
|
// close to the expiration height, in which case we'll use the default
|
|
|
|
// if it is better than what the client provided.
|
|
|
|
confTarget := s.SweepConfTarget
|
2021-05-12 12:09:28 +00:00
|
|
|
if remainingBlocks <= DefaultSweepConfTargetDelta &&
|
2019-06-25 18:41:57 +00:00
|
|
|
confTarget > DefaultSweepConfTarget {
|
2021-05-12 12:09:28 +00:00
|
|
|
|
2019-06-25 18:41:57 +00:00
|
|
|
confTarget = DefaultSweepConfTarget
|
|
|
|
}
|
2021-05-12 12:09:28 +00:00
|
|
|
|
2022-05-05 09:59:31 +00:00
|
|
|
return confTarget, true
|
|
|
|
}
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2022-05-05 09:59:31 +00:00
|
|
|
// clampSweepFee will clamp the passed in sweep fee to the maximum configured
|
|
|
|
// miner fee. Returns false if sweeping should not continue. Note that in the
|
|
|
|
// MuSig2 case we always continue as the preimage is revealed to the server
|
|
|
|
// before cooperatively signing the sweep transaction.
|
|
|
|
func (s *loopOutSwap) clampSweepFee(fee btcutil.Amount) (btcutil.Amount, bool) {
|
2019-06-25 18:41:57 +00:00
|
|
|
// Ensure it doesn't exceed our maximum fee allowed.
|
2019-03-06 20:13:50 +00:00
|
|
|
if fee > s.MaxMinerFee {
|
2019-06-25 18:41:57 +00:00
|
|
|
s.log.Warnf("Required fee %v exceeds max miner fee of %v",
|
2019-03-06 20:13:50 +00:00
|
|
|
fee, s.MaxMinerFee)
|
|
|
|
|
2022-05-05 09:59:31 +00:00
|
|
|
if s.state == loopdb.StatePreimageRevealed {
|
2019-03-06 20:13:50 +00:00
|
|
|
// The currently required fee exceeds the max, but we
|
|
|
|
// already revealed the preimage. The best we can do now
|
|
|
|
// is to republish with the max fee.
|
|
|
|
fee = s.MaxMinerFee
|
|
|
|
} else {
|
|
|
|
s.log.Warnf("Not revealing preimage")
|
2022-05-05 09:59:31 +00:00
|
|
|
return 0, false
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 09:59:31 +00:00
|
|
|
return fee, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// sweepMuSig2 attempts to sweep the on-chain HTLC using MuSig2. If anything
|
|
|
|
// fails, we'll log it but will simply return to allow further retries. Since
|
|
|
|
// the preimage is revealed by the time we attempt to MuSig2 sweep, we'll need
|
|
|
|
// to fall back to a script spend sweep if all MuSig2 sweep attempts fail (for
|
|
|
|
// example the server could be down due to maintenance or any other issue
|
|
|
|
// making the cooperative sweep fail).
|
|
|
|
func (s *loopOutSwap) sweepMuSig2(ctx context.Context,
|
|
|
|
htlcOutpoint wire.OutPoint, htlcValue btcutil.Amount) bool {
|
|
|
|
|
|
|
|
addInputToEstimator := func(e *input.TxWeightEstimator) error {
|
|
|
|
e.AddTaprootKeySpendInput(txscript.SigHashDefault)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
confTarget, _ := s.sweepConfTarget()
|
|
|
|
fee, err := s.sweeper.GetSweepFee(
|
|
|
|
ctx, addInputToEstimator, s.DestAddr, confTarget,
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2022-05-05 09:59:31 +00:00
|
|
|
s.log.Warnf("Failed to estimate fee MuSig2 sweep txn: %v", err)
|
|
|
|
return false
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2022-05-05 09:59:31 +00:00
|
|
|
fee, _ = s.clampSweepFee(fee)
|
|
|
|
|
|
|
|
// Now attempt the co-signing of the txn.
|
|
|
|
sweepTx, err := s.createMuSig2SweepTxn(
|
|
|
|
ctx, htlcOutpoint, htlcValue, fee,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Warnf("Failed to create MuSig2 sweep txn: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, try publish the txn.
|
|
|
|
s.log.Infof("Sweep on chain HTLC using MuSig2 to address %v "+
|
|
|
|
"fee %v (tx %v)", s.DestAddr, fee, sweepTx.TxHash())
|
|
|
|
|
|
|
|
err = s.lnd.WalletKit.PublishTransaction(
|
|
|
|
ctx, sweepTx,
|
|
|
|
labels.LoopOutSweepSuccess(swap.ShortHash(&s.hash)),
|
|
|
|
)
|
|
|
|
if err != nil {
|
2022-07-27 18:33:07 +00:00
|
|
|
var sweepTxBuf bytes.Buffer
|
|
|
|
if err := sweepTx.Serialize(&sweepTxBuf); err != nil {
|
|
|
|
s.log.Warnf("Unable to serialize sweep txn: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.log.Warnf("Publish of MuSig2 sweep failed: %v. Raw tx: %x",
|
|
|
|
err, sweepTxBuf.Bytes())
|
|
|
|
|
2022-05-05 09:59:31 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *loopOutSwap) setStatePreimageRevealed(ctx context.Context) error {
|
2019-03-07 04:32:24 +00:00
|
|
|
if s.state != loopdb.StatePreimageRevealed {
|
|
|
|
s.state = loopdb.StatePreimageRevealed
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
err := s.persistState(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 09:59:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sweep tries to sweep the given htlc to a destination address. It takes into
|
|
|
|
// account the max miner fee and unless the preimage is already revealed
|
|
|
|
// (MuSig2 case), marks the preimage as revealed when it published the tx. If
|
|
|
|
// the preimage has not yet been revealed, and the time during which we can
|
|
|
|
// safely reveal it has passed, the swap will be marked as failed, and the
|
|
|
|
// function will return.
|
|
|
|
func (s *loopOutSwap) sweep(ctx context.Context, htlcOutpoint wire.OutPoint,
|
|
|
|
htlcValue btcutil.Amount) error {
|
|
|
|
|
|
|
|
confTarget, canSweep := s.sweepConfTarget()
|
|
|
|
if !canSweep {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fee, err := s.sweeper.GetSweepFee(
|
|
|
|
ctx, s.htlc.AddSuccessToEstimator, s.DestAddr, confTarget,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fee, canSweep = s.clampSweepFee(fee)
|
|
|
|
if !canSweep {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
witnessFunc := func(sig []byte) (wire.TxWitness, error) {
|
|
|
|
return s.htlc.GenSuccessWitness(sig, s.Preimage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the full script required to unlock the output.
|
|
|
|
redeemScript := s.htlc.SuccessScript()
|
|
|
|
|
|
|
|
// Create sweep tx.
|
|
|
|
sweepTx, err := s.sweeper.CreateSweepTx(
|
|
|
|
ctx, s.height, s.htlc.SuccessSequence(), s.htlc,
|
2023-01-05 17:16:05 +00:00
|
|
|
htlcOutpoint, s.contract.HtlcKeys.ReceiverScriptKey,
|
|
|
|
redeemScript, witnessFunc, htlcValue, fee, s.DestAddr,
|
2022-05-05 09:59:31 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before publishing the tx, already mark the preimage as revealed. This
|
|
|
|
// is a precaution in case the publish call never returns and would
|
|
|
|
// leave us thinking we didn't reveal yet.
|
|
|
|
err = s.setStatePreimageRevealed(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
// Publish tx.
|
|
|
|
s.log.Infof("Sweep on chain HTLC to address %v with fee %v (tx %v)",
|
|
|
|
s.DestAddr, fee, sweepTx.TxHash())
|
|
|
|
|
2020-10-15 07:15:25 +00:00
|
|
|
err = s.lnd.WalletKit.PublishTransaction(
|
|
|
|
ctx, sweepTx,
|
|
|
|
labels.LoopOutSweepSuccess(swap.ShortHash(&s.hash)),
|
|
|
|
)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
2022-07-27 18:33:07 +00:00
|
|
|
var sweepTxBuf bytes.Buffer
|
|
|
|
if err := sweepTx.Serialize(&sweepTxBuf); err != nil {
|
|
|
|
s.log.Warnf("Unable to serialize sweep txn: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.log.Warnf("Publish sweep failed: %v. Raw tx: %x",
|
|
|
|
err, sweepTxBuf.Bytes())
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
// validateLoopOutContract validates the contract parameters against our
|
2019-03-06 20:13:50 +00:00
|
|
|
// request.
|
2022-12-15 15:43:38 +00:00
|
|
|
func validateLoopOutContract(lnd *lndclient.LndServices, request *OutRequest,
|
|
|
|
swapHash lntypes.Hash, response *newLoopOutResponse) error {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
// Check invoice amounts.
|
|
|
|
chainParams := lnd.ChainParams
|
|
|
|
|
2021-12-01 16:04:40 +00:00
|
|
|
_, _, swapInvoiceHash, swapInvoiceAmt, err := swap.DecodeInvoice(
|
2019-03-06 20:13:50 +00:00
|
|
|
chainParams, response.swapInvoice,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-31 08:46:51 +00:00
|
|
|
if swapInvoiceHash != swapHash {
|
|
|
|
return fmt.Errorf(
|
2022-05-05 09:59:31 +00:00
|
|
|
"cannot initiate swap, swap invoice hash %v not equal "+
|
|
|
|
"generated swap hash %v", swapInvoiceHash, swapHash)
|
2019-07-31 08:46:51 +00:00
|
|
|
}
|
|
|
|
|
2021-12-01 16:04:40 +00:00
|
|
|
_, _, _, prepayInvoiceAmt, err := swap.DecodeInvoice(
|
2019-03-06 20:13:50 +00:00
|
|
|
chainParams, response.prepayInvoice,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
swapFee := swapInvoiceAmt + prepayInvoiceAmt - request.Amount
|
|
|
|
if swapFee > request.MaxSwapFee {
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Warnf("Swap fee %v exceeding maximum of %v",
|
2019-03-06 20:13:50 +00:00
|
|
|
swapFee, request.MaxSwapFee)
|
|
|
|
|
|
|
|
return ErrSwapFeeTooHigh
|
|
|
|
}
|
|
|
|
|
|
|
|
if prepayInvoiceAmt > request.MaxPrepayAmount {
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Warnf("Prepay amount %v exceeding maximum of %v",
|
2019-03-06 20:13:50 +00:00
|
|
|
prepayInvoiceAmt, request.MaxPrepayAmount)
|
|
|
|
|
|
|
|
return ErrPrepayAmountTooHigh
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|