watchtower/wtclient/interface: add LoadTower and mock impl

This commit is contained in:
Conner Fromknecht 2019-03-27 16:50:59 -07:00
parent 1c22474ad3
commit 7d99005dde
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7
4 changed files with 33 additions and 1 deletions

@ -221,6 +221,17 @@ func New(config *Config) (*TowerClient, error) {
return nil, err
}
// Reload any towers from disk using the tower IDs contained in each
// candidate session.
for _, s := range c.candidateSessions {
tower, err := c.cfg.DB.LoadTower(s.TowerID)
if err != nil {
return nil, err
}
s.Tower = tower
}
// Finally, load the sweep pkscripts that have been generated for all
// previously registered channels.
c.sweepPkScripts, err = c.cfg.DB.FetchChanPkScripts()

@ -19,6 +19,9 @@ type DB interface {
// sessions.
CreateTower(*lnwire.NetAddress) (*wtdb.Tower, error)
// LoadTower retrieves a tower by its tower ID.
LoadTower(uint64) (*wtdb.Tower, error)
// CreateClientSession saves a newly negotiated client session to the
// client's database. This enables the session to be used across
// restarts.

@ -1,6 +1,7 @@
package wtdb
import (
"errors"
"net"
"sync"
@ -8,6 +9,12 @@ import (
"github.com/lightningnetwork/lnd/lnwire"
)
var (
// ErrTowerNotFound signals that the target tower was not found in the
// database.
ErrTowerNotFound = errors.New("tower not found")
)
// Tower holds the necessary components required to connect to a remote tower.
// Communication is handled by brontide, and requires both a public key and an
// address.

@ -64,6 +64,18 @@ func (m *ClientDB) CreateTower(lnAddr *lnwire.NetAddress) (*wtdb.Tower, error) {
return tower, nil
}
// LoadTower retrieves a tower by its tower ID.
func (m *ClientDB) LoadTower(towerID uint64) (*wtdb.Tower, error) {
m.mu.Lock()
defer m.mu.Unlock()
if tower, ok := m.towers[towerID]; ok {
return tower, nil
}
return nil, wtdb.ErrTowerNotFound
}
// MarkBackupIneligible records that particular commit height is ineligible for
// backup. This allows the client to track which updates it should not attempt
// to retry after startup.
@ -92,7 +104,6 @@ func (m *ClientDB) CreateClientSession(session *wtdb.ClientSession) error {
m.activeSessions[session.ID] = &wtdb.ClientSession{
TowerID: session.TowerID,
Tower: session.Tower,
SessionKeyDesc: session.SessionKeyDesc,
SessionPrivKey: session.SessionPrivKey,
ID: session.ID,