Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added configuration files generation ability to roller init #8

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
*
!*/
!*.*
*.exe
*.exe
/.vscode
14 changes: 0 additions & 14 deletions cmd/config.go

This file was deleted.

15 changes: 15 additions & 0 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package config

import (
configInit "github.com/dymensionxyz/roller/cmd/config/init"
"github.com/spf13/cobra"
)

func ConfigCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Commands for setting up and managing rollapp configuration files.",
}
cmd.AddCommand(configInit.InitCmd())
return cmd
}
69 changes: 69 additions & 0 deletions cmd/config/init/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package init

import (
"io/ioutil"
"os"

"fmt"
"os/exec"
"path/filepath"

"github.com/tidwall/sjson"
)

func initializeRollappGenesis(rollappExecutablePath string, decimals uint64, denom string) error {
zeros := decimals + 9
tokenAmount := "1" + fmt.Sprintf("%0*d", zeros, 0) + denom
rollappConfigDirPath := filepath.Join(os.Getenv("HOME"), rollappConfigDir)
genesisSequencerAccountCmd := exec.Command(rollappExecutablePath, "add-genesis-account", keyNames.RollappSequencer, tokenAmount, "--keyring-backend", "test", "--home", rollappConfigDirPath)
err := genesisSequencerAccountCmd.Run()
if err != nil {
return err
}
err = updateGenesisParams(filepath.Join(rollappConfigDirPath, "config", "genesis.json"), denom)
if err != nil {
return err
}
return nil
}

type pathValue struct {
Path string
Value interface{}
}

func getDefaultGenesisParams(denom string) []pathValue {
return []pathValue{
{"app_state.mint.params.mint_denom", denom},
{"app_state.staking.params.bond_denom", denom},
{"app_state.crisis.constant_fee.denom", denom},
{"app_state.evm.params.evm_denom", denom},
{"app_state.gov.deposit_params.min_deposit.0.denom", denom},
{"consensus_params.block.max_gas", "40000000"},
{"app_state.feemarket.params.no_base_fee", true},
{"app_state.mint.params.blocks_per_year", "157680000"},
{"app_state.distribution.params.base_proposer_reward", "0.8"},
{"app_state.distribution.params.community_tax", "0.00002"},
{"app_state.gov.voting_params.voting_period", "300s"},
{"app_state.staking.params.unbonding_time", "3628800s"},
}
}

func updateGenesisParams(genesisFilePath string, denom string) error {
genesisFileContent, err := ioutil.ReadFile(genesisFilePath)
if err != nil {
return err
}
genesisFileContentString := string(genesisFileContent)
for _, param := range getDefaultGenesisParams(denom) {
genesisFileContentString, err = sjson.Set(genesisFileContentString, param.Path, param.Value)
if err != nil {
return err
}
}
err = ioutil.WriteFile(genesisFilePath, []byte(genesisFileContentString), 0644)
if err != nil {
return err
}
return nil
}
110 changes: 110 additions & 0 deletions cmd/config/init/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package init

import (
"os"
"os/exec"
"path/filepath"

toml "github.com/pelletier/go-toml"
"github.com/spf13/cobra"
)

var flagNames = struct {
LightNodeEndpoint string
Denom string
KeyPrefix string
Decimals string
RollappBinary string
HubRPC string
}{
LightNodeEndpoint: "light-node-endpoint",
Denom: "denom",
KeyPrefix: "key-prefix",
Decimals: "decimals",
RollappBinary: "rollapp-binary",
HubRPC: "hub-rpc",
}

var keyNames = struct {
HubSequencer string
RollappSequencer string
RollappRelayer string
}{
HubSequencer: "hub_sequencer",
RollappSequencer: "rollapp_sequencer",
RollappRelayer: "relayer-rollapp-key",
}

const hubRPC = "https://rpc-hub-35c.dymension.xyz:443"
const lightNodeEndpointFlag = "light-node-endpoint"

const evmCoinType uint32 = 60
const rollappConfigDir = ".rollapp"
const relayerConfigDir = ".relayer"
const hubChainId = "35-C"
const relayerKeysDirName = "keys"
const cosmosDefaultCointype uint32 = 118

func InitCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "init <chain-id>",
Short: "Initialize a rollapp configuration on your local machine",
Run: func(cmd *cobra.Command, args []string) {
chainId := args[0]
rollappBinaryPath := getRollappBinaryPath(cmd.Flag(flagNames.RollappBinary).Value.String())
denom := getDenom(cmd.Flag(flagNames.Denom).Value.String(), chainId)
decimals, err := cmd.Flags().GetUint64(flagNames.Decimals)
if err != nil {
panic(err)
}
generateKeys(cmd.Flags().Changed(lightNodeEndpointFlag), chainId)
initializeRollappConfig(rollappBinaryPath, chainId, denom)
err = initializeRollappGenesis(rollappBinaryPath, decimals, denom)
if err != nil {
panic(err)
}
},
Args: cobra.ExactArgs(1),
}
cmd.Flags().StringP(flagNames.HubRPC, "", hubRPC, "Dymension Hub rpc endpoint")
cmd.Flags().StringP(flagNames.LightNodeEndpoint, "", "", "The data availability light node endpoint. Runs an Arabica Celestia light node if not provided.")
cmd.Flags().StringP("denom", "", "", "The rollapp token smallest denominator, for example `wei` in Ethereum.")
cmd.Flags().StringP("key-prefix", "", "", "The `bech32` prefix of the rollapp keys.")
cmd.Flags().StringP("rollapp-binary", "", "", "The rollapp binary. Should be passed only if you built a custom rollapp.")
cmd.Flags().Uint64P(flagNames.Decimals, "", 18, "The number of decimal places a rollapp token supports.")
return cmd
}

func getDenom(denom string, chainId string) string {
if denom == "" {
return "a" + chainId[:3]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to have some default value in the flag
not sure "a" + chainId[:3] make much sense as the denom default

}
return denom
}

func getRollappBinaryPath(rollappBinaryPath string) string {
if rollappBinaryPath == "" {
rollappBinaryPath = "/usr/local/bin/rollapp_evm"
}
return rollappBinaryPath
}

func setRollappAppConfig(appConfigFilePath string, denom string) {
config, _ := toml.LoadFile(appConfigFilePath)
config.Set("minimum-gas-prices", "0"+denom)
config.Set("api.enable", "true")
config.Set("grpc.address", "0.0.0.0:8080")
config.Set("grpc-web.address", "0.0.0.0:8081")
file, _ := os.Create(appConfigFilePath)
file.WriteString(config.String())
file.Close()
}

func initializeRollappConfig(rollappExecutablePath string, chainId string, denom string) {
initRollappCmd := exec.Command(rollappExecutablePath, "init", keyNames.HubSequencer, "--chain-id", chainId, "--home", filepath.Join(os.Getenv("HOME"), rollappConfigDir))
err := initRollappCmd.Run()
if err != nil {
panic(err)
}
setRollappAppConfig(filepath.Join(os.Getenv("HOME"), rollappConfigDir, "config/app.toml"), denom)
}
45 changes: 45 additions & 0 deletions cmd/config/init/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package init

import (
"os"
"path"
"path/filepath"

"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
)

func createKey(relativePath string, keyId string, coinType ...uint32) (keyring.Info, error) {
var coinTypeVal = cosmosDefaultCointype
if len(coinType) != 0 {
coinTypeVal = coinType[0]
}
rollappAppName := "rollapp"
kr, err := keyring.New(
rollappAppName,
keyring.BackendTest,
filepath.Join(os.Getenv("HOME"), relativePath),
nil,
)
if err != nil {
return nil, err
}
bip44Params := hd.NewFundraiserParams(0, coinTypeVal, 0)
info, _, err := kr.NewMnemonic(keyId, keyring.English, bip44Params.String(), "", hd.Secp256k1)
if err != nil {
return nil, err
}
return info, nil
}

func generateKeys(createLightNode bool, chainId string) {
createKey(rollappConfigDir, keyNames.HubSequencer)
createKey(rollappConfigDir, keyNames.RollappSequencer, evmCoinType)
relayerRollappDir := path.Join(relayerConfigDir, relayerKeysDirName, chainId)
relayerHubDir := path.Join(relayerConfigDir, relayerKeysDirName, hubChainId)
createKey(relayerHubDir, "relayer-hub-key")
createKey(relayerRollappDir, keyNames.RollappRelayer, evmCoinType)
if createLightNode {
createKey(".light_node", "my-celes-key")
}
}
74 changes: 0 additions & 74 deletions cmd/init.go

This file was deleted.

2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"os"

"github.com/dymensionxyz/roller/cmd/config"
"github.com/spf13/cobra"
)

Expand All @@ -24,4 +25,5 @@ func Execute() {
}

func init() {
rootCmd.AddCommand(config.ConfigCmd())
}
Loading