diff --git a/finality-provider/service/app_test.go b/finality-provider/service/app_test.go index 49bdeb46..f5aa1e93 100644 --- a/finality-provider/service/app_test.go +++ b/finality-provider/service/app_test.go @@ -4,7 +4,9 @@ import ( "math/rand" "os" "path/filepath" + "strings" "testing" + "time" bbntypes "github.com/babylonlabs-io/babylon/types" bstypes "github.com/babylonlabs-io/babylon/x/btcstaking/types" @@ -138,3 +140,61 @@ func FuzzRegisterFinalityProvider(f *testing.F) { require.Equal(t, true, fpInfo.IsRunning) }) } + +func FuzzSyncFinalityProviderStatus(f *testing.F) { + testutil.AddRandomSeedsToFuzzer(f, 10) + f.Fuzz(func(t *testing.T, seed int64) { + r := rand.New(rand.NewSource(2)) + + logger := zap.NewNop() + + // create an EOTS manager + eotsHomeDir := filepath.Join(t.TempDir(), "eots-home") + eotsCfg := eotscfg.DefaultConfigWithHomePath(eotsHomeDir) + dbBackend, err := eotsCfg.DatabaseConfig.GetDbBackend() + require.NoError(t, err) + em, err := eotsmanager.NewLocalEOTSManager(eotsHomeDir, eotsCfg.KeyringBackend, dbBackend, logger) + require.NoError(t, err) + defer func() { + dbBackend.Close() + err = os.RemoveAll(eotsHomeDir) + require.NoError(t, err) + }() + + // Create randomized config + fpHomeDir := filepath.Join(t.TempDir(), "fp-home") + fpCfg := config.DefaultConfigWithHome(fpHomeDir) + fpCfg.SyncFpStatusInterval = time.Millisecond * 100 + fpdb, err := fpCfg.DatabaseConfig.GetDbBackend() + require.NoError(t, err) + + randomStartingHeight := uint64(r.Int63n(100) + 1) + currentHeight := randomStartingHeight + uint64(r.Int63n(10)+2) + mockClientController := testutil.PrepareMockedClientController(t, r, randomStartingHeight, currentHeight) + + blkInfo := &types.BlockInfo{Height: currentHeight} + mockClientController.EXPECT().QueryLatestFinalizedBlocks(gomock.Any()).Return(nil, nil).AnyTimes() + mockClientController.EXPECT().QueryBestBlock().Return(blkInfo, nil).AnyTimes() + mockClientController.EXPECT().QueryFinalityProviderVotingPower(gomock.Any(), gomock.Any()).Return(uint64(2), nil).AnyTimes() + mockClientController.EXPECT().QueryBlock(gomock.Any()).Return(blkInfo, nil).AnyTimes() + + app, err := service.NewFinalityProviderApp(&fpCfg, mockClientController, em, fpdb, logger) + require.NoError(t, err) + + err = app.Start() + require.NoError(t, err) + defer func() { + err = app.Stop() + require.NoError(t, err) + }() + + fp := testutil.GenStoredFinalityProvider(r, t, app, passphrase, hdPath, nil) + + require.Eventually(t, func() bool { + fpInfo, err := app.GetFinalityProviderInfo(fp.GetBIP340BTCPK()) + require.NoError(t, err) + + return strings.EqualFold(fpInfo.Status, proto.FinalityProviderStatus_ACTIVE.String()) + }, time.Second*5, time.Millisecond*200, "should eventually become active") + }) +}