Skip to content

Commit

Permalink
make use of sim utils from sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Jul 31, 2023
1 parent b3c5fbf commit b5f8006
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 45 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ jobs:

test-sim-nondeterminism:
runs-on: ubuntu-latest
timeout-minutes: 25
timeout-minutes: 120
steps:
- uses: actions/setup-go@v3
with:
Expand All @@ -153,7 +153,7 @@ jobs:

test-sim-random-genesis-fast:
runs-on: ubuntu-latest
timeout-minutes: 25
timeout-minutes: 120
steps:
- uses: actions/setup-go@v3
with:
Expand All @@ -173,7 +173,7 @@ jobs:

test-sim-import-export:
runs-on: ubuntu-latest
timeout-minutes: 25
timeout-minutes: 120
steps:
- uses: actions/setup-go@v3
with:
Expand All @@ -193,7 +193,7 @@ jobs:

test-sim-after-import:
runs-on: ubuntu-latest
timeout-minutes: 25
timeout-minutes: 120
steps:
- uses: actions/setup-go@v3
with:
Expand Down
65 changes: 24 additions & 41 deletions app/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,50 +153,33 @@ func RandomAccounts(r *rand.Rand, n int) []simtypes.Account {
// StateFn returns the initial application state using a genesis or the simulation parameters.
// It is a wrapper of simapp.AppStateFn to replace evm param EvmDenom with staking param BondDenom.
func StateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simtypes.AppStateFn {
return func(r *rand.Rand, accs []simtypes.Account, config simtypes.Config,
) (appState json.RawMessage, simAccs []simtypes.Account, chainID string, genesisTimestamp time.Time) {
appStateFn := simapp.AppStateFn(cdc, simManager)
appState, simAccs, chainID, genesisTimestamp = appStateFn(r, accs, config)

rawState := make(map[string]json.RawMessage)
err := json.Unmarshal(appState, &rawState)
if err != nil {
panic(err)
}

stakingStateBz, ok := rawState[stakingtypes.ModuleName]
if !ok {
panic("staking genesis state is missing")
}

stakingState := new(stakingtypes.GenesisState)
cdc.MustUnmarshalJSON(stakingStateBz, stakingState)

// we should get the BondDenom and make it the evmdenom.
// thus simulation accounts could have positive amount of gas token.
bondDenom := stakingState.Params.BondDenom

evmStateBz, ok := rawState[evmtypes.ModuleName]
if !ok {
panic("evm genesis state is missing")
}

evmState := new(evmtypes.GenesisState)
cdc.MustUnmarshalJSON(evmStateBz, evmState)
var bondDenom string
return simapp.AppStateFnWithExtendedCbs(
cdc,
simManager,
NewDefaultGenesisState(),
func(moduleName string, genesisState interface{}) {
if moduleName == stakingtypes.ModuleName {
stakingState := genesisState.(*stakingtypes.GenesisState)
bondDenom = stakingState.Params.BondDenom
}
},
func(rawState map[string]json.RawMessage) {
evmStateBz, ok := rawState[evmtypes.ModuleName]
if !ok {
panic("evm genesis state is missing")
}

// we should replace the EvmDenom with BondDenom
evmState.Params.EvmDenom = bondDenom
evmState := new(evmtypes.GenesisState)
cdc.MustUnmarshalJSON(evmStateBz, evmState)

// change appState back
rawState[evmtypes.ModuleName] = cdc.MustMarshalJSON(evmState)
// we should replace the EvmDenom with BondDenom
evmState.Params.EvmDenom = bondDenom

// replace appstate
appState, err = json.Marshal(rawState)
if err != nil {
panic(err)
}
return appState, simAccs, chainID, genesisTimestamp
}
// change appState back
rawState[evmtypes.ModuleName] = cdc.MustMarshalJSON(evmState)
},
)
}

// NewTestGenesisState generate genesis state with single validator
Expand Down

0 comments on commit b5f8006

Please sign in to comment.