Skip to content

Commit

Permalink
fix(simulation): unmarshal the genesis.json file using the AppGenesis…
Browse files Browse the repository at this point in the history
…FromReader function
  • Loading branch information
chenqunl committed Oct 1, 2023
1 parent 9dc8a55 commit 069d82e
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions testutil/sims/state_helpers.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package sims

import (
"bufio"
"encoding/json"
"fmt"
cbftjson "github.com/cometbft/cometbft/libs/json"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"io"
"math/rand"
"os"
"path/filepath"
"time"

"github.com/cosmos/gogoproto/proto"
Expand Down Expand Up @@ -252,23 +253,23 @@ func AppStateRandomizedFn(
// AppStateFromGenesisFileFn util function to generate the genesis AppState
// from a genesis.json file.
func AppStateFromGenesisFileFn(r io.Reader, cdc codec.JSONCodec, genesisFile string) (genutiltypes.AppGenesis, []simtypes.Account, error) {
bytes, err := os.ReadFile(genesisFile)
file, err := os.Open(filepath.Clean(genesisFile))
if err != nil {
panic(err)
}

var genesis genutiltypes.AppGenesis
if err = json.Unmarshal(bytes, &genesis); err != nil {
// NOTE: cometbft uses a custom JSON decoder for GenesisDoc
// On some machines, the above will go wrong, so try using cbftjson for Unmarshal again.
if err = cbftjson.Unmarshal(bytes, &genesis); err != nil {
return genesis, nil, err
}
genesis, err := genutiltypes.AppGenesisFromReader(bufio.NewReader(file))
if err != nil {
return *genesis, nil, err
}

if err := file.Close(); err != nil {
return *genesis, nil, err
}

var appState map[string]json.RawMessage
if err = json.Unmarshal(genesis.AppState, &appState); err != nil {
return genesis, nil, err
return *genesis, nil, err
}

var authGenesis authtypes.GenesisState
Expand All @@ -290,13 +291,13 @@ func AppStateFromGenesisFileFn(r io.Reader, cdc codec.JSONCodec, genesisFile str

a, ok := acc.GetCachedValue().(sdk.AccountI)
if !ok {
return genesis, nil, fmt.Errorf("expected account")
return *genesis, nil, fmt.Errorf("expected account")
}

// create simulator accounts
simAcc := simtypes.Account{PrivKey: privKey, PubKey: privKey.PubKey(), Address: a.GetAddress(), ConsKey: ed25519.GenPrivKeyFromSecret(privkeySeed)}
newAccs[i] = simAcc
}

return genesis, newAccs, nil
return *genesis, newAccs, nil
}

0 comments on commit 069d82e

Please sign in to comment.