Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry committed Sep 30, 2024
1 parent bd2b15b commit 688470b
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions finality-provider/service/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
}

0 comments on commit 688470b

Please sign in to comment.