Skip to content
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0
github.com/jackc/pgconn v1.14.3
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438
github.com/jackc/pgx/v5 v5.6.0
github.com/jessevdk/go-flags v1.4.0
github.com/lib/pq v1.10.9
github.com/lightninglabs/aperture v0.3.13-beta
Expand Down Expand Up @@ -104,7 +105,6 @@ require (
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jackc/pgx/v4 v4.18.2 // indirect
github.com/jackc/pgx/v5 v5.6.0 // indirect
github.com/jackc/puddle v1.3.0 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jackpal/gateway v1.0.5 // indirect
Expand Down
10 changes: 10 additions & 0 deletions loopd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,16 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
clock.NewDefaultClock(), d.lnd.ChainParams,
)

// Run the deposit swap hash migration.
err = loopin.MigrateDepositSwapHash(
d.mainCtx, swapDb, depositStore, staticAddressLoopInStore,
)
if err != nil {
errorf("Deposit swap hash migration failed: %v", err)

return err
}

staticLoopInManager = loopin.NewManager(&loopin.Config{
Server: staticAddressClient,
QuoteGetter: swapClient.Server,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ CREATE TABLE IF NOT EXISTS deposit_updates (

-- update_timestamp is the timestamp of the update.
update_timestamp TIMESTAMP NOT NULL
);
);
1 change: 1 addition & 0 deletions loopdb/sqlc/migrations/000017_deposit_swaphash.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE deposits DROP COLUMN swap_hash;
1 change: 1 addition & 0 deletions loopdb/sqlc/migrations/000017_deposit_swaphash.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE deposits ADD swap_hash BLOB;
1 change: 1 addition & 0 deletions loopdb/sqlc/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions loopdb/sqlc/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions loopdb/sqlc/queries/static_address_loopin.sql
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,51 @@ SELECT EXISTS (
FROM static_address_swaps
WHERE swap_hash = $1
);

-- name: MapDepositToSwap :exec
UPDATE
deposits
SET
swap_hash = $2
WHERE
deposit_id = $1;

-- name: SwapHashForDepositID :one
SELECT
swap_hash
FROM
deposits
WHERE
deposit_id = $1;

-- name: DepositIDsForSwapHash :many
SELECT
deposit_id
FROM
deposits
WHERE
swap_hash = $1;

-- name: DepositsForSwapHash :many
SELECT
d.*,
u.update_state,
u.update_timestamp
FROM
deposits d
LEFT JOIN
deposit_updates u ON u.id = (
SELECT id
FROM deposit_updates
WHERE deposit_id = d.deposit_id
ORDER BY update_timestamp DESC
LIMIT 1
)
WHERE
d.swap_hash = $1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to try to use LATERAL JOIN. It should work faster than an embedded query.

SELECT
    d.*,
    u.update_state,
    u.update_timestamp
FROM
    deposits d
    LEFT JOIN LATERAL (
      SELECT *
      FROM deposit_updates
      WHERE deposit_updates.deposit_id = deposits.deposit_id
      ORDER BY deposit_updates.timestamp DESC
      LIMIT 1
    ) u ON true
WHERE
    d.swap_hash = $1;

Please make sure to have this index:

CREATE INDEX ... ON deposit_updates(deposit_id, timestamp DESC);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LATERAL JOIN is not supported by sqlite.







Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too many empty lines in this file. I propose to have 1 empty line between the queries.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

9 changes: 6 additions & 3 deletions loopdb/sqlc/static_address_deposits.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

137 changes: 137 additions & 0 deletions loopdb/sqlc/static_address_loopin.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions staticaddr/deposit/sql_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (s *SqlStore) GetDeposit(ctx context.Context, id ID) (*Deposit, error) {
return err
}

deposit, err = s.toDeposit(row, latestUpdate)
deposit, err = ToDeposit(row, latestUpdate)
if err != nil {
return err
}
Expand Down Expand Up @@ -177,7 +177,7 @@ func (s *SqlStore) DepositForOutpoint(ctx context.Context,
return err
}

deposit, err = s.toDeposit(row, latestUpdate)
deposit, err = ToDeposit(row, latestUpdate)
if err != nil {
return err
}
Expand Down Expand Up @@ -212,7 +212,7 @@ func (s *SqlStore) AllDeposits(ctx context.Context) ([]*Deposit, error) {
return err
}

d, err := s.toDeposit(deposit, latestUpdate)
d, err := ToDeposit(deposit, latestUpdate)
if err != nil {
return err
}
Expand All @@ -229,9 +229,9 @@ func (s *SqlStore) AllDeposits(ctx context.Context) ([]*Deposit, error) {
return allDeposits, nil
}

// toDeposit converts an sql deposit to a deposit.
func (s *SqlStore) toDeposit(row sqlc.Deposit,
lastUpdate sqlc.DepositUpdate) (*Deposit, error) {
// ToDeposit converts an sql deposit to a deposit.
func ToDeposit(row sqlc.Deposit, lastUpdate sqlc.DepositUpdate) (*Deposit,
error) {

id := ID{}
err := id.FromByteSlice(row.DepositID)
Expand Down
Loading