Skip to content

Commit 4e8b39c

Browse files
committed
fix sim
sims tag
1 parent 5af2028 commit 4e8b39c

File tree

5 files changed

+240
-171
lines changed

5 files changed

+240
-171
lines changed

app/app.go

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"slices"
2727
"sort"
2828

29+
authmodulev1 "cosmossdk.io/api/cosmos/auth/module/v1"
2930
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
3031
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
3132
"cosmossdk.io/client/v2/autocli"
@@ -163,22 +164,31 @@ var (
163164
DefaultNodeHome string
164165

165166
// module account permissions
166-
maccPerms = map[string][]string{
167-
authtypes.FeeCollectorName: nil,
168-
distrtypes.ModuleName: nil,
169-
pooltypes.ModuleName: nil,
170-
pooltypes.StreamAccount: nil,
171-
pooltypes.ProtocolPoolDistrAccount: nil,
172-
minttypes.ModuleName: {authtypes.Minter},
173-
stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking},
174-
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
175-
govtypes.ModuleName: {authtypes.Burner},
167+
moduleAccPerms = []*authmodulev1.ModuleAccountPermission{
168+
{Account: authtypes.FeeCollectorName},
169+
{Account: distrtypes.ModuleName},
170+
{Account: pooltypes.ModuleName},
171+
{Account: pooltypes.StreamAccount},
172+
{Account: pooltypes.ProtocolPoolDistrAccount},
173+
{Account: minttypes.ModuleName, Permissions: []string{authtypes.Minter}},
174+
{Account: stakingtypes.BondedPoolName, Permissions: []string{authtypes.Burner, stakingtypes.ModuleName}},
175+
{Account: stakingtypes.NotBondedPoolName, Permissions: []string{authtypes.Burner, stakingtypes.ModuleName}},
176+
{Account: govtypes.ModuleName, Permissions: []string{authtypes.Burner}},
176177
// used for secure addition and subtraction of balance using module account
177-
evmtypes.ModuleName: {authtypes.Minter, authtypes.Burner},
178+
{Account: evmtypes.ModuleName, Permissions: []string{authtypes.Minter, authtypes.Burner}},
178179
}
179180

180-
// module accounts that are allowed to receive tokens
181-
allowedReceivingModAcc = map[string]bool{}
181+
// blocked account addresses
182+
blockAccAddrs = []string{
183+
authtypes.FeeCollectorName,
184+
distrtypes.ModuleName,
185+
minttypes.ModuleName,
186+
stakingtypes.BondedPoolName,
187+
stakingtypes.NotBondedPoolName,
188+
// We allow the following module accounts to receive funds:
189+
// govtypes.ModuleName
190+
// pooltypes.ModuleName
191+
}
182192
)
183193

