Skip to content

Commit deeed97

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 4d357d6 commit deeed97

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.
@@ -400,44 +436,45 @@ func (s *SqlStore) DepositIDsForSwapHash(ctx context.Context,
400436

401437
// toStaticAddressLoopIn converts sql rows to an instant out struct.
402438
func toStaticAddressLoopIn(_ context.Context, network *chaincfg.Params,
403-
row sqlc.GetStaticAddressLoopInSwapRow,
439+
swap sqlc.GetStaticAddressLoopInSwapRow,
440+
deposits []sqlc.DepositsForSwapHashRow,
404441
updates []sqlc.StaticAddressSwapUpdate) (*StaticAddressLoopIn, error) {
405442

406-
swapHash, err := lntypes.MakeHash(row.SwapHash)
443+
swapHash, err := lntypes.MakeHash(swap.SwapHash)
407444
if err != nil {
408445
return nil, err
409446
}
410447

411-
swapPreImage, err := lntypes.MakePreimage(row.Preimage)
448+
swapPreImage, err := lntypes.MakePreimage(swap.Preimage)
412449
if err != nil {
413450
return nil, err
414451
}
415452

416-
clientKey, err := btcec.ParsePubKey(row.SenderScriptPubkey)
453+
clientKey, err := btcec.ParsePubKey(swap.SenderScriptPubkey)
417454
if err != nil {
418455
return nil, err
419456
}
420457

421-
serverKey, err := btcec.ParsePubKey(row.ReceiverScriptPubkey)
458+
serverKey, err := btcec.ParsePubKey(swap.ReceiverScriptPubkey)
422459
if err != nil {
423460
return nil, err
424461
}
425462

426463
var htlcTimeoutSweepTxHash *chainhash.Hash
427-
if row.HtlcTimeoutSweepTxID.Valid {
464+
if swap.HtlcTimeoutSweepTxID.Valid {
428465
htlcTimeoutSweepTxHash, err = chainhash.NewHashFromStr(
429-
row.HtlcTimeoutSweepTxID.String,
466+
swap.HtlcTimeoutSweepTxID.String,
430467
)
431468
if err != nil {
432469
return nil, err
433470
}
434471
}
435472

436473
depositOutpoints := strings.Split(
437-
row.DepositOutpoints, outpointSeparator,
474+
swap.DepositOutpoints, outpointSeparator,
438475
)
439476

440-
timeoutAddressString := row.HtlcTimeoutSweepAddress
477+
timeoutAddressString := swap.HtlcTimeoutSweepAddress
441478
var timeoutAddress btcutil.Address
442479
if timeoutAddressString != "" {
443480
timeoutAddress, err = btcutil.DecodeAddress(
@@ -448,33 +485,68 @@ func toStaticAddressLoopIn(_ context.Context, network *chaincfg.Params,
448485
}
449486
}
450487

488+
depositList := make([]*deposit.Deposit, 0, len(deposits))
489+
for _, d := range deposits {
490+
id := deposit.ID{}
491+
err = id.FromByteSlice(d.DepositID)
492+
if err != nil {
493+
return nil, err
494+
}
495+
496+
sqlcDeposit := sqlc.Deposit{
497+
DepositID: id[:],
498+
TxHash: d.TxHash,
499+
Amount: d.Amount,
500+
OutIndex: d.OutIndex,
501+
ConfirmationHeight: d.ConfirmationHeight,
502+
TimeoutSweepPkScript: d.TimeoutSweepPkScript,
503+
ExpirySweepTxid: d.ExpirySweepTxid,
504+
FinalizedWithdrawalTx: d.FinalizedWithdrawalTx,
505+
}
506+
507+
sqlcDepositUpdate := sqlc.DepositUpdate{
508+
DepositID: id[:],
509+
UpdateState: d.UpdateState.String,
510+
UpdateTimestamp: d.UpdateTimestamp.Time,
511+
}
512+
deposit, err := deposit.ToDeposit(
513+
sqlcDeposit, sqlcDepositUpdate,
514+
)
515+
if err != nil {
516+
return nil, err
517+
}
518+
519+
depositList = append(depositList, deposit)
520+
}
521+
451522
loopIn := &StaticAddressLoopIn{
452523
SwapHash: swapHash,
453524
SwapPreimage: swapPreImage,
454-
HtlcCltvExpiry: row.CltvExpiry,
455-
MaxSwapFee: btcutil.Amount(row.MaxSwapFee),
456-
InitiationHeight: uint32(row.InitiationHeight),
457-
InitiationTime: row.InitiationTime,
525+
HtlcCltvExpiry: swap.CltvExpiry,
526+
MaxSwapFee: btcutil.Amount(swap.MaxSwapFee),
527+
InitiationHeight: uint32(swap.InitiationHeight),
528+
InitiationTime: swap.InitiationTime,
458529
ProtocolVersion: version.AddressProtocolVersion(
459-
row.ProtocolVersion,
530+
swap.ProtocolVersion,
460531
),
461-
Label: row.Label,
532+
Label: swap.Label,
462533
ClientPubkey: clientKey,
463534
ServerPubkey: serverKey,
464535
HtlcKeyLocator: keychain.KeyLocator{
465-
Family: keychain.KeyFamily(row.ClientKeyFamily),
466-
Index: uint32(row.ClientKeyIndex),
536+
Family: keychain.KeyFamily(swap.ClientKeyFamily),
537+
Index: uint32(swap.ClientKeyIndex),
467538
},
468-
SwapInvoice: row.SwapInvoice,
469-
PaymentTimeoutSeconds: uint32(row.PaymentTimeoutSeconds),
470-
LastHop: row.LastHop,
471-
QuotedSwapFee: btcutil.Amount(row.QuotedSwapFeeSatoshis),
539+
SwapInvoice: swap.SwapInvoice,
540+
PaymentTimeoutSeconds: uint32(swap.PaymentTimeoutSeconds),
541+
LastHop: swap.LastHop,
542+
QuotedSwapFee: btcutil.Amount(swap.QuotedSwapFeeSatoshis),
472543
DepositOutpoints: depositOutpoints,
473544
HtlcTxFeeRate: chainfee.SatPerKWeight(
474-
row.HtlcTxFeeRateSatKw,
545+
swap.HtlcTxFeeRateSatKw,
475546
),
476547
HtlcTimeoutSweepAddress: timeoutAddress,
477548
HtlcTimeoutSweepTxHash: htlcTimeoutSweepTxHash,
549+
Deposits: depositList,
478550
}
479551

480552
if len(updates) > 0 {

0 commit comments

Comments
 (0)