use uuid7 instead of bigserial

This commit is contained in:
Jesse de Wit
2023-10-27 12:29:32 +02:00
parent 3ede7a1ec8
commit e693045594
6 changed files with 30 additions and 17 deletions

2
go.mod
View File

@@ -3,6 +3,7 @@ module github.com/breez/lspd
go 1.19 go 1.19
require ( require (
github.com/GoWebProd/uuid7 v0.0.0-20230623091058-5f5954faed6a
github.com/aws/aws-sdk-go v1.34.0 github.com/aws/aws-sdk-go v1.34.0
github.com/breez/lntest v0.0.28 github.com/breez/lntest v0.0.28
github.com/btcsuite/btcd v0.23.5-0.20230228185050-38331963bddd github.com/btcsuite/btcd v0.23.5-0.20230228185050-38331963bddd
@@ -28,6 +29,7 @@ require (
) )
require ( require (
github.com/GoWebProd/gip v0.0.0-20230623090727-b60d41d5d320 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/Yawning/aez v0.0.0-20211027044916-e49e68abd344 // indirect github.com/Yawning/aez v0.0.0-20211027044916-e49e68abd344 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect github.com/bahlo/generic-list-go v0.2.0 // indirect

View File

@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"time" "time"
"github.com/GoWebProd/uuid7"
"github.com/breez/lspd/chain" "github.com/breez/lspd/chain"
"github.com/breez/lspd/common" "github.com/breez/lspd/common"
"github.com/breez/lspd/lightning" "github.com/breez/lspd/lightning"
@@ -83,7 +84,7 @@ func (s *mockLsps2Store) SetChannelOpened(ctx context.Context, channelOpened *Ch
return s.err return s.err
} }
func (s *mockLsps2Store) SetCompleted(ctx context.Context, registrationId uint64) error { func (s *mockLsps2Store) SetCompleted(ctx context.Context, registrationId uuid7.UUID) error {
return nil return nil
} }

View File

@@ -6,6 +6,7 @@ import (
"log" "log"
"time" "time"
"github.com/GoWebProd/uuid7"
"github.com/breez/lspd/common" "github.com/breez/lspd/common"
"github.com/breez/lspd/lightning" "github.com/breez/lspd/lightning"
"github.com/breez/lspd/lsps0" "github.com/breez/lspd/lsps0"
@@ -27,7 +28,7 @@ type RegisterBuy struct {
} }
type BuyRegistration struct { type BuyRegistration struct {
Id uint64 Id uuid7.UUID
LspId string LspId string
PeerId string // TODO: Make peerId in the registration a byte array. PeerId string // TODO: Make peerId in the registration a byte array.
Token string Token string
@@ -54,7 +55,7 @@ func (b *BuyRegistration) IsExpired() bool {
} }
type ChannelOpened struct { type ChannelOpened struct {
RegistrationId uint64 RegistrationId uuid7.UUID
Outpoint *wire.OutPoint Outpoint *wire.OutPoint
FeeMsat uint64 FeeMsat uint64
PaymentSizeMsat uint64 PaymentSizeMsat uint64
@@ -68,6 +69,6 @@ type Lsps2Store interface {
RegisterBuy(ctx context.Context, req *RegisterBuy) error RegisterBuy(ctx context.Context, req *RegisterBuy) error
GetBuyRegistration(ctx context.Context, scid lightning.ShortChannelID) (*BuyRegistration, error) GetBuyRegistration(ctx context.Context, scid lightning.ShortChannelID) (*BuyRegistration, error)
SetChannelOpened(ctx context.Context, channelOpened *ChannelOpened) error SetChannelOpened(ctx context.Context, channelOpened *ChannelOpened) error
SetCompleted(ctx context.Context, registrationId uint64) error SetCompleted(ctx context.Context, registrationId uuid7.UUID) error
RemoveUnusedExpired(ctx context.Context, before time.Time) error RemoveUnusedExpired(ctx context.Context, before time.Time) error
} }

View File

@@ -8,10 +8,9 @@ import (
) )
func PgConnect(databaseUrl string) (*pgxpool.Pool, error) { func PgConnect(databaseUrl string) (*pgxpool.Pool, error) {
var err error
pgxPool, err := pgxpool.New(context.Background(), databaseUrl) pgxPool, err := pgxpool.New(context.Background(), databaseUrl)
if err != nil { if err != nil {
return nil, fmt.Errorf("pgxpool.Connect(%v): %w", databaseUrl, err) return nil, fmt.Errorf("pgxpool.New(%v): %w", databaseUrl, err)
} }
return pgxPool, nil return pgxPool, nil
} }

View File

@@ -7,6 +7,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/GoWebProd/uuid7"
"github.com/breez/lspd/common" "github.com/breez/lspd/common"
"github.com/breez/lspd/lightning" "github.com/breez/lspd/lightning"
"github.com/breez/lspd/lsps0" "github.com/breez/lspd/lsps0"
@@ -18,10 +19,12 @@ import (
type Lsps2Store struct { type Lsps2Store struct {
pool *pgxpool.Pool pool *pgxpool.Pool
generator *uuid7.Generator
} }
func NewLsps2Store(pool *pgxpool.Pool) *Lsps2Store { func NewLsps2Store(pool *pgxpool.Pool) *Lsps2Store {
return &Lsps2Store{pool: pool} generator := uuid7.New()
return &Lsps2Store{pool: pool, generator: generator}
} }
func (s *Lsps2Store) RegisterBuy( func (s *Lsps2Store) RegisterBuy(
@@ -41,10 +44,12 @@ func (s *Lsps2Store) RegisterBuy(
return fmt.Errorf("promise does not have matching token") return fmt.Errorf("promise does not have matching token")
} }
var uuid [16]byte = s.generator.Next()
_, err = s.pool.Exec( _, err = s.pool.Exec(
ctx, ctx,
`INSERT INTO lsps2.buy_registrations ( `INSERT INTO lsps2.buy_registrations (
lsp_id id
, lsp_id
, peer_id , peer_id
, scid , scid
, mode , mode
@@ -57,7 +62,8 @@ func (s *Lsps2Store) RegisterBuy(
, params_promise , params_promise
, token , token
) )
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)`, VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)`,
uuid,
req.LspId, req.LspId,
req.PeerId, req.PeerId,
int64(uint64(req.Scid)), int64(uint64(req.Scid)),
@@ -106,7 +112,7 @@ func (s *Lsps2Store) GetBuyRegistration(ctx context.Context, scid lightning.Shor
WHERE r.scid = $1`, WHERE r.scid = $1`,
int64(uint64(scid)), int64(uint64(scid)),
) )
var db_id uint64 var db_id [16]byte
var db_lsp_id string var db_lsp_id string
var db_peer_id string var db_peer_id string
var db_scid int64 var db_scid int64
@@ -187,17 +193,21 @@ func (s *Lsps2Store) GetBuyRegistration(ctx context.Context, scid lightning.Shor
} }
func (s *Lsps2Store) SetChannelOpened(ctx context.Context, channelOpened *lsps2.ChannelOpened) error { func (s *Lsps2Store) SetChannelOpened(ctx context.Context, channelOpened *lsps2.ChannelOpened) error {
var uuid [16]byte = s.generator.Next()
var registrationId [16]byte = channelOpened.RegistrationId
_, err := s.pool.Exec( _, err := s.pool.Exec(
ctx, ctx,
`INSERT INTO lsps2.bought_channels ( `INSERT INTO lsps2.bought_channels (
id,
registration_id, registration_id,
funding_tx_id, funding_tx_id,
funding_tx_outnum, funding_tx_outnum,
fee_msat, fee_msat,
payment_size_msat, payment_size_msat,
is_completed is_completed
) VALUES ($1, $2, $3, $4, $5, false)`, ) VALUES ($1, $2, $3, $4, $5, $6, false)`,
channelOpened.RegistrationId, uuid,
registrationId,
channelOpened.Outpoint.Hash[:], channelOpened.Outpoint.Hash[:],
channelOpened.Outpoint.Index, channelOpened.Outpoint.Index,
int64(channelOpened.FeeMsat), int64(channelOpened.FeeMsat),
@@ -207,7 +217,7 @@ func (s *Lsps2Store) SetChannelOpened(ctx context.Context, channelOpened *lsps2.
return err return err
} }
func (s *Lsps2Store) SetCompleted(ctx context.Context, registrationId uint64) error { func (s *Lsps2Store) SetCompleted(ctx context.Context, registrationId uuid7.UUID) error {
rows, err := s.pool.Exec( rows, err := s.pool.Exec(
ctx, ctx,
`UPDATE lsps2.bought_channels `UPDATE lsps2.bought_channels

View File

@@ -1,6 +1,6 @@
CREATE SCHEMA lsps2; CREATE SCHEMA lsps2;
CREATE TABLE lsps2.buy_registrations ( CREATE TABLE lsps2.buy_registrations (
id bigserial PRIMARY KEY, id uuid PRIMARY KEY,
lsp_id varchar NOT NULL, lsp_id varchar NOT NULL,
peer_id varchar NOT NULL, peer_id varchar NOT NULL,
scid bigint NOT NULL, scid bigint NOT NULL,
@@ -18,8 +18,8 @@ CREATE UNIQUE INDEX idx_lsps2_buy_registrations_scid ON lsps2.buy_registrations
CREATE INDEX idx_lsps2_buy_registrations_valid_until ON lsps2.buy_registrations (params_valid_until); CREATE INDEX idx_lsps2_buy_registrations_valid_until ON lsps2.buy_registrations (params_valid_until);
CREATE TABLE lsps2.bought_channels ( CREATE TABLE lsps2.bought_channels (
id bigserial PRIMARY KEY, id uuid PRIMARY KEY,
registration_id bigint NOT NULL, registration_id uuid NOT NULL,
funding_tx_id bytea NOT NULL, funding_tx_id bytea NOT NULL,
funding_tx_outnum bigint NOT NULL, funding_tx_outnum bigint NOT NULL,
fee_msat bigint NOT NULL, fee_msat bigint NOT NULL,