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 all 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
}
102 changes: 102 additions & 0 deletions cmd/config/init/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
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> <denom>",
Short: "Initialize a rollapp configuration on your local machine",
Run: func(cmd *cobra.Command, args []string) {
chainId := args[0]
denom := args[1]
rollappBinaryPath := getRollappBinaryPath(cmd.Flag(flagNames.RollappBinary).Value.String())
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(2),
}
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("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 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())
}
13 changes: 5 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/dymensionxyz/roller
go 1.18

require (
github.com/dymensionxyz/dymension v0.2.0-beta
github.com/gogo/protobuf v1.3.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/cobra v1.7.0
Expand All @@ -27,8 +26,6 @@ require (
github.com/cosmos/btcutil v1.0.4 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/gorocksdb v1.2.0 // indirect
github.com/cosmos/iavl v0.19.3 // indirect
github.com/cosmos/ibc-go/v3 v3.4.0 // indirect
github.com/cosmos/ledger-cosmos-go v0.11.1 // indirect
github.com/cosmos/ledger-go v0.9.2 // indirect
github.com/danieljoos/wincred v1.0.2 // indirect
Expand All @@ -46,9 +43,7 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/google/btree v1.0.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
github.com/gtank/merlin v0.1.1 // indirect
Expand Down Expand Up @@ -76,8 +71,6 @@ require (
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.34.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/regen-network/cosmos-proto v0.3.1 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
Expand All @@ -91,6 +84,10 @@ require (
github.com/tendermint/go-amino v0.16.0 // indirect
github.com/tendermint/tendermint v0.34.22 // indirect
github.com/tendermint/tm-db v0.6.7 // indirect
github.com/tidwall/gjson v1.14.2 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
github.com/zondax/hid v0.9.0 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
Expand Down
Loading