Skip to content

Commit f8c06f8

Browse files
committed
staticaddr: load static address params as part of deposits
1 parent c27dbcb commit f8c06f8

File tree

10 files changed

+130
-27
lines changed

10 files changed

+130
-27
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, %w", err)
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: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ import (
66
"database/sql"
77
"encoding/hex"
88

9+
"github.com/btcsuite/btcd/btcec/v2"
910
"github.com/btcsuite/btcd/btcutil"
1011
"github.com/btcsuite/btcd/chaincfg/chainhash"
1112
"github.com/btcsuite/btcd/wire"
1213
"github.com/lightninglabs/loop/fsm"
1314
"github.com/lightninglabs/loop/loopdb"
1415
"github.com/lightninglabs/loop/loopdb/sqlc"
16+
"github.com/lightninglabs/loop/staticaddr/address"
17+
"github.com/lightninglabs/loop/staticaddr/version"
1518
"github.com/lightningnetwork/lnd/clock"
19+
"github.com/lightningnetwork/lnd/keychain"
1620
"github.com/lightningnetwork/lnd/lntypes"
1721
)
1822

@@ -42,6 +46,7 @@ func (s *SqlStore) CreateDeposit(ctx context.Context, deposit *Deposit) error {
4246
Amount: int64(deposit.Value),
4347
ConfirmationHeight: deposit.ConfirmationHeight,
4448
TimeoutSweepPkScript: deposit.TimeOutSweepPkScript,
49+
StaticAddressID: deposit.AddressID,
4550
}
4651

4752
updateArgs := sqlc.InsertDepositUpdateParams{
@@ -136,7 +141,23 @@ func (s *SqlStore) GetDeposit(ctx context.Context, id ID) (*Deposit, error) {
136141
return err
137142
}
138143

139-
deposit, err = ToDeposit(row, latestUpdate)
144+
allDepositsRow := sqlc.AllDepositsRow{
145+
ID: row.ID,
146+
DepositID: row.DepositID,
147+
TxHash: row.TxHash,
148+
OutIndex: row.OutIndex,
149+
Amount: row.Amount,
150+
ConfirmationHeight: row.ConfirmationHeight,
151+
TimeoutSweepPkScript: row.TimeoutSweepPkScript,
152+
ExpirySweepTxid: row.ExpirySweepTxid,
153+
FinalizedWithdrawalTx: row.FinalizedWithdrawalTx,
154+
SwapHash: row.SwapHash,
155+
StaticAddressID: row.StaticAddressID,
156+
ClientPubkey: row.ClientPubkey,
157+
ServerPubkey: row.ServerPubkey,
158+
Expiry: row.Expiry,
159+
}
160+
deposit, err = ToDeposit(allDepositsRow, latestUpdate)
140161
if err != nil {
141162
return err
142163
}
@@ -178,7 +199,24 @@ func (s *SqlStore) DepositForOutpoint(ctx context.Context,
178199
return err
179200
}
180201

181-
deposit, err = ToDeposit(row, latestUpdate)
202+
allDepositsRow := sqlc.AllDepositsRow{
203+
ID: row.ID,
204+
DepositID: row.DepositID,
205+
TxHash: row.TxHash,
206+
OutIndex: row.OutIndex,
207+
Amount: row.Amount,
208+
ConfirmationHeight: row.ConfirmationHeight,
209+
TimeoutSweepPkScript: row.TimeoutSweepPkScript,
210+
ExpirySweepTxid: row.ExpirySweepTxid,
211+
FinalizedWithdrawalTx: row.FinalizedWithdrawalTx,
212+
SwapHash: row.SwapHash,
213+
StaticAddressID: row.StaticAddressID,
214+
ClientPubkey: row.ClientPubkey,
215+
ServerPubkey: row.ServerPubkey,
216+
Expiry: row.Expiry,
217+
}
218+
219+
deposit, err = ToDeposit(allDepositsRow, latestUpdate)
182220
if err != nil {
183221
return err
184222
}
@@ -205,15 +243,15 @@ func (s *SqlStore) AllDeposits(ctx context.Context) ([]*Deposit, error) {
205243
return err
206244
}
207245

208-
for _, deposit := range deposits {
246+
for _, d := range deposits {
209247
latestUpdate, err := q.GetLatestDepositUpdate(
210-
ctx, deposit.DepositID,
248+
ctx, d.DepositID,
211249
)
212250
if err != nil {
213251
return err
214252
}
215253

216-
d, err := ToDeposit(deposit, latestUpdate)
254+
d, err := ToDeposit(d, latestUpdate)
217255
if err != nil {
218256
return err
219257
}
@@ -231,8 +269,8 @@ func (s *SqlStore) AllDeposits(ctx context.Context) ([]*Deposit, error) {
231269
}
232270

233271
// ToDeposit converts an sql deposit to a deposit.
234-
func ToDeposit(row sqlc.Deposit, lastUpdate sqlc.DepositUpdate) (*Deposit,
235-
error) {
272+
func ToDeposit(row sqlc.AllDepositsRow,
273+
lastUpdate sqlc.DepositUpdate) (*Deposit, error) {
236274

237275
id := ID{}
238276
err := id.FromByteSlice(row.DepositID)
@@ -281,6 +319,31 @@ func ToDeposit(row sqlc.Deposit, lastUpdate sqlc.DepositUpdate) (*Deposit,
281319
swapHash = &hash
282320
}
283321

322+
clientPubkey, err := btcec.ParsePubKey(row.ClientPubkey)
323+
if err != nil {
324+
return nil, err
325+
}
326+
327+
serverPubkey, err := btcec.ParsePubKey(row.ServerPubkey)
328+
if err != nil {
329+
return nil, err
330+
}
331+
332+
params := &address.Parameters{
333+
ClientPubkey: clientPubkey,
334+
ServerPubkey: serverPubkey,
335+
Expiry: uint32(row.Expiry.Int32),
336+
PkScript: row.Pkscript,
337+
KeyLocator: keychain.KeyLocator{
338+
Family: keychain.KeyFamily(row.ClientKeyFamily.Int32),
339+
Index: uint32(row.ClientKeyIndex.Int32),
340+
},
341+
ProtocolVersion: version.AddressProtocolVersion(
342+
row.ProtocolVersion.Int32,
343+
),
344+
InitiationHeight: row.InitiationHeight.Int32,
345+
}
346+
284347
return &Deposit{
285348
ID: id,
286349
state: fsm.StateType(lastUpdate.UpdateState),
@@ -294,6 +357,7 @@ func ToDeposit(row sqlc.Deposit, lastUpdate sqlc.DepositUpdate) (*Deposit,
294357
ExpirySweepTxid: expirySweepTxid,
295358
SwapHash: swapHash,
296359
FinalizedWithdrawalTx: finalizedWithdrawalTx,
360+
AddressParams: params,
297361
}, nil
298362
}
299363

@@ -305,9 +369,7 @@ func (s *SqlStore) BatchSetStaticAddressID(ctx context.Context,
305369
return s.baseDB.ExecTx(
306370
ctx, loopdb.NewSqlWriteOpts(), func(q *sqlc.Queries) error {
307371
return q.SetAllNullDepositsStaticAddressID(
308-
ctx, sql.NullInt32{
309-
Int32: staticAddrID, Valid: true,
310-
},
372+
ctx, staticAddrID,
311373
)
312374
},
313375
)

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)