Skip to content

Commit

Permalink
feat(upgrade): add upgrade handler for new and modified modules (#783)
Browse files Browse the repository at this point in the history
Co-authored-by: Đỗ Việt Hoàng <[email protected]>
Co-authored-by: Omri <[email protected]>
  • Loading branch information
3 people committed Apr 2, 2024
1 parent 6cd9da4 commit 1a1da34
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 27 deletions.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,7 @@ func (app *App) setupUpgradeHandlers() {
UpgradeName,
v3upgrade.CreateUpgradeHandler(
app.mm, app.configurator,
app.RollappKeeper, app.SequencerKeeper, app.DelayedAckKeeper,
),
)

Expand Down
15 changes: 14 additions & 1 deletion app/apptesting/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
app "github.com/dymensionxyz/dymension/v3/app"
evmtypes "github.com/evmos/ethermint/x/evm/types"
)

// DefaultConsensusParams defines the default Tendermint consensus params used in
Expand Down Expand Up @@ -77,7 +78,19 @@ func SetupTestingApp() (*app.App, app.GenesisState) {
params.SetAddressPrefixes()

newApp := app.New(log.NewNopLogger(), db, nil, true, map[int64]bool{}, app.DefaultNodeHome, 5, encCdc, EmptyAppOptions{})
return newApp, app.NewDefaultGenesisState(encCdc.Codec)

defaultGenesisState := app.NewDefaultGenesisState(encCdc.Codec)

// set EnableCreate to false
if evmGenesisStateJson, found := defaultGenesisState[evmtypes.ModuleName]; found {
// force disable Enable Create of x/evm
var evmGenesisState evmtypes.GenesisState
encCdc.Codec.MustUnmarshalJSON(evmGenesisStateJson, &evmGenesisState)
evmGenesisState.Params.EnableCreate = false
defaultGenesisState[evmtypes.ModuleName] = encCdc.Codec.MustMarshalJSON(&evmGenesisState)
}

return newApp, defaultGenesisState
}

// Setup initializes a new SimApp. A Nop logger is set in SimApp.
Expand Down
11 changes: 0 additions & 11 deletions app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package app
import (
"encoding/json"

evmtypes "github.com/evmos/ethermint/x/evm/types"

"github.com/cosmos/cosmos-sdk/codec"
)

Expand All @@ -20,14 +18,5 @@ type GenesisState map[string]json.RawMessage
// NewDefaultGenesisState generates the default state for the application.
func NewDefaultGenesisState(cdc codec.JSONCodec) GenesisState {
defaultGenesisState := ModuleBasics.DefaultGenesis(cdc)

if evmGenesisStateJson, found := defaultGenesisState[evmtypes.ModuleName]; found {
// force disable Enable Create of x/evm
var evmGenesisState evmtypes.GenesisState
cdc.MustUnmarshalJSON(evmGenesisStateJson, &evmGenesisState)
evmGenesisState.Params.EnableCreate = false
defaultGenesisState[evmtypes.ModuleName] = cdc.MustMarshalJSON(&evmGenesisState)
}

return defaultGenesisState
}
20 changes: 20 additions & 0 deletions app/upgrades/v3/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package v3

import (
sdk "github.com/cosmos/cosmos-sdk/types"
delayedacktypes "github.com/dymensionxyz/dymension/v3/x/delayedack/types"
rollapptypes "github.com/dymensionxyz/dymension/v3/x/rollapp/types"
seqtypes "github.com/dymensionxyz/dymension/v3/x/sequencer/types"
)

type RollappKeeper interface {
SetParams(ctx sdk.Context, params rollapptypes.Params)
}

type SequencerKeeper interface {
SetParams(ctx sdk.Context, params seqtypes.Params)
}

type DelayedAckKeeper interface {
SetParams(ctx sdk.Context, params delayedacktypes.Params)
}
30 changes: 28 additions & 2 deletions app/upgrades/v3/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package v3

import (
"math/big"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
appparams "github.com/dymensionxyz/dymension/v3/app/params"

delayedacktypes "github.com/dymensionxyz/dymension/v3/x/delayedack/types"
rollapptypes "github.com/dymensionxyz/dymension/v3/x/rollapp/types"
seqtypes "github.com/dymensionxyz/dymension/v3/x/sequencer/types"
)

