Skip to content

Commit f5377ca

Browse files
committed
staticaddr: fetch deposits as part of swap retrieval
since deposits have been normalized in the db in the context of a static address swap this commit now retrieves the deposit information as part of GetLoopInByHash and GetStaticAddressLoopInSwapsByStates.
1 parent 64d16f6 commit f5377ca

File tree

2 files changed

+373
-29
lines changed

2 files changed

+373
-29
lines changed

staticaddr/loopin/sql_store.go

Lines changed: 101 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ type Querier interface {
8383
// hash.
8484
DepositIDsForSwapHash(ctx context.Context,
8585
swapHash []byte) ([][]byte, error)
86+
87+
// DepositsForSwapHash retrieves all deposits for a given swap hash.
88+
DepositsForSwapHash(ctx context.Context,
89+
swapHash []byte) ([]sqlc.DepositsForSwapHashRow, error)
8690
}
8791

8892
// BaseDB is the interface that contains all the queries generated by sqlc for
@@ -121,10 +125,15 @@ func (s *SqlStore) GetLoopInByHash(ctx context.Context,
121125

122126
var (
123127
err error
124-
row sqlc.GetStaticAddressLoopInSwapRow
128+
swap sqlc.GetStaticAddressLoopInSwapRow
125129
updates []sqlc.StaticAddressSwapUpdate
126130
)
127-
row, err = s.baseDB.GetStaticAddressLoopInSwap(ctx, swapHash[:])
131+
swap, err = s.baseDB.GetStaticAddressLoopInSwap(ctx, swapHash[:])
132+
if err != nil {
133+
return nil, err
134+
}
135+
136+
deposits, err := s.baseDB.DepositsForSwapHash(ctx, swapHash[:])
128137
if err != nil {
129138
return nil, err
130139
}
@@ -134,9 +143,7 @@ func (s *SqlStore) GetLoopInByHash(ctx context.Context,
134143
return nil, err
135144
}
136145

137-
return toStaticAddressLoopIn(
138-
ctx, s.network, row, updates,
139-
)
146+
return toStaticAddressLoopIn(ctx, s.network, swap, deposits, updates)
140147
}
141148

