cosmos-sdk/x/crisis/handler_test.go
Aaron Craelius 2f44fbf2ab
Add support for protobuf TxGenerator and SIGN_MODE_DIRECT (#6385)
* Add TxWrapper, encoder, decoder and DirectModeHandler

* fix pkg name

* Update API and leave test TODO's

* Update TxWrapper API

* tests for tx wrapper (#6410)

* WIP: added test for direct mode handler

* updated code

* Add msg

* Update TxWrapper API

* Fix pubkey declaration

* Add pubkey for tests

* Fix SetFee

* Remove logs

* Avoid global var declaration for tests

* Add test for GetPubKeys

* Fix direct signing tests

* Add more test cases for GetSignBytes

* Revert SetFee API

* Remove logs

* Refactor tests

Co-authored-by: anilCSE <anil@vitwit.com>
Co-authored-by: sahith-narahari <sahithnarahari@gmail.com>

* Refactoring

* Refactoring

* Integrate SignatureV2 API

* Fix wrapper tests

* Fix tests

* Linting and API tweaks

* Update API

* WIP on updating API

* Fix tests

* Update to new SigVerifiableTx

* Rename

* Update docs to reflect ADR 020

* proto-gen

* proto docs

* cleanup

* cleanup

* cleanup

* cleanup

* cleanup

* cleanup

* cleanup

* Add tests

* Refactor and improving test coverage

* WIP on test coverage

* WIP on test coverage

* proto-gen

* Fix CompactBitArray.Size() bug

* Rename

* Remove Builder interface

* Address review comments

* Update x/auth/tx/sigs.go

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

* Update x/auth/tx/encoder.go

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

* Update x/auth/tx/encoder.go

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

* Address review feedback

* Fix build issues

* Resolve conflicts

* Fix ValidateBasic test coverage

* Add test for malicious multisig

Co-authored-by: atheeshp <59333759+atheeshp@users.noreply.github.com>
Co-authored-by: anilCSE <anil@vitwit.com>
Co-authored-by: sahith-narahari <sahithnarahari@gmail.com>
Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
2020-07-06 17:03:45 +00:00

125 lines
4.0 KiB
Go

package crisis_test
import (
"fmt"
"testing"
"github.com/cosmos/cosmos-sdk/codec/testdata"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/crisis"
"github.com/cosmos/cosmos-sdk/x/crisis/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
var (
testModuleName = "dummy"
dummyRouteWhichPasses = types.NewInvarRoute(testModuleName, "which-passes", func(_ sdk.Context) (string, bool) { return "", false })
dummyRouteWhichFails = types.NewInvarRoute(testModuleName, "which-fails", func(_ sdk.Context) (string, bool) { return "whoops", true })
)
func createTestApp() (*simapp.SimApp, sdk.Context, []sdk.AccAddress) {
db := dbm.NewMemDB()
app := simapp.NewSimApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, simapp.DefaultNodeHome, 1)
ctx := app.NewContext(true, abci.Header{})
constantFee := sdk.NewInt64Coin(sdk.DefaultBondDenom, 10)
app.CrisisKeeper.SetConstantFee(ctx, constantFee)
app.StakingKeeper.SetParams(ctx, stakingtypes.DefaultParams())
app.CrisisKeeper.RegisterRoute(testModuleName, dummyRouteWhichPasses.Route, dummyRouteWhichPasses.Invar)
app.CrisisKeeper.RegisterRoute(testModuleName, dummyRouteWhichFails.Route, dummyRouteWhichFails.Invar)
feePool := distrtypes.InitialFeePool()
feePool.CommunityPool = sdk.NewDecCoinsFromCoins(sdk.NewCoins(constantFee)...)
app.DistrKeeper.SetFeePool(ctx, feePool)
app.BankKeeper.SetSupply(ctx, banktypes.NewSupply(sdk.Coins{}))
addrs := simapp.AddTestAddrs(app, ctx, 1, sdk.NewInt(10000))
return app, ctx, addrs
}
//____________________________________________________________________________
func TestHandleMsgVerifyInvariant(t *testing.T) {
app, ctx, addrs := createTestApp()
sender := addrs[0]
cases := []struct {
name string
msg sdk.Msg
expectedResult string
}{
{"bad invariant route", types.NewMsgVerifyInvariant(sender, testModuleName, "route-that-doesnt-exist"), "fail"},
{"invariant broken", types.NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichFails.Route), "panic"},
{"invariant passing", types.NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichPasses.Route), "pass"},
{"invalid msg", testdata.NewTestMsg(), "fail"},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
h := crisis.NewHandler(app.CrisisKeeper)
switch tc.expectedResult {
case "fail":
res, err := h(ctx, tc.msg)
require.Error(t, err)
require.Nil(t, res)
case "pass":
res, err := h(ctx, tc.msg)
require.NoError(t, err)
require.NotNil(t, res)
case "panic":
require.Panics(t, func() {
h(ctx, tc.msg) // nolint:errcheck
})
}
})
}
}
func TestHandleMsgVerifyInvariantWithNotEnoughSenderCoins(t *testing.T) {
app, ctx, addrs := createTestApp()
sender := addrs[0]
coin := app.BankKeeper.GetAllBalances(ctx, sender)[0]
excessCoins := sdk.NewCoin(coin.Denom, coin.Amount.AddRaw(1))
app.CrisisKeeper.SetConstantFee(ctx, excessCoins)
h := crisis.NewHandler(app.CrisisKeeper)
msg := types.NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichPasses.Route)
res, err := h(ctx, msg)
require.Error(t, err)
require.Nil(t, res)
}
func TestHandleMsgVerifyInvariantWithInvariantBrokenAndNotEnoughPoolCoins(t *testing.T) {
app, ctx, addrs := createTestApp()
sender := addrs[0]
// set the community pool to empty
feePool := app.DistrKeeper.GetFeePool(ctx)
feePool.CommunityPool = sdk.DecCoins{}
app.DistrKeeper.SetFeePool(ctx, feePool)
h := crisis.NewHandler(app.CrisisKeeper)
msg := types.NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichFails.Route)
var res *sdk.Result
require.Panics(t, func() {
res, _ = h(ctx, msg)
}, fmt.Sprintf("%v", res))
}