Skip to content

Commit

Permalink
fix: relayer setup should create keys on remote hosts (#1080)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs authored Oct 22, 2024
1 parent 9342c13 commit 14a4d8a
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 108 deletions.
5 changes: 2 additions & 3 deletions cmd/config/init/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/relayer"
"github.com/dymensionxyz/roller/utils/roller"
)

type RelayerFileChainConfig struct {
Expand Down Expand Up @@ -135,9 +134,9 @@ func addChainsConfig(
func InitializeRelayerConfig(
rollappConfig relayer.ChainConfig,
hubConfig relayer.ChainConfig,
initConfig roller.RollappConfig,
home string,
) error {
relayerHome := filepath.Join(initConfig.Home, consts.ConfigDirName.Relayer)
relayerHome := filepath.Join(home, consts.ConfigDirName.Relayer)

if err := initRelayer(relayerHome); err != nil {
return err
Expand Down
178 changes: 174 additions & 4 deletions cmd/relayer/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"strconv"

"github.com/dymensionxyz/roller/utils/config"
"github.com/pterm/pterm"
"github.com/spf13/cobra"

Expand All @@ -34,6 +35,7 @@ const (
flagOverride = "override"
)

// TODO: cleanup required, a lot of duplicate code in this cmd
func Cmd() *cobra.Command {

Check failure on line 39 in cmd/relayer/setup/setup.go

View workflow job for this annotation

GitHub Actions / lint

cyclomatic complexity 94 of func `Cmd` is high (> 30) (gocyclo)
relayerStartCmd := &cobra.Command{
Use: "setup",
Expand All @@ -51,6 +53,7 @@ func Cmd() *cobra.Command {
var hd consts.HubData
var runForExisting bool
var rollerData roller.RollappConfig
var existingRollerConfig bool

rollerConfigFilePath := filepath.Join(home, consts.RollerConfigFileName)

Expand All @@ -60,6 +63,7 @@ func Cmd() *cobra.Command {
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
pterm.Info.Println("existing roller configuration not found")
existingRollerConfig = false
runForExisting = false
} else {
pterm.Error.Println("failed to check existing roller config")
Expand Down Expand Up @@ -102,15 +106,66 @@ func Cmd() *cobra.Command {
}

if !runForExisting {
envs := []string{"playground"}
envs := []string{"playground", "custom"}
env, _ = pterm.DefaultInteractiveSelect.
WithDefaultText(
"select the environment you want to initialize relayer for",
).
WithOptions(envs).
Show()

hd = consts.Hubs[env]
if env == "playground" {
hd = consts.Hubs[env]
} else {
hd = config.GenerateCustomHubData()
// err = dependencies.InstallCustomDymdVersion()
// if err != nil {
// pterm.Error.Println("failed to install custom dymd version: ", err)
// return
// }
}

if !existingRollerConfig {
pterm.Info.Println("creating a new roller config")
err := os.MkdirAll(home, 0o755)
if err != nil {
pterm.Error.Printf(
"failed to create %s: %v", home, err,
)
return
}

_, err = os.Create(rollerConfigFilePath)
if err != nil {
pterm.Error.Printf(
"failed to create %s: %v", rollerConfigFilePath, err,
)
return
}
}

rollerTomlData := map[string]any{
"rollapp_id": raID,
"home": home,

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

for key, value := range rollerTomlData {
err = tomlconfig.UpdateFieldInFile(
rollerConfigFilePath,
key,
value,
)
if err != nil {
fmt.Printf("failed to add %s to roller.toml: %v", key, err)
return
}
}
}

// retrieve rollapp rpc endpoints
Expand Down Expand Up @@ -291,7 +346,7 @@ func Cmd() *cobra.Command {
Denom: consts.Denoms.Hub,
AddressPrefix: consts.AddressPrefixes.Hub,
GasPrices: rollerData.HubData.GAS_PRICE,
}, rollerData,
}, home,
)
if err != nil {
pterm.Error.Printf(
Expand Down Expand Up @@ -500,6 +555,121 @@ func Cmd() *cobra.Command {
}

if srcIbcChannel != "" && dstIbcChannel != "" {
if !isRelayerInitialized || shouldOverwrite {
raResponse, err := rollapp.Show(raID, hd)
if err != nil {
pterm.Error.Println("failed to retrieve rollapp information: ", err)
return
}

raRpc, err := sequencerutils.GetRpcEndpointFromChain(raID, hd)
if err != nil {
pterm.Error.Println("failed to retrieve rollapp rpc endpoint: ", err)
return
}

pterm.Info.Println("initializing relayer config")
err = initconfig.InitializeRelayerConfig(
relayer.ChainConfig{
ID: raResponse.Rollapp.RollappId,
RPC: raRpc,
Denom: raResponse.Rollapp.GenesisInfo.NativeDenom.Base,
AddressPrefix: raResponse.Rollapp.GenesisInfo.Bech32Prefix,
GasPrices: "2000000000",
}, relayer.ChainConfig{
ID: hd.ID,
RPC: hd.RPC_URL,
Denom: consts.Denoms.Hub,
AddressPrefix: consts.AddressPrefixes.Hub,
GasPrices: hd.GAS_PRICE,
}, home,
)
if err != nil {
pterm.Error.Printf(
"failed to initialize relayer config: %v\n",
err,
)
return
}

relayerKeys, err := keys.GenerateRelayerKeys(rollerData)
if err != nil {
pterm.Error.Printf("failed to create relayer keys: %v\n", err)
return
}

for _, key := range relayerKeys {
key.Print(keys.WithMnemonic(), keys.WithName())
}

keysToFund, err := keys.GetRelayerKeys(rollerData)
pterm.Info.Println(
"please fund the hub relayer key with at least 20 dym tokens: ",
)
for _, k := range keysToFund {
k.Print(keys.WithName())
}
proceed, _ := pterm.DefaultInteractiveConfirm.WithDefaultValue(false).
WithDefaultText(
"press 'y' when the wallets are funded",
).Show()
if !proceed {
return
}

if err != nil {
pterm.Error.Printf("failed to create relayer keys: %v\n", err)
return
}

if err := relayer.CreatePath(rollerData); err != nil {
pterm.Error.Printf("failed to create relayer IBC path: %v\n", err)
return
}

pterm.Info.Println("updating application relayer config")
relayerConfigPath := filepath.Join(relayerHome, "config", "config.yaml")

rollappIbcConnection, hubIbcConnection, err := rly.GetActiveConnections(
raData,
hd,
)
if err != nil {
pterm.Error.Printf("failed to retrieve active connections: %v\n", err)
return
}

updates := map[string]interface{}{
// hub
fmt.Sprintf("chains.%s.value.gas-adjustment", rollerData.HubData.ID): 1.5,
fmt.Sprintf("chains.%s.value.is-dym-hub", rollerData.HubData.ID): true,
fmt.Sprintf(
"chains.%s.value.http-addr",
rollerData.HubData.ID,
): rollerData.HubData.API_URL,
fmt.Sprintf("paths.%s.src.client-id", consts.DefaultRelayerPath): hubIbcConnection.ClientID,
fmt.Sprintf("paths.%s.src.connection-id", consts.DefaultRelayerPath): hubIbcConnection.ID,

// ra
fmt.Sprintf("chains.%s.value.gas-adjustment", raResponse.Rollapp.RollappId): 1.3,
fmt.Sprintf("chains.%s.value.is-dym-rollapp", raResponse.Rollapp.RollappId): true,
fmt.Sprintf(
"paths.%s.dst.client-id",
consts.DefaultRelayerPath,
): rollappIbcConnection.ClientID,
fmt.Sprintf("paths.%s.dst.connection-id", consts.DefaultRelayerPath): rollappIbcConnection.ID,

// misc
"extra-codecs": []string{
"ethermint",
},
}
err = yamlconfig.UpdateNestedYAML(relayerConfigPath, updates)
if err != nil {
pterm.Error.Printf("Error updating YAML: %v\n", err)
return
}
}
pterm.Info.Println("existing IBC channels found ")
pterm.Info.Println("Hub: ", srcIbcChannel)
pterm.Info.Println("RollApp: ", dstIbcChannel)
Expand Down Expand Up @@ -529,7 +699,7 @@ func Cmd() *cobra.Command {
Denom: consts.Denoms.Hub,
AddressPrefix: consts.AddressPrefixes.Hub,
GasPrices: rollappChainData.HubData.GAS_PRICE,
}, *rollappChainData,
}, home,
)
if err != nil {
pterm.Error.Printf(
Expand Down
41 changes: 4 additions & 37 deletions cmd/relayer/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/pterm/pterm"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -77,42 +76,10 @@ Consider using 'services' if you want to run a 'systemd' service instead.
raChainID := rlyConfig.Paths.HubRollapp.Dst.ChainID
hubChainID := rlyConfig.Paths.HubRollapp.Src.ChainID

_, hd, found := roller.FindHubDataByID(consts.Hubs, hubChainID)
if !found {
pterm.Error.Println("Hub Data not found for ", hubChainID)
runCustomHub, _ := pterm.DefaultInteractiveConfirm.WithDefaultText("would you like to provide custom hub?").
WithDefaultValue(false).
Show()

if !runCustomHub {
pterm.Error.Println("cancelled by user")
return
}

id, _ := pterm.DefaultInteractiveTextInput.WithDefaultText("provide hub chain id").
Show()
rpcUrl, _ := pterm.DefaultInteractiveTextInput.WithDefaultText(
"provide hub rpc endpoint (including port, example: http://dym.dev:26657)",
).Show()
restUrl, _ := pterm.DefaultInteractiveTextInput.WithDefaultText(
"provide hub rest api endpoint (including port, example: http://dym.dev:1318)",
).Show()
gasPrice, _ := pterm.DefaultInteractiveTextInput.WithDefaultText("provide gas price").
WithDefaultValue("2000000000").
Show()

id = strings.TrimSpace(id)
rpcUrl = strings.TrimSpace(rpcUrl)
restUrl = strings.TrimSpace(restUrl)
gasPrice = strings.TrimSpace(gasPrice)

hd = consts.HubData{
API_URL: restUrl,
ID: id,
RPC_URL: rpcUrl,
ARCHIVE_RPC_URL: rpcUrl,
GAS_PRICE: gasPrice,
}
hd, err := roller.LoadHubData(home)
if err != nil {
pterm.Error.Println("failed to load hub data", err)
return
}

getRaCmd := rollapp.GetRollappCmd(raChainID, hd)
Expand Down
Loading

0 comments on commit 14a4d8a

Please sign in to comment.