2020-01-03 13:01:31 +00:00
|
|
|
package loopd
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-03-12 15:10:37 +00:00
|
|
|
"errors"
|
2019-03-06 20:13:50 +00:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
2020-01-31 12:57:21 +00:00
|
|
|
"sync"
|
2019-11-14 09:35:32 +00:00
|
|
|
"time"
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2020-01-31 12:57:21 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2019-03-06 20:13:50 +00:00
|
|
|
"github.com/lightningnetwork/lnd/queue"
|
2020-02-11 12:25:03 +00:00
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
"github.com/lightninglabs/loop"
|
2019-03-06 23:29:44 +00:00
|
|
|
"github.com/lightninglabs/loop/lndclient"
|
2019-03-07 04:32:24 +00:00
|
|
|
"github.com/lightninglabs/loop/loopdb"
|
2019-10-09 10:36:16 +00:00
|
|
|
"github.com/lightninglabs/loop/swap"
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
"github.com/btcsuite/btcutil"
|
2019-03-06 23:53:17 +00:00
|
|
|
"github.com/lightninglabs/loop/looprpc"
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
2019-06-25 18:41:45 +00:00
|
|
|
const (
|
|
|
|
completedSwapsCount = 5
|
|
|
|
|
|
|
|
// minConfTarget is the minimum confirmation target we'll allow clients
|
|
|
|
// to specify. This is driven by the minimum confirmation target allowed
|
|
|
|
// by the backing fee estimator.
|
|
|
|
minConfTarget = 2
|
|
|
|
)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
// swapClientServer implements the grpc service exposed by loopd.
|
2019-03-06 20:13:50 +00:00
|
|
|
type swapClientServer struct {
|
2020-01-31 12:57:21 +00:00
|
|
|
impl *loop.Client
|
|
|
|
lnd *lndclient.LndServices
|
|
|
|
swaps map[lntypes.Hash]loop.SwapInfo
|
|
|
|
subscribers map[int]chan<- interface{}
|
|
|
|
statusChan chan loop.SwapInfo
|
|
|
|
nextSubscriberID int
|
|
|
|
swapsLock sync.Mutex
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
// LoopOut initiates an loop out swap with the given parameters. The call
|
2019-03-06 20:13:50 +00:00
|
|
|
// returns after the swap has been set up with the swap server. From that point
|
2019-03-07 04:32:24 +00:00
|
|
|
// onwards, progress can be tracked via the LoopOutStatus stream that is
|
2019-03-06 20:13:50 +00:00
|
|
|
// returned from Monitor().
|
2019-03-07 04:32:24 +00:00
|
|
|
func (s *swapClientServer) LoopOut(ctx context.Context,
|
|
|
|
in *looprpc.LoopOutRequest) (
|
2019-03-06 23:53:17 +00:00
|
|
|
*looprpc.SwapResponse, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Infof("Loop out request received")
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-06-25 18:41:49 +00:00
|
|
|
sweepConfTarget, err := validateConfTarget(
|
|
|
|
in.SweepConfTarget, loop.DefaultSweepConfTarget,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
var sweepAddr btcutil.Address
|
|
|
|
if in.Dest == "" {
|
|
|
|
// Generate sweep address if none specified.
|
|
|
|
var err error
|
|
|
|
sweepAddr, err = s.lnd.WalletKit.NextAddr(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("NextAddr error: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var err error
|
2019-03-25 10:06:16 +00:00
|
|
|
sweepAddr, err = btcutil.DecodeAddress(
|
|
|
|
in.Dest, s.lnd.ChainParams,
|
|
|
|
)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("decode address: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
req := &loop.OutRequest{
|
2019-03-06 20:13:50 +00:00
|
|
|
Amount: btcutil.Amount(in.Amt),
|
|
|
|
DestAddr: sweepAddr,
|
|
|
|
MaxMinerFee: btcutil.Amount(in.MaxMinerFee),
|
|
|
|
MaxPrepayAmount: btcutil.Amount(in.MaxPrepayAmt),
|
|
|
|
MaxPrepayRoutingFee: btcutil.Amount(in.MaxPrepayRoutingFee),
|
|
|
|
MaxSwapRoutingFee: btcutil.Amount(in.MaxSwapRoutingFee),
|
|
|
|
MaxSwapFee: btcutil.Amount(in.MaxSwapFee),
|
2019-06-25 18:41:49 +00:00
|
|
|
SweepConfTarget: sweepConfTarget,
|
2019-11-14 09:35:32 +00:00
|
|
|
SwapPublicationDeadline: time.Unix(
|
|
|
|
int64(in.SwapPublicationDeadline), 0,
|
|
|
|
),
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
2020-05-19 08:21:13 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case in.LoopOutChannel != 0 && len(in.OutgoingChanSet) > 0:
|
|
|
|
return nil, errors.New("loop_out_channel and outgoing_" +
|
|
|
|
"chan_ids are mutually exclusive")
|
|
|
|
|
|
|
|
case in.LoopOutChannel != 0:
|
2020-05-19 07:54:27 +00:00
|
|
|
req.OutgoingChanSet = loopdb.ChannelSet{in.LoopOutChannel}
|
2020-05-19 08:21:13 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
req.OutgoingChanSet = in.OutgoingChanSet
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
2020-05-19 08:21:13 +00:00
|
|
|
|
2019-03-28 12:29:21 +00:00
|
|
|
hash, htlc, err := s.impl.LoopOut(ctx, req)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Errorf("LoopOut: %v", err)
|
2019-03-06 20:13:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-06 23:53:17 +00:00
|
|
|
return &looprpc.SwapResponse{
|
2019-03-28 12:29:21 +00:00
|
|
|
Id: hash.String(),
|
2020-01-31 12:57:28 +00:00
|
|
|
IdBytes: hash[:],
|
2019-03-28 12:29:21 +00:00
|
|
|
HtlcAddress: htlc.String(),
|
2019-03-06 20:13:50 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
func (s *swapClientServer) marshallSwap(loopSwap *loop.SwapInfo) (
|
2019-03-06 23:53:17 +00:00
|
|
|
*looprpc.SwapStatus, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-03-06 23:53:17 +00:00
|
|
|
var state looprpc.SwapState
|
2019-03-07 04:32:24 +00:00
|
|
|
switch loopSwap.State {
|
|
|
|
case loopdb.StateInitiated:
|
2019-03-06 23:53:17 +00:00
|
|
|
state = looprpc.SwapState_INITIATED
|
2019-03-07 04:32:24 +00:00
|
|
|
case loopdb.StatePreimageRevealed:
|
2019-03-06 23:53:17 +00:00
|
|
|
state = looprpc.SwapState_PREIMAGE_REVEALED
|
2019-03-12 15:10:37 +00:00
|
|
|
case loopdb.StateHtlcPublished:
|
|
|
|
state = looprpc.SwapState_HTLC_PUBLISHED
|
2019-04-02 08:51:51 +00:00
|
|
|
case loopdb.StateInvoiceSettled:
|
|
|
|
state = looprpc.SwapState_INVOICE_SETTLED
|
2019-03-07 04:32:24 +00:00
|
|
|
case loopdb.StateSuccess:
|
2019-03-06 23:53:17 +00:00
|
|
|
state = looprpc.SwapState_SUCCESS
|
2019-03-06 20:13:50 +00:00
|
|
|
default:
|
|
|
|
// Return less granular status over rpc.
|
2019-03-06 23:53:17 +00:00
|
|
|
state = looprpc.SwapState_FAILED
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 15:10:37 +00:00
|
|
|
var swapType looprpc.SwapType
|
2020-04-24 15:35:32 +00:00
|
|
|
var htlcAddress, htlcAddressP2WSH, htlcAddressNP2WSH string
|
|
|
|
|
2019-03-12 15:10:37 +00:00
|
|
|
switch loopSwap.SwapType {
|
2019-10-09 10:36:16 +00:00
|
|
|
case swap.TypeIn:
|
2019-03-12 15:10:37 +00:00
|
|
|
swapType = looprpc.SwapType_LOOP_IN
|
2020-04-28 15:30:20 +00:00
|
|
|
htlcAddressP2WSH = loopSwap.HtlcAddressP2WSH.EncodeAddress()
|
|
|
|
|
2020-04-24 15:35:32 +00:00
|
|
|
if loopSwap.ExternalHtlc {
|
2020-04-28 15:30:20 +00:00
|
|
|
htlcAddressNP2WSH = loopSwap.HtlcAddressNP2WSH.EncodeAddress()
|
|
|
|
htlcAddress = htlcAddressNP2WSH
|
|
|
|
} else {
|
|
|
|
htlcAddress = htlcAddressP2WSH
|
2020-04-24 15:35:32 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 10:36:16 +00:00
|
|
|
case swap.TypeOut:
|
2019-03-12 15:10:37 +00:00
|
|
|
swapType = looprpc.SwapType_LOOP_OUT
|
2020-04-24 15:35:32 +00:00
|
|
|
htlcAddressP2WSH = loopSwap.HtlcAddressP2WSH.EncodeAddress()
|
|
|
|
htlcAddress = htlcAddressP2WSH
|
|
|
|
|
2019-03-12 15:10:37 +00:00
|
|
|
default:
|
|
|
|
return nil, errors.New("unknown swap type")
|
|
|
|
}
|
|
|
|
|
2019-03-06 23:53:17 +00:00
|
|
|
return &looprpc.SwapStatus{
|
2020-04-24 15:35:32 +00:00
|
|
|
Amt: int64(loopSwap.AmountRequested),
|
|
|
|
Id: loopSwap.SwapHash.String(),
|
|
|
|
IdBytes: loopSwap.SwapHash[:],
|
|
|
|
State: state,
|
|
|
|
InitiationTime: loopSwap.InitiationTime.UnixNano(),
|
|
|
|
LastUpdateTime: loopSwap.LastUpdate.UnixNano(),
|
|
|
|
HtlcAddress: htlcAddress,
|
|
|
|
HtlcAddressP2Wsh: htlcAddressP2WSH,
|
|
|
|
HtlcAddressNp2Wsh: htlcAddressNP2WSH,
|
|
|
|
Type: swapType,
|
|
|
|
CostServer: int64(loopSwap.Cost.Server),
|
|
|
|
CostOnchain: int64(loopSwap.Cost.Onchain),
|
|
|
|
CostOffchain: int64(loopSwap.Cost.Offchain),
|
2019-03-06 20:13:50 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Monitor will return a stream of swap updates for currently active swaps.
|
2019-03-06 23:53:17 +00:00
|
|
|
func (s *swapClientServer) Monitor(in *looprpc.MonitorRequest,
|
|
|
|
server looprpc.SwapClient_MonitorServer) error {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Infof("Monitor request received")
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
send := func(info loop.SwapInfo) error {
|
2019-03-06 20:13:50 +00:00
|
|
|
rpcSwap, err := s.marshallSwap(&info)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return server.Send(rpcSwap)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a notification queue for this subscriber.
|
|
|
|
queue := queue.NewConcurrentQueue(20)
|
|
|
|
queue.Start()
|
|
|
|
|
|
|
|
// Add this subscriber to the global subscriber list. Also create a
|
|
|
|
// snapshot of all pending and completed swaps within the lock, to
|
|
|
|
// prevent subscribers from receiving duplicate updates.
|
2020-01-31 12:57:21 +00:00
|
|
|
s.swapsLock.Lock()
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2020-01-31 12:57:21 +00:00
|
|
|
id := s.nextSubscriberID
|
|
|
|
s.nextSubscriberID++
|
|
|
|
s.subscribers[id] = queue.ChanIn()
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
var pendingSwaps, completedSwaps []loop.SwapInfo
|
2020-01-31 12:57:21 +00:00
|
|
|
for _, swap := range s.swaps {
|
2019-03-07 04:32:24 +00:00
|
|
|
if swap.State.Type() == loopdb.StateTypePending {
|
2019-03-06 20:13:50 +00:00
|
|
|
pendingSwaps = append(pendingSwaps, swap)
|
|
|
|
} else {
|
|
|
|
completedSwaps = append(completedSwaps, swap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-31 12:57:21 +00:00
|
|
|
s.swapsLock.Unlock()
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
defer func() {
|
2020-01-31 12:57:21 +00:00
|
|
|
s.swapsLock.Lock()
|
|
|
|
delete(s.subscribers, id)
|
|
|
|
s.swapsLock.Unlock()
|
2020-04-01 17:04:50 +00:00
|
|
|
queue.Stop()
|
2019-03-06 20:13:50 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Sort completed swaps new to old.
|
|
|
|
sort.Slice(completedSwaps, func(i, j int) bool {
|
|
|
|
return completedSwaps[i].LastUpdate.After(
|
|
|
|
completedSwaps[j].LastUpdate,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Discard all but top x latest.
|
|
|
|
if len(completedSwaps) > completedSwapsCount {
|
|
|
|
completedSwaps = completedSwaps[:completedSwapsCount]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Concatenate both sets.
|
|
|
|
filteredSwaps := append(pendingSwaps, completedSwaps...)
|
|
|
|
|
|
|
|
// Sort again, but this time old to new.
|
|
|
|
sort.Slice(filteredSwaps, func(i, j int) bool {
|
|
|
|
return filteredSwaps[i].LastUpdate.Before(
|
|
|
|
filteredSwaps[j].LastUpdate,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Return swaps to caller.
|
|
|
|
for _, swap := range filteredSwaps {
|
|
|
|
if err := send(swap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// As long as the client is connected, keep passing through swap
|
|
|
|
// updates.
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case queueItem, ok := <-queue.ChanOut():
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
swap := queueItem.(loop.SwapInfo)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err := send(swap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case <-server.Context().Done():
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-31 12:57:22 +00:00
|
|
|
// ListSwaps returns a list of all currently known swaps and their current
|
|
|
|
// status.
|
|
|
|
func (s *swapClientServer) ListSwaps(_ context.Context,
|
|
|
|
_ *looprpc.ListSwapsRequest) (*looprpc.ListSwapsResponse, error) {
|
|
|
|
|
|
|
|
var (
|
|
|
|
rpcSwaps = make([]*looprpc.SwapStatus, len(s.swaps))
|
|
|
|
idx = 0
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2020-04-30 13:48:58 +00:00
|
|
|
s.swapsLock.Lock()
|
|
|
|
defer s.swapsLock.Unlock()
|
|
|
|
|
2020-01-31 12:57:22 +00:00
|
|
|
// We can just use the server's in-memory cache as that contains the
|
|
|
|
// most up-to-date state including temporary failures which aren't
|
|
|
|
// persisted to disk. The swaps field is a map, that's why we need an
|
|
|
|
// additional index.
|
|
|
|
for _, swp := range s.swaps {
|
|
|
|
swp := swp
|
|
|
|
rpcSwaps[idx], err = s.marshallSwap(&swp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
return &looprpc.ListSwapsResponse{Swaps: rpcSwaps}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SwapInfo returns all known details about a single swap.
|
|
|
|
func (s *swapClientServer) SwapInfo(_ context.Context,
|
|
|
|
req *looprpc.SwapInfoRequest) (*looprpc.SwapStatus, error) {
|
|
|
|
|
|
|
|
swapHash, err := lntypes.MakeHash(req.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing swap hash: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just return the server's in-memory cache here too as we also want to
|
|
|
|
// return temporary failures to the client.
|
|
|
|
swp, ok := s.swaps[swapHash]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("swap with hash %s not found", req.Id)
|
|
|
|
}
|
|
|
|
return s.marshallSwap(&swp)
|
|
|
|
}
|
|
|
|
|
2019-03-12 22:34:45 +00:00
|
|
|
// LoopOutTerms returns the terms that the server enforces for loop out swaps.
|
|
|
|
func (s *swapClientServer) LoopOutTerms(ctx context.Context,
|
2019-03-07 04:32:24 +00:00
|
|
|
req *looprpc.TermsRequest) (*looprpc.TermsResponse, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Infof("Loop out terms request received")
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
terms, err := s.impl.LoopOutTerms(ctx)
|
2019-03-06 20:13:50 +00:00
|
|
|
if err != nil {
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Errorf("Terms request: %v", err)
|
2019-03-06 20:13:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-06 23:53:17 +00:00
|
|
|
return &looprpc.TermsResponse{
|
2019-03-06 20:13:50 +00:00
|
|
|
MinSwapAmount: int64(terms.MinSwapAmount),
|
|
|
|
MaxSwapAmount: int64(terms.MaxSwapAmount),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-12 22:34:45 +00:00
|
|
|
// LoopOutQuote returns a quote for a loop out swap with the provided
|
|
|
|
// parameters.
|
|
|
|
func (s *swapClientServer) LoopOutQuote(ctx context.Context,
|
2019-03-06 23:53:17 +00:00
|
|
|
req *looprpc.QuoteRequest) (*looprpc.QuoteResponse, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-06-25 18:41:49 +00:00
|
|
|
confTarget, err := validateConfTarget(
|
|
|
|
req.ConfTarget, loop.DefaultSweepConfTarget,
|
|
|
|
)
|
2019-06-25 18:41:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-07 04:32:24 +00:00
|
|
|
quote, err := s.impl.LoopOutQuote(ctx, &loop.LoopOutQuoteRequest{
|
2019-03-06 20:13:50 +00:00
|
|
|
Amount: btcutil.Amount(req.Amt),
|
2019-06-25 18:41:45 +00:00
|
|
|
SweepConfTarget: confTarget,
|
2020-01-07 12:52:42 +00:00
|
|
|
SwapPublicationDeadline: time.Unix(
|
|
|
|
int64(req.SwapPublicationDeadline), 0,
|
|
|
|
),
|
2019-03-06 20:13:50 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-08 20:28:20 +00:00
|
|
|
|
2019-03-06 23:53:17 +00:00
|
|
|
return &looprpc.QuoteResponse{
|
2019-10-08 20:28:20 +00:00
|
|
|
MinerFee: int64(quote.MinerFee),
|
|
|
|
PrepayAmt: int64(quote.PrepayAmount),
|
|
|
|
SwapFee: int64(quote.SwapFee),
|
|
|
|
SwapPaymentDest: quote.SwapPaymentDest[:],
|
|
|
|
CltvDelta: quote.CltvDelta,
|
2019-03-06 20:13:50 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2019-03-12 15:10:37 +00:00
|
|
|
|
|
|
|
// GetTerms returns the terms that the server enforces for swaps.
|
|
|
|
func (s *swapClientServer) GetLoopInTerms(ctx context.Context, req *looprpc.TermsRequest) (
|
|
|
|
*looprpc.TermsResponse, error) {
|
|
|
|
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Infof("Loop in terms request received")
|
2019-03-12 15:10:37 +00:00
|
|
|
|
|
|
|
terms, err := s.impl.LoopInTerms(ctx)
|
|
|
|
if err != nil {
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Errorf("Terms request: %v", err)
|
2019-03-12 15:10:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &looprpc.TermsResponse{
|
|
|
|
MinSwapAmount: int64(terms.MinSwapAmount),
|
|
|
|
MaxSwapAmount: int64(terms.MaxSwapAmount),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetQuote returns a quote for a swap with the provided parameters.
|
|
|
|
func (s *swapClientServer) GetLoopInQuote(ctx context.Context,
|
|
|
|
req *looprpc.QuoteRequest) (*looprpc.QuoteResponse, error) {
|
|
|
|
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Infof("Loop in quote request received")
|
2019-03-12 15:10:37 +00:00
|
|
|
|
2020-04-15 07:10:21 +00:00
|
|
|
htlcConfTarget, err := validateLoopInRequest(
|
|
|
|
req.ConfTarget, req.ExternalHtlc,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-12 15:10:37 +00:00
|
|
|
quote, err := s.impl.LoopInQuote(ctx, &loop.LoopInQuoteRequest{
|
|
|
|
Amount: btcutil.Amount(req.Amt),
|
2020-04-15 07:10:21 +00:00
|
|
|
HtlcConfTarget: htlcConfTarget,
|
2019-09-23 08:08:34 +00:00
|
|
|
ExternalHtlc: req.ExternalHtlc,
|
2019-03-12 15:10:37 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &looprpc.QuoteResponse{
|
|
|
|
MinerFee: int64(quote.MinerFee),
|
|
|
|
SwapFee: int64(quote.SwapFee),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *swapClientServer) LoopIn(ctx context.Context,
|
|
|
|
in *looprpc.LoopInRequest) (
|
|
|
|
*looprpc.SwapResponse, error) {
|
|
|
|
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Infof("Loop in request received")
|
2019-03-12 15:10:37 +00:00
|
|
|
|
2020-04-15 07:10:21 +00:00
|
|
|
htlcConfTarget, err := validateLoopInRequest(
|
|
|
|
in.HtlcConfTarget, in.ExternalHtlc,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-12 15:10:37 +00:00
|
|
|
req := &loop.LoopInRequest{
|
|
|
|
Amount: btcutil.Amount(in.Amt),
|
|
|
|
MaxMinerFee: btcutil.Amount(in.MaxMinerFee),
|
|
|
|
MaxSwapFee: btcutil.Amount(in.MaxSwapFee),
|
2020-04-15 07:10:21 +00:00
|
|
|
HtlcConfTarget: htlcConfTarget,
|
2019-03-28 12:29:21 +00:00
|
|
|
ExternalHtlc: in.ExternalHtlc,
|
2019-03-12 15:10:37 +00:00
|
|
|
}
|
2020-02-11 12:25:03 +00:00
|
|
|
if in.LastHop != nil {
|
|
|
|
lastHop, err := route.NewVertexFromBytes(in.LastHop)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.LastHop = &lastHop
|
2019-03-12 15:10:37 +00:00
|
|
|
}
|
2020-04-29 16:30:44 +00:00
|
|
|
swapInfo, err := s.impl.LoopIn(ctx, req)
|
2019-03-12 15:10:37 +00:00
|
|
|
if err != nil {
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Errorf("Loop in: %v", err)
|
2019-03-12 15:10:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:35:32 +00:00
|
|
|
response := &looprpc.SwapResponse{
|
2020-05-12 16:51:42 +00:00
|
|
|
Id: swapInfo.SwapHash.String(),
|
|
|
|
IdBytes: swapInfo.SwapHash[:],
|
|
|
|
HtlcAddressP2Wsh: swapInfo.HtlcAddressP2WSH.String(),
|
2020-04-24 15:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.ExternalHtlc {
|
2020-05-12 16:51:42 +00:00
|
|
|
response.HtlcAddressNp2Wsh = swapInfo.HtlcAddressNP2WSH.String()
|
|
|
|
response.HtlcAddress = response.HtlcAddressNp2Wsh
|
|
|
|
} else {
|
|
|
|
response.HtlcAddress = response.HtlcAddressP2Wsh
|
2020-04-24 15:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
2019-03-12 15:10:37 +00:00
|
|
|
}
|
2019-06-25 18:41:45 +00:00
|
|
|
|
2019-11-15 12:57:03 +00:00
|
|
|
// GetLsatTokens returns all tokens that are contained in the LSAT token store.
|
|
|
|
func (s *swapClientServer) GetLsatTokens(ctx context.Context,
|
|
|
|
_ *looprpc.TokensRequest) (*looprpc.TokensResponse, error) {
|
|
|
|
|
|
|
|
log.Infof("Get LSAT tokens request received")
|
|
|
|
|
|
|
|
tokens, err := s.impl.LsatStore.AllTokens()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rpcTokens := make([]*looprpc.LsatToken, len(tokens))
|
|
|
|
idx := 0
|
|
|
|
for key, token := range tokens {
|
|
|
|
macBytes, err := token.BaseMacaroon().MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rpcTokens[idx] = &looprpc.LsatToken{
|
|
|
|
BaseMacaroon: macBytes,
|
|
|
|
PaymentHash: token.PaymentHash[:],
|
|
|
|
PaymentPreimage: token.Preimage[:],
|
|
|
|
AmountPaidMsat: int64(token.AmountPaid),
|
|
|
|
RoutingFeePaidMsat: int64(token.RoutingFeePaid),
|
|
|
|
TimeCreated: token.TimeCreated.Unix(),
|
|
|
|
Expired: !token.IsValid(),
|
|
|
|
StorageName: key,
|
|
|
|
}
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
|
|
|
|
return &looprpc.TokensResponse{Tokens: rpcTokens}, nil
|
|
|
|
}
|
|
|
|
|
2020-01-31 12:57:21 +00:00
|
|
|
// processStatusUpdates reads updates on the status channel and processes them.
|
|
|
|
//
|
|
|
|
// NOTE: This must run inside a goroutine as it blocks until the main context
|
|
|
|
// shuts down.
|
|
|
|
func (s *swapClientServer) processStatusUpdates(mainCtx context.Context) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// On updates, refresh the server's in-memory state and inform
|
|
|
|
// subscribers about the changes.
|
|
|
|
case swp := <-s.statusChan:
|
|
|
|
s.swapsLock.Lock()
|
|
|
|
s.swaps[swp.SwapHash] = swp
|
|
|
|
|
|
|
|
for _, subscriber := range s.subscribers {
|
|
|
|
select {
|
|
|
|
case subscriber <- swp:
|
|
|
|
case <-mainCtx.Done():
|
2020-04-01 17:04:50 +00:00
|
|
|
s.swapsLock.Unlock()
|
2020-01-31 12:57:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.swapsLock.Unlock()
|
|
|
|
|
|
|
|
// Server is shutting down.
|
|
|
|
case <-mainCtx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-25 18:41:45 +00:00
|
|
|
// validateConfTarget ensures the given confirmation target is valid. If one
|
|
|
|
// isn't specified (0 value), then the default target is used.
|
|
|
|
func validateConfTarget(target, defaultTarget int32) (int32, error) {
|
|
|
|
switch {
|
2020-04-15 07:10:21 +00:00
|
|
|
case target == 0:
|
|
|
|
return defaultTarget, nil
|
|
|
|
|
2019-06-25 18:41:45 +00:00
|
|
|
// Ensure the target respects our minimum threshold.
|
|
|
|
case target < minConfTarget:
|
|
|
|
return 0, fmt.Errorf("a confirmation target of at least %v "+
|
|
|
|
"must be provided", minConfTarget)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return target, nil
|
|
|
|
}
|
|
|
|
}
|
2020-04-15 07:10:21 +00:00
|
|
|
|
|
|
|
// validateLoopInRequest fails if the mutually exclusive conf target and
|
|
|
|
// external parameters are both set.
|
|
|
|
func validateLoopInRequest(htlcConfTarget int32, external bool) (int32, error) {
|
|
|
|
// If the htlc is going to be externally set, the htlcConfTarget should
|
|
|
|
// not be set, because it has no relevance when the htlc is external.
|
|
|
|
if external && htlcConfTarget != 0 {
|
|
|
|
return 0, errors.New("external and htlc conf target cannot " +
|
|
|
|
"both be set")
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the htlc is being externally published, we do not need to set a
|
|
|
|
// confirmation target.
|
|
|
|
if external {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return validateConfTarget(htlcConfTarget, loop.DefaultHtlcConfTarget)
|
|
|
|
}
|