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: e2e rewards test flaky #421

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
80fba51
chore: add log
RafilxTenfen Jan 17, 2025
e970146
Merge branch 'main' of github.com:babylonlabs-io/babylon into rafilx/…
RafilxTenfen Jan 29, 2025
5b2abca
Merge branch 'main' of github.com:babylonlabs-io/babylon into rafilx/…
RafilxTenfen Jan 29, 2025
67d5abe
chore: add #421 to changelog and add checks for pub rand commit
RafilxTenfen Jan 29, 2025
f852a0b
fix: check of lenght pub rand commit
RafilxTenfen Jan 30, 2025
0c17d55
Merge branch 'main' of github.com:babylonlabs-io/babylon into rafilx/…
RafilxTenfen Jan 30, 2025
b3b8f67
fix: check num pub rand
RafilxTenfen Jan 30, 2025
5bef2ec
chore: add eventually check of rewards
RafilxTenfen Jan 30, 2025
f2ec45e
chore: reduce number of rwd blocks
RafilxTenfen Jan 30, 2025
efc9a26
chore: add more conditions to eventually run
RafilxTenfen Jan 30, 2025
d48499c
tryfix: modify finality params
RafilxTenfen Jan 30, 2025
e5c8ffc
chore: add wait group to send finality sig together
RafilxTenfen Jan 30, 2025
559a654
chore: add checks for fps prior to epoch finalization and removed gas…
RafilxTenfen Jan 31, 2025
e233712
chore: rollback build testnet upgrade flag
RafilxTenfen Jan 31, 2025
0231296
tryfix: add more balances to wallets
RafilxTenfen Jan 31, 2025
f972d6c
chore: add new function to better verify reward amounts
RafilxTenfen Jan 31, 2025
37ec38f
chore: add check of existence rewards
RafilxTenfen Jan 31, 2025
80f24c7
tryfix: coins withdraw
RafilxTenfen Jan 31, 2025
1617511
fix: lint
RafilxTenfen Jan 31, 2025
f5ba76b
tryfix: ibc conn
RafilxTenfen Jan 31, 2025
4c66241
chore: remove gas limit
RafilxTenfen Jan 31, 2025
1dca41c
fix: avoid division by 0
RafilxTenfen Jan 31, 2025
2f54c82
chore: increase amounts
RafilxTenfen Jan 31, 2025
e1324e5
chore: refactory ibc transfer testing
RafilxTenfen Jan 31, 2025
69cc13f
fix: lint err wrapping
RafilxTenfen Jan 31, 2025
7c8a1b4
fix: lint err check
RafilxTenfen Jan 31, 2025
5783e52
chore: add back gas auto to wasm store
RafilxTenfen Jan 31, 2025
b679910
fix transfer ibc
RafilxTenfen Jan 31, 2025
8cb7770
fix: addrB in transfer back
RafilxTenfen Jan 31, 2025
e19df9d
chore: refactory check of amounts in ibc transfers
RafilxTenfen Jan 31, 2025
8f9b3bd
fix: node that queries tx
RafilxTenfen Jan 31, 2025
cbad0f4
fix: wait for one block to get the tx
RafilxTenfen Jan 31, 2025
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 @@ -39,6 +39,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### Improvements

