Skip to content

Commit

Permalink
remove support for multiple fps running
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry committed Oct 8, 2024
1 parent 8080c4c commit 01040a2
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 304 deletions.
32 changes: 14 additions & 18 deletions finality-provider/cmd/fpd/daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,35 +120,31 @@ func startApp(
fpApp *service.FinalityProviderApp,
fpPkStr, passphrase string,
) error {
// only start the app without starting any finality-provider instance
// as there might be no finality-provider registered yet
// only start the app without starting any finality provider instance
// this is needed for new finality provider registration or unjailing
// finality providers
if err := fpApp.Start(); err != nil {
return fmt.Errorf("failed to start the finality-provider app: %w", err)
return fmt.Errorf("failed to start the finality provider app: %w", err)
}

if fpPkStr != "" {
// start the finality-provider instance with the given public key
fpPk, err := types.NewBIP340PubKeyFromHex(fpPkStr)
if err != nil {
return fmt.Errorf("invalid finality-provider public key %s: %w", fpPkStr, err)
}
// no fp instance will be started if public key is not specified
if fpPkStr == "" {
return nil
}

if err := fpApp.StartHandlingFinalityProvider(fpPk, passphrase); err != nil {
if errors.Is(err, service.ErrFinalityProviderJailed) {
fpApp.Logger().Error("failed to start finality provider", zap.Error(err))
// do not return error as we still want the service to start
return nil
}
return fmt.Errorf("failed to start the finality-provider instance %s: %w", fpPkStr, err)
}
// start the finality-provider instance with the given public key
fpPk, err := types.NewBIP340PubKeyFromHex(fpPkStr)
if err != nil {
return fmt.Errorf("invalid finality provider public key %s: %w", fpPkStr, err)
}

if err := fpApp.StartHandlingAll(); err != nil {
if err := fpApp.StartHandlingFinalityProvider(fpPk, passphrase); err != nil {
if errors.Is(err, service.ErrFinalityProviderJailed) {
fpApp.Logger().Error("failed to start finality provider", zap.Error(err))
// do not return error as we still want the service to start
return nil
}
return fmt.Errorf("failed to start the finality-provider instance %s: %w", fpPkStr, err)
}

return nil
Expand Down
3 changes: 0 additions & 3 deletions finality-provider/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const (
defaultMaxSubmissionRetries = 20
defaultBitcoinNetwork = "signet"
defaultDataDirname = "data"
defaultMaxNumFinalityProviders = 3
)

var (
Expand Down Expand Up @@ -69,7 +68,6 @@ type Config struct {
FastSyncLimit uint64 `long:"fastsynclimit" description:"The maximum number of blocks to catch up for each fast sync"`
FastSyncGap uint64 `long:"fastsyncgap" description:"The block gap that will trigger the fast sync"`
EOTSManagerAddress string `long:"eotsmanageraddress" description:"The address of the remote EOTS manager; Empty if the EOTS manager is running locally"`
MaxNumFinalityProviders uint32 `long:"maxnumfinalityproviders" description:"The maximum number of finality-provider instances running concurrently within the daemon"`
SyncFpStatusInterval time.Duration `long:"syncfpstatusinterval" description:"The duration of time that it should sync FP status with the client blockchain"`

BitcoinNetwork string `long:"bitcoinnetwork" description:"Bitcoin network to run on" choise:"mainnet" choice:"regtest" choice:"testnet" choice:"simnet" choice:"signet"`
Expand Down Expand Up @@ -112,7 +110,6 @@ func DefaultConfigWithHome(homePath string) Config {
BTCNetParams: defaultBTCNetParams,
EOTSManagerAddress: defaultEOTSManagerAddress,
RpcListener: DefaultRpcListener,
MaxNumFinalityProviders: defaultMaxNumFinalityProviders,
Metrics: metrics.DefaultFpConfig(),
SyncFpStatusInterval: defaultSyncFpStatusInterval,
}
Expand Down
22 changes: 8 additions & 14 deletions finality-provider/service/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,6 @@ func (app *FinalityProviderApp) Logger() *zap.Logger {
return app.logger
}

func (app *FinalityProviderApp) ListFinalityProviderInstances() []*FinalityProviderInstance {
return app.fpManager.ListFinalityProviderInstances()
}

func (app *FinalityProviderApp) ListAllFinalityProvidersInfo() ([]*proto.FinalityProviderInfo, error) {
return app.fpManager.AllFinalityProviders()
}
Expand All @@ -164,8 +160,8 @@ func (app *FinalityProviderApp) GetFinalityProviderInfo(fpPk *bbntypes.BIP340Pub
}

// GetFinalityProviderInstance returns the finality-provider instance with the given Babylon public key
func (app *FinalityProviderApp) GetFinalityProviderInstance(fpPk *bbntypes.BIP340PubKey) (*FinalityProviderInstance, error) {
return app.fpManager.GetFinalityProviderInstance(fpPk)
func (app *FinalityProviderApp) GetFinalityProviderInstance() (*FinalityProviderInstance, error) {
return app.fpManager.GetFinalityProviderInstance()
}

func (app *FinalityProviderApp) RegisterFinalityProvider(fpPkStr string) (*RegisterFinalityProviderResponse, error) {
Expand Down Expand Up @@ -220,16 +216,12 @@ func (app *FinalityProviderApp) RegisterFinalityProvider(fpPkStr string) (*Regis
}
}

// StartHandlingFinalityProvider starts a finality-provider instance with the given Babylon public key
// StartHandlingFinalityProvider starts a finality provider instance with the given EOTS public key
// Note: this should be called right after the finality-provider is registered
func (app *FinalityProviderApp) StartHandlingFinalityProvider(fpPk *bbntypes.BIP340PubKey, passphrase string) error {
return app.fpManager.StartFinalityProvider(fpPk, passphrase)
}

func (app *FinalityProviderApp) StartHandlingAll() error {
return app.fpManager.StartAll()
}

// NOTE: this is not safe in production, so only used for testing purpose
func (app *FinalityProviderApp) getFpPrivKey(fpPk []byte) (*btcec.PrivateKey, error) {
record, err := app.eotsManager.KeyRecord(fpPk, "")
Expand Down Expand Up @@ -329,9 +321,11 @@ func (app *FinalityProviderApp) Stop() error {
app.wg.Wait()

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

app.logger.Debug("Stopping EOTS manager")
Expand Down
4 changes: 2 additions & 2 deletions finality-provider/service/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func FuzzRegisterFinalityProvider(f *testing.F) {
err = app.StartHandlingFinalityProvider(fp.GetBIP340BTCPK(), passphrase)
require.NoError(t, err)

fpAfterReg, err := app.GetFinalityProviderInstance(fp.GetBIP340BTCPK())
fpAfterReg, err := app.GetFinalityProviderInstance()
require.NoError(t, err)
require.Equal(t, proto.FinalityProviderStatus_REGISTERED, fpAfterReg.GetStoreFinalityProvider().Status)

Expand Down Expand Up @@ -210,7 +210,7 @@ func FuzzSyncFinalityProviderStatus(f *testing.F) {
if noVotingPowerTable {
expectedStatus = proto.FinalityProviderStatus_REGISTERED
}
fpInstance, err := app.GetFinalityProviderInstance(fpPk)
fpInstance, err := app.GetFinalityProviderInstance()
if err != nil {
return false
}
Expand Down
7 changes: 3 additions & 4 deletions finality-provider/service/fp_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,11 @@ func NewFinalityProviderInstance(
) (*FinalityProviderInstance, error) {
sfp, err := s.GetFinalityProvider(fpPk.MustToBTCPK())
if err != nil {
return nil, fmt.Errorf("failed to retrive the finality-provider %s from DB: %w", fpPk.MarshalHex(), err)
return nil, fmt.Errorf("failed to retrive the finality provider %s from DB: %w", fpPk.MarshalHex(), err)
}

// ensure the finality-provider has been registered
if sfp.Status < proto.FinalityProviderStatus_REGISTERED {
return nil, fmt.Errorf("the finality-provider %s has not been registered", sfp.KeyName)
if !sfp.ShouldStart() {
return nil, fmt.Errorf("the finality provider instance cannot be initiated with status %s", sfp.Status.String())
}

return &FinalityProviderInstance{
Expand Down
3 changes: 1 addition & 2 deletions finality-provider/service/fp_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/babylonlabs-io/babylon/testutil/datagen"
ftypes "github.com/babylonlabs-io/babylon/x/finality/types"

"github.com/babylonlabs-io/finality-provider/clientcontroller"
"github.com/babylonlabs-io/finality-provider/eotsmanager"
eotscfg "github.com/babylonlabs-io/finality-provider/eotsmanager/config"
Expand Down Expand Up @@ -120,8 +121,6 @@ func startFinalityProviderAppWithRegisteredFp(t *testing.T, r *rand.Rand, cc cli
require.NoError(t, err)
err = app.Start()
require.NoError(t, err)
err = app.StartHandlingAll()
require.NoError(t, err)

// create registered finality-provider
fp := testutil.GenStoredFinalityProvider(r, t, app, passphrase, hdPath, nil)
Expand Down
Loading

0 comments on commit 01040a2

Please sign in to comment.