Skip to content

Commit 3255451

Browse files
committed
staticaddr/loopin: persist risk decisions
Store the server's static loop-in confirmation-risk decision and the time it was received. This lets recovered swaps reconstruct whether payment waiting had already started and how much of the payment timeout remains. Wire notification handling to persist accepted and rejected decisions before caching and forwarding them. If the swap row is not present yet, the notification is still cached so the per-swap waiter can replay and store the decision later. Recover accepted decisions by starting the payment deadline from the persisted decision time, and recover rejected decisions by canceling the invoice and failing the swap instead of waiting forever.
1 parent 7b29d92 commit 3255451

16 files changed

Lines changed: 934 additions & 103 deletions

loopd/daemon.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -555,10 +555,30 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
555555
}
556556
}
557557

558+
// Static address loop-in store setup is needed by the notification
559+
// manager so confirmation-risk decisions are durable before fan-out.
560+
staticAddressLoopInStore := loopin.NewSqlStore(
561+
loopdb.NewTypedStore[loopin.Querier](baseDb),
562+
clock.NewDefaultClock(), d.lnd.ChainParams,
563+
)
564+
558565
// Start the notification manager.
559566
notificationCfg := &notifications.Config{
560567
Client: loop_swaprpc.NewSwapServerClient(swapClient.Conn),
561568
CurrentToken: swapClient.L402Store.CurrentToken,
569+
PersistStaticLoopInRiskDecision: func(ctx context.Context,
570+
swapHash lntypes.Hash, accepted bool) error {
571+
572+
decision := loopin.ConfirmationRiskDecisionRejected
573+
if accepted {
574+
decision = loopin.ConfirmationRiskDecisionAccepted
575+
}
576+
577+
return staticAddressLoopInStore.
578+
RecordStaticAddressRiskDecision(
579+
ctx, swapHash, decision,
580+
)
581+
},
562582
}
563583
notificationManager := notifications.NewManager(notificationCfg)
564584

@@ -663,12 +683,6 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
663683
}
664684
openChannelManager = openchannel.NewManager(openChannelCfg)
665685

666-
// Static address loop-in manager setup.
667-
staticAddressLoopInStore := loopin.NewSqlStore(
668-
loopdb.NewTypedStore[loopin.Querier](baseDb),
669-
clock.NewDefaultClock(), d.lnd.ChainParams,
670-
)
671-
672686
// Run the deposit swap hash migration.
673687
err = loopin.MigrateDepositSwapHash(
674688
d.mainCtx, swapDb, depositStore, staticAddressLoopInStore,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- Drop confirmation-risk decision fields from static address loop-ins.
2+
ALTER TABLE static_address_swaps DROP COLUMN confirmation_risk_decision;
3+
ALTER TABLE static_address_swaps DROP COLUMN confirmation_risk_decision_time;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- confirmation_risk_decision records the server's confirmation-risk decision
2+
-- for a static address loop-in. The empty string means no decision has been
3+
-- received yet.
4+
ALTER TABLE static_address_swaps ADD COLUMN confirmation_risk_decision TEXT NOT NULL DEFAULT '';
5+
6+
-- confirmation_risk_decision_time records when loopd received and persisted
7+
-- the server's decision, so payment deadlines can be reconstructed after
8+
-- restart.
9+
ALTER TABLE static_address_swaps ADD COLUMN confirmation_risk_decision_time TIMESTAMP;

loopdb/sqlc/models.go

Lines changed: 14 additions & 12 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: 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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ SET
3333
WHERE
3434
swap_hash = $1;
3535

36+
-- name: RecordStaticAddressRiskDecision :exec
37+
UPDATE static_address_swaps
38+
SET
39+
confirmation_risk_decision = $2,
40+
confirmation_risk_decision_time = $3
41+
WHERE
42+
swap_hash = $1;
43+
3644
-- name: InsertStaticAddressMetaUpdate :exec
3745
INSERT INTO static_address_swap_updates (
3846
swap_hash,
@@ -150,4 +158,3 @@ WHERE
150158

151159

152160

153-

loopdb/sqlc/static_address_loopin.sql.go

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

0 commit comments

Comments
 (0)