Skip to content

Commit d365214

Browse files
committed
test: add ante test
1 parent 04f578c commit d365214

File tree

8 files changed

+330
-82
lines changed

8 files changed

+330
-82
lines changed

app/app.go

+5
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,11 @@ func (app *MigalooApp) LoadHeight(height int64) error {
10161016
return app.LoadVersion(height)
10171017
}
10181018

1019+
// InterfaceRegistry returns Migaloo InterfaceRegistry
1020+
func (app *MigalooApp) InterfaceRegistry() types.InterfaceRegistry {
1021+
return app.interfaceRegistry
1022+
}
1023+
10191024
// ModuleAccountAddrs returns all the app's module account addresses.
10201025
func (app *MigalooApp) ModuleAccountAddrs() map[string]bool {
10211026
modAccAddrs := make(map[string]bool)
File renamed without changes.

x/feeburn/ante/ante_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package ante_test
2+
3+
import (
4+
config "github.com/White-Whale-Defi-Platform/migaloo-chain/v3/app/params"
5+
"github.com/White-Whale-Defi-Platform/migaloo-chain/v3/x/feeburn/ante"
6+
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
7+
sdk "github.com/cosmos/cosmos-sdk/types"
8+
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
9+
"math"
10+
)
11+
12+
func (suite *AnteTestSuite) TestFeeBurnDecorator() {
13+
suite.SetupTest(false) // reset
14+
15+
fbd := ante.NewDeductFeeDecorator(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.FeeGrantKeeper, nil, suite.app.FeeBurnKeeper)
16+
antehandler := sdk.ChainAnteDecorators(fbd)
17+
18+
priv := ed25519.GenPrivKey()
19+
addr := getAddr(priv)
20+
accountAddress := sdk.AccAddress(priv.PubKey().Address().Bytes())
21+
privNew := ed25519.GenPrivKey()
22+
addrRecv := getAddr(privNew)
23+
24+
accBalance := sdk.Coins{{Denom: config.BaseDenom, Amount: sdk.NewInt(int64(math.Pow10(18) * 2))}}
25+
err := suite.FundAccount(suite.ctx, addr, accBalance)
26+
suite.Require().NoError(err)
27+
28+
sendAmount := sdk.NewCoin(config.BaseDenom, sdk.NewInt(10))
29+
amount := sdk.Coins{sendAmount}
30+
sendMsg := banktypes.NewMsgSend(accountAddress, addrRecv, amount)
31+
txBuilder := prepareCosmosTx(priv, sendMsg)
32+
_, err = antehandler(suite.ctx, txBuilder.GetTx(), false)
33+
34+
suite.Require().NoError(err, "Did not error on invalid tx")
35+
}
36+
37+
func (suite *AnteTestSuite) TestFeeBurnDecoratorWhenTxNull() {
38+
suite.SetupTest(false) // reset
39+
40+
fbd := ante.NewDeductFeeDecorator(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.FeeGrantKeeper, nil, suite.app.FeeBurnKeeper)
41+
antehandler := sdk.ChainAnteDecorators(fbd)
42+
43+
priv := ed25519.GenPrivKey()
44+
addr := getAddr(priv)
45+
accBalance := sdk.Coins{{Denom: config.BaseDenom, Amount: sdk.NewInt(int64(math.Pow10(18) * 2))}}
46+
err := suite.FundAccount(suite.ctx, addr, accBalance)
47+
suite.Require().NoError(err)
48+
_, err = antehandler(suite.ctx, nil, false)
49+
suite.Require().Error(err, "Tx must be a FeeTx")
50+
}

x/feeburn/ante/utils_test.go

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package ante_test
2+
3+
import (
4+
"github.com/White-Whale-Defi-Platform/migaloo-chain/v3/app"
5+
config "github.com/White-Whale-Defi-Platform/migaloo-chain/v3/app/params"
6+
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
7+
"github.com/cosmos/cosmos-sdk/client/tx"
8+
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
9+
"github.com/cosmos/cosmos-sdk/types/tx/signing"
10+
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
11+
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
12+
"github.com/cosmos/ibc-go/v7/testing/simapp"
13+
"math"
14+
"testing"
15+
16+
"github.com/stretchr/testify/suite"
17+
18+
"github.com/cosmos/cosmos-sdk/client"
19+
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
20+
"github.com/cosmos/cosmos-sdk/testutil/testdata"
21+
sdk "github.com/cosmos/cosmos-sdk/types"
22+
"github.com/cosmos/cosmos-sdk/x/auth/ante"
23+
"github.com/cosmos/cosmos-sdk/x/auth/types"
24+
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
25+
)
26+
27+
var (
28+
DEFAULT_FEE int64 = 1000000000000
29+
)
30+
var s *AnteTestSuite
31+
32+
// TestAccount represents an account used in the tests in x/auth/ante.
33+
type TestAccount struct {
34+
acc types.AccountI
35+
priv cryptotypes.PrivKey
36+
}
37+
38+
// AnteTestSuite is a test suite to be used with ante handler tests.
39+
type AnteTestSuite struct {
40+
suite.Suite
41+
42+
app *app.MigalooApp
43+
anteHandler sdk.AnteHandler
44+
ctx sdk.Context
45+
clientCtx client.Context
46+
txBuilder client.TxBuilder
47+
}
48+
49+
// returns context and app with params set on account keeper
50+
func createTestApp(isCheckTx bool) (*app.MigalooApp, sdk.Context) {
51+
app := app.Setup(false)
52+
ctx := app.BaseApp.NewContext(isCheckTx, tmproto.Header{})
53+
app.AccountKeeper.SetParams(ctx, authtypes.DefaultParams())
54+
55+
return app, ctx
56+
}
57+
58+
// SetupTest setups a new test, with new app, context, and anteHandler.
59+
func (suite *AnteTestSuite) SetupTest(isCheckTx bool) {
60+
suite.app, suite.ctx = createTestApp(isCheckTx)
61+
suite.ctx = suite.ctx.WithBlockHeight(1)
62+
63+
// Set up TxConfig.
64+
encodingConfig := simapp.MakeTestEncodingConfig()
65+
// We're using TestMsg encoding in some tests, so register it here.
66+
encodingConfig.Amino.RegisterConcrete(&testdata.TestMsg{}, "testdata.TestMsg", nil)
67+
testdata.RegisterInterfaces(encodingConfig.InterfaceRegistry)
68+
69+
suite.clientCtx = client.Context{}.
70+
WithTxConfig(encodingConfig.TxConfig)
71+
72+
anteHandler, err := ante.NewAnteHandler(
73+
ante.HandlerOptions{
74+
AccountKeeper: suite.app.AccountKeeper,
75+
BankKeeper: suite.app.BankKeeper,
76+
FeegrantKeeper: suite.app.FeeGrantKeeper,
77+
SignModeHandler: encodingConfig.TxConfig.SignModeHandler(),
78+
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
79+
},
80+
)
81+
82+
suite.Require().NoError(err)
83+
suite.anteHandler = anteHandler
84+
85+
feePoolBalance := sdk.Coins{{Denom: config.BaseDenom, Amount: sdk.NewInt(int64(math.Pow10(18) * 2))}}
86+
87+
err = suite.app.BankKeeper.MintCoins(suite.ctx, minttypes.ModuleName, feePoolBalance)
88+
suite.Require().NoError(err)
89+
err = suite.app.BankKeeper.SendCoinsFromModuleToModule(suite.ctx, minttypes.ModuleName, authtypes.FeeCollectorName, feePoolBalance)
90+
suite.Require().NoError(err)
91+
}
92+
93+
func TestAnteTestSuite(t *testing.T) {
94+
s = new(AnteTestSuite)
95+
suite.Run(t, s)
96+
}
97+
98+
func getAddr(priv *ed25519.PrivKey) sdk.AccAddress {
99+
return priv.PubKey().Address().Bytes()
100+
}
101+
102+
func prepareCosmosTx(priv *ed25519.PrivKey, msgs ...sdk.Msg) client.TxBuilder {
103+
encodingConfig := config.MakeEncodingConfig()
104+
accountAddress := sdk.AccAddress(priv.PubKey().Address().Bytes())
105+
106+
txBuilder := encodingConfig.TxConfig.NewTxBuilder()
107+
108+
txBuilder.SetGasLimit(1000000)
109+
gasPrice := sdk.NewInt(1)
110+
fees := &sdk.Coins{{Denom: config.BaseDenom, Amount: gasPrice.MulRaw(DEFAULT_FEE)}}
111+
txBuilder.SetFeeAmount(*fees)
112+
err := txBuilder.SetMsgs(msgs...)
113+
s.Require().NoError(err)
114+
115+
seq, err := s.app.AccountKeeper.GetSequence(s.ctx, accountAddress)
116+
s.Require().NoError(err)
117+
118+
// First round: we gather all the signer infos. We use the "set empty
119+
// signature" hack to do that.
120+
sigV2 := signing.SignatureV2{
121+
PubKey: priv.PubKey(),
122+
Data: &signing.SingleSignatureData{
123+
SignMode: encodingConfig.TxConfig.SignModeHandler().DefaultMode(),
124+
Signature: nil,
125+
},
126+
Sequence: seq,
127+
}
128+
129+
sigsV2 := []signing.SignatureV2{sigV2}
130+
131+
err = txBuilder.SetSignatures(sigsV2...)
132+
s.Require().NoError(err)
133+
134+
// Second round: all signer infos are set, so each signer can sign.
135+
accNumber := s.app.AccountKeeper.GetAccount(s.ctx, accountAddress).GetAccountNumber()
136+
signerData := authsigning.SignerData{
137+
ChainID: s.ctx.ChainID(),
138+
AccountNumber: accNumber,
139+
Sequence: seq,
140+
}
141+
sigV2, err = tx.SignWithPrivKey(
142+
encodingConfig.TxConfig.SignModeHandler().DefaultMode(), signerData,
143+
txBuilder, priv, encodingConfig.TxConfig,
144+
seq,
145+
)
146+
s.Require().NoError(err)
147+
148+
sigsV2 = []signing.SignatureV2{sigV2}
149+
err = txBuilder.SetSignatures(sigsV2...)
150+
s.Require().NoError(err)
151+
152+
return txBuilder
153+
}
154+
155+
func (suite *AnteTestSuite) FundAccount(ctx sdk.Context, addr sdk.AccAddress, amounts sdk.Coins) error {
156+
if err := suite.app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, amounts); err != nil {
157+
return err
158+
}
159+
160+
return suite.app.BankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr, amounts)
161+
}

