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

commands: indicate in listcoins response whether coin is from self #1483

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

jp1ac4
Copy link
Collaborator

@jp1ac4 jp1ac4 commented Nov 22, 2024

⚠️ This PR upgrades and migrates DB version so a wallet opened against this PR will no longer work on Liana v8.

This is a first step towards #1375.

Following the approach from #1391, this PR adds a new is_from_self field to the response of the listcoins command.

The underlying information is stored in a new is_from_self column in the coins database table. This column could instead have been added to the transactions table, but for consistency with other transaction-related columns, I added it to the coins table. It's also important to note that being from self is wallet-dependent, so adding it to the transactions table would not work if multiple wallets were supported in the same DB (h/t @edouardparis).

A subsequent PR will then use this field in the GUI to determine which unconfirmed coins, if any, can be included in the confirmed balance. It also needs to be added to the corresponding Liana Connect API call. Another use of this field will be to determine which unconfirmed coins to include in coin selection (#1484).

The first commit in this PR refactors some existing DB migration tests so that they will not be affected by future changes to the coins table schema.

Some migration tests used structs and methods that are expected
to change and so will not be backward compatible.

This change replaces those with structs and methods specific to
the migration tests so that the tests will be unaffected by DB
schema changes. At the same time, these new structs and methods
simplify some of the setup by allowing to store new coins,
including their confirmation and spend status, in a single
DB operation.
@jp1ac4
Copy link
Collaborator Author

jp1ac4 commented Nov 25, 2024

Set PR to draft as I might include changes to the listcoins command here.

@jp1ac4
Copy link
Collaborator Author

jp1ac4 commented Nov 26, 2024

I added commits to include the is_from_self field in the listcoins response.

I also added a check constraint on the coins table so that a coin cannot be both immature and from self. This would also apply to coinbase deposits in general, but we don't currently flag those.

@jp1ac4 jp1ac4 changed the title database: store whether coin is from self commands: indicate in listcoins response whether coin is from self Nov 26, 2024
@jp1ac4 jp1ac4 marked this pull request as ready for review November 26, 2024 18:22
@jp1ac4 jp1ac4 marked this pull request as draft November 27, 2024 14:04
@jp1ac4
Copy link
Collaborator Author

jp1ac4 commented Nov 27, 2024

Following feedback from @edouardparis, I'll make the following changes:

  • Split migration into two separate migrations:
    • The first will modify the schema (in a single database transaction). It will use ALTER TABLE rather than recreating tables, which will require default column values to allow these new columns to be NOT NULL.
    • The second will populate values. For transactions, these updates will be split into separate database transactions in order to avoid a very large database transaction.
  • Take advantage of the migration to include columns num_outputs and is_coinbase in the transactions table (e.g. for Mark mature coinbase deposits as immature following tip rollback #1487).

I found that making changes to the transactions used in the test
can affect the order in which coins are returned, probably due
to the txid changing.
The new columns in the transactions table will be populated for
new transactions by the poller, while existing rows will be
updated in a subsequent migration.

New and existing coins will all have `is_from_self` set to false
due to the default column value.

None of these columns will be used by the wallet at this stage.
This populates the new columns from the previous migration
for existing rows and then maintains them in the poller moving
forward.
@jp1ac4
Copy link
Collaborator Author

jp1ac4 commented Nov 28, 2024

I've now made the changes as outlined above.

For extra safety, I added a max number of iterations based on the number of unconfirmed coins when updating is_from_self. There should never be more than this number of iterations.

@jp1ac4 jp1ac4 marked this pull request as ready for review November 28, 2024 18:29
Comment on lines +428 to +436
let mut i = 0;
loop {
i += 1;
if i > max_iterations {
panic!(
"More than {} iterations while updating is_from_self.",
max_iterations
);
}
Copy link
Member

Choose a reason for hiding this comment

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

I think you can directly do for i in 0..max_iterations with the updated == 0 as a break if it is finished early.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Review
Development

Successfully merging this pull request may close these issues.

2 participants