-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(adr-39): Refactor rewarding (#360)
Implements babylonlabs-io/pm#145 including: - Rewards are calculated with a timeout `finality_sig_timeout` - Rewards should not be assigned to finality providers that have not voted for the block when calculating rewards.
- Loading branch information
Showing
15 changed files
with
312 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package keeper | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/babylonlabs-io/babylon/x/finality/types" | ||
) | ||
|
||
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 | ||
// every finalized block will be processed to reward | ||
nextHeightToReward := k.GetNextHeightToReward(ctx) | ||
if nextHeightToReward == 0 { | ||
// first time to call reward, set it to activated height | ||
activatedHeight, err := k.GetBTCStakingActivatedHeight(ctx) | ||
if err != nil { | ||
panic(err) | ||
} | ||
nextHeightToReward = activatedHeight | ||
} | ||
copiedNextHeightToReward := nextHeightToReward | ||
|
||
for height := nextHeightToReward; height <= uint64(targetHeight); height++ { | ||
block, err := k.GetBlock(ctx, height) | ||
if err != nil { | ||
panic(err) | ||
} | ||
if !block.Finalized { | ||
break | ||
} | ||
k.rewardBTCStaking(ctx, height) | ||
nextHeightToReward = height + 1 | ||
} | ||
|
||
if nextHeightToReward != copiedNextHeightToReward { | ||
k.SetNextHeightToReward(ctx, nextHeightToReward) | ||
} | ||
} | ||
|
||
func (k Keeper) rewardBTCStaking(ctx context.Context, height uint64) { | ||
// distribute rewards to BTC staking stakeholders w.r.t. the voting power distribution cache | ||
dc := k.GetVotingPowerDistCache(ctx, height) | ||
if dc == nil { | ||
// failing to get a voting power distribution cache before distributing reward is a programming error | ||
panic(fmt.Errorf("voting power distribution cache not found at height %d", height)) | ||
} | ||
|
||
// get all the voters for the height | ||
voterBTCPKs := k.GetVoters(ctx, height) | ||
|
||
// reward active finality providers | ||
k.IncentiveKeeper.RewardBTCStaking(ctx, height, dc, voterBTCPKs) | ||
|
||
// remove reward distribution cache afterwards | ||
k.RemoveVotingPowerDistCache(ctx, height) | ||
} | ||
|
||
// SetNextHeightToReward sets the next height to reward as the given height | ||
func (k Keeper) SetNextHeightToReward(ctx context.Context, height uint64) { | ||
store := k.storeService.OpenKVStore(ctx) | ||
heightBytes := sdk.Uint64ToBigEndian(height) | ||
if err := store.Set(types.NextHeightToRewardKey, heightBytes); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// GetNextHeightToReward gets the next height to reward | ||
func (k Keeper) GetNextHeightToReward(ctx context.Context) uint64 { | ||
store := k.storeService.OpenKVStore(ctx) | ||
bz, err := store.Get(types.NextHeightToRewardKey) | ||
if err != nil { | ||
panic(err) | ||
} | ||
if bz == nil { | ||
return 0 | ||
} | ||
height := sdk.BigEndianToUint64(bz) | ||
return height | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/babylonlabs-io/babylon/testutil/datagen" | ||
keepertest "github.com/babylonlabs-io/babylon/testutil/keeper" | ||
"github.com/babylonlabs-io/babylon/x/finality/types" | ||
) | ||
|
||
func FuzzHandleRewarding(f *testing.F) { | ||
datagen.AddRandomSeedsToFuzzer(f, 10) | ||
|
||
f.Fuzz(func(t *testing.T, seed int64) { | ||
r := rand.New(rand.NewSource(seed)) | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
// Setup keepers | ||
bsKeeper := types.NewMockBTCStakingKeeper(ctrl) | ||
iKeeper := types.NewMockIncentiveKeeper(ctrl) | ||
cKeeper := types.NewMockCheckpointingKeeper(ctrl) | ||
fKeeper, ctx := keepertest.FinalityKeeper(t, bsKeeper, iKeeper, cKeeper) | ||
|
||
// Activate BTC staking protocol at a random height | ||
activatedHeight := datagen.RandomInt(r, 10) + 1 | ||
fpPK, err := datagen.GenRandomBIP340PubKey(r) | ||
require.NoError(t, err) | ||
fKeeper.SetVotingPower(ctx, fpPK.MustMarshal(), activatedHeight, 1) | ||
|
||
totalBlocks := uint64(10) | ||
targetHeight := activatedHeight + totalBlocks - 1 | ||
|
||
// First phase: Index blocks with none finalized | ||
for i := activatedHeight; i <= targetHeight; i++ { | ||
fKeeper.SetBlock(ctx, &types.IndexedBlock{ | ||
Height: i, | ||
AppHash: datagen.GenRandomByteArray(r, 32), | ||
Finalized: false, | ||
}) | ||
|
||
// Set voting power distribution cache for each height | ||
dc := types.NewVotingPowerDistCache() | ||
dc.AddFinalityProviderDistInfo(&types.FinalityProviderDistInfo{ | ||
BtcPk: fpPK, | ||
TotalBondedSat: 1, | ||
}) | ||
fKeeper.SetVotingPowerDistCache(ctx, i, dc) | ||
} | ||
|
||
// First call to HandleRewarding - expect no rewards | ||
ctx = datagen.WithCtxHeight(ctx, targetHeight) | ||
fKeeper.HandleRewarding(ctx, int64(targetHeight)) | ||
|
||
nextHeight := fKeeper.GetNextHeightToReward(ctx) | ||
require.Equal(t, uint64(0), nextHeight, | ||
"next height is not updated when no blocks finalized") | ||
|
||
// Second phase: Finalize some blocks | ||
firstBatchFinalized := datagen.RandomInt(r, 5) + 1 | ||
for i := activatedHeight; i < activatedHeight+firstBatchFinalized; i++ { | ||
block, err := fKeeper.GetBlock(ctx, i) | ||
require.NoError(t, err) | ||
block.Finalized = true | ||
fKeeper.SetBlock(ctx, block) | ||
} | ||
|
||
// Expect rewards for first batch of finalized blocks | ||
iKeeper.EXPECT(). | ||
RewardBTCStaking(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()). | ||
Return(). | ||
Times(int(firstBatchFinalized)) | ||
|
||
// Second call to HandleRewarding | ||
fKeeper.HandleRewarding(ctx, int64(targetHeight)) | ||
|
||
nextHeight = fKeeper.GetNextHeightToReward(ctx) | ||
expectedNextHeight := activatedHeight + firstBatchFinalized | ||
require.Equal(t, expectedNextHeight, nextHeight, | ||
"next height should be after first batch of finalized blocks") | ||
|
||
// Third phase: Finalize more blocks | ||
secondBatchFinalized := datagen.RandomInt(r, int(totalBlocks-firstBatchFinalized)) + 1 | ||
for i := expectedNextHeight; i < expectedNextHeight+secondBatchFinalized; i++ { | ||
block, err := fKeeper.GetBlock(ctx, i) | ||
require.NoError(t, err) | ||
block.Finalized = true | ||
fKeeper.SetBlock(ctx, block) | ||
} | ||
|
||
// Expect rewards for second batch of finalized blocks | ||
iKeeper.EXPECT(). | ||
RewardBTCStaking(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()). | ||
Return(). | ||
Times(int(secondBatchFinalized)) | ||
|
||
// Final call to HandleRewarding | ||
fKeeper.HandleRewarding(ctx, int64(targetHeight)) | ||
|
||
// Verify final state | ||
finalNextHeight := fKeeper.GetNextHeightToReward(ctx) | ||
expectedFinalHeight := expectedNextHeight + secondBatchFinalized | ||
require.Equal(t, expectedFinalHeight, finalNextHeight, | ||
"next height should be after second batch of finalized blocks") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.