diff --git a/client.go b/client.go index cf253452..bce79ce7 100644 --- a/client.go +++ b/client.go @@ -43,9 +43,13 @@ var ( ErrSwapAmountTooHigh = errors.New("swap amount too high") // ErrExpiryTooFar is returned when the server proposes an expiry that - // is too soon for us. + // is too far in the future. ErrExpiryTooFar = errors.New("swap expiry too far") + // ErrExpiryTooSoon is returned when the server proposes an expiry that + // is too soon. + ErrExpiryTooSoon = errors.New("swap expiry too soon") + // ErrInsufficientBalance indicates insufficient confirmed balance to // publish a swap. ErrInsufficientBalance = errors.New("insufficient confirmed balance") diff --git a/loopin.go b/loopin.go index c8f56435..bd694cd6 100644 --- a/loopin.go +++ b/loopin.go @@ -38,6 +38,10 @@ var ( // getting us to lock up our funds to an arbitrary point in the future. MaxLoopInAcceptDelta = int32(1500) + // MinLoopInExpiryDelta defines the minimum number of remaining blocks + // that we accept until htlc expiry path that opens up for us to sweep. + MinLoopInExpiryDelta = int32(100) + // MinLoopInPublishDelta defines the minimum number of remaining blocks // until on-chain htlc expiry required to proceed to publishing the htlc // tx. This value isn't critical, as we could even safely publish the @@ -248,7 +252,7 @@ func newLoopInSwap(globalCtx context.Context, cfg *swapConfig, // Validate if the response parameters are outside our allowed range // preventing us from continuing with a swap. - err = validateLoopInContract(currentHeight, swapResp) + err = ValidateLoopInContract(currentHeight, swapResp.expiry) if err != nil { return nil, err } @@ -429,14 +433,20 @@ func resumeLoopInSwap(_ context.Context, cfg *swapConfig, return swap, nil } -// validateLoopInContract validates the contract parameters against our request. -func validateLoopInContract(height int32, response *newLoopInResponse) error { +// ValidateLoopInContract validates the contract parameters against our +// configured maximum values. +func ValidateLoopInContract(height int32, htlcExpiry int32) error { // Verify that we are not forced to publish a htlc that locks up our // funds for too long in case the server doesn't follow through. - if response.expiry-height > MaxLoopInAcceptDelta { + if htlcExpiry-height > MaxLoopInAcceptDelta { return ErrExpiryTooFar } + // Ensure that the expiry height is in the future. + if htlcExpiry < height+MinLoopInExpiryDelta { + return ErrExpiryTooSoon + } + return nil }