Skip to content

Commit

Permalink
chore: add test to check syncChainFpStatusLoop actually modified the …
Browse files Browse the repository at this point in the history
…fp status to Active if it has VP on chain
  • Loading branch information
RafilxTenfen committed Sep 18, 2024
1 parent 95d20c7 commit 93eacf4
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions finality-provider/service/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
})
}

0 comments on commit 93eacf4

Please sign in to comment.