func GetStoreUpgrades() *storetypes.StoreUpgrades {
Expand All @@ -19,12 +26,31 @@ func GetStoreUpgrades() *storetypes.StoreUpgrades {
func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
rollappkeeper RollappKeeper,
seqkeeper SequencerKeeper,
dakeeper DelayedAckKeeper,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
return func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
logger := ctx.Logger().With("upgrade", UpgradeName)

// overwrite params for delayedack module due to proto change
daParams := delayedacktypes.DefaultParams()
dakeeper.SetParams(ctx, daParams)

// overwrite params for rollapp module due to proto change
rollappParams := rollapptypes.DefaultParams()
rollappParams.RollappsEnabled = false
rollappParams.DisputePeriodInBlocks = 120960 // 1 week
rollappkeeper.SetParams(ctx, rollappParams)

// overwrite params for sequencer module due to proto change
seqParams := seqtypes.DefaultParams()
DYM := sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil))
seqParams.MinBond = sdk.NewCoin(appparams.BaseDenom, DYM.Mul(sdk.NewInt(1000))) // 1000DYM
seqkeeper.SetParams(ctx, seqParams)

// Start running the module migrations
logger.Debug("running module migrations ...")
return mm.RunMigrations(ctx, configurator, vm)
return mm.RunMigrations(ctx, configurator, fromVM)
}
}
120 changes: 120 additions & 0 deletions app/upgrades/v3/upgrade_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package v3_test

import (
"fmt"
"math/big"
"testing"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
app "github.com/dymensionxyz/dymension/v3/app"
"github.com/dymensionxyz/dymension/v3/app/apptesting"
incentivestypes "github.com/osmosis-labs/osmosis/v15/x/incentives/types"
"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)

// UpgradeTestSuite defines the structure for the upgrade test suite
type UpgradeTestSuite struct {
suite.Suite
Ctx sdk.Context
App *app.App
}

// SetupTest initializes the necessary items for each test
func (s *UpgradeTestSuite) SetupTest(t *testing.T) {
s.App = apptesting.Setup(t, false)
s.Ctx = s.App.BaseApp.NewContext(false, tmproto.Header{Height: 1, ChainID: "dymension_100-1", Time: time.Now().UTC()})
}

// TestUpgradeTestSuite runs the suite of tests for the upgrade handler
func TestUpgradeTestSuite(t *testing.T) {
suite.Run(t, new(UpgradeTestSuite))
}

var (
DYM = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil))

// CreateGaugeFee is the fee required to create a new gauge.
expectCreateGaugeFee = DYM.Mul(sdk.NewInt(10))
// AddToGagugeFee is the fee required to add to gauge.
expectAddToGaugeFee = sdk.ZeroInt()
)

const (
dummyUpgradeHeight = 5
dummyEndBlockHeight = 10
expectRollappsEnabled = false
expectDisputePeriodInBlocks = 120960
expectMinBond = "1000000000000000000000"
)

