Skip to content

Commit

Permalink
Initialize and config the rollapp configuration from the init stage
Browse files Browse the repository at this point in the history
  • Loading branch information
ItayLevyOfficial committed May 29, 2023
1 parent de1e2c0 commit abf1344
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 125 deletions.
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
}
29 changes: 29 additions & 0 deletions cmd/config/init/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package init

import (
"os"

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

func setRollappAppConfig(appConfigFilePath string, denom string) {
config, _ := toml.LoadFile(appConfigFilePath)
config.Set("minimum-gas-prices", "0"+denom)
config.Set("api.enable", "true")
config.Set("api.address", "tcp://0.0.0.0:1417")
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 setRollappTendermintConfig(tendermintConfigFilePath string) {
config, _ := toml.LoadFile(tendermintConfigFilePath)
config.Set("rpc.laddr", "tcp://0.0.0.0:26657")
config.Set("p2p.laddr", "tcp://0.0.0.0:26657")
config.Set("persistent_peers", "")
file, _ := os.Create(tendermintConfigFilePath)
file.WriteString(config.String())
file.Close()
}
84 changes: 84 additions & 0 deletions cmd/config/init/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package init

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

"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",
}

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

const evmCoinType uint32 = 60
const rollappConfigDir string = ".rollapp"
const relayerConfigDir string = ".relayer"
const hubChainId string = "internal-devnet"
const relayerKeysDirName string = "keys"
const cosmosDefaultCointype uint32 = 118
const sequencerKeyName string = "rollapp_sequencer"

func getDenom(denom string, chainId string) string {
if denom == "" {
return "u" + chainId[:3]
}
return denom
}

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

func initializeRollappConfig(rollappExecutablePath string, chainId string, denom string) {
initRollappCmd := exec.Command(rollappExecutablePath, "init", hubSequencerKeyName, "--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)
setRollappTendermintConfig(filepath.Join(os.Getenv("HOME"), rollappConfigDir, "config/config.toml"))
}

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)
generateKeys(cmd.Flags().Changed(lightNodeEndpointFlag), chainId)
initializeRollappConfig(rollappBinaryPath, chainId, denom)
},
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().Int64P("decimals", "", 18, "The number of decimal places a rollapp token supports.")
return cmd
}
46 changes: 46 additions & 0 deletions cmd/config/init/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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) {
const evmCoinType uint32 = 60
createKey(rollappConfigDir, hubSequencerKeyName)
createKey(rollappConfigDir, sequencerKeyName, evmCoinType)
relayerRollappDir := path.Join(relayerConfigDir, relayerKeysDirName, chainId)
relayerHubDir := path.Join(relayerConfigDir, relayerKeysDirName, hubChainId)
createKey(relayerHubDir, "relayer-hub-key")
createKey(relayerRollappDir, "relayer-rollapp-key", 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())
}
9 changes: 1 addition & 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 Down
Loading

0 comments on commit abf1344

Please sign in to comment.