staticaddr: method to fetch deposits by outpoints

This commit is contained in:
Slyghtning 2025-04-02 14:29:48 +02:00
parent bce9b5d45d
commit e948c94b95
No known key found for this signature in database
GPG key ID: F82D456EA023C9BF
8 changed files with 135 additions and 0 deletions

View file

@ -18,6 +18,7 @@ type Querier interface {
CreateStaticAddress(ctx context.Context, arg CreateStaticAddressParams) error
CreateWithdrawal(ctx context.Context, arg CreateWithdrawalParams) error
CreateWithdrawalDeposit(ctx context.Context, arg CreateWithdrawalDepositParams) error
DepositForOutpoint(ctx context.Context, arg DepositForOutpointParams) (Deposit, error)
FetchLiquidityParams(ctx context.Context) ([]byte, error)
GetAllWithdrawals(ctx context.Context) ([]Withdrawal, error)
GetBatchSweeps(ctx context.Context, batchID int32) ([]Sweep, error)

View file

@ -49,6 +49,16 @@ FROM
WHERE
deposit_id = $1;
-- name: DepositForOutpoint :one
SELECT
*
FROM
deposits
WHERE
tx_hash = $1
AND
out_index = $2;
-- name: AllDeposits :many
SELECT
*

View file

@ -100,6 +100,39 @@ func (q *Queries) CreateDeposit(ctx context.Context, arg CreateDepositParams) er
return err
}
const depositForOutpoint = `-- name: DepositForOutpoint :one
SELECT
id, deposit_id, tx_hash, out_index, amount, confirmation_height, timeout_sweep_pk_script, expiry_sweep_txid, finalized_withdrawal_tx
FROM
deposits
WHERE
tx_hash = $1
AND
out_index = $2
`
type DepositForOutpointParams struct {
TxHash []byte
OutIndex int32
}
func (q *Queries) DepositForOutpoint(ctx context.Context, arg DepositForOutpointParams) (Deposit, error) {
row := q.db.QueryRowContext(ctx, depositForOutpoint, arg.TxHash, arg.OutIndex)
var i Deposit
err := row.Scan(
&i.ID,
&i.DepositID,
&i.TxHash,
&i.OutIndex,
&i.Amount,
&i.ConfirmationHeight,
&i.TimeoutSweepPkScript,
&i.ExpirySweepTxid,
&i.FinalizedWithdrawalTx,
)
return i, err
}
const getDeposit = `-- name: GetDeposit :one
SELECT
id, deposit_id, tx_hash, out_index, amount, confirmation_height, timeout_sweep_pk_script, expiry_sweep_txid, finalized_withdrawal_tx

View file

@ -26,6 +26,10 @@ type Store interface {
// GetDeposit retrieves a deposit with depositID from the database.
GetDeposit(ctx context.Context, depositID ID) (*Deposit, error)
// DepositForOutpoint retrieves the deposit with the given outpoint.
DepositForOutpoint(ctx context.Context, outpoint string) (*Deposit,
error)
// AllDeposits retrieves all deposits from the store.
AllDeposits(ctx context.Context) ([]*Deposit, error)
}

View file

@ -572,3 +572,36 @@ func (m *Manager) toActiveDeposits(outpoints *[]wire.OutPoint) ([]*FSM,
return fsms, deposits
}
// DepositsForOutpoints returns all deposits that are behind the given
// outpoints.
func (m *Manager) DepositsForOutpoints(ctx context.Context,
outpoints []string) ([]*Deposit, error) {
// Check for duplicates.
existingOutpoints := make(map[string]struct{}, len(outpoints))
for i, o := range outpoints {
if _, ok := existingOutpoints[o]; ok {
return nil, fmt.Errorf("duplicate outpoint %s "+
"at index %d", o, i)
}
existingOutpoints[o] = struct{}{}
}
deposits := make([]*Deposit, 0, len(outpoints))
for _, o := range outpoints {
op, err := wire.NewOutPointFromString(o)
if err != nil {
return nil, err
}
deposit, err := m.cfg.Store.DepositForOutpoint(ctx, op.String())
if err != nil {
return nil, err
}
deposits = append(deposits, deposit)
}
return deposits, nil
}

View file

@ -165,6 +165,13 @@ func (s *mockStore) GetDeposit(ctx context.Context, depositID ID) (*Deposit,
return args.Get(0).(*Deposit), args.Error(1)
}
func (s *mockStore) DepositForOutpoint(ctx context.Context,
outpoint string) (*Deposit, error) {
args := s.Called(ctx, outpoint)
return args.Get(0).(*Deposit), args.Error(1)
}
func (s *mockStore) AllDeposits(ctx context.Context) ([]*Deposit, error) {
args := s.Called(ctx)
return args.Get(0).([]*Deposit), args.Error(1)

View file

@ -149,6 +149,48 @@ func (s *SqlStore) GetDeposit(ctx context.Context, id ID) (*Deposit, error) {
return deposit, nil
}
// DepositForOutpoint retrieves the deposit with the given outpoint from the
// database.
func (s *SqlStore) DepositForOutpoint(ctx context.Context,
outpoint string) (*Deposit, error) {
var deposit *Deposit
err := s.baseDB.ExecTx(ctx, loopdb.NewSqlReadOpts(),
func(q *sqlc.Queries) error {
op, err := wire.NewOutPointFromString(outpoint)
if err != nil {
return err
}
params := sqlc.DepositForOutpointParams{
TxHash: op.Hash[:],
OutIndex: int32(op.Index),
}
row, err := q.DepositForOutpoint(ctx, params)
if err != nil {
return err
}
latestUpdate, err := q.GetLatestDepositUpdate(
ctx, row.DepositID,
)
if err != nil {
return err
}
deposit, err = s.toDeposit(row, latestUpdate)
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
return deposit, nil
}
// AllDeposits retrieves all known deposits to our static address.
func (s *SqlStore) AllDeposits(ctx context.Context) ([]*Deposit, error) {
var allDeposits []*Deposit

View file

@ -48,6 +48,11 @@ type DepositManager interface {
// invalid.
TransitionDeposits(ctx context.Context, deposits []*deposit.Deposit,
event fsm.EventType, expectedFinalState fsm.StateType) error
// DepositsForOutpoints returns all deposits that behind the given
// outpoints.
DepositsForOutpoints(ctx context.Context, outpoints []string) (
[]*deposit.Deposit, error)
}
// StaticAddressLoopInStore provides access to the static address loop-in DB.