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: slashed fp and BTC rewards tracker #371

Merged
merged 14 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## Unreleased

### Bug fixes

- [#371](https://github.com/babylonlabs-io/babylon/pull/371) Do not prune BTC
reward tracker structures at the slash of finality provider.

## v1.0.0-rc.1

### Improvements

- [#306](https://github.com/babylonlabs-io/babylon/pull/306) feat: improve BTC reward distribution with
Expand Down
139 changes: 104 additions & 35 deletions test/e2e/btc_rewards_distribution_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"github.com/babylonlabs-io/babylon/crypto/eots"
"github.com/babylonlabs-io/babylon/test/e2e/configurer"
"github.com/babylonlabs-io/babylon/test/e2e/configurer/chain"
"github.com/babylonlabs-io/babylon/testutil/coins"
Expand Down Expand Up @@ -117,7 +118,7 @@ func (s *BtcRewardsDistribution) SetupSuite() {
// Test1CreateFinalityProviders creates all finality providers
func (s *BtcRewardsDistribution) Test1CreateFinalityProviders() {
chainA := s.configurer.GetChainConfig(0)
chainA.WaitUntilHeight(1)
chainA.WaitUntilHeight(2)

n1, err := chainA.GetNodeAtIndex(1)
s.NoError(err)
Expand Down Expand Up @@ -207,9 +208,6 @@ func (s *BtcRewardsDistribution) Test3SubmitCovenantSignature() {
n1.WaitForNextBlock()
}

// wait for a block so that above txs take effect
n1.WaitForNextBlock()

// ensure the BTC delegation has covenant sigs now
activeDelsSet := n1.QueryFinalityProvidersDelegations(s.fp1.BtcPk.MarshalHex(), s.fp2.BtcPk.MarshalHex())
s.Len(activeDelsSet, 3)
Expand All @@ -228,7 +226,7 @@ func (s *BtcRewardsDistribution) Test4CommitPublicRandomnessAndSealed() {
s.NoError(err)

// commit public randomness list
commitStartHeight := uint64(1)
commitStartHeight := uint64(5)

fp1RandListInfo, fp1CommitPubRandList, err := datagen.GenRandomMsgCommitPubRandList(s.r, s.fp1BTCSK, commitStartHeight, numPubRand)
s.NoError(err)
Expand Down Expand Up @@ -293,10 +291,12 @@ func (s *BtcRewardsDistribution) Test4CommitPublicRandomnessAndSealed() {
s.Equal(s.finalityBlockHeightVoted, finalizedBlocks[0].Height)
s.Equal(appHash.Bytes(), finalizedBlocks[0].AppHash)
s.T().Logf("the block %d is finalized", s.finalityBlockHeightVoted)
s.AddFinalityVoteUntilCurrentHeight()
}

// Test5CheckRewardsFirstDelegations verifies the rewards independent of mint amounts
// There might be a difference in rewards if the BTC delegations were included in different blocks
// that is the reason to get the difference in rewards between a block range to assert
// the reward difference between fps and delegators.
func (s *BtcRewardsDistribution) Test5CheckRewardsFirstDelegations() {
n2, err := s.configurer.GetChainConfig(0).GetNodeAtIndex(2)
s.NoError(err)
Expand All @@ -312,21 +312,33 @@ func (s *BtcRewardsDistribution) Test5CheckRewardsFirstDelegations() {
// (del1) => 4_00000000
// (del2) => 4_00000000

// verifies that everyone is active and not slashed
fps := n2.QueryFinalityProviders()
s.Len(fps, 2)
s.Equal(fps[0].Commission.String(), fps[1].Commission.String())
for _, fp := range fps {
s.Equal(fp.SlashedBabylonHeight, uint64(0))
s.Equal(fp.SlashedBtcHeight, uint32(0))
}

dels := n2.QueryFinalityProvidersDelegations(s.fp1.BtcPk.MarshalHex(), s.fp2.BtcPk.MarshalHex())
s.Len(dels, 3)
for _, del := range dels {
s.True(del.Active)
}

// The rewards distributed for the finality providers should be fp1 => 3x, fp2 => 1x
fp1LastRewardGauge, fp2LastRewardGauge, btcDel1LastRewardGauge, btcDel2LastRewardGauge := s.QueryRewardGauges(n2)
fp1DiffRewards, fp2DiffRewards, del1DiffRewards, del2DiffRewards := s.QueryRewardGauges(n2)
s.AddFinalityVoteUntilCurrentHeight()

// fp1 ~2674ubbn
// fp2 ~891ubbn
coins.RequireCoinsDiffInPointOnePercentMargin(
s.T(),
fp2LastRewardGauge.Coins.MulInt(sdkmath.NewIntFromUint64(3)), // ~2673ubbn
fp1LastRewardGauge.Coins,
fp2DiffRewards.Coins.MulInt(sdkmath.NewIntFromUint64(3)),
fp1DiffRewards.Coins,
)

// The rewards distributed to the delegators should be the same for each delegator
// del1 ~7130ubbn
// del2 ~7130ubbn
coins.RequireCoinsDiffInPointOnePercentMargin(s.T(), btcDel1LastRewardGauge.Coins, btcDel2LastRewardGauge.Coins)
coins.RequireCoinsDiffInPointOnePercentMargin(s.T(), del1DiffRewards.Coins, del2DiffRewards.Coins)

CheckWithdrawReward(s.T(), n2, wDel2, s.del2Addr)

Expand Down Expand Up @@ -399,27 +411,9 @@ func (s *BtcRewardsDistribution) Test7CheckRewards() {
// (fp2) => 8_00000000
// (del1) => 4_00000000
// (del2) => 10_00000000
fp1RewardGaugePrev, fp2RewardGaugePrev, btcDel1RewardGaugePrev, btcDel2RewardGaugePrev := s.QueryRewardGauges(n2)
// wait a few block of rewards to calculate the difference
n2.WaitForNextBlocks(2)
s.AddFinalityVoteUntilCurrentHeight()
n2.WaitForNextBlocks(2)
s.AddFinalityVoteUntilCurrentHeight()
n2.WaitForNextBlocks(2)
s.AddFinalityVoteUntilCurrentHeight()
n2.WaitForNextBlocks(2)

fp1RewardGauge, fp2RewardGauge, btcDel1RewardGauge, btcDel2RewardGauge := s.QueryRewardGauges(n2)

// since varius block were created, it is needed to get the difference
// from a certain point where all the delegations were active to properly
// calculate the distribution with the voting power structure with 4 BTC delegations active
// Note: if a new block is mined during the query of reward gauges, the calculation might be a
// bit off by some ubbn
fp1DiffRewards := fp1RewardGauge.Coins.Sub(fp1RewardGaugePrev.Coins...)
fp2DiffRewards := fp2RewardGauge.Coins.Sub(fp2RewardGaugePrev.Coins...)
del1DiffRewards := btcDel1RewardGauge.Coins.Sub(btcDel1RewardGaugePrev.Coins...)
del2DiffRewards := btcDel2RewardGauge.Coins.Sub(btcDel2RewardGaugePrev.Coins...)
// gets the difference in rewards in 8 blocks range
fp1DiffRewards, fp2DiffRewards, del1DiffRewards, del2DiffRewards := s.GetRewardDifferences(8)

// Check the difference in the finality providers
// fp1 should receive ~75% of the rewards received by fp2
Expand All @@ -442,7 +436,82 @@ func (s *BtcRewardsDistribution) Test7CheckRewards() {
s.NotEmpty(del2DiffRewardsStr)
}

// TODO(rafilx): Slash a FP and expect rewards to be withdraw.
// Test8SlashFp slashes the finality provider, but should continue to produce blocks
func (s *BtcRewardsDistribution) Test8SlashFp() {
RafilxTenfen marked this conversation as resolved.
Show resolved Hide resolved
chainA := s.configurer.GetChainConfig(0)
n2, err := chainA.GetNodeAtIndex(2)
s.NoError(err)

badBlockHeightToVote := s.finalityBlockHeightVoted + 1

blockToVote, err := n2.QueryBlock(int64(badBlockHeightToVote))
s.NoError(err)
appHash := blockToVote.AppHash

// generate bad EOTS signature with a diff block height to vote
msgToSign := append(sdk.Uint64ToBigEndian(s.finalityBlockHeightVoted), appHash...)
fp1Sig, err := eots.Sign(s.fp2BTCSK, s.fp2RandListInfo.SRList[s.finalityIdx], msgToSign)
s.NoError(err)

finalitySig := bbn.NewSchnorrEOTSSigFromModNScalar(fp1Sig)

// submit finality signature to slash
n2.AddFinalitySigFromVal(
s.fp2.BtcPk,
s.finalityBlockHeightVoted,
&s.fp2RandListInfo.PRList[s.finalityIdx],
*s.fp2RandListInfo.ProofList[s.finalityIdx].ToProto(),
appHash,
finalitySig,
)

n2.WaitForNextBlocks(2)

fps := n2.QueryFinalityProviders()
require.Len(s.T(), fps, 2)
for _, fp := range fps {
if strings.EqualFold(fp.Addr, s.fp1Addr) {
require.Zero(s.T(), fp.SlashedBabylonHeight)
continue
}
require.NotZero(s.T(), fp.SlashedBabylonHeight)
}

// wait a few blocks to check if it doesn't panic when rewards are being produced
n2.WaitForNextBlocks(5)
}

func (s *BtcRewardsDistribution) GetRewardDifferences(blocksDiff uint64) (
fp1DiffRewards, fp2DiffRewards, del1DiffRewards, del2DiffRewards sdk.Coins,
) {
chainA := s.configurer.GetChainConfig(0)
n2, err := chainA.GetNodeAtIndex(2)
s.NoError(err)

fp1RewardGaugePrev, fp2RewardGaugePrev, btcDel1RewardGaugePrev, btcDel2RewardGaugePrev := s.QueryRewardGauges(n2)
// wait a few block of rewards to calculate the difference
for i := 1; i <= int(blocksDiff); i++ {
if i%2 == 0 {
s.AddFinalityVoteUntilCurrentHeight()
}
n2.WaitForNextBlock()
}

fp1RewardGauge, fp2RewardGauge, btcDel1RewardGauge, btcDel2RewardGauge := s.QueryRewardGauges(n2)

// since varius block were created, it is needed to get the difference
// from a certain point where all the delegations were active to properly
// calculate the distribution with the voting power structure with 4 BTC delegations active
// Note: if a new block is mined during the query of reward gauges, the calculation might be a
// bit off by some ubbn
fp1DiffRewards = fp1RewardGauge.Coins.Sub(fp1RewardGaugePrev.Coins...)
fp2DiffRewards = fp2RewardGauge.Coins.Sub(fp2RewardGaugePrev.Coins...)
del1DiffRewards = btcDel1RewardGauge.Coins.Sub(btcDel1RewardGaugePrev.Coins...)
del2DiffRewards = btcDel2RewardGauge.Coins.Sub(btcDel2RewardGaugePrev.Coins...)

s.AddFinalityVoteUntilCurrentHeight()
return fp1DiffRewards, fp2DiffRewards, del1DiffRewards, del2DiffRewards
}

func (s *BtcRewardsDistribution) AddFinalityVoteUntilCurrentHeight() {
chainA := s.configurer.GetChainConfig(0)
Expand Down
4 changes: 1 addition & 3 deletions testutil/btcstaking-helper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ func (h *Helper) CreateDelegation(
r *rand.Rand,
delSK *btcec.PrivateKey,
fpPK *btcec.PublicKey,
changeAddress string,
stakingValue int64,
stakingTime uint16,
unbondingValue int64,
Expand All @@ -239,7 +238,7 @@ func (h *Helper) CreateDelegation(
addToAllowList bool,
) (string, *types.MsgCreateBTCDelegation, *types.BTCDelegation, *btclctypes.BTCHeaderInfo, *types.InclusionProof, *UnbondingTxInfo, error) {
return h.CreateDelegationWithBtcBlockHeight(
r, delSK, fpPK, changeAddress, stakingValue,
r, delSK, fpPK, stakingValue,
stakingTime, unbondingValue, unbondingTime,
usePreApproval, addToAllowList, 10, 10,
)
Expand All @@ -249,7 +248,6 @@ func (h *Helper) CreateDelegationWithBtcBlockHeight(
r *rand.Rand,
delSK *btcec.PrivateKey,
fpPK *btcec.PublicKey,
changeAddress string,
stakingValue int64,
stakingTime uint16,
unbondingValue int64,
Expand Down
4 changes: 0 additions & 4 deletions testutil/incentives-helper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,13 @@ func (h *IncentiveHelper) CreateBtcDelegation(
) (
stakingTxHash string, msgCreateBTCDel *bstypes.MsgCreateBTCDelegation, actualDel *bstypes.BTCDelegation, unbondingInfo *btcstkhelper.UnbondingTxInfo,
) {
changeAddress, err := datagen.GenRandomBTCAddress(r, h.Net)
h.NoError(err)

delSK, _, err := datagen.GenRandomBTCKeyPair(r)
h.NoError(err)

stakingTxHash, msgCreateBTCDel, _, _, _, unbondingInfo, err = h.CreateDelegationWithBtcBlockHeight(
r,
delSK,
fpPK,
changeAddress.EncodeAddress(),
stakingValue,
stakingTime,
0,
Expand Down
3 changes: 0 additions & 3 deletions x/btcstaking/keeper/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ func benchBeginBlock(b *testing.B, numFPs int, numDelsUnderFP int) {
h := testutil.NewHelper(b, btclcKeeper, btccKeeper)
// set all parameters
covenantSKs, _ := h.GenAndApplyParams(r)
changeAddress, err := datagen.GenRandomBTCAddress(r, h.Net)
h.NoError(err)

// generate new finality providers
fps := []*types.FinalityProvider{}
Expand Down Expand Up @@ -60,7 +58,6 @@ func benchBeginBlock(b *testing.B, numFPs int, numDelsUnderFP int) {
r,
delSK,
fp.BtcPk.MustToBTCPK(),
changeAddress.EncodeAddress(),
stakingValue,
1000,
0,
Expand Down
Loading
Loading