Skip to content

Commit 0789d7a

Browse files
committed
staticaddr: migrate swap hashes to deposits
1 parent 35901d1 commit 0789d7a

11 files changed

+498
-3
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 deposit swap hash migration.
630+
err = loopin.MigrateDepositSwapHash(
631+
d.mainCtx, swapDb, depositStore, staticAddressLoopInStore,
632+
)
633+
if err != nil {
634+
errorf("Deposit swap hash 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+
ALTER TABLE deposits DROP COLUMN swap_hash;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE deposits ADD swap_hash BLOB;

loopdb/sqlc/models.go

Lines changed: 1 addition & 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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,30 @@ SELECT EXISTS (
9393
FROM static_address_swaps
9494
WHERE swap_hash = $1
9595
);
96+
97+
-- name: MapDepositToSwap :exec
98+
UPDATE
99+
deposits
100+
SET
101+
swap_hash = $2
102+
WHERE
103+
deposit_id = $1;
104+
105+
-- name: SwapHashForDepositID :one
106+
SELECT
107+
swap_hash
108+
FROM
109+
deposits
110+
WHERE
111+
deposit_id = $1;
112+
113+
-- name: DepositIDsForSwapHash :many
114+
SELECT
115+
deposit_id
116+
FROM
117+
deposits
118+
WHERE
119+
swap_hash = $1;
120+
121+
122+

loopdb/sqlc/static_address_deposits.sql.go

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

loopdb/sqlc/static_address_loopin.sql.go

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

0 commit comments

Comments
 (0)