Skip to content

Commit

Permalink
chore: add test that breaks handle rewarding with gaps of unfinalized…
Browse files Browse the repository at this point in the history
… blocks
  • Loading branch information
RafilxTenfen committed Jan 3, 2025
1 parent aaac0d4 commit 9c50495
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions x/finality/keeper/rewarding.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/babylonlabs-io/babylon/x/finality/types"
)

// HandleRewarding calls the reward to stakers if the block is finalized
func (k Keeper) HandleRewarding(ctx context.Context, targetHeight int64) {
// rewarding is executed in a range of [nextHeightToReward, heightToExamine]
// this is we don't know when a block will be finalized and we need ensure
Expand Down
50 changes: 50 additions & 0 deletions x/finality/keeper/rewarding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper_test
import (
"math/rand"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -108,3 +109,52 @@ func FuzzHandleRewarding(f *testing.F) {
"next height should be after second batch of finalized blocks")
})
}

func TestHandleRewardingWithGapsOfUnfinalizedBlocks(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

r := rand.New(rand.NewSource(time.Now().Unix()))

// Setup keepers
bsKeeper := types.NewMockBTCStakingKeeper(ctrl)
iKeeper := types.NewMockIncentiveKeeper(ctrl)
cKeeper := types.NewMockCheckpointingKeeper(ctrl)
fKeeper, ctx := keepertest.FinalityKeeper(t, bsKeeper, iKeeper, cKeeper)

fpPK, err := datagen.GenRandomBIP340PubKey(r)
require.NoError(t, err)
fKeeper.SetVotingPower(ctx, fpPK.MustMarshal(), 1, 1)

// starts rewarding at block 1
fKeeper.SetNextHeightToReward(ctx, 1)

fKeeper.SetBlock(ctx, &types.IndexedBlock{
Height: 1,
AppHash: datagen.GenRandomByteArray(r, 32),
Finalized: false,
})
fKeeper.SetBlock(ctx, &types.IndexedBlock{
Height: 2,
AppHash: datagen.GenRandomByteArray(r, 32),
Finalized: false,
})

// adds the latest finalized block
fKeeper.SetBlock(ctx, &types.IndexedBlock{
Height: 3,
AppHash: datagen.GenRandomByteArray(r, 32),
Finalized: true,
})
dc := types.NewVotingPowerDistCache()
dc.AddFinalityProviderDistInfo(&types.FinalityProviderDistInfo{
BtcPk: fpPK,
TotalBondedSat: 1,
})
fKeeper.SetVotingPowerDistCache(ctx, 3, dc)

fKeeper.HandleRewarding(ctx, 3)

actNextBlockToBeRewarded := fKeeper.GetNextHeightToReward(ctx)
require.Equal(t, 4, actNextBlockToBeRewarded)
}

0 comments on commit 9c50495

Please sign in to comment.