Skip to content

Commit

Permalink
use map instead of CustomGenesisDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
anhductn2001 committed Apr 23, 2024
1 parent 1905fc3 commit 200895e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 36 deletions.
10 changes: 2 additions & 8 deletions cmd/rollappd/cmd/genaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"

"github.com/spf13/cobra"
tmjson "github.com/tendermint/tendermint/libs/json"
tmos "github.com/tendermint/tendermint/libs/os"

"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -194,13 +193,8 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
}
appState[banktypes.ModuleName] = bankGenStateBz

appStateJSON, err := json.Marshal(appState)
if err != nil {
return fmt.Errorf("failed to marshal application genesis state: %w", err)
}

genDoc.AppState = appStateJSON
genDocBytes, err := tmjson.MarshalIndent(genDoc, "", " ")
genDoc["app_state"] = appState
genDocBytes, err := json.MarshalIndent(genDoc, "", " ")
if err != nil {
return err
}
Expand Down
43 changes: 15 additions & 28 deletions cmd/rollappd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io"
"os"
"time"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/config"
Expand All @@ -28,13 +27,9 @@ import (
"github.com/spf13/cast"
"github.com/spf13/cobra"
tmcfg "github.com/tendermint/tendermint/config"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
tmcli "github.com/tendermint/tendermint/libs/cli"
tmjson "github.com/tendermint/tendermint/libs/json"
tmlog "github.com/tendermint/tendermint/libs/log"
tmos "github.com/tendermint/tendermint/libs/os"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtypes "github.com/tendermint/tendermint/types"
dbm "github.com/tendermint/tm-db"

rdkserver "github.com/dymensionxyz/dymension-rdk/server"
Expand All @@ -58,17 +53,6 @@ const rollappAscii = `
███████ ████ ██ ██ ██ ██ ██████ ███████ ███████ ██ ██ ██ ██
`

type CustomGenesisDoc struct {
GenesisTime time.Time `json:"genesis_time"`
ChainID string `json:"chain_id"`
Bech32Prefix string `json:"bech32_prefix"`
InitialHeight int64 `json:"initial_height"`
ConsensusParams *tmproto.ConsensusParams `json:"consensus_params,omitempty"`
Validators []tmtypes.GenesisValidator `json:"validators,omitempty"`
AppHash tmbytes.HexBytes `json:"app_hash"`
AppState json.RawMessage `json:"app_state,omitempty"`
}

// NewRootCmd creates a new root rollappd command. It is called once in the main function.
func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
encodingConfig := app.MakeEncodingConfig()
Expand Down Expand Up @@ -134,7 +118,7 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
genFile := cfg.GenesisFile()
if tmos.FileExists(genFile) {
genDoc, _ := GenesisDocFromFile(genFile)
rdk_utils.SetPrefixes(sdkconfig, genDoc.Bech32Prefix)
rdk_utils.SetPrefixes(sdkconfig, genDoc["bech32_prefix"].(string))
} else {
rdk_utils.SetPrefixes(sdkconfig, "ethm")
}
Expand Down Expand Up @@ -204,9 +188,9 @@ func initRootCmd(
fmt.Println("Failed to read genesis doc from file", err)
}

genDoc.Bech32Prefix = prefix
genDoc["bech32_prefix"] = prefix

genDocBytes, err := tmjson.MarshalIndent(genDoc, "", " ")
genDocBytes, err := json.MarshalIndent(genDoc, "", " ")
if err != nil {
return err
}
Expand Down Expand Up @@ -365,7 +349,7 @@ func (ac appCreator) appExport(
// for the application.
//
// NOTE: The pubkey input is this machines pubkey.
func GenesisStateFromGenFile(genFile string) (genesisState map[string]json.RawMessage, genDoc *CustomGenesisDoc, err error) {
func GenesisStateFromGenFile(genFile string) (genesisState map[string]json.RawMessage, genDoc map[string]interface{}, err error) {
if !tmos.FileExists(genFile) {
return genesisState, genDoc,
fmt.Errorf("%s does not exist, run `init` first", genFile)
Expand All @@ -376,33 +360,36 @@ func GenesisStateFromGenFile(genFile string) (genesisState map[string]json.RawMe
return genesisState, genDoc, err
}

if err = json.Unmarshal(genDoc.AppState, &genesisState); err != nil {
bz, err := json.Marshal(genDoc["app_state"])
if err != nil {
return genesisState, genDoc, err
}

err = json.Unmarshal(bz, &genesisState)
return genesisState, genDoc, err
}

// GenesisDocFromFile reads JSON data from a file and unmarshalls it into a GenesisDoc.
func GenesisDocFromFile(genDocFile string) (*CustomGenesisDoc, error) {
func GenesisDocFromFile(genDocFile string) (map[string]interface{}, error) {
jsonBlob, err := os.ReadFile(genDocFile)
if err != nil {
return nil, fmt.Errorf("couldn't read CustomGenesisDoc file: %w", err)
return nil, fmt.Errorf("couldn't read GenesisDoc file: %w", err)
}

genDoc, err := GenesisDocFromJSON(jsonBlob)
if err != nil {
return nil, fmt.Errorf("error reading CustomGenesisDoc at %s: %w", genDocFile, err)
return nil, fmt.Errorf("error reading GenesisDoc at %s: %w", genDocFile, err)
}
return genDoc, nil
}

// GenesisDocFromJSON unmarshalls JSON data into a GenesisDoc.
func GenesisDocFromJSON(jsonBlob []byte) (*CustomGenesisDoc, error) {
genDoc := CustomGenesisDoc{}
err := tmjson.Unmarshal(jsonBlob, &genDoc)
func GenesisDocFromJSON(jsonBlob []byte) (map[string]interface{}, error) {
genDoc := make(map[string]interface{})
err := json.Unmarshal(jsonBlob, &genDoc)
if err != nil {
return nil, err
}

return &genDoc, err
return genDoc, err
}

0 comments on commit 200895e

Please sign in to comment.