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

test: use testKeeper throughout tests #44

Merged
merged 51 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 50 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
b305db9
clean
aljo242 Nov 29, 2023
abe1866
fix
aljo242 Nov 29, 2023
11cf89a
state query
aljo242 Nov 29, 2023
c11844d
test:
aljo242 Nov 29, 2023
8db5ae3
Merge branch 'feat/query-state' into test/integration
aljo242 Nov 29, 2023
84e8063
wip
aljo242 Nov 29, 2023
ad9cad8
remove test
aljo242 Nov 29, 2023
80cffac
Merge branch 'feat/query-state' into test/integration
aljo242 Nov 29, 2023
c354608
fix
aljo242 Nov 29, 2023
69f64d2
fix
aljo242 Nov 29, 2023
83b539f
wip
aljo242 Nov 29, 2023
642dea1
wrong
aljo242 Nov 30, 2023
70d9fdc
working
aljo242 Nov 30, 2023
e31e3a0
fix
aljo242 Nov 30, 2023
127a2b1
fix
aljo242 Nov 30, 2023
bc36f96
refactor to use params
aljo242 Nov 30, 2023
1935d45
clean
aljo242 Nov 30, 2023
d637493
wip
aljo242 Dec 1, 2023
0361328
Merge branch 'main' into test/integration
aljo242 Dec 1, 2023
127629c
fix
aljo242 Dec 1, 2023
20d1fda
refactor
aljo242 Dec 1, 2023
b1f618c
fix
aljo242 Dec 1, 2023
1e2df2f
fix
aljo242 Dec 1, 2023
d426f2c
rename
aljo242 Dec 1, 2023
03ce1ff
rename
aljo242 Dec 1, 2023
afd8766
sample
aljo242 Dec 1, 2023
1b32f10
encoding
aljo242 Dec 1, 2023
50313e3
sample
aljo242 Dec 1, 2023
16854b2
use sample
aljo242 Dec 1, 2023
aaac5b0
add
aljo242 Dec 3, 2023
33298b9
network suite
aljo242 Dec 3, 2023
e6c2b69
wip
aljo242 Dec 4, 2023
f918b59
refactor
aljo242 Dec 4, 2023
1a410d8
add feemarket
aljo242 Dec 4, 2023
3d5ac57
init integration pkg
aljo242 Dec 4, 2023
9c41daa
update to latest commit
aljo242 Dec 4, 2023
bcd4e1d
go mod tidy
aljo242 Dec 4, 2023
5a82c26
Merge branch 'fix/linter' into test/integration-setup
aljo242 Dec 4, 2023
1b9f50c
lint fix
aljo242 Dec 4, 2023
bb349f9
use encoding
aljo242 Dec 4, 2023
36af52a
init
aljo242 Dec 4, 2023
460ffb4
refactor
aljo242 Dec 5, 2023
4865616
clean
aljo242 Dec 5, 2023
4b7b95b
refactor
aljo242 Dec 5, 2023
b7bff5b
refactor
aljo242 Dec 5, 2023
aa801df
refactor
aljo242 Dec 5, 2023
98479ef
Merge branch 'main' into test/integration-keeper
aljo242 Dec 5, 2023
840d6a9
refactor clean
aljo242 Dec 5, 2023
76229f7
format
aljo242 Dec 5, 2023
9c384e7
Merge branch 'main' into test/integration-keeper
aljo242 Dec 6, 2023
94d31fd
fix
aljo242 Dec 7, 2023
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
41 changes: 41 additions & 0 deletions tests/integration/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,44 @@ func (s *NetworkTestSuite) TestGetState() {
})
}
}

