Skip to content

Commit 8be67b3

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 543719a commit 8be67b3

File tree

2 files changed

+364
-29
lines changed

2 files changed

+364
-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.
@@ -412,44 +448,45 @@ func (s *SqlStore) DepositIDsForSwapHash(ctx context.Context,
412448

413449
// toStaticAddressLoopIn converts sql rows to an instant out struct.
414450
func toStaticAddressLoopIn(_ context.Context, network *chaincfg.Params,
415-
row sqlc.GetStaticAddressLoopInSwapRow,
451+
swap sqlc.GetStaticAddressLoopInSwapRow,
452+
deposits []sqlc.DepositsForSwapHashRow,
416453
updates []sqlc.StaticAddressSwapUpdate) (*StaticAddressLoopIn, error) {
417454

418-
swapHash, err := lntypes.MakeHash(row.SwapHash)
455+
swapHash, err := lntypes.MakeHash(swap.SwapHash)
419456
if err != nil {
420457
return nil, err
421458
}
422459

423-
swapPreImage, err := lntypes.MakePreimage(row.Preimage)
460+
swapPreImage, err := lntypes.MakePreimage(swap.Preimage)
424461
if err != nil {
425462
return nil, err
426463
}
427464

428-
clientKey, err := btcec.ParsePubKey(row.SenderScriptPubkey)
465+
clientKey, err := btcec.ParsePubKey(swap.SenderScriptPubkey)
429466
if err != nil {
430467
return nil, err
431468
}
432469

433-
serverKey, err := btcec.ParsePubKey(row.ReceiverScriptPubkey)
470+
serverKey, err := btcec.ParsePubKey(swap.ReceiverScriptPubkey)
434471
if err != nil {
435472
return nil, err
436473
}
437474

438475
var htlcTimeoutSweepTxHash *chainhash.Hash
439-
if row.HtlcTimeoutSweepTxID.Valid {
476+
if swap.HtlcTimeoutSweepTxID.Valid {
440477
htlcTimeoutSweepTxHash, err = chainhash.NewHashFromStr(
441-
row.HtlcTimeoutSweepTxID.String,
478+
swap.HtlcTimeoutSweepTxID.String,
442479
)
443480
if err != nil {
444481
return nil, err
445482
}
446483
}
447484

448485
depositOutpoints := strings.Split(
449-
row.DepositOutpoints, outpointSeparator,
486+
swap.DepositOutpoints, outpointSeparator,
450487
)
451488

452-
timeoutAddressString := row.HtlcTimeoutSweepAddress
489+
timeoutAddressString := swap.HtlcTimeoutSweepAddress
453490
var timeoutAddress btcutil.Address
454491
if timeoutAddressString != "" {
455492
timeoutAddress, err = btcutil.DecodeAddress(
@@ -460,33 +497,68 @@ func toStaticAddressLoopIn(_ context.Context, network *chaincfg.Params,
460497
}
461498
}
462499

500+
depositList := make([]*deposit.Deposit, 0, len(deposits))
501+
for _, d := range deposits {
502+
id := deposit.ID{}
503+
err = id.FromByteSlice(d.DepositID)
504+
if err != nil {
505+
return nil, err
506+
}
507+
508+
sqlcDeposit := sqlc.Deposit{
509+
DepositID: id[:],
510+
TxHash: d.TxHash,
511+
Amount: d.Amount,
512+
OutIndex: d.OutIndex,
513+
ConfirmationHeight: d.ConfirmationHeight,
514+
TimeoutSweepPkScript: d.TimeoutSweepPkScript,
515+
ExpirySweepTxid: d.ExpirySweepTxid,
516+
FinalizedWithdrawalTx: d.FinalizedWithdrawalTx,
517+
}
518+
519+
sqlcDepositUpdate := sqlc.DepositUpdate{
520+
DepositID: id[:],
521+
UpdateState: d.UpdateState.String,
522+
UpdateTimestamp: d.UpdateTimestamp.Time,
523+
}
524+
deposit, err := deposit.ToDeposit(
525+
sqlcDeposit, sqlcDepositUpdate,
526+
)
527+
if err != nil {
528+
return nil, err
529+
}
530+
531+
depositList = append(depositList, deposit)
532+
}
533+
463534
loopIn := &StaticAddressLoopIn{
464535
SwapHash: swapHash,
465536
SwapPreimage: swapPreImage,
466-
HtlcCltvExpiry: row.CltvExpiry,
467-
MaxSwapFee: btcutil.Amount(row.MaxSwapFee),
468-
InitiationHeight: uint32(row.InitiationHeight),
469-
InitiationTime: row.InitiationTime,
537+
HtlcCltvExpiry: swap.CltvExpiry,
538+
MaxSwapFee: btcutil.Amount(swap.MaxSwapFee),
539+
InitiationHeight: uint32(swap.InitiationHeight),
540+
InitiationTime: swap.InitiationTime,
470541
ProtocolVersion: version.AddressProtocolVersion(
471-
row.ProtocolVersion,
542+
swap.ProtocolVersion,
472543
),
473-
Label: row.Label,
544+
Label: swap.Label,
474545
ClientPubkey: clientKey,
475546
ServerPubkey: serverKey,
476547
HtlcKeyLocator: keychain.KeyLocator{
477-
Family: keychain.KeyFamily(row.ClientKeyFamily),
478-
Index: uint32(row.ClientKeyIndex),
548+
Family: keychain.KeyFamily(swap.ClientKeyFamily),
549+
Index: uint32(swap.ClientKeyIndex),
479550
},
480-
SwapInvoice: row.SwapInvoice,
481-
PaymentTimeoutSeconds: uint32(row.PaymentTimeoutSeconds),
482-
LastHop: row.LastHop,
483-
QuotedSwapFee: btcutil.Amount(row.QuotedSwapFeeSatoshis),
551+
SwapInvoice: swap.SwapInvoice,
552+
PaymentTimeoutSeconds: uint32(swap.PaymentTimeoutSeconds),
553+
LastHop: swap.LastHop,
554+
QuotedSwapFee: btcutil.Amount(swap.QuotedSwapFeeSatoshis),
484555
DepositOutpoints: depositOutpoints,
485556
HtlcTxFeeRate: chainfee.SatPerKWeight(
486-
row.HtlcTxFeeRateSatKw,
557+
swap.HtlcTxFeeRateSatKw,
487558
),
488559
HtlcTimeoutSweepAddress: timeoutAddress,
489560
HtlcTimeoutSweepTxHash: htlcTimeoutSweepTxHash,
561+
Deposits: depositList,
490562
}
491563

492564
if len(updates) > 0 {

0 commit comments

Comments
 (0)