Skip to content

staticaddr: method to fetch deposits by outpoints #959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions loopdb/sqlc/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions loopdb/sqlc/queries/static_address_deposits.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
33 changes: 33 additions & 0 deletions loopdb/sqlc/static_address_deposits.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions staticaddr/deposit/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/lightninglabs/loop/staticaddr/address"
"github.com/lightninglabs/loop/staticaddr/script"
"github.com/lightningnetwork/lnd/lnwallet"
Expand All @@ -26,6 +27,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, txHash chainhash.Hash,
idx uint32) (*Deposit, error)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to change the signature to more high level:

DepositForOutpoint(ctx context.Context, outpoint string) (*Deposit, error)


// AllDeposits retrieves all deposits from the store.
AllDeposits(ctx context.Context) ([]*Deposit, error)
}
Expand Down
35 changes: 35 additions & 0 deletions staticaddr/deposit/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,3 +572,38 @@ 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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where would we use the function? Could you please add coverage?

Copy link
Collaborator Author

@hieblmi hieblmi Jun 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes sure, it is used to restore deposits of a swap after fetching it from the db, see hieblmi@349b930#diff-2c26627ef62d3f06764cb007a83924ef8abb36066a183b4d05e7f42e7c6a7ac9R23-R283

Ideally this would all be handled by GetLoopInByHash, but it isn't currently.

outpoints []string) ([]*Deposit, error) {

// Check for duplicates.
duplicates := make(map[string]struct{})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: i'd call this existingOutpoints.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to pre-allocate the map. We now the max size.

for i, o := range outpoints {
if _, ok := duplicates[o]; ok {
return nil, fmt.Errorf("duplicate outpoint %s "+
"at index %d", o, i)
}
duplicates[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.Hash, op.Index,
)
if err != nil {
return nil, err
}

deposits = append(deposits, deposit)
}

return deposits, nil
}
7 changes: 7 additions & 0 deletions staticaddr/deposit/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
txHash chainhash.Hash, idx uint32) (*Deposit, error) {

args := s.Called(ctx, txHash, idx)
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)
Expand Down
38 changes: 38 additions & 0 deletions staticaddr/deposit/sql_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,44 @@ 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,
txHash chainhash.Hash, idx uint32) (*Deposit, error) {

var deposit *Deposit
err := s.baseDB.ExecTx(ctx, loopdb.NewSqlReadOpts(),
func(q *sqlc.Queries) error {
params := sqlc.DepositForOutpointParams{
TxHash: txHash[:],
OutIndex: int32(idx),
}
row, err := q.DepositForOutpoint(ctx, params)
if err != nil {
return err
}

latestUpdate, err := q.GetLatestDepositUpdate(
ctx, row.DepositID,
)
if err != nil {
return err
}
Comment on lines +164 to +174
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do it with SQL join and avoid making an explicit DB transaction (ExecTx).


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
Expand Down
5 changes: 5 additions & 0 deletions staticaddr/loopin/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading
Loading