wtclient+lnrpc: move LookupTower to Manager

This commit is contained in:
Elle Mouton 2023-05-16 14:11:43 +02:00
parent 0b3d751e33
commit 4e51bf3a3f
No known key found for this signature in database
GPG Key ID: D7D916376026F177
3 changed files with 62 additions and 38 deletions

View File

@ -324,40 +324,42 @@ func (c *WatchtowerClient) GetTowerInfo(ctx context.Context,
req.IncludeSessions, req.ExcludeExhaustedSessions, req.IncludeSessions, req.ExcludeExhaustedSessions,
) )
// Get the tower and its sessions from anchors client. towersPerBlobType, err := c.cfg.ClientMgr.LookupTower(pubKey, opts...)
tower, err := c.cfg.AnchorClient.LookupTower(pubKey, opts...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var resTower *Tower
for blobType, tower := range towersPerBlobType {
policyType := PolicyType_LEGACY
if blobType.IsAnchorChannel() {
policyType = PolicyType_ANCHOR
}
rpcTower := marshallTower( rpcTower := marshallTower(
tower, PolicyType_ANCHOR, req.IncludeSessions, ackCounts, tower, policyType, req.IncludeSessions,
committedUpdateCounts, ackCounts, committedUpdateCounts,
) )
// Get the tower and its sessions from legacy client. if resTower == nil {
tower, err = c.cfg.Client.LookupTower(pubKey, opts...) resTower = rpcTower
if err != nil { continue
return nil, err
} }
rpcLegacyTower := marshallTower( if !bytes.Equal(rpcTower.Pubkey, resTower.Pubkey) {
tower, PolicyType_LEGACY, req.IncludeSessions, ackCounts, return nil, fmt.Errorf("tower clients returned " +
committedUpdateCounts,
)
if !bytes.Equal(rpcTower.Pubkey, rpcLegacyTower.Pubkey) {
return nil, fmt.Errorf("legacy and anchor clients returned " +
"inconsistent results for the given tower") "inconsistent results for the given tower")
} }
rpcTower.SessionInfo = append( resTower.SessionInfo = append(
rpcTower.SessionInfo, rpcLegacyTower.SessionInfo..., resTower.SessionInfo, rpcTower.SessionInfo...,
) )
rpcTower.Sessions = append( resTower.Sessions = append(
rpcTower.Sessions, rpcLegacyTower.Sessions..., resTower.Sessions, rpcTower.Sessions...,
) )
}
return rpcTower, nil return resTower, nil
} }
// constructFunctionalOptions is a helper function that constructs a list of // constructFunctionalOptions is a helper function that constructs a list of

View File

@ -95,10 +95,6 @@ type RegisteredTower struct {
// Client is the primary interface used by the daemon to control a client's // Client is the primary interface used by the daemon to control a client's
// lifecycle and backup revoked states. // lifecycle and backup revoked states.
type Client interface { type Client interface {
// LookupTower retrieves a registered watchtower through its public key.
LookupTower(*btcec.PublicKey,
...wtdb.ClientSessionListOption) (*RegisteredTower, error)
// Policy returns the active client policy configuration. // Policy returns the active client policy configuration.
Policy() wtpolicy.Policy Policy() wtpolicy.Policy
@ -1600,15 +1596,11 @@ func (c *TowerClient) registeredTowers(towers []*wtdb.Tower,
return registeredTowers, nil return registeredTowers, nil
} }
// LookupTower retrieves a registered watchtower through its public key. // lookupTower retrieves the info of sessions held with the given tower handled
func (c *TowerClient) LookupTower(pubKey *btcec.PublicKey, // by this client.
func (c *TowerClient) lookupTower(tower *wtdb.Tower,
opts ...wtdb.ClientSessionListOption) (*RegisteredTower, error) { opts ...wtdb.ClientSessionListOption) (*RegisteredTower, error) {
tower, err := c.cfg.DB.LoadTower(pubKey)
if err != nil {
return nil, err
}
opts = append(opts, wtdb.WithPreEvalFilterFn(c.genSessionFilter(false))) opts = append(opts, wtdb.WithPreEvalFilterFn(c.genSessionFilter(false)))
towerSessions, err := c.cfg.DB.ListClientSessions(&tower.ID, opts...) towerSessions, err := c.cfg.DB.ListClientSessions(&tower.ID, opts...)

View File

@ -43,6 +43,10 @@ type TowerClientManager interface {
// type. // type.
RegisteredTowers(opts ...wtdb.ClientSessionListOption) ( RegisteredTowers(opts ...wtdb.ClientSessionListOption) (
map[blob.Type][]*RegisteredTower, error) map[blob.Type][]*RegisteredTower, error)
// LookupTower retrieves a registered watchtower through its public key.
LookupTower(*btcec.PublicKey, ...wtdb.ClientSessionListOption) (
map[blob.Type]*RegisteredTower, error)
} }
// Config provides the TowerClient with access to the resources it requires to // Config provides the TowerClient with access to the resources it requires to
@ -354,3 +358,29 @@ func (m *Manager) RegisteredTowers(opts ...wtdb.ClientSessionListOption) (
return resp, nil return resp, nil
} }
// LookupTower retrieves a registered watchtower through its public key.
func (m *Manager) LookupTower(key *btcec.PublicKey,
opts ...wtdb.ClientSessionListOption) (map[blob.Type]*RegisteredTower,
error) {
tower, err := m.cfg.DB.LoadTower(key)
if err != nil {
return nil, err
}
m.clientsMu.Lock()
defer m.clientsMu.Unlock()
resp := make(map[blob.Type]*RegisteredTower)
for _, client := range m.clients {
tower, err := client.lookupTower(tower, opts...)
if err != nil {
return nil, err
}
resp[client.Policy().BlobType] = tower
}
return resp, nil
}