Skip to content

Commit 90432fb

Browse files
committed
staticaddr: load static address params as part of deposits
1 parent 6737728 commit 90432fb

File tree

13 files changed

+257
-41
lines changed

13 files changed

+257
-41
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"
@@ -300,6 +303,13 @@ func (m *Manager) createNewDeposit(ctx context.Context,
300303
"parameters for deposit with pkscript %x", utxo.PkScript)
301304
}
302305

306+
addressID, err := m.cfg.AddressManager.GetStaticAddressID(
307+
ctx, utxo.PkScript,
308+
)
309+
if err != nil {
310+
return nil, err
311+
}
312+
303313
deposit := &Deposit{
304314
ID: id,
305315
state: Deposited,
@@ -308,6 +318,7 @@ func (m *Manager) createNewDeposit(ctx context.Context,
308318
ConfirmationHeight: int64(blockHeight),
309319
TimeOutSweepPkScript: timeoutSweepPkScript,
310320
AddressParams: params,
321+
AddressID: addressID,
311322
}
312323

313324
err = m.cfg.Store.CreateDeposit(ctx, deposit)
@@ -570,7 +581,7 @@ func (m *Manager) toActiveDeposits(outpoints *[]wire.OutPoint) ([]*FSM,
570581
}
571582

572583
// DepositsForOutpoints returns all deposits that are behind the given
573-
// outpoints.
584+
// outpoints. If there's no deposit for an outpoint, it's skipped.
574585
func (m *Manager) DepositsForOutpoints(ctx context.Context,
575586
outpoints []string) ([]*Deposit, error) {
576587

@@ -593,6 +604,11 @@ func (m *Manager) DepositsForOutpoints(ctx context.Context,
593604

594605
deposit, err := m.cfg.Store.DepositForOutpoint(ctx, op.String())
595606
if err != nil {
607+
if errors.Is(err, sql.ErrNoRows) ||
608+
errors.Is(err, pgx.ErrNoRows) {
609+
610+
continue
611+
}
596612
return nil, err
597613
}
598614

staticaddr/deposit/manager_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/lightninglabs/lndclient"
1414
"github.com/lightninglabs/loop/staticaddr/address"
1515
"github.com/lightninglabs/loop/staticaddr/script"
16+
"github.com/lightninglabs/loop/staticaddr/version"
1617
"github.com/lightninglabs/loop/swap"
1718
"github.com/lightninglabs/loop/swapserverrpc"
1819
"github.com/lightninglabs/loop/test"
@@ -116,6 +117,14 @@ func (m *mockAddressManager) GetStaticAddressParameters(ctx context.Context) (
116117
args.Error(1)
117118
}
118119

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

@@ -300,6 +309,11 @@ func newManagerTestContext(t *testing.T) *ManagerTestContext {
300309
Value: utxo.Value,
301310
ConfirmationHeight: 3,
302311
TimeOutSweepPkScript: []byte{0x42, 0x21, 0x69},
312+
AddressID: 1,
313+
AddressParams: &address.Parameters{
314+
ProtocolVersion: version.ProtocolVersion_V0,
315+
Expiry: 100,
316+
},
303317
},
304318
}
305319

staticaddr/deposit/sql_store.go

Lines changed: 86 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,8 @@ func ToDeposit(row sqlc.Deposit, lastUpdate sqlc.DepositUpdate) (*Deposit,
294365
ExpirySweepTxid: expirySweepTxid,
295366
SwapHash: swapHash,
296367
FinalizedWithdrawalTx: finalizedWithdrawalTx,
368+
AddressParams: params,
369+
AddressID: row.StaticAddressID.Int32,
297370
}, nil
298371
}
299372

@@ -302,11 +375,16 @@ func ToDeposit(row sqlc.Deposit, lastUpdate sqlc.DepositUpdate) (*Deposit,
302375
func (s *SqlStore) BatchSetStaticAddressID(ctx context.Context,
303376
staticAddrID int32) error {
304377

378+
if staticAddrID <= 0 {
379+
return fmt.Errorf("static address ID must be set")
380+
}
381+
305382
return s.baseDB.ExecTx(
306383
ctx, loopdb.NewSqlWriteOpts(), func(q *sqlc.Queries) error {
307384
return q.SetAllNullDepositsStaticAddressID(
308385
ctx, sql.NullInt32{
309-
Int32: staticAddrID, Valid: true,
386+
Int32: staticAddrID,
387+
Valid: true,
310388
},
311389
)
312390
},

0 commit comments

Comments
 (0)