Skip to content

Commit 6f6ac92

Browse files
committed
fix sim
1 parent 5af2028 commit 6f6ac92

File tree

5 files changed

+234
-167
lines changed

5 files changed

+234
-167
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: 81 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package app_test
44
import (
55
"encoding/json"
66
"flag"
7+
"fmt"
78
"io"
89
"math/rand"
910
"sync"
@@ -21,12 +22,17 @@ import (
2122
abci "github.com/cometbft/cometbft/abci/types"
2223
cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
2324
"github.com/cosmos/cosmos-sdk/baseapp"
25+
"github.com/cosmos/cosmos-sdk/client/flags"
26+
"github.com/cosmos/cosmos-sdk/runtime"
27+
"github.com/cosmos/cosmos-sdk/server"
2428
servertypes "github.com/cosmos/cosmos-sdk/server/types"
2529
"github.com/cosmos/cosmos-sdk/simsx"
30+
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
2631
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
2732
"github.com/cosmos/cosmos-sdk/x/simulation"
2833
simcli "github.com/cosmos/cosmos-sdk/x/simulation/client/cli"
2934
"github.com/evmos/ethermint/app"
35+
"github.com/evmos/ethermint/app/ante"
3036
"github.com/evmos/ethermint/testutil"
3137
"github.com/stretchr/testify/assert"
3238
"github.com/stretchr/testify/require"
@@ -55,25 +61,24 @@ func setupStateFactory(app *app.EthermintApp) simsx.SimStateFactory {
5561
return simsx.SimStateFactory{
5662
Codec: app.AppCodec(),
5763
AppStateFn: testutil.StateFn(app),
58-
BlockedAddr: app.BlockedAddrs(),
64+
BlockedAddr: app.BlockedAddresses(),
5965
AccountSource: app.AuthKeeper,
6066
BalanceSource: app.BankKeeper,
6167
}
6268
}
6369

6470
func TestFullAppSimulation(t *testing.T) {
65-
return
6671
config := simcli.NewConfigFromFlags()
6772
config.ChainID = SimAppChainID
6873
config.BlockMaxGas = SimBlockMaxGas
69-
simsx.RunWithSeed(
74+
simsx.RunWithSeedAndRandAcc(
7075
t,
7176
config,
72-
app.NewEthermintApp,
77+
NewSimApp,
7378
setupStateFactory,
7479
config.Seed,
7580
config.FuzzSeed,
76-
simtypes.RandomAccounts,
81+
testutil.RandomAccounts,
7782
)
7883
}
7984

@@ -82,15 +87,54 @@ var (
8287
exportWithValidatorSet []string
8388
)
8489

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

104148
t.Log("importing genesis...\n")
105-
newTestInstance := simsx.NewSimulationAppInstance(t, ti.Cfg, app.NewEthermintApp)
149+
newTestInstance := simsx.NewSimulationAppInstance(t, ti.Cfg, NewSimApp)
106150
newApp := newTestInstance.App
107151
var genesisState map[string]json.RawMessage
108152
require.NoError(t, json.Unmarshal(exported.AppState, &genesisState))
109-
ctxB := newApp.NewContextLegacy(true, cmtproto.Header{Height: a.LastBlockHeight()})
153+
ctxB := newApp.NewContextLegacy(true, cmtproto.Header{
154+
Height: a.LastBlockHeight(),
155+
ChainID: config.ChainID,
156+
})
110157
_, err = newApp.ModuleManager.InitGenesis(ctxB, genesisState)
111158
if simapp.IsEmptyValidatorSetErr(err) {
112159
t.Skip("Skipping simulation as all validators have been unbonded")
@@ -135,10 +182,10 @@ func TestAppSimulationAfterImport(t *testing.T) {
135182
config := simcli.NewConfigFromFlags()
136183
config.ChainID = SimAppChainID
137184
config.BlockMaxGas = SimBlockMaxGas
138-
simsx.RunWithSeed(
185+
simsx.RunWithSeedAndRandAcc(
139186
t,
140187
config,
141-
app.NewEthermintApp,
188+
NewSimApp,
142189
setupStateFactory,
143190
config.Seed,
144191
config.FuzzSeed,
@@ -158,7 +205,7 @@ func TestAppSimulationAfterImport(t *testing.T) {
158205

159206
_, err = a.InitChain(&abci.InitChainRequest{
160207
AppStateBytes: exported.AppState,
161-
ChainId: simsx.SimAppChainID,
208+
ChainId: config.ChainID,
162209
InitialHeight: exported.Height,
163210
Time: genesisTimestamp,
164211
})
@@ -170,18 +217,17 @@ func TestAppSimulationAfterImport(t *testing.T) {
170217
// use accounts from initial run
171218
return exported.AppState, accs, config.ChainID, genesisTimestamp
172219
},
173-
BlockedAddr: a.BlockedAddrs(),
220+
BlockedAddr: a.BlockedAddresses(),
174221
AccountSource: a.AuthKeeper,
175222
BalanceSource: a.BankKeeper,
176223
}
177224
}
178225
ti.Cfg.InitialBlockHeight = int(exported.Height)
179-
simsx.RunWithSeed(t, ti.Cfg, app.NewEthermintApp, importGenesisStateFactory, ti.Cfg.Seed, ti.Cfg.FuzzSeed, testutil.RandomAccounts)
226+
simsx.RunWithSeedAndRandAcc(t, ti.Cfg, NewSimApp, importGenesisStateFactory, ti.Cfg.Seed, ti.Cfg.FuzzSeed, testutil.RandomAccounts)
180227
})
181228
}
182229

183230
func TestAppStateDeterminism(t *testing.T) {
184-
return
185231
const numTimesToRunPerSeed = 3
186232
var seeds []int64
187233
if s := simcli.NewConfigFromFlags().Seed; s != simcli.DefaultSeedValue {
@@ -214,7 +260,7 @@ func TestAppStateDeterminism(t *testing.T) {
214260
return others.Get(k)
215261
})
216262
}
217-
return app.NewEthermintApp(logger, db, nil, true, appOpts, append(baseAppOptions, interBlockCacheOpt())...)
263+
return NewSimApp(logger, db, nil, true, appOpts, append(baseAppOptions, interBlockCacheOpt())...)
218264
}
219265
var mx sync.Mutex
220266
appHashResults := make(map[int64][][]byte)
@@ -245,13 +291,23 @@ func TestAppStateDeterminism(t *testing.T) {
245291
}
246292
}
247293
// run simulations
248-
simsx.RunWithSeeds(
249-
t,
250-
interBlockCachingAppFactory,
251-
setupStateFactory,
252-
seeds,
253-
[]byte{},
254-
testutil.RandomAccounts,
255-
captureAndCheckHash,
256-
)
294+
cfg := simcli.NewConfigFromFlags()
295+
cfg.ChainID = SimAppChainID
296+
for i := range seeds {
297+
seed := seeds[i]
298+
t.Run(fmt.Sprintf("seed: %d", seed), func(t *testing.T) {
299+
t.Parallel()
300+
simsx.RunWithSeedAndRandAcc(
301+
t,
302+
cfg,
303+
interBlockCachingAppFactory,
304+
setupStateFactory,
305+
seed,
306+
[]byte{},
307+
testutil.RandomAccounts,
308+
captureAndCheckHash,
309+
)
310+
})
311+
}
312+
257313
}

0 commit comments

Comments
 (0)