- [#421](https://github.com/babylonlabs-io/babylon/pull/421) Add checks to public
randomness commit at `TestBTCRewardsDistribution`.
- [#391](https://github.com/babylonlabs-io/babylon/pull/391) Fix e2e `TestBTCRewardsDistribution` flunky
check of rewards
- [#458](https://github.com/babylonlabs-io/babylon/pull/458) Set `CosmosProvider` functions as public
Expand Down
4 changes: 4 additions & 0 deletions app/ante/fee_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func CheckTxFeeWithGlobalMinGasPrices(ctx sdk.Context, tx sdk.Tx) (sdk.Coins, in
// provided in a transaction.
// NOTE: This implementation should not be used for txs with multiple coins.
func getTxPriority(fee sdk.Coins, gas int64) int64 {
if gas == 0 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would panic to quote as zero

return 0
}

var priority int64
for _, c := range fee {
p := c.Amount.Mul(sdkmath.NewInt(priorityScalingFactor)).QuoRaw(gas)
Expand Down
6 changes: 6 additions & 0 deletions app/ante/get_tx_priority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ func TestGetTxPriority(t *testing.T) {
gas: 1_000_000,
expectedPri: 1000,
},
{
name: "0 gas, should return 0 priority",
fee: sdk.NewCoins(sdk.NewInt64Coin(denom, 1_000)),
gas: 0,
expectedPri: 0,
},
}

for _, tc := range cases {
Expand Down
148 changes: 115 additions & 33 deletions test/e2e/btc_rewards_distribution_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math"
"math/rand"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -126,10 +127,13 @@ func (s *BtcRewardsDistribution) Test1CreateFinalityProviders() {
n2, err := chainA.GetNodeAtIndex(2)
s.NoError(err)

n1.WaitForNextBlock()
n2.WaitForNextBlock()

s.fp1Addr = n1.KeysAdd(wFp1)
s.fp2Addr = n2.KeysAdd(wFp2)

n2.BankMultiSendFromNode([]string{s.fp1Addr, s.fp2Addr}, "100000ubbn")
n2.BankMultiSendFromNode([]string{s.fp1Addr, s.fp2Addr}, "1000000ubbn")

n2.WaitForNextBlock()

Expand Down Expand Up @@ -164,7 +168,7 @@ func (s *BtcRewardsDistribution) Test2CreateFirstBtcDelegations() {
s.del1Addr = n2.KeysAdd(wDel1)
s.del2Addr = n2.KeysAdd(wDel2)

n2.BankMultiSendFromNode([]string{s.del1Addr, s.del2Addr}, "100000ubbn")
n2.BankMultiSendFromNode([]string{s.del1Addr, s.del2Addr}, "1000000ubbn")

n2.WaitForNextBlock()

Expand All @@ -174,6 +178,9 @@ func (s *BtcRewardsDistribution) Test2CreateFirstBtcDelegations() {
s.CreateBTCDelegationAndCheck(n2, wDel2, s.fp1, s.del2BTCSK, s.del2Addr, s.fp1Del2StakingAmt)
// fp2Del1
s.CreateBTCDelegationAndCheck(n2, wDel1, s.fp2, s.del1BTCSK, s.del1Addr, s.fp2Del1StakingAmt)

resp := n2.QueryBtcDelegations(bstypes.BTCDelegationStatus_ANY)
require.Len(s.T(), resp.BtcDelegations, 3)
}

// Test3SubmitCovenantSignature covenant approves all the 3 BTC delegation
Expand All @@ -192,13 +199,13 @@ func (s *BtcRewardsDistribution) Test3SubmitCovenantSignature() {
}
s.covenantWallets = covWallets

n1.BankMultiSendFromNode(covAddrs, "100000ubbn")
n1.BankMultiSendFromNode(covAddrs, "1000000ubbn")

// tx bank send needs to take effect
n1.WaitForNextBlock()

pendingDelsResp := n1.QueryFinalityProvidersDelegations(s.fp1.BtcPk.MarshalHex(), s.fp2.BtcPk.MarshalHex())
s.Equal(len(pendingDelsResp), 3)
s.Require().Equal(len(pendingDelsResp), 3)

for _, pendingDelResp := range pendingDelsResp {
pendingDel, err := ParseRespBTCDelToBTCDel(pendingDelResp)
Expand All @@ -211,9 +218,9 @@ func (s *BtcRewardsDistribution) Test3SubmitCovenantSignature() {

// ensure the BTC delegation has covenant sigs now
activeDelsSet := n1.QueryFinalityProvidersDelegations(s.fp1.BtcPk.MarshalHex(), s.fp2.BtcPk.MarshalHex())
s.Len(activeDelsSet, 3)
s.Require().Len(activeDelsSet, 3)
for _, activeDel := range activeDelsSet {
s.True(activeDel.Active)
s.Require().True(activeDel.Active)
}
}

Expand Down Expand Up @@ -253,34 +260,87 @@ func (s *BtcRewardsDistribution) Test4CommitPublicRandomnessAndSealed() {
fp2CommitPubRandList.Sig,
)

n1.WaitUntilCurrentEpochIsSealedAndFinalized(1)
// needs to wait for a block to make sure the pub rand is committed
// prior to epoch finalization
n2.WaitForNextBlockWithSleep50ms()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

important part, before the pub rand might not get included in the latest epoch and then it would not be finalized


// check all FPs requirement to be active
// TotalBondedSat > 0
// IsTimestamped
// !IsJailed
// !IsSlashed

fp1CommitPubRand := n1.QueryListPubRandCommit(fp1CommitPubRandList.FpBtcPk)
fp1PubRand := fp1CommitPubRand[commitStartHeight]
s.Require().Equal(fp1PubRand.NumPubRand, numPubRand)

fp2CommitPubRand := n2.QueryListPubRandCommit(fp2CommitPubRandList.FpBtcPk)
fp2PubRand := fp2CommitPubRand[commitStartHeight]
s.Require().Equal(fp2PubRand.NumPubRand, numPubRand)

finalizedEpoch := n1.WaitUntilCurrentEpochIsSealedAndFinalized(1)
s.Require().GreaterOrEqual(finalizedEpoch, fp1PubRand.EpochNum)
s.Require().GreaterOrEqual(finalizedEpoch, fp2PubRand.EpochNum)

fps := n2.QueryFinalityProviders()
s.Require().Len(fps, 2)
for _, fp := range fps {
s.Require().False(fp.Jailed, "fp is jailed")
s.Require().Zero(fp.SlashedBabylonHeight, "fp is slashed")
fpDels := n2.QueryFinalityProviderDelegations(fp.BtcPk.MarshalHex())
if fp.BtcPk.Equals(s.fp1.BtcPk) {
s.Require().Len(fpDels, 2)
} else {
s.Require().Len(fpDels, 1)
}
for _, fpDelStaker := range fpDels {
for _, fpDel := range fpDelStaker.Dels {
s.Require().True(fpDel.Active)
s.Require().GreaterOrEqual(fpDel.TotalSat, uint64(0))
}
}
}

s.finalityBlockHeightVoted = n1.WaitFinalityIsActivated()

// submit finality signature
s.finalityIdx = s.finalityBlockHeightVoted - commitStartHeight

appHash := n1.AddFinalitySignatureToBlock(
s.fp1BTCSK,
s.fp1.BtcPk,
s.finalityBlockHeightVoted,
s.fp1RandListInfo.SRList[s.finalityIdx],
&s.fp1RandListInfo.PRList[s.finalityIdx],
*s.fp1RandListInfo.ProofList[s.finalityIdx].ToProto(),
fmt.Sprintf("--from=%s", wFp1),
)

n2.AddFinalitySignatureToBlock(
s.fp2BTCSK,
s.fp2.BtcPk,
s.finalityBlockHeightVoted,
s.fp2RandListInfo.SRList[s.finalityIdx],
&s.fp2RandListInfo.PRList[s.finalityIdx],
*s.fp2RandListInfo.ProofList[s.finalityIdx].ToProto(),
fmt.Sprintf("--from=%s", wFp2),
n1.WaitForNextBlockWithSleep50ms()
var (
wg sync.WaitGroup
appHash bytes.HexBytes
)
wg.Add(2)

go func() {
defer wg.Done()
appHash = n1.AddFinalitySignatureToBlock(
s.fp1BTCSK,
s.fp1.BtcPk,
s.finalityBlockHeightVoted,
s.fp1RandListInfo.SRList[s.finalityIdx],
&s.fp1RandListInfo.PRList[s.finalityIdx],
*s.fp1RandListInfo.ProofList[s.finalityIdx].ToProto(),
fmt.Sprintf("--from=%s", wFp1),
)
}()

go func() {
defer wg.Done()
n2.AddFinalitySignatureToBlock(
s.fp2BTCSK,
s.fp2.BtcPk,
s.finalityBlockHeightVoted,
s.fp2RandListInfo.SRList[s.finalityIdx],
&s.fp2RandListInfo.PRList[s.finalityIdx],
*s.fp2RandListInfo.ProofList[s.finalityIdx].ToProto(),
fmt.Sprintf("--from=%s", wFp2),
)
}()

n2.WaitForNextBlock()
wg.Wait()
n1.WaitForNextBlockWithSleep50ms()

// ensure vote is eventually cast
var finalizedBlocks []*ftypes.IndexedBlock
Expand Down Expand Up @@ -328,6 +388,15 @@ func (s *BtcRewardsDistribution) Test5CheckRewardsFirstDelegations() {
s.True(del.Active)
}

// makes sure there is some reward there
s.Eventually(func() bool {
_, errFp1 := n2.QueryRewardGauge(s.fp1.Address())
_, errFp2 := n2.QueryRewardGauge(s.fp2.Address())
_, errDel1 := n2.QueryRewardGauge(sdk.MustAccAddressFromBech32(s.del1Addr))
_, errDel2 := n2.QueryRewardGauge(sdk.MustAccAddressFromBech32(s.del2Addr))
return errFp1 == nil && errFp2 == nil && errDel1 == nil && errDel2 == nil
}, time.Minute*2, time.Second*3, "wait to have some rewards available in the gauge")

// The rewards distributed for the finality providers should be fp1 => 3x, fp2 => 1x
fp1DiffRewards, fp2DiffRewards, del1DiffRewards, del2DiffRewards := s.QueryRewardGauges(n2)
s.AddFinalityVoteUntilCurrentHeight()
Expand Down Expand Up @@ -413,8 +482,8 @@ func (s *BtcRewardsDistribution) Test7CheckRewards() {
// (del1) => 4_00000000
// (del2) => 10_00000000

// gets the difference in rewards in 8 blocks range
fp1DiffRewards, fp2DiffRewards, del1DiffRewards, del2DiffRewards := s.GetRewardDifferences(8)
// gets the difference in rewards in 4 blocks range
fp1DiffRewards, fp2DiffRewards, del1DiffRewards, del2DiffRewards := s.GetRewardDifferences(4)

// Check the difference in the finality providers
// fp1 should receive ~75% of the rewards received by fp2
Expand Down Expand Up @@ -604,19 +673,31 @@ func (s *BtcRewardsDistribution) QueryRewardGauges(n *chain.NodeConfig) (

g.Go(func() error {
fp1RewardGauges, err = n.QueryRewardGauge(s.fp1.Address())
return err
if err != nil {
return fmt.Errorf("failed to query rewards for fp1: %w", err)
}
return nil
})
g.Go(func() error {
fp2RewardGauges, err = n.QueryRewardGauge(s.fp2.Address())
return err
if err != nil {
return fmt.Errorf("failed to query rewards for fp2: %w", err)
}
return nil
})
g.Go(func() error {
btcDel1RewardGauges, err = n.QueryRewardGauge(sdk.MustAccAddressFromBech32(s.del1Addr))
return err
if err != nil {
return fmt.Errorf("failed to query rewards for del1: %w", err)
}
return nil
})
g.Go(func() error {
btcDel2RewardGauges, err = n.QueryRewardGauge(sdk.MustAccAddressFromBech32(s.del2Addr))
return err
if err != nil {
return fmt.Errorf("failed to query rewards for del2: %w", err)
}
return nil
})
s.NoError(g.Wait())

Expand Down Expand Up @@ -660,12 +741,13 @@ func CheckWithdrawReward(
n.WaitForNextBlockWithSleep50ms()

delBalanceBeforeWithdraw, err := n.QueryBalances(delAddr)
require.NoError(t, err)

txHash := n.WithdrawReward(itypes.BTCDelegationType.String(), delWallet)

n.WaitForNextBlock()

_, txResp := n.QueryTx(txHash)
require.NoError(t, err)

delRwdGauge, errRwdGauge := n.QueryRewardGauge(accDelAddr)
require.NoError(t, errRwdGauge)
Expand Down
76 changes: 8 additions & 68 deletions test/e2e/btc_staking_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,64 +331,11 @@ func (s *BTCStakingTestSuite) Test3CommitPublicRandomnessAndSubmitFinalitySignat

func (s *BTCStakingTestSuite) Test4WithdrawReward() {
chainA := s.configurer.GetChainConfig(0)
nonValidatorNode, err := chainA.GetNodeAtIndex(2)
s.NoError(err)

// finality provider balance before withdraw
fpBabylonAddr, err := sdk.AccAddressFromBech32(s.cacheFP.Addr)
s.NoError(err)
delBabylonAddr := fpBabylonAddr

fpBalance, err := nonValidatorNode.QueryBalances(fpBabylonAddr.String())
s.NoError(err)
// finality provider reward gauge should not be fully withdrawn
fpRgs, err := nonValidatorNode.QueryRewardGauge(fpBabylonAddr)
n, err := chainA.GetNodeAtIndex(2)
s.NoError(err)
fpRg := fpRgs[itypes.FinalityProviderType.String()]
s.T().Logf("finality provider's withdrawable reward before withdrawing: %s", convertToRewardGauge(fpRg).GetWithdrawableCoins().String())
s.False(convertToRewardGauge(fpRg).IsFullyWithdrawn())

// withdraw finality provider reward
nonValidatorNode.WithdrawReward(itypes.FinalityProviderType.String(), initialization.ValidatorWalletName)
nonValidatorNode.WaitForNextBlock()

// balance after withdrawing finality provider reward
fpBalance2, err := nonValidatorNode.QueryBalances(fpBabylonAddr.String())
s.NoError(err)
s.T().Logf("fpBalance2: %s; fpBalance: %s", fpBalance2.String(), fpBalance.String())
s.True(fpBalance2.IsAllGT(fpBalance))
// finality provider reward gauge should be fully withdrawn now
fpRgs2, err := nonValidatorNode.QueryRewardGauge(fpBabylonAddr)
s.NoError(err)
fpRg2 := fpRgs2[itypes.FinalityProviderType.String()]
s.T().Logf("finality provider's withdrawable reward after withdrawing: %s", convertToRewardGauge(fpRg2).GetWithdrawableCoins().String())
s.True(convertToRewardGauge(fpRg2).IsFullyWithdrawn())

// BTC delegation balance before withdraw
btcDelBalance, err := nonValidatorNode.QueryBalances(delBabylonAddr.String())
s.NoError(err)
// BTC delegation reward gauge should not be fully withdrawn
btcDelRgs, err := nonValidatorNode.QueryRewardGauge(delBabylonAddr)
s.NoError(err)
btcDelRg := btcDelRgs[itypes.BTCDelegationType.String()]
s.T().Logf("BTC delegation's withdrawable reward before withdrawing: %s", convertToRewardGauge(btcDelRg).GetWithdrawableCoins().String())
s.False(convertToRewardGauge(btcDelRg).IsFullyWithdrawn())

// withdraw BTC delegation reward
nonValidatorNode.WithdrawReward(itypes.BTCDelegationType.String(), initialization.ValidatorWalletName)
nonValidatorNode.WaitForNextBlock()

// balance after withdrawing BTC delegation reward
btcDelBalance2, err := nonValidatorNode.QueryBalances(delBabylonAddr.String())
s.NoError(err)
s.T().Logf("btcDelBalance2: %s; btcDelBalance: %s", btcDelBalance2.String(), btcDelBalance.String())
s.True(btcDelBalance2.IsAllGT(btcDelBalance))
// BTC delegation reward gauge should be fully withdrawn now
btcDelRgs2, err := nonValidatorNode.QueryRewardGauge(delBabylonAddr)
s.NoError(err)
btcDelRg2 := btcDelRgs2[itypes.BTCDelegationType.String()]
s.T().Logf("BTC delegation's withdrawable reward after withdrawing: %s", convertToRewardGauge(btcDelRg2).GetWithdrawableCoins().String())
s.True(convertToRewardGauge(btcDelRg2).IsFullyWithdrawn())
n.WithdrawRewardCheckingBalances(itypes.FinalityProviderType.String(), s.cacheFP.Addr)
n.WithdrawRewardCheckingBalances(itypes.BTCDelegationType.String(), s.cacheFP.Addr)
}

// Test5SubmitStakerUnbonding is an end-to-end test for user unbonding
Expand Down Expand Up @@ -465,7 +412,7 @@ func (s *BTCStakingTestSuite) Test6MultisigBTCDelegation() {
nonValidatorNode.KeysAdd(w2)
// creates and fund multisig
multisigAddr := nonValidatorNode.KeysAdd(wMultisig, []string{fmt.Sprintf("--multisig=%s,%s", w1, w2), "--multisig-threshold=2"}...)
nonValidatorNode.BankSendFromNode(multisigAddr, "100000ubbn")
nonValidatorNode.BankSendFromNode(multisigAddr, "10000000ubbn")

// create a random BTC delegation under the cached finality provider
// BTC staking params, BTC delegation key pairs and PoP
Expand Down Expand Up @@ -534,7 +481,7 @@ func (s *BTCStakingTestSuite) Test7BTCDelegationFeeGrant() {
feePayerAddr := sdk.MustAccAddressFromBech32(nonValidatorNode.KeysAdd(wGranter))
granteeStakerAddr := sdk.MustAccAddressFromBech32(nonValidatorNode.KeysAdd(wGratee))

feePayerBalanceBeforeBTCDel := sdk.NewCoin(appparams.DefaultBondDenom, sdkmath.NewInt(100000))
feePayerBalanceBeforeBTCDel := sdk.NewCoin(appparams.DefaultBondDenom, sdkmath.NewInt(5000000))

// fund the granter
nonValidatorNode.BankSendFromNode(feePayerAddr.String(), feePayerBalanceBeforeBTCDel.String())
Expand Down Expand Up @@ -623,9 +570,9 @@ func (s *BTCStakingTestSuite) Test8BTCDelegationFeeGrantTyped() {
feePayerAddr := sdk.MustAccAddressFromBech32(node.KeysAdd(wGranter))
granteeStakerAddr := sdk.MustAccAddressFromBech32(node.KeysAdd(wGratee))

feePayerBalanceBeforeBTCDel := sdk.NewCoin(appparams.DefaultBondDenom, sdkmath.NewInt(100000))
stakerBalance := sdk.NewCoin(appparams.DefaultBondDenom, sdkmath.NewInt(100))
fees := sdk.NewCoin(appparams.DefaultBondDenom, sdkmath.NewInt(50000))
feePayerBalanceBeforeBTCDel := sdk.NewCoin(appparams.DefaultBondDenom, sdkmath.NewInt(4000000))
stakerBalance := sdk.NewCoin(appparams.DefaultBondDenom, sdkmath.NewInt(1000))
fees := sdk.NewCoin(appparams.DefaultBondDenom, sdkmath.NewInt(500000))

// fund the granter and the staker
node.BankSendFromNode(feePayerAddr.String(), feePayerBalanceBeforeBTCDel.String())
Expand Down Expand Up @@ -1017,10 +964,3 @@ func (s *BTCStakingTestSuite) BTCStakingUnbondSlashInfo(

return testStakingInfo, blockWithStakingTx.SpvProof.BtcTransaction, inclusionProof, testUnbondingInfo, delegatorSig
}

func convertToRewardGauge(rg *itypes.RewardGaugesResponse) *itypes.RewardGauge {
return &itypes.RewardGauge{
Coins: rg.Coins,
WithdrawnCoins: rg.WithdrawnCoins,
}
}
Loading
Loading