Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: HandleRewarding gaps of unfinalized blocks #378

Merged
merged 5 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

- [#374](https://github.com/babylonlabs-io/babylon/pull/374) Fix non-consecutive finalization
of the block in `TallyBlocks` function
- [#378](https://github.com/babylonlabs-io/babylon/pull/378) Fix give out rewards
with gaps of unfinalized blocks

## v1.0.0-rc2

Expand Down
6 changes: 3 additions & 3 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 All @@ -29,10 +30,9 @@ func (k Keeper) HandleRewarding(ctx context.Context, targetHeight int64) {
if err != nil {
panic(err)
}
if !block.Finalized {
break
if block.Finalized {
k.rewardBTCStaking(ctx, height)
}
k.rewardBTCStaking(ctx, height)
nextHeightToReward = height + 1
}

Expand Down
55 changes: 55 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,57 @@ 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,
RafilxTenfen marked this conversation as resolved.
Show resolved Hide resolved
})
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)

iKeeper.EXPECT().
RewardBTCStaking(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return().
Times(1) // number of finalized blocks processed

fKeeper.HandleRewarding(ctx, 3)

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