Skip to content

Commit

Permalink
fix e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry committed Oct 9, 2024
1 parent 72fb47f commit b3fd5af
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 42 deletions.
8 changes: 3 additions & 5 deletions finality-provider/service/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,9 @@ func (app *FinalityProviderApp) Stop() error {
app.wg.Wait()

app.logger.Debug("Stopping finality providers")
if app.fpManager.isStarted.Load() {
if err := app.fpManager.Stop(); err != nil {
stopErr = err
return
}
if err := app.fpManager.Stop(); err != nil {
stopErr = err
return
}

app.logger.Debug("Stopping EOTS manager")
Expand Down
69 changes: 33 additions & 36 deletions finality-provider/service/fp_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/avast/retry-go/v4"
bbntypes "github.com/babylonlabs-io/babylon/types"
"go.uber.org/atomic"
"go.uber.org/zap"

"github.com/babylonlabs-io/finality-provider/clientcontroller"
Expand All @@ -34,9 +33,9 @@ func (ce *CriticalError) Error() string {
// FinalityProviderManager is responsible to initiate and start the given finality
// provider instance, monitor its running status
type FinalityProviderManager struct {
isStarted *atomic.Bool
startOnce sync.Once
stopOnce sync.Once

// mutex to acess map of fp instances (fpIns)
wg sync.WaitGroup

fpIns *FinalityProviderInstance
Expand Down Expand Up @@ -67,7 +66,6 @@ func NewFinalityProviderManager(
) (*FinalityProviderManager, error) {
return &FinalityProviderManager{
criticalErrChan: make(chan *CriticalError),
isStarted: atomic.NewBool(false),
fps: fps,
pubRandStore: pubRandStore,
config: config,
Expand All @@ -88,32 +86,28 @@ func (fpm *FinalityProviderManager) monitorCriticalErr() {

var criticalErr *CriticalError

exitLoop := false
for !exitLoop {
for {
select {
case criticalErr = <-fpm.criticalErrChan:
fpi, err := fpm.GetFinalityProviderInstance()
if err != nil {
fpm.logger.Debug("the finality-provider instance is already shutdown",
zap.String("pk", criticalErr.fpBtcPk.MarshalHex()))

exitLoop = true
continue
}
if errors.Is(criticalErr.err, ErrFinalityProviderSlashed) {
fpm.setFinalityProviderSlashed(fpi)
fpm.logger.Debug("the finality-provider has been slashed",
zap.String("pk", criticalErr.fpBtcPk.MarshalHex()))

exitLoop = true
continue
}
if errors.Is(criticalErr.err, ErrFinalityProviderJailed) {
fpm.setFinalityProviderJailed(fpi)
fpm.logger.Debug("the finality-provider has been jailed",
zap.String("pk", criticalErr.fpBtcPk.MarshalHex()))

exitLoop = true
continue
}
fpm.logger.Fatal(instanceTerminatingMsg,
Expand All @@ -122,10 +116,6 @@ func (fpm *FinalityProviderManager) monitorCriticalErr() {
return
}
}

if err := fpm.Stop(); err != nil {
fpm.logger.Fatal("failed to stop the finality provider manager", zap.Error(err))
}
}

// monitorStatusUpdate periodically check the status of each managed finality providers and update
Expand Down Expand Up @@ -242,42 +232,49 @@ func (fpm *FinalityProviderManager) setFinalityProviderJailed(fpi *FinalityProvi
}

func (fpm *FinalityProviderManager) StartFinalityProvider(fpPk *bbntypes.BIP340PubKey, passphrase string) error {
if !fpm.isStarted.Load() {
fpm.isStarted.Store(true)

if err := fpm.startFinalityProviderInstance(fpPk, passphrase); err != nil {
return err
}

fpm.wg.Add(1)
fpm.startOnce.Do(func() {
fpm.wg.Add(2)
go fpm.monitorCriticalErr()

fpm.wg.Add(1)
go fpm.monitorStatusUpdate()
})

fpm.logger.Info("starting finality provider", zap.String("pk", fpPk.MarshalHex()))

if err := fpm.startFinalityProviderInstance(fpPk, passphrase); err != nil {
return err
}

fpm.logger.Info("finality provider is started", zap.String("pk", fpPk.MarshalHex()))

return nil
}

func (fpm *FinalityProviderManager) Stop() error {
if !fpm.isStarted.Swap(false) {
return fmt.Errorf("the finality-provider manager has already stopped")
}

defer func() {
var stopErr error
fpm.stopOnce.Do(func() {
close(fpm.quit)
fpm.wg.Wait()
}()

if fpm.fpIns == nil {
return nil
}
if fpm.fpIns == nil {
return
}

if !fpm.fpIns.IsRunning() {
return nil
}
if !fpm.fpIns.IsRunning() {
return
}

pkHex := fpm.fpIns.GetBtcPkHex()
fpm.logger.Info("stopping finality provider", zap.String("pk", pkHex))

if err := fpm.fpIns.Stop(); err != nil {
stopErr = err
return
}

fpm.logger.Info("finality provider is stopped", zap.String("pk", pkHex))
})

return fpm.fpIns.Stop()
return stopErr
}

func (fpm *FinalityProviderManager) GetFinalityProviderInstance() (*FinalityProviderInstance, error) {
Expand Down
4 changes: 3 additions & 1 deletion itest/test_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,9 @@ func StartManagerWithFinalityProvider(t *testing.T) (*TestManager, *service.Fina
}

func (tm *TestManager) Stop(t *testing.T) {
err := tm.manager.ClearResources()
err := tm.Fpa.Stop()
require.NoError(t, err)
err = tm.manager.ClearResources()
require.NoError(t, err)
err = os.RemoveAll(tm.baseDir)
require.NoError(t, err)
Expand Down

0 comments on commit b3fd5af

Please sign in to comment.