-
Notifications
You must be signed in to change notification settings - Fork 123
sweepbatcher: allow swap_hash to be non-unique #906
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| -- We kept old table as sweeps_old. Use it. | ||
| ALTER TABLE sweeps RENAME TO sweeps_new; | ||
| ALTER TABLE sweeps_old RENAME TO sweeps; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| -- We want to make column swap_hash non-unique and to use the outpoint as a key. | ||
| -- We can't make a column non-unique or remove it in sqlite, so work around. | ||
| -- See https://stackoverflow.com/a/42013422 | ||
|
|
||
| -- We also made outpoint a single point replacing columns outpoint_txid and | ||
| -- outpoint_index. | ||
|
|
||
| -- sweeps stores the individual sweeps that are part of a batch. | ||
| CREATE TABLE sweeps2 ( | ||
| -- id is the autoincrementing primary key. | ||
| id INTEGER PRIMARY KEY, | ||
|
|
||
| -- swap_hash is the hash of the swap that is being swept. | ||
| swap_hash BLOB NOT NULL, | ||
|
|
||
| -- batch_id is the id of the batch this swap is part of. | ||
| batch_id INTEGER NOT NULL, | ||
|
|
||
| -- outpoint is the UTXO id of the output being swept ("txid:index"). | ||
| outpoint TEXT NOT NULL UNIQUE, | ||
|
|
||
| -- amt is the amount of the output being swept. | ||
| amt BIGINT NOT NULL, | ||
|
|
||
| -- completed indicates whether the sweep has been completed. | ||
| completed BOOLEAN NOT NULL DEFAULT FALSE, | ||
|
|
||
| -- Foreign key constraint to ensure that we reference an existing batch | ||
| -- id. | ||
| FOREIGN KEY (batch_id) REFERENCES sweep_batches(id), | ||
|
|
||
| -- Foreign key constraint to ensure that swap_hash references an | ||
| -- existing swap. | ||
| FOREIGN KEY (swap_hash) REFERENCES swaps(swap_hash) | ||
| ); | ||
|
|
||
| -- Copy all the data from sweeps to sweeps2. | ||
| -- Explanation: | ||
| -- - seq(i) goes from 1 to 32 | ||
| -- - substr(outpoint_txid, 32+1-i, 1) indexes BLOB bytes in reverse order | ||
| -- (SQLite uses 1-based indexing) | ||
| -- - hex(...) gives uppercase by default, so wrapped in lower(...) | ||
| -- - group_concat(..., '') combines all hex digits | ||
| -- - concatenated with ':' || CAST(outpoint_index AS TEXT) for full outpoint. | ||
| WITH RECURSIVE seq(i) AS ( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I understand this query now, but could you add a short explanation of what we do so it looks less intimidating?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added explanations of sqlite query to the migration file and adjustments for postgres to |
||
| SELECT 1 | ||
| UNION ALL | ||
| SELECT i + 1 FROM seq WHERE i < 32 | ||
| ) | ||
| INSERT INTO sweeps2 ( | ||
| id, swap_hash, batch_id, outpoint, amt, completed | ||
| ) | ||
| SELECT | ||
| id, | ||
| swap_hash, | ||
| batch_id, | ||
| ( | ||
| SELECT lower(group_concat(hex(substr(outpoint_txid,32+1-i,1)),'')) | ||
| FROM seq | ||
| ) || ':' || CAST(outpoint_index AS TEXT), | ||
| amt, | ||
| completed | ||
| FROM sweeps; | ||
|
|
||
| -- Rename tables. | ||
| ALTER TABLE sweeps RENAME TO sweeps_old; | ||
| ALTER TABLE sweeps2 RENAME TO sweeps; | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
don't we have to drop
sweeps_newto reverse the creation?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.
This is dangerous, since it would be irrevocable. If some sweeps were added after the migration, they would be lost. Ideally we should write full SQL query which would split
outpointback tooutpoint_txidandoutpoint_index. Then we don't needsweeps_oldand could just remove an unneeded table.