Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor account/watcher and add mocks/tests #316

Merged
merged 6 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 22 additions & 29 deletions account/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ type Manager struct {
started sync.Once
stopped sync.Once

cfg ManagerConfig
watcher *watcher.Watcher
cfg ManagerConfig
watcherCtrl watcher.Controller

// pendingBatchMtx guards access to any database calls involving pending
// batches. This is mostly used to prevent race conditions when handling
Expand All @@ -153,11 +153,10 @@ func NewManager(cfg *ManagerConfig) *Manager {
quit: make(chan struct{}),
}

m.watcher = watcher.New(&watcher.Config{
ChainNotifier: cfg.ChainNotifier,
HandleAccountConf: m.handleAccountConf,
HandleAccountSpend: m.handleAccountSpend,
HandleAccountExpiry: m.handleAccountExpiry,
m.watcherCtrl = watcher.NewController(&watcher.CtrlConfig{
ChainNotifier: cfg.ChainNotifier,
// The manager implements the EventHandler interface
Handlers: m,
})

return m
Expand All @@ -178,7 +177,7 @@ func (m *Manager) start() error {

// We'll start by resuming all of our accounts. This requires the
// watcher to be started first.
if err := m.watcher.Start(); err != nil {
if err := m.watcherCtrl.Start(); err != nil {
return err
}

Expand Down Expand Up @@ -229,7 +228,7 @@ func (m *Manager) start() error {
// Stop safely stops any ongoing operations within the Manager.
func (m *Manager) Stop() {
m.stopped.Do(func() {
m.watcher.Stop()
m.watcherCtrl.Stop()

close(m.quit)
m.wg.Wait()
Expand Down Expand Up @@ -382,8 +381,8 @@ func (m *Manager) WatchMatchedAccounts(ctx context.Context,
// canceling all previous spend and confirmation watchers. We
// then only watch the latest batch and once it confirms, create
// a new spend watcher on that.
m.watcher.CancelAccountSpend(matchedAccount)
m.watcher.CancelAccountConf(matchedAccount)
m.watcherCtrl.CancelAccountSpend(matchedAccount)
m.watcherCtrl.CancelAccountConf(matchedAccount)

// After taking part in a batch, the account is either pending
// closed because it was used up or pending batch update because
Expand Down Expand Up @@ -620,7 +619,7 @@ func (m *Manager) resumeAccount(ctx context.Context, account *Account, // nolint
)
log.Infof("Waiting for %v confirmation(s) of account %x",
numConfs, account.TraderKey.PubKey.SerializeCompressed())
err = m.watcher.WatchAccountConf(
err = m.watcherCtrl.WatchAccountConf(
account.TraderKey.PubKey, account.OutPoint.Hash,
accountOutput.PkScript, numConfs, account.HeightHint,
)
Expand Down Expand Up @@ -657,7 +656,7 @@ func (m *Manager) resumeAccount(ctx context.Context, account *Account, // nolint
)
log.Infof("Waiting for %v confirmation(s) of account %x",
numConfs, account.TraderKey.PubKey.SerializeCompressed())
err = m.watcher.WatchAccountConf(
err = m.watcherCtrl.WatchAccountConf(
account.TraderKey.PubKey, account.OutPoint.Hash,
accountOutput.PkScript, numConfs, account.HeightHint,
)
Expand Down Expand Up @@ -706,7 +705,7 @@ func (m *Manager) resumeAccount(ctx context.Context, account *Account, // nolint
log.Infof("Waiting for %v confirmation(s) of expired account %x",
numConfs, account.TraderKey.PubKey.SerializeCompressed())

err = m.watcher.WatchAccountConf(
err = m.watcherCtrl.WatchAccountConf(
account.TraderKey.PubKey, account.OutPoint.Hash,
accountOutput.PkScript, numConfs, account.HeightHint,
)
Expand All @@ -721,7 +720,7 @@ func (m *Manager) resumeAccount(ctx context.Context, account *Account, // nolint
log.Infof("Watching expired account %x for spend",
account.TraderKey.PubKey.SerializeCompressed())

err = m.watcher.WatchAccountSpend(
err = m.watcherCtrl.WatchAccountSpend(
account.TraderKey.PubKey, account.OutPoint,
accountOutput.PkScript, account.HeightHint,
)
Expand All @@ -745,7 +744,7 @@ func (m *Manager) resumeAccount(ctx context.Context, account *Account, // nolint

log.Infof("Watching account %x for spend",
account.TraderKey.PubKey.SerializeCompressed())
err = m.watcher.WatchAccountSpend(
err = m.watcherCtrl.WatchAccountSpend(
account.TraderKey.PubKey, account.OutPoint,
accountOutput.PkScript, account.HeightHint,
)
Expand Down Expand Up @@ -836,20 +835,17 @@ func (m *Manager) handleStateOpen(ctx context.Context, account *Account) error {
return err
}

err = m.watcher.WatchAccountSpend(
err = m.watcherCtrl.WatchAccountSpend(
account.TraderKey.PubKey, account.OutPoint,
accountOutput.PkScript, account.HeightHint,
)
if err != nil {
return fmt.Errorf("unable to watch for spend: %v", err)
}

err = m.watcher.WatchAccountExpiration(
m.watcherCtrl.WatchAccountExpiration(
account.TraderKey.PubKey, account.Expiry,
)
if err != nil {
return fmt.Errorf("unable to watch for expiration: %v", err)
}

// Now that we have an open account, subscribe for updates to it to the
// server. We subscribe for the account instead of the individual orders
Expand All @@ -866,9 +862,9 @@ func (m *Manager) handleStateOpen(ctx context.Context, account *Account) error {
return nil
}

// handleAccountConf takes the necessary steps after detecting the confirmation
// HandleAccountConf takes the necessary steps after detecting the confirmation
// of an account on-chain.
func (m *Manager) handleAccountConf(traderKey *btcec.PublicKey,
func (m *Manager) HandleAccountConf(traderKey *btcec.PublicKey,
confDetails *chainntnfs.TxConfirmation) error {

account, err := m.cfg.Store.Account(traderKey)
Expand Down Expand Up @@ -919,7 +915,7 @@ func (m *Manager) handleAccountConf(traderKey *btcec.PublicKey,
// only track the spend of the latest batch, after it confirmed. So the account
// output in the spend transaction should always match our database state if
// it was a cooperative spend.
func (m *Manager) handleAccountSpend(traderKey *btcec.PublicKey,
func (m *Manager) HandleAccountSpend(traderKey *btcec.PublicKey,
positiveblue marked this conversation as resolved.
Show resolved Hide resolved
spendDetails *chainntnfs.SpendDetail) error {

account, err := m.cfg.Store.Account(traderKey)
Expand Down Expand Up @@ -1016,7 +1012,7 @@ func (m *Manager) handleAccountSpend(traderKey *btcec.PublicKey,
}

// handleAccountExpiry marks an account as expired within the database.
func (m *Manager) handleAccountExpiry(traderKey *btcec.PublicKey,
func (m *Manager) HandleAccountExpiry(traderKey *btcec.PublicKey,
height uint32) error {

account, err := m.cfg.Store.Account(traderKey)
Expand Down Expand Up @@ -1253,10 +1249,7 @@ func (m *Manager) RenewAccount(ctx context.Context,

// Begin to track the new account expiration, which will overwrite the
// existing expiration request.
err = m.watcher.WatchAccountExpiration(traderKey, modifiedAccount.Expiry)
if err != nil {
return nil, nil, err
}
m.watcherCtrl.WatchAccountExpiration(traderKey, modifiedAccount.Expiry)

return modifiedAccount, spendPkg.tx, nil
}
Expand Down
Loading