func (s *NetworkTestSuite) TestSpamTx() {
s.T().Parallel()

ctx := s.Network.Validators[0].ClientCtx

common := []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
}
for _, tc := range []struct {
name string

args []string
err error
obj types.State
}{
{
name: "should return default state",
args: common,
obj: types.DefaultState(),
},
} {
// TODO SPAM TX
aljo242 marked this conversation as resolved.
Show resolved Hide resolved

s.T().Run(tc.name, func(t *testing.T) {
tc := tc
out, err := clitestutil.ExecTestCLICmd(ctx, cli.GetStateCmd(), tc.args)
if tc.err != nil {
stat, ok := status.FromError(tc.err)
require.True(t, ok)
require.ErrorIs(t, stat.Err(), tc.err)
} else {
require.NoError(t, err)
var resp types.StateResponse
require.NoError(t, s.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp.State))
require.NotNil(t, resp.State)
require.Equal(t, tc.obj, resp.State)
}
})
}
}
3 changes: 3 additions & 0 deletions x/feemarket/ante/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@ type BankKeeper interface {
type FeeMarketKeeper interface {
GetState(ctx sdk.Context) (feemarkettypes.State, error)
GetMinGasPrices(ctx sdk.Context) (sdk.Coins, error)
GetParams(ctx sdk.Context) (feemarkettypes.Params, error)
SetState(ctx sdk.Context, state feemarkettypes.State) error
SetParams(ctx sdk.Context, params feemarkettypes.Params) error
aljo242 marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion x/feemarket/ante/fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestAnteHandle(t *testing.T) {

for _, tc := range testCases {
t.Run(fmt.Sprintf("Case %s", tc.Name), func(t *testing.T) {
s := antesuite.SetupTestSuite(t)
s := antesuite.SetupTestSuite(t, true)
s.TxBuilder = s.ClientCtx.TxConfig.NewTxBuilder()
args := tc.Malleate(s)

Expand Down
88 changes: 33 additions & 55 deletions x/feemarket/ante/suite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,21 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

appparams "github.com/skip-mev/feemarket/tests/app/params"
"github.com/skip-mev/feemarket/testutils/encoding"
testkeeper "github.com/skip-mev/feemarket/testutils/keeper"
feemarketante "github.com/skip-mev/feemarket/x/feemarket/ante"
"github.com/skip-mev/feemarket/x/feemarket/ante/mocks"
"github.com/skip-mev/feemarket/x/feemarket/keeper"
feemarketpost "github.com/skip-mev/feemarket/x/feemarket/post"
"github.com/skip-mev/feemarket/x/feemarket/types"
)

type TestSuite struct {
Expand All @@ -36,13 +32,14 @@ type TestSuite struct {
ClientCtx client.Context
TxBuilder client.TxBuilder

AccountKeeper authkeeper.AccountKeeper
FeemarketKeeper *keeper.Keeper
BankKeeper *mocks.BankKeeper
FeeGrantKeeper *mocks.FeeGrantKeeper
EncCfg appparams.EncodingConfig
Key *storetypes.KVStoreKey
AuthorityAccount sdk.AccAddress
AccountKeeper feemarketante.AccountKeeper
FeeMarketKeeper feemarketante.FeeMarketKeeper
BankKeeper feemarketante.BankKeeper
FeeGrantKeeper feemarketante.FeeGrantKeeper

MockBankKeeper *mocks.BankKeeper
MockFeeGrantKeeper *mocks.FeeGrantKeeper
EncCfg appparams.EncodingConfig
}

// TestAccount represents an account used in the tests in x/auth/ante.
Expand All @@ -69,57 +66,39 @@ func (s *TestSuite) CreateTestAccounts(numAccs int) []TestAccount {
}

// SetupTestSuite setups a new test, with new app, context, and anteHandler.
func SetupTestSuite(t *testing.T) *TestSuite {
func SetupTestSuite(t *testing.T, mock bool) *TestSuite {
s := &TestSuite{}

s.EncCfg = encoding.MakeTestEncodingConfig()
s.Key = storetypes.NewKVStoreKey(types.StoreKey)
tkey := storetypes.NewTransientStoreKey("transient_test_feemarket")
testCtx := testutil.DefaultContextWithDB(t, s.Key, tkey)
s.Ctx = testCtx.Ctx.WithIsCheckTx(false).WithBlockHeight(1)
cms, db := testCtx.CMS, testCtx.DB

authKey := storetypes.NewKVStoreKey(authtypes.StoreKey)
tkey = storetypes.NewTransientStoreKey("transient_test_auth")
cms.MountStoreWithDB(authKey, storetypes.StoreTypeIAVL, db)
cms.MountStoreWithDB(tkey, storetypes.StoreTypeTransient, db)
err := cms.LoadLatestVersion()
require.NoError(t, err)

maccPerms := map[string][]string{
types.ModuleName: nil,
types.FeeCollectorName: {"burner"},
}

s.AuthorityAccount = authtypes.NewModuleAddress("gov")
s.AccountKeeper = authkeeper.NewAccountKeeper(
s.EncCfg.Codec, authKey, authtypes.ProtoBaseAccount, maccPerms, sdk.Bech32MainPrefix, s.AuthorityAccount.String(),
)

s.FeemarketKeeper = keeper.NewKeeper(
s.EncCfg.Codec,
s.Key,
s.AccountKeeper,
s.AuthorityAccount.String(),
)

err = s.FeemarketKeeper.SetParams(s.Ctx, types.DefaultParams())
require.NoError(t, err)
ctx, testKeepers, _ := testkeeper.NewTestSetup(t)
s.Ctx = ctx

err = s.FeemarketKeeper.SetState(s.Ctx, types.DefaultState())
require.NoError(t, err)

s.BankKeeper = mocks.NewBankKeeper(t)
s.FeeGrantKeeper = mocks.NewFeeGrantKeeper(t)
s.AccountKeeper = testKeepers.AccountKeeper
s.FeeMarketKeeper = testKeepers.FeeMarketKeeper
s.MockBankKeeper = mocks.NewBankKeeper(t)
s.MockFeeGrantKeeper = mocks.NewFeeGrantKeeper(t)

s.ClientCtx = client.Context{}.WithTxConfig(s.EncCfg.TxConfig)
s.TxBuilder = s.ClientCtx.TxConfig.NewTxBuilder()

s.SetupHandlers(mock)
s.SetT(t)
return s
}

func (s *TestSuite) SetupHandlers(mock bool) {
bankKeeper := s.BankKeeper
feeGrantKeeper := s.FeeGrantKeeper
if mock {
bankKeeper = s.MockBankKeeper
feeGrantKeeper = s.MockFeeGrantKeeper
}

// create basic antehandler with the feemarket decorator
anteDecorators := []sdk.AnteDecorator{
authante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
feemarketante.NewFeeMarketCheckDecorator( // fee market replaces fee deduct decorator
s.FeemarketKeeper,
s.FeeMarketKeeper,
),
authante.NewSigGasConsumeDecorator(s.AccountKeeper, authante.DefaultSigVerificationGasConsumer),
}
Expand All @@ -130,14 +109,13 @@ func SetupTestSuite(t *testing.T) *TestSuite {
postDecorators := []sdk.PostDecorator{
feemarketpost.NewFeeMarketDeductDecorator(
s.AccountKeeper,
s.BankKeeper,
s.FeeGrantKeeper,
s.FeemarketKeeper,
bankKeeper,
feeGrantKeeper,
s.FeeMarketKeeper,
),
}

s.PostHandler = sdk.ChainPostDecorators(postDecorators...)
return s
}

// TestCase represents a test case used in test tables.
Expand Down
3 changes: 2 additions & 1 deletion x/feemarket/fuzz/aimd_eip1559_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"testing"

"cosmossdk.io/math"
"github.com/skip-mev/feemarket/x/feemarket/types"
"github.com/stretchr/testify/require"
"pgregory.net/rapid"

"github.com/skip-mev/feemarket/x/feemarket/types"
)

// TestAIMDLearningRate ensure's that the additive increase
Expand Down
3 changes: 2 additions & 1 deletion x/feemarket/fuzz/eip1559_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"testing"

"cosmossdk.io/math"
"github.com/skip-mev/feemarket/x/feemarket/types"
"github.com/stretchr/testify/require"
"pgregory.net/rapid"

"github.com/skip-mev/feemarket/x/feemarket/types"
)

// TestLearningRate ensure's that the learning rate is always
Expand Down
28 changes: 8 additions & 20 deletions x/feemarket/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (

"cosmossdk.io/math"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/stretchr/testify/suite"

appparams "github.com/skip-mev/feemarket/tests/app/params"
"github.com/skip-mev/feemarket/testutils/encoding"
testkeeper "github.com/skip-mev/feemarket/testutils/keeper"
"github.com/skip-mev/feemarket/x/feemarket/keeper"
"github.com/skip-mev/feemarket/x/feemarket/types"
"github.com/skip-mev/feemarket/x/feemarket/types/mocks"
Expand Down Expand Up @@ -39,27 +41,13 @@ func TestKeeperTestSuite(t *testing.T) {

func (s *KeeperTestSuite) SetupTest() {
s.encCfg = encoding.MakeTestEncodingConfig()
s.key = storetypes.NewKVStoreKey(types.StoreKey)
testCtx := testutil.DefaultContextWithDB(s.T(), s.key, storetypes.NewTransientStoreKey("transient_test"))
s.ctx = testCtx.Ctx

s.authorityAccount = []byte("authority")
s.authorityAccount = authtypes.NewModuleAddress(govtypes.ModuleName)
aljo242 marked this conversation as resolved.
Show resolved Hide resolved
s.accountKeeper = mocks.NewAccountKeeper(s.T())
ctx, tk, tm := testkeeper.NewTestSetup(s.T())
aljo242 marked this conversation as resolved.
Show resolved Hide resolved

s.feeMarketKeeper = keeper.NewKeeper(
s.encCfg.Codec,
s.key,
s.accountKeeper,
s.authorityAccount.String(),
)

err := s.feeMarketKeeper.SetParams(s.ctx, types.DefaultParams())
s.Require().NoError(err)

err = s.feeMarketKeeper.SetState(s.ctx, types.DefaultState())
s.Require().NoError(err)

s.msgServer = keeper.NewMsgServer(*s.feeMarketKeeper)
s.ctx = ctx
s.feeMarketKeeper = tk.FeeMarketKeeper
s.msgServer = tm.FeeMarketMsgServer
s.queryServer = keeper.NewQueryServer(*s.feeMarketKeeper)
}

Expand Down
53 changes: 36 additions & 17 deletions x/feemarket/post/fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ func TestDeductCoins(t *testing.T) {
}
for _, tc := range tests {
t.Run(fmt.Sprintf("Case %s", tc.name), func(t *testing.T) {
s := antesuite.SetupTestSuite(t)
s := antesuite.SetupTestSuite(t, true)
acc := s.CreateTestAccounts(1)[0]
if !tc.invalidCoin {
s.BankKeeper.On("SendCoinsFromAccountToModule", s.Ctx, acc.Account.GetAddress(), types.FeeCollectorName, tc.coins).Return(nil).Once()
s.MockBankKeeper.On("SendCoinsFromAccountToModule", s.Ctx, acc.Account.GetAddress(), types.FeeCollectorName, tc.coins).Return(nil).Once()
}

if err := post.DeductCoins(s.BankKeeper, s.Ctx, acc.Account, tc.coins); (err != nil) != tc.wantErr {
if err := post.DeductCoins(s.MockBankKeeper, s.Ctx, acc.Account, tc.coins); (err != nil) != tc.wantErr {
s.Errorf(err, "DeductCoins() error = %v, wantErr %v", err, tc.wantErr)
}
})
Expand Down Expand Up @@ -86,13 +86,13 @@ func TestSendTip(t *testing.T) {
}
for _, tc := range tests {
t.Run(fmt.Sprintf("Case %s", tc.name), func(t *testing.T) {
s := antesuite.SetupTestSuite(t)
s := antesuite.SetupTestSuite(t, true)
accs := s.CreateTestAccounts(2)
if !tc.invalidCoin {
s.BankKeeper.On("SendCoins", s.Ctx, mock.Anything, mock.Anything, tc.coins).Return(nil).Once()
s.MockBankKeeper.On("SendCoins", s.Ctx, mock.Anything, mock.Anything, tc.coins).Return(nil).Once()
}

if err := post.SendTip(s.BankKeeper, s.Ctx, accs[0].Account.GetAddress(), accs[1].Account.GetAddress(), tc.coins); (err != nil) != tc.wantErr {
if err := post.SendTip(s.MockBankKeeper, s.Ctx, accs[0].Account.GetAddress(), accs[1].Account.GetAddress(), tc.coins); (err != nil) != tc.wantErr {
s.Errorf(err, "SendTip() error = %v, wantErr %v", err, tc.wantErr)
}
})
Expand All @@ -110,9 +110,9 @@ func TestPostHandle(t *testing.T) {
testCases := []antesuite.TestCase{
{
Name: "signer has no funds",
Malleate: func(suite *antesuite.TestSuite) antesuite.TestCaseArgs {
accs := suite.CreateTestAccounts(1)
suite.BankKeeper.On("SendCoinsFromAccountToModule", mock.Anything, accs[0].Account.GetAddress(), types.FeeCollectorName, mock.Anything).Return(sdkerrors.ErrInsufficientFunds)
Malleate: func(s *antesuite.TestSuite) antesuite.TestCaseArgs {
accs := s.CreateTestAccounts(1)
s.MockBankKeeper.On("SendCoinsFromAccountToModule", mock.Anything, accs[0].Account.GetAddress(), types.FeeCollectorName, mock.Anything).Return(sdkerrors.ErrInsufficientFunds)

return antesuite.TestCaseArgs{
Msgs: []sdk.Msg{testdata.NewTestMsg(accs[0].Account.GetAddress())},
Expand Down Expand Up @@ -145,9 +145,9 @@ func TestPostHandle(t *testing.T) {
},
{
Name: "signer has enough funds, should pass, no tip",
Malleate: func(suite *antesuite.TestSuite) antesuite.TestCaseArgs {
accs := suite.CreateTestAccounts(1)
suite.BankKeeper.On("SendCoinsFromAccountToModule", mock.Anything, accs[0].Account.GetAddress(), types.FeeCollectorName, mock.Anything).Return(nil)
Malleate: func(s *antesuite.TestSuite) antesuite.TestCaseArgs {
accs := s.CreateTestAccounts(1)
s.MockBankKeeper.On("SendCoinsFromAccountToModule", mock.Anything, accs[0].Account.GetAddress(), types.FeeCollectorName, mock.Anything).Return(nil)

return antesuite.TestCaseArgs{
Msgs: []sdk.Msg{testdata.NewTestMsg(accs[0].Account.GetAddress())},
Expand All @@ -163,10 +163,29 @@ func TestPostHandle(t *testing.T) {
},
{
Name: "signer has enough funds, should pass with tip",
Malleate: func(suite *antesuite.TestSuite) antesuite.TestCaseArgs {
accs := suite.CreateTestAccounts(1)
suite.BankKeeper.On("SendCoinsFromAccountToModule", mock.Anything, accs[0].Account.GetAddress(), types.FeeCollectorName, mock.Anything).Return(nil)
suite.BankKeeper.On("SendCoins", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
Malleate: func(s *antesuite.TestSuite) antesuite.TestCaseArgs {
accs := s.CreateTestAccounts(1)
s.MockBankKeeper.On("SendCoinsFromAccountToModule", mock.Anything, accs[0].Account.GetAddress(), types.FeeCollectorName, mock.Anything).Return(nil)
s.MockBankKeeper.On("SendCoins", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()

return antesuite.TestCaseArgs{
Msgs: []sdk.Msg{testdata.NewTestMsg(accs[0].Account.GetAddress())},
GasLimit: gasLimit,
FeeAmount: validFeeWithTip,
}
},
RunAnte: true,
RunPost: true,
Simulate: false,
ExpPass: true,
ExpErr: nil,
},
{
Name: "signer has enough funds, should pass with tip",
Malleate: func(s *antesuite.TestSuite) antesuite.TestCaseArgs {
accs := s.CreateTestAccounts(1)
s.MockBankKeeper.On("SendCoinsFromAccountToModule", mock.Anything, accs[0].Account.GetAddress(), types.FeeCollectorName, mock.Anything).Return(nil)
s.MockBankKeeper.On("SendCoins", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()

return antesuite.TestCaseArgs{
Msgs: []sdk.Msg{testdata.NewTestMsg(accs[0].Account.GetAddress())},
Expand All @@ -184,7 +203,7 @@ func TestPostHandle(t *testing.T) {

for _, tc := range testCases {
t.Run(fmt.Sprintf("Case %s", tc.Name), func(t *testing.T) {
s := antesuite.SetupTestSuite(t)
s := antesuite.SetupTestSuite(t, true)
s.TxBuilder = s.ClientCtx.TxConfig.NewTxBuilder()
args := tc.Malleate(s)

Expand Down
Loading
Loading