Skip to content

Commit

Permalink
feat!: configure bech32prefix in genesis (#170)
Browse files Browse the repository at this point in the history
Co-authored-by: Trinity <[email protected]>
Co-authored-by: Omri <[email protected]>
  • Loading branch information
3 people authored Apr 25, 2024
1 parent f58feaa commit 7855699
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export EXECUTABLE="rollapp-evm"
export ROLLAPP_CHAIN_ID="rollappevm_1234-1"
export KEY_NAME_ROLLAPP="rol-user"
export BASE_DENOM="arax"
export BECH32="rol"
export DENOM=$(echo "$BASE_DENOM" | sed 's/^.//')
export MONIKER="$ROLLAPP_CHAIN_ID-sequencer"

Expand Down
4 changes: 2 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ import (
)

const (
AccountAddressPrefix = "ethm"
Name = "rollapp_evm"
Name = "rollapp_evm"
)

var (
Expand Down Expand Up @@ -546,6 +545,7 @@ func NewRollapp(
appCodec,
keys[denommetadatamoduletypes.StoreKey],
app.BankKeeper,
app.TransferKeeper,
denomMetadataHooks,
app.GetSubspace(denommetadatamoduletypes.ModuleName),
)
Expand Down
14 changes: 6 additions & 8 deletions cmd/rollappd/cmd/genaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"

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

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
Expand All @@ -16,8 +17,6 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
authvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"

ethtutil "github.com/ethereum/go-ethereum/common"
"github.com/evmos/evmos/v12/crypto/hd"
Expand Down Expand Up @@ -136,7 +135,7 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
}

genFile := config.GenesisFile()
appState, genDoc, err := genutiltypes.GenesisStateFromGenFile(genFile)
appState, genDoc, err := GenesisStateFromGenFile(genFile)
if err != nil {
return fmt.Errorf("failed to unmarshal genesis state: %w", err)
}
Expand Down Expand Up @@ -194,13 +193,12 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
}
appState[banktypes.ModuleName] = bankGenStateBz

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

genDoc.AppState = appStateJSON
return genutil.ExportGenesisFile(genDoc, genFile)
return tmos.WriteFile(genFile, genDocBytes, 0o644)
},
}

Expand Down
100 changes: 92 additions & 8 deletions cmd/rollappd/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -28,6 +29,7 @@ import (
tmcfg "github.com/tendermint/tendermint/config"
tmcli "github.com/tendermint/tendermint/libs/cli"
tmlog "github.com/tendermint/tendermint/libs/log"
tmos "github.com/tendermint/tendermint/libs/os"
dbm "github.com/tendermint/tm-db"

rdkserver "github.com/dymensionxyz/dymension-rdk/server"
Expand All @@ -39,6 +41,7 @@ import (

ethermintclient "github.com/evmos/evmos/v12/client"

rdk_genutilcli "github.com/dymensionxyz/dymension-rdk/x/genutil/client/cli"
evmserver "github.com/evmos/evmos/v12/server"
evmconfig "github.com/evmos/evmos/v12/server/config"
)
Expand Down Expand Up @@ -109,6 +112,18 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
//create Block Explorer Json-RPC toml config file
berpcconfig.EnsureRoot(home, berpcconfig.DefaultBeJsonRpcConfig())

// Set config
sdkconfig := sdk.GetConfig()
utils.SetBip44CoinType(sdkconfig)
cfg := serverCtx.Config
genFile := cfg.GenesisFile()
if tmos.FileExists(genFile) {
genDoc, _ := GenesisDocFromFile(genFile)
rdk_utils.SetPrefixes(sdkconfig, genDoc["bech32_prefix"].(string))
} else {
rdk_utils.SetPrefixes(sdkconfig, "ethm")
}
sdkconfig.Seal()
return nil
},
}
Expand Down Expand Up @@ -155,18 +170,38 @@ func initRootCmd(
rootCmd *cobra.Command,
encodingConfig params.EncodingConfig,
) {
// Set config
sdkconfig := sdk.GetConfig()
rdk_utils.SetPrefixes(sdkconfig, app.AccountAddressPrefix)
utils.SetBip44CoinType(sdkconfig)
sdkconfig.Seal()

ac := appCreator{
encCfg: encodingConfig,
}

initCmd := genutilcli.InitCmd(app.ModuleBasics, app.DefaultNodeHome)
initCmd.Flags().String(FlagBech32Prefix, "ethm", "set bech32 prefix for rollapp, if left blank default value is 'ethm'")

initCmd.PostRunE = func(cmd *cobra.Command, args []string) error {
prefix, _ := initCmd.Flags().GetString(FlagBech32Prefix)

serverCtx := server.GetServerContextFromCmd(cmd)
config := serverCtx.Config
path := config.GenesisFile()

genDoc, err := GenesisDocFromFile(path)
if err != nil {
fmt.Println("Failed to read genesis doc from file", err)
}

genDoc["bech32_prefix"] = prefix

genDocBytes, err := json.MarshalIndent(genDoc, "", " ")
if err != nil {
return err
}
return tmos.WriteFile(path, genDocBytes, 0o644)

}

rootCmd.AddCommand(
genutilcli.InitCmd(app.ModuleBasics, app.DefaultNodeHome),
genutilcli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome),
initCmd,
rdk_genutilcli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome),
genutilcli.MigrateGenesisCmd(),
genutilcli.GenTxCmd(app.ModuleBasics, encodingConfig.TxConfig, banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome),

