Skip to content

Commit e5b23c6

Browse files
committed
staticaddr: remove GetStaticAddressParameters from address manager
1 parent aaa7a96 commit e5b23c6

File tree

18 files changed

+120
-148
lines changed

18 files changed

+120
-148
lines changed

loopd/daemon.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,6 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
662662
LndClient: d.lnd.Client,
663663
InvoicesClient: d.lnd.Invoices,
664664
NodePubkey: d.lnd.NodePubkey,
665-
AddressManager: staticAddressManager,
666665
DepositManager: depositManager,
667666
Store: staticAddressLoopInStore,
668667
WalletKit: d.lnd.WalletKit,

loopd/swapclient_server.go

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,6 +1777,11 @@ func (s *swapClientServer) ListStaticAddressDeposits(ctx context.Context,
17771777
return nil, err
17781778
}
17791779

1780+
lndInfo, err := s.lnd.Client.GetInfo(ctx)
1781+
if err != nil {
1782+
return nil, err
1783+
}
1784+
17801785
// Deposits filtered by state or outpoints.
17811786
var filteredDeposits []*looprpc.Deposit
17821787
if len(outpoints) > 0 {
@@ -1788,7 +1793,7 @@ func (s *swapClientServer) ListStaticAddressDeposits(ctx context.Context,
17881793
}
17891794
return false
17901795
}
1791-
filteredDeposits = filter(allDeposits, network, f)
1796+
filteredDeposits = filter(allDeposits, network, lndInfo, f)
17921797

