Skip to content

Commit d363927

Browse files
committed
staticaddr: method to fetch deposits by outpoints
1 parent 214c8fe commit d363927

File tree

8 files changed

+124
-0
lines changed

8 files changed

+124
-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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,3 +578,28 @@ func (m *Manager) toActiveDeposits(outpoints *[]wire.OutPoint) ([]*FSM,
578578

579579
return fsms, deposits
580580
}
581+
582+
// DepositsForOutpoints returns all deposits that are behind the given
583+
// outpoints.
584+
func (m *Manager) DepositsForOutpoints(ctx context.Context,
585+
outpoints []string) ([]*Deposit, error) {
586+
587+
deposits := make([]*Deposit, 0, len(outpoints))
588+
for _, o := range outpoints {
589+
op, err := wire.NewOutPointFromString(o)
590+
if err != nil {
591+
return nil, err
592+
}
593+
594+
deposit, err := m.cfg.Store.DepositForOutpoint(
595+
ctx, op.Hash, op.Index,
596+
)
597+
if err != nil {
598+
return nil, err
599+
}
600+
601+
deposits = append(deposits, deposit)
602+
}
603+
604+
return deposits, nil
605+
}

staticaddr/deposit/manager_test.go

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

170+
func (s *mockStore) DepositForOutpoint(ctx context.Context,
171+
txHash chainhash.Hash, idx uint32) (*Deposit, error) {
172+
173+
args := s.Called(ctx, txHash, idx)
174+
return args.Get(0).(*Deposit), args.Error(1)
175+
}
176+
170177
func (s *mockStore) AllDeposits(ctx context.Context) ([]*Deposit, error) {
171178
args := s.Called(ctx)
172179
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)