Skip to content

Commit

Permalink
Merge pull request #863 from comdex-official/feature/dev
Browse files Browse the repository at this point in the history
Forced faster block times config, export refactor, v13 upgrade plan changed
  • Loading branch information
cgsingh33 authored Nov 9, 2023
2 parents 81343d5 + 9f5ea06 commit 9c9c264
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 24 deletions.
49 changes: 32 additions & 17 deletions app/export.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

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

Expand Down Expand Up @@ -34,15 +35,12 @@ func (a *App) ExportAppStateAndValidators(
}

validators, err := staking.WriteValidators(ctx, a.StakingKeeper)
if err != nil {
return servertypes.ExportedApp{}, err
}
return servertypes.ExportedApp{
AppState: appState,
Validators: validators,
Height: height,
ConsensusParams: a.BaseApp.GetConsensusParams(ctx),
}, nil
}, err
}

// prepare for fresh start at zero height
Expand Down Expand Up @@ -74,20 +72,21 @@ func (a *App) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []strin

// withdraw all validator commission
a.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
_, err := a.DistrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator())
if err != nil {
panic(err)
}
_, _ = a.DistrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator())
return false
})

// withdraw all delegator rewards
dels := a.StakingKeeper.GetAllDelegations(ctx)
for _, delegation := range dels {
_, err := a.DistrKeeper.WithdrawDelegationRewards(ctx, delegation.GetDelegatorAddr(), delegation.GetValidatorAddr())
valAddr, err := sdk.ValAddressFromBech32(delegation.ValidatorAddress)
if err != nil {
panic(err)
}

delAddr := sdk.MustAccAddressFromBech32(delegation.DelegatorAddress)

_, _ = a.DistrKeeper.WithdrawDelegationRewards(ctx, delAddr, valAddr)
}

// clear validator slash events
Expand All @@ -108,14 +107,29 @@ func (a *App) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []strin
feePool.CommunityPool = feePool.CommunityPool.Add(scraps...)
a.DistrKeeper.SetFeePool(ctx, feePool)

a.DistrKeeper.Hooks().AfterValidatorCreated(ctx, val.GetOperator())
if err := a.DistrKeeper.Hooks().AfterValidatorCreated(ctx, val.GetOperator()); err !=nil {
panic(err)
}
return false
})

// reinitialize all delegations
for _, del := range dels {
a.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, del.GetDelegatorAddr(), del.GetValidatorAddr())
a.DistrKeeper.Hooks().AfterDelegationModified(ctx, del.GetDelegatorAddr(), del.GetValidatorAddr())
valAddr, err := sdk.ValAddressFromBech32(del.ValidatorAddress)
if err != nil {
panic(err)
}
delAddr := sdk.MustAccAddressFromBech32(del.DelegatorAddress)

if err := a.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, delAddr, valAddr); err != nil {
// never called as BeforeDelegationCreated always returns nil
panic(fmt.Errorf("error while incrementing period: %w", err))
}

if err := a.DistrKeeper.Hooks().AfterDelegationModified(ctx, delAddr, valAddr); err != nil {
// never called as AfterDelegationModified always returns nil
panic(fmt.Errorf("error while creating a new delegation period record: %w", err))
}
}

// reset context height
Expand Down Expand Up @@ -148,7 +162,7 @@ func (a *App) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []strin
counter := int16(0)

