cosmos-sdk/x/crisis/handler_test.go
frog power 4000 df4394185e
Merge PR #3656: Broken-Invar Tx - aka. Crisis module
* beginning thinking on issue

* ...

* working

* working

* working fee pool distribution

* spek outline

* spec update

* gas refund calculations

* simulation saved to ~/.gaiad/simulations/

* lean simulation output

int

* cleanup bank simulation messages

* operation messges

int

* lint

* move simulation to its own module

* move simulation log code to log.go

* logger overhaul

int

* distribution comments

* fix compiling

* cleanup modifications to x/distribution/keeper/allocation.go

int

int

int

* gov bug

* result.IsOK() minimization

* importExport typo bug

* pending

* address @alexanderbez comments

* simple @cwgoes comments addressed

* event logging unified approach

* distr module name constant

* implementing

* compiles

* gaia integration

* proper constant fee removal

* crisis genesis

* go.sum update

* ...

* debugging

* fix sum errors

* missing err checks

* working implementing CLI

* remove query command

* crisis expected keepers in other modules

* crisis testing infrastructure

* working

* tests complete

* modify handler to still panic if not enough pool coins, docs working

* spec tags

* docs complete

* CL

* assert invariants on a blockly basis gaiad functionality

* gaiad CL

* transaction details in runtime invariance panic

* Apply suggestions from code review

Co-Authored-By: rigelrozanski <rigel.rozanski@gmail.com>

* sender tags

* @mossid suggestions

int

* @cwgoes comments final

* Apply suggestions from code review

Co-Authored-By: rigelrozanski <rigel.rozanski@gmail.com>

* bug seems fixed (#3998)

* delete unused line in zero height export bug
2019-03-28 19:27:47 -04:00

101 lines
3.5 KiB
Go

package crisis
import (
"errors"
"fmt"
"testing"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/stretchr/testify/require"
)
var (
testModuleName = "dummy"
dummyRouteWhichPasses = NewInvarRoute(testModuleName, "which-passes", func(_ sdk.Context) error { return nil })
dummyRouteWhichFails = NewInvarRoute(testModuleName, "which-fails", func(_ sdk.Context) error { return errors.New("whoops") })
addrs = distr.TestAddrs
)
func CreateTestInput(t *testing.T) (sdk.Context, Keeper, auth.AccountKeeper, distr.Keeper) {
communityTax := sdk.NewDecWithPrec(2, 2)
ctx, accKeeper, bankKeeper, distrKeeper, _, feeCollectionKeeper, paramsKeeper :=
distr.CreateTestInputAdvanced(t, false, 10, communityTax)
paramSpace := paramsKeeper.Subspace(DefaultParamspace)
crisisKeeper := NewKeeper(paramSpace, distrKeeper, bankKeeper, feeCollectionKeeper)
constantFee := sdk.NewInt64Coin("stake", 10000000)
crisisKeeper.SetConstantFee(ctx, constantFee)
crisisKeeper.RegisterRoute(testModuleName, dummyRouteWhichPasses.Route, dummyRouteWhichPasses.Invar)
crisisKeeper.RegisterRoute(testModuleName, dummyRouteWhichFails.Route, dummyRouteWhichFails.Invar)
// set the community pool to pay back the constant fee
feePool := distr.InitialFeePool()
feePool.CommunityPool = sdk.NewDecCoins(sdk.NewCoins(constantFee))
distrKeeper.SetFeePool(ctx, feePool)
return ctx, crisisKeeper, accKeeper, distrKeeper
}
//____________________________________________________________________________
func TestHandleMsgVerifyInvariantWithNotEnoughSenderCoins(t *testing.T) {
ctx, crisisKeeper, accKeeper, _ := CreateTestInput(t)
sender := addrs[0]
coin := accKeeper.GetAccount(ctx, sender).GetCoins()[0]
excessCoins := sdk.NewCoin(coin.Denom, coin.Amount.AddRaw(1))
crisisKeeper.SetConstantFee(ctx, excessCoins)
msg := NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichPasses.Route)
res := handleMsgVerifyInvariant(ctx, msg, crisisKeeper)
require.False(t, res.IsOK())
}
func TestHandleMsgVerifyInvariantWithBadInvariant(t *testing.T) {
ctx, crisisKeeper, _, _ := CreateTestInput(t)
sender := addrs[0]
msg := NewMsgVerifyInvariant(sender, testModuleName, "route-that-doesnt-exist")
res := handleMsgVerifyInvariant(ctx, msg, crisisKeeper)
require.False(t, res.IsOK())
}
func TestHandleMsgVerifyInvariantWithInvariantBroken(t *testing.T) {
ctx, crisisKeeper, _, _ := CreateTestInput(t)
sender := addrs[0]
msg := NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichFails.Route)
var res sdk.Result
require.Panics(t, func() {
res = handleMsgVerifyInvariant(ctx, msg, crisisKeeper)
}, fmt.Sprintf("%v", res))
}
func TestHandleMsgVerifyInvariantWithInvariantBrokenAndNotEnoughPoolCoins(t *testing.T) {
ctx, crisisKeeper, _, distrKeeper := CreateTestInput(t)
sender := addrs[0]
// set the community pool to empty
feePool := distrKeeper.GetFeePool(ctx)
feePool.CommunityPool = sdk.DecCoins{}
distrKeeper.SetFeePool(ctx, feePool)
msg := NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichFails.Route)
var res sdk.Result
require.Panics(t, func() {
res = handleMsgVerifyInvariant(ctx, msg, crisisKeeper)
}, fmt.Sprintf("%v", res))
}
func TestHandleMsgVerifyInvariantWithInvariantNotBroken(t *testing.T) {
ctx, crisisKeeper, _, _ := CreateTestInput(t)
sender := addrs[0]
msg := NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichPasses.Route)
res := handleMsgVerifyInvariant(ctx, msg, crisisKeeper)
require.True(t, res.IsOK())
}