142149
// GetStaticAddressLoopInSwapsByStates returns all static address loop-ins from
@@ -164,6 +171,11 @@ func (s *SqlStore) GetStaticAddressLoopInSwapsByStates(ctx context.Context,
164171

165172
loopIns := make([]*StaticAddressLoopIn, 0, len(rows))
166173
for _, row := range rows {
174+
deposits, err := s.baseDB.DepositsForSwapHash(ctx, row.SwapHash)
175+
if err != nil {
176+
return nil, err
177+
}
178+
167179
updates, err = s.baseDB.GetLoopInSwapUpdates(
168180
ctx, row.SwapHash,
169181
)
@@ -173,7 +185,7 @@ func (s *SqlStore) GetStaticAddressLoopInSwapsByStates(ctx context.Context,
173185

174186
loopIn, err = toStaticAddressLoopIn(
175187
ctx, s.network, sqlc.GetStaticAddressLoopInSwapRow(row),
176-
updates,
188+
deposits, updates,
177189
)
178190
if err != nil {
179191
return nil, err
@@ -205,6 +217,14 @@ func toStrings(states []fsm.StateType) []string {
205217
func (s *SqlStore) CreateLoopIn(ctx context.Context,
206218
loopIn *StaticAddressLoopIn) error {
207219

220+
if loopIn == nil {
221+
return errors.New("loop-in cannot be nil")
222+
}
223+
224+
if len(loopIn.Deposits) == 0 {
225+
return errors.New("loop-in must have at least one deposit")
226+
}
227+
208228
swapArgs := sqlc.InsertSwapParams{
209229
SwapHash: loopIn.SwapHash[:],
210230
Preimage: loopIn.SwapPreimage[:],
@@ -273,8 +293,24 @@ func (s *SqlStore) CreateLoopIn(ctx context.Context,
273293
return err
274294
}
275295

296+
// Map each deposit to the swap hash in the
297+
// deposit_to_swap table. This allows us to track which
298+
// deposits are used for which swaps.
299+
for _, d := range loopIn.Deposits {
300+
err = q.MapDepositToSwap(
301+
ctx, sqlc.MapDepositToSwapParams{
302+
DepositID: d.ID[:],
303+
SwapHash: loopIn.SwapHash[:],
304+
},
305+
)
306+
if err != nil {
307+
return err
308+
}
309+
}
310+
276311
return q.InsertStaticAddressMetaUpdate(ctx, updateArgs)
277-
})
312+
},
313+
)
278314
}
279315

280316
// UpdateLoopIn updates the loop-in in the database.
@@ -404,44 +440,45 @@ func (s *SqlStore) DepositIDsForSwapHash(ctx context.Context,
404440

405441
// toStaticAddressLoopIn converts sql rows to an instant out struct.
406442
func toStaticAddressLoopIn(_ context.Context, network *chaincfg.Params,
407-
row sqlc.GetStaticAddressLoopInSwapRow,
443+
swap sqlc.GetStaticAddressLoopInSwapRow,
444+
deposits []sqlc.DepositsForSwapHashRow,
408445
updates []sqlc.StaticAddressSwapUpdate) (*StaticAddressLoopIn, error) {
409446

410-
swapHash, err := lntypes.MakeHash(row.SwapHash)
447+
swapHash, err := lntypes.MakeHash(swap.SwapHash)
411448
if err != nil {
412449
return nil, err
413450
}
414451

415-
swapPreImage, err := lntypes.MakePreimage(row.Preimage)
452+
swapPreImage, err := lntypes.MakePreimage(swap.Preimage)
416453
if err != nil {
417454
return nil, err
418455
}
419456

420-
clientKey, err := btcec.ParsePubKey(row.SenderScriptPubkey)
457+
clientKey, err := btcec.ParsePubKey(swap.SenderScriptPubkey)
421458
if err != nil {
422459
return nil, err
423460
}
424461

425-
serverKey, err := btcec.ParsePubKey(row.ReceiverScriptPubkey)
462+
serverKey, err := btcec.ParsePubKey(swap.ReceiverScriptPubkey)
426463
if err != nil {
427464
return nil, err
428465
}
429466

430467
var htlcTimeoutSweepTxHash *chainhash.Hash
431-
if row.HtlcTimeoutSweepTxID.Valid {
468+
if swap.HtlcTimeoutSweepTxID.Valid {
432469
htlcTimeoutSweepTxHash, err = chainhash.NewHashFromStr(
433-
row.HtlcTimeoutSweepTxID.String,
470+
swap.HtlcTimeoutSweepTxID.String,
434471
)
435472
if err != nil {
436473
return nil, err
437474
}
438475
}
439476

440477
depositOutpoints := strings.Split(
441-
row.DepositOutpoints, outpointSeparator,
478+
swap.DepositOutpoints, outpointSeparator,
442479
)
443480

444-
timeoutAddressString := row.HtlcTimeoutSweepAddress
481+
timeoutAddressString := swap.HtlcTimeoutSweepAddress
445482
var timeoutAddress btcutil.Address
446483
if timeoutAddressString != "" {
447484
timeoutAddress, err = btcutil.DecodeAddress(
@@ -452,33 +489,68 @@ func toStaticAddressLoopIn(_ context.Context, network *chaincfg.Params,
452489
}
453490
}
454491

492+
depositList := make([]*deposit.Deposit, 0, len(deposits))
493+
for _, d := range deposits {
494+
id := deposit.ID{}
495+
err = id.FromByteSlice(d.DepositID)
496+
if err != nil {
497+
return nil, err
498+
}
499+
500+
sqlcDeposit := sqlc.Deposit{
501+
DepositID: id[:],
502+
TxHash: d.TxHash,
503+
Amount: d.Amount,
504+
OutIndex: d.OutIndex,
505+
ConfirmationHeight: d.ConfirmationHeight,
506+
TimeoutSweepPkScript: d.TimeoutSweepPkScript,
507+
ExpirySweepTxid: d.ExpirySweepTxid,
508+
FinalizedWithdrawalTx: d.FinalizedWithdrawalTx,
509+
}
510+
511+
sqlcDepositUpdate := sqlc.DepositUpdate{
512+
DepositID: id[:],
513+
UpdateState: d.UpdateState.String,
514+
UpdateTimestamp: d.UpdateTimestamp.Time,
515+
}
516+
deposit, err := deposit.ToDeposit(
517+
sqlcDeposit, sqlcDepositUpdate,
518+
)
519+
if err != nil {
520+
return nil, err
521+
}
522+
523+
depositList = append(depositList, deposit)
524+
}
525+
455526
loopIn := &StaticAddressLoopIn{
456527
SwapHash: swapHash,
457528
SwapPreimage: swapPreImage,
458-
HtlcCltvExpiry: row.CltvExpiry,
459-
MaxSwapFee: btcutil.Amount(row.MaxSwapFee),
460-
InitiationHeight: uint32(row.InitiationHeight),
461-
InitiationTime: row.InitiationTime,
529+
HtlcCltvExpiry: swap.CltvExpiry,
530+
MaxSwapFee: btcutil.Amount(swap.MaxSwapFee),
531+
InitiationHeight: uint32(swap.InitiationHeight),
532+
InitiationTime: swap.InitiationTime,
462533
ProtocolVersion: version.AddressProtocolVersion(
463-
row.ProtocolVersion,
534+
swap.ProtocolVersion,
464535
),
465-
Label: row.Label,
536+
Label: swap.Label,
466537
ClientPubkey: clientKey,
467538
ServerPubkey: serverKey,
468539
HtlcKeyLocator: keychain.KeyLocator{
469-
Family: keychain.KeyFamily(row.ClientKeyFamily),
470-
Index: uint32(row.ClientKeyIndex),
540+
Family: keychain.KeyFamily(swap.ClientKeyFamily),
541+
Index: uint32(swap.ClientKeyIndex),
471542
},
472-
SwapInvoice: row.SwapInvoice,
473-
PaymentTimeoutSeconds: uint32(row.PaymentTimeoutSeconds),
474-
LastHop: row.LastHop,
475-
QuotedSwapFee: btcutil.Amount(row.QuotedSwapFeeSatoshis),
543+
SwapInvoice: swap.SwapInvoice,
544+
PaymentTimeoutSeconds: uint32(swap.PaymentTimeoutSeconds),
545+
LastHop: swap.LastHop,
546+
QuotedSwapFee: btcutil.Amount(swap.QuotedSwapFeeSatoshis),
476547
DepositOutpoints: depositOutpoints,
477548
HtlcTxFeeRate: chainfee.SatPerKWeight(
478-
row.HtlcTxFeeRateSatKw,
549+
swap.HtlcTxFeeRateSatKw,
479550
),
480551
HtlcTimeoutSweepAddress: timeoutAddress,
481552
HtlcTimeoutSweepTxHash: htlcTimeoutSweepTxHash,
553+
Deposits: depositList,
482554
}
483555

484556
if len(updates) > 0 {

0 commit comments

Comments
 (0)