Skip to content
Open
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
5 changes: 5 additions & 0 deletions docs/release-notes/release-notes-next.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@

#### Maintenance

* Reserved distinct static-address receive and change key families and
persisted deposit address ownership metadata in preparation for multi-address
Static Address Loop In support.
[PR #1210](https://github.com/lightninglabs/loop/pull/1210)

* Updated the gRPC dependency to v1.83.1.

* Updated the Taproot Assets dependency to v0.8.1; asset conversions that
Expand Down
101 changes: 101 additions & 0 deletions loopdb/migration_22_deposit_address_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package loopdb

import (
"context"
"database/sql"
"io/fs"
"net/http"
"path/filepath"
"testing"
"testing/fstest"

"github.com/golang-migrate/migrate/v4"
sqlite_migrate "github.com/golang-migrate/migrate/v4/database/sqlite"
"github.com/golang-migrate/migrate/v4/source/httpfs"
"github.com/stretchr/testify/require"
)

// TestDepositAddressBackfill verifies that migration 22 only assigns legacy
// deposits when their static-address owner is unambiguous.
func TestDepositAddressBackfill(t *testing.T) {
t.Parallel()

tests := []struct {
name string
addressCount int
wantOwner bool
}{
{name: "no address", addressCount: 0},
{name: "one address", addressCount: 1, wantOwner: true},
{name: "multiple addresses", addressCount: 2},
}

for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()

db, err := sql.Open(
"sqlite", filepath.Join(t.TempDir(), "loop.db"),
)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, db.Close())
})

ctx := context.Background()
_, err = db.ExecContext(ctx, `
CREATE TABLE static_addresses (id INTEGER PRIMARY KEY);
CREATE TABLE deposits (id INTEGER PRIMARY KEY);`)
require.NoError(t, err)

migrationSQL, err := fs.ReadFile(
sqlSchemas,
"sqlc/migrations/000022_deposit_static_address_id.up.sql",
)
require.NoError(t, err)
migrationFS := fstest.MapFS{
"migrations/000001_deposit_address.up.sql": {
Data: migrationSQL,
},
}

driver, err := sqlite_migrate.WithInstance(
db, &sqlite_migrate.Config{},
)
require.NoError(t, err)
source, err := httpfs.New(
http.FS(migrationFS), "migrations",
)
require.NoError(t, err)
migrator, err := migrate.NewWithInstance(
"migrations", source, "sqlc", driver,
)
require.NoError(t, err)

for i := 0; i < testCase.addressCount; i++ {
_, err := db.ExecContext(ctx,
"INSERT INTO static_addresses (id) VALUES (?)",
i+1,
)
require.NoError(t, err)
}

_, err = db.ExecContext(ctx,
"INSERT INTO deposits (id) VALUES (1)",
)
require.NoError(t, err)

require.NoError(t, migrator.Up())

