Skip to content

Commit

Permalink
Merge pull request #2983 from irisnet/feature/up_simulate
Browse files Browse the repository at this point in the history
update simulation test
  • Loading branch information
mitch1024 authored Dec 19, 2024
2 parents af27100 + f2c606d commit 684f347
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 83 deletions.
15 changes: 12 additions & 3 deletions app/ante/decorators.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,15 @@ func (vtd ValidateTokenDecorator) AnteHandle(
}

// ValidateServiceDecorator is responsible for checking the permission to execute MsgCallService
type ValidateServiceDecorator struct{}
type ValidateServiceDecorator struct {
SimulateTest bool
}

// NewValidateServiceDecorator returns an instance of ServiceAuthDecorator
func NewValidateServiceDecorator() ValidateServiceDecorator {
return ValidateServiceDecorator{}
func NewValidateServiceDecorator(simulateTest bool) ValidateServiceDecorator {
return ValidateServiceDecorator{
SimulateTest: simulateTest,
}
}

// AnteHandle checks the transaction
Expand All @@ -77,6 +81,10 @@ func (vsd ValidateServiceDecorator) AnteHandle(
simulate bool,
next sdk.AnteHandler,
) (sdk.Context, error) {
if vsd.SimulateTest {
return next(ctx, tx, simulate)
}

for _, msg := range tx.GetMsgs() {
switch msg := msg.(type) {
case *servicetypes.MsgCallService:
Expand All @@ -85,6 +93,7 @@ func (vsd ValidateServiceDecorator) AnteHandle(
}
}
}

return next(ctx, tx, simulate)
}

Expand Down
3 changes: 2 additions & 1 deletion app/ante/handler_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type HandlerOptions struct {
FeeMarketKeeper ethante.FeeMarketKeeper
BypassMinFeeMsgTypes []string
MaxTxGasWanted uint64
SimulationTest bool
}

// newCosmosAnteHandler creates the default ante handler for Ethereum transactions
Expand Down Expand Up @@ -85,7 +86,7 @@ func newCosmosAnteHandler(options HandlerOptions) sdk.AnteHandler {
NewValidateTokenDecorator(options.TokenKeeper),
tokenkeeper.NewValidateTokenFeeDecorator(options.TokenKeeper, options.BankKeeper),
oraclekeeper.NewValidateOracleAuthDecorator(options.OracleKeeper, options.GuardianKeeper),
NewValidateServiceDecorator(),
NewValidateServiceDecorator(options.SimulationTest),
ante.NewIncrementSequenceDecorator(options.AccountKeeper),
ibcante.NewRedundantRelayDecorator(options.IBCKeeper),
)
Expand Down
8 changes: 8 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ func NewIrisApp(
app.MountMemoryStores(app.MemoryStoreKeys())

maxGasWanted := cast.ToUint64(appOpts.Get(srvflags.EVMMaxTxGasWanted))

simulationTest := false
opt = appOpts.Get(params.SimulationTest)
if opt, ok := opt.(bool); ok {
simulationTest = opt
}

anteHandler := irishubante.NewAnteHandler(
irishubante.HandlerOptions{
HandlerOptions: ante.HandlerOptions{
Expand All @@ -195,6 +202,7 @@ func NewIrisApp(
FeeMarketKeeper: app.FeeMarketKeeper,
BypassMinFeeMsgTypes: []string{},
MaxTxGasWanted: maxGasWanted,
SimulationTest: simulationTest,
},
)

Expand Down
95 changes: 52 additions & 43 deletions app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package app
import (
"encoding/json"
"fmt"
"log"

tmproto "github.com/cometbft/cometbft/proto/tendermint/types"

storetypes "cosmossdk.io/store/types"
Expand Down Expand Up @@ -72,9 +70,9 @@ func (app *IrisApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [
allowedAddrsMap := make(map[string]bool)

for _, addr := range jailAllowedAddrs {
_, err := sdk.ValAddressFromBech32(addr)
_, err := app.InterfaceRegistry().SigningContext().ValidatorAddressCodec().StringToBytes(addr)
if err != nil {
log.Fatal(err)
panic(err)
}
allowedAddrsMap[addr] = true
}
Expand All @@ -85,31 +83,32 @@ func (app *IrisApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [
/* Handle fee distribution state. */

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

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

for _, delegation := range dels {
valAddr, err := sdk.ValAddressFromBech32(delegation.ValidatorAddress)
if err != nil {
panic(err)
}

delAddr, err := sdk.AccAddressFromBech32(delegation.DelegatorAddress)
if err != nil {
panic(err)
}
delAddr := sdk.MustAccAddressFromBech32(delegation.DelegatorAddress)

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

Expand All @@ -124,30 +123,36 @@ func (app *IrisApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [
ctx = ctx.WithBlockHeight(0)

// reinitialize all validators
app.StakingKeeper.IterateValidators(
err = app.StakingKeeper.IterateValidators(
ctx,
func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
valBz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator())
if err != nil {
panic(err)
}
// donate any unwithdrawn outstanding reward fraction tokens to the community pool
scraps, err := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, sdk.ValAddress(val.GetOperator()))
scraps, err := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, valBz)
if err != nil {
panic(err)
}
feePool, err := app.DistrKeeper.FeePool.Get(ctx)
if err != nil {
panic(err)
}

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

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

// reinitialize all delegations
for _, del := range dels {
Expand All @@ -174,28 +179,31 @@ func (app *IrisApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [
/* Handle staking state. */

// iterate through redelegations, reset creation height
app.StakingKeeper.IterateRedelegations(
ctx,
func(_ int64, red stakingtypes.Redelegation) (stop bool) {
for i := range red.Entries {
red.Entries[i].CreationHeight = 0
}
app.StakingKeeper.SetRedelegation(ctx, red)
return false
},
)
err = app.StakingKeeper.IterateRedelegations(ctx, func(_ int64, red stakingtypes.Redelegation) (stop bool) {
for i := range red.Entries {
red.Entries[i].CreationHeight = 0
}
err = app.StakingKeeper.SetRedelegation(ctx, red)
if err != nil {
panic(err)
}
return false
})
if err != nil {
panic(err)
}

// iterate through unbonding delegations, reset creation height
app.StakingKeeper.IterateUnbondingDelegations(
ctx,
func(_ int64, ubd stakingtypes.UnbondingDelegation) (stop bool) {
for i := range ubd.Entries {
ubd.Entries[i].CreationHeight = 0
}
app.StakingKeeper.SetUnbondingDelegation(ctx, ubd)
return false
},
)
app.StakingKeeper.IterateUnbondingDelegations(ctx, func(_ int64, ubd stakingtypes.UnbondingDelegation) (stop bool) {
for i := range ubd.Entries {
ubd.Entries[i].CreationHeight = 0
}
err = app.StakingKeeper.SetUnbondingDelegation(ctx, ubd)
if err != nil {
panic(err)
}
return false
})

// Iterate through validators by power descending, reset bond heights, and
// update bond intra-tx counters.
Expand All @@ -207,15 +215,16 @@ func (app *IrisApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [
addr := sdk.ValAddress(stakingtypes.AddressFromValidatorsKey(iter.Key()))
validator, err := app.StakingKeeper.GetValidator(ctx, addr)
if err != nil {
panic(err)
panic("expected validator, not found")
}

validator.UnbondingHeight = 0
if applyAllowedAddrs && !allowedAddrsMap[addr.String()] {
validator.Jailed = true
}

if err := app.StakingKeeper.SetValidator(ctx, validator); err != nil {
err = app.StakingKeeper.SetValidator(ctx, validator)
if err != nil {
panic(err)
}
counter++
Expand Down
1 change: 1 addition & 0 deletions app/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ package params
const (
StakePerAccount = "stake_per_account"
InitiallyBondedValidators = "initially_bonded_validators"
SimulationTest = "simulation_test"
)
10 changes: 8 additions & 2 deletions app/sim_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
simcli "github.com/cosmos/cosmos-sdk/x/simulation/client/cli"

"github.com/irisnet/irishub/v4/app/params"
)

// Profile with:
Expand Down Expand Up @@ -51,7 +53,9 @@ func BenchmarkFullAppSimulation(b *testing.B) {
nil,
true,
encfg,
EmptyAppOptions{},
SimTestAppOptions{
options: map[string]interface{}{params.SimulationTest: true},
},
interBlockCacheOpt(),
)

Expand Down Expand Up @@ -118,7 +122,9 @@ func BenchmarkInvariants(b *testing.B) {
nil,
true,
encfg,
EmptyAppOptions{},
SimTestAppOptions{
options: map[string]interface{}{params.SimulationTest: true},
},
interBlockCacheOpt(),
)

Expand Down
23 changes: 19 additions & 4 deletions app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func TestAppImportExport(t *testing.T) {
require.NoError(t, os.RemoveAll(newDir))
}()

newApp := createApp(logger, db, encfg, fauxMerkleModeOpt)
newApp := createApp(logger, newDB, encfg, fauxMerkleModeOpt)
require.Equal(t, "IrisApp", newApp.Name())

var genesisState iristypes.GenesisState
Expand Down Expand Up @@ -366,13 +366,16 @@ func TestAppSimulationAfterImport(t *testing.T) {
require.NoError(t, os.RemoveAll(newDir))
}()

newApp := createApp(logger, db, encfg, fauxMerkleModeOpt)
newApp := createApp(logger, newDB, encfg, fauxMerkleModeOpt)
require.Equal(t, "IrisApp", newApp.Name())

newApp.InitChain(&abci.RequestInitChain{
_, err = newApp.InitChain(&abci.RequestInitChain{
AppStateBytes: exported.AppState,
ChainId: AppChainID,
})

require.NoError(t, err)

_, _, err = simulation.SimulateFromSeed(
t,
os.Stdout,
Expand Down Expand Up @@ -475,6 +478,16 @@ func (ao EmptyAppOptions) Get(o string) interface{} {
return nil
}

// SimTestAppOptions is a stub implementing AppOptions
type SimTestAppOptions struct {
options map[string]interface{}
}

// Get implements AppOptions
func (o SimTestAppOptions) Get(key string) interface{} {
return o.options[key]
}

func createApp(
logger log.Logger,
db dbm.DB,
Expand All @@ -491,7 +504,9 @@ func createApp(
nil,
true,
encodingConfig,
EmptyAppOptions{},
SimTestAppOptions{
options: map[string]interface{}{params.SimulationTest: true},
},
baseAppOptions...,
)
}
20 changes: 10 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ require (
github.com/cosmos/gogoproto v1.7.0
github.com/cosmos/iavl v1.2.0 // indirect
github.com/evmos/ethermint v0.22.0
mods.irisnet.org/modules/coinswap v0.0.0-20241209074433-1380d52b7709
mods.irisnet.org/modules/farm v0.0.0-20241209074433-1380d52b7709
mods.irisnet.org/modules/htlc v0.0.0-20241209074433-1380d52b7709
mods.irisnet.org/modules/mt v0.0.0-20241209074433-1380d52b7709
mods.irisnet.org/modules/nft v0.0.0-20241209074433-1380d52b7709
mods.irisnet.org/modules/oracle v0.0.0-20241209074433-1380d52b7709
mods.irisnet.org/modules/random v0.0.0-20241209074433-1380d52b7709
mods.irisnet.org/modules/record v0.0.0-20241209074433-1380d52b7709
mods.irisnet.org/modules/service v0.0.0-20241209074433-1380d52b7709
mods.irisnet.org/modules/token v0.0.0-20241209074433-1380d52b7709
mods.irisnet.org/modules/coinswap v0.0.0-20241217080151-0ad41be03ac6
mods.irisnet.org/modules/farm v0.0.0-20241217080151-0ad41be03ac6
mods.irisnet.org/modules/htlc v0.0.0-20241217080151-0ad41be03ac6
mods.irisnet.org/modules/mt v0.0.0-20241217080151-0ad41be03ac6
mods.irisnet.org/modules/nft v0.0.0-20241217080151-0ad41be03ac6
mods.irisnet.org/modules/oracle v0.0.0-20241217080151-0ad41be03ac6
mods.irisnet.org/modules/random v0.0.0-20241217080151-0ad41be03ac6
mods.irisnet.org/modules/record v0.0.0-20241217080151-0ad41be03ac6
mods.irisnet.org/modules/service v0.0.0-20241217080151-0ad41be03ac6
mods.irisnet.org/modules/token v0.0.0-20241217080151-0ad41be03ac6
)

require (
Expand Down
Loading

0 comments on commit 684f347

Please sign in to comment.