Skip to content

Commit

Permalink
feat: upgrade to Cosmos SDK v0.50 (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Aug 2, 2023
1 parent 55dd856 commit ae00b06
Show file tree
Hide file tree
Showing 10 changed files with 340 additions and 250 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minid
debug_container.dot
debug_container.log
67 changes: 17 additions & 50 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"os"
"path/filepath"

dbm "github.com/cosmos/cosmos-db"

"cosmossdk.io/depinject"
dbm "github.com/cometbft/cometbft-db"
"github.com/cometbft/cometbft/libs/log"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -18,42 +20,16 @@ import (
"github.com/cosmos/cosmos-sdk/server/api"
"github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/store/streaming"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/cosmos/cosmos-sdk/x/bank"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/consensus"
consensuskeeper "github.com/cosmos/cosmos-sdk/x/consensus/keeper"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
)

var (
// DefaultNodeHome default home directories for the application daemon
DefaultNodeHome string

// ModuleBasics defines the module BasicManager is in charge of setting up basic,
// non-dependant module elements, such as codec registration
// and genesis verification.
ModuleBasics = module.NewBasicManager(
auth.AppModuleBasic{},
genutil.NewAppModuleBasic(genutiltypes.DefaultMessageValidator),
bank.AppModuleBasic{},
staking.AppModuleBasic{},
mint.AppModuleBasic{},
distr.AppModuleBasic{},
consensus.AppModuleBasic{},
)
)
// DefaultNodeHome default home directories for the application daemon
var DefaultNodeHome string

var (
_ runtime.AppI = (*MiniApp)(nil)
Expand Down Expand Up @@ -98,14 +74,14 @@ func NewMiniApp(
loadLatest bool,
appOpts servertypes.AppOptions,
baseAppOptions ...func(*baseapp.BaseApp),
) *MiniApp {
) (*MiniApp, error) {
var (
app = &MiniApp{}
appBuilder *runtime.AppBuilder
appConfig = depinject.Configs(AppConfig, depinject.Supply(appOpts))
)

if err := depinject.Inject(appConfig,
if err := depinject.Inject(
depinject.Configs(AppConfig, depinject.Supply(logger, appOpts)),
&appBuilder,
&app.appCodec,
&app.legacyAmino,
Expand All @@ -117,39 +93,30 @@ func NewMiniApp(
&app.DistrKeeper,
&app.ConsensusParamsKeeper,
); err != nil {
panic(err)
return nil, err
}

app.App = appBuilder.Build(logger, db, traceStore, baseAppOptions...)
app.App = appBuilder.Build(db, traceStore, baseAppOptions...)

// load state streaming if enabled
if _, _, err := streaming.LoadStreamingServices(app.App.BaseApp, appOpts, app.appCodec, logger, app.kvStoreKeys()); err != nil {
logger.Error("failed to load state streaming", "err", err)
os.Exit(1)
// register streaming services
if err := app.RegisterStreamingServices(appOpts, app.kvStoreKeys()); err != nil {
return nil, err
}

/**** Module Options ****/

// add test gRPC service for testing gRPC queries in isolation
testdata.RegisterQueryServer(app.GRPCQueryRouter(), testdata.QueryImpl{})

// create the simulation manager and define the order of the modules for deterministic simulations
//
// NOTE: this is not required apps that don't use the simulator for fuzz testing
// transactions
// NOTE: this is not required apps that don't use the simulator for fuzz testing transactions
app.sm = module.NewSimulationManagerFromAppModules(app.ModuleManager.Modules, make(map[string]module.AppModuleSimulation, 0))
app.sm.RegisterStoreDecoders()

if err := app.Load(loadLatest); err != nil {
panic(err)
return nil, err
}

return app
return app, nil
}

// Name returns the name of the App
func (app *MiniApp) Name() string { return app.BaseApp.Name() }

// LegacyAmino returns MiniApp's amino codec.
func (app *MiniApp) LegacyAmino() *codec.LegacyAmino {
return app.legacyAmino
Expand Down
31 changes: 19 additions & 12 deletions app/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,24 @@ import (
stakingmodulev1 "cosmossdk.io/api/cosmos/staking/module/v1"
txconfigv1 "cosmossdk.io/api/cosmos/tx/config/v1"
"cosmossdk.io/core/appconfig"
"cosmossdk.io/depinject"

"github.com/cosmos/cosmos-sdk/types/module"
_ "github.com/cosmos/cosmos-sdk/x/auth/tx/config" // import for side-effects
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
consensustypes "github.com/cosmos/cosmos-sdk/x/consensus/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

_ "github.com/cosmos/cosmos-sdk/x/auth" // import for side-effects
_ "github.com/cosmos/cosmos-sdk/x/bank" // import for side-effects
_ "github.com/cosmos/cosmos-sdk/x/consensus" // import for side-effects
_ "github.com/cosmos/cosmos-sdk/x/distribution" // import for side-effects
_ "github.com/cosmos/cosmos-sdk/x/mint" // import for side-effects
_ "github.com/cosmos/cosmos-sdk/x/staking" // import for side-effects
)

var (
Expand All @@ -41,7 +51,7 @@ var (
}

// application configuration (used by depinject)
AppConfig = appconfig.Compose(&appv1alpha1.Config{
AppConfig = depinject.Configs(appconfig.Compose(&appv1alpha1.Config{
Modules: []*appv1alpha1.ModuleConfig{
{
Name: "runtime",
Expand All @@ -54,18 +64,9 @@ var (
BeginBlockers: []string{
distrtypes.ModuleName,
stakingtypes.ModuleName,
authtypes.ModuleName,
banktypes.ModuleName,
genutiltypes.ModuleName,
consensustypes.ModuleName,
},
EndBlockers: []string{
stakingtypes.ModuleName,
authtypes.ModuleName,
banktypes.ModuleName,
distrtypes.ModuleName,
genutiltypes.ModuleName,
consensustypes.ModuleName,
},
OverrideStoreKeys: []*runtimev1alpha1.StoreKeyConfig{
{
Expand All @@ -82,7 +83,7 @@ var (
distrtypes.ModuleName,
stakingtypes.ModuleName,
genutiltypes.ModuleName,
consensustypes.ModuleName,
consensustypes.ModuleName, // TODO(@julienrbrt) To be removed after https://github.com/cosmos/cosmos-sdk/pull/17262
},
}),
},
Expand Down Expand Up @@ -120,5 +121,11 @@ var (
Config: appconfig.WrapAny(&consensusmodulev1.Module{}),
},
},
})
}),
depinject.Supply(
// supply custom module basics
map[string]module.AppModuleBasic{
genutiltypes.ModuleName: genutil.NewAppModuleBasic(genutiltypes.DefaultMessageValidator),
},
))
)
42 changes: 32 additions & 10 deletions app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package app

import (
"encoding/json"
"errors"
"fmt"
"log"

storetypes "cosmossdk.io/store/types"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -19,7 +21,7 @@ func (app *MiniApp) ExportAppStateAndValidators(
modulesToExport []string,
) (servertypes.ExportedApp, error) {
// as if they could withdraw from the start of the next block
ctx := app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()})
ctx := app.NewContextLegacy(true, tmproto.Header{Height: app.LastBlockHeight()})

// We export at last height + 1, because that's the height at which
// CometBFT will start InitChain.
Expand All @@ -29,7 +31,11 @@ func (app *MiniApp) ExportAppStateAndValidators(
app.prepForZeroHeightGenesis(ctx, jailAllowedAddrs)
}

genState := app.ModuleManager.ExportGenesis(ctx, app.appCodec)
genState, err := app.ModuleManager.ExportGenesis(ctx, app.appCodec)
if err != nil {
return servertypes.ExportedApp{}, fmt.Errorf("failed to export genesis state: %w", err)
}

appState, err := json.MarshalIndent(genState, "", " ")
if err != nil {
return servertypes.ExportedApp{}, err
Expand Down Expand Up @@ -73,7 +79,11 @@ func (app *MiniApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [
})

// withdraw all delegator rewards
dels := app.StakingKeeper.GetAllDelegations(ctx)
dels, err := app.StakingKeeper.GetAllDelegations(ctx)
if err != nil {
panic(err)
}

for _, delegation := range dels {
valAddr, err := sdk.ValAddressFromBech32(delegation.ValidatorAddress)
if err != nil {
Expand Down Expand Up @@ -101,10 +111,20 @@ func (app *MiniApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [
// reinitialize all validators
app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
// donate any unwithdrawn outstanding reward fraction tokens to the community pool
scraps := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, val.GetOperator())
feePool := app.DistrKeeper.GetFeePool(ctx)
scraps, err := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, val.GetOperator())
if err != nil {
panic(err)
}

feePool, err := app.DistrKeeper.FeePool.Get(ctx)
if err != nil {
panic(err)
}

feePool.CommunityPool = feePool.CommunityPool.Add(scraps...)
app.DistrKeeper.SetFeePool(ctx, feePool)
if err := app.DistrKeeper.FeePool.Set(ctx, feePool); err != nil {
panic(err)
}

if err := app.DistrKeeper.Hooks().AfterValidatorCreated(ctx, val.GetOperator()); err != nil {
panic(err)
Expand Down Expand Up @@ -161,14 +181,16 @@ func (app *MiniApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [
// Iterate through validators by power descending, reset bond heights, and
// update bond intra-tx counters.
store := ctx.KVStore(app.GetKey(stakingtypes.StoreKey))
iter := sdk.KVStoreReversePrefixIterator(store, stakingtypes.ValidatorsKey)
iter := storetypes.KVStoreReversePrefixIterator(store, stakingtypes.ValidatorsKey)
counter := int16(0)

for ; iter.Valid(); iter.Next() {
addr := sdk.ValAddress(stakingtypes.AddressFromValidatorsKey(iter.Key()))
validator, found := app.StakingKeeper.GetValidator(ctx, addr)
if !found {
validator, err := app.StakingKeeper.GetValidator(ctx, addr)
if errors.Is(err, stakingtypes.ErrNoValidatorFound) {
panic("expected validator, not found")
} else if err != nil {
panic(err)
}

validator.UnbondingHeight = 0
Expand All @@ -185,7 +207,7 @@ func (app *MiniApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [
return
}

_, err := app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
_, err = app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
if err != nil {
log.Fatal(err)
}
Expand Down
11 changes: 7 additions & 4 deletions app/params/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package params

import (
"cosmossdk.io/errors"
"cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -34,7 +37,7 @@ func init() {
}

func RegisterDenoms() {
err := sdk.RegisterDenom(CoinUnit, sdk.OneDec())
err := sdk.RegisterDenom(CoinUnit, math.LegacyOneDec())
if err != nil {
panic(err)
}
Expand All @@ -50,15 +53,15 @@ func SetAddressPrefixes() {
// source: https://github.com/cosmos/cosmos-sdk/blob/v0.43.0-beta1/types/address.go#L141
config.SetAddressVerifier(func(bytes []byte) error {
if len(bytes) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrUnknownAddress, "addresses cannot be empty")
return errors.Wrap(sdkerrors.ErrUnknownAddress, "addresses cannot be empty")
}

if len(bytes) > address.MaxAddrLen {
return sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "address max length is %d, got %d", address.MaxAddrLen, len(bytes))
return errors.Wrapf(sdkerrors.ErrUnknownAddress, "address max length is %d, got %d", address.MaxAddrLen, len(bytes))
}

if len(bytes) != 20 && len(bytes) != 32 {
return sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "address length must be 20 or 32 bytes, got %d", len(bytes))
return errors.Wrapf(sdkerrors.ErrUnknownAddress, "address length must be 20 or 32 bytes, got %d", len(bytes))
}

return nil
Expand Down
Loading

0 comments on commit ae00b06

Please sign in to comment.