Skip to content

Commit 4e74cc8

Browse files
committed
staticaddr: interfaces
1 parent 7dbfda2 commit 4e74cc8

File tree

4 files changed

+267
-22
lines changed

4 files changed

+267
-22
lines changed
Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
1-
package staticaddr
1+
package address
22

33
import (
44
"context"
55
"fmt"
66

77
"github.com/btcsuite/btcd/btcec/v2"
8+
"github.com/lightninglabs/loop/staticaddr"
89
"github.com/lightningnetwork/lnd/keychain"
910
)
1011

1112
var (
1213
ErrAddressAlreadyExists = fmt.Errorf("address already exists")
13-
ErrAddressNotFound = fmt.Errorf("address not found")
1414
)
1515

16-
// AddressStore is the database interface that is used to store and retrieve
16+
// Store is the database interface that is used to store and retrieve
1717
// static addresses.
18-
type AddressStore interface {
18+
type Store interface {
1919
// CreateStaticAddress inserts a new static address with its parameters
2020
// into the store.
21-
CreateStaticAddress(ctx context.Context,
22-
addrParams *AddressParameters) error
21+
CreateStaticAddress(ctx context.Context, addrParams *Parameters) error
2322

2423
// GetStaticAddress fetches static address parameters for a given
2524
// address ID.
26-
GetStaticAddress(ctx context.Context,
27-
pkScript []byte) (*AddressParameters, error)
25+
GetStaticAddress(ctx context.Context, pkScript []byte) (*Parameters,
26+
error)
2827

2928
// GetAllStaticAddresses retrieves all static addresses from the store.
30-
GetAllStaticAddresses(ctx context.Context) (
31-
[]*AddressParameters, error)
29+
GetAllStaticAddresses(ctx context.Context) ([]*Parameters,
30+
error)
3231
}
3332

34-
// AddressParameters holds all the necessary information for the 2-of-2 multisig
33+
// Parameters holds all the necessary information for the 2-of-2 multisig
3534
// address.
36-
type AddressParameters struct {
35+
type Parameters struct {
3736
// ClientPubkey is the client's pubkey for the static address. It is
3837
// used for the 2-of-2 funding output as well as for the client's
3938
// timeout path.
@@ -54,5 +53,5 @@ type AddressParameters struct {
5453
KeyLocator keychain.KeyLocator
5554

5655
// ProtocolVersion is the protocol version of the static address.
57-
ProtocolVersion AddressProtocolVersion
56+
ProtocolVersion staticaddr.AddressProtocolVersion
5857
}

staticaddr/sql_store.go renamed to staticaddr/address/sql_store.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package staticaddr
1+
package address
22

33
import (
44
"context"
@@ -8,6 +8,7 @@ import (
88
"github.com/jackc/pgx/v4"
99
"github.com/lightninglabs/loop/loopdb"
1010
"github.com/lightninglabs/loop/loopdb/sqlc"
11+
"github.com/lightninglabs/loop/staticaddr"
1112
"github.com/lightningnetwork/lnd/keychain"
1213
)
1314

@@ -63,7 +64,7 @@ func (s *SqlStore) ExecTx(ctx context.Context, txOptions loopdb.TxOptions,
6364

6465
// CreateStaticAddress creates a static address record in the database.
6566
func (s *SqlStore) CreateStaticAddress(ctx context.Context,
66-
addrParams *AddressParameters) error {
67+
addrParams *Parameters) error {
6768

6869
createArgs := sqlc.CreateStaticAddressParams{
6970
ClientPubkey: addrParams.ClientPubkey.SerializeCompressed(),
@@ -80,7 +81,7 @@ func (s *SqlStore) CreateStaticAddress(ctx context.Context,
8081

8182
// GetStaticAddress retrieves static address parameters for a given pkScript.
8283
func (s *SqlStore) GetStaticAddress(ctx context.Context,
83-
pkScript []byte) (*AddressParameters, error) {
84+
pkScript []byte) (*Parameters, error) {
8485

8586
staticAddress, err := s.baseDB.Queries.GetStaticAddress(ctx, pkScript)
8687
if err != nil {
@@ -91,15 +92,15 @@ func (s *SqlStore) GetStaticAddress(ctx context.Context,
9192
}
9293

9394
// GetAllStaticAddresses returns all address known to the server.
94-
func (s *SqlStore) GetAllStaticAddresses(ctx context.Context) (
95-
[]*AddressParameters, error) {
95+
func (s *SqlStore) GetAllStaticAddresses(ctx context.Context) ([]*Parameters,
96+
error) {
9697

9798
staticAddresses, err := s.baseDB.Queries.AllStaticAddresses(ctx)
9899
if err != nil {
99100
return nil, err
100101
}
101102

102-
var result []*AddressParameters
103+
var result []*Parameters
103104
for _, address := range staticAddresses {
104105
res, err := s.toAddressParameters(address)
105106
if err != nil {
@@ -120,7 +121,7 @@ func (s *SqlStore) Close() {
120121
// toAddressParameters transforms a database representation of a static address
121122
// to an AddressParameters struct.
122123
func (s *SqlStore) toAddressParameters(row sqlc.StaticAddress) (
123-
*AddressParameters, error) {
124+
*Parameters, error) {
124125

125126
clientPubkey, err := btcec.ParsePubKey(row.ClientPubkey)
126127
if err != nil {
@@ -132,7 +133,7 @@ func (s *SqlStore) toAddressParameters(row sqlc.StaticAddress) (
132133
return nil, err
133134
}
134135

135-
return &AddressParameters{
136+
return &Parameters{
136137
ClientPubkey: clientPubkey,
137138
ServerPubkey: serverPubkey,
138139
PkScript: row.Pkscript,
@@ -141,6 +142,6 @@ func (s *SqlStore) toAddressParameters(row sqlc.StaticAddress) (
141142
Family: keychain.KeyFamily(row.ClientKeyFamily),
142143
Index: uint32(row.ClientKeyIndex),
143144
},
144-
ProtocolVersion: AddressProtocolVersion(row.ProtocolVersion),
145+
ProtocolVersion: staticaddr.AddressProtocolVersion(row.ProtocolVersion),
145146
}, nil
146147
}

staticaddr/deposit/interface.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package deposit
2+
3+
import (
4+
"context"
5+
6+
"github.com/lightninglabs/loop/staticaddr/address"
7+
"github.com/lightninglabs/loop/staticaddr/script"
8+
"github.com/lightningnetwork/lnd/lnwallet"
9+
)
10+
11+
const (
12+
IdLength = 32
13+
)
14+
15+
// Store is the database interface that is used to store and retrieve
16+
// static address deposits.
17+
type Store interface {
18+
// CreateDeposit inserts a new deposit into the store.
19+
CreateDeposit(ctx context.Context, deposit *Deposit) error
20+
21+
// UpdateDeposit updates the deposit in the database.
22+
UpdateDeposit(ctx context.Context, deposit *Deposit) error
23+
24+
// GetDeposit retrieves a deposit with depositID from the database.
25+
GetDeposit(ctx context.Context, depositID ID) (*Deposit, error)
26+
27+
// AllDeposits retrieves all deposits from the store.
28+
AllDeposits(ctx context.Context) ([]*Deposit, error)
29+
}
30+
31+
// AddressManager handles fetching of address parameters.
32+
type AddressManager interface {
33+
// GetStaticAddressParameters returns the static address parameters.
34+
GetStaticAddressParameters(ctx context.Context) (*address.Parameters,
35+
error)
36+
37+
// GetStaticAddress returns the deposit address for the given
38+
// client and server public keys.
39+
GetStaticAddress(ctx context.Context) (*script.StaticAddress, error)
40+
41+
// ListUnspent returns a list of utxos at the static address.
42+
ListUnspent(ctx context.Context, minConfs,
43+
maxConfs int32) ([]*lnwallet.Utxo, error)
44+
}

staticaddr/deposit/sql_store.go

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
package deposit
2+
3+
import (
4+
"context"
5+
"database/sql"
6+
7+
"github.com/btcsuite/btcd/btcutil"
8+
"github.com/btcsuite/btcd/chaincfg/chainhash"
9+
"github.com/btcsuite/btcd/wire"
10+
"github.com/lightninglabs/loop/fsm"
11+
"github.com/lightninglabs/loop/loopdb"
12+
"github.com/lightninglabs/loop/loopdb/sqlc"
13+
"github.com/lightningnetwork/lnd/clock"
14+
)
15+
16+
// SqlStore is the backing store for static addresses.
17+
type SqlStore struct {
18+
baseDB *loopdb.BaseDB
19+
20+
clock clock.Clock
21+
}
22+
23+
// NewSqlStore constructs a new SQLStore from a BaseDB. The BaseDB is agnostic
24+
// to the underlying driver which can be postgres or sqlite.
25+
func NewSqlStore(db *loopdb.BaseDB) *SqlStore {
26+
return &SqlStore{
27+
baseDB: db,
28+
29+
clock: clock.NewDefaultClock(),
30+
}
31+
}
32+
33+
// CreateDeposit creates a static address record in the database.
34+
func (s *SqlStore) CreateDeposit(ctx context.Context, deposit *Deposit) error {
35+
createArgs := sqlc.CreateDepositParams{
36+
DepositID: deposit.ID[:],
37+
TxHash: deposit.Hash[:],
38+
OutIndex: int32(deposit.Index),
39+
Amount: int64(deposit.Value),
40+
ConfirmationHeight: deposit.ConfirmationHeight,
41+
TimeoutSweepPkScript: deposit.TimeOutSweepPkScript,
42+
}
43+
44+
updateArgs := sqlc.InsertDepositUpdateParams{
45+
DepositID: deposit.ID[:],
46+
UpdateTimestamp: s.clock.Now().UTC(),
47+
UpdateState: string(deposit.State),
48+
}
49+
50+
return s.baseDB.ExecTx(ctx, &loopdb.SqliteTxOptions{},
51+
func(q *sqlc.Queries) error {
52+
err := q.CreateDeposit(ctx, createArgs)
53+
if err != nil {
54+
return err
55+
}
56+
57+
return q.InsertDepositUpdate(ctx, updateArgs)
58+
})
59+
}
60+
61+
// UpdateDeposit updates the deposit in the database.
62+
func (s *SqlStore) UpdateDeposit(ctx context.Context, deposit *Deposit) error {
63+
insertUpdateArgs := sqlc.InsertDepositUpdateParams{
64+
DepositID: deposit.ID[:],
65+
UpdateTimestamp: s.clock.Now().UTC(),
66+
UpdateState: string(deposit.State),
67+
}
68+
69+
var (
70+
txHash = deposit.Hash[:]
71+
outIndex = sql.NullInt32{
72+
Int32: int32(deposit.Index),
73+
Valid: true,
74+
}
75+
)
76+
77+
updateArgs := sqlc.UpdateDepositParams{
78+
DepositID: deposit.ID[:],
79+
TxHash: txHash,
80+
OutIndex: outIndex.Int32,
81+
ConfirmationHeight: sql.NullInt64{
82+
Int64: deposit.ConfirmationHeight,
83+
Valid: deposit.ConfirmationHeight != 0,
84+
}.Int64,
85+
}
86+
87+
return s.baseDB.ExecTx(ctx, &loopdb.SqliteTxOptions{},
88+
func(q *sqlc.Queries) error {
89+
err := q.UpdateDeposit(ctx, updateArgs)
90+
if err != nil {
91+
return err
92+
}
93+
94+
return q.InsertDepositUpdate(ctx, insertUpdateArgs)
95+
})
96+
}
97+
98+
// GetDeposit retrieves the deposit from the database.
99+
func (s *SqlStore) GetDeposit(ctx context.Context, id ID) (*Deposit, error) {
100+
var deposit *Deposit
101+
err := s.baseDB.ExecTx(ctx, loopdb.NewSqlReadOpts(),
102+
func(q *sqlc.Queries) error {
103+
row, err := q.GetDeposit(ctx, id[:])
104+
if err != nil {
105+
return err
106+
}
107+
108+
latestUpdate, err := q.GetLatestDepositUpdate(
109+
ctx, id[:],
110+
)
111+
if err != nil {
112+
return err
113+
}
114+
115+
deposit, err = s.toDeposit(row, latestUpdate)
116+
if err != nil {
117+
return err
118+
}
119+
120+
return nil
121+
})
122+
if err != nil {
123+
return nil, err
124+
}
125+
126+
return deposit, nil
127+
}
128+
129+
// AllDeposits retrieves all known deposits to our static address.
130+
func (s *SqlStore) AllDeposits(ctx context.Context) ([]*Deposit, error) {
131+
var allDeposits []*Deposit
132+
133+
err := s.baseDB.ExecTx(ctx, loopdb.NewSqlReadOpts(),
134+
func(q *sqlc.Queries) error {
135+
var err error
136+
137+
deposits, err := q.AllDeposits(ctx)
138+
if err != nil {
139+
return err
140+
}
141+
142+
for _, deposit := range deposits {
143+
latestUpdate, err := q.GetLatestDepositUpdate(
144+
ctx, deposit.DepositID,
145+
)
146+
if err != nil {
147+
return err
148+
}
149+
150+
d, err := s.toDeposit(deposit, latestUpdate)
151+
if err != nil {
152+
return err
153+
}
154+
155+
allDeposits = append(allDeposits, d)
156+
}
157+
158+
return nil
159+
})
160+
if err != nil {
161+
return nil, err
162+
}
163+
164+
return allDeposits, nil
165+
}
166+
167+
// toDeposit converts an sql deposit to a deposit.
168+
func (s *SqlStore) toDeposit(row sqlc.Deposit,
169+
lastUpdate sqlc.DepositUpdate) (*Deposit, error) {
170+
171+
id := ID{}
172+
err := id.FromByteSlice(row.DepositID)
173+
if err != nil {
174+
return nil, err
175+
}
176+
177+
var txHash *chainhash.Hash
178+
if row.TxHash != nil {
179+
txHash, err = chainhash.NewHash(row.TxHash)
180+
if err != nil {
181+
return nil, err
182+
}
183+
}
184+
185+
return &Deposit{
186+
ID: id,
187+
State: fsm.StateType(lastUpdate.UpdateState),
188+
OutPoint: wire.OutPoint{
189+
Hash: *txHash,
190+
Index: uint32(row.OutIndex),
191+
},
192+
Value: btcutil.Amount(row.Amount),
193+
ConfirmationHeight: row.ConfirmationHeight,
194+
TimeOutSweepPkScript: row.TimeoutSweepPkScript,
195+
}, nil
196+
}
197+
198+
// Close closes the database connection.
199+
func (s *SqlStore) Close() {
200+
s.baseDB.DB.Close()
201+
}

0 commit comments

Comments
 (0)