|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/golang/mock/gomock" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + abci "github.com/tendermint/tendermint/abci/types" |
| 11 | + "github.com/tendermint/tendermint/libs/log" |
| 12 | + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" |
| 13 | + dbm "github.com/tendermint/tm-db" |
| 14 | + |
| 15 | + "github.com/cosmos/cosmos-sdk/baseapp" |
| 16 | + "github.com/cosmos/cosmos-sdk/tests/mocks" |
| 17 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 18 | + "github.com/cosmos/cosmos-sdk/types/module" |
| 19 | + "github.com/cosmos/cosmos-sdk/x/auth" |
| 20 | + "github.com/cosmos/cosmos-sdk/x/auth/vesting" |
| 21 | + authzmodule "github.com/cosmos/cosmos-sdk/x/authz/module" |
| 22 | + "github.com/cosmos/cosmos-sdk/x/bank" |
| 23 | + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" |
| 24 | + "github.com/cosmos/cosmos-sdk/x/capability" |
| 25 | + "github.com/cosmos/cosmos-sdk/x/crisis" |
| 26 | + "github.com/cosmos/cosmos-sdk/x/distribution" |
| 27 | + "github.com/cosmos/cosmos-sdk/x/evidence" |
| 28 | + feegrantmodule "github.com/cosmos/cosmos-sdk/x/feegrant/module" |
| 29 | + "github.com/cosmos/cosmos-sdk/x/genutil" |
| 30 | + "github.com/cosmos/cosmos-sdk/x/gov" |
| 31 | + "github.com/cosmos/cosmos-sdk/x/mint" |
| 32 | + "github.com/cosmos/cosmos-sdk/x/params" |
| 33 | + "github.com/cosmos/cosmos-sdk/x/slashing" |
| 34 | + "github.com/cosmos/cosmos-sdk/x/staking" |
| 35 | + "github.com/cosmos/cosmos-sdk/x/upgrade" |
| 36 | + |
| 37 | + "github.com/tendermint/liquidity/x/liquidity" |
| 38 | +) |
| 39 | + |
| 40 | +func TestSimAppExportAndBlockedAddrs(t *testing.T) { |
| 41 | + encCfg := MakeEncodingConfig() |
| 42 | + db := dbm.NewMemDB() |
| 43 | + app := NewLiquidityApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{}) |
| 44 | + |
| 45 | + for acc := range maccPerms { |
| 46 | + require.True( |
| 47 | + t, |
| 48 | + app.BankKeeper.BlockedAddr(app.AccountKeeper.GetModuleAddress(acc)), |
| 49 | + "ensure that blocked addresses are properly set in bank keeper", |
| 50 | + ) |
| 51 | + } |
| 52 | + |
| 53 | + genesisState := NewDefaultGenesisState() |
| 54 | + stateBytes, err := json.MarshalIndent(genesisState, "", " ") |
| 55 | + require.NoError(t, err) |
| 56 | + |
| 57 | + // Initialize the chain |
| 58 | + app.InitChain( |
| 59 | + abci.RequestInitChain{ |
| 60 | + Validators: []abci.ValidatorUpdate{}, |
| 61 | + AppStateBytes: stateBytes, |
| 62 | + }, |
| 63 | + ) |
| 64 | + app.Commit() |
| 65 | + |
| 66 | + // Making a new app object with the db, so that initchain hasn't been called |
| 67 | + app2 := NewLiquidityApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{}) |
| 68 | + _, err = app2.ExportAppStateAndValidators(false, []string{}) |
| 69 | + require.NoError(t, err, "ExportAppStateAndValidators should not have an error") |
| 70 | +} |
| 71 | + |
| 72 | +func TestGetMaccPerms(t *testing.T) { |
| 73 | + dup := GetMaccPerms() |
| 74 | + require.Equal(t, maccPerms, dup, "duplicated module account permissions differed from actual module account permissions") |
| 75 | +} |
| 76 | + |
| 77 | +func TestRunMigrations(t *testing.T) { |
| 78 | + db := dbm.NewMemDB() |
| 79 | + encCfg := MakeEncodingConfig() |
| 80 | + logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)) |
| 81 | + app := NewLiquidityApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{}) |
| 82 | + |
| 83 | + // Create a new baseapp and configurator for the purpose of this test. |
| 84 | + bApp := baseapp.NewBaseApp(appName, logger, db, encCfg.TxConfig.TxDecoder()) |
| 85 | + bApp.SetCommitMultiStoreTracer(nil) |
| 86 | + bApp.SetInterfaceRegistry(encCfg.InterfaceRegistry) |
| 87 | + app.BaseApp = bApp |
| 88 | + app.configurator = module.NewConfigurator(app.appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter()) |
| 89 | + |
| 90 | + // We register all modules on the Configurator, except x/bank. x/bank will |
| 91 | + // serve as the test subject on which we run the migration tests. |
| 92 | + // |
| 93 | + // The loop below is the same as calling `RegisterServices` on |
| 94 | + // ModuleManager, except that we skip x/bank. |
| 95 | + for _, module := range app.mm.Modules { |
| 96 | + if module.Name() == banktypes.ModuleName { |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + module.RegisterServices(app.configurator) |
| 101 | + } |
| 102 | + |
| 103 | + // Initialize the chain |
| 104 | + app.InitChain(abci.RequestInitChain{}) |
| 105 | + app.Commit() |
| 106 | + |
| 107 | + testCases := []struct { |
| 108 | + name string |
| 109 | + moduleName string |
| 110 | + forVersion uint64 |
| 111 | + expRegErr bool // errors while registering migration |
| 112 | + expRegErrMsg string |
| 113 | + expRunErr bool // errors while running migration |
| 114 | + expRunErrMsg string |
| 115 | + expCalled int |
| 116 | + }{ |
| 117 | + { |
| 118 | + "cannot register migration for version 0", |
| 119 | + "bank", 0, |
| 120 | + true, "module migration versions should start at 1: invalid version", false, "", 0, |
| 121 | + }, |
| 122 | + { |
| 123 | + "throws error on RunMigrations if no migration registered for bank", |
| 124 | + "", 1, |
| 125 | + false, "", true, "no migrations found for module bank: not found", 0, |
| 126 | + }, |
| 127 | + { |
| 128 | + "can register and run migration handler for x/bank", |
| 129 | + "bank", 1, |
| 130 | + false, "", false, "", 1, |
| 131 | + }, |
| 132 | + { |
| 133 | + "cannot register migration handler for same module & forVersion", |
| 134 | + "bank", 1, |
| 135 | + true, "another migration for module bank and version 1 already exists: internal logic error", false, "", 0, |
| 136 | + }, |
| 137 | + } |
| 138 | + |
| 139 | + for _, tc := range testCases { |
| 140 | + t.Run(tc.name, func(t *testing.T) { |
| 141 | + var err error |
| 142 | + |
| 143 | + // Since it's very hard to test actual in-place store migrations in |
| 144 | + // tests (due to the difficulty of maintaining multiple versions of a |
| 145 | + // module), we're just testing here that the migration logic is |
| 146 | + // called. |
| 147 | + called := 0 |
| 148 | + |
| 149 | + if tc.moduleName != "" { |
| 150 | + // Register migration for module from version `forVersion` to `forVersion+1`. |
| 151 | + err = app.configurator.RegisterMigration(tc.moduleName, tc.forVersion, func(sdk.Context) error { |
| 152 | + called++ |
| 153 | + |
| 154 | + return nil |
| 155 | + }) |
| 156 | + |
| 157 | + if tc.expRegErr { |
| 158 | + require.EqualError(t, err, tc.expRegErrMsg) |
| 159 | + |
| 160 | + return |
| 161 | + } |
| 162 | + } |
| 163 | + require.NoError(t, err) |
| 164 | + |
| 165 | + // Run migrations only for bank. That's why we put the initial |
| 166 | + // version for bank as 1, and for all other modules, we put as |
| 167 | + // their latest ConsensusVersion. |
| 168 | + _, err = app.mm.RunMigrations( |
| 169 | + app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()}), app.configurator, |
| 170 | + module.VersionMap{ |
| 171 | + "bank": 1, |
| 172 | + "auth": auth.AppModule{}.ConsensusVersion(), |
| 173 | + "authz": authzmodule.AppModule{}.ConsensusVersion(), |
| 174 | + "staking": staking.AppModule{}.ConsensusVersion(), |
| 175 | + "mint": mint.AppModule{}.ConsensusVersion(), |
| 176 | + "distribution": distribution.AppModule{}.ConsensusVersion(), |
| 177 | + "slashing": slashing.AppModule{}.ConsensusVersion(), |
| 178 | + "gov": gov.AppModule{}.ConsensusVersion(), |
| 179 | + "params": params.AppModule{}.ConsensusVersion(), |
| 180 | + "upgrade": upgrade.AppModule{}.ConsensusVersion(), |
| 181 | + "vesting": vesting.AppModule{}.ConsensusVersion(), |
| 182 | + "feegrant": feegrantmodule.AppModule{}.ConsensusVersion(), |
| 183 | + "evidence": evidence.AppModule{}.ConsensusVersion(), |
| 184 | + "crisis": crisis.AppModule{}.ConsensusVersion(), |
| 185 | + "genutil": genutil.AppModule{}.ConsensusVersion(), |
| 186 | + "capability": capability.AppModule{}.ConsensusVersion(), |
| 187 | + "liquidity": liquidity.AppModule{}.ConsensusVersion(), |
| 188 | + }, |
| 189 | + ) |
| 190 | + if tc.expRunErr { |
| 191 | + require.EqualError(t, err, tc.expRunErrMsg) |
| 192 | + } else { |
| 193 | + require.NoError(t, err) |
| 194 | + // Make sure bank's migration is called. |
| 195 | + require.Equal(t, tc.expCalled, called) |
| 196 | + } |
| 197 | + }) |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +func TestInitGenesisOnMigration(t *testing.T) { |
| 202 | + db := dbm.NewMemDB() |
| 203 | + encCfg := MakeEncodingConfig() |
| 204 | + logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)) |
| 205 | + app := NewLiquidityApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{}) |
| 206 | + ctx := app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()}) |
| 207 | + |
| 208 | + // Create a mock module. This module will serve as the new module we're |
| 209 | + // adding during a migration. |
| 210 | + mockCtrl := gomock.NewController(t) |
| 211 | + t.Cleanup(mockCtrl.Finish) |
| 212 | + mockModule := mocks.NewMockAppModule(mockCtrl) |
| 213 | + mockDefaultGenesis := json.RawMessage(`{"key": "value"}`) |
| 214 | + mockModule.EXPECT().DefaultGenesis(gomock.Eq(app.appCodec)).Times(1).Return(mockDefaultGenesis) |
| 215 | + mockModule.EXPECT().InitGenesis(gomock.Eq(ctx), gomock.Eq(app.appCodec), gomock.Eq(mockDefaultGenesis)).Times(1).Return(nil) |
| 216 | + mockModule.EXPECT().ConsensusVersion().Times(1).Return(uint64(0)) |
| 217 | + |
| 218 | + app.mm.Modules["mock"] = mockModule |
| 219 | + |
| 220 | + // Run migrations only for "mock" module. We exclude it from |
| 221 | + // the VersionMap to simulate upgrading with a new module. |
| 222 | + _, err := app.mm.RunMigrations(ctx, app.configurator, |
| 223 | + module.VersionMap{ |
| 224 | + "bank": bank.AppModule{}.ConsensusVersion(), |
| 225 | + "auth": auth.AppModule{}.ConsensusVersion(), |
| 226 | + "authz": authzmodule.AppModule{}.ConsensusVersion(), |
| 227 | + "staking": staking.AppModule{}.ConsensusVersion(), |
| 228 | + "mint": mint.AppModule{}.ConsensusVersion(), |
| 229 | + "distribution": distribution.AppModule{}.ConsensusVersion(), |
| 230 | + "slashing": slashing.AppModule{}.ConsensusVersion(), |
| 231 | + "gov": gov.AppModule{}.ConsensusVersion(), |
| 232 | + "params": params.AppModule{}.ConsensusVersion(), |
| 233 | + "upgrade": upgrade.AppModule{}.ConsensusVersion(), |
| 234 | + "vesting": vesting.AppModule{}.ConsensusVersion(), |
| 235 | + "feegrant": feegrantmodule.AppModule{}.ConsensusVersion(), |
| 236 | + "evidence": evidence.AppModule{}.ConsensusVersion(), |
| 237 | + "crisis": crisis.AppModule{}.ConsensusVersion(), |
| 238 | + "genutil": genutil.AppModule{}.ConsensusVersion(), |
| 239 | + "capability": capability.AppModule{}.ConsensusVersion(), |
| 240 | + "liquidity": liquidity.AppModule{}.ConsensusVersion(), |
| 241 | + }, |
| 242 | + ) |
| 243 | + require.NoError(t, err) |
| 244 | +} |
| 245 | + |
| 246 | +func TestUpgradeStateOnGenesis(t *testing.T) { |
| 247 | + encCfg := MakeEncodingConfig() |
| 248 | + db := dbm.NewMemDB() |
| 249 | + app := NewLiquidityApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{}) |
| 250 | + genesisState := NewDefaultGenesisState() |
| 251 | + stateBytes, err := json.MarshalIndent(genesisState, "", " ") |
| 252 | + require.NoError(t, err) |
| 253 | + |
| 254 | + // Initialize the chain |
| 255 | + app.InitChain( |
| 256 | + abci.RequestInitChain{ |
| 257 | + Validators: []abci.ValidatorUpdate{}, |
| 258 | + AppStateBytes: stateBytes, |
| 259 | + }, |
| 260 | + ) |
| 261 | + |
| 262 | + // make sure the upgrade keeper has version map in state |
| 263 | + ctx := app.NewContext(false, tmproto.Header{}) |
| 264 | + vm := app.UpgradeKeeper.GetModuleVersionMap(ctx) |
| 265 | + for v, i := range app.mm.Modules { |
| 266 | + require.Equal(t, vm[v], i.ConsensusVersion()) |
| 267 | + } |
| 268 | +} |
0 commit comments