From 688470bb7197b106ca04f103976a77fc809799d0 Mon Sep 17 00:00:00 2001 From: Fangyu Gai Date: Mon, 30 Sep 2024 18:55:32 +0800 Subject: [PATCH] add test --- finality-provider/service/app_test.go | 67 +++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/finality-provider/service/app_test.go b/finality-provider/service/app_test.go index f5e3d39f..d4ccad71 100644 --- a/finality-provider/service/app_test.go +++ b/finality-provider/service/app_test.go @@ -222,3 +222,70 @@ func FuzzSyncFinalityProviderStatus(f *testing.F) { }, time.Second*5, time.Millisecond*200, "should eventually be registered or active") }) } + +func FuzzUnjailFinalityProvider(f *testing.F) { + testutil.AddRandomSeedsToFuzzer(f, 10) + f.Fuzz(func(t *testing.T, seed int64) { + r := rand.New(rand.NewSource(seed)) + + logger := zap.NewNop() + + pathSuffix := datagen.GenRandomHexStr(r, 10) + // create an EOTS manager + eotsHomeDir := filepath.Join(t.TempDir(), "eots-home", pathSuffix) + 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) + + // Create randomized config + fpHomeDir := filepath.Join(t.TempDir(), "fp-home", pathSuffix) + fpCfg := config.DefaultConfigWithHome(fpHomeDir) + // use shorter interval for the test to end faster + fpCfg.SyncFpStatusInterval = time.Millisecond * 10 + fpCfg.StatusUpdateInterval = time.Millisecond * 10 + fpCfg.SubmissionRetryInterval = time.Millisecond * 10 + 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().QueryLastCommittedPublicRand(gomock.Any(), uint64(1)).Return(nil, nil).AnyTimes() + mockClientController.EXPECT().QueryLatestFinalizedBlocks(gomock.Any()).Return(nil, nil).AnyTimes() + mockClientController.EXPECT().QueryBestBlock().Return(blkInfo, nil).Return(blkInfo, nil).AnyTimes() + mockClientController.EXPECT().QueryBlock(gomock.Any()).Return(nil, errors.New("chain not online")).AnyTimes() + + // set voting power to be positive so that the fp should eventually become ACTIVE + mockClientController.EXPECT().QueryFinalityProviderVotingPower(gomock.Any(), gomock.Any()).Return(uint64(0), nil).AnyTimes() + mockClientController.EXPECT().QueryActivatedHeight().Return(uint64(1), nil).AnyTimes() + mockClientController.EXPECT().QueryFinalityProviderSlashedOrJailed(gomock.Any()).Return(false, false, nil).AnyTimes() + + app, err := service.NewFinalityProviderApp(&fpCfg, mockClientController, em, fpdb, logger) + require.NoError(t, err) + + err = app.Start() + defer func() { + err := app.Stop() + require.NoError(t, err) + }() + require.NoError(t, err) + + fp := testutil.GenStoredFinalityProvider(r, t, app, "", hdPath, nil) + err = app.GetFinalityProviderStore().SetFpStatus(fp.BtcPk, proto.FinalityProviderStatus_JAILED) + require.NoError(t, err) + + expectedTxHash := datagen.GenRandomHexStr(r, 32) + mockClientController.EXPECT().UnjailFinalityProvider(fp.BtcPk).Return(&types.TxResponse{TxHash: expectedTxHash}, nil) + txHash, err := app.UnjailFinalityProvider(fp.GetBIP340BTCPK()) + require.NoError(t, err) + require.Equal(t, expectedTxHash, txHash) + fpInfo, err := app.GetFinalityProviderInfo(fp.GetBIP340BTCPK()) + require.NoError(t, err) + require.Equal(t, proto.FinalityProviderStatus_INACTIVE.String(), fpInfo.GetStatus()) + }) +}