Skip to content

Commit 827b3f0

Browse files
committed
staticaddr: use generated change addresses
1 parent 97a0911 commit 827b3f0

17 files changed

Lines changed: 494 additions & 83 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE static_address_swaps DROP COLUMN change_static_address_id;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
ALTER TABLE static_address_swaps
2+
ADD change_static_address_id INT REFERENCES static_addresses(id);
3+
4+
-- Existing fractional swaps sent change back to the legacy static address.
5+
-- Backfill that relation so in-flight swaps remain recoverable after the
6+
-- client starts requiring explicit per-swap change metadata.
7+
UPDATE static_address_swaps
8+
SET change_static_address_id = (
9+
SELECT id FROM static_addresses ORDER BY id ASC LIMIT 1
10+
)
11+
WHERE selected_amount > 0
12+
AND change_static_address_id IS NULL
13+
AND EXISTS (SELECT 1 FROM static_addresses);

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/queries/static_address_loopin.sql

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ INSERT INTO static_address_swaps (
1010
htlc_tx_fee_rate_sat_kw,
1111
htlc_timeout_sweep_tx_id,
1212
htlc_timeout_sweep_address,
13-
fast
13+
fast,
14+
change_static_address_id
1415
) VALUES (
1516
$1,
1617
$2,
@@ -22,7 +23,8 @@ INSERT INTO static_address_swaps (
2223
$8,
2324
$9,
2425
$10,
25-
$11
26+
$11,
27+
$12
2628
);
2729

2830
-- name: UpdateStaticAddressLoopIn :exec
@@ -48,27 +50,49 @@ INSERT INTO static_address_swap_updates (
4850
SELECT
4951
swaps.*,
5052
static_address_swaps.*,
51-
htlc_keys.*
53+
htlc_keys.*,
54+
change_address.client_pubkey change_client_pubkey,
55+
change_address.server_pubkey change_server_pubkey,
56+
change_address.expiry change_expiry,
57+
change_address.client_key_family change_client_key_family,
58+
change_address.client_key_index change_client_key_index,
59+
change_address.pkscript change_pkscript,
60+
change_address.protocol_version change_protocol_version,
61+
change_address.initiation_height change_initiation_height
5262
FROM
5363
swaps
5464
JOIN
5565
static_address_swaps ON swaps.swap_hash = static_address_swaps.swap_hash
5666
JOIN
5767
htlc_keys ON swaps.swap_hash = htlc_keys.swap_hash
68+
LEFT JOIN
69+
static_addresses change_address
70+
ON static_address_swaps.change_static_address_id = change_address.id
5871
WHERE
5972
swaps.swap_hash = $1;
6073

6174
-- name: GetStaticAddressLoopInSwapsByStates :many
6275
SELECT
6376
swaps.*,
6477
static_address_swaps.*,
65-
htlc_keys.*
78+
htlc_keys.*,
79+
change_address.client_pubkey change_client_pubkey,
80+
change_address.server_pubkey change_server_pubkey,
81+
change_address.expiry change_expiry,
82+
change_address.client_key_family change_client_key_family,
83+
change_address.client_key_index change_client_key_index,
84+
change_address.pkscript change_pkscript,
85+
change_address.protocol_version change_protocol_version,
86+
change_address.initiation_height change_initiation_height
6687
FROM
6788
swaps
6889
JOIN
6990
static_address_swaps ON swaps.swap_hash = static_address_swaps.swap_hash
7091
JOIN
7192
htlc_keys ON swaps.swap_hash = htlc_keys.swap_hash
93+
LEFT JOIN
94+
static_addresses change_address
95+
ON static_address_swaps.change_static_address_id = change_address.id
7296
JOIN
7397
static_address_swap_updates u ON swaps.swap_hash = u.swap_hash
7498
-- This subquery ensures that we are checking only the latest update for
@@ -158,4 +182,3 @@ WHERE
158182

159183

160184

161-

loopdb/sqlc/static_address_loopin.sql.go

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

staticaddr/loopin/actions.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,29 @@ func (f *FSM) InitHtlcAction(ctx context.Context,
104104
}
105105
swapInvoiceAmt := swapAmount - f.loopIn.QuotedSwapFee
106106

107+
var changeOutput *swapserverrpc.StaticAddressChangeOutput
108+
if hasChange {
109+
changeAmount := f.loopIn.ExpectedChangeAmount()
110+
f.loopIn.ChangeAddressParams, err =
111+
f.cfg.AddressManager.NewChangeAddress(ctx)
112+
if err != nil {
113+
err = fmt.Errorf("unable to create static address "+
114+
"change output: %w", err)
115+
116+
return returnError(err)
117+
}
118+
119+
changeOutput, err = staticutil.ChangeOutput(
120+
f.loopIn.ChangeAddressParams, changeAmount,
121+
)
122+
if err != nil {
123+
err = fmt.Errorf("unable to prepare static address "+
124+
"change output: %w", err)
125+
126+
return returnError(err)
127+
}
128+
}
129+
107130
// Generate random preimage.
108131
var swapPreimage lntypes.Preimage
109132
if _, err = rand.Read(swapPreimage[:]); err != nil {
@@ -174,6 +197,7 @@ func (f *FSM) InitHtlcAction(ctx context.Context,
174197
PaymentTimeoutSeconds: f.loopIn.PaymentTimeoutSeconds,
175198
Fast: f.loopIn.Fast,
176199
DepositToClientPubkeys: depositClientPubkeys,
200+
ChangeOutput: changeOutput,
177201
}
178202
if f.loopIn.LastHop != nil {
179203
loopInReq.LastHop = f.loopIn.LastHop

0 commit comments

Comments
 (0)