Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use uuid7 instead of bigserial #135

Merged
merged 3 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/breez/lspd
go 1.19

require (
github.com/GoWebProd/uuid7 v0.0.0-20230623091058-5f5954faed6a
github.com/aws/aws-sdk-go v1.34.0
github.com/breez/lntest v0.0.27
github.com/btcsuite/btcd v0.23.5-0.20230228185050-38331963bddd
Expand All @@ -16,7 +17,7 @@ require (
github.com/golang/protobuf v1.5.2
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/jackc/pgtype v1.8.1
github.com/jackc/pgx/v4 v4.13.0
github.com/jackc/pgx/v5 v5.4.3
github.com/lightningnetwork/lightning-onion v1.2.1-0.20221202012345-ca23184850a1
github.com/lightningnetwork/lnd v0.16.2-beta
github.com/lightningnetwork/lnd/tlv v1.1.0
Expand All @@ -28,6 +29,7 @@ require (
)

require (
github.com/GoWebProd/gip v0.0.0-20230623090727-b60d41d5d320 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/Yawning/aez v0.0.0-20211027044916-e49e68abd344 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect
Expand All @@ -37,6 +39,14 @@ require (
github.com/ethereum/go-ethereum v1.10.17 // indirect
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.10.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.1.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v4 v4.13.0 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/lightninglabs/neutrino/cache v1.1.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
Expand All @@ -51,7 +61,7 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.0.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.0.1 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/tools v0.6.0 // indirect
gotest.tools/v3 v3.4.0 // indirect
lukechampine.com/uint128 v1.2.0 // indirect
Expand Down Expand Up @@ -104,13 +114,6 @@ require (
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.5.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.10.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.1.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/puddle v1.1.3 // indirect
github.com/jessevdk/go-flags v1.4.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
Expand Down Expand Up @@ -175,10 +178,10 @@ require (
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/term v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
google.golang.org/genproto v0.0.0-20210617175327-b9e0b3197ced // indirect
google.golang.org/protobuf v1.27.1
Expand Down
22 changes: 4 additions & 18 deletions itest/lspd_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/decred/dcrd/dcrec/secp256k1/v4"
ecies "github.com/ecies/go/v2"
"github.com/golang/protobuf/proto"
"github.com/jackc/pgx/v4/pgxpool"
)

var (
Expand Down Expand Up @@ -188,14 +187,7 @@ func (l *lspBase) Initialize() error {
return err
}

pgxPool, err := pgxpool.Connect(l.harness.Ctx, l.postgresBackend.ConnectionString())
if err != nil {
lntest.PerformCleanup(cleanups)
return fmt.Errorf("failed to connect to postgres: %w", err)
}
defer pgxPool.Close()

_, err = pgxPool.Exec(
_, err = l.postgresBackend.Pool().Exec(
l.harness.Ctx,
`DELETE FROM new_channel_params`,
)
Expand All @@ -204,7 +196,7 @@ func (l *lspBase) Initialize() error {
return fmt.Errorf("failed to delete new_channel_params: %w", err)
}

_, err = pgxPool.Exec(
_, err = l.postgresBackend.Pool().Exec(
l.harness.Ctx,
`INSERT INTO new_channel_params (validity, params, token)
VALUES
Expand Down Expand Up @@ -295,13 +287,7 @@ type FeeParamSetting struct {
}

func SetFeeParams(l LspNode, settings []*FeeParamSetting) error {
pgxPool, err := pgxpool.Connect(l.Harness().Ctx, l.PostgresBackend().ConnectionString())
if err != nil {
return fmt.Errorf("failed to connect to postgres: %w", err)
}
defer pgxPool.Close()

_, err = pgxPool.Exec(l.Harness().Ctx, "DELETE FROM new_channel_params")
_, err := l.PostgresBackend().Pool().Exec(l.Harness().Ctx, "DELETE FROM new_channel_params")
if err != nil {
return fmt.Errorf("failed to delete new_channel_params: %w", err)
}
Expand All @@ -327,7 +313,7 @@ func SetFeeParams(l LspNode, settings []*FeeParamSetting) error {
first = false
}
query += `;`
_, err = pgxPool.Exec(l.Harness().Ctx, query)
_, err = l.PostgresBackend().Pool().Exec(l.Harness().Ctx, query)
if err != nil {
return fmt.Errorf("failed to insert new_channel_params: %w", err)
}
Expand Down
9 changes: 1 addition & 8 deletions itest/lsps2_buy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/breez/lntest"
"github.com/breez/lspd/lightning"
"github.com/breez/lspd/lsps0"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -89,16 +88,10 @@ func testLsps2Buy(p *testParams) {
err = json.Unmarshal(buyResp.Data, b)
lntest.CheckError(p.t, err)

pgxPool, err := pgxpool.Connect(p.h.Ctx, p.lsp.PostgresBackend().ConnectionString())
if err != nil {
p.h.T.Fatalf("Failed to connect to postgres backend: %v", err)
}
defer pgxPool.Close()

scid, err := lightning.NewShortChannelIDFromString(b.Result.Jit_channel_scid)
lntest.CheckError(p.t, err)

rows, err := pgxPool.Query(
rows, err := p.lsp.PostgresBackend().Pool().Query(
p.h.Ctx,
`SELECT token
FROM lsps2.buy_registrations
Expand Down
31 changes: 21 additions & 10 deletions itest/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/jackc/pgx/v5/pgxpool"
)

type PostgresContainer struct {
Expand All @@ -29,9 +29,14 @@ type PostgresContainer struct {
logfile string
isInitialized bool
isStarted bool
pool *pgxpool.Pool
mtx sync.Mutex
}

func (p *PostgresContainer) Pool() *pgxpool.Pool {
return p.pool
}

func NewPostgresContainer(logfile string) (*PostgresContainer, error) {
port, err := lntest.GetPort()
if err != nil {
Expand Down Expand Up @@ -91,9 +96,16 @@ HealthCheck:
return fmt.Errorf("container '%s' unhealthy", c.id)
case "healthy":
for {
pgxPool, err := pgxpool.Connect(ctx, c.ConnectionString())
if c.pool == nil {
c.pool, err = pgxpool.New(ctx, c.ConnectionString())
if err != nil {
<-time.After(50 * time.Millisecond)
continue
}
}

_, err = c.pool.Exec(ctx, "SELECT 1;")
if err == nil {
pgxPool.Close()
break HealthCheck
}

Expand Down Expand Up @@ -175,6 +187,11 @@ func (c *PostgresContainer) Stop(ctx context.Context) error {
return nil
}

if c.pool != nil {
c.pool.Close()
c.pool = nil
}

defer c.cli.Close()
err := c.cli.ContainerStop(ctx, c.id, nil)
c.isStarted = false
Expand Down Expand Up @@ -246,19 +263,13 @@ func (c *PostgresContainer) RunMigrations(ctx context.Context, migrationDir stri

sort.Strings(filenames)

pgxPool, err := pgxpool.Connect(ctx, c.ConnectionString())
if err != nil {
return fmt.Errorf("failed to connect to postgres: %w", err)
}
defer pgxPool.Close()

for _, filename := range filenames {
data, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("failed to read migration file '%s': %w", filename, err)
}

_, err = pgxPool.Exec(ctx, string(data))
_, err = c.pool.Exec(ctx, string(data))
if err != nil {
return fmt.Errorf("failed to execute migration file '%s': %w", filename, err)
}
Expand Down
9 changes: 1 addition & 8 deletions itest/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/breez/lntest"
lspd "github.com/breez/lspd/rpc"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/stretchr/testify/assert"
)

Expand All @@ -25,13 +24,7 @@ func registerPaymentWithTag(p *testParams) {
Tag: expected,
}, false)

pgxPool, err := pgxpool.Connect(p.h.Ctx, p.lsp.PostgresBackend().ConnectionString())
if err != nil {
p.h.T.Fatalf("Failed to connect to postgres backend: %v", err)
}
defer pgxPool.Close()

rows, err := pgxPool.Query(
rows, err := p.lsp.PostgresBackend().Pool().Query(
p.h.Ctx,
"SELECT tag FROM public.payments WHERE payment_hash=$1",
i.PaymentHash,
Expand Down
3 changes: 2 additions & 1 deletion lsps2/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"time"

"github.com/GoWebProd/uuid7"
"github.com/breez/lspd/chain"
"github.com/breez/lspd/common"
"github.com/breez/lspd/lightning"
Expand Down Expand Up @@ -83,7 +84,7 @@ func (s *mockLsps2Store) SetChannelOpened(ctx context.Context, channelOpened *Ch
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
}

Expand Down
7 changes: 4 additions & 3 deletions lsps2/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"time"

"github.com/GoWebProd/uuid7"
"github.com/breez/lspd/common"
"github.com/breez/lspd/lightning"
"github.com/breez/lspd/lsps0"
Expand All @@ -27,7 +28,7 @@ type RegisterBuy struct {
}

type BuyRegistration struct {
Id uint64
Id uuid7.UUID
LspId string
PeerId string // TODO: Make peerId in the registration a byte array.
Token string
Expand All @@ -54,7 +55,7 @@ func (b *BuyRegistration) IsExpired() bool {
}

type ChannelOpened struct {
RegistrationId uint64
RegistrationId uuid7.UUID
Outpoint *wire.OutPoint
FeeMsat uint64
PaymentSizeMsat uint64
Expand All @@ -68,6 +69,6 @@ type Lsps2Store interface {
RegisterBuy(ctx context.Context, req *RegisterBuy) error
GetBuyRegistration(ctx context.Context, scid lightning.ShortChannelID) (*BuyRegistration, 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
}
7 changes: 3 additions & 4 deletions postgresql/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"context"
"fmt"

"github.com/jackc/pgx/v4/pgxpool"
"github.com/jackc/pgx/v5/pgxpool"
)

func PgConnect(databaseUrl string) (*pgxpool.Pool, error) {
var err error
pgxPool, err := pgxpool.Connect(context.Background(), databaseUrl)
pgxPool, err := pgxpool.New(context.Background(), databaseUrl)
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
}
4 changes: 2 additions & 2 deletions postgresql/forwarding_event_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"log"

"github.com/breez/lspd/lnd"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)

type ForwardingEventStore struct {
Expand Down
4 changes: 2 additions & 2 deletions postgresql/intercept_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/breez/lspd/lightning"
"github.com/btcsuite/btcd/wire"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)

type PostgresInterceptStore struct {
Expand Down
Loading