From 34915fc81aa400cf46c4177b41e276ec87467a86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nina=20/=20=E1=83=9C=E1=83=98=E1=83=9C=E1=83=90?= Date: Thu, 18 Apr 2024 14:50:43 +0200 Subject: [PATCH] refactor: move upgrade unit tests together (#3302) ## Overview Moves all upgrade unit tests in one place --- app/test/ica_upgrade_test.go | 50 ------- app/test/upgrade_test.go | 141 ++++++++++++++++-- .../packetforward_upgrade_test.go | 69 --------- x/minfee/upgrade_test.go | 128 ---------------- 4 files changed, 126 insertions(+), 262 deletions(-) delete mode 100644 app/test/ica_upgrade_test.go delete mode 100644 test/packetforward/packetforward_upgrade_test.go delete mode 100644 x/minfee/upgrade_test.go diff --git a/app/test/ica_upgrade_test.go b/app/test/ica_upgrade_test.go deleted file mode 100644 index 6a86ba42b1..0000000000 --- a/app/test/ica_upgrade_test.go +++ /dev/null @@ -1,50 +0,0 @@ -package app_test - -import ( - "testing" - - "github.com/celestiaorg/celestia-app/v2/test/util" - "github.com/cosmos/cosmos-sdk/x/params/types/proposal" - icahosttypes "github.com/cosmos/ibc-go/v6/modules/apps/27-interchain-accounts/host/types" - "github.com/stretchr/testify/require" - abci "github.com/tendermint/tendermint/abci/types" - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - version "github.com/tendermint/tendermint/proto/tendermint/version" -) - -// TestICA verifies that the ICA module's params are overridden during an -// upgrade from v1 -> v2. -func TestICA(t *testing.T) { - testApp, _ := util.SetupTestAppWithUpgradeHeight(t, 3) - ctx := testApp.NewContext(true, tmproto.Header{ - Version: version.Consensus{ - App: 1, - }, - }) - testApp.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{ - Height: 2, - Version: version.Consensus{App: 1}, - }}) - require.EqualValues(t, 1, testApp.AppVersion()) - - // Query the ICA host module params - gotBefore, err := testApp.ParamsKeeper.Params(ctx, &proposal.QueryParamsRequest{ - Subspace: icahosttypes.SubModuleName, - Key: string(icahosttypes.KeyHostEnabled), - }) - require.NoError(t, err) - require.Equal(t, "", gotBefore.Param.Value) - - // Upgrade from v1 -> v2 - testApp.EndBlock(abci.RequestEndBlock{Height: 2}) - testApp.Commit() - require.EqualValues(t, 2, testApp.AppVersion()) - - newCtx := testApp.NewContext(true, tmproto.Header{Version: version.Consensus{App: 2}}) - got, err := testApp.ParamsKeeper.Params(newCtx, &proposal.QueryParamsRequest{ - Subspace: icahosttypes.SubModuleName, - Key: string(icahosttypes.KeyHostEnabled), - }) - require.NoError(t, err) - require.Equal(t, "true", got.Param.Value) -} diff --git a/app/test/upgrade_test.go b/app/test/upgrade_test.go index 441586c9ad..c0e296a3ae 100644 --- a/app/test/upgrade_test.go +++ b/app/test/upgrade_test.go @@ -1,33 +1,144 @@ package app_test import ( + "encoding/json" + "fmt" + "strings" "testing" + "time" + app "github.com/celestiaorg/celestia-app/v2/app" + "github.com/celestiaorg/celestia-app/v2/app/encoding" v1 "github.com/celestiaorg/celestia-app/v2/pkg/appconsts/v1" v2 "github.com/celestiaorg/celestia-app/v2/pkg/appconsts/v2" "github.com/celestiaorg/celestia-app/v2/test/util" + "github.com/celestiaorg/celestia-app/v2/x/minfee" + "github.com/cosmos/cosmos-sdk/crypto/keyring" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/params/types/proposal" + icahosttypes "github.com/cosmos/ibc-go/v6/modules/apps/27-interchain-accounts/host/types" "github.com/stretchr/testify/require" abci "github.com/tendermint/tendermint/abci/types" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" tmversion "github.com/tendermint/tendermint/proto/tendermint/version" + dbm "github.com/tendermint/tm-db" + + packetforwardtypes "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v6/packetforward/types" + + "github.com/tendermint/tendermint/libs/log" ) -func TestUpgradeAppVersion(t *testing.T) { - testApp, _ := util.SetupTestAppWithUpgradeHeight(t, 3) +// TestAppUpgrades verifies that the all module's params are overridden during an +// upgrade from v1 -> v2 and the app version changes correctly. +func TestAppUpgrades(t *testing.T) { + GlobalMinGasPriceDec, err := sdk.NewDecFromStr(fmt.Sprintf("%f", v2.GlobalMinGasPrice)) + require.NoError(t, err) + + tests := []struct { + module string + subspace string + key string + expectedValue string + }{ + { + module: "MinFee", + subspace: minfee.ModuleName, + key: string(minfee.KeyGlobalMinGasPrice), + expectedValue: GlobalMinGasPriceDec.String(), + }, + { + module: "ICA", + subspace: icahosttypes.SubModuleName, + key: string(icahosttypes.KeyHostEnabled), + expectedValue: "true", + }, + { + module: "PFM", + subspace: packetforwardtypes.ModuleName, + key: string(packetforwardtypes.KeyFeePercentage), + expectedValue: "0.000000000000000000", + }, + } + for _, tt := range tests { + t.Run(tt.module, func(t *testing.T) { + testApp, _ := SetupTestAppWithUpgradeHeight(t, 3) + + supportedVersions := []uint64{v1.Version, v2.Version} + require.Equal(t, supportedVersions, testApp.SupportedVersions()) + + ctx := testApp.NewContext(true, tmproto.Header{ + Version: tmversion.Consensus{ + App: 1, + }, + }) + testApp.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{ + Height: 2, + Version: tmversion.Consensus{App: 1}, + }}) + // app version should not have changed yet + require.EqualValues(t, 1, testApp.AppVersion()) + + // Query the module params + gotBefore, err := testApp.ParamsKeeper.Params(ctx, &proposal.QueryParamsRequest{ + Subspace: tt.subspace, + Key: tt.key, + }) + require.NoError(t, err) + require.Equal(t, "", gotBefore.Param.Value) + + // Upgrade from v1 -> v2 + testApp.EndBlock(abci.RequestEndBlock{Height: 2}) + testApp.Commit() + require.EqualValues(t, 2, testApp.AppVersion()) + + newCtx := testApp.NewContext(true, tmproto.Header{Version: tmversion.Consensus{App: 2}}) + got, err := testApp.ParamsKeeper.Params(newCtx, &proposal.QueryParamsRequest{ + Subspace: tt.subspace, + Key: tt.key, + }) + require.NoError(t, err) + require.Equal(t, tt.expectedValue, strings.Trim(got.Param.Value, "\"")) + }) + } +} + +func SetupTestAppWithUpgradeHeight(t *testing.T, upgradeHeight int64) (*app.App, keyring.Keyring) { + t.Helper() + + db := dbm.NewMemDB() + chainID := "test_chain" + encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...) + testApp := app.New(log.NewNopLogger(), db, nil, 0, encCfg, upgradeHeight, util.EmptyAppOptions{}) + genesisState, _, kr := util.GenesisStateWithSingleValidator(testApp, "account") + stateBytes, err := json.MarshalIndent(genesisState, "", " ") + require.NoError(t, err) + infoResp := testApp.Info(abci.RequestInfo{}) + require.EqualValues(t, 0, infoResp.AppVersion) + cp := app.DefaultInitialConsensusParams() + abciParams := &abci.ConsensusParams{ + Block: &abci.BlockParams{ + MaxBytes: cp.Block.MaxBytes, + MaxGas: cp.Block.MaxGas, + }, + Evidence: &cp.Evidence, + Validator: &cp.Validator, + Version: &cp.Version, + } - supportedVersions := []uint64{v1.Version, v2.Version} + _ = testApp.InitChain( + abci.RequestInitChain{ + Time: time.Now(), + Validators: []abci.ValidatorUpdate{}, + ConsensusParams: abciParams, + AppStateBytes: stateBytes, + ChainId: chainID, + }, + ) - require.Equal(t, supportedVersions, testApp.SupportedVersions()) + // assert that the chain starts with version provided in genesis + infoResp = testApp.Info(abci.RequestInfo{}) + require.EqualValues(t, app.DefaultInitialConsensusParams().Version.AppVersion, infoResp.AppVersion) - testApp.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{ - Height: 2, - Version: tmversion.Consensus{App: 1}, - }}) - // app version should not have changed yet - require.EqualValues(t, 1, testApp.AppVersion()) - respEndBlock := testApp.EndBlock(abci.RequestEndBlock{Height: 2}) - // now the app version changes - require.NotNil(t, respEndBlock.ConsensusParamUpdates.Version) - require.EqualValues(t, 2, respEndBlock.ConsensusParamUpdates.Version.AppVersion) - require.EqualValues(t, 2, testApp.AppVersion()) + _ = testApp.Commit() + return testApp, kr } diff --git a/test/packetforward/packetforward_upgrade_test.go b/test/packetforward/packetforward_upgrade_test.go deleted file mode 100644 index 45e5de5994..0000000000 --- a/test/packetforward/packetforward_upgrade_test.go +++ /dev/null @@ -1,69 +0,0 @@ -package packetforward_test - -import ( - "strings" - "testing" - - v1 "github.com/celestiaorg/celestia-app/v2/pkg/appconsts/v1" - v2 "github.com/celestiaorg/celestia-app/v2/pkg/appconsts/v2" - "github.com/celestiaorg/celestia-app/v2/test/util" - "github.com/cosmos/cosmos-sdk/x/params/types/proposal" - packetforwardtypes "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v6/packetforward/types" - "github.com/stretchr/testify/require" - abci "github.com/tendermint/tendermint/abci/types" - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - version "github.com/tendermint/tendermint/proto/tendermint/version" -) - -// TestPacketForwardMiddlewareAgainstAppUpgrades verifies that the PFM module's params are overridden during an -// upgrade from v1 -> v2. -func TestPacketForwardMiddlewareAgainstAppUpgrades(t *testing.T) { - testApp, _ := util.SetupTestAppWithUpgradeHeight(t, 3) - supportedVersions := []uint64{v1.Version, v2.Version} - require.Equal(t, supportedVersions, testApp.SupportedVersions()) - - ctx := testApp.NewContext(true, tmproto.Header{ - Version: version.Consensus{ - App: 1, - }, - }) - testApp.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{ - Height: 2, - Version: version.Consensus{App: 1}, - }}) - - // app version should not have changed yet - require.EqualValues(t, 1, testApp.AppVersion()) - - // PacketForwardMiddleware should not have been set yet - gotBefore, err := testApp.ParamsKeeper.Params(ctx, &proposal.QueryParamsRequest{ - Subspace: packetforwardtypes.ModuleName, - Key: string(packetforwardtypes.KeyFeePercentage), - }) - require.Equal(t, "", gotBefore.Param.Value) - require.NoError(t, err) - - // now the app version changes - respEndBlock := testApp.EndBlock(abci.RequestEndBlock{Height: 2}) - testApp.Commit() - - require.NotNil(t, respEndBlock.ConsensusParamUpdates.Version) - require.EqualValues(t, 2, respEndBlock.ConsensusParamUpdates.Version.AppVersion) - require.EqualValues(t, 2, testApp.AppVersion()) - - // create a new context after endBlock - newCtx := testApp.NewContext(true, tmproto.Header{ - Version: version.Consensus{ - App: 2, - }, - }) - - got, err := testApp.ParamsKeeper.Params(newCtx, &proposal.QueryParamsRequest{ - Subspace: packetforwardtypes.ModuleName, - Key: string(packetforwardtypes.KeyFeePercentage), - }) - require.NoError(t, err) - - require.NoError(t, err) - require.Equal(t, "0.000000000000000000", strings.Trim(got.Param.Value, "\"")) -} diff --git a/x/minfee/upgrade_test.go b/x/minfee/upgrade_test.go deleted file mode 100644 index 3fb5c02c84..0000000000 --- a/x/minfee/upgrade_test.go +++ /dev/null @@ -1,128 +0,0 @@ -package minfee_test - -import ( - "encoding/json" - "fmt" - "strings" - "testing" - "time" - - "github.com/celestiaorg/celestia-app/v2/app" - "github.com/celestiaorg/celestia-app/v2/app/encoding" - "github.com/celestiaorg/celestia-app/v2/x/minfee" - - v1 "github.com/celestiaorg/celestia-app/v2/pkg/appconsts/v1" - v2 "github.com/celestiaorg/celestia-app/v2/pkg/appconsts/v2" - "github.com/celestiaorg/celestia-app/v2/test/util" - - "github.com/cosmos/cosmos-sdk/crypto/keyring" - - sdk "github.com/cosmos/cosmos-sdk/types" - - "github.com/cosmos/cosmos-sdk/x/params/types/proposal" - "github.com/stretchr/testify/require" - abci "github.com/tendermint/tendermint/abci/types" - "github.com/tendermint/tendermint/libs/log" - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - version "github.com/tendermint/tendermint/proto/tendermint/version" - dbm "github.com/tendermint/tm-db" -) - -func TestUpgradeAppVersion(t *testing.T) { - testApp, _ := setupTestApp(t, 3) - - supportedVersions := []uint64{v1.Version, v2.Version} - - require.Equal(t, supportedVersions, testApp.SupportedVersions()) - - ctx := testApp.NewContext(true, tmproto.Header{ - Version: version.Consensus{ - App: 1, - }, - }) - testApp.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{ - Height: 2, - Version: version.Consensus{App: 1}, - }}) - - // app version should not have changed yet - require.EqualValues(t, 1, testApp.AppVersion()) - - // global min gas price should not have been set yet - gotBefore, err := testApp.ParamsKeeper.Params(ctx, &proposal.QueryParamsRequest{ - Subspace: minfee.ModuleName, - Key: string(minfee.KeyGlobalMinGasPrice), - }) - require.Equal(t, "", gotBefore.Param.Value) - require.NoError(t, err) - - // now the app version changes - respEndBlock := testApp.EndBlock(abci.RequestEndBlock{Height: 2}) - testApp.Commit() - - require.NotNil(t, respEndBlock.ConsensusParamUpdates.Version) - require.EqualValues(t, 2, respEndBlock.ConsensusParamUpdates.Version.AppVersion) - require.EqualValues(t, 2, testApp.AppVersion()) - - // create a new context after endBlock - newCtx := testApp.NewContext(true, tmproto.Header{ - Version: version.Consensus{ - App: 2, - }, - }) - - // global min gas price should be set - got, err := testApp.ParamsKeeper.Params(newCtx, &proposal.QueryParamsRequest{ - Subspace: minfee.ModuleName, - Key: string(minfee.KeyGlobalMinGasPrice), - }) - require.NoError(t, err) - - want, err := sdk.NewDecFromStr(fmt.Sprintf("%f", v2.GlobalMinGasPrice)) - require.NoError(t, err) - require.Equal(t, want.String(), strings.Trim(got.Param.Value, "\"")) -} - -func setupTestApp(t *testing.T, upgradeHeight int64) (*app.App, keyring.Keyring) { - t.Helper() - - db := dbm.NewMemDB() - chainID := "test_chain" - encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...) - testApp := app.New(log.NewNopLogger(), db, nil, 0, encCfg, upgradeHeight, util.EmptyAppOptions{}) - - genesisState, _, kr := util.GenesisStateWithSingleValidator(testApp, "account") - - stateBytes, err := json.MarshalIndent(genesisState, "", " ") - require.NoError(t, err) - infoResp := testApp.Info(abci.RequestInfo{}) - require.EqualValues(t, 0, infoResp.AppVersion) - - cp := app.DefaultInitialConsensusParams() - abciParams := &abci.ConsensusParams{ - Block: &abci.BlockParams{ - MaxBytes: cp.Block.MaxBytes, - MaxGas: cp.Block.MaxGas, - }, - Evidence: &cp.Evidence, - Validator: &cp.Validator, - Version: &cp.Version, - } - - _ = testApp.InitChain( - abci.RequestInitChain{ - Time: time.Now(), - Validators: []abci.ValidatorUpdate{}, - ConsensusParams: abciParams, - AppStateBytes: stateBytes, - ChainId: chainID, - }, - ) - - // assert that the chain starts with version provided in genesis - infoResp = testApp.Info(abci.RequestInfo{}) - require.EqualValues(t, app.DefaultInitialConsensusParams().Version.AppVersion, infoResp.AppVersion) - - _ = testApp.Commit() - return testApp, kr -}