-
Notifications
You must be signed in to change notification settings - Fork 67
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
ItayLevyOfficial
merged 7 commits into
main
from
itaylevyofficial/7-init-coniguration-generation
May 30, 2023
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
abf1344
Initialize and config the rollapp configuration from the init stage
ItayLevyOfficial 76cb361
Fixed Michael PR comments
ItayLevyOfficial 0df3d84
Fixed Omri's comments
ItayLevyOfficial 1504d22
Removed redundant config.go file
ItayLevyOfficial d9aa8d0
feat: generates EVM genesis file using rollapp init cmd (#10)
ItayLevyOfficial dac083d
Replaced denom flag with a param
ItayLevyOfficial 08cc9fe
Merge branch 'itaylevyofficial/7-init-coniguration-generation' of git…
ItayLevyOfficial File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
* | ||
!*/ | ||
!*.* | ||
*.exe | ||
*.exe | ||
/.vscode |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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