wtclient: refactor client creation after database calls

This commit is contained in:
Wilmer Paulino 2019-06-07 17:45:42 -07:00
parent 76f9c9bd52
commit 159883665d
No known key found for this signature in database
GPG Key ID: 6DF57B9F9514972F

@ -202,11 +202,51 @@ func New(config *Config) (*TowerClient, error) {
candidates := newTowerListIterator(tower) candidates := newTowerListIterator(tower)
targetTowerIDs := candidates.TowerIDs() targetTowerIDs := candidates.TowerIDs()
// Next, load all active sessions from the db into the client. We will
// use any of these session if their policies match the current policy
// of the client, otherwise they will be ignored and new sessions will
// be requested.
sessions, err := cfg.DB.ListClientSessions()
if err != nil {
return nil, err
}
// Reload any towers from disk using the tower IDs contained in each
// candidate session. We will also rederive any session keys needed to
// be able to communicate with the towers and authenticate session
// requests. This prevents us from having to store the private keys on
// disk.
for _, s := range sessions {
tower, err := cfg.DB.LoadTower(s.TowerID)
if err != nil {
return nil, err
}
sessionPriv, err := DeriveSessionKey(
cfg.SecretKeyRing, s.KeyIndex,
)
if err != nil {
return nil, err
}
s.Tower = tower
s.SessionPrivKey = sessionPriv
}
// Load the sweep pkscripts that have been generated for all previously
// registered channels.
chanSummaries, err := cfg.DB.FetchChanSummaries()
if err != nil {
return nil, err
}
c := &TowerClient{ c := &TowerClient{
cfg: cfg, cfg: cfg,
pipeline: newTaskPipeline(), pipeline: newTaskPipeline(),
candidateSessions: sessions,
activeSessions: make(sessionQueueSet), activeSessions: make(sessionQueueSet),
targetTowerIDs: targetTowerIDs, targetTowerIDs: targetTowerIDs,
summaries: chanSummaries,
statTicker: time.NewTicker(DefaultStatInterval), statTicker: time.NewTicker(DefaultStatInterval),
forceQuit: make(chan struct{}), forceQuit: make(chan struct{}),
} }
@ -223,48 +263,10 @@ func New(config *Config) (*TowerClient, error) {
MaxBackoff: cfg.MaxBackoff, MaxBackoff: cfg.MaxBackoff,
}) })
// Next, load all active sessions from the db into the client. We will
// use any of these session if their policies match the current policy
// of the client, otherwise they will be ignored and new sessions will
// be requested.
c.candidateSessions, err = c.cfg.DB.ListClientSessions()
if err != nil {
return nil, err
}
// Reload any towers from disk using the tower IDs contained in each
// candidate session. We will also rederive any session keys needed to
// be able to communicate with the towers and authenticate session
// requests. This prevents us from having to store the private keys on
// disk.
for _, s := range c.candidateSessions {
tower, err := c.cfg.DB.LoadTower(s.TowerID)
if err != nil {
return nil, err
}
sessionPriv, err := DeriveSessionKey(
c.cfg.SecretKeyRing, s.KeyIndex,
)
if err != nil {
return nil, err
}
s.Tower = tower
s.SessionPrivKey = sessionPriv
}
// Reconstruct the highest commit height processed for each channel // Reconstruct the highest commit height processed for each channel
// under the client's current policy. // under the client's current policy.
c.buildHighestCommitHeights() c.buildHighestCommitHeights()
// Finally, load the sweep pkscripts that have been generated for all
// previously registered channels.
c.summaries, err = c.cfg.DB.FetchChanSummaries()
if err != nil {
return nil, err
}
return c, nil return c, nil
} }