2019-05-18 01:42:24 -07:00
|
|
|
package simapp
|
2018-04-06 22:12:00 -07:00
|
|
|
|
|
|
|
import (
|
2020-08-13 06:20:02 -07:00
|
|
|
"encoding/json"
|
2018-07-25 14:10:07 -07:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2018-12-10 06:27:25 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-02-10 09:49:31 -08:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2018-12-10 06:27:25 -08:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2021-02-10 09:49:31 -08:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2019-08-02 06:20:39 -07:00
|
|
|
dbm "github.com/tendermint/tm-db"
|
2018-12-10 06:27:25 -08:00
|
|
|
|
2021-02-10 09:49:31 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
2018-04-06 22:12:00 -07:00
|
|
|
)
|
|
|
|
|
2021-01-06 07:20:12 -08:00
|
|
|
func TestSimAppExportAndBlockedAddrs(t *testing.T) {
|
|
|
|
encCfg := MakeTestEncodingConfig()
|
2019-08-02 06:20:39 -07:00
|
|
|
db := dbm.NewMemDB()
|
2021-01-06 07:20:12 -08:00
|
|
|
app := NewSimApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{})
|
2018-04-06 22:12:00 -07:00
|
|
|
|
2021-01-06 07:20:12 -08:00
|
|
|
for acc := range maccPerms {
|
|
|
|
require.Equal(t, !allowedReceivingModAcc[acc], app.BankKeeper.BlockedAddr(app.AccountKeeper.GetModuleAddress(acc)),
|
|
|
|
"ensure that blocked addresses are properly set in bank keeper")
|
|
|
|
}
|
|
|
|
|
|
|
|
genesisState := NewDefaultGenesisState(encCfg.Marshaler)
|
2020-08-13 06:20:02 -07:00
|
|
|
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
|
2019-06-05 16:26:17 -07:00
|
|
|
require.NoError(t, err)
|
2018-04-06 22:12:00 -07:00
|
|
|
|
|
|
|
// Initialize the chain
|
2019-05-18 01:42:24 -07:00
|
|
|
app.InitChain(
|
2019-05-16 08:25:32 -07:00
|
|
|
abci.RequestInitChain{
|
|
|
|
Validators: []abci.ValidatorUpdate{},
|
|
|
|
AppStateBytes: stateBytes,
|
|
|
|
},
|
|
|
|
)
|
2019-05-18 01:42:24 -07:00
|
|
|
app.Commit()
|
2019-06-05 16:26:17 -07:00
|
|
|
|
|
|
|
// Making a new app object with the db, so that initchain hasn't been called
|
2021-01-06 07:20:12 -08:00
|
|
|
app2 := NewSimApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{})
|
2020-09-03 03:11:46 -07:00
|
|
|
_, err = app2.ExportAppStateAndValidators(false, []string{})
|
2019-06-05 16:26:17 -07:00
|
|
|
require.NoError(t, err, "ExportAppStateAndValidators should not have an error")
|
2018-04-06 22:12:00 -07:00
|
|
|
}
|
2019-08-13 13:59:01 -07:00
|
|
|
|
|
|
|
func TestGetMaccPerms(t *testing.T) {
|
|
|
|
dup := GetMaccPerms()
|
|
|
|
require.Equal(t, maccPerms, dup, "duplicated module account permissions differed from actual module account permissions")
|
|
|
|
}
|
2021-02-10 09:49:31 -08:00
|
|
|
|
|
|
|
func TestRunMigrations(t *testing.T) {
|
|
|
|
db := dbm.NewMemDB()
|
|
|
|
encCfg := MakeTestEncodingConfig()
|
|
|
|
app := NewSimApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{})
|
|
|
|
|
|
|
|
// Create a new configurator for the purpose of this test.
|
|
|
|
app.configurator = module.NewConfigurator(app.MsgServiceRouter(), app.GRPCQueryRouter())
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
moduleName string
|
|
|
|
forVersion uint64
|
|
|
|
expRegErr bool // errors while registering migration
|
|
|
|
expRegErrMsg string
|
|
|
|
expRunErr bool // errors while running migration
|
|
|
|
expRunErrMsg string
|
|
|
|
expCalled int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"cannot register migration for version 0",
|
|
|
|
"bank", 0,
|
|
|
|
true, "module migration versions should start at 1: invalid version", false, "", 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"throws error on RunMigrations if no migration registered for bank",
|
|
|
|
"", 1,
|
|
|
|
false, "", true, "no migrations found for module bank: not found", 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"can register and run migration handler for x/bank",
|
|
|
|
"bank", 1,
|
|
|
|
false, "", false, "", 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cannot register migration handler for same module & forVersion",
|
|
|
|
"bank", 1,
|
|
|
|
true, "another migration for module bank and version 1 already exists: internal logic error", false, "", 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Since it's very hard to test actual in-place store migrations in
|
2021-02-19 07:02:53 -08:00
|
|
|
// tests (due to the difficulty of maintaining multiple versions of a
|
2021-02-10 09:49:31 -08:00
|
|
|
// module), we're just testing here that the migration logic is
|
|
|
|
// called.
|
|
|
|
called := 0
|
|
|
|
|
|
|
|
if tc.moduleName != "" {
|
|
|
|
// Register migration for module from version `forVersion` to `forVersion+1`.
|
|
|
|
err = app.configurator.RegisterMigration(tc.moduleName, tc.forVersion, func(sdk.Context) error {
|
|
|
|
called++
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if tc.expRegErr {
|
|
|
|
require.EqualError(t, err, tc.expRegErrMsg)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = app.RunMigrations(
|
|
|
|
app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()}),
|
|
|
|
module.MigrationMap{
|
|
|
|
"auth": 1, "authz": 1, "bank": 1, "staking": 1, "mint": 1, "distribution": 1,
|
|
|
|
"slashing": 1, "gov": 1, "params": 1, "ibc": 1, "upgrade": 1, "vesting": 1,
|
|
|
|
"feegrant": 1, "transfer": 1, "evidence": 1, "crisis": 1, "genutil": 1, "capability": 1,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if tc.expRunErr {
|
|
|
|
require.EqualError(t, err, tc.expRunErrMsg)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.expCalled, called)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|