From a84b1837e79384f54b6069b3718cc4eae28762e0 Mon Sep 17 00:00:00 2001 From: Uhyun Park <96884119+uhyunpark@users.noreply.github.com> Date: Fri, 16 Aug 2024 15:20:42 +0900 Subject: [PATCH] upgrade: cosmos to 0.47, evmos to v19 (#203) --- app/app.go | 32 +++++++--- app/keepers.go | 2 +- app/test_helpers.go | 1 - app/upgrades/v1/constants.go | 13 ++++ app/upgrades/v1/upgrades.go | 100 +++++++++++++++++++++++++++++- cmd/settlusd/cmd/testnet.go | 11 +++- go.mod | 6 +- go.sum | 8 +-- testutil/network/network.go | 2 +- types/hex_address.go | 2 +- types/nft.go | 4 +- x/oracle/keeper/feeder.go | 2 +- x/oracle/keeper/keeper_test.go | 4 +- x/oracle/types/vote_data.go | 2 +- x/oracle/types/vote_data_test.go | 12 ++-- x/settlement/keeper/msg_server.go | 4 +- 16 files changed, 167 insertions(+), 38 deletions(-) diff --git a/app/app.go b/app/app.go index 3286a58f..67c9f22d 100644 --- a/app/app.go +++ b/app/app.go @@ -62,6 +62,7 @@ import ( memiavlstore "github.com/crypto-org-chain/cronos/store" + upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" "github.com/settlus/chain/app/ante" "github.com/settlus/chain/app/post" "github.com/settlus/chain/app/upgrades/v1" @@ -77,9 +78,10 @@ func init() { } DefaultNodeHome = filepath.Join(userHomeDir, "."+Name) - - // manually update the power reduction by replacing micro (u) -> atto (a) evmos - sdk.DefaultPowerReduction = evmostypes.PowerReduction + // Set power reduction + sdk.DefaultPowerReduction = sdk.NewIntFromUint64(1000000) + // Constant reward is true to follow Settlus reward distribution rules + sdk.ConstantReward = true // modify fee market parameter defaults through global feemarkettypes.DefaultMinGasPrice = evmosapp.MainnetMinGasPrices feemarkettypes.DefaultMinGasMultiplier = evmosapp.MainnetMinGasMultiplier @@ -91,10 +93,8 @@ const ( Name = "settlus" ) -var ( - // DefaultNodeHome default home directories for the application daemon - DefaultNodeHome string -) +// DefaultNodeHome default home directories for the application daemon +var DefaultNodeHome string var ( _ runtime.AppI = (*SettlusApp)(nil) @@ -262,6 +262,7 @@ func NewSettlus( app.setPostHandler() app.SetEndBlocker(app.EndBlocker) app.setupUpgradeHandlers() + app.setUpgradeStoreLoaders() if loadLatest { if err := app.LoadLatestVersion(); err != nil { @@ -485,11 +486,26 @@ func (app *SettlusApp) setupUpgradeHandlers() { app.UpgradeKeeper.SetUpgradeHandler( v1.UpgradeName, v1.CreateUpgradeHandler( - app.mm, app.configurator, + app.mm, app.configurator, app.AccountKeeper, app.ConsensusParamsKeeper, app.IBCKeeper.ClientKeeper, app.ParamsKeeper, app.appCodec, ), ) } +func (app *SettlusApp) setUpgradeStoreLoaders() { + upgradeInfo, err := app.AppKeepers.UpgradeKeeper.ReadUpgradeInfoFromDisk() + if err != nil { + panic(fmt.Errorf("failed to read upgrade info from disk: %w", err)) + } + if app.AppKeepers.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) { + return + } + + if upgradeInfo.Name == v1.UpgradeName { + storeUpgrades := v1.StoreUpgrades + app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &storeUpgrades)) + } +} + // GetKey returns the KVStoreKey for the provided store key. // // NOTE: This is solely to be used for testing purposes. diff --git a/app/keepers.go b/app/keepers.go index 3a071994..c98da5af 100644 --- a/app/keepers.go +++ b/app/keepers.go @@ -366,7 +366,7 @@ func NewAppKeeper( // NOTE: app.Erc20Keeper is already initialized elsewhere // Override the ICS20 app module - //transferModule := transfer.NewAppModule(appKeepers.TransferKeeper) + // transferModule := transfer.NewAppModule(appKeepers.TransferKeeper) /* Create Transfer Stack diff --git a/app/test_helpers.go b/app/test_helpers.go index 37595050..76046fb8 100644 --- a/app/test_helpers.go +++ b/app/test_helpers.go @@ -159,7 +159,6 @@ func GenesisStateWithValSet(app *SettlusApp, genesisState simapp.GenesisState, Commission: stakingtypes.NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), MinSelfDelegation: sdk.ZeroInt(), Probono: false, - ProbonoRate: sdk.ZeroDec(), } validators = append(validators, validator) delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdk.OneDec())) diff --git a/app/upgrades/v1/constants.go b/app/upgrades/v1/constants.go index 17bf8bf3..36c1c004 100644 --- a/app/upgrades/v1/constants.go +++ b/app/upgrades/v1/constants.go @@ -1,6 +1,19 @@ package v1 +import ( + store "github.com/cosmos/cosmos-sdk/store/types" + consensustypes "github.com/cosmos/cosmos-sdk/x/consensus/types" + crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types" +) + const ( // UpgradeName is the shared upgrade plan name for mainnet UpgradeName = "v2.0.0" ) + +var StoreUpgrades = store.StoreUpgrades{ + Added: []string{ + consensustypes.ModuleName, + crisistypes.ModuleName, + }, +} diff --git a/app/upgrades/v1/upgrades.go b/app/upgrades/v1/upgrades.go index bb7468a8..d3ae4a71 100644 --- a/app/upgrades/v1/upgrades.go +++ b/app/upgrades/v1/upgrades.go @@ -1,18 +1,114 @@ package v1 import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" + authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + consensuskeeper "github.com/cosmos/cosmos-sdk/x/consensus/keeper" + crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types" + distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types" + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" + minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" + paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper" + paramstypes "github.com/cosmos/cosmos-sdk/x/params/types" + slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" + ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types" + ibctmmigrations "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint/migrations" + evmtypes "github.com/evmos/evmos/v19/x/evm/types" + feemarkettypes "github.com/evmos/evmos/v19/x/feemarket/types" ) -// CreateUpgradeHandler creates an SDK upgrade handler for v2 func CreateUpgradeHandler( mm *module.Manager, configurator module.Configurator, + ak authkeeper.AccountKeeper, + ck consensuskeeper.Keeper, + clientKeeper ibctmmigrations.ClientKeeper, + pk paramskeeper.Keeper, + cdc codec.BinaryCodec, ) upgradetypes.UpgradeHandler { return func(ctx sdk.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) { - // Leave modules as-is to avoid running InitGenesis. + logger := ctx.Logger().With("upgrade", UpgradeName) + + // Set param key table for params module migration + for _, subspace := range pk.GetSubspaces() { + var keyTable paramstypes.KeyTable + switch subspace.Name() { + case authtypes.ModuleName: + keyTable = authtypes.ParamKeyTable() //nolint:staticcheck + case banktypes.ModuleName: + keyTable = banktypes.ParamKeyTable() //nolint:staticcheck,nolintlint + case stakingtypes.ModuleName: + keyTable = stakingtypes.ParamKeyTable() + case minttypes.ModuleName: + keyTable = minttypes.ParamKeyTable() //nolint:staticcheck + case distrtypes.ModuleName: + keyTable = distrtypes.ParamKeyTable() //nolint:staticcheck,nolintlint + case slashingtypes.ModuleName: + keyTable = slashingtypes.ParamKeyTable() //nolint:staticcheck + case govtypes.ModuleName: + keyTable = govv1.ParamKeyTable() //nolint:staticcheck + case crisistypes.ModuleName: + keyTable = crisistypes.ParamKeyTable() //nolint:staticcheck + case ibctransfertypes.ModuleName: + keyTable = ibctransfertypes.ParamKeyTable() + case evmtypes.ModuleName: + keyTable = evmtypes.ParamKeyTable() //nolint:staticcheck + case feemarkettypes.ModuleName: + keyTable = feemarkettypes.ParamKeyTable() + default: + continue + } + if !subspace.HasKeyTable() { + subspace.WithKeyTable(keyTable) + } + } + + baseAppLegacySS := pk.Subspace(baseapp.Paramspace).WithKeyTable(paramstypes.ConsensusParamsKeyTable()) + + baseapp.MigrateParams(ctx, baseAppLegacySS, &ck) + + // Include this when migrating to ibc-go v7 (optional) + // source: https://github.com/cosmos/ibc-go/blob/v7.2.0/docs/migrations/v6-to-v7.md + // prune expired tendermint consensus states to save storage space + if _, err := ibctmmigrations.PruneExpiredConsensusStates(ctx, cdc, clientKeeper); err != nil { + return nil, err + } + + if err := MigrateFeeCollector(ak, ctx); err != nil { + return nil, err + } + + logger.Debug("running module migrations ...") return mm.RunMigrations(ctx, configurator, vm) } } + +func MigrateFeeCollector(ak authkeeper.AccountKeeper, ctx sdk.Context) error { + feeCollectorModuleAccount := ak.GetModuleAccount(ctx, authtypes.FeeCollectorName) + if feeCollectorModuleAccount == nil { + return fmt.Errorf("fee collector module account not found") + } + + modAcc, ok := feeCollectorModuleAccount.(*authtypes.ModuleAccount) + if !ok { + return fmt.Errorf("fee collector module account is not a module account") + } + + // Create a new FeeCollector module account with the same address and balance as the old one. + newFeeCollectorModuleAccount := authtypes.NewModuleAccount(modAcc.BaseAccount, authtypes.FeeCollectorName, authtypes.Burner) + + // Override the FeeCollector module account in the auth keeper. + ak.SetModuleAccount(ctx, newFeeCollectorModuleAccount) + + return nil +} diff --git a/cmd/settlusd/cmd/testnet.go b/cmd/settlusd/cmd/testnet.go index 236d2c27..4cbfc723 100644 --- a/cmd/settlusd/cmd/testnet.go +++ b/cmd/settlusd/cmd/testnet.go @@ -315,7 +315,7 @@ func initTestnetFiles( stakingtypes.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), math.OneInt(), math.ZeroInt(), - sdk.ZeroDec(), + false, ) if err != nil { return err @@ -428,6 +428,13 @@ func initGenFiles( appGenState[stakingtypes.ModuleName] = clientCtx.Codec.MustMarshalJSON(&stakingGenState) var govGenState govv1.GenesisState + newPeriod := time.Minute * 2 + clientCtx.Codec.MustUnmarshalJSON(appGenState[govtypes.ModuleName], &govGenState) + + govGenState.Params.MaxDepositPeriod = &newPeriod + govGenState.Params.VotingPeriod = &newPeriod + govGenState.Params.MinDeposit[0].Amount = sdk.NewInt(1000) + govGenState.Params.MinDeposit[0].Denom = coinDenom clientCtx.Codec.MustUnmarshalJSON(appGenState[govtypes.ModuleName], &govGenState) appGenState[govtypes.ModuleName] = clientCtx.Codec.MustMarshalJSON(&govGenState) @@ -436,7 +443,7 @@ func initGenFiles( clientCtx.Codec.MustUnmarshalJSON(appGenState[minttypes.ModuleName], &mintGenState) mintGenState.Params.MintDenom = coinDenom - mintGenState.Params.BlockReward = sdk.NewInt(5000000000000000000) + mintGenState.Params.BlockReward = sdk.NewInt(3000000000000000000) appGenState[minttypes.ModuleName] = clientCtx.Codec.MustMarshalJSON(&mintGenState) var crisisGenState crisistypes.GenesisState diff --git a/go.mod b/go.mod index 9b63557d..9215bf73 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,6 @@ module github.com/settlus/chain go 1.22.2 -toolchain go1.22.5 - require ( cosmossdk.io/api v0.3.1 cosmossdk.io/errors v1.0.1 @@ -253,11 +251,11 @@ replace ( // use cosmos fork of keyring github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0 // use Settlus flavored Cosmos-SDK https://github.com/settlus/cosmos-sdk/releases - github.com/cosmos/cosmos-sdk => github.com/settlus/cosmos-sdk v0.47.12-settlus-alpha + github.com/cosmos/cosmos-sdk => github.com/settlus/cosmos-sdk v0.47.12-settlus.2 // use Evmos geth fork github.com/ethereum/go-ethereum => github.com/evmos/go-ethereum v1.10.26-evmos-rc4 // use Settlus flavored Evmos - github.com/evmos/evmos/v19 => github.com/settlus/evmos/v19 v19.0.0-settlus.1 + github.com/evmos/evmos/v19 => github.com/settlus/evmos/v19 v19.0.0-settlus.3.rc // Security Advisory https://github.com/advisories/GHSA-h395-qcrw-5vmq github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.9.1 // use cosmos flavored protobufs diff --git a/go.sum b/go.sum index 1c2cf1b8..78f477f5 100644 --- a/go.sum +++ b/go.sum @@ -1213,10 +1213,10 @@ github.com/segmentio/fasthash v1.0.3/go.mod h1:waKX8l2N8yckOgmSsXJi7x1ZfdKZ4x7KR github.com/segmentio/kafka-go v0.1.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo= github.com/segmentio/kafka-go v0.2.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= -github.com/settlus/cosmos-sdk v0.47.12-settlus-alpha h1:8XEuwxKhRIA1mZvL3y71wz0jSnqazZTWCNZv06Ja+f4= -github.com/settlus/cosmos-sdk v0.47.12-settlus-alpha/go.mod h1:ADjORYzUQqQv/FxDi0H0K5gW/rAk1CiDR3ZKsExfJV0= -github.com/settlus/evmos/v19 v19.0.0-settlus.1 h1:/Mflt1By3l92QiJiSXg4exUzmAjji1YVRZSQXtQ4i4I= -github.com/settlus/evmos/v19 v19.0.0-settlus.1/go.mod h1:BjlwXOxPrB+9o3zdxseaPV7gylV1XHoJrJYQIYshQ6E= +github.com/settlus/cosmos-sdk v0.47.12-settlus.2 h1:ho5ED+o0UI3ITiEckNdlqxgIxE++tYU2CgwfBanbttA= +github.com/settlus/cosmos-sdk v0.47.12-settlus.2/go.mod h1:ADjORYzUQqQv/FxDi0H0K5gW/rAk1CiDR3ZKsExfJV0= +github.com/settlus/evmos/v19 v19.0.0-settlus.3.rc h1:z9ctKKoVsTw18fhFLW+OTUqwA76gMK63f/1ECU8T0dY= +github.com/settlus/evmos/v19 v19.0.0-settlus.3.rc/go.mod h1:cH2IV7ZrZlp+2KfeExMvtlQe8sxOYt2cC4J3ZiiwJHU= github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible h1:Bn1aCHHRnjv4Bl16T8rcaFjYSrGrIZvpiGO6P3Q4GpU= github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= diff --git a/testutil/network/network.go b/testutil/network/network.go index 355a1baf..f8e9a00e 100644 --- a/testutil/network/network.go +++ b/testutil/network/network.go @@ -430,7 +430,7 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) { stakingtypes.NewCommissionRates(commission, sdk.ZeroDec(), sdk.ZeroDec()), math.OneInt(), math.ZeroInt(), - sdk.ZeroDec(), + false, ) if err != nil { diff --git a/types/hex_address.go b/types/hex_address.go index 001293ce..85f65f34 100644 --- a/types/hex_address.go +++ b/types/hex_address.go @@ -14,7 +14,7 @@ func NewHexAddrFromBytes(addr []byte) HexAddressString { return HexAddressString(common.BytesToAddress(addr).Hex()) } -func NoramlizeHexAddress(addr string) HexAddressString { +func NormalizeHexAddress(addr string) HexAddressString { return HexAddressString(common.HexToAddress(addr).Hex()) } diff --git a/types/nft.go b/types/nft.go index e74c03ba..360a0df9 100644 --- a/types/nft.go +++ b/types/nft.go @@ -14,8 +14,8 @@ func ParseNftId(nftId string) (Nft, error) { return Nft{ ChainId: data[0], - ContractAddr: NoramlizeHexAddress(data[1]), - TokenId: NoramlizeHexAddress(data[2]), + ContractAddr: NormalizeHexAddress(data[1]), + TokenId: NormalizeHexAddress(data[2]), }, nil } diff --git a/x/oracle/keeper/feeder.go b/x/oracle/keeper/feeder.go index 718c8874..a2702d01 100644 --- a/x/oracle/keeper/feeder.go +++ b/x/oracle/keeper/feeder.go @@ -313,7 +313,7 @@ func (k Keeper) RewardBallotWinners(ctx sdk.Context, validatorClaimMap map[strin } if !rewardCoins.IsZero() { - if receiverVal.Probono { + if receiverVal.IsProbono() { probonoReward = probonoReward.Add(rewardCoins...) continue } diff --git a/x/oracle/keeper/keeper_test.go b/x/oracle/keeper/keeper_test.go index a43c4363..0fa18b09 100644 --- a/x/oracle/keeper/keeper_test.go +++ b/x/oracle/keeper/keeper_test.go @@ -157,7 +157,7 @@ func (suite *OracleTestSuite) NewValidator(amt sdkmath.Int) *stakingtypes.MsgCre stakingtypes.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), sdkmath.OneInt(), sdkmath.ZeroInt(), - sdk.ZeroDec(), + false, ) suite.Require().NoError(err) return msgCreate @@ -499,7 +499,7 @@ func (suite *OracleTestSuite) TestKeeper_RewardBallotWinners_WithProbono() { s.NoError(err) for _, validator := range s.validators { - if validator.Probono { + if validator.IsProbono() { probonoRewards = probonoRewards.Add(tt.rewardMap[validator.GetOperator().String()].AmountOf("asetl")) validator.Probono = false continue diff --git a/x/oracle/types/vote_data.go b/x/oracle/types/vote_data.go index b9cd83ea..1d06083c 100644 --- a/x/oracle/types/vote_data.go +++ b/x/oracle/types/vote_data.go @@ -47,7 +47,7 @@ func StringToOwnershipData(voteString string) (ctypes.Nft, ctypes.HexAddressStri return ctypes.Nft{}, "", fmt.Errorf("invalid nftId: %s", data[0]) } - owner := ctypes.NoramlizeHexAddress(data[1]) + owner := ctypes.NormalizeHexAddress(data[1]) return nft, owner, err } diff --git a/x/oracle/types/vote_data_test.go b/x/oracle/types/vote_data_test.go index 683e3dc4..a1543da4 100644 --- a/x/oracle/types/vote_data_test.go +++ b/x/oracle/types/vote_data_test.go @@ -30,10 +30,10 @@ func TestStringToOwnershipData(t *testing.T) { expected: Result{ nft: ctypes.Nft{ ChainId: "1", - ContractAddr: ctypes.NoramlizeHexAddress("0x123"), - TokenId: ctypes.NoramlizeHexAddress("0x0"), + ContractAddr: ctypes.NormalizeHexAddress("0x123"), + TokenId: ctypes.NormalizeHexAddress("0x0"), }, - owner: ctypes.NoramlizeHexAddress("0x777"), + owner: ctypes.NormalizeHexAddress("0x777"), err: false, }, }, @@ -43,10 +43,10 @@ func TestStringToOwnershipData(t *testing.T) { expected: Result{ nft: ctypes.Nft{ ChainId: "1", - ContractAddr: ctypes.NoramlizeHexAddress("0x123"), - TokenId: ctypes.NoramlizeHexAddress("0xa"), + ContractAddr: ctypes.NormalizeHexAddress("0x123"), + TokenId: ctypes.NormalizeHexAddress("0xa"), }, - owner: ctypes.NoramlizeHexAddress("777"), + owner: ctypes.NormalizeHexAddress("777"), err: false, }, }, diff --git a/x/settlement/keeper/msg_server.go b/x/settlement/keeper/msg_server.go index 3dda2d0f..386f6423 100644 --- a/x/settlement/keeper/msg_server.go +++ b/x/settlement/keeper/msg_server.go @@ -53,8 +53,8 @@ func (k msgServer) Record(goCtx context.Context, msg *types.MsgRecord) (*types.M } payoutBlock := uint64(ctx.BlockHeight()) - contractAddr := ctypes.NoramlizeHexAddress(msg.ContractAddress) - tokenId := ctypes.NoramlizeHexAddress(msg.TokenIdHex) + contractAddr := ctypes.NormalizeHexAddress(msg.ContractAddress) + tokenId := ctypes.NormalizeHexAddress(msg.TokenIdHex) utxrId, err := k.CreateUTXR( ctx, msg.TenantId,