Skip to content

Commit

Permalink
chore: add new function to better verify reward amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
RafilxTenfen committed Jan 31, 2025
1 parent 0231296 commit f972d6c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 56 deletions.
59 changes: 3 additions & 56 deletions test/e2e/btc_staking_pre_approval_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,64 +408,11 @@ func (s *BTCStakingPreApprovalTestSuite) Test4CommitPublicRandomnessAndSubmitFin

func (s *BTCStakingPreApprovalTestSuite) 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)
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())
n, err := chainA.GetNodeAtIndex(2)
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 := convertToRewardGauge(btcDelRgs2[itypes.BTCDelegationType.String()])
s.T().Logf("BTC delegation's withdrawable reward after withdrawing: %s", btcDelRg2.GetWithdrawableCoins().String())
s.True(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
29 changes: 29 additions & 0 deletions test/e2e/configurer/chain/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,35 @@ func (n *NodeConfig) WithdrawReward(sType, from string) (txHash string) {
return GetTxHashFromOutput(outBuf.String())
}

// WithdrawRewardCheckingBalances will withdraw the rewards and verify the amount was correctly withdraw
func (n *NodeConfig) WithdrawRewardCheckingBalances(sType, fromAddr string) {
balanceBeforeRwdWithdraw, err := n.QueryBalances(fromAddr)
require.NoError(n.t, err)

rewardGauge, err := n.QueryRewardGauge(sdk.MustAccAddressFromBech32(fromAddr))
require.NoError(n.t, err)

fpRg := rewardGauge[sType]
n.t.Logf("address: %s withdrawable reward before withdrawing: %s", fromAddr, fpRg.WithdrawnCoins.String())
require.False(n.t, fpRg.Coins.Equal(fpRg.WithdrawnCoins))

txHash := n.WithdrawReward(sType, fromAddr)
n.WaitForNextBlock()

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

// balance after withdrawing reward
balanceAfterRwdWithdraw, err := n.QueryBalances(fromAddr)
require.NoError(n.t, err)

actualAmt := balanceAfterRwdWithdraw.String()
expectedAmt := balanceBeforeRwdWithdraw.Add(fpRg.WithdrawnCoins...).Sub(txResp.AuthInfo.Fee.Amount...).String()
require.Equal(n.t, expectedAmt, actualAmt, "Expected(after withdraw): %s, actual(before withdraw + withdraw - TxFees): %s", expectedAmt, actualAmt)

n.t.Logf("BalanceAfterRwdWithdraw: %s; BalanceBeforeRwdWithdraw: %s", balanceAfterRwdWithdraw.String(), balanceBeforeRwdWithdraw.String())
}

// TxMultisigSign sign a tx in a file with one wallet for a multisig address.
func (n *NodeConfig) TxMultisigSign(walletName, multisigAddr, txFileFullPath, fileName string, overallFlags ...string) (fullFilePathInContainer string) {
return n.TxSign(walletName, txFileFullPath, fileName, fmt.Sprintf("--multisig=%s", multisigAddr))
Expand Down

0 comments on commit f972d6c

Please sign in to comment.