Skip to content

Commit

Permalink
fix: Verify rollapp hub height is valid before creating a connection (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ItayLevyOfficial authored Aug 29, 2023
1 parent 5e5b21c commit 2d422e5
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 13 deletions.
4 changes: 2 additions & 2 deletions cmd/config/set/da.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func setDA(rlpCfg config.RollappConfig, value string) error {
if daValue == rlpCfg.DA {
return nil
}
supportedDas := []config.DAType{config.Celestia, config.Avail, config.Local}

if !config.IsValidDAType(value) {
return fmt.Errorf("invalid DA type. Supported types are: %v", supportedDas)
return fmt.Errorf("invalid DA type. Supported types are: %v", config.SupportedDas)
}
return updateDaConfig(rlpCfg, daValue)
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/relayer/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package start
import (
"context"
"fmt"
"github.com/dymensionxyz/roller/sequencer"
"math/big"

"github.com/dymensionxyz/roller/cmd/consts"
Expand Down Expand Up @@ -48,7 +49,8 @@ func Start() *cobra.Command {
fmt.Println("💈 IBC transfer channel is already established!")
} else {
fmt.Println("💈 Establishing IBC transfer channel")
_, err := rly.CreateIBCChannel(override, logFileOption)
seq := sequencer.GetInstance(rollappConfig)
_, err := rly.CreateIBCChannel(override, logFileOption, seq)
utils.PrettifyErrorIfExists(err)
}

Expand Down
4 changes: 3 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const (
Avail DAType = "avail"
)

var SupportedDas = []DAType{Celestia, Avail, Local}

type RollappConfig struct {
Home string
RollappID string
Expand Down Expand Up @@ -68,7 +70,7 @@ func (c RollappConfig) Validate() error {
}

if !IsValidDAType(string(c.DA)) {
return fmt.Errorf("invalid DA type: %s", c.DA)
return fmt.Errorf("invalid DA type: %s. supported types %s", c.DA, SupportedDas)
}

if !IsValidVMType(string(c.VMType)) {
Expand Down
77 changes: 72 additions & 5 deletions relayer/create_ibc_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ package relayer
import (
"context"
"fmt"
"github.com/dymensionxyz/roller/sequencer"
"os/exec"
"path/filepath"
"strconv"
"time"

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

// Creates an IBC channel between the hub and the client, and return the source channel ID.
func (r *Relayer) CreateIBCChannel(override bool, logFileOption utils.CommandOption) (ConnectionChannels, error) {
// CreateIBCChannel Creates an IBC channel between the hub and the client, and return the source channel ID.
func (r *Relayer) CreateIBCChannel(override bool, logFileOption utils.CommandOption, seq *sequencer.Sequencer,
) (ConnectionChannels, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand All @@ -25,16 +29,23 @@ func (r *Relayer) CreateIBCChannel(override bool, logFileOption utils.CommandOpt
return ConnectionChannels{}, err
}

//after succesfull update clients, keep running in the background
//after successful update clients, keep running in the background
updateClientsCmd := r.GetUpdateClientsCmd()
utils.RunCommandEvery(ctx, updateClientsCmd.Path, updateClientsCmd.Args[1:], 10, utils.WithDiscardLogging())

createConnectionCmd := r.getCreateConnectionCmd(override)
status = "Creating block..."
fmt.Printf("💈 %s\n", status)
if err := r.WriteRelayerStatus(status); err != nil {
return ConnectionChannels{}, err
}
if err := waitForValidRollappHeight(seq); err != nil {
return ConnectionChannels{}, err
}
status = "Creating connection..."
fmt.Printf("💈 %s\n", status)
if err := r.WriteRelayerStatus(status); err != nil {
return ConnectionChannels{}, err
}
createConnectionCmd := r.getCreateConnectionCmd(override)
if err := utils.ExecBashCmd(createConnectionCmd, logFileOption); err != nil {
return ConnectionChannels{}, err
}
Expand Down Expand Up @@ -73,6 +84,62 @@ func (r *Relayer) CreateIBCChannel(override bool, logFileOption utils.CommandOpt
}, nil
}

func waitForValidRollappHeight(seq *sequencer.Sequencer) error {
initialHubHeightStr, err := seq.GetHubHeight()
if err != nil {
return err
}
initialHubHeight, err := strconv.Atoi(initialHubHeightStr)
if err != nil {
return err
}
initialRollappHeightStr, err := seq.GetRollappHeight()
if err != nil {
return err
}
initialRollappHeight, err := strconv.Atoi(initialRollappHeightStr)
if err != nil {
return err
}
for {
time.Sleep(30 * time.Second)
hubHeightStr, err := seq.GetHubHeight()
if err != nil {
fmt.Printf("💈 Error getting rollapp hub height, %s", err.Error())
continue
}
hubHeight, err := strconv.Atoi(hubHeightStr)
if err != nil {
fmt.Printf("💈 Error converting hub height to int, %s", err.Error())
continue
}
if hubHeight < 3 {
fmt.Printf("💈 Waiting for hub height to be greater than 2, current height: %d\n", hubHeight)
continue
}
if hubHeight <= initialHubHeight {
fmt.Printf("💈 Waiting for hub height to be greater than initial height,"+
" initial height: %d,current height: %d\n", initialHubHeight, hubHeight)
continue
}
rollappHeightStr, err := seq.GetRollappHeight()
if err != nil {
fmt.Printf("💈 Error getting rollapp height, %s", err.Error())
continue
}
rollappHeight, err := strconv.Atoi(rollappHeightStr)
if err != nil {
fmt.Printf("💈 Error converting rollapp height to int, %s", err.Error())
continue
}
if rollappHeight <= initialRollappHeight {
fmt.Printf("💈 Waiting for rollapp height to be greater than initial height,"+
" initial height: %d,current height: %d\n", initialRollappHeight, rollappHeight)
}
return nil
}
}

func (r *Relayer) getCreateClientsCmd(override bool) *exec.Cmd {
args := []string{"tx", "clients"}
args = append(args, r.getRelayerDefaultArgs()...)
Expand Down
8 changes: 4 additions & 4 deletions sequencer/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Response struct {
Result Result `json:"result"`
}

func (seq *Sequencer) getRollappHeight() (string, error) {
func (seq *Sequencer) GetRollappHeight() (string, error) {
rollappRPCEndpoint := seq.GetRPCEndpoint()
resp, err := http.Get(fmt.Sprintf("%s/status", rollappRPCEndpoint))
if err != nil {
Expand Down Expand Up @@ -60,7 +60,7 @@ type HubResponse struct {
} `json:"stateInfo"`
}

func (seq *Sequencer) getHubHeight() (string, error) {
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)
Expand All @@ -83,7 +83,7 @@ func (seq *Sequencer) getHubHeight() (string, error) {
}
func (seq *Sequencer) GetSequencerStatus(config.RollappConfig) string {
// TODO: Make sure the sequencer status endpoint is being changed after block production is paused.
rolHeight, err := seq.getRollappHeight()
rolHeight, err := seq.GetRollappHeight()
if err != nil {
seq.logger.Println(err)
}
Expand All @@ -92,7 +92,7 @@ func (seq *Sequencer) GetSequencerStatus(config.RollappConfig) string {
} else if rolHeight == "-2" {
return "Unhealthy"
} else {
hubHeight, err := seq.getHubHeight()
hubHeight, err := seq.GetHubHeight()
if err != nil {
seq.logger.Println(err)
return fmt.Sprintf("Active, Height: %s", rolHeight)
Expand Down

0 comments on commit 2d422e5

Please sign in to comment.