Skip to content

Commit

Permalink
x/auth,x/bank: modify bank tests with assertions for fee_collector an…
Browse files Browse the repository at this point in the history
…d account balances
  • Loading branch information
Raneet10 committed Mar 27, 2024
1 parent 1a3e51f commit 46dd21b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
3 changes: 2 additions & 1 deletion x/auth/ante/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package ante

import (
"bytes"
"cosmossdk.io/math"
"fmt"
"math/big"

"cosmossdk.io/math"

errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down
70 changes: 61 additions & 9 deletions x/bank/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,23 +155,35 @@ func TestSendNotEnoughBalance(t *testing.T) {
sender sdk.AccAddress
privKey *secp256k1.PrivKey
account *authtypes.BaseAccount
balance sdk.Coins
expectedBalances []expectedBalance
shouldSeqIncrease bool
}{
{
"enough balance to pay for fees but not for transfer",
addr1,
priv1,
acc1,
sdk.Coins{sdk.NewInt64Coin("matic", 67*defaultFeeAmount)},
[]expectedBalance{
{addr1, sdk.Coins{sdk.NewInt64Coin("matic", 66*defaultFeeAmount)}},
{s.AccountKeeper.GetModuleAddress(authtypes.FeeCollectorName), sdk.Coins{sdk.NewInt64Coin("matic", defaultFeeAmount)}},
},
true,
},
{
"not enough balance to pay for fees",
addr3,
priv3,
acc3,
sdk.Coins{sdk.NewInt64Coin("matic", defaultFeeAmount-1)},
[]expectedBalance{
{addr3, sdk.Coins{sdk.NewInt64Coin("matic", defaultFeeAmount-1)}},

// TODO HV2: fee_collector's balance should be 2*defaultFeeAmount but since distribution module
// flushes the fees to the its module account at beginning of the block,
// the fee_collector's balance is 0.
// We should replace the native simapp with a modified version that imitates heimdall as much as possible
// to avoid such discrepancies.
{s.AccountKeeper.GetModuleAddress(authtypes.FeeCollectorName), sdk.Coins{}},
},
false,
},
}
Expand All @@ -192,12 +204,6 @@ func TestSendNotEnoughBalance(t *testing.T) {
_, _, err := simtestutil.SignCheckDeliver(t, txConfig, baseApp, header, []sdk.Msg{sendMsg}, "", []uint64{origAccNum}, []uint64{origSeq}, false, false, tc.privKey)
require.Error(t, err)

if tc.shouldSeqIncrease {
checkBalance(t, baseApp, tc.sender, tc.balance.Sub(sdk.NewInt64Coin("matic", defaultFeeAmount)), s.BankKeeper)
} else {
checkBalance(t, baseApp, tc.sender, tc.balance, s.BankKeeper)
}

ctx2 := baseApp.NewContext(true)
res2 := s.AccountKeeper.GetAccount(ctx2, tc.sender)
require.NotNil(t, res2)
Expand All @@ -209,6 +215,9 @@ func TestSendNotEnoughBalance(t *testing.T) {
require.Equal(t, origSeq, res2.GetSequence())
}

for _, eb := range tc.expectedBalances {
checkBalance(t, baseApp, eb.addr, eb.coins, s.BankKeeper)
}
})
}

