From f7e5747177e74fa289b406ede076c78f21a76e85 Mon Sep 17 00:00:00 2001 From: Jesse de Wit Date: Mon, 4 Sep 2023 13:56:48 +0200 Subject: [PATCH] lsps2: save token with generated promises --- lsps2/mocks.go | 4 ++++ lsps2/server.go | 9 +++++++++ lsps2/store.go | 6 ++++++ postgresql/lsps2_store.go | 21 +++++++++++++++++++++ 4 files changed, 40 insertions(+) diff --git a/lsps2/mocks.go b/lsps2/mocks.go index 1db2f91a..05c039d0 100644 --- a/lsps2/mocks.go +++ b/lsps2/mocks.go @@ -79,6 +79,10 @@ func (s *mockLsps2Store) SetCompleted(ctx context.Context, registrationId uint64 return nil } +func (s *mockLsps2Store) SavePromises(ctx context.Context, req *SavePromises) error { + return nil +} + type mockLightningClient struct { openResponses []*wire.OutPoint openRequests []*lightning.OpenChannelRequest diff --git a/lsps2/server.go b/lsps2/server.go index 3826d07b..2ff59de1 100644 --- a/lsps2/server.go +++ b/lsps2/server.go @@ -132,6 +132,15 @@ func (s *server) GetInfo( return nil, status.New(codes.InternalError, "internal error").Err() } + err = s.store.SavePromises(ctx, &SavePromises{ + Menu: m, + Token: *request.Token, + }) + if err != nil { + log.Printf("Lsps2Server.GetInfo: store.SavePromises(%+v, %s) err: %v", m, *request.Token, err) + return nil, status.New(codes.InternalError, "internal error").Err() + } + menu := []*OpeningFeeParams{} for _, p := range m { menu = append(menu, &OpeningFeeParams{ diff --git a/lsps2/store.go b/lsps2/store.go index 6f3c7b23..21ed9f28 100644 --- a/lsps2/store.go +++ b/lsps2/store.go @@ -12,6 +12,11 @@ import ( "github.com/btcsuite/btcd/wire" ) +type SavePromises struct { + Menu []*shared.OpeningFeeParams + Token string +} + type RegisterBuy struct { LspId string PeerId string @@ -59,6 +64,7 @@ var ErrScidExists = errors.New("scid exists") var ErrNotFound = errors.New("not found") type Lsps2Store interface { + SavePromises(ctx context.Context, req *SavePromises) error RegisterBuy(ctx context.Context, req *RegisterBuy) error GetBuyRegistration(ctx context.Context, scid lightning.ShortChannelID) (*BuyRegistration, error) SetChannelOpened(ctx context.Context, channelOpened *ChannelOpened) error diff --git a/postgresql/lsps2_store.go b/postgresql/lsps2_store.go index fed6de65..c123de47 100644 --- a/postgresql/lsps2_store.go +++ b/postgresql/lsps2_store.go @@ -216,3 +216,24 @@ func (s *Lsps2Store) SetCompleted(ctx context.Context, registrationId uint64) er return nil } + +func (s *Lsps2Store) SavePromises( + ctx context.Context, + req *lsps2.SavePromises, +) error { + if len(req.Menu) == 0 { + return nil + } + + rows := [][]interface{}{} + for _, p := range req.Menu { + rows = append(rows, []interface{}{p.Promise, req.Token}) + } + _, err := s.pool.CopyFrom( + ctx, + pgx.Identifier{"promises"}, + []string{"promise", "token"}, + pgx.CopyFromRows(rows), + ) + return err +}