Skip to content

Commit 78bc133

Browse files
committed
staticaddr: method to fetch deposits by outpoints
1 parent d023b0c commit 78bc133

File tree

8 files changed

+135
-0
lines changed

8 files changed

+135
-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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ type Store interface {
2626
// GetDeposit retrieves a deposit with depositID from the database.
2727
GetDeposit(ctx context.Context, depositID ID) (*Deposit, error)
2828

29+
// DepositForOutpoint retrieves the deposit with the given outpoint.
30+
DepositForOutpoint(ctx context.Context, outpoint string) (*Deposit,
31+
error)
32+
2933
// AllDeposits retrieves all deposits from the store.
3034
AllDeposits(ctx context.Context) ([]*Deposit, error)
3135
}

staticaddr/deposit/manager.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,3 +572,36 @@ func (m *Manager) toActiveDeposits(outpoints *[]wire.OutPoint) ([]*FSM,
572572

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

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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,48 @@ 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+
outpoint string) (*Deposit, error) {
156+
157+
var deposit *Deposit
158+
err := s.baseDB.ExecTx(ctx, loopdb.NewSqlReadOpts(),
159+
func(q *sqlc.Queries) error {
160+
op, err := wire.NewOutPointFromString(outpoint)
161+
if err != nil {
162+
return err
163+
}
164+
params := sqlc.DepositForOutpointParams{
165+
TxHash: op.Hash[:],
166+
OutIndex: int32(op.Index),
167+
}
168+
row, err := q.DepositForOutpoint(ctx, params)
169+
if err != nil {
170+
return err
171+
}
172+
173+
latestUpdate, err := q.GetLatestDepositUpdate(
174+
ctx, row.DepositID,
175+
)
176+
if err != nil {
177+
return err
178+
}
179+
180+
deposit, err = s.toDeposit(row, latestUpdate)
181+
if err != nil {
182+
return err
183+
}
184+
185+
return nil
186+
})
187+
if err != nil {
188+
return nil, err
189+
}
190+
191+
return deposit, nil
192+
}
193+
152194
// AllDeposits retrieves all known deposits to our static address.
153195
func (s *SqlStore) AllDeposits(ctx context.Context) ([]*Deposit, error) {
154196
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)