Skip to content

Commit

Permalink
fix: slashed fp and BTC rewards tracker (#371)
Browse files Browse the repository at this point in the history
`EventPowerDistUpdate_SlashedFp` and
[`RewardBTCStaking`](https://github.com/babylonlabs-io/babylon/blob/b132d16483d2e12ac68be74a524a4b7121edac65/x/incentive/keeper/btc_staking_gauge.go#L19)
are called at different blocks.
The rewards are being called a few blocks behind and if the slashed fp
is pruned in BTC reward tracker data, it fails to find and panics

On babylond-deployments an fp is being slashed at block height 46, the
call of `RewardBTCStaking` happens at Babylon block 48 but is called to
distribute rewards of voting power table from block 44 (which didn't had
the FP slashed yet) which then checks for fp rewards for the FP that was
pruned at the slashing processed event.

```
6:04PM INF finalizing commit of block hash=153E869F7B0056A9CD8208301F2019FFACA2BEFBBB35AEADF70DCA14C15F3972 height=47 module=consensus num_txs=2 root=62B5F025459DEEBE2EE6F5D869C5BCBE903299FF80825FB9DE62551E6B725D52
 ERR CONSENSUS FAILURE!!! err="failed to add fp rewards for btc delegation bbn1wwkaq6z3kdkekm7ltwxng4ftvzux8gzp2xjx8d at height 44: finality provider current rewards not found
```

```shell
$~ babylond q btcstaking finality-providers -o json | jq
...
    {
      "description": {
        "moniker": "Finality Provider 0",
        "identity": "",
        "website": "",
        "security_contact": "",
        "details": ""
      },
      "commission": "0.050000000000000000",
      "addr": "bbn1wwkaq6z3kdkekm7ltwxng4ftvzux8gzp2xjx8d",
      "btc_pk": "6ba387c315dc6105ec02b50684593505103a9f13452d3a597c16ac2f22b924dc",
      "pop": {
        "btc_sig_type": "BIP340",
        "btc_sig": "Q3PcfiKT0fwWOVx95rjV+mQvI/juTbYIV84Hfz2NAmQByiHRXz8OWnFbyugJgOrd/TrqR+6BOgtr10bZUFDFbA=="
      },
      "slashed_babylon_height": "46",
      "slashed_btc_height": 131,
      "height": "46",
      "jailed": false,
      "highest_voted_height": 45
    },
```

Basically, this means we are processing the rewards a few blocks down
the road after the slash and then it is not possible to prune the data
for reward tracker until that fp slashed height is processed.

The fix for the time being: do nothing at the btc reward tracker
structures if some fp gets slashed just continue to store that data. In
the future during the issue
#362 we can properly
handle the pruning of slashed finality provider data
  • Loading branch information
RafilxTenfen committed Dec 24, 2024
1 parent a3b749d commit c7c6733
Show file tree
Hide file tree
Showing 14 changed files with 342 additions and 173 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## Unreleased

## v1.0.0-rc1
## v1.0.0-rc2

### 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

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() {
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

0 comments on commit c7c6733

Please sign in to comment.