-
Notifications
You must be signed in to change notification settings - Fork 135
staticaddr: persist multi-address ownership foundation #1210
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
Open
hieblmi
wants to merge
6
commits into
lightninglabs:master
Choose a base branch
from
hieblmi:codex/multi-address-foundation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,307
−125
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3edbc43
swap: reserve multi-address key families
hieblmi 5bf93c3
loopdb: persist deposit address ownership
hieblmi 5076730
docs: add multi-address foundation release note
hieblmi 22c590a
staticaddr: harden deposit address ownership
hieblmi 2273c04
staticaddr: harden deposit ownership recovery
hieblmi dc460df
staticaddr: fix ownership review lint
hieblmi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| }) | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
loopdb/sqlc/migrations/000022_deposit_static_address_id.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ALTER TABLE deposits DROP COLUMN static_address_id; | ||
8 changes: 8 additions & 0 deletions
8
loopdb/sqlc/migrations/000022_deposit_static_address_id.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
hieblmi marked this conversation as resolved.
|
||
| ) | ||
| WHERE static_address_id IS NULL | ||
| AND (SELECT COUNT(*) FROM static_addresses) = 1; | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:1ALTER TABLE deposits DROP COLUMN static_address_idfails on SQLite because SQLite refuses to drop a column that is used in a foreign key constraint, and the up migration adds this column withREFERENCES 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 theREFERENCESclause from the up migration (SQLite does not enforce it unlessforeign_keysis on, and the application already guards ownership inCreateDeposit) or rebuilding the table in the down migration.