Skip to content

Commit

Permalink
fix(relayer run): wait for rollapp to be healthy after block time cha…
Browse files Browse the repository at this point in the history
…nge (#861)
  • Loading branch information
artemijspavlovs authored Aug 26, 2024
1 parent 02d52fb commit 207b17a
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 33 deletions.
6 changes: 3 additions & 3 deletions cmd/relayer/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func Cmd() *cobra.Command {

if currentHeight <= 2 {
pterm.Warning.Println("current height is too low, updating dymint config")
err = dymintutils.UpdateDymintConfigForIBC(home, "5s")
err = dymintutils.UpdateDymintConfigForIBC(home, "5s", false)
if err != nil {
pterm.Error.Println("failed to update dymint config")
return
Expand Down Expand Up @@ -220,7 +220,7 @@ func Cmd() *cobra.Command {
pterm.Info.Println(
"updating dymint config to 5s block time for relayer configuration",
)
err = dymintutils.UpdateDymintConfigForIBC(home, "5s")
err = dymintutils.UpdateDymintConfigForIBC(home, "5s", false)
if err != nil {
pterm.Error.Println(
"failed to update dymint config for ibc creation",
Expand Down Expand Up @@ -340,7 +340,7 @@ func Cmd() *cobra.Command {
)

pterm.Info.Println("reverting dymint config to 1h")
err = dymintutils.UpdateDymintConfigForIBC(home, "1h0m0s")
err = dymintutils.UpdateDymintConfigForIBC(home, "1h0m0s", true)
if err != nil {
pterm.Error.Println("failed to update dymint config")
return
Expand Down
45 changes: 22 additions & 23 deletions relayer/create_ibc_channel.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package relayer

import (
"context"
"fmt"
"os/exec"
"path/filepath"
Expand All @@ -23,32 +22,32 @@ func (r *Relayer) CreateIBCChannel(
logFileOption bash.CommandOption,
seq *sequencer.Sequencer,
) (ConnectionChannels, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// ctx, cancel := context.WithCancel(context.Background())
// defer cancel()

// Run send funds command from sequencer to itself to make sure the chain is
// progressing for connection and channel creation.
// replaced update clients to avoid account sequence mismatch and
// premature heights updates e.g "TrustedHeight {1 x} must be less than header height {1 y}"
sequencerAddress, err := utils.GetAddressBinary(
utils.KeyConfig{
Dir: filepath.Join(seq.RlpCfg.Home, consts.ConfigDirName.Rollapp),
ID: consts.KeysIds.RollappSequencer,
}, consts.Executables.RollappEVM,
)
if err != nil {
return ConnectionChannels{}, err
}

sendFundsCmd := seq.GetSendCmd(sequencerAddress)
bash.RunCommandEvery(
ctx,
sendFundsCmd.Path,
sendFundsCmd.Args[1:],
5,
utils.WithDiscardLogging(),
)

// sequencerAddress, err := utils.GetAddressBinary(
// utils.KeyConfig{
// Dir: filepath.Join(seq.RlpCfg.Home, consts.ConfigDirName.Rollapp),
// ID: consts.KeysIds.RollappSequencer,
// }, consts.Executables.RollappEVM,
// )
// if err != nil {
// return ConnectionChannels{}, err
// }
//
// sendFundsCmd := seq.GetSendCmd(sequencerAddress)
// bash.RunCommandEvery(
// ctx,
// sendFundsCmd.Path,
// sendFundsCmd.Args[1:],
// 5,
// utils.WithDiscardLogging(),
// )
//
var status string

// Create client if it doesn't exist or override is true
Expand Down Expand Up @@ -117,7 +116,7 @@ func (r *Relayer) CreateIBCChannel(
return ConnectionChannels{}, err
}

_, _, err = r.LoadActiveChannel()
_, _, err := r.LoadActiveChannel()
if err != nil {
return ConnectionChannels{}, err
}
Expand Down
73 changes: 66 additions & 7 deletions utils/dymint/dymint.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package dymint

import (
"encoding/json"
"fmt"
"io"
"net/http"
"os/exec"
"time"

Expand Down Expand Up @@ -61,7 +65,16 @@ type dymintInstrumentationConfig struct {
PrometheusListenAddr string `toml:"prometheus_listen_addr"`
}

func UpdateDymintConfigForIBC(home string, t string) error {
type RollappHealthResponse struct {
JSONRPC string `json:"jsonrpc"`
Result struct {
IsHealthy bool `json:"isHealthy"`
Error string `json:"error"`
} `json:"result"`
ID int `json:"id"`
}

func UpdateDymintConfigForIBC(home string, t string, forceUpdate bool) error {
pterm.Info.Println("checking dymint block time settings")
dymintPath := sequencer.GetDymintFilePath(home)
dymintCfg, err := tomlconfig.Load(dymintPath)
Expand All @@ -86,11 +99,13 @@ func UpdateDymintConfigForIBC(home string, t string) error {
return err
}

if want < have {
pterm.Info.Println(
"block time is higher then recommended when creating ibc channels: ",
have,
)
if want < have || forceUpdate {
if want < have {
pterm.Info.Println(
"block time is higher then recommended when creating ibc channels: ",
have,
)
}
pterm.Info.Println("updating dymint config")

err = utils.UpdateFieldInToml(dymintPath, "max_idle_time", want.String())
Expand All @@ -117,8 +132,8 @@ func UpdateDymintConfigForIBC(home string, t string) error {
"sudo", "systemctl", "restart", "rollapp",
)

// TODO: check for the systemd service status and /health endpoint instead
time.Sleep(time.Second * 2)
waitForHealthyRollApp("http://localhost:26657")
_, err = bash.ExecCommandWithStdout(cmd)
if err != nil {
return err
Expand All @@ -129,3 +144,47 @@ func UpdateDymintConfigForIBC(home string, t string) error {

return nil
}

func waitForHealthyRollApp(url string) {
timeout := time.After(20 * time.Second)
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()

spinner, _ := pterm.DefaultSpinner.Start("waiting for rollapp to become healthy")

for {
select {
case <-timeout:
spinner.Fail("Timeout: Failed to receive expected response within 20 seconds")
return
case <-ticker.C:
// nolint:gosec
resp, err := http.Get(url)
if err != nil {
fmt.Printf("Error making request: %v\n", err)
continue
}
// nolint:errcheck
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error reading response body: %v\n", err)
continue
}

var response RollappHealthResponse
err = json.Unmarshal(body, &response)
if err != nil {
fmt.Printf("Error unmarshaling JSON: %v\n", err)
continue
}

if response.Result.IsHealthy {
spinner.Success("RollApp is healthy")
fmt.Printf("%+v\n", response)
return
}
}
}
}

0 comments on commit 207b17a

Please sign in to comment.