for ; iter.Valid(); iter.Next() {
addr := sdk.ValAddress(iter.Key()[1:])
addr := sdk.ValAddress(stakingtypes.AddressFromValidatorsKey(iter.Key()))
validator, found := a.StakingKeeper.GetValidator(ctx, addr)
if !found {
panic("expected validator, not found")
Expand All @@ -163,13 +177,14 @@ func (a *App) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []strin
counter++
}

err := iter.Close()
if err != nil {
if err := iter.Close(); err != nil {
a.Logger().Error("error while closing the key-value store reverse prefix iterator: ", err)
return
}

if _, err := a.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx); err != nil {
panic(err)
_, err := a.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
if err != nil {
log.Fatal(err)
}

/* Handle slashing state. */
Expand Down
2 changes: 1 addition & 1 deletion app/upgrades/testnet/v13/constants.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package v13

const (
UpgradeName = "v13.0.0"
UpgradeName = "v13.1.0"
UpgradeHeight = ""
UpgradeInfo = `'{
"binaries": {
Expand Down
2 changes: 1 addition & 1 deletion app/upgrades/testnet/v13/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func CreateUpgradeHandlerV13(
bandoracleKeeper bandoraclemodulekeeper.Keeper,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
ctx.Logger().Info("Applying test net upgrade - v.13.0.0")
ctx.Logger().Info("Applying test net upgrade - v.13.1.0")
logger := ctx.Logger().With("upgrade", UpgradeName)

// Migrate Tendermint consensus parameters from x/params module to a deprecated x/consensus module.
Expand Down
44 changes: 39 additions & 5 deletions cmd/comdex/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"io"
"os"
"path/filepath"
"time"

"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/prometheus/client_golang/prometheus"

tmdb "github.com/cometbft/cometbft-db"
Expand All @@ -24,6 +26,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server"
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/snapshots"
snapshottypes "github.com/cosmos/cosmos-sdk/snapshots/types"
Expand Down Expand Up @@ -58,7 +61,7 @@ func NewRootCmd() (*cobra.Command, comdex.EncodingConfig) {
cobra.EnableCommandSorting = false
root := &cobra.Command{
Use: "comdex",
Short: "Comdex - Decentralised Synthetic Asset Exchange",
Short: "Comdex - DeFi Infrastructure layer for the Cosmos ecosystem",
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
initClientCtx, err := client.ReadPersistentCommandFlags(initClientCtx, cmd.Flags())
if err != nil {
Expand All @@ -72,28 +75,59 @@ func NewRootCmd() (*cobra.Command, comdex.EncodingConfig) {
if err := client.SetCmdClientContextHandler(initClientCtx, cmd); err != nil {
return err
}
// 2 seconds + 1 second tendermint = 3 second blocks
timeoutCommit := 2 * time.Second

// bump47: recheck if customTMConfig is required, else replace with nil
customTMConfig := initTendermintConfig()
customAppTemplate, customAppConfig := initAppConfig()
customTMConfig := initTendermintConfig(timeoutCommit)

return server.InterceptConfigsPreRunHandler(cmd, "", nil, customTMConfig)
// Force faster block times
os.Setenv("COMDEX_CONSENSUS_TIMEOUT_COMMIT", cast.ToString(timeoutCommit))

return server.InterceptConfigsPreRunHandler(cmd, customAppTemplate, customAppConfig, customTMConfig)
},
}

initRootCmd(root, encodingConfig)
return root, encodingConfig
}

func initTendermintConfig() *tmcfg.Config {
func initTendermintConfig(timeoutCommit time.Duration) *tmcfg.Config {
cfg := tmcfg.DefaultConfig()

// these values put a higher strain on node memory
// cfg.P2P.MaxNumInboundPeers = 100
// cfg.P2P.MaxNumOutboundPeers = 40

// While this is set, it only applies to new configs.
cfg.Consensus.TimeoutCommit = timeoutCommit

return cfg
}

// initAppConfig helps to override default appConfig template and configs.
// return "", nil if no custom configuration is required for the application.
func initAppConfig() (string, interface{}) {
type CustomAppConfig struct {
serverconfig.Config

Wasm wasmtypes.WasmConfig `mapstructure:"wasm"`
}

// Optionally allow the chain developer to overwrite the SDK's default
// server config.
srvCfg := serverconfig.DefaultConfig()

customAppConfig := CustomAppConfig{
Config: *srvCfg,
Wasm: wasmtypes.DefaultWasmConfig(),
}

customAppTemplate := serverconfig.DefaultConfigTemplate + wasmtypes.DefaultConfigTemplate()

return customAppTemplate, customAppConfig
}

func initRootCmd(rootCmd *cobra.Command, encoding comdex.EncodingConfig) {
cfg := sdk.GetConfig()
cfg.Seal()
Expand Down

0 comments on commit 9c9c264

Please sign in to comment.