mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-11-10 06:07:16 +01:00
routerrpc: inject clock for testable expiry validation
Refactors payment request expiry validation to use an injected clock dependency instead of calling time.Now() directly.
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
sphinx "github.com/lightningnetwork/lightning-onion"
|
||||
"github.com/lightningnetwork/lnd/channeldb"
|
||||
"github.com/lightningnetwork/lnd/clock"
|
||||
"github.com/lightningnetwork/lnd/feature"
|
||||
"github.com/lightningnetwork/lnd/fn/v2"
|
||||
"github.com/lightningnetwork/lnd/htlcswitch"
|
||||
@@ -118,6 +119,10 @@ type RouterBackend struct {
|
||||
// ShouldSetExpEndorsement returns a boolean indicating whether the
|
||||
// experimental endorsement bit should be set.
|
||||
ShouldSetExpEndorsement func() bool
|
||||
|
||||
// Clock is the clock used to validate payment requests expiry.
|
||||
// It is useful for testing.
|
||||
Clock clock.Clock
|
||||
}
|
||||
|
||||
// MissionControl defines the mission control dependencies of routerrpc.
|
||||
@@ -980,7 +985,7 @@ func (r *RouterBackend) extractIntentFromSendRequest(
|
||||
}
|
||||
|
||||
// Next, we'll ensure that this payreq hasn't already expired.
|
||||
err = ValidatePayReqExpiry(payReq)
|
||||
err = ValidatePayReqExpiry(r.Clock, payReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -1370,10 +1375,10 @@ func UnmarshalFeatures(
|
||||
|
||||
// ValidatePayReqExpiry checks if the passed payment request has expired. In
|
||||
// the case it has expired, an error will be returned.
|
||||
func ValidatePayReqExpiry(payReq *zpay32.Invoice) error {
|
||||
func ValidatePayReqExpiry(clock clock.Clock, payReq *zpay32.Invoice) error {
|
||||
expiry := payReq.Expiry()
|
||||
validUntil := payReq.Timestamp.Add(expiry)
|
||||
if time.Now().After(validUntil) {
|
||||
if clock.Now().After(validUntil) {
|
||||
return fmt.Errorf("invoice expired. Valid until %v", validUntil)
|
||||
}
|
||||
|
||||
|
||||
@@ -5,9 +5,11 @@ import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcd/btcutil"
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
"github.com/lightningnetwork/lnd/lnmock"
|
||||
"github.com/lightningnetwork/lnd/lnrpc"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
"github.com/lightningnetwork/lnd/record"
|
||||
@@ -509,6 +511,9 @@ func TestExtractIntentFromSendRequest(t *testing.T) {
|
||||
target, err := route.NewVertexFromBytes(destNodeBytes)
|
||||
require.NoError(t, err)
|
||||
|
||||
mockClock := &lnmock.MockClock{}
|
||||
mockClock.On("Now").Return(time.Date(2025, 3, 1, 13, 0, 0, 0, time.UTC))
|
||||
|
||||
testCases := []extractIntentTestCase{
|
||||
{
|
||||
name: "Time preference out of range",
|
||||
@@ -706,6 +711,7 @@ func TestExtractIntentFromSendRequest(t *testing.T) {
|
||||
return false
|
||||
},
|
||||
ActiveNetParams: &chaincfg.RegressionNetParams,
|
||||
Clock: mockClock,
|
||||
},
|
||||
sendReq: &SendPaymentRequest{
|
||||
Amt: int64(paymentAmount),
|
||||
|
||||
Reference in New Issue
Block a user