From 95a9bff4263efdf57f5a471022ceee81598e8a98 Mon Sep 17 00:00:00 2001 From: artpav <19916123+artemijspavlovs@users.noreply.github.com> Date: Sun, 11 Aug 2024 11:42:45 +0300 Subject: [PATCH] feat: full node flow (#827) --- cmd/config/export/bech32.go | 2 +- cmd/config/export/export.go | 22 +- cmd/config/init/consts.go | 6 +- cmd/config/init/flags.go | 14 +- cmd/config/init/format.go | 2 +- cmd/config/init/genesis.go | 19 +- cmd/config/init/keys.go | 23 +- cmd/config/init/output.go | 2 +- cmd/config/init/relayer.go | 2 +- cmd/config/init/rollapp.go | 6 +- cmd/config/set/da.go | 17 +- cmd/config/set/hub_rpc.go | 13 +- cmd/config/set/lc_rpc.go | 18 +- cmd/config/set/rollapp_rpc.go | 8 +- cmd/config/set/set.go | 7 +- cmd/config/show/show.go | 7 +- cmd/consts/consts.go | 65 +- cmd/da-light-client/start/start.go | 21 +- cmd/eibc/init.go | 4 +- cmd/eibc/scale.go | 4 +- cmd/eibc/start.go | 6 +- cmd/keys/export/export.go | 24 +- cmd/keys/list/list.go | 19 +- cmd/migrate/migrate.go | 20 +- cmd/migrate/v0_1_11.go | 2 +- cmd/migrate/v0_1_18.go | 8 +- cmd/migrate/v1_0_00.go | 14 +- cmd/migrate/v1_0_05.go | 6 +- cmd/migrate/v_0_1_12.go | 14 +- cmd/migrate/v_0_1_13.go | 2 +- cmd/migrate/v_0_1_4.go | 2 +- cmd/migrate/v_0_1_5.go | 2 +- cmd/migrate/v_0_1_6.go | 2 +- cmd/migrate/v_0_1_8.go | 2 +- cmd/relayer/run/run.go | 17 +- cmd/relayer/start/start.go | 21 +- cmd/relayer/status/status.go | 9 +- cmd/rollapp/init/hubgenesis.go | 4 +- cmd/rollapp/init/main.go | 55 +- cmd/rollapp/init/utils.go | 173 ++---- cmd/rollapp/rollapp.go | 2 + cmd/rollapp/run/run.go | 571 +++++++++++++----- cmd/rollapp/start/start.go | 186 +++--- cmd/rollapp/status/status.go | 7 +- cmd/run/run.go | 35 +- cmd/run/services_info.go | 2 +- cmd/run/services_status.go | 2 +- cmd/run/ui.go | 12 +- cmd/services/load/load.go | 30 +- cmd/tx/claim/claim.go | 5 +- cmd/tx/fund_faucet/fund_faucet.go | 12 +- cmd/utils/balance.go | 5 +- cmd/utils/fetch_accounts_data.go | 2 +- cmd/utils/key_info.go | 6 +- cmd/utils/keys.go | 9 +- cmd/utils/log.go | 11 +- cmd/utils/output.go | 19 +- config/toml.go | 41 -- config/yaml.go | 22 - data_layer/avail/avail.go | 2 +- data_layer/celestia/celestia.go | 37 +- data_layer/da_layer.go | 14 +- data_layer/damock/damock.go | 10 +- go.mod | 9 +- go.sum | 11 +- relayer/channels.go | 6 +- relayer/config.go | 2 +- relayer/connections.go | 8 +- relayer/create_ibc_channel.go | 11 +- relayer/query.go | 2 +- relayer/relayer_manager.go | 4 +- scripts/install-binaries.sh | 11 +- sequencer/config.go | 8 +- sequencer/sequencer_manager.go | 2 +- sequencer/status.go | 33 +- test/config/init/testutils/keys.go | 2 +- test/config/init/testutils/relayer.go | 2 +- {cmd/utils => utils/bash}/bash_commands.go | 28 +- {config => utils/config}/chainid.go | 0 {config => utils/config}/config.go | 51 +- utils/config/tomlconfig/toml.go | 84 +++ utils/dymint/dymint.go | 4 +- .../errorhandling}/error_handling.go | 4 +- utils/rollapp/rollapp.go | 20 +- utils/sequencer/sequencer.go | 62 +- utils/sequencer/types.go | 48 +- utils/service_manager/service.go | 5 +- 87 files changed, 1310 insertions(+), 813 deletions(-) delete mode 100644 config/toml.go delete mode 100644 config/yaml.go rename {cmd/utils => utils/bash}/bash_commands.go (87%) rename {config => utils/config}/chainid.go (100%) rename {config => utils/config}/config.go (75%) create mode 100644 utils/config/tomlconfig/toml.go rename {cmd/utils => utils/errorhandling}/error_handling.go (94%) diff --git a/cmd/config/export/bech32.go b/cmd/config/export/bech32.go index 19e14413..9fcb747a 100644 --- a/cmd/config/export/bech32.go +++ b/cmd/config/export/bech32.go @@ -6,7 +6,7 @@ import ( "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" + "github.com/dymensionxyz/roller/utils/config" ) func getBech32Prefix(rlpCfg config.RollappConfig) (string, error) { diff --git a/cmd/config/export/export.go b/cmd/config/export/export.go index d1631f7a..90603194 100644 --- a/cmd/config/export/export.go +++ b/cmd/config/export/export.go @@ -10,8 +10,10 @@ import ( "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/relayer" + "github.com/dymensionxyz/roller/utils/config" + "github.com/dymensionxyz/roller/utils/config/tomlconfig" + "github.com/dymensionxyz/roller/utils/errorhandling" ) func Cmd() *cobra.Command { @@ -20,10 +22,10 @@ func Cmd() *cobra.Command { Short: "Export the rollapp configurations jsons needed to list your rollapp.", Run: func(cmd *cobra.Command, args []string) { home := cmd.Flag(utils.FlagNames.Home).Value.String() - rlpCfg, err := config.LoadRollerConfigFromTOML(home) - utils.PrettifyErrorIfExists(err) + rlpCfg, err := tomlconfig.LoadRollerConfig(home) + errorhandling.PrettifyErrorIfExists(err) bech32, err := getBech32Prefix(rlpCfg) - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) faucetUrls := map[string]string{ consts.LocalHubID: "", consts.TestnetHubID: "https://discord.com/channels/956961633165529098/1196803789911498763", @@ -32,13 +34,13 @@ func Cmd() *cobra.Command { baseDenom := rlpCfg.Denom coinType := 118 - if rlpCfg.VMType == config.EVM_ROLLAPP { + if rlpCfg.VMType == consts.EVM_ROLLAPP { coinType = 60 } rly := relayer.NewRelayer(rlpCfg.Home, rlpCfg.RollappID, rlpCfg.HubData.ID) _, _, err = rly.LoadActiveChannel() if err != nil || rly.SrcChannel == "" || rly.DstChannel == "" { - utils.PrettifyErrorIfExists( + errorhandling.PrettifyErrorIfExists( errors.New( "failed to export rollapp json." + " Please verify that the rollapp is running on your local machine and a relayer channel has been established", @@ -73,23 +75,23 @@ func Cmd() *cobra.Command { Type: RollApp, Analytics: true, } - if rlpCfg.VMType == config.EVM_ROLLAPP { + if rlpCfg.VMType == consts.EVM_ROLLAPP { evmID := config.GetEthID(rlpCfg.RollappID) hexEvmID, err := decimalToHexStr(evmID) - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) networkJson.Evm = &EvmConfig{ ChainId: hexEvmID, Rpc: "", } } - if rlpCfg.DA == config.Avail { + if rlpCfg.DA == consts.Avail { networkJson.Da = Avail } else { networkJson.Da = Celestia } networkJsonString, err := json.MarshalIndent(networkJson, "", " ") - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) println("💈 networks.json:") println(string(networkJsonString)) }, diff --git a/cmd/config/init/consts.go b/cmd/config/init/consts.go index 3337fa69..37ca32ef 100644 --- a/cmd/config/init/consts.go +++ b/cmd/config/init/consts.go @@ -1,6 +1,8 @@ package initconfig -import "github.com/dymensionxyz/roller/config" +import ( + "github.com/dymensionxyz/roller/cmd/consts" +) var FlagNames = struct { TokenSupply string @@ -30,7 +32,7 @@ const ( ) // TODO(#112): The available hub networks should be read from YAML file -var Hubs = map[string]config.HubData{ +var Hubs = map[string]consts.HubData{ StagingHubName: { API_URL: "https://dymension-devnet.api.silknodes.io:443", ID: "devnet_304-1", diff --git a/cmd/config/init/flags.go b/cmd/config/init/flags.go index 604ee427..4f4f26d9 100644 --- a/cmd/config/init/flags.go +++ b/cmd/config/init/flags.go @@ -6,11 +6,11 @@ import ( "strings" "github.com/BurntSushi/toml" + cmdutils "github.com/dymensionxyz/roller/cmd/utils" + "github.com/dymensionxyz/roller/utils/config" "github.com/spf13/cobra" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" globalutils "github.com/dymensionxyz/roller/utils" ) @@ -31,7 +31,7 @@ func AddFlags(cmd *cobra.Command) error { "The rollapp binary. Should be passed only if you built a custom rollapp", ) cmd.Flags(). - StringP(FlagNames.VMType, "", string(config.EVM_ROLLAPP), "The rollapp type [evm, sdk]. Defaults to evm") + StringP(FlagNames.VMType, "", string(consts.EVM_ROLLAPP), "The rollapp type [evm, sdk]. Defaults to evm") cmd.Flags(). StringP(FlagNames.TokenSupply, "", consts.DefaultTokenSupply, "The total token supply of the RollApp") // cmd.Flags().BoolP(FlagNames.Interactive, "i", false, "Run roller in interactive mode") @@ -64,12 +64,12 @@ func GetInitConfig( ) (*config.RollappConfig, error) { var cfg config.RollappConfig - home, err := globalutils.ExpandHomePath(initCmd.Flag(utils.FlagNames.Home).Value.String()) + home, err := globalutils.ExpandHomePath(initCmd.Flag(cmdutils.FlagNames.Home).Value.String()) if err != nil { fmt.Println("failed to expand home path: ", err) } - rollerConfigFilePath := filepath.Join(home, config.RollerConfigFileName) + rollerConfigFilePath := filepath.Join(home, consts.RollerConfigFileName) if _, err := toml.DecodeFile(rollerConfigFilePath, &cfg); err != nil { return nil, err } @@ -82,7 +82,7 @@ func GetInitConfig( // token supply is provided in the pre-created genesis // cfg.TokenSupply = initCmd.Flag(FlagNames.TokenSupply).Value.String() - cfg.DA = config.DAType(strings.ToLower(string(cfg.DA))) + cfg.DA = consts.DAType(strings.ToLower(string(cfg.DA))) var hubID string @@ -105,7 +105,7 @@ func GetInitConfig( // cfg.RollappID = raID // cfg.Denom = raBaseDenom - if cfg.VMType == config.EVM_ROLLAPP { + if cfg.VMType == consts.EVM_ROLLAPP { cfg.Decimals = 18 } else { cfg.Decimals = 6 diff --git a/cmd/config/init/format.go b/cmd/config/init/format.go index 70ab42b0..1848102f 100644 --- a/cmd/config/init/format.go +++ b/cmd/config/init/format.go @@ -4,11 +4,11 @@ import ( "fmt" "strings" + "github.com/dymensionxyz/roller/utils/config" "github.com/pterm/pterm" "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" datalayer "github.com/dymensionxyz/roller/data_layer" ) diff --git a/cmd/config/init/genesis.go b/cmd/config/init/genesis.go index bcc81c2b..f70ba8ea 100644 --- a/cmd/config/init/genesis.go +++ b/cmd/config/init/genesis.go @@ -5,13 +5,14 @@ import ( "os" "os/exec" "path/filepath" - "strings" "github.com/tidwall/sjson" "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" + "github.com/dymensionxyz/roller/utils/bash" + "github.com/dymensionxyz/roller/utils/config" + "github.com/dymensionxyz/roller/utils/config/tomlconfig" ) // const ( @@ -79,7 +80,7 @@ func UpdateGenesisParams(home string, raCfg *config.RollappConfig) error { if err != nil { return err } - cfg, err := config.LoadRollerConfigFromTOML(home) + cfg, err := tomlconfig.LoadRollerConfig(home) if err != nil { return err } @@ -102,7 +103,7 @@ func UpdateGenesisParams(home string, raCfg *config.RollappConfig) error { "test", ) - _, err = utils.ExecBashCommandWithStdout(addGenAccountCmd) + _, err = bash.ExecCommandWithStdout(addGenAccountCmd) if err != nil { return err } @@ -143,13 +144,13 @@ func getGenesisOperatorAddress(home string) (string, error) { "val", ) - addr, err := utils.ExecBashCommandWithStdout(getOperatorAddrCommand) + addr, err := bash.ExecCommandWithStdout(getOperatorAddrCommand) if err != nil { fmt.Println("val addr failed") return "", err } - a := strings.TrimSpace(addr.String()) + a := addr.String() return a, nil } @@ -159,7 +160,7 @@ func GetRollappSequencerAddress(home string) (string, error) { Dir: rollappConfigDirPath, ID: consts.KeysIds.RollappSequencer, ChainBinary: consts.Executables.RollappEVM, - Type: config.EVM_ROLLAPP, + Type: consts.EVM_ROLLAPP, } addr, err := utils.GetAddressBinary(seqKeyConfig, consts.Executables.RollappEVM) if err != nil { @@ -182,7 +183,7 @@ func GetRollappSequencerAddress(home string) (string, error) { // "--home", // rollappConfigDirPath, // ) -// _, err = utils.ExecBashCommandWithStdout(collectGentx) +// _, err = utils.ExecCommandWithStdout(collectGentx) // if err != nil { // return err // } @@ -220,7 +221,7 @@ func GetRollappSequencerAddress(home string) (string, error) { // "--home", // rollappConfigDirPath, // ) -// _, err = utils.ExecBashCommandWithStdout(gentxCmd) +// _, err = utils.ExecCommandWithStdout(gentxCmd) // if err != nil { // return err // } diff --git a/cmd/config/init/keys.go b/cmd/config/init/keys.go index a46c8586..3627741e 100644 --- a/cmd/config/init/keys.go +++ b/cmd/config/init/keys.go @@ -10,7 +10,8 @@ import ( "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" + "github.com/dymensionxyz/roller/utils/bash" + "github.com/dymensionxyz/roller/utils/config" ) func GenerateSequencersKeys(initConfig config.RollappConfig) ([]utils.KeyInfo, error) { @@ -51,14 +52,14 @@ func getSequencerKeysConfig(rollappConfig config.RollappConfig) []utils.KeyConfi ID: consts.KeysIds.HubSequencer, ChainBinary: consts.Executables.Dymension, // Eventhough the hub can get evm signatures, we still use the native - Type: config.SDK_ROLLAPP, + Type: consts.SDK_ROLLAPP, }, { Dir: consts.ConfigDirName.HubKeys, ID: consts.KeysIds.HubGenesis, ChainBinary: consts.Executables.Dymension, // Eventhough the hub can get evm signatures, we still use the native - Type: config.SDK_ROLLAPP, + Type: consts.SDK_ROLLAPP, }, { Dir: consts.ConfigDirName.Rollapp, @@ -81,7 +82,7 @@ func getRelayerKeysConfig(rollappConfig config.RollappConfig) map[string]utils.K Dir: path.Join(rollappConfig.Home, consts.ConfigDirName.Relayer), ID: consts.KeysIds.HubRelayer, ChainBinary: consts.Executables.Dymension, - Type: config.SDK_ROLLAPP, + Type: consts.SDK_ROLLAPP, }, } } @@ -96,7 +97,7 @@ func CreateAddressBinary( "--output", "json", } createKeyCommand := exec.Command(keyConfig.ChainBinary, args...) - out, err := utils.ExecBashCommandWithStdout(createKeyCommand) + out, err := bash.ExecCommandWithStdout(createKeyCommand) if err != nil { return nil, err } @@ -117,7 +118,7 @@ func GetRelayerKeys(rollappConfig config.RollappConfig) ([]utils.KeyInfo, error) rollappConfig.HubData.ID, ) - out, err := utils.ExecBashCommandWithStdout(showRollappKeyCmd) + out, err := bash.ExecCommandWithStdout(showRollappKeyCmd) if err != nil { pterm.Error.Printf("failed to retrieve rollapp key: %v\n", err) } @@ -127,7 +128,7 @@ func GetRelayerKeys(rollappConfig config.RollappConfig) ([]utils.KeyInfo, error) } fmt.Println(relayerRollappAddress) - out, err = utils.ExecBashCommandWithStdout(showHubKeyCmd) + out, err = bash.ExecCommandWithStdout(showHubKeyCmd) if err != nil { pterm.Error.Printf("failed to retrieve hub key: %v\n", err) } @@ -159,7 +160,7 @@ func GenerateRelayerKeys(rollappConfig config.RollappConfig) ([]utils.KeyInfo, e createHubKeyCmd := getAddRlyKeyCmd(keys[consts.KeysIds.HubRelayer], rollappConfig.HubData.ID) pterm.Info.Println("creating relayer rollapp key") - out, err := utils.ExecBashCommandWithStdout(createRollappKeyCmd) + out, err := bash.ExecCommandWithStdout(createRollappKeyCmd) if err != nil { return nil, err } @@ -173,7 +174,7 @@ func GenerateRelayerKeys(rollappConfig config.RollappConfig) ([]utils.KeyInfo, e ) pterm.Info.Println("creating relayer hub key") - out, err = utils.ExecBashCommandWithStdout(createHubKeyCmd) + out, err = bash.ExecCommandWithStdout(createHubKeyCmd) if err != nil { return nil, err } @@ -191,7 +192,7 @@ func GenerateRelayerKeys(rollappConfig config.RollappConfig) ([]utils.KeyInfo, e func getAddRlyKeyCmd(keyConfig utils.KeyConfig, chainID string) *exec.Cmd { coinType := "118" - if keyConfig.Type == config.EVM_ROLLAPP { + if keyConfig.Type == consts.EVM_ROLLAPP { coinType = "60" } return exec.Command( @@ -209,7 +210,7 @@ func getAddRlyKeyCmd(keyConfig utils.KeyConfig, chainID string) *exec.Cmd { func getShowRlyKeyCmd(keyConfig utils.KeyConfig, chainID string) *exec.Cmd { coinType := "118" - if keyConfig.Type == config.EVM_ROLLAPP { + if keyConfig.Type == consts.EVM_ROLLAPP { coinType = "60" } return exec.Command( diff --git a/cmd/config/init/output.go b/cmd/config/init/output.go index 3ff79a09..ab6e7cf9 100644 --- a/cmd/config/init/output.go +++ b/cmd/config/init/output.go @@ -7,7 +7,7 @@ import ( "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" + "github.com/dymensionxyz/roller/utils/config" ) type OutputHandler struct { diff --git a/cmd/config/init/relayer.go b/cmd/config/init/relayer.go index 1d0b8771..964d8a96 100644 --- a/cmd/config/init/relayer.go +++ b/cmd/config/init/relayer.go @@ -6,10 +6,10 @@ import ( "os/exec" "path/filepath" + "github.com/dymensionxyz/roller/utils/config" "github.com/pterm/pterm" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/relayer" ) diff --git a/cmd/config/init/rollapp.go b/cmd/config/init/rollapp.go index f715d7e5..40da4860 100644 --- a/cmd/config/init/rollapp.go +++ b/cmd/config/init/rollapp.go @@ -5,9 +5,9 @@ import ( "path/filepath" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/sequencer" + "github.com/dymensionxyz/roller/utils/bash" + "github.com/dymensionxyz/roller/utils/config" ) func InitializeRollappConfig(initConfig config.RollappConfig) error { @@ -23,7 +23,7 @@ func InitializeRollappConfig(initConfig config.RollappConfig) error { home, ) - _, err := utils.ExecBashCommandWithStdout(initRollappCmd) + _, err := bash.ExecCommandWithStdout(initRollappCmd) if err != nil { return err } diff --git a/cmd/config/set/da.go b/cmd/config/set/da.go index bae5dc64..c6eee396 100644 --- a/cmd/config/set/da.go +++ b/cmd/config/set/da.go @@ -7,25 +7,26 @@ import ( "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" datalayer "github.com/dymensionxyz/roller/data_layer" "github.com/dymensionxyz/roller/sequencer" globalutils "github.com/dymensionxyz/roller/utils" + config2 "github.com/dymensionxyz/roller/utils/config" + "github.com/dymensionxyz/roller/utils/config/tomlconfig" ) -func setDA(rlpCfg config.RollappConfig, value string) error { - daValue := config.DAType(value) +func setDA(rlpCfg config2.RollappConfig, value string) error { + daValue := consts.DAType(value) if daValue == rlpCfg.DA { return nil } - if !config.IsValidDAType(value) { - return fmt.Errorf("invalid DA type. Supported types are: %v", config.SupportedDas) + if !config2.IsValidDAType(value) { + return fmt.Errorf("invalid DA type. Supported types are: %v", config2.SupportedDas) } return updateDaConfig(rlpCfg, daValue) } -func updateDaConfig(rlpCfg config.RollappConfig, newDa config.DAType) error { +func updateDaConfig(rlpCfg config2.RollappConfig, newDa consts.DAType) error { daCfgDirPath := filepath.Join(rlpCfg.Home, consts.ConfigDirName.DALightNode) dirExist, err := globalutils.DirNotEmpty(daCfgDirPath) if err != nil { @@ -54,12 +55,12 @@ func updateDaConfig(rlpCfg config.RollappConfig, newDa config.DAType) error { return err } - if err := config.WriteConfigToTOML(rlpCfg); err != nil { + if err := tomlconfig.Write(rlpCfg); err != nil { return err } fmt.Printf("💈 RollApp DA has been successfully set to '%s'\n\n", newDa) - if newDa != config.Local { + if newDa != consts.Local { addresses := make([]utils.KeyInfo, 0) damanager := datalayer.NewDAManager(newDa, rlpCfg.Home) daAddress, err := damanager.GetDAAccountAddress() diff --git a/cmd/config/set/hub_rpc.go b/cmd/config/set/hub_rpc.go index 5420b62f..1828e831 100644 --- a/cmd/config/set/hub_rpc.go +++ b/cmd/config/set/hub_rpc.go @@ -1,19 +1,22 @@ package set import ( - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/relayer" "github.com/dymensionxyz/roller/sequencer" "github.com/dymensionxyz/roller/utils" + config2 "github.com/dymensionxyz/roller/utils/config" + "github.com/dymensionxyz/roller/utils/config/tomlconfig" ) -func setHubRPC(rlpCfg config.RollappConfig, value string) error { +func setHubRPC(rlpCfg config2.RollappConfig, value string) error { rlpCfg.HubData.RPC_URL = value - if err := config.WriteConfigToTOML(rlpCfg); err != nil { + if err := tomlconfig.Write(rlpCfg); err != nil { return err } - if err := relayer.UpdateRlyConfigValue(rlpCfg, []string{"chains", rlpCfg.HubData.ID, "value", "rpc-addr"}, - value); err != nil { + if err := relayer.UpdateRlyConfigValue( + rlpCfg, []string{"chains", rlpCfg.HubData.ID, "value", "rpc-addr"}, + value, + ); err != nil { return err } dymintTomlPath := sequencer.GetDymintFilePath(rlpCfg.Home) diff --git a/cmd/config/set/lc_rpc.go b/cmd/config/set/lc_rpc.go index 3134792d..d611fa90 100644 --- a/cmd/config/set/lc_rpc.go +++ b/cmd/config/set/lc_rpc.go @@ -5,20 +5,22 @@ import ( "path/filepath" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/sequencer" "github.com/dymensionxyz/roller/utils" + "github.com/dymensionxyz/roller/utils/config" ) func setLCGatewayPort(cfg config.RollappConfig, value string) error { if err := validatePort(value); err != nil { return err } - if cfg.DA != config.Celestia { + if cfg.DA != consts.Celestia { return errors.New("setting the LC RPC port is only supported for Celestia") } - if err := utils.UpdateFieldInToml(filepath.Join(cfg.Home, consts.ConfigDirName.DALightNode, "config.toml"), - "Gateway.Port", value); err != nil { + if err := utils.UpdateFieldInToml( + filepath.Join(cfg.Home, consts.ConfigDirName.DALightNode, "config.toml"), + "Gateway.Port", value, + ); err != nil { return err } return sequencer.UpdateDymintDAConfig(cfg) @@ -28,11 +30,13 @@ func setLCRPCPort(cfg config.RollappConfig, value string) error { if err := validatePort(value); err != nil { return err } - if cfg.DA != config.Celestia { + if cfg.DA != consts.Celestia { return errors.New("setting the LC RPC port is only supported for Celestia") } - if err := utils.UpdateFieldInToml(filepath.Join(cfg.Home, consts.ConfigDirName.DALightNode, "config.toml"), - "RPC.Port", value); err != nil { + if err := utils.UpdateFieldInToml( + filepath.Join(cfg.Home, consts.ConfigDirName.DALightNode, "config.toml"), + "RPC.Port", value, + ); err != nil { return err } return sequencer.UpdateDymintDAConfig(cfg) diff --git a/cmd/config/set/rollapp_rpc.go b/cmd/config/set/rollapp_rpc.go index bd0bdcbe..6ae2c305 100644 --- a/cmd/config/set/rollapp_rpc.go +++ b/cmd/config/set/rollapp_rpc.go @@ -6,17 +6,19 @@ import ( "strconv" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/relayer" "github.com/dymensionxyz/roller/utils" + "github.com/dymensionxyz/roller/utils/config" ) func setRollappRPC(rlpCfg config.RollappConfig, value string) error { if err := validatePort(value); err != nil { return err } - if err := relayer.UpdateRlyConfigValue(rlpCfg, []string{"chains", rlpCfg.RollappID, "value", "rpc-addr"}, "http://localhost:"+ - value); err != nil { + if err := relayer.UpdateRlyConfigValue( + rlpCfg, []string{"chains", rlpCfg.RollappID, "value", "rpc-addr"}, "http://localhost:"+ + value, + ); err != nil { return err } if err := updateRlpCfg(rlpCfg, value); err != nil { diff --git a/cmd/config/set/set.go b/cmd/config/set/set.go index 400eece7..aa74eb5d 100644 --- a/cmd/config/set/set.go +++ b/cmd/config/set/set.go @@ -7,10 +7,11 @@ import ( "github.com/spf13/cobra" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" + config2 "github.com/dymensionxyz/roller/utils/config" + "github.com/dymensionxyz/roller/utils/config/tomlconfig" ) -var keyUpdateFuncs = map[string]func(cfg config.RollappConfig, value string) error{ +var keyUpdateFuncs = map[string]func(cfg config2.RollappConfig, value string) error{ "rollapp-rpc-port": setRollappRPC, "lc-gateway-port": setLCGatewayPort, "lc-rpc-port": setLCRPCPort, @@ -32,7 +33,7 @@ func Cmd() *cobra.Command { Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { home := cmd.Flag(utils.FlagNames.Home).Value.String() - rlpCfg, err := config.LoadRollerConfigFromTOML(home) + rlpCfg, err := tomlconfig.LoadRollerConfig(home) if err != nil { return err } diff --git a/cmd/config/show/show.go b/cmd/config/show/show.go index 02b486a6..7a88012e 100644 --- a/cmd/config/show/show.go +++ b/cmd/config/show/show.go @@ -5,10 +5,11 @@ import ( "os" "path/filepath" + "github.com/dymensionxyz/roller/cmd/consts" "github.com/spf13/cobra" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" + "github.com/dymensionxyz/roller/utils/errorhandling" ) func Cmd() *cobra.Command { @@ -17,8 +18,8 @@ func Cmd() *cobra.Command { Short: "Show the configuration of the rollapp on the local machine.", Run: func(cmd *cobra.Command, args []string) { home := cmd.Flag(utils.FlagNames.Home).Value.String() - utils.PrettifyErrorIfExists( - printFileContent(filepath.Join(home, config.RollerConfigFileName)), + errorhandling.PrettifyErrorIfExists( + printFileContent(filepath.Join(home, consts.RollerConfigFileName)), ) }, } diff --git a/cmd/consts/consts.go b/cmd/consts/consts.go index 35435087..fe71d35a 100644 --- a/cmd/consts/consts.go +++ b/cmd/consts/consts.go @@ -2,13 +2,12 @@ package consts import ( "fmt" - - "github.com/dymensionxyz/roller/config" ) const ( binsDir = "/usr/local/bin" DefaultTokenSupply = "1000000000000000000000000000" + DefaultFee = 100000000000000000 // 0.1 ) var internalBinsDir = fmt.Sprintf("%s/roller_bins", binsDir) @@ -95,7 +94,15 @@ var SpinnerMsgs = struct { BalancesVerification: " Verifying balances...\n", } -var MainnetHubData = config.HubData{ +type HubData = struct { + API_URL string `toml:"api_url"` + ID string `toml:"id"` + RPC_URL string `toml:"rpc_url"` + ARCHIVE_RPC_URL string `toml:"archive_rpc_url"` + GAS_PRICE string `toml:"gas_price"` +} + +var MainnetHubData = HubData{ API_URL: "https://dymension-mainnet-rest.public.blastapi.io", ID: MainnetHubID, RPC_URL: "https://dymension-mainnet-tendermint.public.blastapi.io", @@ -103,7 +110,7 @@ var MainnetHubData = config.HubData{ GAS_PRICE: "20000000000", } -var TestnetHubData = config.HubData{ +var TestnetHubData = HubData{ API_URL: "https://api-blumbus.mzonder.com", ID: TestnetHubID, RPC_URL: "https://rpc-blumbus.mzonder.com", @@ -111,7 +118,15 @@ var TestnetHubData = config.HubData{ GAS_PRICE: "20000000000", } -var LocalHubData = config.HubData{ +var DevnetHubData = HubData{ + API_URL: "http://52.58.111.62:1318", + ID: DevnetHubID, + RPC_URL: "http://52.58.111.62:36657", + ARCHIVE_RPC_URL: "http://52.58.111.62:36657", + GAS_PRICE: "100000000", +} + +var LocalHubData = HubData{ API_URL: "http://localhost:1318", ID: LocalHubID, RPC_URL: "http://localhost:36657", @@ -119,7 +134,7 @@ var LocalHubData = config.HubData{ GAS_PRICE: "100000000", } -var MockHubData = config.HubData{ +var MockHubData = HubData{ API_URL: "", ID: MockHubID, RPC_URL: "", @@ -128,9 +143,10 @@ var MockHubData = config.HubData{ } // TODO(#112): The available hub networks should be read from YAML file -var Hubs = map[string]config.HubData{ +var Hubs = map[string]HubData{ MockHubName: MockHubData, LocalHubName: LocalHubData, + DevnetHubName: DevnetHubData, TestnetHubName: TestnetHubData, MainnetHubName: MainnetHubData, } @@ -138,6 +154,7 @@ var Hubs = map[string]config.HubData{ const ( MockHubName = "mock" LocalHubName = "local" + DevnetHubName = "devnet" TestnetHubName = "testnet" MainnetHubName = "mainnet" ) @@ -145,6 +162,40 @@ const ( const ( MockHubID = "mock" LocalHubID = "dymension_100-1" + DevnetHubID = "dymension_100-1" TestnetHubID = "blumbus_111-1" MainnetHubID = "dymension_1100-1" ) + +var NodeType = struct { + Sequencer string + FullNode string +}{ + Sequencer: "sequencer", + FullNode: "fullnode", +} + +var DaAuthTokenType = struct { + Admin string + Read string +}{ + Admin: "admin", + Read: "read", +} + +const RollerConfigFileName = "roller.toml" + +type VMType string + +const ( + SDK_ROLLAPP VMType = "sdk" + EVM_ROLLAPP VMType = "evm" +) + +type DAType string + +const ( + Local DAType = "local" + Celestia DAType = "celestia" + Avail DAType = "avail" +) diff --git a/cmd/da-light-client/start/start.go b/cmd/da-light-client/start/start.go index 8143ffd9..caa6a186 100644 --- a/cmd/da-light-client/start/start.go +++ b/cmd/da-light-client/start/start.go @@ -7,10 +7,13 @@ import ( "github.com/spf13/cobra" + "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" datalayer "github.com/dymensionxyz/roller/data_layer" "github.com/dymensionxyz/roller/data_layer/celestia" + "github.com/dymensionxyz/roller/utils/bash" + "github.com/dymensionxyz/roller/utils/config/tomlconfig" + "github.com/dymensionxyz/roller/utils/errorhandling" ) const ( @@ -26,19 +29,19 @@ func Cmd() *cobra.Command { Short: "Runs the DA light client.", Run: func(cmd *cobra.Command, args []string) { home := cmd.Flag(utils.FlagNames.Home).Value.String() - rollappConfig, err := config.LoadRollerConfigFromTOML(home) - utils.PrettifyErrorIfExists(err) - utils.RequireMigrateIfNeeded(rollappConfig) + rollappConfig, err := tomlconfig.LoadRollerConfig(home) + errorhandling.PrettifyErrorIfExists(err) + errorhandling.RequireMigrateIfNeeded(rollappConfig) metricsEndpoint := cmd.Flag(metricsEndpointFlag).Value.String() - if metricsEndpoint != "" && rollappConfig.DA != config.Celestia { - utils.PrettifyErrorIfExists( + if metricsEndpoint != "" && rollappConfig.DA != consts.Celestia { + errorhandling.PrettifyErrorIfExists( errors.New("metrics endpoint can only be set for celestia"), ) } damanager := datalayer.NewDAManager(rollappConfig.DA, rollappConfig.Home) insufficientBalances, err := damanager.CheckDABalance() - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) utils.PrintInsufficientBalancesIfAny(insufficientBalances) rpcEndpoint := cmd.Flag(rpcEndpointFlag).Value.String() @@ -50,7 +53,7 @@ func Cmd() *cobra.Command { } startDALCCmd := damanager.GetStartDACmd() if startDALCCmd == nil { - utils.PrettifyErrorIfExists( + errorhandling.PrettifyErrorIfExists( errors.New( "DA doesn't need to run seperatly. It runs automatically with the app", ), @@ -60,7 +63,7 @@ func Cmd() *cobra.Command { LCEndpoint = damanager.GetLightNodeEndpoint() ctx, cancel := context.WithCancel(context.Background()) defer cancel() - go utils.RunBashCmdAsync( + go bash.RunCmdAsync( ctx, startDALCCmd, printOutput, diff --git a/cmd/eibc/init.go b/cmd/eibc/init.go index ba0e6b8b..0b9ac988 100644 --- a/cmd/eibc/init.go +++ b/cmd/eibc/init.go @@ -7,10 +7,10 @@ import ( "os/exec" "path/filepath" + "github.com/dymensionxyz/roller/utils/bash" "github.com/spf13/cobra" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/cmd/utils" global_utils "github.com/dymensionxyz/roller/utils" ) @@ -33,7 +33,7 @@ func initCmd() *cobra.Command { c := GetInitCommand() - err = utils.ExecBashCmd(c) + err = bash.ExecCmd(c) if err != nil { return } diff --git a/cmd/eibc/scale.go b/cmd/eibc/scale.go index c5a733a5..912e8a36 100644 --- a/cmd/eibc/scale.go +++ b/cmd/eibc/scale.go @@ -3,10 +3,10 @@ package eibc import ( "os/exec" + "github.com/dymensionxyz/roller/utils/bash" "github.com/spf13/cobra" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/cmd/utils" ) func scaleCmd() *cobra.Command { @@ -19,7 +19,7 @@ func scaleCmd() *cobra.Command { c := GetScaleCmd(count) - err := utils.ExecBashCmdFollow(c) + err := bash.ExecCmdFollow(c) if err != nil { return } diff --git a/cmd/eibc/start.go b/cmd/eibc/start.go index 29745bd8..d873adb6 100644 --- a/cmd/eibc/start.go +++ b/cmd/eibc/start.go @@ -7,10 +7,10 @@ import ( "os/exec" "path/filepath" + "github.com/dymensionxyz/roller/utils/bash" "github.com/spf13/cobra" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/cmd/utils" global_utils "github.com/dymensionxyz/roller/utils" ) @@ -30,7 +30,7 @@ func startCmd() *cobra.Command { fmt.Println("eibc home directory not present, running init") c := GetInitCommand() - _, err := utils.ExecBashCommandWithStdout(c) + _, err := bash.ExecCommandWithStdout(c) if err != nil { return } @@ -48,7 +48,7 @@ func startCmd() *cobra.Command { } c := GetStartCmd() - err = utils.ExecBashCmdFollow(c) + err = bash.ExecCmdFollow(c) if err != nil { return } diff --git a/cmd/keys/export/export.go b/cmd/keys/export/export.go index 1a25b603..61313bb2 100644 --- a/cmd/keys/export/export.go +++ b/cmd/keys/export/export.go @@ -9,8 +9,10 @@ import ( "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" datalayer "github.com/dymensionxyz/roller/data_layer" + "github.com/dymensionxyz/roller/utils/bash" + "github.com/dymensionxyz/roller/utils/config/tomlconfig" + "github.com/dymensionxyz/roller/utils/errorhandling" ) func Cmd() *cobra.Command { @@ -19,8 +21,8 @@ func Cmd() *cobra.Command { Short: "Exports the private key of the given key id.", Run: func(cmd *cobra.Command, args []string) { home := cmd.Flag(utils.FlagNames.Home).Value.String() - rlpCfg, err := config.LoadRollerConfigFromTOML(home) - utils.PrettifyErrorIfExists(err) + rlpCfg, err := tomlconfig.LoadRollerConfig(home) + errorhandling.PrettifyErrorIfExists(err) supportedKeys := []string{ consts.KeysIds.HubSequencer, consts.KeysIds.RollappSequencer, @@ -36,27 +38,27 @@ func Cmd() *cobra.Command { filepath.Join(home, consts.ConfigDirName.HubKeys), consts.Executables.Dymension, ) - out, err := utils.ExecBashCommandWithStdout(exportKeyCmd) - utils.PrettifyErrorIfExists(err) + out, err := bash.ExecCommandWithStdout(exportKeyCmd) + errorhandling.PrettifyErrorIfExists(err) printHexKeyOutput(out.String()) } else if keyID == consts.KeysIds.RollappSequencer { exportKeyCmd := utils.GetExportKeyCmdBinary( keyID, filepath.Join(home, consts.ConfigDirName.Rollapp), rlpCfg.RollappBinary, ) - out, err := utils.ExecBashCommandWithStdout(exportKeyCmd) - utils.PrettifyErrorIfExists(err) + out, err := bash.ExecCommandWithStdout(exportKeyCmd) + errorhandling.PrettifyErrorIfExists(err) printHexKeyOutput(out.String()) } else if keyID != "" && keyID == damanager.GetKeyName() { privateKey, err := damanager.GetPrivateKey() - utils.PrettifyErrorIfExists(err) - if rlpCfg.DA == config.Celestia { + errorhandling.PrettifyErrorIfExists(err) + if rlpCfg.DA == consts.Celestia { printHexKeyOutput(privateKey) - } else if rlpCfg.DA == config.Avail { + } else if rlpCfg.DA == consts.Avail { printMnemonicKeyOutput(privateKey) } } else { - utils.PrettifyErrorIfExists( + errorhandling.PrettifyErrorIfExists( fmt.Errorf( "invalid key id: %s. The supported keys are %s", keyID, strings.Join(supportedKeys, ", "), diff --git a/cmd/keys/list/list.go b/cmd/keys/list/list.go index 10f2192e..b8ebb410 100644 --- a/cmd/keys/list/list.go +++ b/cmd/keys/list/list.go @@ -9,8 +9,9 @@ import ( "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" datalayer "github.com/dymensionxyz/roller/data_layer" + "github.com/dymensionxyz/roller/utils/config/tomlconfig" + "github.com/dymensionxyz/roller/utils/errorhandling" ) var flagNames = struct { @@ -25,13 +26,13 @@ func Cmd() *cobra.Command { Short: "List all the addresses of roller on the local machine.", Run: func(cmd *cobra.Command, args []string) { home := cmd.Flag(utils.FlagNames.Home).Value.String() - rollappConfig, err := config.LoadRollerConfigFromTOML(home) - utils.PrettifyErrorIfExists(err) + rollappConfig, err := tomlconfig.LoadRollerConfig(home) + errorhandling.PrettifyErrorIfExists(err) addresses := make([]utils.KeyInfo, 0) damanager := datalayer.NewDAManager(rollappConfig.DA, rollappConfig.Home) daAddr, err := damanager.DataLayer.GetDAAccountAddress() - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) if daAddr != nil { addresses = append( addresses, utils.KeyInfo{ @@ -47,7 +48,7 @@ func Cmd() *cobra.Command { ID: consts.KeysIds.HubSequencer, }, consts.Executables.Dymension, ) - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) addresses = append( addresses, utils.KeyInfo{ Address: hubSeqInfo.Address, @@ -61,7 +62,7 @@ func Cmd() *cobra.Command { ID: consts.KeysIds.RollappSequencer, }, rollappConfig.RollappBinary, ) - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) addresses = append( addresses, utils.KeyInfo{ Address: raSeqInfo.Address, @@ -70,7 +71,7 @@ func Cmd() *cobra.Command { ) hubRlyAddr, err := utils.GetRelayerAddress(rollappConfig.Home, rollappConfig.HubData.ID) - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) addresses = append( addresses, utils.KeyInfo{ Address: hubRlyAddr, @@ -82,7 +83,7 @@ func Cmd() *cobra.Command { rollappConfig.Home, rollappConfig.RollappID, ) - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) addresses = append( addresses, utils.KeyInfo{ @@ -92,7 +93,7 @@ func Cmd() *cobra.Command { ) outputType := cmd.Flag(flagNames.outputType).Value.String() if outputType == "json" { - utils.PrettifyErrorIfExists(printAsJSON(addresses)) + errorhandling.PrettifyErrorIfExists(printAsJSON(addresses)) } else if outputType == "text" { utils.PrintAddressesWithTitle(addresses) } diff --git a/cmd/migrate/migrate.go b/cmd/migrate/migrate.go index a2bd1d01..7b4506a5 100644 --- a/cmd/migrate/migrate.go +++ b/cmd/migrate/migrate.go @@ -6,7 +6,9 @@ import ( "github.com/spf13/cobra" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" + config2 "github.com/dymensionxyz/roller/utils/config" + "github.com/dymensionxyz/roller/utils/config/tomlconfig" + "github.com/dymensionxyz/roller/utils/errorhandling" "github.com/dymensionxyz/roller/version" ) @@ -29,26 +31,26 @@ func Cmd() *cobra.Command { Short: "Migrates the roller configuration to the newly installed version.", Run: func(cmd *cobra.Command, args []string) { home := cmd.Flag(utils.FlagNames.Home).Value.String() - rlpCfg, err := config.LoadRollerConfigFromTOML(home) - utils.PrettifyErrorIfExists(err) + rlpCfg, err := tomlconfig.LoadRollerConfig(home) + errorhandling.PrettifyErrorIfExists(err) prevVersionData, err := GetPrevVersionData(rlpCfg) - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) for _, migrator := range migrationsRegistry { if migrator.ShouldMigrate(*prevVersionData) { - utils.PrettifyErrorIfExists(migrator.PerformMigration(rlpCfg)) + errorhandling.PrettifyErrorIfExists(migrator.PerformMigration(rlpCfg)) } } trimmedCurrentVersion := version.TrimVersionStr(version.BuildVersion) rlpCfg.RollerVersion = trimmedCurrentVersion - err = config.WriteConfigToTOML(rlpCfg) - utils.PrettifyErrorIfExists(err) + err = tomlconfig.Write(rlpCfg) + errorhandling.PrettifyErrorIfExists(err) fmt.Printf("💈 Roller has migrated successfully to %s!\n", trimmedCurrentVersion) }, } return cmd } -func GetPrevVersionData(rlpCfg config.RollappConfig) (*VersionData, error) { +func GetPrevVersionData(rlpCfg config2.RollappConfig) (*VersionData, error) { rollerPrevVersion := rlpCfg.RollerVersion var major, minor, patch int // Special case for the first version of roller, that didn't have a version field. @@ -78,7 +80,7 @@ func GetPrevVersionData(rlpCfg config.RollappConfig) (*VersionData, error) { } type VersionMigrator interface { - PerformMigration(rlpCfg config.RollappConfig) error + PerformMigration(rlpCfg config2.RollappConfig) error ShouldMigrate(prevVersion VersionData) bool } diff --git a/cmd/migrate/v0_1_11.go b/cmd/migrate/v0_1_11.go index 7af87694..5cf0550f 100644 --- a/cmd/migrate/v0_1_11.go +++ b/cmd/migrate/v0_1_11.go @@ -1,9 +1,9 @@ package migrate import ( - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/sequencer" "github.com/dymensionxyz/roller/utils" + "github.com/dymensionxyz/roller/utils/config" ) type VersionMigratorV0111 struct{} diff --git a/cmd/migrate/v0_1_18.go b/cmd/migrate/v0_1_18.go index 9a1bb321..cae5fc12 100644 --- a/cmd/migrate/v0_1_18.go +++ b/cmd/migrate/v0_1_18.go @@ -2,8 +2,8 @@ package migrate import ( initconfig "github.com/dymensionxyz/roller/cmd/config/init" - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/relayer" + "github.com/dymensionxyz/roller/utils/config" ) type VersionMigratorV0118 struct{} @@ -20,7 +20,11 @@ func (v *VersionMigratorV0118) PerformMigration(rlpCfg config.RollappConfig) err //nolint:errcheck hubRpcAddress := rlyCfg["chains"].(map[interface{}]interface{})[rlpCfg.HubData.ID].(map[interface{}]interface{})["value"].(map[interface{}]interface{})["rpc-addr"].(string) if hubRpcAddress == initconfig.Hubs[initconfig.FroopylandHubName].RPC_URL { - if err := relayer.UpdateRlyConfigValue(rlpCfg, []string{"chains", rlpCfg.HubData.ID, "value", "rpc-addr"}, initconfig.Hubs[initconfig.FroopylandHubName].ARCHIVE_RPC_URL); err != nil { + if err := relayer.UpdateRlyConfigValue( + rlpCfg, + []string{"chains", rlpCfg.HubData.ID, "value", "rpc-addr"}, + initconfig.Hubs[initconfig.FroopylandHubName].ARCHIVE_RPC_URL, + ); err != nil { return err } } diff --git a/cmd/migrate/v1_0_00.go b/cmd/migrate/v1_0_00.go index 6dcb5a83..1e31b0fc 100644 --- a/cmd/migrate/v1_0_00.go +++ b/cmd/migrate/v1_0_00.go @@ -5,9 +5,9 @@ import ( "os/exec" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/relayer" + "github.com/dymensionxyz/roller/utils/bash" + "github.com/dymensionxyz/roller/utils/config" ) type VersionMigratorV1000 struct{} @@ -27,7 +27,7 @@ func (v *VersionMigratorV1000) PerformMigration(rlpCfg config.RollappConfig) err "--keyring-backend", "test", ) - out, err := utils.ExecBashCommandWithStdout(migrateRollappKeyCmd) + out, err := bash.ExecCommandWithStdout(migrateRollappKeyCmd) if err != nil { return err } @@ -42,13 +42,17 @@ func (v *VersionMigratorV1000) PerformMigration(rlpCfg config.RollappConfig) err "--keyring-backend", "test", ) - out, err = utils.ExecBashCommandWithStdout(migrateHubKeyCmd) + out, err = bash.ExecCommandWithStdout(migrateHubKeyCmd) if err != nil { return err } fmt.Println(out.String()) fmt.Println("💈 Updating relayer configuration to match new relayer key...") - if err := relayer.UpdateRlyConfigValue(rlpCfg, []string{"chains", rlpCfg.RollappID, "value", "extra-codecs"}, []string{"ethermint"}); err != nil { + if err := relayer.UpdateRlyConfigValue( + rlpCfg, + []string{"chains", rlpCfg.RollappID, "value", "extra-codecs"}, + []string{"ethermint"}, + ); err != nil { return err } return nil diff --git a/cmd/migrate/v1_0_05.go b/cmd/migrate/v1_0_05.go index fbc082fb..18bf24e6 100644 --- a/cmd/migrate/v1_0_05.go +++ b/cmd/migrate/v1_0_05.go @@ -6,11 +6,11 @@ import ( "path/filepath" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/config" datalayer "github.com/dymensionxyz/roller/data_layer" "github.com/dymensionxyz/roller/data_layer/celestia" "github.com/dymensionxyz/roller/sequencer" "github.com/dymensionxyz/roller/utils" + "github.com/dymensionxyz/roller/utils/config" ) type VersionMigratorV1005 struct{} @@ -25,13 +25,13 @@ func (v *VersionMigratorV1005) ShouldMigrate(prevVersion VersionData) bool { func (v *VersionMigratorV1005) PerformMigration(rlpCfg config.RollappConfig) error { // If the DA is not celestia, no-op - if rlpCfg.DA != config.Celestia { + if rlpCfg.DA != consts.Celestia { return nil } // Update dymint config with celestia new config dymintTomlPath := sequencer.GetDymintFilePath(rlpCfg.Home) da := datalayer.NewDAManager(rlpCfg.DA, rlpCfg.Home) - sequencerDaConfig := da.GetSequencerDAConfig() + sequencerDaConfig := da.GetSequencerDAConfig(consts.NodeType.Sequencer) if sequencerDaConfig == "" { return nil } diff --git a/cmd/migrate/v_0_1_12.go b/cmd/migrate/v_0_1_12.go index b815ca8b..c791da55 100644 --- a/cmd/migrate/v_0_1_12.go +++ b/cmd/migrate/v_0_1_12.go @@ -3,11 +3,13 @@ package migrate import ( "path/filepath" - "github.com/dymensionxyz/roller/config" + "github.com/dymensionxyz/roller/cmd/consts" datalayer "github.com/dymensionxyz/roller/data_layer" "github.com/dymensionxyz/roller/data_layer/avail" "github.com/dymensionxyz/roller/sequencer" "github.com/dymensionxyz/roller/utils" + config2 "github.com/dymensionxyz/roller/utils/config" + "github.com/dymensionxyz/roller/utils/config/tomlconfig" ) type VersionMigratorV0112 struct{} @@ -16,20 +18,20 @@ func (v *VersionMigratorV0112) ShouldMigrate(prevVersion VersionData) bool { return prevVersion.Major < 1 && prevVersion.Minor < 2 && prevVersion.Patch < 12 } -func (v *VersionMigratorV0112) PerformMigration(rlpCfg config.RollappConfig) error { +func (v *VersionMigratorV0112) PerformMigration(rlpCfg config2.RollappConfig) error { dymintTomlPath := sequencer.GetDymintFilePath(rlpCfg.Home) if rlpCfg.DA == "mock" { - rlpCfg.DA = config.Local - return config.WriteConfigToTOML(rlpCfg) + rlpCfg.DA = consts.Local + return tomlconfig.Write(rlpCfg) } - if rlpCfg.DA == config.Avail { + if rlpCfg.DA == consts.Avail { availNewCfgPath := avail.GetCfgFilePath(rlpCfg.Home) if err := utils.MoveFile(filepath.Join(rlpCfg.Home, avail.ConfigFileName), availNewCfgPath); err != nil { return err } } da := datalayer.NewDAManager(rlpCfg.DA, rlpCfg.Home) - sequencerDaConfig := da.GetSequencerDAConfig() + sequencerDaConfig := da.GetSequencerDAConfig(consts.NodeType.Sequencer) if sequencerDaConfig == "" { return nil } diff --git a/cmd/migrate/v_0_1_13.go b/cmd/migrate/v_0_1_13.go index de67a04a..2c9ce8ad 100644 --- a/cmd/migrate/v_0_1_13.go +++ b/cmd/migrate/v_0_1_13.go @@ -1,10 +1,10 @@ package migrate import ( - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/relayer" "github.com/dymensionxyz/roller/sequencer" "github.com/dymensionxyz/roller/utils" + "github.com/dymensionxyz/roller/utils/config" ) type VersionMigratorV0113 struct{} diff --git a/cmd/migrate/v_0_1_4.go b/cmd/migrate/v_0_1_4.go index 8464669a..b717c9b2 100644 --- a/cmd/migrate/v_0_1_4.go +++ b/cmd/migrate/v_0_1_4.go @@ -1,8 +1,8 @@ package migrate import ( - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/sequencer" + "github.com/dymensionxyz/roller/utils/config" ) type VersionMigratorV014 struct{} diff --git a/cmd/migrate/v_0_1_5.go b/cmd/migrate/v_0_1_5.go index 8c5d0843..49cd9e53 100644 --- a/cmd/migrate/v_0_1_5.go +++ b/cmd/migrate/v_0_1_5.go @@ -1,8 +1,8 @@ package migrate import ( - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/sequencer" + "github.com/dymensionxyz/roller/utils/config" ) type VersionMigratorV015 struct{} diff --git a/cmd/migrate/v_0_1_6.go b/cmd/migrate/v_0_1_6.go index 22c6b9d5..31592239 100644 --- a/cmd/migrate/v_0_1_6.go +++ b/cmd/migrate/v_0_1_6.go @@ -1,8 +1,8 @@ package migrate import ( - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/sequencer" + "github.com/dymensionxyz/roller/utils/config" ) type VersionMigratorV016 struct{} diff --git a/cmd/migrate/v_0_1_8.go b/cmd/migrate/v_0_1_8.go index f35b7ca2..c1206d8c 100644 --- a/cmd/migrate/v_0_1_8.go +++ b/cmd/migrate/v_0_1_8.go @@ -1,9 +1,9 @@ package migrate import ( - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/sequencer" "github.com/dymensionxyz/roller/utils" + "github.com/dymensionxyz/roller/utils/config" ) type VersionMigratorV018 struct{} diff --git a/cmd/relayer/run/run.go b/cmd/relayer/run/run.go index 156e95a9..cd628811 100644 --- a/cmd/relayer/run/run.go +++ b/cmd/relayer/run/run.go @@ -14,11 +14,14 @@ import ( initconfig "github.com/dymensionxyz/roller/cmd/config/init" "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/relayer" "github.com/dymensionxyz/roller/sequencer" globalutils "github.com/dymensionxyz/roller/utils" + "github.com/dymensionxyz/roller/utils/bash" + config2 "github.com/dymensionxyz/roller/utils/config" + "github.com/dymensionxyz/roller/utils/config/tomlconfig" dymintutils "github.com/dymensionxyz/roller/utils/dymint" + "github.com/dymensionxyz/roller/utils/errorhandling" rollapputils "github.com/dymensionxyz/roller/utils/rollapp" ) @@ -40,7 +43,7 @@ func Cmd() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { home, _ := globalutils.ExpandHomePath(cmd.Flag(utils.FlagNames.Home).Value.String()) relayerHome := filepath.Join(home, consts.ConfigDirName.Relayer) - rollappConfig, err := config.LoadRollerConfigFromTOML(home) + rollappConfig, err := tomlconfig.LoadRollerConfig(home) if err != nil { pterm.Error.Printf("failed to load rollapp config: %v\n", err) return @@ -163,7 +166,7 @@ func Cmd() *cobra.Command { } logFileOption := utils.WithLoggerLogging(relayerLogger) - utils.RequireMigrateIfNeeded(rollappConfig) + errorhandling.RequireMigrateIfNeeded(rollappConfig) err = rollappConfig.Validate() if err != nil { @@ -240,7 +243,7 @@ func Cmd() *cobra.Command { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - go utils.RunBashCmdAsync( + go bash.RunCmdAsync( ctx, rly.GetStartCmd(), func() {}, @@ -262,7 +265,7 @@ func Cmd() *cobra.Command { return relayerStartCmd } -func VerifyRelayerBalances(rolCfg config.RollappConfig) error { +func VerifyRelayerBalances(rolCfg config2.RollappConfig) error { insufficientBalances, err := GetRelayerInsufficientBalances(rolCfg) if err != nil { return err @@ -273,7 +276,7 @@ func VerifyRelayerBalances(rolCfg config.RollappConfig) error { } func GetRlyHubInsufficientBalances( - config config.RollappConfig, + config config2.RollappConfig, ) ([]utils.NotFundedAddressData, error) { HubRlyAddr, err := utils.GetRelayerAddress(config.Home, config.HubData.ID) if err != nil { @@ -310,7 +313,7 @@ func GetRlyHubInsufficientBalances( } func GetRelayerInsufficientBalances( - config config.RollappConfig, + config config2.RollappConfig, ) ([]utils.NotFundedAddressData, error) { insufficientBalances, err := GetRlyHubInsufficientBalances(config) if err != nil { diff --git a/cmd/relayer/start/start.go b/cmd/relayer/start/start.go index 64d44e47..30b5d7ab 100644 --- a/cmd/relayer/start/start.go +++ b/cmd/relayer/start/start.go @@ -9,9 +9,12 @@ import ( "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/relayer" "github.com/dymensionxyz/roller/sequencer" + "github.com/dymensionxyz/roller/utils/bash" + "github.com/dymensionxyz/roller/utils/config" + "github.com/dymensionxyz/roller/utils/config/tomlconfig" + "github.com/dymensionxyz/roller/utils/errorhandling" ) // TODO: Test relaying on 35-C and update the prices @@ -30,9 +33,9 @@ func Start() *cobra.Command { Short: "Starts a relayer between the Dymension hub and the rollapp.", Run: func(cmd *cobra.Command, args []string) { home := cmd.Flag(utils.FlagNames.Home).Value.String() - rollappConfig, err := config.LoadRollerConfigFromTOML(home) - utils.PrettifyErrorIfExists(err) - utils.RequireMigrateIfNeeded(rollappConfig) + rollappConfig, err := tomlconfig.LoadRollerConfig(home) + errorhandling.PrettifyErrorIfExists(err) + errorhandling.RequireMigrateIfNeeded(rollappConfig) VerifyRelayerBalances(rollappConfig) relayerLogFilePath := utils.GetRelayerLogPath(rollappConfig) @@ -46,7 +49,7 @@ func Start() *cobra.Command { rly.SetLogger(logger) _, _, err = rly.LoadActiveChannel() - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) override := cmd.Flag(flagOverride).Changed if override { @@ -56,16 +59,16 @@ func Start() *cobra.Command { fmt.Println("💈 IBC transfer channel is already established!") status := fmt.Sprintf("Active src, %s <-> %s, dst", rly.SrcChannel, rly.DstChannel) err := rly.WriteRelayerStatus(status) - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) } else { fmt.Println("💈 Establishing IBC transfer channel") seq := sequencer.GetInstance(rollappConfig) _, err := rly.CreateIBCChannel(override, logFileOption, seq) - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) } ctx, cancel := context.WithCancel(context.Background()) defer cancel() - go utils.RunBashCmdAsync( + go bash.RunCmdAsync( ctx, rly.GetStartCmd(), func() {}, @@ -89,7 +92,7 @@ func Start() *cobra.Command { func VerifyRelayerBalances(rolCfg config.RollappConfig) { insufficientBalances, err := GetRelayerInsufficientBalances(rolCfg) - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) utils.PrintInsufficientBalancesIfAny(insufficientBalances) } diff --git a/cmd/relayer/status/status.go b/cmd/relayer/status/status.go index 57923d36..57fdbb57 100644 --- a/cmd/relayer/status/status.go +++ b/cmd/relayer/status/status.go @@ -8,8 +8,9 @@ import ( "github.com/spf13/cobra" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/relayer" + "github.com/dymensionxyz/roller/utils/config/tomlconfig" + "github.com/dymensionxyz/roller/utils/errorhandling" ) func Cmd() *cobra.Command { @@ -18,8 +19,8 @@ func Cmd() *cobra.Command { Short: "Show the status of the relayer on the local machine.", Run: func(cmd *cobra.Command, args []string) { home := cmd.Flag(utils.FlagNames.Home).Value.String() - rollappConfig, err := config.LoadRollerConfigFromTOML(home) - utils.PrettifyErrorIfExists(err) + rollappConfig, err := tomlconfig.LoadRollerConfig(home) + errorhandling.PrettifyErrorIfExists(err) rly := relayer.NewRelayer( rollappConfig.Home, rollappConfig.RollappID, @@ -33,7 +34,7 @@ func Cmd() *cobra.Command { return } } else { - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) } fmt.Println(string(bytes)) }, diff --git a/cmd/rollapp/init/hubgenesis.go b/cmd/rollapp/init/hubgenesis.go index 0fda4e3b..1997689f 100644 --- a/cmd/rollapp/init/hubgenesis.go +++ b/cmd/rollapp/init/hubgenesis.go @@ -6,7 +6,7 @@ import ( "regexp" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/cmd/utils" + "github.com/dymensionxyz/roller/utils/bash" ) const ( @@ -29,7 +29,7 @@ func getDebugAddrCmd(a string) *exec.Cmd { func GetHubGenesisAddress() (string, error) { cmd := getDebugAddrCmd(hubgenesisAddr) - o, err := utils.ExecBashCommandWithStdout(cmd) + o, err := bash.ExecCommandWithStdout(cmd) if err != nil { return "", err } diff --git a/cmd/rollapp/init/main.go b/cmd/rollapp/init/main.go index 2390a12f..3d6337da 100644 --- a/cmd/rollapp/init/main.go +++ b/cmd/rollapp/init/main.go @@ -11,7 +11,7 @@ import ( func Cmd() *cobra.Command { cmd := &cobra.Command{ - Use: "init [path-to-config-archive]", + Use: "init [rollapp-id]", Short: "Inititlize a RollApp", Long: ``, Args: cobra.MaximumNArgs(1), @@ -22,30 +22,23 @@ func Cmd() *cobra.Command { return } - if len(args) != 0 { - archivePath, err := checkConfigArchive(args[0]) - if err != nil { - fmt.Printf("failed to get archive: %v\n", err) - return - } - - err = runInit(cmd, WithConfig(archivePath)) - if err != nil { - fmt.Printf("failed to initialize the RollApp: %v\n", err) - return - } - - return - } - options := []string{"mock", "dymension"} backend, _ := pterm.DefaultInteractiveSelect. WithDefaultText("select the settlement layer backend"). WithOptions(options). Show() + var raID string + if len(args) != 0 { + raID = args[0] + } else { + raID, _ = pterm.DefaultInteractiveTextInput.WithDefaultText( + "provide a rollapp ID that you want to run the node for", + ).Show() + } + if backend == "mock" { - err := runInit(cmd, WithMockSettlement()) + err := runInit(cmd, "mock", raID) if err != nil { fmt.Println("failed to run init: ", err) return @@ -53,29 +46,13 @@ func Cmd() *cobra.Command { return } - hasConfig, _ := pterm.DefaultInteractiveConfirm.WithDefaultText( - "do you have an existing configuration archive?", - ).Show() - - if !hasConfig { - fmt.Println( - `To generate a RollApp configuration file go to -or run 'rollapp config' to expose the UI on localhost:11133. -after configuration files are generated, rerun the 'init' command`, - ) - return - } - - fp, _ := pterm.DefaultInteractiveTextInput.WithDefaultText("provide the configuration archive path"). + envs := []string{"devnet", "testnet", "mainnet"} + env, _ := pterm.DefaultInteractiveSelect. + WithDefaultText("select the node type you want to run"). + WithOptions(envs). Show() - archivePath, err := checkConfigArchive(fp) - if err != nil { - fmt.Printf("failed to get archive: %v\n", err) - return - } - - err = runInit(cmd, WithConfig(archivePath)) + err = runInit(cmd, env, raID) if err != nil { fmt.Printf("failed to initialize the RollApp: %v\n", err) return diff --git a/cmd/rollapp/init/utils.go b/cmd/rollapp/init/utils.go index 950cfe21..3670561a 100644 --- a/cmd/rollapp/init/utils.go +++ b/cmd/rollapp/init/utils.go @@ -13,61 +13,31 @@ import ( initconfig "github.com/dymensionxyz/roller/cmd/config/init" "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" + cmdutils "github.com/dymensionxyz/roller/cmd/utils" datalayer "github.com/dymensionxyz/roller/data_layer" globalutils "github.com/dymensionxyz/roller/utils" - "github.com/dymensionxyz/roller/utils/archives" + "github.com/dymensionxyz/roller/utils/config/tomlconfig" + "github.com/dymensionxyz/roller/utils/errorhandling" ) -type Options struct { - configArchivePath string - useMockSettlement bool -} - -type Option func(*Options) - -func WithConfig(configArchivePath string) Option { - return func(o *Options) { - o.configArchivePath = configArchivePath - } -} - -func WithMockSettlement() Option { - return func(o *Options) { - o.useMockSettlement = true - } -} - -func defaultOptions() Options { - return Options{ - configArchivePath: "", - } -} - // in runInit I parse the entire genesis creator zip file twice to extract // the file this looks awful but since the archive has only 2 files it's // kinda fine -func runInit(cmd *cobra.Command, opts ...Option) error { - options := defaultOptions() - for _, opt := range opts { - opt(&options) - } - - home, err := globalutils.ExpandHomePath(cmd.Flag(utils.FlagNames.Home).Value.String()) +func runInit(cmd *cobra.Command, env string, raID string) error { + home, err := globalutils.ExpandHomePath(cmd.Flag(cmdutils.FlagNames.Home).Value.String()) if err != nil { pterm.Error.Println("failed to expand home directory") return err } outputHandler := initconfig.NewOutputHandler(false) - configArchivePath := options.configArchivePath defer outputHandler.StopSpinner() // TODO: extract into util isRootExist, err := globalutils.DirNotEmpty(home) if err != nil { - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) return err } @@ -75,13 +45,19 @@ func runInit(cmd *cobra.Command, opts ...Option) error { outputHandler.StopSpinner() shouldOverwrite, err := outputHandler.PromptOverwriteConfig(home) if err != nil { - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) return err } if shouldOverwrite { err = os.RemoveAll(home) if err != nil { - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) + return err + } + // nolint:gofumpt + err = os.MkdirAll(home, 0o755) + if err != nil { + errorhandling.PrettifyErrorIfExists(err) return err } } else { @@ -89,43 +65,20 @@ func runInit(cmd *cobra.Command, opts ...Option) error { } } - // nolint:gofumpt - err = os.MkdirAll(home, 0o755) - if err != nil { - utils.PrettifyErrorIfExists(err) - return err - } - - if configArchivePath != "" { - err = archives.ExtractFileFromNestedTar( - configArchivePath, - config.RollerConfigFileName, - home, - ) - if err != nil { - return err - } - } else { - mockRollerConfig := NewMockRollerConfig(cmd) - err := WriteMockRollerconfigToFile(mockRollerConfig) - if err != nil { - return err - } - } + // initConfigPtr, err := initconfig.GetInitConfig(cmd, options.useMockSettlement) - initConfigPtr, err := initconfig.GetInitConfig(cmd, options.useMockSettlement) + initConfigPtr, err := tomlconfig.LoadRollappMetadataFromChain(home, raID) if err != nil { - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) return err } - initConfig := *initConfigPtr - utils.RunOnInterrupt(outputHandler.StopSpinner) + errorhandling.RunOnInterrupt(outputHandler.StopSpinner) outputHandler.StartSpinner(consts.SpinnerMsgs.UniqueIdVerification) err = initConfig.Validate() if err != nil { - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) return err } @@ -135,7 +88,7 @@ func runInit(cmd *cobra.Command, opts ...Option) error { /* ------------------------------ Generate keys ----------------------------- */ addresses, err := initconfig.GenerateSequencersKeys(initConfig) if err != nil { - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) return err } @@ -167,17 +120,18 @@ func runInit(cmd *cobra.Command, opts ...Option) error { return err } - if configArchivePath != "" { - // genesis creator archive - err = archives.ExtractFileFromNestedTar( - configArchivePath, - "genesis.json", - filepath.Join(home, consts.ConfigDirName.Rollapp, "config"), - ) - if err != nil { - return err - } - } + // TODO: where to retrieve genesis from? + // if configArchivePath != "" { + // // genesis creator archive + // err = archives.ExtractFileFromNestedTar( + // configArchivePath, + // "genesis.json", + // filepath.Join(home, consts.ConfigDirName.Rollapp, "config"), + // ) + // if err != nil { + // return err + // } + // } // adds the sequencer address to the whitelists err = initconfig.UpdateGenesisParams(home, &initConfig) @@ -192,50 +146,27 @@ func runInit(cmd *cobra.Command, opts ...Option) error { return err } - err = globalutils.UpdateFieldInToml(rollerConfigFilePath, "HubData.ID", initConfig.HubData.ID) - if err != nil { - fmt.Println("failed to add HubData.ID to roller.toml: ", err) - return err - } - - err = globalutils.UpdateFieldInToml( - rollerConfigFilePath, - "HubData.rpc_url", - initConfig.HubData.RPC_URL, - ) - if err != nil { - fmt.Println("failed to add HubData.RpcUrl to roller.toml: ", err) - return err - } - - err = globalutils.UpdateFieldInToml( - rollerConfigFilePath, - "HubData.gas_price", - initConfig.HubData.GAS_PRICE, - ) - if err != nil { - fmt.Println("failed to add HubData.GasPrices to roller.toml: ", err) - return err - } - - err = globalutils.UpdateFieldInToml( - rollerConfigFilePath, - "da", - strings.ToLower(string(initConfig.DA)), - ) - if err != nil { - fmt.Println("failed to add HubData.RpcUrl to roller.toml: ", err) - return err + hd := consts.Hubs[env] + rollerTomlData := map[string]string{ + "HubData.ID": hd.ID, + "HubData.api_url": hd.API_URL, + "HubData.rpc_url": hd.RPC_URL, + "HubData.archive_rpc_url": hd.ARCHIVE_RPC_URL, + "HubData.gas_price": hd.GAS_PRICE, + "da": strings.ToLower(string(initConfig.DA)), + "rollapp_binary": strings.ToLower(consts.Executables.RollappEVM), } - err = globalutils.UpdateFieldInToml( - rollerConfigFilePath, - "rollapp_binary", - strings.ToLower(consts.Executables.RollappEVM), - ) - if err != nil { - fmt.Println("failed to add HubData.RpcUrl to roller.toml: ", err) - return err + for key, value := range rollerTomlData { + err = globalutils.UpdateFieldInToml( + rollerConfigFilePath, + key, + value, + ) + if err != nil { + fmt.Printf("failed to add %s to roller.toml: %v", key, err) + return err + } } /* ------------------------------ Create Init Files ---------------------------- */ @@ -247,7 +178,7 @@ func runInit(cmd *cobra.Command, opts ...Option) error { // TODO: review, roller config is generated using genesis-creator // some of the config values should be moved there - // err = config.WriteConfigToTOML(initConfig) + // err = config.Write(initConfig) // if err != nil { // return err // } diff --git a/cmd/rollapp/rollapp.go b/cmd/rollapp/rollapp.go index d5b321de..9d511960 100644 --- a/cmd/rollapp/rollapp.go +++ b/cmd/rollapp/rollapp.go @@ -6,6 +6,7 @@ import ( "github.com/dymensionxyz/roller/cmd/config" initrollapp "github.com/dymensionxyz/roller/cmd/rollapp/init" "github.com/dymensionxyz/roller/cmd/rollapp/run" + "github.com/dymensionxyz/roller/cmd/rollapp/start" "github.com/dymensionxyz/roller/cmd/rollapp/status" ) @@ -17,6 +18,7 @@ func Cmd() *cobra.Command { cmd.AddCommand(initrollapp.Cmd()) cmd.AddCommand(status.Cmd()) + cmd.AddCommand(start.Cmd()) cmd.AddCommand(config.Cmd()) cmd.AddCommand(run.Cmd()) diff --git a/cmd/rollapp/run/run.go b/cmd/rollapp/run/run.go index 7486ca2e..958d7e33 100644 --- a/cmd/rollapp/run/run.go +++ b/cmd/rollapp/run/run.go @@ -2,25 +2,34 @@ package run import ( "context" + "encoding/json" + "errors" "fmt" + "io/ioutil" "math/big" "os" "os/exec" "path/filepath" + "regexp" "strings" cosmossdkmath "cosmossdk.io/math" cosmossdktypes "github.com/cosmos/cosmos-sdk/types" + github_com_cosmos_cosmos_sdk_types "github.com/cosmos/cosmos-sdk/types" + dymensionseqtypes "github.com/dymensionxyz/dymension/v3/x/sequencer/types" "github.com/pterm/pterm" "github.com/spf13/cobra" initconfig "github.com/dymensionxyz/roller/cmd/config/init" "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" datalayer "github.com/dymensionxyz/roller/data_layer" "github.com/dymensionxyz/roller/sequencer" globalutils "github.com/dymensionxyz/roller/utils" + "github.com/dymensionxyz/roller/utils/bash" + "github.com/dymensionxyz/roller/utils/config" + tomlconfig "github.com/dymensionxyz/roller/utils/config/tomlconfig" + "github.com/dymensionxyz/roller/utils/errorhandling" rollapputils "github.com/dymensionxyz/roller/utils/rollapp" sequencerutils "github.com/dymensionxyz/roller/utils/sequencer" ) @@ -36,8 +45,8 @@ var ( // nolint:gocyclo func Cmd() *cobra.Command { cmd := &cobra.Command{ - Use: "run", - Short: "Initialize RollApp locally", + Use: "run [rollapp-id]", + Short: "Run the RollApp nodes", Long: ``, Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { @@ -47,16 +56,25 @@ func Cmd() *cobra.Command { return } + var raID string + if len(args) != 0 { + raID = args[0] + } else { + raID, _ = pterm.DefaultInteractiveTextInput.WithDefaultText( + "provide a rollapp ID that you want to run the node for", + ).Show() + } + home, err := globalutils.ExpandHomePath(cmd.Flag(utils.FlagNames.Home).Value.String()) if err != nil { pterm.Error.Println("failed to expand home directory") return } - rollappConfig, err := config.LoadRollerConfigFromTOML(home) - utils.PrettifyErrorIfExists(err) + rollappConfig, err := tomlconfig.LoadRollappMetadataFromChain(home, raID) + errorhandling.PrettifyErrorIfExists(err) - seq := sequencer.GetInstance(rollappConfig) + seq := sequencer.GetInstance(*rollappConfig) startRollappCmd := seq.GetStartCmd() LogPath = filepath.Join( @@ -69,137 +87,220 @@ func Cmd() *cobra.Command { if rollappConfig.HubData.ID == "mock" { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - go utils.RunBashCmdAsync( + go bash.RunCmdAsync( ctx, startRollappCmd, func() { - printOutput(rollappConfig, startRollappCmd) + printOutput(*rollappConfig, startRollappCmd) err := createPidFile(RollappDirPath, startRollappCmd) if err != nil { pterm.Warning.Println("failed to create pid file") } }, parseError, - utils.WithLogging(utils.GetSequencerLogPath(rollappConfig)), + utils.WithLogging(utils.GetSequencerLogPath(*rollappConfig)), ) select {} } options := []string{"sequencer", "fullnode"} nodeType, _ := pterm.DefaultInteractiveSelect. - WithDefaultText("select the settlement layer backend"). + WithDefaultText("select the node type you want to run"). WithOptions(options). Show() switch nodeType { case "sequencer": pterm.Info.Println("getting the existing sequencer address ") - hubSeqKC := utils.KeyConfig{ Dir: filepath.Join(rollappConfig.Home, consts.ConfigDirName.HubKeys), ID: consts.KeysIds.HubSequencer, ChainBinary: consts.Executables.Dymension, - Type: config.SDK_ROLLAPP, + Type: consts.SDK_ROLLAPP, } seqAddrInfo, err := utils.GetAddressInfoBinary(hubSeqKC, hubSeqKC.ChainBinary) if err != nil { pterm.Error.Println("failed to get address info: ", err) return } - seqAddrInfo.Address = strings.TrimSpace(seqAddrInfo.Address) - balance, err := utils.QueryBalance( - utils.ChainQueryConfig{ - Denom: consts.Denoms.Hub, - RPC: rollappConfig.HubData.RPC_URL, - Binary: consts.Executables.Dymension, - }, seqAddrInfo.Address, - ) - if err != nil { - pterm.Error.Println("failed to get address balance: ", err) - return - } - minBond, _ := sequencerutils.GetMinSequencerBond() - var bondAmount cosmossdktypes.Coin - bondAmount.Denom = consts.Denoms.Hub - - var desiredBond cosmossdktypes.Coin - desiredBondAmount, _ := pterm.DefaultInteractiveTextInput.WithDefaultText( - fmt.Sprintf( - "what is your desired bond amount? ( min: %s ) press enter to proceed with %s", - minBond.String(), - minBond.String(), - ), - ).WithDefaultValue(minBond.Amount.String()).Show() - - if strings.TrimSpace(desiredBondAmount) == "" { - desiredBond = *minBond - } else { - desiredBondAmountInt, ok := cosmossdkmath.NewIntFromString(desiredBondAmount) - if !ok { - pterm.Error.Printf("failed to convert %s to int\n", desiredBondAmount) - return - } + // check whether the address is registered as sequencer + pterm.Info.Printf( + "checking whether sequencer is already registered for %s\n", + rollappConfig.RollappID, + ) - desiredBond.Denom = consts.Denoms.Hub - desiredBond.Amount = desiredBondAmountInt + seq, err := rollapputils.GetRegisteredSequencers(rollappConfig.RollappID) + if err != nil { + pterm.Error.Println("failed to retrieve registered sequencers: ", err) } - isAddrFunded := balance.Amount.Cmp(minBond.Amount.BigInt()) == 1 - - isInitialSequencer, err := rollapputils.IsInitialSequencer( + isSequencerRegistered := sequencerutils.IsRegisteredAsSequencer( + seq.Sequencers, seqAddrInfo.Address, - rollappConfig.RollappID, ) - if err != nil { - fmt.Println(err) - } - if !isAddrFunded { - pterm.DefaultSection.WithIndentCharacter("🔔"). - Println("Please fund the addresses below to register and run the sequencer.") - seqAddrInfo.Print(utils.WithName()) - proceed, _ := pterm.DefaultInteractiveConfirm.WithDefaultValue(true). - WithDefaultText( - "press enter when funded", - ).Show() + if !isSequencerRegistered { + minBond, _ := sequencerutils.GetMinSequencerBond() + var bondAmount cosmossdktypes.Coin + bondAmount.Denom = consts.Denoms.Hub + + var desiredBond cosmossdktypes.Coin + desiredBondAmount, _ := pterm.DefaultInteractiveTextInput.WithDefaultText( + fmt.Sprintf( + "what is your desired bond amount? ( min: %s ) press enter to proceed with %s", + minBond.String(), + minBond.String(), + ), + ).WithDefaultValue(minBond.Amount.String()).Show() + + if strings.TrimSpace(desiredBondAmount) == "" { + desiredBond = *minBond + } else { + desiredBondAmountInt, ok := cosmossdkmath.NewIntFromString(desiredBondAmount) + if !ok { + pterm.Error.Printf("failed to convert %s to int\n", desiredBondAmount) + return + } - if !proceed { - return + desiredBond.Denom = consts.Denoms.Hub + desiredBond.Amount = desiredBondAmountInt } - } - if isInitialSequencer { - pterm.Info.Printf( - "the %s ( %s ) address matches the initial sequencer address of the %s\n", - seqAddrInfo.Name, - seqAddrInfo.Address, - rollappConfig.RollappID, - ) - pterm.Info.Println( - "checking whether sequencer is already registered", - rollappConfig.RollappID, + pterm.Info.Println("getting the existing sequencer address balance") + balance, err := utils.QueryBalance( + utils.ChainQueryConfig{ + Denom: consts.Denoms.Hub, + RPC: rollappConfig.HubData.RPC_URL, + Binary: consts.Executables.Dymension, + }, seqAddrInfo.Address, ) - - seq, err := rollapputils.GetRegisteredSequencers(rollappConfig.RollappID) if err != nil { - pterm.Error.Println("failed to retrieve registered sequencers: ", err) + pterm.Error.Println("failed to get address balance: ", err) + return } - isInitialSequencerRegistered := sequencerutils.IsRegisteredAsSequencer( - seq.Sequencers, - seqAddrInfo.Address, + // TODO: use NotFundedAddressData instead + var necessaryBalance big.Int + necessaryBalance.Add( + desiredBond.Amount.BigInt(), + cosmossdkmath.NewInt(consts.DefaultFee).BigInt(), ) - if !isInitialSequencerRegistered { - pterm.Info.Println( - "initial sequencer address is not registered for ", - rollappConfig.RollappID, - ) - err = sequencerutils.Register(rollappConfig) - if err != nil { - pterm.Error.Println("failed to register sequencer: ", err) + pterm.Info.Printf( + "current balance: %s\nnecessary balance: %s\n", + balance.Amount.String(), + necessaryBalance.String(), + ) + + // check whether balance is bigger or equal to the necessaryBalance + isAddrFunded := balance.Amount.Cmp(&necessaryBalance) == 1 || + balance.Amount.Cmp( + &necessaryBalance, + ) == 0 + + if !isAddrFunded { + pterm.DefaultSection.WithIndentCharacter("🔔"). + Println("Please fund the addresses below to register and run the sequencer.") + seqAddrInfo.Print(utils.WithName()) + proceed, _ := pterm.DefaultInteractiveConfirm.WithDefaultValue(true). + WithDefaultText( + "press enter when funded", + ).Show() + + if !proceed { return } } + + // isInitialSequencer, err := rollapputils.IsInitialSequencer( + // seqAddrInfo.Address, + // rollappConfig.RollappID, + // ) + // if err != nil { + // pterm.Error.Printf( + // "failed to check whether %s is the initial sequencer\n", + // seqAddrInfo.Address, + // ) + // } + + // if isInitialSequencer { + // pterm.Info.Printf( + // "the %s ( %s ) address matches the initial sequencer address of the %s\n", + // seqAddrInfo.Name, + // seqAddrInfo.Address, + // rollappConfig.RollappID, + // ) + // pterm.Info.Printf( + // "initial sequencer address is not registered for %s\n", + // rollappConfig.RollappID, + // ) + + err = populateSequencerMetadata(*rollappConfig) + if err != nil { + pterm.Error.Println("failed to populate sequencer metadata: ", err) + return + } + + err = sequencerutils.Register(*rollappConfig) + if err != nil { + pterm.Error.Println("failed to register sequencer: ", err) + return + } + pterm.Info.Printf( + "%s ( %s ) is registered as a sequencer for %s\n", + seqAddrInfo.Name, + seqAddrInfo.Address, + rollappConfig.RollappID, + ) + // } else { + // pterm.Info.Printf( + // "%s ( %s ) is not the initial sequencer address\n", + // seqAddrInfo.Name, + // seqAddrInfo.Address, + // ) + // + // pterm.Info.Printf( + // "checking whether the initial sequencer is already registered for %s\n", + // rollappConfig.RollappID, + // ) + // initialSeqAddr, err := rollapputils.GetInitialSequencerAddress(rollappConfig.RollappID) + // if err != nil { + // pterm.Error.Println("failed to retrieve initial sequencer address: ", err) + // return + // } + // + // isInitialSequencerRegistered := sequencerutils.IsRegisteredAsSequencer( + // seq.Sequencers, + // initialSeqAddr, + // ) + // + // if !isInitialSequencerRegistered { + // pterm.Warning.Println("additional sequencers can only be added after the initial sequencer is registered") + // pterm.Info.Println("exiting") + // return + // } + // + // pterm.Info.Println( + // "initial sequencer is already registered, proceeding with creation of your sequencer", + // ) + // + // err = populateSequencerMetadata(rollappConfig) + // if err != nil { + // pterm.Error.Println("failed to populate sequencer metadata: ", err) + // return + // } + // err = sequencerutils.Register(rollappConfig) + // if err != nil { + // pterm.Error.Println("failed to register sequencer: ", err) + // return + // } + // pterm.Info.Printf( + // "%s ( %s ) is registered as a sequencer for %s\n", + // seqAddrInfo.Name, + // seqAddrInfo.Address, + // rollappConfig.RollappID, + // ) + // } + } else { pterm.Info.Printf( "%s ( %s ) is registered as a sequencer for %s\n", seqAddrInfo.Name, @@ -207,8 +308,35 @@ func Cmd() *cobra.Command { rollappConfig.RollappID, ) } + case "fullnode": - pterm.Info.Println("getting the fullnode address ") + pterm.Info.Println("retrieving the latest available snapshot") + si, err := sequencerutils.GetLatestSnapshot(rollappConfig.RollappID) + if err != nil { + pterm.Error.Println("failed to retrieve ") + } + + if si == nil { + pterm.Warning.Printf( + "no snapshots were found for %s\n", + rollappConfig.RollappID, + ) + } else { + fmt.Printf( + "found a snapshot for height %s\nchecksum: %s\nurl: %s", + si.Height, + si.Checksum, + si.SnapshotUrl, + ) + } + + options := []string{"p2p", "da"} + syncMode, _ := pterm.DefaultInteractiveSelect. + WithDefaultText("select the desired syncing mode"). + WithOptions(options). + Show() + + fmt.Printf("the node is set to sync from %s\n", syncMode) } // DA @@ -226,6 +354,7 @@ func Cmd() *cobra.Command { var shouldOverwrite bool if isDaInitialized { + pterm.Info.Println("DA client is already initialized") oh.StopSpinner() shouldOverwrite, err = oh.PromptOverwriteConfig(daHome) if err != nil { @@ -237,7 +366,7 @@ func Cmd() *cobra.Command { pterm.Info.Println("overriding the existing da configuration") err := os.RemoveAll(daHome) if err != nil { - pterm.Error.Printf("failed to recuresively remove %s: %v\n", daHome, err) + pterm.Error.Printf("failed to remove %s: %v\n", daHome, err) return } @@ -249,63 +378,79 @@ func Cmd() *cobra.Command { } if !isDaInitialized || shouldOverwrite { - if rollappConfig.DA == "celestia" { - mnemonic, err := damanager.InitializeLightNodeConfig() - if err != nil { - pterm.Error.Println("failed to initialize da light client: ", err) - return - } + mnemonic, err := damanager.InitializeLightNodeConfig() + if err != nil { + pterm.Error.Println("failed to initialize da light client: ", err) + return + } - daWalletInfo, err := damanager.GetDAAccountAddress() - if err != nil { - pterm.Error.Println("failed to retrieve da wallet address: ", err) - return - } - daWalletInfo.Mnemonic = mnemonic + daWalletInfo, err := damanager.GetDAAccountAddress() + if err != nil { + pterm.Error.Println("failed to retrieve da wallet address: ", err) + return + } + daWalletInfo.Mnemonic = mnemonic + daWalletInfo.Print(utils.WithMnemonic(), utils.WithName()) + } - if nodeType == "sequencer" { - pterm.DefaultSection.WithIndentCharacter("🔔"). - Println("Please fund the addresses below to register and run the sequencer.") - daWalletInfo.Print(utils.WithMnemonic(), utils.WithName()) + var daConfig string + dymintConfigPath := sequencer.GetDymintFilePath(home) + daNamespace := damanager.DataLayer.GetNamespaceID() - proceed, _ := pterm.DefaultInteractiveConfirm.WithDefaultValue(true). - WithDefaultText( - "press enter when funded", - ).Show() + switch nodeType { + case "sequencer": + pterm.Info.Println("checking DA account balance") + insufficientBalances, err := damanager.CheckDABalance() + if err != nil { + pterm.Error.Println("failed to check balance", err) + } - pterm.Info.Println("updating dymint configuration") - daconfig := damanager.DataLayer.GetSequencerDAConfig() - dans := damanager.DataLayer.GetNamespaceID() - - dymintConfigPath := sequencer.GetDymintFilePath(home) - _ = globalutils.UpdateFieldInToml( - dymintConfigPath, - "da_layer", - string(rollappConfig.DA), - ) - _ = globalutils.UpdateFieldInToml( - dymintConfigPath, - "namespace_id", - dans, - ) - _ = globalutils.UpdateFieldInToml( - dymintConfigPath, - "da_config", - daconfig, - ) + utils.PrintInsufficientBalancesIfAny(insufficientBalances) + + // TODO: daconfig should be a struct + daConfig = damanager.DataLayer.GetSequencerDAConfig( + consts.NodeType.Sequencer, + ) + + case "fullnode": + daConfig = damanager.DataLayer.GetSequencerDAConfig( + consts.NodeType.FullNode, + ) + default: + pterm.Error.Println("unsupported node type") + return - if !proceed { - pterm.Info.Println("exiting") - return - } - } - } } - // node sync - // retrieve snapshot with the highest height + pterm.Info.Println("updating dymint configuration") + _ = globalutils.UpdateFieldInToml( + dymintConfigPath, + "da_layer", + string(rollappConfig.DA), + ) + _ = globalutils.UpdateFieldInToml( + dymintConfigPath, + "namespace_id", + daNamespace, + ) + _ = globalutils.UpdateFieldInToml( + dymintConfigPath, + "da_config", + daConfig, + ) - pterm.Info.Println("done") + pterm.Info.Println("initialization complete") + pterm.Info.Println("next steps:") + pterm.Info.Printf( + "run %s to create IBC channels and start relaying packets\n", + pterm.DefaultBasicText.WithStyle(pterm.FgYellow.ToStyle()). + Sprintf("roller relayer run"), + ) + pterm.Info.Printf( + "run %s load the necessary systemd services\n", + pterm.DefaultBasicText.WithStyle(pterm.FgYellow.ToStyle()). + Sprintf("roller services load"), + ) }, } @@ -359,3 +504,149 @@ func parseError(errMsg string) string { } return errMsg } + +func populateSequencerMetadata(raCfg config.RollappConfig) error { + cd := dymensionseqtypes.ContactDetails{ + Website: "", + Telegram: "", + X: "", + } + defaultGasPrice, ok := github_com_cosmos_cosmos_sdk_types.NewIntFromString( + raCfg.HubData.GAS_PRICE, + ) + if !ok { + return errors.New("failed to parse gas price") + } + + var defaultSnapshots []*dymensionseqtypes.SnapshotInfo + sm := dymensionseqtypes.SequencerMetadata{ + Moniker: "", + Details: "", + P2PSeeds: []string{}, + Rpcs: []string{}, + EvmRpcs: []string{}, + RestApiUrls: []string{}, + ExplorerUrl: "", + GenesisUrls: []string{}, + ContactDetails: &cd, + ExtraData: []byte{}, + Snapshots: defaultSnapshots, + GasPrice: &defaultGasPrice, + } + + path := filepath.Join( + raCfg.Home, + consts.ConfigDirName.Rollapp, + "init", + "sequencer-metadata.json", + ) + pterm.DefaultSection.WithIndentCharacter("🔔"). + Println("The following values are mandatory for sequencer creation") + + var rpc string + var rest string + var evmRpc string + + // todo: clean up + + for { + // Prompt the user for the RPC URL + rpc, _ = pterm.DefaultInteractiveTextInput.WithDefaultText( + "Enter a valid RPC endpoint (example: rpc.rollapp.dym.xyz)", + ).Show() + if !strings.HasPrefix(rpc, "http://") && !strings.HasPrefix(rpc, "https://") { + rpc = "https://" + rpc + } + + isValid := isValidURL(rpc) + + // Validate the URL + if !isValid { + pterm.Error.Println("Invalid URL. Please try again.") + } else { + // Valid URL, break out of the loop + break + } + } + + for { + // Prompt the user for the RPC URL + rest, _ = pterm.DefaultInteractiveTextInput.WithDefaultText( + "rest endpoint that you will provide (example: api.rollapp.dym.xyz", + ).Show() + if !strings.HasPrefix(rest, "http://") && !strings.HasPrefix(rest, "https://") { + rest = "https://" + rest + } + + isValid := isValidURL(rest) + + // Validate the URL + if !isValid { + pterm.Error.Println("Invalid URL. Please try again.") + } else { + // Valid URL, break out of the loop + break + } + } + + for { + // Prompt the user for the RPC URL + evmRpc, _ = pterm.DefaultInteractiveTextInput.WithDefaultText( + "evm evmRpc endpoint that you will provide (example: json-rpc.rollapp.dym.xyz", + ).Show() + if !strings.HasPrefix(evmRpc, "http://") && !strings.HasPrefix(evmRpc, "https://") { + evmRpc = "https://" + evmRpc + } + + isValid := isValidURL(evmRpc) + + // Validate the URL + if !isValid { + pterm.Error.Println("Invalid URL. Please try again.") + } else { + // Valid URL, break out of the loop + break + } + } + + sm.Rpcs = append(sm.Rpcs, rpc) + sm.RestApiUrls = append(sm.RestApiUrls, rest) + sm.EvmRpcs = append(sm.EvmRpcs, evmRpc) + + _, _ = pterm.DefaultInteractiveConfirm.WithDefaultText( + "Would you also like to fill optional metadata for your sequencer?", + ).Show() + + err := WriteStructToJSONFile(&sm, path) + if err != nil { + return err + } + return nil +} + +func isValidURL(url string) bool { + regex := `^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$` + re := regexp.MustCompile(regex) + return re.MatchString(url) +} + +func WriteStructToJSONFile(data *dymensionseqtypes.SequencerMetadata, filePath string) error { + // Marshal the struct into JSON + jsonData, err := json.Marshal(data) + if err != nil { + return fmt.Errorf("error marshalling JSON: %w", err) + } + + // Create the directory path if it doesn't exist + dir := filepath.Dir(filePath) + if err := os.MkdirAll(dir, os.ModePerm); err != nil { + return fmt.Errorf("error creating directories: %w", err) + } + + // Write the JSON data to the file + if err := ioutil.WriteFile(filePath, jsonData, 0o644); err != nil { + return fmt.Errorf("error writing to file: %w", err) + } + + return nil +} diff --git a/cmd/rollapp/start/start.go b/cmd/rollapp/start/start.go index 5dd6b922..0bec966a 100644 --- a/cmd/rollapp/start/start.go +++ b/cmd/rollapp/start/start.go @@ -1,76 +1,116 @@ package start -// import ( -// "fmt" -// "math/big" -// "os" -// "os/exec" -// "path/filepath" -// "strings" -// -// "github.com/spf13/cobra" -// -// "github.com/dymensionxyz/roller/config" -// "github.com/dymensionxyz/roller/sequencer" -// ) -// -// // TODO: Test sequencing on 35-C and update the price +import ( + "context" + "fmt" + "os" + "os/exec" + "path/filepath" + "strings" + + "github.com/pterm/pterm" + "github.com/spf13/cobra" + + initconfig "github.com/dymensionxyz/roller/cmd/config/init" + "github.com/dymensionxyz/roller/cmd/consts" + "github.com/dymensionxyz/roller/cmd/utils" + "github.com/dymensionxyz/roller/sequencer" + globalutils "github.com/dymensionxyz/roller/utils" + "github.com/dymensionxyz/roller/utils/bash" + "github.com/dymensionxyz/roller/utils/config" + "github.com/dymensionxyz/roller/utils/config/tomlconfig" + "github.com/dymensionxyz/roller/utils/errorhandling" +) + // var OneDaySequencePrice = big.NewInt(1) -// -// var ( -// RollappDirPath string -// LogPath string -// ) -// -// func Cmd() *cobra.Command { -// cmd := &cobra.Command{ -// Use: "start", -// Short: "Show the status of the sequencer on the local machine.", -// Run: func(cmd *cobra.Command, args []string) { -// }, -// } -// return cmd -// } -// -// func printOutput(rlpCfg config.RollappConfig, cmd *exec.Cmd) { -// seq := sequencer.GetInstance(rlpCfg) -// fmt.Println("💈 The Rollapp sequencer is running on your local machine!") -// fmt.Println("💈 Endpoints:") -// -// fmt.Printf("💈 EVM RPC: http://0.0.0.0:%v\n", seq.JsonRPCPort) -// fmt.Printf("💈 Node RPC: http://0.0.0.0:%v\n", seq.RPCPort) -// fmt.Printf("💈 Rest API: http://0.0.0.0:%v\n", seq.APIPort) -// -// fmt.Println("💈 Log file path: ", LogPath) -// fmt.Println("💈 Rollapp root dir: ", RollappDirPath) -// fmt.Println("💈 PID: ", cmd.Process.Pid) -// } -// -// func createPidFile(path string, cmd *exec.Cmd) error { -// pidPath := filepath.Join(path, "rollapp.pid") -// file, err := os.Create(pidPath) -// if err != nil { -// fmt.Println("Error creating file:", err) -// return err -// } -// // nolint errcheck -// defer file.Close() -// -// pid := cmd.Process.Pid -// _, err = file.WriteString(fmt.Sprintf("%d", pid)) -// if err != nil { -// fmt.Println("Error writing to file:", err) -// return err -// } -// -// return nil -// } -// -// func parseError(errMsg string) string { -// lines := strings.Split(errMsg, "\n") -// if len(lines) > 0 && -// lines[0] == "Error: failed to initialize database: resource temporarily unavailable" { -// return "The Rollapp sequencer is already running on your local machine. Only one sequencer can run at any given time." -// } -// return errMsg -// } + +var ( + RollappDirPath string + LogPath string +) + +func Cmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "start", + Short: "Show the status of the sequencer on the local machine.", + Run: func(cmd *cobra.Command, args []string) { + err := initconfig.AddFlags(cmd) + if err != nil { + pterm.Error.Println("failed to add flags") + return + } + home, err := globalutils.ExpandHomePath(cmd.Flag(utils.FlagNames.Home).Value.String()) + if err != nil { + pterm.Error.Println("failed to expand home directory") + return + } + + rollappConfig, err := tomlconfig.LoadRollerConfig(home) + errorhandling.PrettifyErrorIfExists(err) + + seq := sequencer.GetInstance(rollappConfig) + startRollappCmd := seq.GetStartCmd() + + LogPath = filepath.Join(rollappConfig.Home, consts.ConfigDirName.Rollapp, "rollapp.log") + RollappDirPath = filepath.Join(rollappConfig.Home, consts.ConfigDirName.Rollapp) + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + go bash.RunCmdAsync( + ctx, startRollappCmd, func() { + printOutput(rollappConfig, startRollappCmd) + err := createPidFile(RollappDirPath, startRollappCmd) + if err != nil { + pterm.Warning.Println("failed to create pid file") + } + }, parseError, + utils.WithLogging(utils.GetSequencerLogPath(rollappConfig)), + ) + select {} + }, + } + return cmd +} + +func printOutput(rlpCfg config.RollappConfig, cmd *exec.Cmd) { + seq := sequencer.GetInstance(rlpCfg) + fmt.Println("💈 The Rollapp sequencer is running on your local machine!") + fmt.Println("💈 Endpoints:") + + fmt.Printf("💈 EVM RPC: http://0.0.0.0:%v\n", seq.JsonRPCPort) + fmt.Printf("💈 Node RPC: http://0.0.0.0:%v\n", seq.RPCPort) + fmt.Printf("💈 Rest API: http://0.0.0.0:%v\n", seq.APIPort) + + fmt.Println("💈 Log file path: ", LogPath) + fmt.Println("💈 Rollapp root dir: ", RollappDirPath) + fmt.Println("💈 PID: ", cmd.Process.Pid) +} + +func createPidFile(path string, cmd *exec.Cmd) error { + pidPath := filepath.Join(path, "rollapp.pid") + file, err := os.Create(pidPath) + if err != nil { + fmt.Println("Error creating file:", err) + return err + } + // nolint errcheck + defer file.Close() + + pid := cmd.Process.Pid + _, err = file.WriteString(fmt.Sprintf("%d", pid)) + if err != nil { + fmt.Println("Error writing to file:", err) + return err + } + + return nil +} + +func parseError(errMsg string) string { + lines := strings.Split(errMsg, "\n") + if len(lines) > 0 && + lines[0] == "Error: failed to initialize database: resource temporarily unavailable" { + return "The Rollapp sequencer is already running on your local machine. Only one sequencer can run at any given time." + } + return errMsg +} diff --git a/cmd/rollapp/status/status.go b/cmd/rollapp/status/status.go index 834449ce..2a4634f7 100644 --- a/cmd/rollapp/status/status.go +++ b/cmd/rollapp/status/status.go @@ -6,8 +6,9 @@ import ( "github.com/spf13/cobra" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/sequencer" + "github.com/dymensionxyz/roller/utils/config/tomlconfig" + "github.com/dymensionxyz/roller/utils/errorhandling" ) func Cmd() *cobra.Command { @@ -16,12 +17,12 @@ func Cmd() *cobra.Command { Short: "Show the status of the sequencer on the local machine.", Run: func(cmd *cobra.Command, args []string) { home := cmd.Flag(utils.FlagNames.Home).Value.String() - rollappConfig, err := config.LoadRollerConfigFromTOML(home) + rollappConfig, err := tomlconfig.LoadRollerConfig(home) if err != nil { fmt.Println("failed to load config:", err) return } - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) seq := sequencer.GetInstance(rollappConfig) fmt.Println(seq.GetSequencerStatus(rollappConfig)) }, diff --git a/cmd/run/run.go b/cmd/run/run.go index 503cb8c8..892b991e 100644 --- a/cmd/run/run.go +++ b/cmd/run/run.go @@ -8,14 +8,16 @@ import ( "github.com/spf13/cobra" - relayer_run "github.com/dymensionxyz/roller/cmd/relayer/run" - rollapp_run "github.com/dymensionxyz/roller/cmd/rollapp/run" + relayerrun "github.com/dymensionxyz/roller/cmd/relayer/run" + rollapprun "github.com/dymensionxyz/roller/cmd/rollapp/run" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" datalayer "github.com/dymensionxyz/roller/data_layer" "github.com/dymensionxyz/roller/data_layer/celestia" "github.com/dymensionxyz/roller/relayer" "github.com/dymensionxyz/roller/sequencer" + "github.com/dymensionxyz/roller/utils/config" + "github.com/dymensionxyz/roller/utils/config/tomlconfig" + "github.com/dymensionxyz/roller/utils/errorhandling" servicemanager "github.com/dymensionxyz/roller/utils/service_manager" ) @@ -31,9 +33,9 @@ func Cmd() *cobra.Command { Short: "Runs the rollapp on the local machine.", Run: func(cmd *cobra.Command, args []string) { home := cmd.Flag(utils.FlagNames.Home).Value.String() - rollappConfig, err := config.LoadRollerConfigFromTOML(home) - utils.PrettifyErrorIfExists(err) - utils.RequireMigrateIfNeeded(rollappConfig) + rollappConfig, err := tomlconfig.LoadRollerConfig(home) + errorhandling.PrettifyErrorIfExists(err) + errorhandling.RequireMigrateIfNeeded(rollappConfig) logger := utils.GetRollerLogger(rollappConfig.Home) ctx, cancel := context.WithCancel(context.Background()) @@ -48,14 +50,14 @@ func Cmd() *cobra.Command { /* ------------------------------ run processes ----------------------------- */ seq := sequencer.GetInstance(rollappConfig) - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) runSequencerWithRestarts(seq, serviceConfig) runDaWithRestarts(rollappConfig, serviceConfig) runRelayerWithRestarts(rollappConfig, serviceConfig) /* ------------------------------ render output ----------------------------- */ noOutput, err := cmd.Flags().GetBool(flagNames.NoOutput) - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) if noOutput { select {} } else { @@ -65,7 +67,7 @@ func Cmd() *cobra.Command { spin := utils.GetLoadingSpinner() spin.Suffix = " Stopping rollapp services, please wait..." spin.Start() - utils.RunOnInterrupt(spin.Stop) + errorhandling.RunOnInterrupt(spin.Stop) waitingGroup.Wait() spin.Stop() }, @@ -74,7 +76,10 @@ func Cmd() *cobra.Command { return cmd } -func runRelayerWithRestarts(cfg config.RollappConfig, serviceConfig *servicemanager.ServiceConfig) { +func runRelayerWithRestarts( + cfg config.RollappConfig, + serviceConfig *servicemanager.ServiceConfig, +) { startRelayerCmd := getStartRelayerCmd(cfg) rly := relayer.NewRelayer(cfg.Home, cfg.RollappID, cfg.HubData.ID) @@ -136,16 +141,16 @@ func runSequencerWithRestarts( func verifyBalances(rollappConfig config.RollappConfig) { damanager := datalayer.NewDAManager(rollappConfig.DA, rollappConfig.Home) insufficientBalances, err := damanager.CheckDABalance() - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) sequencerInsufficientBalances, err := utils.GetSequencerInsufficientAddrs( - rollappConfig, rollapp_run.OneDaySequencePrice, + rollappConfig, rollapprun.OneDaySequencePrice, ) - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) insufficientBalances = append(insufficientBalances, sequencerInsufficientBalances...) - rlyAddrs, err := relayer_run.GetRlyHubInsufficientBalances(rollappConfig) - utils.PrettifyErrorIfExists(err) + rlyAddrs, err := relayerrun.GetRlyHubInsufficientBalances(rollappConfig) + errorhandling.PrettifyErrorIfExists(err) insufficientBalances = append(insufficientBalances, rlyAddrs...) utils.PrintInsufficientBalancesIfAny(insufficientBalances) } diff --git a/cmd/run/services_info.go b/cmd/run/services_info.go index e2e4ead5..45f5aa60 100644 --- a/cmd/run/services_info.go +++ b/cmd/run/services_info.go @@ -5,8 +5,8 @@ import ( "strings" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/sequencer" + "github.com/dymensionxyz/roller/utils/config" "github.com/gizak/termui/v3" "github.com/gizak/termui/v3/widgets" diff --git a/cmd/run/services_status.go b/cmd/run/services_status.go index cd2b90d6..1c07609e 100644 --- a/cmd/run/services_status.go +++ b/cmd/run/services_status.go @@ -5,11 +5,11 @@ import ( "path/filepath" "time" + "github.com/dymensionxyz/roller/utils/config" ui "github.com/gizak/termui/v3" "github.com/gizak/termui/v3/widgets" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" servicemanager "github.com/dymensionxyz/roller/utils/service_manager" ) diff --git a/cmd/run/ui.go b/cmd/run/ui.go index 40dea9fa..f0197c94 100644 --- a/cmd/run/ui.go +++ b/cmd/run/ui.go @@ -6,10 +6,10 @@ import ( "sort" "strings" - termui "github.com/gizak/termui/v3" + "github.com/gizak/termui/v3" "github.com/gizak/termui/v3/widgets" - "github.com/dymensionxyz/roller/config" + "github.com/dymensionxyz/roller/utils/config" servicemanager "github.com/dymensionxyz/roller/utils/service_manager" ) @@ -44,9 +44,11 @@ func updateUITable( cfg config.RollappConfig, ) { table.Rows = [][]string{{"Name", "Balance", "Status"}} - sort.Slice(serviceData, func(i, j int) bool { - return serviceData[i].Name < serviceData[j].Name - }) + sort.Slice( + serviceData, func(i, j int) bool { + return serviceData[i].Name < serviceData[j].Name + }, + ) for _, service := range serviceData { balances := []string{} for _, account := range service.Accounts { diff --git a/cmd/services/load/load.go b/cmd/services/load/load.go index 97e68dd0..fe65ccc1 100644 --- a/cmd/services/load/load.go +++ b/cmd/services/load/load.go @@ -6,13 +6,15 @@ import ( "fmt" "os" "os/exec" + "path/filepath" "runtime" "text/template" "github.com/spf13/cobra" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/cmd/utils" + "github.com/dymensionxyz/roller/utils/bash" + "github.com/dymensionxyz/roller/utils/errorhandling" ) type Service struct { @@ -32,28 +34,28 @@ func Cmd() *cobra.Command { Short: "Loads the different rollapp services on the local machine", Run: func(cmd *cobra.Command, args []string) { if runtime.GOOS != "linux" { - utils.PrettifyErrorIfExists( + errorhandling.PrettifyErrorIfExists( errors.New("the services commands are only available on linux machines"), ) } - for _, service := range []string{"sequencer", "da-light-client", "relayer"} { + for _, service := range []string{"rollapp", "da-light-client", "relayer"} { serviceData := ServiceTemplateData{ Name: service, ExecPath: consts.Executables.Roller, UserName: os.Getenv("USER"), } tpl, err := generateServiceTemplate(serviceData) - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) err = writeServiceFile(tpl, service) - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) } - _, err := utils.ExecBashCommandWithStdout( + _, err := bash.ExecCommandWithStdout( exec.Command("sudo", "systemctl", "daemon-reload"), ) - utils.PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) fmt.Println( - "💈 Services 'sequencer', 'da-light-client' and 'relayer' been loaded successfully." + - " To start them, use 'systemctl start '.", + "💈 Services 'sequencer', 'da-light-client' and 'relayer' been loaded successfully.", + // " To start them, use 'systemctl start '.", ) }, } @@ -61,9 +63,13 @@ func Cmd() *cobra.Command { } func writeServiceFile(serviceTxt *bytes.Buffer, serviceName string) error { - filePath := fmt.Sprintf("/etc/systemd/system/%s.service", serviceName) - cmd := exec.Command("bash", "-c", fmt.Sprintf("echo '%s' | sudo tee %s", - serviceTxt.String(), filePath)) + filePath := filepath.Join("/etc/systemd/system/", fmt.Sprintf("%s.service", serviceName)) + cmd := exec.Command( + "bash", "-c", fmt.Sprintf( + "echo '%s' | sudo tee %s", + serviceTxt.String(), filePath, + ), + ) // Need to start and wait instead of run to allow sudo to prompt for password err := cmd.Start() if err != nil { diff --git a/cmd/tx/claim/claim.go b/cmd/tx/claim/claim.go index 55b67dac..ca72a152 100644 --- a/cmd/tx/claim/claim.go +++ b/cmd/tx/claim/claim.go @@ -10,6 +10,7 @@ import ( "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" + "github.com/dymensionxyz/roller/utils/bash" ) func Cmd() *cobra.Command { @@ -41,7 +42,7 @@ func Cmd() *cobra.Command { consts.Executables.Simd, "keys", "import-hex", consts.KeysIds.HubSequencer, args[0], "--home", tempDir, ) - _, err = utils.ExecBashCommandWithStdout(importKeyCmd) + _, err = bash.ExecCommandWithStdout(importKeyCmd) if err != nil { return err } @@ -100,7 +101,7 @@ func Cmd() *cobra.Command { "--home", tempDir, ) - _, err = utils.ExecBashCommandWithStdout(sendAllFundsCmd) + _, err = bash.ExecCommandWithStdout(sendAllFundsCmd) if err != nil { return err } diff --git a/cmd/tx/fund_faucet/fund_faucet.go b/cmd/tx/fund_faucet/fund_faucet.go index 92fa5185..ab8ccef2 100644 --- a/cmd/tx/fund_faucet/fund_faucet.go +++ b/cmd/tx/fund_faucet/fund_faucet.go @@ -12,8 +12,10 @@ import ( "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/tx/tx_utils" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/relayer" + "github.com/dymensionxyz/roller/utils/bash" + "github.com/dymensionxyz/roller/utils/config/tomlconfig" + "github.com/dymensionxyz/roller/utils/errorhandling" ) var flagNames = struct { @@ -34,7 +36,7 @@ func Cmd() *cobra.Command { Use: "fund-faucet", Short: "Fund the Dymension faucet with rollapp tokens", Run: func(cmd *cobra.Command, args []string) { - utils.PrettifyErrorIfExists(fundFaucet(cmd, args)) + errorhandling.PrettifyErrorIfExists(fundFaucet(cmd, args)) }, } cmd.Flags(). @@ -45,7 +47,7 @@ func Cmd() *cobra.Command { func fundFaucet(cmd *cobra.Command, args []string) error { home := cmd.Flag(utils.FlagNames.Home).Value.String() - rlpCfg, err := config.LoadRollerConfigFromTOML(home) + rlpCfg, err := tomlconfig.LoadRollerConfig(home) if err != nil { return err } @@ -55,7 +57,7 @@ func fundFaucet(cmd *cobra.Command, args []string) error { return err } defer outputHandler.StopSpinner() - utils.RunOnInterrupt(outputHandler.StopSpinner) + errorhandling.RunOnInterrupt(outputHandler.StopSpinner) outputHandler.StartSpinner(" Loading relayer channel...") rly := relayer.NewRelayer(rlpCfg.Home, rlpCfg.RollappID, rlpCfg.HubData.ID) _, _, err = rly.LoadActiveChannel() @@ -102,7 +104,7 @@ func fundFaucet(cmd *cobra.Command, args []string) error { "json", ) outputHandler.StartSpinner(" Funding faucet...") - stdout, err := utils.ExecBashCommandWithStdout(fundFaucetCmd) + stdout, err := bash.ExecCommandWithStdout(fundFaucetCmd) if err != nil { return err } diff --git a/cmd/utils/balance.go b/cmd/utils/balance.go index 30ac30ee..f4b3f1d2 100644 --- a/cmd/utils/balance.go +++ b/cmd/utils/balance.go @@ -10,7 +10,8 @@ import ( "strings" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/config" + "github.com/dymensionxyz/roller/utils/bash" + "github.com/dymensionxyz/roller/utils/config" ) type ChainQueryConfig struct { @@ -31,7 +32,7 @@ func QueryBalance(chainConfig ChainQueryConfig, address string) (Balance, error) "--output", "json", ) - out, err := ExecBashCommandWithStdout(cmd) + out, err := bash.ExecCommandWithStdout(cmd) if err != nil { return Balance{}, err } diff --git a/cmd/utils/fetch_accounts_data.go b/cmd/utils/fetch_accounts_data.go index bc71097a..fd8c35b5 100644 --- a/cmd/utils/fetch_accounts_data.go +++ b/cmd/utils/fetch_accounts_data.go @@ -4,7 +4,7 @@ import ( "path/filepath" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/config" + "github.com/dymensionxyz/roller/utils/config" ) func GetSequencerData(cfg config.RollappConfig) ([]AccountData, error) { diff --git a/cmd/utils/key_info.go b/cmd/utils/key_info.go index 62508526..072d3668 100644 --- a/cmd/utils/key_info.go +++ b/cmd/utils/key_info.go @@ -7,6 +7,8 @@ import ( "os/exec" "github.com/pterm/pterm" + + "github.com/dymensionxyz/roller/utils/bash" ) // KeyInfo struct stores information about a generated wallet @@ -74,7 +76,7 @@ func GetAddressInfoBinary(keyConfig KeyConfig, binaryPath string) (*KeyInfo, err "--output", "json", ) - output, err := ExecBashCommandWithStdout(showKeyCommand) + output, err := bash.ExecCommandWithStdout(showKeyCommand) if err != nil { return nil, err } @@ -94,7 +96,7 @@ func GetAddressBinary(keyConfig KeyConfig, binaryPath string) (string, error) { keyConfig.Dir, ) - output, err := ExecBashCommandWithStdout(showKeyCommand) + output, err := bash.ExecCommandWithStdout(showKeyCommand) if err != nil { return "", err } diff --git a/cmd/utils/keys.go b/cmd/utils/keys.go index 169698fd..45f18067 100644 --- a/cmd/utils/keys.go +++ b/cmd/utils/keys.go @@ -9,7 +9,8 @@ import ( "github.com/pterm/pterm" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/config" + "github.com/dymensionxyz/roller/utils/bash" + "github.com/dymensionxyz/roller/utils/config" ) // KeyConfig struct store information about a wallet @@ -19,7 +20,7 @@ type KeyConfig struct { // TODO: this is not descriptive, Name would be more expressive ID string ChainBinary string - Type config.VMType + Type consts.VMType } // TODO: KeyInfo and AddressData seem redundant, should be moved into @@ -34,7 +35,7 @@ func GetRelayerAddress(home string, chainID string) (string, error) { "--home", filepath.Join(home, consts.ConfigDirName.Relayer), ) - out, err := ExecBashCommandWithStdout(showKeyCmd) + out, err := bash.ExecCommandWithStdout(showKeyCmd) if err != nil { pterm.Error.Printf("no relayer address found: %v", err) return "", err @@ -64,7 +65,7 @@ func GetSequencerPubKey(rollappConfig config.RollappConfig) (string, error) { func GetAddressPrefix(binaryPath string) (string, error) { cmd := exec.Command(binaryPath, "debug", "addr", "ffffffffffffff") - out, err := ExecBashCommandWithStdout(cmd) + out, err := bash.ExecCommandWithStdout(cmd) if err != nil { return "", err } diff --git a/cmd/utils/log.go b/cmd/utils/log.go index d9c45fca..439ed2e7 100644 --- a/cmd/utils/log.go +++ b/cmd/utils/log.go @@ -6,17 +6,18 @@ import ( "os/exec" "path/filepath" - "gopkg.in/natefinch/lumberjack.v2" + lumberjack "gopkg.in/natefinch/lumberjack.v2" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/config" + "github.com/dymensionxyz/roller/utils/bash" + "github.com/dymensionxyz/roller/utils/config" ) func GetRollerLogger(home string) *log.Logger { return GetLogger(filepath.Join(home, "roller.log")) } -func WithLogging(logFile string) CommandOption { +func WithLogging(logFile string) bash.CommandOption { return func(cmd *exec.Cmd) { logger := GetLogger(logFile) cmd.Stdout = logger.Writer() @@ -24,14 +25,14 @@ func WithLogging(logFile string) CommandOption { } } -func WithLoggerLogging(logger *log.Logger) CommandOption { +func WithLoggerLogging(logger *log.Logger) bash.CommandOption { return func(cmd *exec.Cmd) { cmd.Stdout = logger.Writer() cmd.Stderr = logger.Writer() } } -func WithDiscardLogging() CommandOption { +func WithDiscardLogging() bash.CommandOption { return func(cmd *exec.Cmd) { cmd.Stdout = io.Discard cmd.Stderr = io.Discard diff --git a/cmd/utils/output.go b/cmd/utils/output.go index 0f02d65e..69678b55 100644 --- a/cmd/utils/output.go +++ b/cmd/utils/output.go @@ -1,7 +1,6 @@ package utils import ( - "errors" "fmt" "math/big" "os" @@ -10,6 +9,7 @@ import ( "github.com/briandowns/spinner" "github.com/manifoldco/promptui" "github.com/olekukonko/tablewriter" + "github.com/pterm/pterm" ) func PrintInsufficientBalancesIfAny( @@ -40,13 +40,20 @@ func PrintInsufficientBalancesIfAny( fmt.Println() table.Render() fmt.Println() - fmt.Println("💈 Please fund these addresses and try again.") } - PrettifyErrorIfExists( - errors.New("the following addresses have insufficient balance to perform this operation"), - printAddresses, - ) + pterm.DefaultSection.WithIndentCharacter("🔔"). + Println("Please fund the addresses below to register and run the sequencer.") + printAddresses() + + proceed, _ := pterm.DefaultInteractiveConfirm.WithDefaultValue(true). + WithDefaultText( + "press enter when funded", + ).Show() + if !proceed { + pterm.Info.Println("exiting") + return + } } type NotFundedAddressData struct { diff --git a/config/toml.go b/config/toml.go deleted file mode 100644 index 6299afdb..00000000 --- a/config/toml.go +++ /dev/null @@ -1,41 +0,0 @@ -package config - -import ( - "os" - "path/filepath" - - toml "github.com/pelletier/go-toml" -) - -func WriteConfigToTOML(rlpCfg RollappConfig) error { - tomlBytes, err := toml.Marshal(rlpCfg) - if err != nil { - return err - } - // nolint:gofumpt - return os.WriteFile(filepath.Join(rlpCfg.Home, RollerConfigFileName), tomlBytes, 0o644) -} - -// TODO: should be called from root command -func LoadRollerConfigFromTOML(root string) (RollappConfig, error) { - var config RollappConfig - tomlBytes, err := os.ReadFile(filepath.Join(root, RollerConfigFileName)) - if err != nil { - return config, err - } - err = toml.Unmarshal(tomlBytes, &config) - if err != nil { - return config, err - } - - return config, nil -} - -func LoadConfigFromTOML(path string) ([]byte, error) { - tomlBytes, err := os.ReadFile(path) - if err != nil { - return tomlBytes, err - } - - return tomlBytes, nil -} diff --git a/config/yaml.go b/config/yaml.go deleted file mode 100644 index cacdffb9..00000000 --- a/config/yaml.go +++ /dev/null @@ -1,22 +0,0 @@ -package config - -import ( - "os" - "path/filepath" - - "gopkg.in/yaml.v3" -) - -func LoadConfigFromYAML(root string) (RollappConfig, error) { - var config RollappConfig - yamlBytes, err := os.ReadFile(filepath.Join(root, RollerConfigFileName)) - if err != nil { - return config, err - } - err = yaml.Unmarshal(yamlBytes, &config) - if err != nil { - return config, err - } - - return config, nil -} diff --git a/data_layer/avail/avail.go b/data_layer/avail/avail.go index eb66a235..c30fdb02 100644 --- a/data_layer/avail/avail.go +++ b/data_layer/avail/avail.go @@ -12,7 +12,7 @@ import ( "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" + "github.com/dymensionxyz/roller/utils/config" ) const ( diff --git a/data_layer/celestia/celestia.go b/data_layer/celestia/celestia.go index 22257222..6b5ddff2 100644 --- a/data_layer/celestia/celestia.go +++ b/data_layer/celestia/celestia.go @@ -3,6 +3,7 @@ package celestia import ( "bufio" "encoding/json" + "errors" "fmt" "math/big" "os/exec" @@ -14,8 +15,9 @@ import ( "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" globalutils "github.com/dymensionxyz/roller/utils" + "github.com/dymensionxyz/roller/utils/bash" + "github.com/dymensionxyz/roller/utils/config" ) // TODO: test how much is enough to run the LC for one day and set the minimum balance accordingly. @@ -43,7 +45,7 @@ func NewCelestia(home string) *Celestia { func (c *Celestia) GetPrivateKey() (string, error) { exportKeyCmd := c.GetExportKeyCmd() - out, err := utils.ExecBashCommandWithStdErr(exportKeyCmd) + out, err := bash.ExecCommandWithStdErr(exportKeyCmd) if err != nil { return "", err } @@ -117,7 +119,7 @@ func (c *Celestia) GetDAAccountAddress() (*utils.KeyInfo, error) { consts.Executables.CelKey, "show", c.GetKeyName(), "--node.type", "light", "--keyring-dir", daKeysDir, "--keyring-backend", "test", "--output", "json", ) - output, err := utils.ExecBashCommandWithStdout(cmd) + output, err := bash.ExecCommandWithStdout(cmd) if err != nil { return nil, err } @@ -134,7 +136,7 @@ func (c *Celestia) InitializeLightNodeConfig() (string, error) { "--node.store", filepath.Join(c.Root, consts.ConfigDirName.DALightNode), ) // err := initLightNodeCmd.Run() - out, err := utils.ExecBashCommandWithStdout(initLightNodeCmd) + out, err := bash.ExecCommandWithStdout(initLightNodeCmd) if err != nil { return "", err } @@ -233,6 +235,7 @@ func (c *Celestia) CheckDABalance() ([]utils.NotFundedAddressData, error) { }, ) } + return insufficientBalances, nil } @@ -267,37 +270,49 @@ func (c *Celestia) GetNamespaceID() string { return c.NamespaceID } -func (c *Celestia) getAuthToken() (string, error) { +func (c *Celestia) getAuthToken(t string) (string, error) { getAuthTokenCmd := exec.Command( consts.Executables.Celestia, "light", "auth", - "admin", + t, "--p2p.network", DefaultCelestiaNetwork, "--node.store", filepath.Join(c.Root, consts.ConfigDirName.DALightNode), ) - output, err := utils.ExecBashCommandWithStdout(getAuthTokenCmd) + output, err := bash.ExecCommandWithStdout(getAuthTokenCmd) if err != nil { return "", err } return strings.TrimSuffix(output.String(), "\n"), nil } -func (c *Celestia) GetSequencerDAConfig() string { +func (c *Celestia) GetSequencerDAConfig(nt string) string { if c.NamespaceID == "" { c.NamespaceID = generateRandNamespaceID() } lcEndpoint := c.GetLightNodeEndpoint() - authToken, err := c.getAuthToken() + + var authToken string + var err error + + if nt == consts.NodeType.Sequencer { + authToken, err = c.getAuthToken(consts.DaAuthTokenType.Admin) + } else if nt == consts.NodeType.FullNode { + authToken, err = c.getAuthToken(consts.DaAuthTokenType.Read) + } else { + // TODO: don't panic,return an err + err := errors.New("invalid node type") + panic(err) + } + if err != nil { panic(err) } return fmt.Sprintf( - `{"base_url": "%s", "timeout": 60000000000, "gas_prices":1.0, "gas_adjustment": 1.3, "namespace_id":"%s", -"auth_token":"%s"},"backoff":{"initial_delay":6000000000,"max_delay":6000000000,"growth_factor":2},"retry_attempts":4,"retry_delay":3000000000}`, + `{"base_url": "%s", "timeout": 60000000000, "gas_prices":1.0, "gas_adjustment": 1.3, "namespace_id":"%s","auth_token":"%s","backoff":{"initial_delay":6000000000,"max_delay":6000000000,"growth_factor":2},"retry_attempts":4,"retry_delay":3000000000}`, lcEndpoint, c.NamespaceID, authToken, diff --git a/data_layer/da_layer.go b/data_layer/da_layer.go index 38db4002..85a2b883 100644 --- a/data_layer/da_layer.go +++ b/data_layer/da_layer.go @@ -3,10 +3,11 @@ package datalayer import ( "os/exec" + "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/data_layer/celestia" "github.com/dymensionxyz/roller/data_layer/damock" + "github.com/dymensionxyz/roller/utils/config" ) type DataLayer interface { @@ -16,7 +17,8 @@ type DataLayer interface { GetStartDACmd() *exec.Cmd GetDAAccData(c config.RollappConfig) ([]utils.AccountData, error) GetLightNodeEndpoint() string - GetSequencerDAConfig() string + // todo: Refactor, node type makes reusability awful + GetSequencerDAConfig(nt string) string SetRPCEndpoint(string) SetMetricsEndpoint(endpoint string) GetNetworkName() string @@ -28,19 +30,19 @@ type DataLayer interface { } type DAManager struct { - datype config.DAType + datype consts.DAType DataLayer } -func NewDAManager(datype config.DAType, home string) *DAManager { +func NewDAManager(datype consts.DAType, home string) *DAManager { var dalayer DataLayer switch datype { - case config.Celestia: + case consts.Celestia: dalayer = celestia.NewCelestia(home) // case config.Avail: // dalayer = avail.NewAvail(home) - case config.Local: + case consts.Local: dalayer = &damock.DAMock{} default: panic("Unknown data layer type") diff --git a/data_layer/damock/damock.go b/data_layer/damock/damock.go index 6da472c3..3a52478c 100644 --- a/data_layer/damock/damock.go +++ b/data_layer/damock/damock.go @@ -4,7 +4,7 @@ import ( "os/exec" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" + "github.com/dymensionxyz/roller/utils/config" ) type DAMock struct{} @@ -20,11 +20,11 @@ func (d *DAMock) GetStatus(c config.RollappConfig) string { return "Running local DA" } -func (c *DAMock) GetRootDirectory() string { +func (d *DAMock) GetRootDirectory() string { return "" } -func (c *DAMock) GetNamespaceID() string { +func (d *DAMock) GetNamespaceID() string { return "" } @@ -56,14 +56,14 @@ func (d *DAMock) GetLightNodeEndpoint() string { return "" } -func (d *DAMock) GetSequencerDAConfig() string { +func (d *DAMock) GetSequencerDAConfig(nt string) string { return "" } func (d *DAMock) SetRPCEndpoint(string) { } -func (c *DAMock) GetKeyName() string { +func (d *DAMock) GetKeyName() string { return "" } diff --git a/go.mod b/go.mod index 88cbc97f..6812edd5 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( ) require ( + cosmossdk.io/math v1.3.0 github.com/BurntSushi/toml v1.4.0 github.com/briandowns/spinner v1.23.0 github.com/centrifuge/go-substrate-rpc-client/v4 v4.0.0-00010101000000-000000000000 @@ -16,10 +17,11 @@ require ( github.com/cosmos/go-bip39 v1.0.0 github.com/docker/docker v27.0.3+incompatible github.com/docker/go-connections v0.5.0 - github.com/dymensionxyz/dymension/v3 v3.1.0-rc03.0.20240802123505-afd8254a010c + github.com/dymensionxyz/dymension/v3 v3.1.0-rc03.0.20240808102132-04bd9df0eec8 github.com/gizak/termui/v3 v3.1.0 github.com/gogo/protobuf v1.3.3 github.com/manifoldco/promptui v0.9.0 + github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416 github.com/olekukonko/tablewriter v0.0.5 github.com/pelletier/go-toml v1.9.5 github.com/pterm/pterm v0.12.79 @@ -27,7 +29,6 @@ require ( github.com/tidwall/sjson v1.2.5 gopkg.in/natefinch/lumberjack.v2 v2.2.1 gopkg.in/yaml.v2 v2.4.0 - gopkg.in/yaml.v3 v3.0.1 ) require ( @@ -38,7 +39,6 @@ require ( cosmossdk.io/core v0.5.1 // indirect cosmossdk.io/depinject v1.0.0-alpha.4 // indirect cosmossdk.io/errors v1.0.1 // indirect - cosmossdk.io/math v1.3.0 // indirect filippo.io/edwards25519 v1.0.0 // indirect github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect github.com/99designs/keyring v1.2.1 // indirect @@ -88,6 +88,7 @@ require ( github.com/dustin/go-humanize v1.0.1 // indirect github.com/dvsekhvalnov/jose2go v1.6.0 // indirect github.com/dymensionxyz/gerr-cosmos v1.0.0 // indirect + github.com/dymensionxyz/sdk-utils v0.2.7 // indirect github.com/ethereum/go-ethereum v1.10.26 // indirect github.com/fatih/color v1.15.0 // indirect github.com/felixge/httpsnoop v1.0.4 // indirect @@ -135,6 +136,7 @@ require ( github.com/moby/docker-image-spec v1.3.1 // indirect github.com/moby/term v0.5.0 // indirect github.com/mtibben/percent v0.2.1 // indirect + github.com/naoina/go-stringutil v0.1.0 // indirect github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d // indirect github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/image-spec v1.1.0 // indirect @@ -194,6 +196,7 @@ require ( google.golang.org/protobuf v1.34.2 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect pgregory.net/rapid v1.1.0 // indirect sigs.k8s.io/yaml v1.4.0 // indirect ) diff --git a/go.sum b/go.sum index e0e0518c..96a7868d 100644 --- a/go.sum +++ b/go.sum @@ -573,12 +573,12 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkp github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/dvsekhvalnov/jose2go v1.6.0 h1:Y9gnSnP4qEI0+/uQkHvFXeD2PLPJeXEL+ySMEA2EjTY= github.com/dvsekhvalnov/jose2go v1.6.0/go.mod h1:QsHjhyTlD/lAVqn/NSbVZmSCGeDehTB/mPZadG+mhXU= -github.com/dymensionxyz/dymension/v3 v3.1.0-rc03.0.20240802123505-afd8254a010c h1:HL66BmrUHrEwhXpqbV8sf8/zeucz5mxUbFA538ZaHfQ= -github.com/dymensionxyz/dymension/v3 v3.1.0-rc03.0.20240802123505-afd8254a010c/go.mod h1:qXov17cZXRQ1MUS14efi1vLBTfdtbDyasYFp5skZTcg= +github.com/dymensionxyz/dymension/v3 v3.1.0-rc03.0.20240808102132-04bd9df0eec8 h1:L0WeE/sOQNs5bm6/CxyJ7oeF8YttRHfBdwq9YN2dY4M= +github.com/dymensionxyz/dymension/v3 v3.1.0-rc03.0.20240808102132-04bd9df0eec8/go.mod h1:1+b3syquF6h0iANMxsMqW2Ojd3Cw1D91n0jB2ei3ks8= github.com/dymensionxyz/gerr-cosmos v1.0.0 h1:oi91rgOkpJWr41oX9JOyjvvBnhGY54tj513x8VlDAEc= github.com/dymensionxyz/gerr-cosmos v1.0.0/go.mod h1:n+0olxPogzWqFKba45mCpvrHLGmeS8W9UZjggHnWk6c= -github.com/dymensionxyz/sdk-utils v0.2.2 h1:RoANEXbR4nkEMaRGWCLT1P9A7KBCmzJ2U9wAZnNbszo= -github.com/dymensionxyz/sdk-utils v0.2.2/go.mod h1:ywr7+EEhHyuXCPUyLsktF3R4Mr31uCiviIjqwWNbQ84= +github.com/dymensionxyz/sdk-utils v0.2.7 h1:9Hy56ivv81ISIG7x9AMaNo6GABRuEgZQ+MmS2PdfuW0= +github.com/dymensionxyz/sdk-utils v0.2.7/go.mod h1:it9owYOpnIe17+ftTATQNDN4z+mBQx20/2Jm8SK15Rk= github.com/eclipse/paho.mqtt.golang v1.2.0/go.mod h1:H9keYFcgq3Qr5OUJm/JZI/i6U7joQ8SYLhZwfeOo6Ts= github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= @@ -974,6 +974,7 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/labstack/echo/v4 v4.2.1/go.mod h1:AA49e0DZ8kk5jTOOCKNuPR6oTnBS0dYiM4FW1e6jwpg= github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k= @@ -1065,7 +1066,9 @@ github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae/go.mod h1:qAyveg+e4CE github.com/mtibben/percent v0.2.1 h1:5gssi8Nqo8QU/r2pynCm+hBQHpkB/uNK7BJCFogWdzs= github.com/mtibben/percent v0.2.1/go.mod h1:KG9uO+SZkUp+VkRHsCdYQV3XSZrrSpR3O9ibNBTZrns= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/naoina/go-stringutil v0.1.0 h1:rCUeRUHjBjGTSHl0VC00jUPLz8/F9dDzYI70Hzifhks= github.com/naoina/go-stringutil v0.1.0/go.mod h1:XJ2SJL9jCtBh+P9q5btrd/Ylo8XwT/h1USek5+NqSA0= +github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416 h1:shk/vn9oCoOTmwcouEdwIeOtOGA/ELRUw/GwvxwfT+0= github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416/go.mod h1:NBIhNtsFMo3G2szEBne+bO4gS192HuIYRqfvOWb4i1E= github.com/neilotoole/errgroup v0.1.6/go.mod h1:Q2nLGf+594h0CLBs/Mbg6qOr7GtqDK7C2S41udRnToE= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= diff --git a/relayer/channels.go b/relayer/channels.go index e11a711c..23636737 100644 --- a/relayer/channels.go +++ b/relayer/channels.go @@ -7,8 +7,8 @@ import ( "path/filepath" "github.com/dymensionxyz/roller/cmd/consts" - cmdutils "github.com/dymensionxyz/roller/cmd/utils" "github.com/dymensionxyz/roller/utils" + "github.com/dymensionxyz/roller/utils/bash" ) // TODO: Change to use the connection for fetching relevant channel using connection-channels rly command @@ -30,7 +30,7 @@ func (r *Relayer) LoadActiveChannel() (string, string, error) { return "", "", nil } - output, err := cmdutils.ExecBashCommandWithStdout(r.queryChannelsRollappCmd(activeConnectionID)) + output, err := bash.ExecCommandWithStdout(r.queryChannelsRollappCmd(activeConnectionID)) if err != nil { return "", "", err } @@ -61,7 +61,7 @@ func (r *Relayer) LoadActiveChannel() (string, string, error) { // found STATE_OPEN channel // Check if the channel is open on the hub var res HubQueryResult - outputHub, err := cmdutils.ExecBashCommandWithStdout( + outputHub, err := bash.ExecCommandWithStdout( r.queryChannelsHubCmd(outputStruct.ChannelID), ) if err != nil { diff --git a/relayer/config.go b/relayer/config.go index 0cec0404..6aa280d0 100644 --- a/relayer/config.go +++ b/relayer/config.go @@ -10,8 +10,8 @@ import ( yaml "gopkg.in/yaml.v2" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/utils" + "github.com/dymensionxyz/roller/utils/config" ) func CreatePath(rlpCfg config.RollappConfig) error { diff --git a/relayer/connections.go b/relayer/connections.go index 4b0a61ba..80499147 100644 --- a/relayer/connections.go +++ b/relayer/connections.go @@ -6,8 +6,8 @@ import ( "path/filepath" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/cmd/utils" roller_utils "github.com/dymensionxyz/roller/utils" + "github.com/dymensionxyz/roller/utils/bash" ) type ConnectionsQueryResult struct { @@ -95,7 +95,7 @@ func (r *Relayer) GetActiveConnection() (string, error) { // END: try to read connection information from the configuration file var hubConnectionInfo ConnectionsQueryResult - hubConnectionOutput, err := utils.ExecBashCommandWithStdout(r.queryConnectionsHubCmd()) + hubConnectionOutput, err := bash.ExecCommandWithStdout(r.queryConnectionsHubCmd()) if err != nil { r.logger.Printf("couldn't find any open connections for %s", r.HubID) return "", err @@ -106,7 +106,7 @@ func (r *Relayer) GetActiveConnection() (string, error) { } // fetch connection from the chain - rollappConnectionOutput, err := utils.ExecBashCommandWithStdout( + rollappConnectionOutput, err := bash.ExecCommandWithStdout( r.queryConnectionRollappCmd( hubConnectionInfo. Counterparty.ConnectionID, @@ -135,7 +135,7 @@ func (r *Relayer) GetActiveConnection() (string, error) { // Check if the connection is open on the hub var res ConnectionQueryResult - outputHub, err := utils.ExecBashCommandWithStdout(r.queryConnectionHubCmd(hubConnectionInfo.ID)) + outputHub, err := bash.ExecCommandWithStdout(r.queryConnectionHubCmd(hubConnectionInfo.ID)) if err != nil { return "", err } diff --git a/relayer/create_ibc_channel.go b/relayer/create_ibc_channel.go index b91529cc..6d93110a 100644 --- a/relayer/create_ibc_channel.go +++ b/relayer/create_ibc_channel.go @@ -8,6 +8,7 @@ import ( "strconv" "time" + "github.com/dymensionxyz/roller/utils/bash" "github.com/pterm/pterm" "github.com/dymensionxyz/roller/cmd/consts" @@ -19,7 +20,7 @@ import ( // and return the source channel ID. func (r *Relayer) CreateIBCChannel( override bool, - logFileOption utils.CommandOption, + logFileOption bash.CommandOption, seq *sequencer.Sequencer, ) (ConnectionChannels, error) { ctx, cancel := context.WithCancel(context.Background()) @@ -40,7 +41,7 @@ func (r *Relayer) CreateIBCChannel( } sendFundsCmd := seq.GetSendCmd(sequencerAddress) - utils.RunCommandEvery( + bash.RunCommandEvery( ctx, sendFundsCmd.Path, sendFundsCmd.Args[1:], @@ -75,7 +76,7 @@ func (r *Relayer) CreateIBCChannel( return ConnectionChannels{}, err } - if err := utils.ExecBashCmd(createClientsCmd, logFileOption); err != nil { + if err := bash.ExecCmd(createClientsCmd, logFileOption); err != nil { fmt.Println(err) return ConnectionChannels{}, err } @@ -92,7 +93,7 @@ func (r *Relayer) CreateIBCChannel( return ConnectionChannels{}, err } createConnectionCmd := r.getCreateConnectionCmd(override) - if err := utils.ExecBashCmd(createConnectionCmd, logFileOption); err != nil { + if err := bash.ExecCmd(createConnectionCmd, logFileOption); err != nil { return ConnectionChannels{}, err } } @@ -107,7 +108,7 @@ func (r *Relayer) CreateIBCChannel( if err := r.WriteRelayerStatus(status); err != nil { return ConnectionChannels{}, err } - if err := utils.ExecBashCmd(createChannelCmd, logFileOption); err != nil { + if err := bash.ExecCmd(createChannelCmd, logFileOption); err != nil { return ConnectionChannels{}, err } status = "" diff --git a/relayer/query.go b/relayer/query.go index 2766cfc3..94d045c7 100644 --- a/relayer/query.go +++ b/relayer/query.go @@ -3,8 +3,8 @@ package relayer import ( "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/sequencer" + "github.com/dymensionxyz/roller/utils/config" ) func GetRolRlyAccData(cfg config.RollappConfig) (*utils.AccountData, error) { diff --git a/relayer/relayer_manager.go b/relayer/relayer_manager.go index 35af9ee3..cb8f4354 100644 --- a/relayer/relayer_manager.go +++ b/relayer/relayer_manager.go @@ -9,7 +9,7 @@ import ( "path/filepath" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/config" + "github.com/dymensionxyz/roller/utils/config" ) type Relayer struct { @@ -50,7 +50,7 @@ func (r *Relayer) GetRelayerStatus(config.RollappConfig) string { func (r *Relayer) WriteRelayerStatus(status string) error { // nolint:gofumpt - return os.WriteFile(r.StatusFilePath(), []byte(status), 0644) + return os.WriteFile(r.StatusFilePath(), []byte(status), 0o644) } func (r *Relayer) StatusFilePath() string { diff --git a/scripts/install-binaries.sh b/scripts/install-binaries.sh index 8d5fc278..0184242c 100755 --- a/scripts/install-binaries.sh +++ b/scripts/install-binaries.sh @@ -3,11 +3,12 @@ BINS_DIR="/usr/local/bin" ROLLER_BINS_DIR="$BINS_DIR/roller_bins" ROLLAPP_EVN_VERSION="v2.2.1-rc01" -DYMD_VERSION="" +DYMD_VERSION="main" DYMD_COMMIT="b9d863e6" EIBC_VERSION="main" RLY_VERSION="v0.3.4-v2.5.2-relayer" CELESTIA_VERSION="v0.14.1" +CELESTIA_APP_VERSION="v1.11.0" if [ -z "$BECH32_PREFIX" ]; then echo "please provide BECH32_PREFIX of the RollApp before running this script" @@ -76,6 +77,14 @@ if ! command -v "$ROLLER_BINS_DIR/celestia" &> /dev/null || ! command -v "$ROLLE cd ../ && rm -rf celestia-node fi +# celestia-app +if ! command -v "$ROLLER_BINS_DIR/celestia-appd" &> /dev/null; then + cd ~/ && rm -rf celestia-app/ + git clone https://github.com/celestiaorg/celestia-app.git --branch $CELESTIA_APP_VERSION && cd celestia-app || exit 1 + make build && sudo mv build/celestia-appd "$ROLLER_BINS_DIR" + cd ../ && rm -rf celestia-app/ +fi + # rly if ! command -v "$ROLLER_BINS_DIR/rly" &> /dev/null; then cd ~/ && rm -rf go-relayer/ diff --git a/sequencer/config.go b/sequencer/config.go index fb22f04f..a6868550 100644 --- a/sequencer/config.go +++ b/sequencer/config.go @@ -6,13 +6,13 @@ import ( "path/filepath" "strings" - toml "github.com/pelletier/go-toml" + "github.com/pelletier/go-toml" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/config" datalayer "github.com/dymensionxyz/roller/data_layer" "github.com/dymensionxyz/roller/data_layer/celestia" "github.com/dymensionxyz/roller/utils" + "github.com/dymensionxyz/roller/utils/config" ) func SetDefaultDymintConfig(rlpCfg config.RollappConfig) error { @@ -73,7 +73,7 @@ func updateDaConfigInToml(rlpCfg config.RollappConfig, dymintCfg *toml.Tree) err dymintCfg.Set("da_layer", "mock") // daConfig := damanager.GetSequencerDAConfig() // dymintCfg.Set("da_config", daConfig) - if rlpCfg.DA == config.Celestia { + if rlpCfg.DA == consts.Celestia { celDAManager, ok := damanager.DataLayer.(*celestia.Celestia) if !ok { return fmt.Errorf( @@ -84,7 +84,7 @@ func updateDaConfigInToml(rlpCfg config.RollappConfig, dymintCfg *toml.Tree) err dymintCfg.Set("namespace_id", celDAManager.NamespaceID) } - if rlpCfg.DA == config.Local { + if rlpCfg.DA == consts.Local { dymintCfg.Set("da_layer", "mock") } diff --git a/sequencer/sequencer_manager.go b/sequencer/sequencer_manager.go index c37c91d3..016175eb 100644 --- a/sequencer/sequencer_manager.go +++ b/sequencer/sequencer_manager.go @@ -10,7 +10,7 @@ import ( "github.com/pterm/pterm" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/config" + "github.com/dymensionxyz/roller/utils/config" ) type Sequencer struct { diff --git a/sequencer/status.go b/sequencer/status.go index 5cd63309..6cc8cc76 100644 --- a/sequencer/status.go +++ b/sequencer/status.go @@ -10,8 +10,8 @@ import ( "strconv" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" + "github.com/dymensionxyz/roller/utils/bash" + "github.com/dymensionxyz/roller/utils/config" ) type NodeInfo struct { @@ -68,15 +68,19 @@ func (seq *Sequencer) GetRollappHeight() (string, error) { if response.Result.NodeInfo.Network == seq.RlpCfg.RollappID { return response.Result.SyncInfo.LatestBlockHeight, nil } else { - return "-1", fmt.Errorf("wrong sequencer is running on the machine. Expected network ID %s,"+ - " got %s", seq.RlpCfg.RollappID, response.Result.NodeInfo.Network) + return "-1", fmt.Errorf( + "wrong sequencer is running on the machine. Expected network ID %s,"+ + " got %s", seq.RlpCfg.RollappID, response.Result.NodeInfo.Network, + ) } } func (seq *Sequencer) GetHubHeight() (string, error) { - cmd := exec.Command(consts.Executables.Dymension, "q", "rollapp", "state", seq.RlpCfg.RollappID, - "--output", "json", "--node", seq.RlpCfg.HubData.RPC_URL) - out, err := utils.ExecBashCommandWithStdout(cmd) + cmd := exec.Command( + consts.Executables.Dymension, "q", "rollapp", "state", seq.RlpCfg.RollappID, + "--output", "json", "--node", seq.RlpCfg.HubData.RPC_URL, + ) + out, err := bash.ExecCommandWithStdout(cmd) if err != nil { return "", err } @@ -140,7 +144,8 @@ func (seq *Sequencer) GetSequencerStatus(config.RollappConfig) string { err = seq.GetSequencerHealth() if err != nil { - return fmt.Sprintf(` + return fmt.Sprintf( + ` status: Unhealthy error: %v `, err, @@ -160,16 +165,19 @@ error: %v fmt.Println("failed to retrieve ports: ", err) } - return fmt.Sprintf(`RollApp + return fmt.Sprintf( + `RollApp status: Healthy height: %s Endpoints: rpc: %s -rest: %s`, rolHeight, localRPCEndpoint, localAPIEndpoint) +rest: %s`, rolHeight, localRPCEndpoint, localAPIEndpoint, + ) } - return fmt.Sprintf(`RollApp: + return fmt.Sprintf( + `RollApp: status: Healthy height: %s @@ -178,5 +186,6 @@ rpc: %s rest: %s Hub: -height: %s`, rolHeight, localRPCEndpoint, localAPIEndpoint, hubHeight) +height: %s`, rolHeight, localRPCEndpoint, localAPIEndpoint, hubHeight, + ) } diff --git a/test/config/init/testutils/keys.go b/test/config/init/testutils/keys.go index 3aa0f3f4..62f597f2 100644 --- a/test/config/init/testutils/keys.go +++ b/test/config/init/testutils/keys.go @@ -7,10 +7,10 @@ import ( "path/filepath" "regexp" + "github.com/dymensionxyz/roller/utils/config" "github.com/pelletier/go-toml" "github.com/dymensionxyz/roller/cmd/consts" - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/sequencer" "github.com/dymensionxyz/roller/utils" ) diff --git a/test/config/init/testutils/relayer.go b/test/config/init/testutils/relayer.go index 69cc5331..41a233c2 100644 --- a/test/config/init/testutils/relayer.go +++ b/test/config/init/testutils/relayer.go @@ -4,9 +4,9 @@ import ( "errors" "reflect" - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/relayer" "github.com/dymensionxyz/roller/utils" + "github.com/dymensionxyz/roller/utils/config" ) const ( diff --git a/cmd/utils/bash_commands.go b/utils/bash/bash_commands.go similarity index 87% rename from cmd/utils/bash_commands.go rename to utils/bash/bash_commands.go index 977a1c12..a4c8ad57 100644 --- a/cmd/utils/bash_commands.go +++ b/utils/bash/bash_commands.go @@ -1,4 +1,4 @@ -package utils +package bash import ( "bufio" @@ -13,7 +13,7 @@ import ( "sync" "time" - "github.com/dymensionxyz/roller/config" + "github.com/dymensionxyz/roller/utils/errorhandling" ) func RunCommandEvery( @@ -50,15 +50,9 @@ func RunCommandEvery( }() } -func GetCommonDymdFlags(rollappConfig config.RollappConfig) []string { - return []string{ - "--node", rollappConfig.HubData.RPC_URL, "--output", "json", - } -} - type CommandOption func(cmd *exec.Cmd) -func RunBashCmdAsync( +func RunCmdAsync( ctx context.Context, cmd *exec.Cmd, printOutput func(), @@ -84,9 +78,9 @@ func RunBashCmdAsync( if err != nil { errMsg := parseError(stderr.String()) if errMsg == "" { - PrettifyErrorIfExists(err) + errorhandling.PrettifyErrorIfExists(err) } - PrettifyErrorIfExists(errors.New(errMsg)) + errorhandling.PrettifyErrorIfExists(errors.New(errMsg)) } printOutput() @@ -103,11 +97,11 @@ func RunBashCmdAsync( err = cmd.Wait() if err != nil { errMsg := parseError(stderr.String()) - PrettifyErrorIfExists(errors.New(errMsg)) + errorhandling.PrettifyErrorIfExists(errors.New(errMsg)) } } -func ExecBashCommandWithStdout(cmd *exec.Cmd) (bytes.Buffer, error) { +func ExecCommandWithStdout(cmd *exec.Cmd) (bytes.Buffer, error) { var stderr bytes.Buffer var stdout bytes.Buffer cmd.Stderr = &stderr @@ -119,7 +113,7 @@ func ExecBashCommandWithStdout(cmd *exec.Cmd) (bytes.Buffer, error) { return stdout, nil } -func ExecBashCommandWithStdErr(cmd *exec.Cmd) (bytes.Buffer, error) { +func ExecCommandWithStdErr(cmd *exec.Cmd) (bytes.Buffer, error) { var stderr bytes.Buffer var stdout bytes.Buffer cmd.Stderr = &stderr @@ -131,7 +125,7 @@ func ExecBashCommandWithStdErr(cmd *exec.Cmd) (bytes.Buffer, error) { return stderr, nil } -func ExecBashCmd(cmd *exec.Cmd, options ...CommandOption) error { +func ExecCmd(cmd *exec.Cmd, options ...CommandOption) error { for _, option := range options { option(cmd) } @@ -142,7 +136,7 @@ func ExecBashCmd(cmd *exec.Cmd, options ...CommandOption) error { return nil } -func ExecBashCmdFollow(cmd *exec.Cmd) error { +func ExecCmdFollow(cmd *exec.Cmd) error { stdout, err := cmd.StdoutPipe() if err != nil { return err @@ -207,7 +201,7 @@ func ExecBashCmdFollow(cmd *exec.Cmd) error { } // TODO: generalize -func ExecBashCommandWithInput(cmd *exec.Cmd) error { +func ExecCommandWithInput(cmd *exec.Cmd) error { // Create pipes for stdin, stdout, and stderr stdin, err := cmd.StdinPipe() if err != nil { diff --git a/config/chainid.go b/utils/config/chainid.go similarity index 100% rename from config/chainid.go rename to utils/config/chainid.go diff --git a/config/config.go b/utils/config/config.go similarity index 75% rename from config/config.go rename to utils/config/config.go index a2b7cde7..1019528a 100644 --- a/config/config.go +++ b/utils/config/config.go @@ -4,37 +4,22 @@ import ( "fmt" "strings" "unicode" -) - -const RollerConfigFileName = "roller.toml" - -type VMType string - -const ( - SDK_ROLLAPP VMType = "sdk" - EVM_ROLLAPP VMType = "evm" -) - -type DAType string -const ( - Local DAType = "local" - Celestia DAType = "celestia" - Avail DAType = "avail" + "github.com/dymensionxyz/roller/cmd/consts" ) -var SupportedDas = []DAType{Celestia, Avail, Local} +var SupportedDas = []consts.DAType{consts.Celestia, consts.Avail, consts.Local} type RollappConfig struct { - Home string `toml:"home"` - RollappID string `toml:"rollapp_id"` - RollappBinary string `toml:"rollapp_binary"` - VMType VMType `toml:"execution"` - Denom string `toml:"denom"` + Home string `toml:"home"` + RollappID string `toml:"rollapp_id"` + RollappBinary string `toml:"rollapp_binary"` + VMType consts.VMType `toml:"execution"` + Denom string `toml:"denom"` // TokenSupply string Decimals uint - HubData HubData - DA DAType + HubData consts.HubData + DA consts.DAType RollerVersion string `toml:"roller_version"` // new roller.toml @@ -46,14 +31,6 @@ type RollappConfig struct { MinGasPrices string `toml:"minimum_gas_prices"` } -type HubData = struct { - API_URL string `toml:"api_url"` - ID string `toml:"id"` - RPC_URL string `toml:"rpc_url"` - ARCHIVE_RPC_URL string `toml:"archive_rpc_url"` - GAS_PRICE string `toml:"gas_price"` -} - func (c RollappConfig) Validate() error { err := VerifyHubData(c.HubData) if err != nil { @@ -86,22 +63,22 @@ func (c RollappConfig) Validate() error { } func IsValidDAType(t string) bool { - switch DAType(t) { - case Local, Celestia, Avail: + switch consts.DAType(t) { + case consts.Local, consts.Celestia, consts.Avail: return true } return false } func IsValidVMType(t string) bool { - switch VMType(t) { - case SDK_ROLLAPP, EVM_ROLLAPP: + switch consts.VMType(t) { + case consts.SDK_ROLLAPP, consts.EVM_ROLLAPP: return true } return false } -func VerifyHubData(data HubData) error { +func VerifyHubData(data consts.HubData) error { if data.ID == "mock" { return nil } diff --git a/utils/config/tomlconfig/toml.go b/utils/config/tomlconfig/toml.go new file mode 100644 index 00000000..1599d01c --- /dev/null +++ b/utils/config/tomlconfig/toml.go @@ -0,0 +1,84 @@ +package tomlconfig + +import ( + "encoding/json" + "fmt" + "os" + "os/exec" + "path/filepath" + + dymensionratypes "github.com/dymensionxyz/dymension/v3/x/rollapp/types" + naoinatoml "github.com/naoina/toml" + + "github.com/dymensionxyz/roller/cmd/consts" + "github.com/dymensionxyz/roller/utils/bash" + "github.com/dymensionxyz/roller/utils/config" +) + +func Write(rlpCfg config.RollappConfig) error { + tomlBytes, err := naoinatoml.Marshal(rlpCfg) + if err != nil { + return err + } + // nolint:gofumpt + return os.WriteFile(filepath.Join(rlpCfg.Home, consts.RollerConfigFileName), tomlBytes, 0o644) +} + +// TODO: should be called from root command +func LoadRollerConfig(root string) (config.RollappConfig, error) { + var config config.RollappConfig + tomlBytes, err := os.ReadFile(filepath.Join(root, consts.RollerConfigFileName)) + if err != nil { + return config, err + } + err = naoinatoml.Unmarshal(tomlBytes, &config) + if err != nil { + return config, err + } + + return config, nil +} + +func Load(path string) ([]byte, error) { + tomlBytes, err := os.ReadFile(path) + if err != nil { + return tomlBytes, err + } + + return tomlBytes, nil +} + +func LoadRollappMetadataFromChain(home, raID string) (*config.RollappConfig, error) { + var config config.RollappConfig + var ra dymensionratypes.QueryGetRollappResponse + + tomlBytes, err := os.ReadFile(filepath.Join(home, consts.RollerConfigFileName)) + if err != nil { + return &config, err + } + err = naoinatoml.Unmarshal(tomlBytes, &config) + if err != nil { + return &config, err + } + + getRollappCmd := exec.Command( + consts.Executables.Dymension, + "q", "rollapp", "show", + raID, + ) + + out, err := bash.ExecCommandWithStdout(getRollappCmd) + if err != nil { + return &config, err + } + + err = json.Unmarshal(out.Bytes(), &ra) + if err != nil { + return &config, err + } + + j, _ := json.MarshalIndent(ra, "", " ") + fmt.Println(string(j)) + + return &config, nil +} diff --git a/utils/dymint/dymint.go b/utils/dymint/dymint.go index d0e96362..057d25f9 100644 --- a/utils/dymint/dymint.go +++ b/utils/dymint/dymint.go @@ -7,9 +7,9 @@ import ( "github.com/BurntSushi/toml" "github.com/pterm/pterm" - "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/sequencer" "github.com/dymensionxyz/roller/utils" + tomlconfig "github.com/dymensionxyz/roller/utils/config/tomlconfig" ) // TODO: use dymint instead @@ -64,7 +64,7 @@ func UpdateDymintConfigForIBC(home string) error { pterm.Info.Println("checking dymint block time settings") dymintPath := sequencer.GetDymintFilePath(home) fmt.Println(dymintPath) - dymintCfg, err := config.LoadConfigFromTOML(dymintPath) + dymintCfg, err := tomlconfig.Load(dymintPath) if err != nil { return err } diff --git a/cmd/utils/error_handling.go b/utils/errorhandling/error_handling.go similarity index 94% rename from cmd/utils/error_handling.go rename to utils/errorhandling/error_handling.go index ee75caed..265f5e13 100644 --- a/cmd/utils/error_handling.go +++ b/utils/errorhandling/error_handling.go @@ -1,4 +1,4 @@ -package utils +package errorhandling import ( "os" @@ -6,7 +6,7 @@ import ( "github.com/pterm/pterm" - "github.com/dymensionxyz/roller/config" + "github.com/dymensionxyz/roller/utils/config" "github.com/dymensionxyz/roller/version" ) diff --git a/utils/rollapp/rollapp.go b/utils/rollapp/rollapp.go index 22d24e4f..6de0f4b0 100644 --- a/utils/rollapp/rollapp.go +++ b/utils/rollapp/rollapp.go @@ -7,16 +7,16 @@ import ( "strings" dymensiontypes "github.com/dymensionxyz/dymension/v3/x/rollapp/types" + globalutils "github.com/dymensionxyz/roller/utils/bash" tmtypes "github.com/tendermint/tendermint/types" "github.com/dymensionxyz/roller/cmd/consts" - globalutils "github.com/dymensionxyz/roller/cmd/utils" sequencerutils "github.com/dymensionxyz/roller/utils/sequencer" ) func GetCurrentHeight() (*BlockInformation, error) { cmd := getCurrentBlockCmd() - out, err := globalutils.ExecBashCommandWithStdout(cmd) + out, err := globalutils.ExecCommandWithStdout(cmd) if err != nil { return nil, nil } @@ -50,7 +50,7 @@ func GetInitialSequencerAddress(raID string) (string, error) { "json", ) - out, err := globalutils.ExecBashCommandWithStdout(cmd) + out, err := globalutils.ExecCommandWithStdout(cmd) if err != nil { fmt.Println(err) } @@ -58,7 +58,7 @@ func GetInitialSequencerAddress(raID string) (string, error) { var ra dymensiontypes.QueryGetRollappResponse _ = json.Unmarshal(out.Bytes(), &ra) - return ra.Rollapp.InitialSequencerAddress, nil + return ra.Rollapp.InitialSequencer, nil } func IsInitialSequencer(addr, raID string) (bool, error) { @@ -67,6 +67,8 @@ func IsInitialSequencer(addr, raID string) (bool, error) { return false, err } + fmt.Printf("%s\n%s\n", addr, initSeqAddr) + if strings.TrimSpace(addr) == strings.TrimSpace(initSeqAddr) { return true, nil } @@ -76,8 +78,8 @@ func IsInitialSequencer(addr, raID string) (bool, error) { func GetRegisteredSequencers( raID string, -) (*Sequencers, error) { - var seq Sequencers +) (*sequencerutils.Sequencers, error) { + var seq sequencerutils.Sequencers cmd := exec.Command( consts.Executables.Dymension, "q", @@ -87,7 +89,7 @@ func GetRegisteredSequencers( "--output", "json", ) - out, err := globalutils.ExecBashCommandWithStdout(cmd) + out, err := globalutils.ExecCommandWithStdout(cmd) if err != nil { return nil, err } @@ -104,7 +106,3 @@ type BlockInformation struct { BlockId tmtypes.BlockID `json:"block_id"` Block tmtypes.Block `json:"block"` } - -type Sequencers struct { - Sequencers []sequencerutils.Info `json:"sequencers,omitempty"` -} diff --git a/utils/sequencer/sequencer.go b/utils/sequencer/sequencer.go index 81317e98..3e5f710c 100644 --- a/utils/sequencer/sequencer.go +++ b/utils/sequencer/sequencer.go @@ -8,6 +8,7 @@ import ( "os/exec" "path/filepath" "slices" + "strconv" "strings" cosmossdktypes "github.com/cosmos/cosmos-sdk/types" @@ -15,7 +16,8 @@ import ( "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" + "github.com/dymensionxyz/roller/utils/bash" + "github.com/dymensionxyz/roller/utils/config" ) func Register(raCfg config.RollappConfig) error { @@ -40,6 +42,7 @@ func Register(raCfg config.RollappConfig) error { return err } + // TODO: handle raw_log cmd := exec.Command( consts.Executables.Dymension, "tx", @@ -51,11 +54,11 @@ func Register(raCfg config.RollappConfig) error { fmt.Sprintf("%s%s", seqMinBond.Amount.String(), seqMinBond.Denom), "--from", consts.KeysIds.HubSequencer, "--keyring-backend", "test", - "--fees", "1000000000000000000adym", + "--fees", fmt.Sprintf("%d%s", consts.DefaultFee, consts.Denoms.Hub), "--keyring-dir", filepath.Join(utils.GetRollerRootDir(), consts.ConfigDirName.HubKeys), ) - err = utils.ExecBashCommandWithInput(cmd) + err = bash.ExecCommandWithInput(cmd) if err != nil { return err } @@ -93,7 +96,7 @@ func GetMinSequencerBond() (*cosmossdktypes.Coin, error) { "q", "sequencer", "params", "-o", "json", ) - out, err := utils.ExecBashCommandWithStdout(cmd) + out, err := bash.ExecCommandWithStdout(cmd) if err != nil { return nil, err } @@ -105,8 +108,59 @@ func GetMinSequencerBond() (*cosmossdktypes.Coin, error) { // TODO: dymd q sequencer show-sequencer could be used instead func IsRegisteredAsSequencer(seq []Info, addr string) bool { + if len(seq) == 0 { + return false + } + return slices.ContainsFunc( seq, func(s Info) bool { return strings.Compare(s.Address, addr) == 0 }, ) } + +func GetSequencersByRollappID(raID string) (*Sequencers, error) { + cmd := exec.Command( + consts.Executables.Dymension, + "q", "sequencer", "show-sequencers-by-rollapp", + raID, "-o", "json", + ) + + var sequencers Sequencers + out, err := bash.ExecCommandWithStdout(cmd) + if err != nil { + return nil, err + } + + err = json.Unmarshal(out.Bytes(), &sequencers) + if err != nil { + return nil, err + } + + return &sequencers, nil +} + +func GetLatestSnapshot(raID string) (*SnapshotInfo, error) { + sequencers, err := GetSequencersByRollappID(raID) + if err != nil { + return nil, err + } + + var latestSnapshot *SnapshotInfo + maxHeight := 0 + + for _, s := range sequencers.Sequencers { + for _, snapshot := range s.Metadata.Snapshots { + height, err := strconv.Atoi(snapshot.Height) + if err != nil { + continue + } + + if height > maxHeight { + maxHeight = height + latestSnapshot = snapshot + } + } + } + + return latestSnapshot, nil +} diff --git a/utils/sequencer/types.go b/utils/sequencer/types.go index c1a6cfe7..d8a9c992 100644 --- a/utils/sequencer/types.go +++ b/utils/sequencer/types.go @@ -3,11 +3,16 @@ package sequencer import ( "time" + cosmossdkmath "cosmossdk.io/math" cosmossdktypes "github.com/cosmos/cosmos-sdk/types" dymensionseqtypes "github.com/dymensionxyz/dymension/v3/x/sequencer/types" "github.com/gogo/protobuf/types" ) +type Sequencers struct { + Sequencers []Info `json:"sequencers,omitempty"` +} + type Info struct { // address is the bech32-encoded address of the sequencer account which is the account that the message was sent from. Address string `protobuf:"bytes,1,opt,name=address,proto3" json:"address,omitempty"` @@ -16,7 +21,7 @@ type Info struct { // rollappId defines the rollapp to which the sequencer belongs. RollappId string `protobuf:"bytes,3,opt,name=rollappId,proto3" json:"rollappId,omitempty"` // metadata defines the extra information for the sequencer. - Metadata dymensionseqtypes.SequencerMetadata `protobuf:"bytes,4,opt,name=metadata,proto3" json:"metadata"` + Metadata Metadata `protobuf:"bytes,4,opt,name=metadata,proto3" json:"metadata"` // jailed defined whether the sequencer has been jailed from bonded status or not. Jailed bool `protobuf:"varint,5,opt,name=jailed,proto3" json:"jailed,omitempty"` // proposer defines whether the sequencer is a proposer or not. @@ -30,3 +35,44 @@ type Info struct { // unbond_time defines, if unbonding, the min time for the sequencer to complete unbonding. UnbondTime time.Time `protobuf:"bytes,10,opt,name=unbond_time,json=unbondTime,proto3,stdtime" json:"unbond_time"` } + +type Metadata struct { + // moniker defines a human-readable name for the sequencer. + Moniker string `protobuf:"bytes,1,opt,name=moniker,proto3" json:"moniker,omitempty"` + // details define other optional details. + Details string `protobuf:"bytes,5,opt,name=details,proto3" json:"details,omitempty"` + // bootstrap nodes list + P2PSeeds []string `protobuf:"bytes,6,rep,name=p2p_seeds,json=p2pSeeds,proto3" json:"p2p_seeds,omitempty"` + // RPCs list + Rpcs []string `protobuf:"bytes,7,rep,name=rpcs,proto3" json:"rpcs,omitempty"` + // evm RPCs list + EvmRpcs []string `protobuf:"bytes,8,rep,name=evm_rpcs,json=evmRpcs,proto3" json:"evm_rpcs,omitempty"` + // REST API URLs + RestApiUrls []string `protobuf:"bytes,9,rep,name=rest_api_urls,json=restApiUrls,proto3" json:"rest_api_urls,omitempty"` + // block explorer URL + ExplorerUrl string `protobuf:"bytes,10,opt,name=explorer_url,json=explorerUrl,proto3" json:"explorer_url,omitempty"` + // genesis URLs + GenesisUrls []string `protobuf:"bytes,11,rep,name=genesis_urls,json=genesisUrls,proto3" json:"genesis_urls,omitempty"` + // contact details + // nolint:govet,staticcheck + ContactDetails *dymensionseqtypes.ContactDetails `protobuf:"bytes,12,opt,name=contact_details, +json=contactDetails, +proto3" json:"contact_details,omitempty"` + // json dump the sequencer can add (limited by size) + ExtraData []byte `protobuf:"bytes,13,opt,name=extra_data,json=extraData,proto3" json:"extra_data,omitempty"` + // snapshots of the sequencer + Snapshots []*SnapshotInfo `protobuf:"bytes,14,rep,name=snapshots,proto3" json:"snapshots,omitempty"` + // gas_price defines the value for each gas unit + // nolint:govet,staticcheck + GasPrice *cosmossdkmath.Int `protobuf:"bytes,15,opt,name=gas_price,json=gasPrice,proto3, +customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"gas_price,omitempty"` +} + +type SnapshotInfo struct { + // the snapshot url + SnapshotUrl string `protobuf:"bytes,1,opt,name=snapshot_url,json=snapshotUrl,proto3" json:"snapshot_url,omitempty"` + // The snapshot height + Height string `protobuf:"varint,2,opt,name=height,proto3" json:"height,omitempty"` + // sha-256 checksum value for the snapshot file + Checksum string `protobuf:"bytes,3,opt,name=checksum,proto3" json:"checksum,omitempty"` +} diff --git a/utils/service_manager/service.go b/utils/service_manager/service.go index b23e2340..9e6c226d 100644 --- a/utils/service_manager/service.go +++ b/utils/service_manager/service.go @@ -8,7 +8,8 @@ import ( "time" "github.com/dymensionxyz/roller/cmd/utils" - "github.com/dymensionxyz/roller/config" + "github.com/dymensionxyz/roller/utils/bash" + "github.com/dymensionxyz/roller/utils/config" ) type ServiceConfig struct { @@ -76,7 +77,7 @@ func (s *ServiceConfig) AddService(name string, data Service) { s.Services[name] = data } -func (s *ServiceConfig) RunServiceWithRestart(name string, options ...utils.CommandOption) { +func (s *ServiceConfig) RunServiceWithRestart(name string, options ...bash.CommandOption) { if _, ok := s.Services[name]; !ok { panic("service with that name does not exist") }