diff --git a/instantout/actions.go b/instantout/actions.go index 46e53af..d0c8f1b 100644 --- a/instantout/actions.go +++ b/instantout/actions.go @@ -21,7 +21,7 @@ import ( "github.com/lightningnetwork/lnd/lntypes" ) -var ( +const ( // Define route independent max routing fees. We have currently no way // to get a reliable estimate of the routing fees. Best we can do is // the minimum routing fees, which is not very indicative. @@ -46,6 +46,10 @@ var ( // defaultPollPaymentTime is the default time to poll the server for the // payment status. defaultPollPaymentTime = time.Second * 15 + + // htlcExpiryDelta is the delta in blocks we require between the htlc + // expiry and reservation expiry. + htlcExpiryDelta = int32(40) ) // InitInstantOutCtx contains the context for the InitInstantOutAction. @@ -96,6 +100,15 @@ func (f *FSM) InitInstantOutAction(eventCtx fsm.EventContext) fsm.EventType { reservationAmt += uint64(res.Value) reservationIds = append(reservationIds, resId[:]) reservations = append(reservations, res) + + // Check that the reservation expiry is larger than the cltv + // expiry of the swap, with an additional delta to allow for + // preimage reveal. + if int32(res.Expiry) < initCtx.cltvExpiry+htlcExpiryDelta { + return f.HandleError(fmt.Errorf("reservation %x has "+ + "expiry %v which is less than the swap expiry %v", + resId, res.Expiry, initCtx.cltvExpiry)) + } } // Create the preimage for the swap. diff --git a/instantout/manager.go b/instantout/manager.go index 1de7f5c..b3e74e6 100644 --- a/instantout/manager.go +++ b/instantout/manager.go @@ -8,6 +8,7 @@ import ( "time" "github.com/btcsuite/btcd/btcutil" + "github.com/lightninglabs/loop/fsm" "github.com/lightninglabs/loop/instantout/reservation" "github.com/lightninglabs/loop/swapserverrpc" "github.com/lightningnetwork/lnd/lntypes" @@ -169,15 +170,10 @@ func (m *Manager) NewInstantOut(ctx context.Context, // waiting for sweepless sweep to be confirmed. err = instantOut.DefaultObserver.WaitForState( ctx, defaultStateWaitTime, WaitForSweeplessSweepConfirmed, + fsm.WithAbortEarlyOnErrorOption(), ) if err != nil { - if instantOut.LastActionError != nil { - return instantOut, fmt.Errorf( - "error waiting for sweepless sweep "+ - "confirmed: %w", instantOut.LastActionError, - ) - } - return instantOut, nil + return nil, err } return instantOut, nil