Skip to content

Commit 1cc4bbb

Browse files
committed
staticaddr: method to fetch deposits by outpoints
1 parent 947d08b commit 1cc4bbb

File tree

8 files changed

+134
-0
lines changed

8 files changed

+134
-0
lines changed

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_deposits.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ FROM
4949
WHERE
5050
deposit_id = $1;
5151

52+
-- name: DepositForOutpoint :one
53+
SELECT
54+
*
55+
FROM
56+
deposits
57+
WHERE
58+
tx_hash = $1
59+
AND
60+
out_index = $2;
61+
5262
-- name: AllDeposits :many
5363
SELECT
5464
*

loopdb/sqlc/static_address_deposits.sql.go

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

staticaddr/deposit/interface.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/btcsuite/btcd/btcec/v2"
77
"github.com/btcsuite/btcd/btcutil"
8+
"github.com/btcsuite/btcd/chaincfg/chainhash"
89
"github.com/lightninglabs/loop/staticaddr/address"
910
"github.com/lightninglabs/loop/staticaddr/script"
1011
"github.com/lightningnetwork/lnd/lnwallet"
@@ -26,6 +27,10 @@ type Store interface {
2627
// GetDeposit retrieves a deposit with depositID from the database.
2728
GetDeposit(ctx context.Context, depositID ID) (*Deposit, error)
2829

30+
// DepositForOutpoint retrieves the deposit with the given outpoint.
31+
DepositForOutpoint(ctx context.Context, txHash chainhash.Hash,
32+
idx uint32) (*Deposit, error)
33+
2934
// AllDeposits retrieves all deposits from the store.
3035
AllDeposits(ctx context.Context) ([]*Deposit, error)
3136
}

staticaddr/deposit/manager.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,3 +567,38 @@ func (m *Manager) toActiveDeposits(outpoints *[]wire.OutPoint) ([]*FSM,
567567

568568
return fsms, deposits
569569
}
570+
571+
// DepositsForOutpoints returns all deposits that are behind the given
572+
// outpoints.
573+
func (m *Manager) DepositsForOutpoints(ctx context.Context,
574+
outpoints []string) ([]*Deposit, error) {
575+
576+
// Check for duplicates.
577+
duplicates := make(map[string]struct{})
578+
for i, o := range outpoints {
579+
if _, ok := duplicates[o]; ok {
580+
return nil, fmt.Errorf("duplicate outpoint %s "+
581+
"at index %d", o, i)
582+
}
583+
duplicates[o] = struct{}{}
584+
}
585+
586+
deposits := make([]*Deposit, 0, len(outpoints))
587+
for _, o := range outpoints {
588+
op, err := wire.NewOutPointFromString(o)
589+
if err != nil {
590+
return nil, err
591+
}
592+
593+
deposit, err := m.cfg.Store.DepositForOutpoint(
594+
ctx, op.Hash, op.Index,
595+
)
596+
if err != nil {
597+
return nil, err
598+
}
599+
600+
deposits = append(deposits, deposit)
601+
}
602+
603+
return deposits, nil
604+
}

staticaddr/deposit/manager_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ func (s *mockStore) GetDeposit(ctx context.Context, depositID ID) (*Deposit,
165165
return args.Get(0).(*Deposit), args.Error(1)
166166
}
167167

168+
func (s *mockStore) DepositForOutpoint(ctx context.Context,
169+
txHash chainhash.Hash, idx uint32) (*Deposit, error) {
170+
171+
args := s.Called(ctx, txHash, idx)
172+
return args.Get(0).(*Deposit), args.Error(1)
173+
}
174+
168175
func (s *mockStore) AllDeposits(ctx context.Context) ([]*Deposit, error) {
169176
args := s.Called(ctx)
170177
return args.Get(0).([]*Deposit), args.Error(1)

staticaddr/deposit/sql_store.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,44 @@ func (s *SqlStore) GetDeposit(ctx context.Context, id ID) (*Deposit, error) {
149149
return deposit, nil
150150
}
151151

152+
// DepositForOutpoint retrieves the deposit with the given outpoint from the
153+
// database.
154+
func (s *SqlStore) DepositForOutpoint(ctx context.Context,
155+
txHash chainhash.Hash, idx uint32) (*Deposit, error) {
156+
157+
var deposit *Deposit
158+
err := s.baseDB.ExecTx(ctx, loopdb.NewSqlReadOpts(),
159+
func(q *sqlc.Queries) error {
160+
params := sqlc.DepositForOutpointParams{
161+
TxHash: txHash[:],
162+
OutIndex: int32(idx),
163+
}
164+
row, err := q.DepositForOutpoint(ctx, params)
165+
if err != nil {
166+
return err
167+
}
168+
169+
latestUpdate, err := q.GetLatestDepositUpdate(
170+
ctx, row.DepositID,
171+
)
172+
if err != nil {
173+
return err
174+
}
175+
176+
deposit, err = s.toDeposit(row, latestUpdate)
177+
if err != nil {
178+
return err
179+
}
180+
181+
return nil
182+
})
183+
if err != nil {
184+
return nil, err
185+
}
186+
187+
return deposit, nil
188+
}
189+
152190
// AllDeposits retrieves all known deposits to our static address.
153191
func (s *SqlStore) AllDeposits(ctx context.Context) ([]*Deposit, error) {
154192
var allDeposits []*Deposit

staticaddr/loopin/interface.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ type DepositManager interface {
4848
// invalid.
4949
TransitionDeposits(ctx context.Context, deposits []*deposit.Deposit,
5050
event fsm.EventType, expectedFinalState fsm.StateType) error
51+
52+
// DepositsForOutpoints returns all deposits that behind the given
53+
// outpoints.
54+
DepositsForOutpoints(ctx context.Context, outpoints []string) (
55+
[]*deposit.Deposit, error)
5156
}
5257

5358
// StaticAddressLoopInStore provides access to the static address loop-in DB.

0 commit comments

Comments
 (0)