Expand Down Expand Up @@ -246,6 +255,7 @@ func TestMsgMultiSendWithAccounts(t *testing.T) {
expectedBalances: []expectedBalance{
{addr1, sdk.Coins{sdk.NewInt64Coin("matic", 57*defaultFeeAmount)}},
{addr2, sdk.Coins{sdk.NewInt64Coin("matic", 10*defaultFeeAmount)}},
{s.AccountKeeper.GetModuleAddress(authtypes.FeeCollectorName), sdk.Coins{sdk.NewInt64Coin("matic", defaultFeeAmount)}},
},
},
{
Expand All @@ -256,6 +266,18 @@ func TestMsgMultiSendWithAccounts(t *testing.T) {
expSimPass: true, // doesn't check signature
expPass: false,
privKeys: []cryptotypes.PrivKey{priv1},
expectedBalances: []expectedBalance{
{addr1, sdk.Coins{sdk.NewInt64Coin("matic", 57*defaultFeeAmount)}},
{addr2, sdk.Coins{sdk.NewInt64Coin("matic", 10*defaultFeeAmount)}},
{addr3, sdk.Coins{}},

// TODO HV2: fee_collector's balance should be defaultFeeAmount but since distribution module
// flushes the fees to the its module account at beginning of the block,
// the fee_collector's balance is 0.
// We should replace the native simapp with a modified version that imitates heimdall as much as possible
// to avoid such discrepancies.
{s.AccountKeeper.GetModuleAddress(authtypes.FeeCollectorName), sdk.Coins{}},
},
},
{
desc: "wrong accSeq should not pass Simulate",
Expand All @@ -265,6 +287,15 @@ func TestMsgMultiSendWithAccounts(t *testing.T) {
expSimPass: false,
expPass: false,
privKeys: []cryptotypes.PrivKey{priv1},
expectedBalances: []expectedBalance{
{addr1, sdk.Coins{sdk.NewInt64Coin("matic", 57*defaultFeeAmount)}},
// TODO HV2: fee_collector's balance should be defaultFeeAmount but since distribution module
// flushes the fees to the distribution module account at beginning of the block,
// the fee_collector's balance is 0.
// We should replace the native simapp with a modified version that imitates heimdall as much as possible
// to avoid such discrepancies.
{s.AccountKeeper.GetModuleAddress(authtypes.FeeCollectorName), sdk.Coins{}},
},
},
{
desc: "multiple inputs not allowed",
Expand All @@ -274,6 +305,17 @@ func TestMsgMultiSendWithAccounts(t *testing.T) {
expSimPass: false,
expPass: false,
privKeys: []cryptotypes.PrivKey{priv1},
expectedBalances: []expectedBalance{
{addr1, sdk.Coins{sdk.NewInt64Coin("matic", 57*defaultFeeAmount)}},
{addr2, sdk.Coins{sdk.NewInt64Coin("matic", 10*defaultFeeAmount)}},

// TODO HV2: fee_collector's balance should be defaultFeeAmount but since distribution module
// flushes the fees to the distribution module account at beginning of the block,
// the fee_collector's balance is 0.
// We should replace the native simapp with a modified version that imitates heimdall as much as possible
// to avoid such discrepancies.
{s.AccountKeeper.GetModuleAddress(authtypes.FeeCollectorName), sdk.Coins{}},
},
},
}

Expand Down Expand Up @@ -326,6 +368,7 @@ func TestMsgMultiSendMultipleOut(t *testing.T) {
{addr1, sdk.Coins{sdk.NewInt64Coin("matic", 32*defaultFeeAmount)}},
{addr2, sdk.Coins{sdk.NewInt64Coin("matic", 47*defaultFeeAmount)}},
{addr3, sdk.Coins{sdk.NewInt64Coin("matic", 5*defaultFeeAmount)}},
{s.AccountKeeper.GetModuleAddress(authtypes.FeeCollectorName), sdk.Coins{sdk.NewInt64Coin("matic", defaultFeeAmount)}},
},
},
}
Expand Down Expand Up @@ -371,6 +414,7 @@ func TestMsgMultiSendDependent(t *testing.T) {
expectedBalances: []expectedBalance{
{addr1, sdk.Coins{sdk.NewInt64Coin("matic", 32*defaultFeeAmount)}},
{addr2, sdk.Coins{sdk.NewInt64Coin("matic", 11*defaultFeeAmount)}},
{s.AccountKeeper.GetModuleAddress(authtypes.FeeCollectorName), sdk.Coins{sdk.NewInt64Coin("matic", defaultFeeAmount)}},
},
},
{
Expand All @@ -382,6 +426,14 @@ func TestMsgMultiSendDependent(t *testing.T) {
privKeys: []cryptotypes.PrivKey{priv2},
expectedBalances: []expectedBalance{
{addr1, sdk.Coins{sdk.NewInt64Coin("matic", 42*defaultFeeAmount)}},
{addr2, sdk.Coins{}},

// TODO HV2: fee_collector's balance should be 2*defaultFeeAmount but since distribution module
// flushes the fees to the distribution module account at beginning of the block,
// the fee_collector's balance is 0.
// We should replace the native simapp with a modified version that imitates heimdall as much as possible
// to avoid such discrepancies.
{s.AccountKeeper.GetModuleAddress(authtypes.FeeCollectorName), sdk.Coins{sdk.NewInt64Coin("matic", defaultFeeAmount)}},
},
},
}
Expand Down

0 comments on commit 46dd21b

Please sign in to comment.