-
Notifications
You must be signed in to change notification settings - Fork 122
staticaddr: migrate deposit swap hashes #988
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
Conversation
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.
Summary of Changes
Hello @hieblmi, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request introduces a significant database schema change and a corresponding migration to improve how deposit IDs are associated with static address loop-in swaps. By creating a dedicated deposit_to_swap table, the system gains enhanced flexibility for querying and managing deposit-related data, ensuring that historical and future swap deposits are properly normalized and easily retrievable.
Highlights
- Database Schema Enhancement: A new
deposit_to_swaptable is introduced to explicitly mapdeposit_idtoswap_hash, enabling more versatile queries for deposit-related swap information. - Data Migration: A one-time migration (
MigrateDepositNormalization) is implemented and integrated into the daemon's startup. This migration populates the newdeposit_to_swaptable by extracting deposit outpoint information from existing static address loop-in swaps. - New Data Access Layer: New SQL queries and corresponding Go methods are added to the
loopdbandstaticaddr/loopin/sql_storeto facilitate the creation, retrieval, and batch processing of these new deposit-to-swap mappings. - Improved Data Normalization: The changes normalize the storage of deposit-to-swap relationships, moving away from potentially less structured storage within loop-in entries to a dedicated, queryable table.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
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.
Code Review
This pull request introduces a new database table deposit_to_swap to create a mapping between deposits and swaps, which will help in querying. A database migration is included to populate this new table for existing swaps. The changes include new SQL schema, queries, and Go code to interact with the new table, as well as the migration logic and tests.
My review has identified a critical issue in the sqlc-generated code that will likely cause runtime errors, along with several medium-severity issues related to maintainability, such as a function name typo, an incorrect comment, use of magic numbers, and inconsistent naming conventions in generated code. Addressing these points will improve the quality and robustness of the code.
8be67b3 to
deeed97
Compare
deeed97 to
f5377ca
Compare
| const ( | ||
| // depositNormalizationMigrationID is the identifier for the deposit | ||
| // normalization migration. | ||
| depositNormalizationMigrationID = "deposit_normalization" |
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.
rename "deposit normalization" to something like deposit swap hash migration...
The PR description seems outdated. It no longer introduces a new table, but a new column in an existing table. |
starius
left a comment
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.
LGTM! 🎾
Added few comments.
| swap_hash = $1; | ||
|
|
||
|
|
||
|
|
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.
Too many empty lines in this file. I propose to have 1 empty line between the queries.
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.
fixed
| // First we'll fetch all past and pending static loop in swaps from the | ||
| // database. | ||
| swaps, err := swapStore.GetStaticAddressLoopInSwapsByStates( | ||
| ctx, append(PendingStates, FinalStates...), |
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.
You can use AllStates.
Also I propose to redefine AllStates not to append to another public variable:
var AllStates = append(
append([]fsm.StateType{}, PendingStates...), FinalStates...,
)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.
fixed
| "not found", outpoint) | ||
| } | ||
|
|
||
| depositsToSwapHashes[deposit.ID] = swap.SwapHash |
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.
I propose to add a check that depositsToSwapHashes[deposit.ID] didn't exist already.
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.
added, and log a warning if its a duplicate, but not interrupt migration. Deposit IDs are unique so this should not happen.
if _, ok := depositsToSwapHashes[deposit.ID]; !ok {
depositsToSwapHashes[deposit.ID] = swap.SwapHash
} else {
log.Warnf("Duplicate deposit ID %s found for "+
"outpoint %s, skipping",
deposit.ID, outpoint)
}
staticaddr/loopin/sql_store.go
Outdated
| for _, id := range depositIDs { | ||
| swapHash, err := s.baseDB.SwapHashForDepositID(ctx, id[:]) | ||
| if err != nil { | ||
| if errors.Is(err, sql.ErrNoRows) { |
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.
|| errors.Is(err, pgx.ErrNoRows) {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.
added
| OnHtlcTimeoutSweepPublished = fsm.EventType("OnHtlcTimeoutSweepPublished") | ||
| OnHtlcTimeoutSwept = fsm.EventType("OnHtlcTimeoutSwept") | ||
| OnPaymentReceived = fsm.EventType("OnPaymentReceived") | ||
| OnPaymentDeadlineExceeded = fsm.EventType("OnPaymentDeadlineExceeded") |
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.
👍
| LIMIT 1 | ||
| ) | ||
| WHERE | ||
| d.swap_hash = $1; |
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.
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);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.
LATERAL JOIN is not supported by sqlite.
| func TestGetStaticAddressLoopInSwapsByStates(t *testing.T) { | ||
| // Set up test context objects. | ||
| ctxb := context.Background() | ||
| testDb := loopdb.NewTestDB(t) |
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.
👍
staticaddr/loopin/sql_store_test.go
Outdated
| require.Equal(t, deposit.LoopedIn, finalizedDeposits[0].GetState()) | ||
| } | ||
|
|
||
| // TestCreateLoopIn ... |
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.
Please complete the godoc.
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.
completed
f5377ca to
6e2fce5
Compare
6e2fce5 to
62e649b
Compare
since deposits have been normalized in the db in the context of a static address swap this commit now retrieves the deposit information as part of GetLoopInByHash and GetStaticAddressLoopInSwapsByStates.
62e649b to
fe7622d
Compare
sputn1ck
left a comment
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.
LGTM!
To allow for versatile queries when handling deposits related to swaps this PR adds a column
swap_hashto thedepositstable that maps deposit ids to their respective swap hashes.A migration maps the deposits that are stored in a loop-in entry to that new table.
Loop-In getter methods pull related deposit records and attach them to the swap object.