var owner sql.NullInt64
err = db.QueryRowContext(ctx,
"SELECT static_address_id FROM deposits",
).Scan(&owner)
require.NoError(t, err)
require.Equal(t, testCase.wantOwner, owner.Valid)
if testCase.wantOwner {
require.EqualValues(t, 1, owner.Int64)
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE deposits DROP COLUMN static_address_id;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 F13 (Minor) — Down migration 22 cannot run on SQLite · loopdb/sqlc/migrations/000022_deposit_static_address_id.down.sql:1

ALTER TABLE deposits DROP COLUMN static_address_id fails on SQLite because SQLite refuses to drop a column that is used in a foreign key constraint, and the up migration adds this column with REFERENCES static_addresses(id). This is what distinguishes migration 22's rollback from earlier column-adding migrations in this directory, whose columns carry no FK.

The forward path is unaffected, so this only surfaces if something actually runs the down migration. I cannot see from the loaded context whether loopd or its test suite ever does — no migration-down harness is in file_contents — so severity is scaled to that uncertainty. Fixing it requires either dropping the REFERENCES clause from the up migration (SQLite does not enforce it unless foreign_keys is on, and the application already guards ownership in CreateDeposit) or rebuilding the table in the down migration.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ALTER TABLE deposits ADD static_address_id INT REFERENCES static_addresses(id);

UPDATE deposits
SET static_address_id = (
SELECT id FROM static_addresses ORDER BY id ASC LIMIT 1
Comment thread
hieblmi marked this conversation as resolved.
)
WHERE static_address_id IS NULL
AND (SELECT COUNT(*) FROM static_addresses) = 1;
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.

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

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

58 changes: 48 additions & 10 deletions loopdb/sqlc/queries/static_address_deposits.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ INSERT INTO deposits (
confirmation_height,
timeout_sweep_pk_script,
expiry_sweep_txid,
finalized_withdrawal_tx
finalized_withdrawal_tx,
static_address_id
) VALUES (
$1,
$2,
Expand All @@ -16,7 +17,8 @@ INSERT INTO deposits (
$5,
$6,
$7,
$8
$8,
$9
);

-- name: UpdateDeposit :exec
Expand All @@ -42,30 +44,66 @@ INSERT INTO deposit_updates (
);

-- name: GetDeposit :one
SELECT * FROM deposits WHERE deposit_id = $1;

-- name: GetDepositWithAddress :one
SELECT
*
d.*,
sa.client_pubkey client_pubkey,
sa.server_pubkey server_pubkey,
sa.expiry expiry,
sa.client_key_family client_key_family,
sa.client_key_index client_key_index,
sa.pkscript pkscript,
sa.protocol_version protocol_version,
sa.initiation_height initiation_height
FROM
deposits
deposits d
LEFT JOIN static_addresses sa ON sa.id = d.static_address_id
WHERE
deposit_id = $1;

-- name: DepositForOutpoint :one
SELECT * FROM deposits WHERE tx_hash = $1 AND out_index = $2;

-- name: DepositForOutpointWithAddress :one
SELECT
*
d.*,
sa.client_pubkey client_pubkey,
sa.server_pubkey server_pubkey,
sa.expiry expiry,
sa.client_key_family client_key_family,
sa.client_key_index client_key_index,
sa.pkscript pkscript,
sa.protocol_version protocol_version,
sa.initiation_height initiation_height
FROM
deposits
deposits d
LEFT JOIN static_addresses sa ON sa.id = d.static_address_id
WHERE
tx_hash = $1
AND
out_index = $2;

-- name: AllDeposits :many
SELECT * FROM deposits ORDER BY id ASC;

-- name: AllDepositsWithAddress :many
SELECT
*
d.*,
sa.client_pubkey client_pubkey,
sa.server_pubkey server_pubkey,
sa.expiry expiry,
sa.client_key_family client_key_family,
sa.client_key_index client_key_index,
sa.pkscript pkscript,
sa.protocol_version protocol_version,
sa.initiation_height initiation_height
FROM
deposits
deposits d
LEFT JOIN static_addresses sa ON sa.id = d.static_address_id
ORDER BY
id ASC;
d.id ASC;

-- name: GetLatestDepositUpdate :one
SELECT
Expand All @@ -76,4 +114,4 @@ WHERE
deposit_id = $1
ORDER BY
update_timestamp DESC
LIMIT 1;
LIMIT 1;
10 changes: 9 additions & 1 deletion loopdb/sqlc/queries/static_address_loopin.sql
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,19 @@ WHERE
-- name: DepositsForSwapHash :many
SELECT
d.*,
sa.client_pubkey client_pubkey,
sa.server_pubkey server_pubkey,
sa.expiry expiry,
sa.client_key_family client_key_family,
sa.client_key_index client_key_index,
sa.pkscript pkscript,
sa.protocol_version protocol_version,
sa.initiation_height initiation_height,
u.update_state,
u.update_timestamp
FROM
deposits d
LEFT JOIN static_addresses sa ON sa.id = d.static_address_id
LEFT JOIN
deposit_updates u ON u.id = (
SELECT id
Expand All @@ -162,4 +171,3 @@ FROM
WHERE
d.swap_hash = $1;


14 changes: 12 additions & 2 deletions loopdb/sqlc/queries/static_addresses.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
-- name: AllStaticAddresses :many
SELECT * FROM static_addresses;
SELECT * FROM static_addresses
ORDER BY id ASC;

-- name: GetStaticAddress :one
SELECT * FROM static_addresses
WHERE pkscript=$1;

-- name: GetStaticAddressID :one
SELECT id FROM static_addresses
WHERE pkscript=$1;

-- name: CreateStaticAddress :exec
INSERT INTO static_addresses (
client_pubkey,
Expand All @@ -24,4 +29,9 @@ INSERT INTO static_addresses (
$6,
$7,
$8
);
);

-- name: GetLegacyAddress :one
SELECT * FROM static_addresses
ORDER BY id ASC
LIMIT 1;
Loading
Loading