Skip to content

Commit 723243b

Browse files
committed
staticaddr: load static address params as part of deposits
1 parent b509918 commit 723243b

File tree

10 files changed

+143
-25
lines changed

10 files changed

+143
-25
lines changed

staticaddr/address/interface.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ type Store interface {
2020
// into the store.
2121
CreateStaticAddress(ctx context.Context, addrParams *Parameters) error
2222

23-
// GetStaticAddress fetches static address parameters for a given
24-
// address ID.
25-
GetStaticAddress(ctx context.Context, pkScript []byte) (*Parameters,
26-
error)
23+
// GetStaticAddressID retrieves the ID of a static address from the
24+
// database.
25+
GetStaticAddressID(ctx context.Context, pkScript []byte) (int32, error)
2726

2827
// GetAllStaticAddresses retrieves all static addresses from the store.
2928
GetAllStaticAddresses(ctx context.Context) ([]*Parameters,

staticaddr/address/manager.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,12 @@ func (m *Manager) GetParameters(pkScript []byte) *Parameters {
357357
return m.activeStaticAddresses[string(pkScript)]
358358
}
359359

360+
func (m *Manager) GetStaticAddressID(ctx context.Context,
361+
pkScript []byte) (int32, error) {
362+
363+
return m.cfg.Store.GetStaticAddressID(ctx, pkScript)
364+
}
365+
360366
// GetStaticAddress returns a taproot address for the given client and server
361367
// public keys and expiry.
362368
func (m *Manager) GetStaticAddress(ctx context.Context) (*script.StaticAddress,

staticaddr/address/sql_store.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,15 @@ func (s *SqlStore) CreateStaticAddress(ctx context.Context,
4141
return s.baseDB.Queries.CreateStaticAddress(ctx, createArgs)
4242
}
4343

44-
// GetStaticAddress retrieves static address parameters for a given pkScript.
45-
func (s *SqlStore) GetStaticAddress(ctx context.Context,
46-
pkScript []byte) (*Parameters, error) {
44+
func (s *SqlStore) GetStaticAddressID(ctx context.Context,
45+
pkScript []byte) (int32, error) {
4746

48-
staticAddress, err := s.baseDB.Queries.GetStaticAddress(ctx, pkScript)
47+
address, err := s.baseDB.Queries.GetStaticAddress(ctx, pkScript)
4948
if err != nil {
50-
return nil, err
49+
return 0, err
5150
}
5251

53-
return s.toAddressParameters(staticAddress)
52+
return address.ID, nil
5453
}
5554

5655
// GetAllStaticAddresses returns all address known to the server.

staticaddr/deposit/deposit.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ type Deposit struct {
6767
// AddressParams are the parameters of the address that are backing this
6868
// deposit.
6969
AddressParams *address.Parameters
70+
71+
// AddressID is the ID of the address that is backing this deposit.
72+
AddressID int32
7073
}
7174

7275
// IsInFinalState returns true if the deposit is final.

staticaddr/deposit/interface.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ type Store interface {
3434

3535
// AddressManager handles fetching of address parameters.
3636
type AddressManager interface {
37+
// GetStaticAddressID the ID of the static address for the given
38+
// pkScript.
39+
GetStaticAddressID(ctx context.Context, pkScript []byte) (int32, error)
40+
3741
// GetParameters returns the static address parameters for the given
3842
// pkScript.
3943
GetParameters(pkScript []byte) *address.Parameters

staticaddr/deposit/manager.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package deposit
22

33
import (
44
"context"
5+
"database/sql"
6+
"errors"
57
"fmt"
68
"sort"
79
"sync"
@@ -10,6 +12,7 @@ import (
1012
"github.com/btcsuite/btcd/chaincfg"
1113
"github.com/btcsuite/btcd/txscript"
1214
"github.com/btcsuite/btcd/wire"
15+
"github.com/jackc/pgx/v5"
1316
"github.com/lightninglabs/lndclient"
1417
"github.com/lightninglabs/loop"
1518
"github.com/lightninglabs/loop/fsm"
@@ -314,6 +317,13 @@ func (m *Manager) createNewDeposit(ctx context.Context,
314317
"parameters for deposit with pkscript %x", utxo.PkScript)
315318
}
316319

320+
addressID, err := m.cfg.AddressManager.GetStaticAddressID(
321+
ctx, utxo.PkScript,
322+
)
323+
if err != nil {
324+
return nil, err
325+
}
326+
317327
deposit := &Deposit{
318328
ID: id,
319329
state: Deposited,
@@ -322,6 +332,7 @@ func (m *Manager) createNewDeposit(ctx context.Context,
322332
ConfirmationHeight: int64(blockHeight),
323333
TimeOutSweepPkScript: timeoutSweepPkScript,
324334
AddressParams: params,
335+
AddressID: addressID,
325336
}
326337

327338
err = m.cfg.Store.CreateDeposit(ctx, deposit)
@@ -584,7 +595,7 @@ func (m *Manager) toActiveDeposits(outpoints *[]wire.OutPoint) ([]*FSM,
584595
}
585596

586597
// DepositsForOutpoints returns all deposits that are behind the given
587-
// outpoints.
598+
// outpoints. If there's no deposit for an outpoint, it's skipped.
588599
func (m *Manager) DepositsForOutpoints(ctx context.Context,
589600
outpoints []string) ([]*Deposit, error) {
590601

@@ -607,6 +618,11 @@ func (m *Manager) DepositsForOutpoints(ctx context.Context,
607618

608619
deposit, err := m.cfg.Store.DepositForOutpoint(ctx, op.String())
609620
if err != nil {
621+
if errors.Is(err, sql.ErrNoRows) ||
622+
errors.Is(err, pgx.ErrNoRows) {
623+
624+
continue
625+
}
610626
return nil, err
611627
}
612628

staticaddr/deposit/manager_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ func (m *mockAddressManager) GetStaticAddressParameters(ctx context.Context) (
116116
args.Error(1)
117117
}
118118

119+
func (m *mockAddressManager) GetStaticAddressID(ctx context.Context,
120+
pkScript []byte) (int32, error) {
121+
122+
args := m.Called(ctx, pkScript)
123+
124+
return args.Get(0).(int32), nil
125+
}
126+
119127
func (m *mockAddressManager) GetParameters(
120128
pkScript []byte) *address.Parameters {
121129

staticaddr/deposit/sql_store.go

Lines changed: 85 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@ import (
55
"context"
66
"database/sql"
77
"encoding/hex"
8+
"fmt"
89

10+
"github.com/btcsuite/btcd/btcec/v2"
911
"github.com/btcsuite/btcd/btcutil"
1012
"github.com/btcsuite/btcd/chaincfg/chainhash"
1113
"github.com/btcsuite/btcd/wire"
1214
"github.com/lightninglabs/loop/fsm"
1315
"github.com/lightninglabs/loop/loopdb"
1416
"github.com/lightninglabs/loop/loopdb/sqlc"
17+
"github.com/lightninglabs/loop/staticaddr/address"
18+
"github.com/lightninglabs/loop/staticaddr/version"
1519
"github.com/lightningnetwork/lnd/clock"
20+
"github.com/lightningnetwork/lnd/keychain"
1621
"github.com/lightningnetwork/lnd/lntypes"
1722
)
1823

@@ -35,13 +40,21 @@ func NewSqlStore(db *loopdb.BaseDB) *SqlStore {
3540

3641
// CreateDeposit creates a static address deposit record in the database.
3742
func (s *SqlStore) CreateDeposit(ctx context.Context, deposit *Deposit) error {
43+
if deposit.AddressID <= 0 {
44+
return fmt.Errorf("static address ID must be set")
45+
}
46+
3847
createArgs := sqlc.CreateDepositParams{
3948
DepositID: deposit.ID[:],
4049
TxHash: deposit.Hash[:],
4150
OutIndex: int32(deposit.Index),
4251
Amount: int64(deposit.Value),
4352
ConfirmationHeight: deposit.ConfirmationHeight,
4453
TimeoutSweepPkScript: deposit.TimeOutSweepPkScript,
54+
StaticAddressID: sql.NullInt32{
55+
Int32: deposit.AddressID,
56+
Valid: true,
57+
},
4558
}
4659

4760
updateArgs := sqlc.InsertDepositUpdateParams{
@@ -136,7 +149,23 @@ func (s *SqlStore) GetDeposit(ctx context.Context, id ID) (*Deposit, error) {
136149
return err
137150
}
138151

139-
deposit, err = ToDeposit(row, latestUpdate)
152+
allDepositsRow := sqlc.AllDepositsRow{
153+
ID: row.ID,
154+
DepositID: row.DepositID,
155+
TxHash: row.TxHash,
156+
OutIndex: row.OutIndex,
157+
Amount: row.Amount,
158+
ConfirmationHeight: row.ConfirmationHeight,
159+
TimeoutSweepPkScript: row.TimeoutSweepPkScript,
160+
ExpirySweepTxid: row.ExpirySweepTxid,
161+
FinalizedWithdrawalTx: row.FinalizedWithdrawalTx,
162+
SwapHash: row.SwapHash,
163+
StaticAddressID: row.StaticAddressID,
164+
ClientPubkey: row.ClientPubkey,
165+
ServerPubkey: row.ServerPubkey,
166+
Expiry: row.Expiry,
167+
}
168+
deposit, err = ToDeposit(allDepositsRow, latestUpdate)
140169
if err != nil {
141170
return err
142171
}
@@ -178,7 +207,24 @@ func (s *SqlStore) DepositForOutpoint(ctx context.Context,
178207
return err
179208
}
180209

181-
deposit, err = ToDeposit(row, latestUpdate)
210+
allDepositsRow := sqlc.AllDepositsRow{
211+
ID: row.ID,
212+
DepositID: row.DepositID,
213+
TxHash: row.TxHash,
214+
OutIndex: row.OutIndex,
215+
Amount: row.Amount,
216+
ConfirmationHeight: row.ConfirmationHeight,
217+
TimeoutSweepPkScript: row.TimeoutSweepPkScript,
218+
ExpirySweepTxid: row.ExpirySweepTxid,
219+
FinalizedWithdrawalTx: row.FinalizedWithdrawalTx,
220+
SwapHash: row.SwapHash,
221+
StaticAddressID: row.StaticAddressID,
222+
ClientPubkey: row.ClientPubkey,
223+
ServerPubkey: row.ServerPubkey,
224+
Expiry: row.Expiry,
225+
}
226+
227+
deposit, err = ToDeposit(allDepositsRow, latestUpdate)
182228
if err != nil {
183229
return err
184230
}
@@ -205,15 +251,15 @@ func (s *SqlStore) AllDeposits(ctx context.Context) ([]*Deposit, error) {
205251
return err
206252
}
207253

208-
for _, deposit := range deposits {
254+
for _, d := range deposits {
209255
latestUpdate, err := q.GetLatestDepositUpdate(
210-
ctx, deposit.DepositID,
256+
ctx, d.DepositID,
211257
)
212258
if err != nil {
213259
return err
214260
}
215261

216-
d, err := ToDeposit(deposit, latestUpdate)
262+
d, err := ToDeposit(d, latestUpdate)
217263
if err != nil {
218264
return err
219265
}
@@ -231,8 +277,8 @@ func (s *SqlStore) AllDeposits(ctx context.Context) ([]*Deposit, error) {
231277
}
232278

233279
// ToDeposit converts an sql deposit to a deposit.
234-
func ToDeposit(row sqlc.Deposit, lastUpdate sqlc.DepositUpdate) (*Deposit,
235-
error) {
280+
func ToDeposit(row sqlc.AllDepositsRow,
281+
lastUpdate sqlc.DepositUpdate) (*Deposit, error) {
236282

237283
id := ID{}
238284
err := id.FromByteSlice(row.DepositID)
@@ -281,6 +327,31 @@ func ToDeposit(row sqlc.Deposit, lastUpdate sqlc.DepositUpdate) (*Deposit,
281327
swapHash = &hash
282328
}
283329

330+
clientPubkey, err := btcec.ParsePubKey(row.ClientPubkey)
331+
if err != nil {
332+
return nil, err
333+
}
334+
335+
serverPubkey, err := btcec.ParsePubKey(row.ServerPubkey)
336+
if err != nil {
337+
return nil, err
338+
}
339+
340+
params := &address.Parameters{
341+
ClientPubkey: clientPubkey,
342+
ServerPubkey: serverPubkey,
343+
Expiry: uint32(row.Expiry.Int32),
344+
PkScript: row.Pkscript,
345+
KeyLocator: keychain.KeyLocator{
346+
Family: keychain.KeyFamily(row.ClientKeyFamily.Int32),
347+
Index: uint32(row.ClientKeyIndex.Int32),
348+
},
349+
ProtocolVersion: version.AddressProtocolVersion(
350+
row.ProtocolVersion.Int32,
351+
),
352+
InitiationHeight: row.InitiationHeight.Int32,
353+
}
354+
284355
return &Deposit{
285356
ID: id,
286357
state: fsm.StateType(lastUpdate.UpdateState),
@@ -294,6 +365,7 @@ func ToDeposit(row sqlc.Deposit, lastUpdate sqlc.DepositUpdate) (*Deposit,
294365
ExpirySweepTxid: expirySweepTxid,
295366
SwapHash: swapHash,
296367
FinalizedWithdrawalTx: finalizedWithdrawalTx,
368+
AddressParams: params,
297369
}, nil
298370
}
299371

@@ -302,11 +374,16 @@ func ToDeposit(row sqlc.Deposit, lastUpdate sqlc.DepositUpdate) (*Deposit,
302374
func (s *SqlStore) BatchSetStaticAddressID(ctx context.Context,
303375
staticAddrID int32) error {
304376

377+
if staticAddrID <= 0 {
378+
return fmt.Errorf("static address ID must be set")
379+
}
380+
305381
return s.baseDB.ExecTx(
306382
ctx, loopdb.NewSqlWriteOpts(), func(q *sqlc.Queries) error {
307383
return q.SetAllNullDepositsStaticAddressID(
308384
ctx, sql.NullInt32{
309-
Int32: staticAddrID, Valid: true,
385+
Int32: staticAddrID,
386+
Valid: true,
310387
},
311388
)
312389
},

staticaddr/deposit/sql_store_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ func TestToDeposit(t *testing.T) {
2222

2323
tests := []struct {
2424
name string
25-
row sqlc.Deposit
25+
row sqlc.AllDepositsRow
2626
lastUpdate sqlc.DepositUpdate
2727
expectErr bool
2828
}{
2929
{
3030
name: "fully valid data",
31-
row: sqlc.Deposit{
31+
row: sqlc.AllDepositsRow{
3232
DepositID: depositID[:],
3333
TxHash: txHash[:],
3434
Amount: 100000000,
@@ -42,7 +42,7 @@ func TestToDeposit(t *testing.T) {
4242
},
4343
{
4444
name: "fully valid data",
45-
row: sqlc.Deposit{
45+
row: sqlc.AllDepositsRow{
4646
DepositID: depositID[:],
4747
TxHash: txHash[:],
4848
Amount: 100000000,

staticaddr/loopin/sql_store.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -500,15 +500,21 @@ func toStaticAddressLoopIn(_ context.Context, network *chaincfg.Params,
500500
return nil, err
501501
}
502502

503-
sqlcDeposit := sqlc.Deposit{
503+
allDepositsRow := sqlc.AllDepositsRow{
504504
DepositID: id[:],
505+
ID: d.ID,
505506
TxHash: d.TxHash,
506-
Amount: d.Amount,
507507
OutIndex: d.OutIndex,
508+
Amount: d.Amount,
508509
ConfirmationHeight: d.ConfirmationHeight,
509510
TimeoutSweepPkScript: d.TimeoutSweepPkScript,
510511
ExpirySweepTxid: d.ExpirySweepTxid,
511512
FinalizedWithdrawalTx: d.FinalizedWithdrawalTx,
513+
SwapHash: d.SwapHash,
514+
StaticAddressID: d.StaticAddressID,
515+
ClientPubkey: d.ClientPubkey,
516+
ServerPubkey: d.ServerPubkey,
517+
Expiry: d.Expiry,
512518
}
513519

514520
sqlcDepositUpdate := sqlc.DepositUpdate{
@@ -517,7 +523,7 @@ func toStaticAddressLoopIn(_ context.Context, network *chaincfg.Params,
517523
UpdateTimestamp: d.UpdateTimestamp.Time,
518524
}
519525
deposit, err := deposit.ToDeposit(
520-
sqlcDeposit, sqlcDepositUpdate,
526+
allDepositsRow, sqlcDepositUpdate,
521527
)
522528
if err != nil {
523529
return nil, err

0 commit comments

Comments
 (0)