ntfn: use l402 store for tokens

This commit changes the way we fetch the l402 tokens for the
notification stream. Instead of fetching them from the server and
paying an invoice, we now look into our local store for the token.
This means that a loop client will not autofetch a token.
This commit is contained in:
sputn1ck 2024-10-30 14:18:12 +01:00
parent 15ee9789d9
commit e9f1a1aef3
No known key found for this signature in database
GPG key ID: 671103D881A5F0E4
3 changed files with 15 additions and 8 deletions

View file

@ -504,8 +504,8 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
// Start the notification manager.
notificationCfg := &notifications.Config{
Client: loop_swaprpc.NewSwapServerClient(swapClient.Conn),
FetchL402: swapClient.Server.FetchL402,
Client: loop_swaprpc.NewSwapServerClient(swapClient.Conn),
CurrentToken: swapClient.L402Store.CurrentToken,
}
notificationManager := notifications.NewManager(notificationCfg)

View file

@ -5,6 +5,7 @@ import (
"sync"
"time"
"github.com/lightninglabs/aperture/l402"
"github.com/lightninglabs/loop/swapserverrpc"
"google.golang.org/grpc"
)
@ -37,8 +38,9 @@ type Config struct {
// Client is the client used to communicate with the swap server.
Client Client
// FetchL402 is the function used to fetch the l402 token.
FetchL402 func(context.Context) error
// CurrentToken returns the token that is currently contained in the
// store or an l402.ErrNoToken error if there is none.
CurrentToken func() (*l402.Token, error)
}
// Manager is a manager for notifications that the swap server sends to the
@ -113,9 +115,13 @@ func (m *Manager) Run(ctx context.Context) error {
// the FetchL402 method. As a client might not have outbound capacity
// yet, we'll retry until we get a valid response.
if !m.hasL402 {
err := m.cfg.FetchL402(ctx)
_, err := m.cfg.CurrentToken()
if err != nil {
log.Errorf("Error fetching L402: %v", err)
// We only log the error if it's not the case that we
// don't have a token yet to avoid spamming the logs.
if err != l402.ErrNoToken {
log.Errorf("Error getting L402 from store: %v", err)
}
continue
}
m.hasL402 = true

View file

@ -7,6 +7,7 @@ import (
"testing"
"time"
"github.com/lightninglabs/aperture/l402"
"github.com/lightninglabs/loop/swapserverrpc"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
@ -101,9 +102,9 @@ func TestManager_ReservationNotification(t *testing.T) {
// Create a Manager with the mock client
mgr := NewManager(&Config{
Client: mockClient,
FetchL402: func(ctx context.Context) error {
CurrentToken: func() (*l402.Token, error) {
// Simulate successful fetching of L402
return nil
return nil, nil
},
})