Skip to content

Commit 25197e6

Browse files
committed
staticaddr: normalize swapped deposits
1 parent dd85a61 commit 25197e6

File tree

10 files changed

+507
-0
lines changed

10 files changed

+507
-0
lines changed

loopd/daemon.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,16 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
626626
clock.NewDefaultClock(), d.lnd.ChainParams,
627627
)
628628

629+
// Run the selected amount migration.
630+
err = loopin.MigrateDepositNormalization(
631+
d.mainCtx, swapDb, depositStore, staticAddressLoopInStore,
632+
)
633+
if err != nil {
634+
errorf("Deposit normalization migration failed: %v", err)
635+
636+
return err
637+
}
638+
629639
staticLoopInManager = loopin.NewManager(&loopin.Config{
630640
Server: staticAddressClient,
631641
QuoteGetter: swapClient.Server,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE IF EXISTS deposit_to_swap;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- deposit_to_swap table maps deposits to swaps.
2+
CREATE TABLE IF NOT EXISTS deposit_to_swap (
3+
-- deposit_id is a unique identifier from the deposits table.
4+
deposit_id BLOB NOT NULL UNIQUE REFERENCES deposits(deposit_id),
5+
6+
-- swap_hash is the identifier from static_address_swaps.
7+
swap_hash BLOB NOT NULL REFERENCES static_address_swaps(swap_hash)
8+
);

loopdb/sqlc/models.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

loopdb/sqlc/querier.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

loopdb/sqlc/queries/static_address_loopin.sql

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,31 @@ SELECT EXISTS (
9393
FROM static_address_swaps
9494
WHERE swap_hash = $1
9595
);
96+
97+
-- name: MapDepositToSwap :exec
98+
INSERT INTO deposit_to_swap (
99+
deposit_id,
100+
swap_hash
101+
) VALUES (
102+
$1,
103+
$2
104+
);
105+
106+
-- name: SwapHashForDepositID :one
107+
SELECT
108+
swap_hash
109+
FROM
110+
deposit_to_swap
111+
WHERE
112+
deposit_id = $1;
113+
114+
115+
-- name: DepositIDsForSwapHash :many
116+
SELECT
117+
deposit_id
118+
FROM
119+
deposit_to_swap
120+
WHERE
121+
swap_hash = $1;
122+
123+

loopdb/sqlc/static_address_loopin.sql.go

Lines changed: 68 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package loopin
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
"github.com/lightninglabs/loop/loopdb"
9+
"github.com/lightninglabs/loop/staticaddr/deposit"
10+
"github.com/lightningnetwork/lnd/lntypes"
11+
)
12+
13+
const (
14+
// depositNormalizationMigrationID is the identifier for the deposit
15+
// normalization migration.
16+
depositNormalizationMigrationID = "deposit_normalization"
17+
)
18+
19+
// MigrateDepositNormalization will retrieve the comma separated deposit list of
20+
// past and pending swaps and map them to the swap hash in table
21+
// deposit_to_swap.
22+
func MigrateDepositNormalization(ctx context.Context, db loopdb.SwapStore,
23+
depositStore *deposit.SqlStore, swapStore *SqlStore) error {
24+
25+
migrationDone, err := db.HasMigration(
26+
ctx, depositNormalizationMigrationID,
27+
)
28+
if err != nil {
29+
return fmt.Errorf("unable to check migration status: %w", err)
30+
}
31+
if migrationDone {
32+
log.Infof("Deposit normalization migration already done, " +
33+
"skipping")
34+
35+
return nil
36+
}
37+
38+
log.Infof("Starting deposit normalization migration")
39+
startTs := time.Now()
40+
defer func() {
41+
log.Infof("Finished deposit normalization migration in %v",
42+
time.Since(startTs))
43+
}()
44+
45+
// First we'll fetch all past and pending static loop in swaps from the
46+
// database.
47+
swaps, err := swapStore.GetStaticAddressLoopInSwapsByStates(
48+
ctx, append(PendingStates, FinalStates...),
49+
)
50+
if err != nil {
51+
return err
52+
}
53+
54+
// Now we'll map each deposit of a swap to its respective swap hash.
55+
depositsToSwapHashes := make(map[deposit.ID]lntypes.Hash)
56+
for _, swap := range swaps {
57+
for _, outpoint := range swap.DepositOutpoints {
58+
deposit, err := depositStore.DepositForOutpoint(
59+
ctx, outpoint,
60+
)
61+
if err != nil {
62+
return fmt.Errorf("unable to fetch deposit "+
63+
"for outpoint %s: %w", outpoint, err)
64+
}
65+
if deposit == nil {
66+
return fmt.Errorf("deposit for outpoint %s "+
67+
"not found", outpoint)
68+
}
69+
70+
depositsToSwapHashes[deposit.ID] = swap.SwapHash
71+
}
72+
}
73+
74+
log.Infof("Batch-mapping %d deposits to swap hashes",
75+
len(depositsToSwapHashes))
76+
77+
err = swapStore.BatchMapDepositsToSwapHashes(ctx, depositsToSwapHashes)
78+
if err != nil {
79+
return err
80+
}
81+
82+
// Finally mark the migration as done.
83+
return db.SetMigration(ctx, depositNormalizationMigrationID)
84+
}

0 commit comments

Comments
 (0)