From bd3882d5b0aea7a76f24602e0d97ce23b51bd670 Mon Sep 17 00:00:00 2001 From: Slyghtning Date: Thu, 2 Jul 2026 10:47:06 +0200 Subject: [PATCH] staticaddr/loopin: use payment timeout duration helper --- staticaddr/loopin/loopin.go | 4 ++-- staticaddr/loopin/loopin_test.go | 36 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/staticaddr/loopin/loopin.go b/staticaddr/loopin/loopin.go index 6c1860bd..9b0fc03b 100644 --- a/staticaddr/loopin/loopin.go +++ b/staticaddr/loopin/loopin.go @@ -470,9 +470,9 @@ func (l *StaticAddressLoopIn) TotalDepositAmount() btcutil.Amount { // initiation time of the swap. If more than the swap's configured payment // timeout has passed, the remaining time will be negative. func (l *StaticAddressLoopIn) RemainingPaymentTimeSeconds() int64 { - elapsedSinceInitiation := time.Since(l.InitiationTime).Seconds() + deadline := l.InitiationTime.Add(l.PaymentTimeoutDuration()) - return l.paymentTimeoutSeconds() - int64(elapsedSinceInitiation) + return int64(time.Until(deadline).Seconds()) } // PaymentTimeoutDuration returns the configured payment timeout duration, diff --git a/staticaddr/loopin/loopin_test.go b/staticaddr/loopin/loopin_test.go index d4be020f..8b0892e6 100644 --- a/staticaddr/loopin/loopin_test.go +++ b/staticaddr/loopin/loopin_test.go @@ -147,6 +147,42 @@ func TestCreateHtlcSweepTxSweepValue(t *testing.T) { "the HTLC output") } +// TestPaymentTimeoutDuration verifies that zero timeout values fall back to the +// default payment timeout duration. +func TestPaymentTimeoutDuration(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + paymentTimeoutSeconds uint32 + expected time.Duration + }{ + { + name: "default", + expected: time.Duration(DefaultPaymentTimeoutSeconds) * time.Second, + }, + { + name: "configured", + paymentTimeoutSeconds: 42, + expected: 42 * time.Second, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + loopIn := &StaticAddressLoopIn{ + PaymentTimeoutSeconds: test.paymentTimeoutSeconds, + } + + require.Equal( + t, test.expected, loopIn.PaymentTimeoutDuration(), + ) + }) + } +} + // newStaticAddress creates a StaticAddress for testing. func newStaticAddress(clientKey, serverKey *btcec.PublicKey, csvExpiry int64) (*script.StaticAddress, error) {