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:
Erick Cestari committed 2025-06-20 09:23:02 -03:00
1 parent a27bd69b9c
commit c01a66783f
3 files changed
+18 -4

No files matched your search

+8 -3
View File
@@ -15,6 +15,7 @@ import (
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
sphinx "github.com/lightningnetwork/lightning-onion" sphinx "github.com/lightningnetwork/lightning-onion"
"github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/feature" "github.com/lightningnetwork/lnd/feature"
"github.com/lightningnetwork/lnd/fn/v2" "github.com/lightningnetwork/lnd/fn/v2"
"github.com/lightningnetwork/lnd/htlcswitch" "github.com/lightningnetwork/lnd/htlcswitch"
@@ -118,6 +119,10 @@ type RouterBackend struct {
// ShouldSetExpEndorsement returns a boolean indicating whether the // ShouldSetExpEndorsement returns a boolean indicating whether the
// experimental endorsement bit should be set. // experimental endorsement bit should be set.
ShouldSetExpEndorsement func() bool 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. // 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. // Next, we'll ensure that this payreq hasn't already expired.
err = ValidatePayReqExpiry(payReq) err = ValidatePayReqExpiry(r.Clock, payReq)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -1370,10 +1375,10 @@ func UnmarshalFeatures(
// ValidatePayReqExpiry checks if the passed payment request has expired. In // ValidatePayReqExpiry checks if the passed payment request has expired. In
// the case it has expired, an error will be returned. // 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() expiry := payReq.Expiry()
validUntil := payReq.Timestamp.Add(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) return fmt.Errorf("invoice expired. Valid until %v", validUntil)
} }
+6
View File
@@ -5,9 +5,11 @@ import (
"context" "context"
"encoding/hex" "encoding/hex"
"testing" "testing"
"time"
"github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg"
"github.com/lightningnetwork/lnd/lnmock"
"github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/record" "github.com/lightningnetwork/lnd/record"
@@ -509,6 +511,9 @@ func TestExtractIntentFromSendRequest(t *testing.T) {
target, err := route.NewVertexFromBytes(destNodeBytes) target, err := route.NewVertexFromBytes(destNodeBytes)
require.NoError(t, err) require.NoError(t, err)
mockClock := &lnmock.MockClock{}
mockClock.On("Now").Return(time.Date(2025, 3, 1, 13, 0, 0, 0, time.UTC))
testCases := []extractIntentTestCase{ testCases := []extractIntentTestCase{
{ {
name: "Time preference out of range", name: "Time preference out of range",
@@ -706,6 +711,7 @@ func TestExtractIntentFromSendRequest(t *testing.T) {
return false return false
}, },
ActiveNetParams: &chaincfg.RegressionNetParams, ActiveNetParams: &chaincfg.RegressionNetParams,
Clock: mockClock,
}, },
sendReq: &SendPaymentRequest{ sendReq: &SendPaymentRequest{
Amt: int64(paymentAmount), Amt: int64(paymentAmount),
+4 -1
View File
@@ -696,6 +696,7 @@ func (r *rpcServer) addDeps(s *server, macService *macaroons.Service,
routerBackend := &routerrpc.RouterBackend{ routerBackend := &routerrpc.RouterBackend{
SelfNode: selfNode.PubKeyBytes, SelfNode: selfNode.PubKeyBytes,
Clock: clock.NewDefaultClock(),
FetchChannelCapacity: func(chanID uint64) (btcutil.Amount, FetchChannelCapacity: func(chanID uint64) (btcutil.Amount,
error) { error) {
@@ -5609,7 +5610,9 @@ func (r *rpcServer) extractPaymentIntent(rpcPayReq *rpcPaymentRequest) (rpcPayme
} }
// Next, we'll ensure that this payreq hasn't already expired. // Next, we'll ensure that this payreq hasn't already expired.
err = routerrpc.ValidatePayReqExpiry(payReq) err = routerrpc.ValidatePayReqExpiry(
r.routerBackend.Clock, payReq,
)
if err != nil { if err != nil {
return payIntent, err return payIntent, err
} }