x/feeburn/handler.go

-26
This file was deleted.

x/feeburn/keeper/keeper_test.go

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package keeper_test
2+
3+
import (
4+
"github.com/White-Whale-Defi-Platform/migaloo-chain/v3/app"
5+
config "github.com/White-Whale-Defi-Platform/migaloo-chain/v3/app/params"
6+
"github.com/White-Whale-Defi-Platform/migaloo-chain/v3/x/feeburn/types"
7+
abci "github.com/cometbft/cometbft/abci/types"
8+
"github.com/cometbft/cometbft/crypto/tmhash"
9+
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
10+
tmversion "github.com/cometbft/cometbft/proto/tendermint/version"
11+
"github.com/cometbft/cometbft/version"
12+
"github.com/cosmos/cosmos-sdk/baseapp"
13+
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
14+
sdk "github.com/cosmos/cosmos-sdk/types"
15+
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
16+
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
17+
"github.com/stretchr/testify/suite"
18+
"testing"
19+
"time"
20+
)
21+
22+
type KeeperTestSuite struct {
23+
suite.Suite
24+
app *app.MigalooApp
25+
ctx sdk.Context
26+
queryClient types.QueryClient
27+
validator stakingtypes.Validator
28+
}
29+
30+
func TestKeeperTestSuite(t *testing.T) {
31+
suite.Run(t, new(KeeperTestSuite))
32+
}
33+
34+
func (suite *KeeperTestSuite) SetupTest() {
35+
// t := suite.T()
36+
// init app
37+
suite.app = app.Setup(false)
38+
privCons := ed25519.GenPrivKey()
39+
consAddress := sdk.ConsAddress(privCons.PubKey().Address())
40+
header := tmproto.Header{
41+
Height: 1,
42+
Time: time.Now().UTC(),
43+
ProposerAddress: consAddress.Bytes(),
44+
Version: tmversion.Consensus{
45+
Block: version.BlockProtocol,
46+
},
47+
LastBlockId: tmproto.BlockID{
48+
Hash: tmhash.Sum([]byte("block_id")),
49+
PartSetHeader: tmproto.PartSetHeader{
50+
Total: 11,
51+
Hash: tmhash.Sum([]byte("partset_header")),
52+
},
53+
},
54+
AppHash: tmhash.Sum([]byte("app")),
55+
DataHash: tmhash.Sum([]byte("data")),
56+
EvidenceHash: tmhash.Sum([]byte("evidence")),
57+
ValidatorsHash: tmhash.Sum([]byte("validators")),
58+
NextValidatorsHash: tmhash.Sum([]byte("next_validators")),
59+
ConsensusHash: tmhash.Sum([]byte("consensus")),
60+
LastResultsHash: tmhash.Sum([]byte("last_result")),
61+
}
62+
suite.app = app.Setup(false)
63+
suite.ctx = suite.app.BaseApp.NewContext(false, header)
64+
65+
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry())
66+
types.RegisterQueryServer(queryHelper, suite.app.FeeBurnKeeper)
67+
suite.queryClient = types.NewQueryClient(queryHelper)
68+
69+
params := types.DefaultParams()
70+
params.TxFeeBurnPercent = "50"
71+
suite.app.FeeBurnKeeper.SetParams(suite.ctx, params)
72+
73+
stakingParams := suite.app.StakingKeeper.GetParams(suite.ctx)
74+
stakingParams.BondDenom = config.BaseDenom
75+
suite.app.StakingKeeper.SetParams(suite.ctx, stakingParams)
76+
77+
mintParams := suite.app.MintKeeper.GetParams(suite.ctx)
78+
mintParams.MintDenom = config.BaseDenom
79+
suite.app.MintKeeper.SetParams(suite.ctx, mintParams)
80+
}
81+
82+
// Commit commits and starts a new block with an updated context.
83+
func (suite *KeeperTestSuite) Commit() {
84+
suite.CommitAfter(time.Second * 0)
85+
}
86+
87+
// Commit commits a block at a given time.
88+
func (suite *KeeperTestSuite) CommitAfter(t time.Duration) {
89+
header := suite.ctx.BlockHeader()
90+
suite.app.EndBlock(abci.RequestEndBlock{Height: suite.ctx.BlockHeight()})
91+
suite.app.Commit()
92+
93+
header.Height += 1
94+
header.Time = header.Time.Add(t)
95+
suite.app.BeginBlock(abci.RequestBeginBlock{
96+
Header: header,
97+
})
98+
99+
// update ctx
100+
suite.ctx = suite.app.BaseApp.NewContext(false, header)
101+
102+
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry())
103+
types.RegisterQueryServer(queryHelper, suite.app.FeeBurnKeeper)
104+
suite.queryClient = types.NewQueryClient(queryHelper)
105+
}
106+
107+
func (suite *KeeperTestSuite) FundAccount(ctx sdk.Context, addr sdk.AccAddress, amounts sdk.Coins) error {
108+
if err := suite.app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, amounts); err != nil {
109+
return err
110+
}
111+
112+
return suite.app.BankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr, amounts)
113+
}

x/feeburn/keeper/proposal_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (suite *KeeperTestSuite) TestUpdateTxFeeBurnPercent() {
2626
} else {
2727
suite.Nil(err)
2828
params := suite.app.FeeBurnKeeper.GetParams(suite.ctx)
29-
suite.Equal(tt.fee, params.TxFeeBurnPercent)
29+
suite.Equal(tt.fee, params.GetTxFeeBurnPercent())
3030
}
3131
})
3232
}

0 commit comments

Comments
 (0)