From ab2f781b4ad97d63b39e7b411d03c4f40140dd91 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Tue, 16 May 2023 14:15:09 +0200 Subject: [PATCH] wtclient+lnrpc: move Policy to Manager --- lnrpc/wtclientrpc/wtclient.go | 13 +++++++++---- watchtower/wtclient/client.go | 7 ++----- watchtower/wtclient/manager.go | 22 ++++++++++++++++++++-- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/lnrpc/wtclientrpc/wtclient.go b/lnrpc/wtclientrpc/wtclient.go index 9ee94977b..bf7f9a1da 100644 --- a/lnrpc/wtclientrpc/wtclient.go +++ b/lnrpc/wtclientrpc/wtclient.go @@ -16,9 +16,9 @@ import ( "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/watchtower" + "github.com/lightningnetwork/lnd/watchtower/blob" "github.com/lightningnetwork/lnd/watchtower/wtclient" "github.com/lightningnetwork/lnd/watchtower/wtdb" - "github.com/lightningnetwork/lnd/watchtower/wtpolicy" "google.golang.org/grpc" "gopkg.in/macaroon-bakery.v2/bakery" ) @@ -437,17 +437,22 @@ func (c *WatchtowerClient) Policy(ctx context.Context, return nil, err } - var policy wtpolicy.Policy + var blobType blob.Type switch req.PolicyType { case PolicyType_LEGACY: - policy = c.cfg.Client.Policy() + blobType = blob.TypeAltruistCommit case PolicyType_ANCHOR: - policy = c.cfg.AnchorClient.Policy() + blobType = blob.TypeAltruistAnchorCommit default: return nil, fmt.Errorf("unknown policy type: %v", req.PolicyType) } + policy, err := c.cfg.ClientMgr.Policy(blobType) + if err != nil { + return nil, err + } + return &PolicyResponse{ MaxUpdates: uint32(policy.MaxUpdates), SweepSatPerVbyte: uint32(policy.SweepFeeRate.FeePerVByte()), diff --git a/watchtower/wtclient/client.go b/watchtower/wtclient/client.go index d68be1afd..871960712 100644 --- a/watchtower/wtclient/client.go +++ b/watchtower/wtclient/client.go @@ -95,9 +95,6 @@ type RegisteredTower struct { // Client is the primary interface used by the daemon to control a client's // lifecycle and backup revoked states. type Client interface { - // Policy returns the active client policy configuration. - Policy() wtpolicy.Policy - // RegisterChannel persistently initializes any channel-dependent // parameters within the client. This should be called during link // startup to ensure that the client is able to support the link during @@ -1620,8 +1617,8 @@ func (c *TowerClient) getStats() ClientStats { return c.stats.getStatsCopy() } -// Policy returns the active client policy configuration. -func (c *TowerClient) Policy() wtpolicy.Policy { +// policy returns the active client policy configuration. +func (c *TowerClient) policy() wtpolicy.Policy { return c.cfg.Policy } diff --git a/watchtower/wtclient/manager.go b/watchtower/wtclient/manager.go index ba2cdb27d..61386548e 100644 --- a/watchtower/wtclient/manager.go +++ b/watchtower/wtclient/manager.go @@ -38,6 +38,9 @@ type TowerClientManager interface { // Stats returns the in-memory statistics of the client since startup. Stats() ClientStats + // Policy returns the active client policy configuration. + Policy(blob.Type) (wtpolicy.Policy, error) + // RegisteredTowers retrieves the list of watchtowers registered with // the client. It returns a set of registered towers per client policy // type. @@ -353,7 +356,7 @@ func (m *Manager) RegisteredTowers(opts ...wtdb.ClientSessionListOption) ( return nil, err } - resp[client.Policy().BlobType] = towers + resp[client.policy().BlobType] = towers } return resp, nil @@ -379,8 +382,23 @@ func (m *Manager) LookupTower(key *btcec.PublicKey, return nil, err } - resp[client.Policy().BlobType] = tower + resp[client.policy().BlobType] = tower } return resp, nil } + +// Policy returns the active client policy configuration for the client using +// the given blob type. +func (m *Manager) Policy(blobType blob.Type) (wtpolicy.Policy, error) { + m.clientsMu.Lock() + defer m.clientsMu.Unlock() + + var policy wtpolicy.Policy + client, ok := m.clients[blobType] + if !ok { + return policy, fmt.Errorf("no client for the given blob type") + } + + return client.policy(), nil +}