Expand Down Expand Up @@ -310,3 +345,52 @@ func (ac appCreator) appExport(

return rollapp.ExportAppStateAndValidators(forZeroHeight, jailAllowedAddrs)
}

// GenesisStateFromGenFile creates the core parameters for genesis initialization
// for the application.
//
// NOTE: The pubkey input is this machines pubkey.
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)
}

genDoc, err = GenesisDocFromFile(genFile)
if err != nil {
return genesisState, genDoc, err
}

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) (map[string]interface{}, error) {
jsonBlob, err := os.ReadFile(genDocFile)
if err != nil {
return nil, fmt.Errorf("couldn't read GenesisDoc file: %w", err)
}

genDoc, err := GenesisDocFromJSON(jsonBlob)
if err != nil {
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) (map[string]interface{}, error) {
genDoc := make(map[string]interface{})
err := json.Unmarshal(jsonBlob, &genDoc)
if err != nil {
return nil, err
}

return genDoc, err
}
1 change: 1 addition & 0 deletions cmd/rollappd/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const (
FlagUnsafeSkipUpgrades = "unsafe-skip-upgrades"
FlagTrace = "trace"
FlagInvCheckPeriod = "inv-check-period"
FlagBech32Prefix = "bech32-prefix"

FlagPruning = "pruning"
FlagPruningKeepRecent = "pruning-keep-recent"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/bcdevtools/evm-block-explorer-rpc-cosmos v1.1.0
github.com/cosmos/cosmos-sdk v0.46.16
github.com/cosmos/ibc-go/v6 v6.2.1
github.com/dymensionxyz/dymension-rdk v1.5.0-beta
github.com/dymensionxyz/dymension-rdk v1.5.0-beta.0.20240423161402-f6997e0d1a5c
github.com/dymensionxyz/dymint v1.0.1-alpha.0.20240414124654-eb08e30da2c5
github.com/ethereum/go-ethereum v1.12.0
github.com/evmos/evmos/v12 v12.1.6
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,8 @@ github.com/dvsekhvalnov/jose2go v1.5.0 h1:3j8ya4Z4kMCwT5nXIKFSV84YS+HdqSSO0VsTQx
github.com/dvsekhvalnov/jose2go v1.5.0/go.mod h1:QsHjhyTlD/lAVqn/NSbVZmSCGeDehTB/mPZadG+mhXU=
github.com/dymensionxyz/cosmosclient v0.4.2-beta h1:sokBefcN1tIOlUKmB8Q2E9XMJ93LueqtFThiM/kA4DI=
github.com/dymensionxyz/cosmosclient v0.4.2-beta/go.mod h1:GQQu3ITEjWfi5ULR2B6X2i2YZNennY1yzcT5qdL4MGI=
github.com/dymensionxyz/dymension-rdk v1.5.0-beta h1:whKxcgcXB3l7rK7F0Vnq0EF0Ri9475CSZTseaUQN6o0=
github.com/dymensionxyz/dymension-rdk v1.5.0-beta/go.mod h1:kzNFKt3yfFzZ2u3+47Nlpobub3/UXsICOg1WmGud3DE=
github.com/dymensionxyz/dymension-rdk v1.5.0-beta.0.20240423161402-f6997e0d1a5c h1:ee4pw3mkuYLQzwhTbiwD5msPvwoDvuCHrEMSBrZMcPc=
github.com/dymensionxyz/dymension-rdk v1.5.0-beta.0.20240423161402-f6997e0d1a5c/go.mod h1:kzNFKt3yfFzZ2u3+47Nlpobub3/UXsICOg1WmGud3DE=
github.com/dymensionxyz/dymension/v3 v3.1.0-rc03.0.20240411195658-f7cd96f53b56 h1:cmpJYdRviuUfmlJdHrcAND8Jd6JIY4rp63bWAQzPr54=
github.com/dymensionxyz/dymension/v3 v3.1.0-rc03.0.20240411195658-f7cd96f53b56/go.mod h1:3Pfrr8j/BR9ztNKztGfC5PqDiO6CcrzMLCJtFtPEVW4=
github.com/dymensionxyz/dymint v1.0.1-alpha.0.20240414124654-eb08e30da2c5 h1:7UEyfIyW54zJXUL4hSAJdLol7CkJtD0eL865vLTf9Lg=
Expand Down
2 changes: 1 addition & 1 deletion scripts/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ if [ -z "$KEY_NAME_ROLLAPP" ]; then
fi

# ------------------------------- init rollapp ------------------------------- #
"$EXECUTABLE" init "$MONIKER" --chain-id "$ROLLAPP_CHAIN_ID"
"$EXECUTABLE" init "$MONIKER" --chain-id "$ROLLAPP_CHAIN_ID" --bech32-prefix "$BECH32"

# ------------------------------- client config ------------------------------ #
"$EXECUTABLE" config chain-id "$ROLLAPP_CHAIN_ID"
Expand Down

0 comments on commit 7855699

Please sign in to comment.