Skip to content

Commit

Permalink
feat(endorsement,incentives): simulation tests (#1536)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Tsitrin <[email protected]>
  • Loading branch information
keruch and mtsitrin authored Dec 2, 2024
1 parent 0988440 commit ed7dde7
Show file tree
Hide file tree
Showing 22 changed files with 1,215 additions and 14 deletions.
14 changes: 13 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
runtimeservices "github.com/cosmos/cosmos-sdk/runtime/services"
"github.com/cosmos/cosmos-sdk/x/auth"
authsims "github.com/cosmos/cosmos-sdk/x/auth/simulation"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

simappparams "cosmossdk.io/simapp/params"
"github.com/cosmos/cosmos-sdk/runtime"
Expand Down Expand Up @@ -113,6 +116,8 @@ type App struct {
mm *module.Manager
// module configurator
configurator module.Configurator
// simulation manager
sm *module.SimulationManager
}

// New returns a reference to an initialized blockchain app
Expand Down Expand Up @@ -195,6 +200,13 @@ func New(
app.configurator = module.NewConfigurator(app.appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter())
app.mm.RegisterServices(app.configurator)

/**** Simulations ****/
overrideModules := map[string]module.AppModuleSimulation{
authtypes.ModuleName: auth.NewAppModule(app.appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts, app.GetSubspace(authtypes.ModuleName)),
}
app.sm = module.NewSimulationManagerFromAppModules(app.mm.Modules, overrideModules)
app.sm.RegisterStoreDecoders()

// initialize stores
app.MountKVStores(keepers.KVStoreKeys)
app.MountTransientStores(app.GetTransientStoreKey())
Expand Down Expand Up @@ -358,7 +370,7 @@ func RegisterSwaggerAPI(_ client.Context, rtr *mux.Router) {

// SimulationManager implements the SimulationApp interface
func (app *App) SimulationManager() *module.SimulationManager {
return nil
return app.sm
}

// GetTxConfig implements ibctesting.TestingApp
Expand Down
2 changes: 1 addition & 1 deletion app/keepers/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (a *AppKeepers) SetupModules(
iro.NewAppModule(appCodec, *a.IROKeeper),

sequencermodule.NewAppModule(appCodec, a.SequencerKeeper),
sponsorship.NewAppModule(a.SponsorshipKeeper),
sponsorship.NewAppModule(a.SponsorshipKeeper, a.AccountKeeper, a.BankKeeper, a.IncentivesKeeper, a.StakingKeeper),
streamermodule.NewAppModule(a.StreamerKeeper, a.AccountKeeper, a.BankKeeper, a.EpochsKeeper),
delayedackmodule.NewAppModule(appCodec, a.DelayedAckKeeper, a.delayedAckMiddleware),
denommetadatamodule.NewAppModule(a.DenomMetadataKeeper, *a.EvmKeeper, a.BankKeeper),
Expand Down
3 changes: 2 additions & 1 deletion ibctesting/genesis_bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"
"time"

"github.com/stretchr/testify/suite"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
Expand All @@ -13,7 +15,6 @@ import (
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
ibctesting "github.com/cosmos/ibc-go/v7/testing"
"github.com/stretchr/testify/suite"

"github.com/dymensionxyz/dymension/v3/app/apptesting"
appparams "github.com/dymensionxyz/dymension/v3/app/params"
Expand Down
52 changes: 52 additions & 0 deletions internal/collcompat/simulation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package collcompat

import (
"bytes"
"fmt"

"cosmossdk.io/collections"
collcodec "cosmossdk.io/collections/codec"

"github.com/cosmos/cosmos-sdk/types/kv"
)

func NewStoreDecoderFuncFromCollectionsSchema(schema collections.Schema) func(kvA, kvB kv.Pair) string {
colls := schema.ListCollections()
prefixes := make([][]byte, len(colls))
valueCodecs := make([]collcodec.UntypedValueCodec, len(colls))
for i, coll := range colls {
prefixes[i] = coll.GetPrefix()
valueCodecs[i] = coll.ValueCodec()
}

return func(kvA, kvB kv.Pair) string {
for i, prefix := range prefixes {
if bytes.HasPrefix(kvA.Key, prefix) {
if !bytes.HasPrefix(kvB.Key, prefix) {
panic(fmt.Sprintf("prefix mismatch, keyA has prefix %x (%s), but keyB does not %x (%s)", prefix, prefix, kvB.Key, kvB.Key))
}
vc := valueCodecs[i]
// unmarshal kvA.Value to the corresponding type
vA, err := vc.Decode(kvA.Value)
if err != nil {
panic(err)
}
// unmarshal kvB.Value to the corresponding type
vB, err := vc.Decode(kvB.Value)
if err != nil {
panic(err)
}
vAString, err := vc.Stringify(vA)
if err != nil {
panic(err)
}
vBString, err := vc.Stringify(vB)
if err != nil {
panic(err)
}
return vAString + "\n" + vBString
}
}
panic(fmt.Errorf("unexpected key %X (%s)", kvA.Key, kvA.Key))
}
}
204 changes: 204 additions & 0 deletions simulation/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package simulation_test

import (
"fmt"
"time"

"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
auth "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
govtypes2 "github.com/cosmos/cosmos-sdk/x/gov/types"
govtypes1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
slashing "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
feemarkettypes "github.com/evmos/ethermint/x/feemarket/types"
epochstypes "github.com/osmosis-labs/osmosis/v15/x/epochs/types"
gammtypes "github.com/osmosis-labs/osmosis/v15/x/gamm/types"
txfeestypes "github.com/osmosis-labs/osmosis/v15/x/txfees/types"

"github.com/dymensionxyz/dymension/v3/app"
dymnstypes "github.com/dymensionxyz/dymension/v3/x/dymns/types"
incentivestypes "github.com/dymensionxyz/dymension/v3/x/incentives/types"
rollapptypes "github.com/dymensionxyz/dymension/v3/x/rollapp/types"
sequencertypes "github.com/dymensionxyz/dymension/v3/x/sequencer/types"
)

func prepareGenesis(cdc codec.JSONCodec) (app.GenesisState, error) {
genesis := app.NewDefaultGenesisState(cdc)

// Modify gov params
govGenesis := govtypes1.DefaultGenesisState()
govGenesis.Params.MinDeposit[0].Amount = math.NewInt(10000000000)
govGenesis.Params.MinDeposit[0].Denom = "adym"
govVotingPeriod := time.Minute
govGenesis.Params.VotingPeriod = &govVotingPeriod
govRawGenesis, err := cdc.MarshalJSON(govGenesis)
if err != nil {
return app.GenesisState{}, fmt.Errorf("failed to marshal gov genesis state: %w", err)
}
genesis[govtypes2.ModuleName] = govRawGenesis

// Modify rollapp params
rollappGenesis := rollapptypes.DefaultGenesis()
rollappGenesis.Params.DisputePeriodInBlocks = 50
rollappRawGenesis, err := cdc.MarshalJSON(rollappGenesis)
if err != nil {
return app.GenesisState{}, fmt.Errorf("failed to marshal rollapp genesis state: %w", err)
}
genesis[rollapptypes.ModuleName] = rollappRawGenesis

// Modify sequencer params
sequencerGenesis := sequencertypes.DefaultGenesis()
sequencerGenesis.Params.NoticePeriod = time.Minute
sequencerRawGenesis, err := cdc.MarshalJSON(sequencerGenesis)
if err != nil {
return app.GenesisState{}, fmt.Errorf("failed to marshal sequencer genesis state: %w", err)
}
genesis[sequencertypes.ModuleName] = sequencerRawGenesis

// Modify auth params
authGenesis := auth.DefaultGenesisState()
authGenesis.Params.TxSizeCostPerByte = 100
authRawGenesis, err := cdc.MarshalJSON(authGenesis)
if err != nil {
return app.GenesisState{}, fmt.Errorf("failed to marshal auth genesis state: %w", err)
}
genesis[auth.ModuleName] = authRawGenesis

// Modify slashing params
slashingGenesis := slashing.DefaultGenesisState()
slashingGenesis.Params.SignedBlocksWindow = 10000
slashingGenesis.Params.MinSignedPerWindow = math.LegacyMustNewDecFromStr("0.800000000000000000")
slashingGenesis.Params.DowntimeJailDuration = 2 * time.Minute
slashingGenesis.Params.SlashFractionDowntime = math.LegacyZeroDec()
slashingRawGenesis, err := cdc.MarshalJSON(slashingGenesis)
if err != nil {
return app.GenesisState{}, fmt.Errorf("failed to marshal slashing genesis state: %w", err)
}
genesis[slashing.ModuleName] = slashingRawGenesis

// Modify staking params
stakingGenesis := stakingtypes.DefaultGenesisState()
stakingGenesis.Params.BondDenom = "adym"
stakingRawGenesis, err := cdc.MarshalJSON(stakingGenesis)
if err != nil {
return app.GenesisState{}, fmt.Errorf("failed to marshal staking genesis state: %w", err)
}
genesis[stakingtypes.ModuleName] = stakingRawGenesis

// Modify mint params
mintGenesis := minttypes.DefaultGenesisState()
mintGenesis.Params.MintDenom = "adym"
mintRawGenesis, err := cdc.MarshalJSON(mintGenesis)
if err != nil {
return app.GenesisState{}, fmt.Errorf("failed to marshal mint genesis state: %w", err)
}
genesis[minttypes.ModuleName] = mintRawGenesis

// Modify evm params
evmGenesis := evmtypes.DefaultGenesisState()
evmGenesis.Params.EvmDenom = "adym"
evmGenesis.Params.EnableCreate = false
evmRawGenesis, err := cdc.MarshalJSON(evmGenesis)
if err != nil {
return app.GenesisState{}, fmt.Errorf("failed to marshal evm genesis state: %w", err)
}
genesis[evmtypes.ModuleName] = evmRawGenesis

// Modify feemarket params
feemarketGenesis := feemarkettypes.DefaultGenesisState()
feemarketGenesis.Params.NoBaseFee = true
feemarketRawGenesis, err := cdc.MarshalJSON(feemarketGenesis)
if err != nil {
return app.GenesisState{}, fmt.Errorf("failed to marshal feemarket genesis state: %w", err)
}
genesis[feemarkettypes.ModuleName] = feemarketRawGenesis

// Modify dymns params
dymnsGenesis := dymnstypes.DefaultGenesis()
dymnsGenesis.Params.Misc.SellOrderDuration = 2 * time.Minute
dymnsRawGenesis, err := cdc.MarshalJSON(dymnsGenesis)
if err != nil {
return app.GenesisState{}, fmt.Errorf("failed to marshal dymns genesis state: %w", err)
}
genesis[dymnstypes.ModuleName] = dymnsRawGenesis

// Modify bank denom metadata
bankGenesis := banktypes.DefaultGenesisState()
bankGenesis.DenomMetadata = []banktypes.Metadata{
{
Base: "adym",
DenomUnits: []*banktypes.DenomUnit{
{Denom: "adym", Exponent: 0},
{Denom: "DYM", Exponent: 18},
},
Description: "Denom metadata for DYM (adym)",
Display: "DYM",
Name: "DYM",
Symbol: "DYM",
},
}
bankRawGenesis, err := cdc.MarshalJSON(bankGenesis)
if err != nil {
return app.GenesisState{}, fmt.Errorf("failed to marshal bank genesis state: %w", err)
}
genesis[banktypes.ModuleName] = bankRawGenesis

// Modify misc params
crisisGenesis := crisistypes.DefaultGenesisState()
crisisGenesis.ConstantFee.Denom = "adym"
crisisRawGenesis, err := cdc.MarshalJSON(crisisGenesis)
if err != nil {
return app.GenesisState{}, fmt.Errorf("failed to marshal crisis genesis state: %w", err)
}
genesis[crisistypes.ModuleName] = crisisRawGenesis

txfeesGenesis := txfeestypes.DefaultGenesis()
txfeesGenesis.Basedenom = "adym"
txfeesGenesis.Params.EpochIdentifier = "minute"
txfeesRawGenesis, err := cdc.MarshalJSON(txfeesGenesis)
if err != nil {
return app.GenesisState{}, fmt.Errorf("failed to marshal txfees genesis state: %w", err)
}
genesis[txfeestypes.ModuleName] = txfeesRawGenesis

gammGenesis := gammtypes.DefaultGenesis()
gammGenesis.Params.PoolCreationFee[0].Denom = "adym"
gammGenesis.Params.EnableGlobalPoolFees = true
gammRawGenesis, err := cdc.MarshalJSON(gammGenesis)
if err != nil {
return app.GenesisState{}, fmt.Errorf("failed to marshal gamm genesis state: %w", err)
}
genesis[gammtypes.ModuleName] = gammRawGenesis

// Modify incentives params
incentivesGenesis := incentivestypes.DefaultGenesis()
incentivesGenesis.Params.DistrEpochIdentifier = "minute"
incentivesGenesis.LockableDurations = []time.Duration{time.Minute}
incentivesRawGenesis, err := cdc.MarshalJSON(incentivesGenesis)
if err != nil {
return app.GenesisState{}, fmt.Errorf("failed to marshal incentives genesis state: %w", err)
}
genesis[incentivestypes.ModuleName] = incentivesRawGenesis

// Modify epochs params
epochsGenesis := epochstypes.DefaultGenesis()
epochsGenesis.Epochs = append(epochsGenesis.Epochs, epochstypes.EpochInfo{
Identifier: "minute",
StartTime: time.Time{},
Duration: time.Minute,
CurrentEpoch: 0,
CurrentEpochStartHeight: 0,
})
epochsRawGenesis, err := cdc.MarshalJSON(epochsGenesis)
if err != nil {
return app.GenesisState{}, fmt.Errorf("failed to marshal epochs genesis state: %w", err)
}
genesis[epochstypes.ModuleName] = epochsRawGenesis

return genesis, nil
}
Loading

0 comments on commit ed7dde7

Please sign in to comment.