184194
var (
@@ -386,7 +396,7 @@ func NewEthermintApp(
386396
appCodec,
387397
ethermint.ProtoAccount,
388398
accountsKeeper,
389-
maccPerms,
399+
GetMaccPerms(),
390400
signingCtx.AddressCodec(),
391401
sdk.Bech32MainPrefix,
392402
authAddr,
@@ -397,7 +407,7 @@ func NewEthermintApp(
397407
appCodec,
398408
okeys[banktypes.ObjectStoreKey],
399409
app.AuthKeeper,
400-
app.BlockedAddrs(),
410+
app.BlockedAddresses(),
401411
authAddr,
402412
)
403413

@@ -952,25 +962,23 @@ func (app *EthermintApp) LoadHeight(height int64) error {
952962
return app.LoadVersion(height)
953963
}
954964

955-
// ModuleAccountAddrs returns all the app's module account addresses.
956-
func (app *EthermintApp) ModuleAccountAddrs() map[string]bool {
957-
modAccAddrs := make(map[string]bool)
958-
for acc := range maccPerms {
959-
modAccAddrs[authtypes.NewModuleAddress(acc).String()] = true
960-
}
961-
962-
return modAccAddrs
963-
}
965+
// BlockedAddresses returns all the app's blocked account addresses.
966+
// This function takes an address.Codec parameter to maintain compatibility
967+
// with the signature of the same function in appV1.
968+
func (app *EthermintApp) BlockedAddresses() map[string]bool {
969+
result := make(map[string]bool)
964970

965-
// BlockedAddrs returns all the app's module account addresses that are not
966-
// allowed to receive external tokens.
967-
func (app *EthermintApp) BlockedAddrs() map[string]bool {
968-
blockedAddrs := make(map[string]bool)
969-
for acc := range maccPerms {
970-
blockedAddrs[authtypes.NewModuleAddress(acc).String()] = !allowedReceivingModAcc[acc]
971+
if len(blockAccAddrs) > 0 {
972+
for _, addr := range blockAccAddrs {
973+
result[addr] = true
974+
}
975+
} else {
976+
for addr := range GetMaccPerms() {
977+
result[addr] = true
978+
}
971979
}
972980

973-
return blockedAddrs
981+
return result
974982
}
975983

976984
// LegacyAmino returns EthermintApp's amino codec.
@@ -1168,12 +1176,15 @@ func RegisterSwaggerAPI(_ client.Context, rtr *mux.Router) {
11681176
}
11691177

11701178
// GetMaccPerms returns a copy of the module account permissions
1179+
//
1180+
// NOTE: This is solely to be used for testing purposes.
11711181
func GetMaccPerms() map[string][]string {
1172-
dupMaccPerms := make(map[string][]string)
1173-
for k, v := range maccPerms {
1174-
dupMaccPerms[k] = v
1182+
dup := make(map[string][]string)
1183+
for _, perms := range moduleAccPerms {
1184+
dup[perms.Account] = perms.Permissions
11751185
}
1176-
return dupMaccPerms
1186+
1187+
return dup
11771188
}
11781189

11791190
// initParamsKeeper init params keeper and its subspaces

app/simulation_test.go

Lines changed: 83 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
//go:build sims
2+
13
package app_test
24

35
// TODO: COsmos SDK fix for the simulator issue for custom keys
46
import (
57
"encoding/json"
68
"flag"
9+
"fmt"
710
"io"
811
"math/rand"
912
"sync"
@@ -21,12 +24,17 @@ import (
2124
abci "github.com/cometbft/cometbft/abci/types"
2225
cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
2326
"github.com/cosmos/cosmos-sdk/baseapp"
27+
"github.com/cosmos/cosmos-sdk/client/flags"
28+
"github.com/cosmos/cosmos-sdk/runtime"
29+
"github.com/cosmos/cosmos-sdk/server"
2430
servertypes "github.com/cosmos/cosmos-sdk/server/types"
2531
"github.com/cosmos/cosmos-sdk/simsx"
32+
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
2633
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
2734
"github.com/cosmos/cosmos-sdk/x/simulation"
2835
simcli "github.com/cosmos/cosmos-sdk/x/simulation/client/cli"
2936
"github.com/evmos/ethermint/app"
37+
"github.com/evmos/ethermint/app/ante"
3038
"github.com/evmos/ethermint/testutil"
3139
"github.com/stretchr/testify/assert"
3240
"github.com/stretchr/testify/require"
@@ -55,25 +63,24 @@ func setupStateFactory(app *app.EthermintApp) simsx.SimStateFactory {
5563
return simsx.SimStateFactory{
5664
Codec: app.AppCodec(),
5765
AppStateFn: testutil.StateFn(app),
58-
BlockedAddr: app.BlockedAddrs(),
66+
BlockedAddr: app.BlockedAddresses(),
5967
AccountSource: app.AuthKeeper,
6068
BalanceSource: app.BankKeeper,
6169
}
6270
}
6371

6472
func TestFullAppSimulation(t *testing.T) {
65-
return
6673
config := simcli.NewConfigFromFlags()
6774
config.ChainID = SimAppChainID
6875
config.BlockMaxGas = SimBlockMaxGas
69-
simsx.RunWithSeed(
76+
simsx.RunWithSeedAndRandAcc(
7077
t,
7178
config,
72-
app.NewEthermintApp,
79+
NewSimApp,
7380
setupStateFactory,
7481
config.Seed,
7582
config.FuzzSeed,
76-
simtypes.RandomAccounts,
83+
testutil.RandomAccounts,
7784
)
7885
}
7986

@@ -82,15 +89,54 @@ var (
8289
exportWithValidatorSet []string
8390
)
8491

92+
func NewSimApp(
93+
logger log.Logger,
94+
db corestore.KVStoreWithBatch,
95+
traceStore io.Writer,
96+
loadLatest bool,
97+
appOpts servertypes.AppOptions,
98+
baseAppOptions ...func(*baseapp.BaseApp),
99+
) *app.EthermintApp {
100+
appOptions := make(simtestutil.AppOptionsMap, 0)
101+
appOptions[flags.FlagHome] = app.DefaultNodeHome
102+
appOptions[server.FlagInvCheckPeriod] = simcli.FlagPeriodValue
103+
app := app.NewEthermintApp(logger, db, nil, false, appOptions, baseAppOptions...)
104+
// disable feemarket on native tx
105+
anteHandler, err := ante.NewAnteHandler(ante.HandlerOptions{
106+
Environment: runtime.NewEnvironment(
107+
nil,
108+
logger,
109+
), // nil is set as the kvstoreservice to avoid module access
110+
ConsensusKeeper: app.ConsensusParamsKeeper,
111+
AccountKeeper: app.AuthKeeper,
112+
AccountAbstractionKeeper: app.AccountsKeeper,
113+
BankKeeper: app.BankKeeper,
114+
SignModeHandler: app.TxConfig().SignModeHandler(),
115+
FeegrantKeeper: app.FeeGrantKeeper,
116+
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
117+
EvmKeeper: app.EvmKeeper,
118+
FeeMarketKeeper: app.FeeMarketKeeper,
119+
MaxTxGasWanted: 0,
120+
})
121+
if err != nil {
122+
panic(err)
123+
}
124+
app.SetAnteHandler(anteHandler)
125+
if err := app.LoadLatestVersion(); err != nil {
126+
panic(err)
127+
}
128+
return app
129+
}
130+
85131
func TestAppImportExport(t *testing.T) {
86132
return
87133
config := simcli.NewConfigFromFlags()
88134
config.ChainID = SimAppChainID
89135
config.BlockMaxGas = SimBlockMaxGas
90-
simsx.RunWithSeed(
136+
simsx.RunWithSeedAndRandAcc(
91137
t,
92138
config,
93-
app.NewEthermintApp,
139+
NewSimApp,
94140
setupStateFactory,
95141
config.Seed,
96142
config.FuzzSeed,
@@ -102,11 +148,14 @@ func TestAppImportExport(t *testing.T) {
102148
require.NoError(t, err)
103149

104150
t.Log("importing genesis...\n")
105-
newTestInstance := simsx.NewSimulationAppInstance(t, ti.Cfg, app.NewEthermintApp)
151+
newTestInstance := simsx.NewSimulationAppInstance(t, ti.Cfg, NewSimApp)
106152
newApp := newTestInstance.App
107153
var genesisState map[string]json.RawMessage
108154
require.NoError(t, json.Unmarshal(exported.AppState, &genesisState))
109-
ctxB := newApp.NewContextLegacy(true, cmtproto.Header{Height: a.LastBlockHeight()})
155+
ctxB := newApp.NewContextLegacy(true, cmtproto.Header{
156+
Height: a.LastBlockHeight(),
157+
ChainID: config.ChainID,
158+
})
110159
_, err = newApp.ModuleManager.InitGenesis(ctxB, genesisState)
111160
if simapp.IsEmptyValidatorSetErr(err) {
112161
t.Skip("Skipping simulation as all validators have been unbonded")
@@ -135,10 +184,10 @@ func TestAppSimulationAfterImport(t *testing.T) {
135184
config := simcli.NewConfigFromFlags()
136185
config.ChainID = SimAppChainID
137186
config.BlockMaxGas = SimBlockMaxGas
138-
simsx.RunWithSeed(
187+
simsx.RunWithSeedAndRandAcc(
139188
t,
140189
config,
141-
app.NewEthermintApp,
190+
NewSimApp,
142191
setupStateFactory,
143192
config.Seed,
144193
config.FuzzSeed,
@@ -158,7 +207,7 @@ func TestAppSimulationAfterImport(t *testing.T) {
158207

159208
_, err = a.InitChain(&abci.InitChainRequest{
160209
AppStateBytes: exported.AppState,
161-
ChainId: simsx.SimAppChainID,
210+
ChainId: config.ChainID,
162211
InitialHeight: exported.Height,
163212
Time: genesisTimestamp,
164213
})
@@ -170,18 +219,17 @@ func TestAppSimulationAfterImport(t *testing.T) {
170219
// use accounts from initial run
171220
return exported.AppState, accs, config.ChainID, genesisTimestamp
172221
},
173-
BlockedAddr: a.BlockedAddrs(),
222+
BlockedAddr: a.BlockedAddresses(),
174223
AccountSource: a.AuthKeeper,
175224
BalanceSource: a.BankKeeper,
176225
}
177226
}
178227
ti.Cfg.InitialBlockHeight = int(exported.Height)
179-
simsx.RunWithSeed(t, ti.Cfg, app.NewEthermintApp, importGenesisStateFactory, ti.Cfg.Seed, ti.Cfg.FuzzSeed, testutil.RandomAccounts)
228+
simsx.RunWithSeedAndRandAcc(t, ti.Cfg, NewSimApp, importGenesisStateFactory, ti.Cfg.Seed, ti.Cfg.FuzzSeed, testutil.RandomAccounts)
180229
})
181230
}
182231

183232
func TestAppStateDeterminism(t *testing.T) {
184-
return
185233
const numTimesToRunPerSeed = 3
186234
var seeds []int64
187235
if s := simcli.NewConfigFromFlags().Seed; s != simcli.DefaultSeedValue {
@@ -214,7 +262,7 @@ func TestAppStateDeterminism(t *testing.T) {
214262
return others.Get(k)
215263
})
216264
}
217-
return app.NewEthermintApp(logger, db, nil, true, appOpts, append(baseAppOptions, interBlockCacheOpt())...)
265+
return NewSimApp(logger, db, nil, true, appOpts, append(baseAppOptions, interBlockCacheOpt())...)
218266
}
219267
var mx sync.Mutex
220268
appHashResults := make(map[int64][][]byte)
@@ -245,13 +293,23 @@ func TestAppStateDeterminism(t *testing.T) {
245293
}
246294
}
247295
// run simulations
248-
simsx.RunWithSeeds(
249-
t,
250-
interBlockCachingAppFactory,
251-
setupStateFactory,
252-
seeds,
253-
[]byte{},
254-
testutil.RandomAccounts,
255-
captureAndCheckHash,
256-
)
296+
cfg := simcli.NewConfigFromFlags()
297+
cfg.ChainID = SimAppChainID
298+
for i := range seeds {
299+
seed := seeds[i]
300+
t.Run(fmt.Sprintf("seed: %d", seed), func(t *testing.T) {
301+
t.Parallel()
302+
simsx.RunWithSeedAndRandAcc(
303+
t,
304+
cfg,
305+
interBlockCachingAppFactory,
306+
setupStateFactory,
307+
seed,
308+
[]byte{},
309+
testutil.RandomAccounts,
310+
captureAndCheckHash,
311+
)
312+
})
313+
}
314+
257315
}

0 commit comments

Comments
 (0)