// TestUpgrade is a method of UpgradeTestSuite to test the upgrade process.
func (s *UpgradeTestSuite) TestUpgrade() {
testCases := []struct {
msg string
upgrade func()
postUpgrade func() error
expPass bool
}{
{
"Test that upgrade does not panic and sets correct parameters",

func() {
// Run upgrade
s.Ctx = s.Ctx.WithBlockHeight(dummyUpgradeHeight - 1)
plan := upgradetypes.Plan{Name: "v3", Height: dummyUpgradeHeight}
err := s.App.UpgradeKeeper.ScheduleUpgrade(s.Ctx, plan)
s.Require().NoError(err)
_, exists := s.App.UpgradeKeeper.GetUpgradePlan(s.Ctx)
s.Require().True(exists)

s.Ctx = s.Ctx.WithBlockHeight(dummyUpgradeHeight)
// simulate the upgrade process not panic.
s.Require().NotPanics(func() {
// simulate the upgrade process.
s.App.BeginBlocker(s.Ctx, abci.RequestBeginBlock{})
})
},
func() error {
// Post-update validation to ensure parameters are correctly set

// Check Rollapp parameters
rollappParams := s.App.RollappKeeper.GetParams(s.Ctx)
if rollappParams.RollappsEnabled != expectRollappsEnabled || rollappParams.DisputePeriodInBlocks != expectDisputePeriodInBlocks {
return fmt.Errorf("rollapp parameters not set correctly")
}

// Check Sequencer parameters
seqParams := s.App.SequencerKeeper.GetParams(s.Ctx)
if seqParams.MinBond.Amount.String() != expectMinBond {
return fmt.Errorf("sequencer parameters not set correctly")
}

// Check Incentives parameters
if !incentivestypes.CreateGaugeFee.Equal(expectCreateGaugeFee) || !incentivestypes.AddToGaugeFee.Equal(expectAddToGaugeFee) {
return fmt.Errorf("incentives parameters not set correctly")
}

return nil
},
true,
},
}

for _, tc := range testCases {
s.Run(fmt.Sprintf("Case %s", tc.msg), func() {
s.SetupTest(s.T()) // Reset for each case

tc.upgrade()
err := tc.postUpgrade()
if tc.expPass {
s.Require().NoError(err)
} else {
s.Require().Error(err)
}
})
}
}
2 changes: 1 addition & 1 deletion cmd/dymd/cmd/inspect_tendermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func getTendermintState(config *cfg.Config) error {
fmt.Printf("%+v\n", latestBlock)

if bh != state.LastBlockHeight {
// priniting block for state height
// printing block for state height
fmt.Println("LOADING BLOCK FOR STATE HEIGHT")
block := blockStore.LoadBlock(state.LastBlockHeight)
if block == nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ replace (
github.com/evmos/ethermint => github.com/dymensionxyz/ethermint v0.22.0-dymension-v0.4.1
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
github.com/osmosis-labs/osmosis/osmomath => github.com/dymensionxyz/osmosis/osmomath v0.0.6-dym-v0.0.1
github.com/osmosis-labs/osmosis/v15 => github.com/dymensionxyz/osmosis/v15 v15.2.1-0.20240310110736-a0811b5943a5
github.com/osmosis-labs/osmosis/v15 => github.com/dymensionxyz/osmosis/v15 v15.2.0-dymension-v1.1.3

// broken goleveldb
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,8 @@ github.com/dymensionxyz/ethermint v0.22.0-dymension-v0.4.1 h1:J31ASxmDVm7ytjmk9T
github.com/dymensionxyz/ethermint v0.22.0-dymension-v0.4.1/go.mod h1:O2J61ZwM0Vdms6pRa1fb43pwmCuNRctro3AB90WlOc0=
github.com/dymensionxyz/osmosis/osmomath v0.0.6-dym-v0.0.1 h1:59ZE3Ocrn04MUpb5VJgfi24eDcnQ9VBfCSw0Mx1n7OI=
github.com/dymensionxyz/osmosis/osmomath v0.0.6-dym-v0.0.1/go.mod h1:2idySYJxP5YfMAEeSeqD8e7fSchfsI4jn7XFHJgNUsM=
github.com/dymensionxyz/osmosis/v15 v15.2.1-0.20240310110736-a0811b5943a5 h1:fdu2lHVWNsACjqA5A63Y8sSVV8UiIoDKjOScUDTivOc=
github.com/dymensionxyz/osmosis/v15 v15.2.1-0.20240310110736-a0811b5943a5/go.mod h1:BHHGzl86byC0sov3EUfTzyDuIsGY//rcrek6gCOa+fw=
github.com/dymensionxyz/osmosis/v15 v15.2.0-dymension-v1.1.3 h1:8L0sMJfGjIgDmA373Qokoix4kdCrzG1j1lYe//B5kkQ=
github.com/dymensionxyz/osmosis/v15 v15.2.0-dymension-v1.1.3/go.mod h1:BHHGzl86byC0sov3EUfTzyDuIsGY//rcrek6gCOa+fw=
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
Expand Down
5 changes: 0 additions & 5 deletions x/delayedack/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"encoding/json"
"fmt"

// this line is used by starport scaffolding # 1

"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -122,11 +120,8 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
// InitGenesis performs the module's genesis initialization. It returns no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, gs json.RawMessage) []abci.ValidatorUpdate {
var genState types.GenesisState
// Initialize global index to index in genesis state
cdc.MustUnmarshalJSON(gs, &genState)

InitGenesis(ctx, am.keeper, genState)

return []abci.ValidatorUpdate{}
}

Expand Down
5 changes: 1 addition & 4 deletions x/denommetadata/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ var (
// ModuleName defines the module name.
ModuleName = "denommetadata"

// StoreKey defines the primary module store key.
StoreKey = ModuleName

// RouterKey is the message route for slashing.
// RouterKey is the message route for the denommetadata module
RouterKey = ModuleName

// QuerierRoute defines the module's query routing key.
Expand Down

0 comments on commit 1a1da34

Please sign in to comment.