17931798
if len(outpoints) != len(filteredDeposits) {
17941799
return nil, fmt.Errorf("not all outpoints found in " +
@@ -1804,24 +1809,7 @@ func (s *swapClientServer) ListStaticAddressDeposits(ctx context.Context,
18041809

18051810
return d.IsInState(toServerState(req.StateFilter))
18061811
}
1807-
filteredDeposits = filter(allDeposits, network, f)
1808-
}
1809-
1810-
// Calculate the blocks until expiry for each deposit.
1811-
lndInfo, err := s.lnd.Client.GetInfo(ctx)
1812-
if err != nil {
1813-
return nil, err
1814-
}
1815-
1816-
bestBlockHeight := int64(lndInfo.BlockHeight)
1817-
params, err := s.staticAddressManager.GetStaticAddressParameters(ctx)
1818-
if err != nil {
1819-
return nil, err
1820-
}
1821-
for i := 0; i < len(filteredDeposits); i++ {
1822-
filteredDeposits[i].BlocksUntilExpiry =
1823-
filteredDeposits[i].ConfirmationHeight +
1824-
int64(params.Expiry) - bestBlockHeight
1812+
filteredDeposits = filter(allDeposits, network, lndInfo, f)
18251813
}
18261814

18271815
return &looprpc.ListStaticAddressDepositsResponse{
@@ -1908,13 +1896,6 @@ func (s *swapClientServer) ListStaticAddressSwaps(ctx context.Context,
19081896
return nil, err
19091897
}
19101898

1911-
addrParams, err := s.staticAddressManager.GetStaticAddressParameters(
1912-
ctx,
1913-
)
1914-
if err != nil {
1915-
return nil, err
1916-
}
1917-
19181899
// Fetch all deposits at once and index them by swap hash for a quick
19191900
// lookup.
19201901
allDeposits, err := s.depositManager.GetAllDeposits(ctx)
@@ -1953,7 +1934,7 @@ func (s *swapClientServer) ListStaticAddressSwaps(ctx context.Context,
19531934
for _, d := range ds {
19541935
state := toClientDepositState(d.GetState())
19551936
blocksUntilExpiry := d.ConfirmationHeight +
1956-
int64(addrParams.Expiry) -
1937+
int64(d.AddressParams.Expiry) -
19571938
int64(lndInfo.BlockHeight)
19581939

19591940
pd := &looprpc.Deposit{
@@ -2095,7 +2076,7 @@ func (s *swapClientServer) GetStaticAddressSummary(ctx context.Context,
20952076
}
20962077
}
20972078

2098-
deprecatedParams, err := s.staticAddressManager.GetStaticAddressParameters(ctx)
2079+
deprecatedParams, err := s.staticAddressManager.GetLegacyParameters(ctx)
20992080
if err != nil {
21002081
return nil, err
21012082
}
@@ -2193,7 +2174,7 @@ func (s *swapClientServer) StaticAddressLoopIn(ctx context.Context,
21932174
type filterFunc func(deposits *deposit.Deposit) bool
21942175

21952176
func filter(deposits []*deposit.Deposit, network *chaincfg.Params,
2196-
f filterFunc) []*looprpc.Deposit {
2177+
lndInfo *lndclient.Info, f filterFunc) []*looprpc.Deposit {
21972178

21982179
var clientDeposits []*looprpc.Deposit
21992180
for _, d := range deposits {
@@ -2219,6 +2200,9 @@ func filter(deposits []*deposit.Deposit, network *chaincfg.Params,
22192200
ConfirmationHeight: d.ConfirmationHeight,
22202201
SwapHash: swapHash,
22212202
StaticAddress: staticAddr,
2203+
BlocksUntilExpiry: d.ConfirmationHeight +
2204+
int64(d.AddressParams.Expiry) -
2205+
int64(lndInfo.BlockHeight),
22222206
}
22232207

22242208
clientDeposits = append(clientDeposits, deposit)

loopd/swapclient_server_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,12 @@ func (s *mockAddressStore) GetAllStaticAddresses(_ context.Context) (
957957
return s.params, nil
958958
}
959959

960+
func (s *mockAddressStore) GetLegacyParameters(_ context.Context) (
961+
*address.Parameters, error) {
962+
963+
return s.params[0], nil
964+
}
965+
960966
// mockDepositStore implements deposit.Store minimally for DepositsForOutpoints.
961967
type mockDepositStore struct {
962968
byOutpoint map[string]*deposit.Deposit

loopdb/sqlc/querier.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

loopdb/sqlc/queries/static_addresses.sql

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,10 @@ INSERT INTO static_addresses (
2424
$6,
2525
$7,
2626
$8
27-
);
27+
);
28+
29+
-- name: GetLegacyAddress :one
30+
SELECT * FROM static_addresses
31+
ORDER BY id ASC
32+
LIMIT 1;
33+

loopdb/sqlc/static_addresses.sql.go

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staticaddr/address/interface.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ type Store interface {
2525
GetStaticAddressID(ctx context.Context, pkScript []byte) (int32, error)
2626

2727
// GetAllStaticAddresses retrieves all static addresses from the store.
28-
GetAllStaticAddresses(ctx context.Context) ([]*Parameters,
29-
error)
28+
GetAllStaticAddresses(ctx context.Context) ([]*Parameters, error)
29+
30+
// GetLegacyParameters retrieves the parameters of the legacy static
31+
// address.
32+
GetLegacyParameters(ctx context.Context) (*Parameters, error)
3033
}
3134

3235
// Parameters hold all the necessary information for the 2-of-2 multisig

staticaddr/address/manager.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -348,20 +348,17 @@ func (m *Manager) ListUnspentRaw(ctx context.Context, minConfs,
348348
return resultList, nil
349349
}
350350

351-
// GetStaticAddressParameters returns the parameters of the static address.
352-
func (m *Manager) GetStaticAddressParameters(ctx context.Context) (*Parameters,
351+
// GetLegacyParameters returns the first address parameters that were created
352+
// under this L402.
353+
func (m *Manager) GetLegacyParameters(ctx context.Context) (*Parameters,
353354
error) {
354355

355-
params, err := m.cfg.Store.GetAllStaticAddresses(ctx)
356+
params, err := m.cfg.Store.GetLegacyParameters(ctx)
356357
if err != nil {
357358
return nil, err
358359
}
359360

360-
if len(params) == 0 {
361-
return nil, fmt.Errorf("no static address parameters found")
362-
}
363-
364-
return params[0], nil
361+
return params, nil
365362
}
366363

367364
// GetDefaultParameters returns the parameters of the static address.

staticaddr/address/sql_store.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ func (s *SqlStore) GetAllStaticAddresses(ctx context.Context) ([]*Parameters,
7474
return result, nil
7575
}
7676

77+
// GetLegacyParameters returns all address known to the server.
78+
func (s *SqlStore) GetLegacyParameters(ctx context.Context) (*Parameters,
79+
error) {
80+
81+
legacyAddress, err := s.baseDB.Queries.GetLegacyAddress(ctx)
82+
if err != nil {
83+
return nil, err
84+
}
85+
86+
res, err := s.toAddressParameters(legacyAddress)
87+
if err != nil {
88+
return nil, err
89+
}
90+
return res, nil
91+
}
92+
7793
// Close closes the database connection.
7894
func (s *SqlStore) Close() {
7995
s.baseDB.DB.Close()

staticaddr/deposit/manager_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ type mockAddressManager struct {
108108
mock.Mock
109109
}
110110

111-
func (m *mockAddressManager) GetStaticAddressParameters(ctx context.Context) (
111+
func (m *mockAddressManager) GetLegacyParameters(ctx context.Context) (
112112
*address.Parameters, error) {
113113

114114
args := m.Called(ctx)
@@ -326,7 +326,7 @@ func newManagerTestContext(t *testing.T) *ManagerTestContext {
326326
).Return(nil)
327327

328328
mockAddressManager.On(
329-
"GetStaticAddressParameters", mock.Anything,
329+
"GetLegacyParameters", mock.Anything,
330330
).Return(&address.Parameters{
331331
Expiry: defaultExpiry,
332332
}, nil)

0 commit comments

Comments
 (0)