Skip to content

Commit

Permalink
feat: finalize relayer flow (#833)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs authored Aug 15, 2024
1 parent c5f5189 commit ef430b1
Show file tree
Hide file tree
Showing 25 changed files with 745 additions and 437 deletions.
7 changes: 0 additions & 7 deletions cmd/config/init/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ type PathValue struct {
Value interface{}
}

func GetGenesisFilePath(root string) string {
return filepath.Join(
RollappConfigDir(root),
"genesis.json",
)
}

// TODO(#130): fix to support epochs
func getDefaultGenesisParams(
sequencerAddr, genesisOperatorAddress string, raCfg *config.RollappConfig,
Expand Down
50 changes: 44 additions & 6 deletions cmd/config/init/rollapp.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package initconfig

import (
"encoding/json"
"os/exec"
"path/filepath"

comettypes "github.com/cometbft/cometbft/types"
"github.com/pterm/pterm"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/sequencer"
"github.com/dymensionxyz/roller/utils/bash"
"github.com/dymensionxyz/roller/utils/config"
genesisutils "github.com/dymensionxyz/roller/utils/genesis"
)

func InitializeRollappConfig(initConfig config.RollappConfig) error {
func InitializeRollappConfig(initConfig *config.RollappConfig, hd consts.HubData) error {
home := filepath.Join(initConfig.Home, consts.ConfigDirName.Rollapp)

initRollappCmd := exec.Command(
Expand All @@ -28,7 +33,44 @@ func InitializeRollappConfig(initConfig config.RollappConfig) error {
return err
}

err = setRollappConfig(initConfig)
if initConfig.HubData.ID != "mock" {
err := genesisutils.DownloadGenesis(initConfig.Home, *initConfig)
if err != nil {
pterm.Error.Println("failed to download genesis file: ", err)
return err
}

isChecksumValid, err := genesisutils.CompareGenesisChecksum(
initConfig.Home,
initConfig.RollappID,
hd,
)
if !isChecksumValid {
return err
}

genesisDoc, err := comettypes.GenesisDocFromFile(
genesisutils.GetGenesisFilePath(initConfig.Home),
)
if err != nil {
return err
}

// TODO: refactor
var need genesisutils.AppState
j, _ := genesisDoc.AppState.MarshalJSON()
err = json.Unmarshal(j, &need)
if err != nil {
return err
}
rollappBaseDenom := need.Bank.Supply[0].Denom
rollappDenom := rollappBaseDenom[1:]

initConfig.BaseDenom = rollappBaseDenom
initConfig.Denom = rollappDenom
}

err = setRollappConfig(*initConfig)
if err != nil {
return err
}
Expand All @@ -48,7 +90,3 @@ func setRollappConfig(rlpCfg config.RollappConfig) error {
}
return nil
}

func RollappConfigDir(root string) string {
return filepath.Join(root, consts.ConfigDirName.Rollapp, "config")
}
4 changes: 1 addition & 3 deletions cmd/da-light-client/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
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"
Expand Down Expand Up @@ -69,7 +68,6 @@ func Cmd() *cobra.Command {
ctx, cancel := context.WithCancel(context.Background())

fmt.Println(startDALCCmd.String())

defer cancel()
go bash.RunCmdAsync(
ctx,
Expand All @@ -88,7 +86,7 @@ func Cmd() *cobra.Command {

func addFlags(cmd *cobra.Command) {
cmd.Flags().
StringP(rpcEndpointFlag, "", celestia.DefaultCelestiaRPC, "The DA rpc endpoint to connect to.")
StringP(rpcEndpointFlag, "", "celestia-testnet-consensus.itrocket.net", "The DA rpc endpoint to connect to.")
cmd.Flags().
StringP(metricsEndpointFlag, "", "", "The OTEL collector metrics endpoint to connect to.")
}
Expand Down
8 changes: 7 additions & 1 deletion cmd/relayer/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import (
"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/relayer/run"
"github.com/dymensionxyz/roller/cmd/relayer/start"
"github.com/dymensionxyz/roller/cmd/relayer/status"
"github.com/dymensionxyz/roller/cmd/services"
loadservices "github.com/dymensionxyz/roller/cmd/services/load"
startservices "github.com/dymensionxyz/roller/cmd/services/start"
)

func Cmd() *cobra.Command {
Expand All @@ -13,7 +17,9 @@ func Cmd() *cobra.Command {
Short: "Commands for running and managing the RollApp relayer.",
}
cmd.AddCommand(run.Cmd())
// cmd.AddCommand(start.Cmd())
cmd.AddCommand(start.Cmd())
cmd.AddCommand(status.Cmd())
cmd.AddCommand(services.Cmd(loadservices.RelayerCmd(), startservices.RelayerCmd()))

return cmd
}
68 changes: 56 additions & 12 deletions cmd/relayer/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package run

import (
"context"
"encoding/json"
"fmt"
"math/big"
"os"
"path/filepath"
"strconv"

comettypes "github.com/cometbft/cometbft/types"
"github.com/pterm/pterm"
"github.com/spf13/cobra"

Expand All @@ -22,6 +24,7 @@ import (
"github.com/dymensionxyz/roller/utils/config/tomlconfig"
dymintutils "github.com/dymensionxyz/roller/utils/dymint"
"github.com/dymensionxyz/roller/utils/errorhandling"
genesisutils "github.com/dymensionxyz/roller/utils/genesis"
rollapputils "github.com/dymensionxyz/roller/utils/rollapp"
)

Expand All @@ -43,15 +46,41 @@ 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)

genesis, err := comettypes.GenesisDocFromFile(genesisutils.GetGenesisFilePath(home))
if err != nil {
return
}

// TODO: refactor
var need genesisutils.AppState
j, _ := genesis.AppState.MarshalJSON()
json.Unmarshal(j, &need)
rollappDenom := need.Bank.Supply[0].Denom

rollerConfigFilePath := filepath.Join(home, consts.RollerConfigFileName)
globalutils.UpdateFieldInToml(rollerConfigFilePath, "base_denom", rollappDenom)

rollappConfig, err := tomlconfig.LoadRollerConfig(home)
if err != nil {
pterm.Error.Printf("failed to load rollapp config: %v\n", err)
return
}
rollerConfigFilePath := filepath.Join(home, "roller.toml")
relayerLogFilePath := utils.GetRelayerLogPath(rollappConfig)
relayerLogger := utils.GetLogger(relayerLogFilePath)

hd, err := tomlconfig.LoadHubData(home)
if err != nil {
pterm.Error.Println("failed to load hub data from roller.toml")
}

rollappChainData, err := tomlconfig.LoadRollappMetadataFromChain(
home,
rollappConfig.RollappID,
&hd,
)
errorhandling.PrettifyErrorIfExists(err)

/* ---------------------------- Initialize relayer --------------------------- */
outputHandler := initconfig.NewOutputHandler(false)
isRelayerInitialized, err := globalutils.DirNotEmpty(relayerHome)
Expand Down Expand Up @@ -93,37 +122,35 @@ func Cmd() *cobra.Command {
return
}
currentHeight, err := strconv.Atoi(
strconv.FormatInt(blockInformation.Block.Header.Height, 10),
blockInformation.Block.Header.Height,
)
if err != nil {
pterm.Error.Printf("failed to get current block height: %v\n", err)
return
}
if currentHeight <= 2 {
pterm.Warning.Println("current height is too low, updating dymint config")
err := dymintutils.UpdateDymintConfigForIBC(home)
err := dymintutils.UpdateDymintConfigForIBC(home, "5s")
if err != nil {
pterm.Error.Println("failed to update dymint config")
return
}
}

rollappPrefix, err := globalutils.GetKeyFromTomlFile(
rollerConfigFilePath,
"bech32_prefix",
)
rollappPrefix := rollappChainData.Bech32Prefix
if err != nil {
pterm.Error.Printf("failed to retrieve bech32_prefix: %v\n", err)
return
}

pterm.Info.Println("initializing relayer config")
err = initconfig.InitializeRelayerConfig(
relayer.ChainConfig{
ID: rollappConfig.RollappID,
RPC: consts.DefaultRollappRPC,
Denom: rollappConfig.Denom,
Denom: rollappDenom,
AddressPrefix: rollappPrefix,
GasPrices: "0",
GasPrices: "1000000000",
}, relayer.ChainConfig{
ID: rollappConfig.HubData.ID,
RPC: rollappConfig.HubData.RPC_URL,
Expand All @@ -140,7 +167,10 @@ func Cmd() *cobra.Command {
return
}

err = dymintutils.UpdateDymintConfigForIBC(home)
pterm.Info.Println(
"updating dymint config to 5s block time for relayer configuration",
)
err = dymintutils.UpdateDymintConfigForIBC(home, "5s")
if err != nil {
pterm.Error.Println(
"failed to update dymint config for ibc creation",
Expand Down Expand Up @@ -168,7 +198,7 @@ func Cmd() *cobra.Command {
}
logFileOption := utils.WithLoggerLogging(relayerLogger)

errorhandling.RequireMigrateIfNeeded(rollappConfig)
// errorhandling.RequireMigrateIfNeeded(rollappConfig)

err = rollappConfig.Validate()
if err != nil {
Expand Down Expand Up @@ -236,6 +266,7 @@ func Cmd() *cobra.Command {

pterm.Info.Println("establishing IBC transfer channel")
seq := sequencer.GetInstance(rollappConfig)

_, err = rly.CreateIBCChannel(shouldOverwrite, logFileOption, seq)
if err != nil {
pterm.Error.Printf("failed to create IBC channel: %v\n", err)
Expand All @@ -258,7 +289,20 @@ func Cmd() *cobra.Command {
rly.DstChannel,
)

select {}
pterm.Info.Println("reverting dymint config to 1h")
err = dymintutils.UpdateDymintConfigForIBC(home, "1h0m0s")
if err != nil {
pterm.Error.Println("failed to update dymint config")
return
}

// select {}
pterm.Info.Println("next steps:")
pterm.Info.Printf(
"run %s to start rollapp and da-light-client on your local machine\n",
pterm.DefaultBasicText.WithStyle(pterm.FgYellow.ToStyle()).
Sprintf("roller relayer services load"),
)
},
}

Expand Down
30 changes: 18 additions & 12 deletions cmd/relayer/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"fmt"
"math/big"

"github.com/pterm/pterm"
"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"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"
Expand All @@ -27,15 +27,15 @@ const (
flagOverride = "override"
)

func Start() *cobra.Command {
func Cmd() *cobra.Command {
relayerStartCmd := &cobra.Command{
Use: "start",
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 := tomlconfig.LoadRollerConfig(home)
errorhandling.PrettifyErrorIfExists(err)
errorhandling.RequireMigrateIfNeeded(rollappConfig)
// errorhandling.RequireMigrateIfNeeded(rollappConfig)

VerifyRelayerBalances(rollappConfig)
relayerLogFilePath := utils.GetRelayerLogPath(rollappConfig)
Expand All @@ -51,30 +51,36 @@ func Start() *cobra.Command {
_, _, err = rly.LoadActiveChannel()
errorhandling.PrettifyErrorIfExists(err)

override := cmd.Flag(flagOverride).Changed
if override {
fmt.Println("πŸ’ˆ Overriding the existing relayer channel")
}
if rly.ChannelReady() && !override {
// override := cmd.Flag(flagOverride).Changed
//
// if override {
// fmt.Println("πŸ’ˆ Overriding the existing relayer channel")
// }

if rly.ChannelReady() {
fmt.Println("πŸ’ˆ IBC transfer channel is already established!")
status := fmt.Sprintf("Active src, %s <-> %s, dst", rly.SrcChannel, rly.DstChannel)
err := rly.WriteRelayerStatus(status)
errorhandling.PrettifyErrorIfExists(err)
} else {
fmt.Println("πŸ’ˆ Establishing IBC transfer channel")
seq := sequencer.GetInstance(rollappConfig)
_, err := rly.CreateIBCChannel(override, logFileOption, seq)
errorhandling.PrettifyErrorIfExists(err)
pterm.Error.Println("πŸ’ˆ No channels found, ensure you've setup the relayer")
// seq := sequencer.GetInstance(rollappConfig)
// _, err := rly.CreateIBCChannel(override, logFileOption, seq)
// errorhandling.PrettifyErrorIfExists(err)
return
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go bash.RunCmdAsync(
ctx,
rly.GetStartCmd(),
func() {},
func(errMessage string) string { return errMessage },
logFileOption,
)

fmt.Printf(
"πŸ’ˆ The relayer is running successfully on you local machine! Channels: src, %s <-> %s, dst",
rly.SrcChannel,
Expand Down
Loading

0 comments on commit ef430b1

Please sign in to comment.