Skip to content

Commit

Permalink
Implemented relayer running
Browse files Browse the repository at this point in the history
  • Loading branch information
ItayLevyOfficial committed Jun 22, 2023
1 parent 8619333 commit 66597b4
Show file tree
Hide file tree
Showing 15 changed files with 278 additions and 72 deletions.
2 changes: 1 addition & 1 deletion cmd/config/init/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var Hubs = map[string]utils.HubData{
},
LocalHubID: {
API_URL: "http://localhost:1317",
ID: "local",
ID: "local-testnet",
RPC_URL: "http://localhost:36657",
},
}
Expand Down
7 changes: 5 additions & 2 deletions cmd/config/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package initconfig

import (
"fmt"
"github.com/dymensionxyz/roller/cmd/consts"
"os"
"path/filepath"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -48,9 +49,11 @@ func InitCmd() *cobra.Command {
addresses, err := generateKeys(initConfig)
utils.PrettifyErrorIfExists(err)
utils.PrettifyErrorIfExists(initializeLightNodeConfig(initConfig))
daAddress, err := utils.GetCelestiaAddress(filepath.Join(initConfig.Home, consts.ConfigDirName.DALightNode, consts.KeysDirName))
utils.PrettifyErrorIfExists(err)
addresses[consts.KeyNames.DALightNode] = daAddress
initializeRollappConfig(initConfig)
utils.PrettifyErrorIfExists(initializeRollappGenesis(initConfig))

utils.PrettifyErrorIfExists(utils.WriteConfigToTOML(initConfig))
printInitOutput(addresses, initConfig.RollappID)
},
Expand Down
42 changes: 8 additions & 34 deletions cmd/config/init/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"path/filepath"
"strconv"

"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
)
Expand All @@ -28,45 +26,21 @@ func generateSequencersKeys(initConfig utils.RollappConfig) (map[string]string,
keys := getSequencerKeysConfig()
addresses := make(map[string]string)
for _, key := range keys {
var address string
var err error
if key.Prefix == consts.AddressPrefixes.Rollapp {
address, err := createAddressBinary(key, consts.Executables.RollappEVM, initConfig.Home)
if err != nil {
return nil, err
}
addresses[key.ID] = address
address, err = createAddressBinary(key, consts.Executables.RollappEVM, initConfig.Home)
} else {
keyInfo, err := createKey(key, initConfig.Home)
if err != nil {
return nil, err
}
formattedAddress, err := utils.KeyInfoToBech32Address(keyInfo, key.Prefix)
if err != nil {
return nil, err
}
addresses[key.ID] = formattedAddress
address, err = createAddressBinary(key, consts.Executables.Dymension, initConfig.Home)
}
if err != nil {
return nil, err
}
addresses[key.ID] = address
}
return addresses, nil
}

func createKey(keyConfig utils.KeyConfig, home string) (keyring.Info, error) {
kr, err := keyring.New(
"",
keyring.BackendTest,
filepath.Join(home, keyConfig.Dir),
nil,
)
if err != nil {
return nil, err
}
bip44Params := hd.NewFundraiserParams(0, keyConfig.CoinType, 0)
info, _, err := kr.NewMnemonic(keyConfig.ID, keyring.English, bip44Params.String(), "", hd.Secp256k1)
if err != nil {
return nil, err
}
return info, nil
}

func getSequencerKeysConfig() []utils.KeyConfig {
return []utils.KeyConfig{
{
Expand Down
11 changes: 6 additions & 5 deletions cmd/register/bash_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func getRegisterRollappCmd(rollappConfig utils.RollappConfig) *exec.Cmd {
cmdArgs := []string{
"tx", "rollapp", "create-rollapp", rollappConfig.RollappID, "stamp1", "genesis-path/1", "3", "3", `{"Addresses":[]}`,
}
cmdArgs = append(cmdArgs, getCommonFlags(rollappConfig)...)
cmdArgs = append(cmdArgs, getCommonDymdTxFlags(rollappConfig)...)
return exec.Command(
consts.Executables.Dymension, cmdArgs...,
)
Expand Down Expand Up @@ -50,16 +50,17 @@ func getRegisterSequencerCmd(rollappConfig utils.RollappConfig) (*exec.Cmd, erro
rollappConfig.RollappID,
description,
}
cmdArgs = append(cmdArgs, getCommonFlags(rollappConfig)...)
cmdArgs = append(cmdArgs, getCommonDymdTxFlags(rollappConfig)...)
return exec.Command(consts.Executables.Dymension, cmdArgs...), nil
}

func getCommonFlags(rollappConfig utils.RollappConfig) []string {
return []string{
func getCommonDymdTxFlags(rollappConfig utils.RollappConfig) []string {
commonFlags := utils.GetCommonDymdFlags(rollappConfig)
txArgs := []string{
"--from", consts.KeyNames.HubSequencer,
"--keyring-backend", "test",
"--keyring-dir", filepath.Join(rollappConfig.Home, consts.ConfigDirName.Rollapp),
"--node", rollappConfig.HubData.RPC_URL, "--output", "json",
"--yes", "--broadcast-mode", "block", "--chain-id", rollappConfig.HubData.ID,
}
return append(commonFlags, txArgs...)
}
4 changes: 2 additions & 2 deletions cmd/register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
"encoding/json"

initconfig "github.com/dymensionxyz/roller/cmd/config/init"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/spf13/cobra"
)

func RegisterCmd() *cobra.Command {
func Cmd() *cobra.Command {
registerCmd := &cobra.Command{
Use: "register",
Short: "Registers the rollapp and the sequencer to the Dymension hub.",
Expand Down
73 changes: 73 additions & 0 deletions cmd/relayer/start/create_ibc_channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package start

import (
"fmt"
"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"os/exec"
"path/filepath"
)

func createIBCChannelIfNeeded(rollappConfig utils.RollappConfig) (string, error) {
/**
Creates an IBC channel between the hub and the client, and return the source channel ID.
*/
createClientsCmd := getCreateClientsCmd(rollappConfig, rollappConfig.RollappID, rollappConfig.HubData.ID)
fmt.Println("Creating clients...")
if err := utils.ExecBashCmdWithOSOutput(createClientsCmd); err != nil {
return "", err
}
dstConnectionId, err := GetDstConnectionIDFromYAMLFile(filepath.Join(rollappConfig.Home, consts.ConfigDirName.Relayer,
"config", "config.yaml"))
if err != nil {
return "", err
}
if dstConnectionId == "" {
createConnectionCmd := getCreateConnectionCmd(rollappConfig)
fmt.Println("Creating connection...")
if err := utils.ExecBashCmdWithOSOutput(createConnectionCmd); err != nil {
return "", err
}
}
srcChannelId, err := GetSourceChannelForConnection(dstConnectionId, rollappConfig)
if err != nil {
return "", err
}
if srcChannelId == "" {
createChannelCmd := getCreateChannelCmd(rollappConfig)
fmt.Println("Creating channel...")
if err := utils.ExecBashCmdWithOSOutput(createChannelCmd); err != nil {
return "", err
}
srcChannelId, err = GetSourceChannelForConnection(dstConnectionId, rollappConfig)
if err != nil {
return "", err
}
}
return srcChannelId, nil
}

func getCreateChannelCmd(config utils.RollappConfig) *exec.Cmd {
defaultRlyArgs := getRelayerDefaultArgs(config)
args := []string{"tx", "channel", "--override"}
args = append(args, defaultRlyArgs...)
return exec.Command(consts.Executables.Relayer, args...)
}

func getCreateClientsCmd(rollappConfig utils.RollappConfig, srcId string, dstId string) *exec.Cmd {
defaultRlyArgs := getRelayerDefaultArgs(rollappConfig)
args := []string{"tx", "clients"}
args = append(args, defaultRlyArgs...)
return exec.Command(consts.Executables.Relayer, args...)
}

func getRelayerDefaultArgs(config utils.RollappConfig) []string {
return []string{consts.DefaultRelayerPath, "--home", filepath.Join(config.Home, consts.ConfigDirName.Relayer)}
}

func getCreateConnectionCmd(config utils.RollappConfig) *exec.Cmd {
defaultRlyArgs := getRelayerDefaultArgs(config)
args := []string{"tx", "connection"}
args = append(args, defaultRlyArgs...)
return exec.Command(consts.Executables.Relayer, args...)
}
53 changes: 53 additions & 0 deletions cmd/relayer/start/get_active_src_channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package start

import (
"encoding/json"
"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"os/exec"
)

func GetSourceChannelForConnection(dstConnectionID string, rollappConfig utils.RollappConfig) (string, error) {
/**
Returns the open source channel for the given destination connection ID. If no open channel exists, it returns an
emtpy string.
*/
commonDymdFlags := utils.GetCommonDymdFlags(rollappConfig)
args := []string{"query", "ibc", "channel", "connections", dstConnectionID}
args = append(args, commonDymdFlags...)
cmd := exec.Command(consts.Executables.Dymension, args...)
out, err := cmd.Output()
if err != nil {
return "", err
}
channelId, err := GetOpenStateChannelID(out)
if err != nil {
return "", err
}
return channelId, nil
}

type Channel struct {
State string `json:"state"`
Counterparty struct {
ChannelID string `json:"channel_id"`
} `json:"counterparty"`
}

type ChannelList struct {
Channels []Channel `json:"channels"`
}

func GetOpenStateChannelID(jsonData []byte) (string, error) {
var channels ChannelList
if err := json.Unmarshal(jsonData, &channels); err != nil {
return "", err
}

for _, channel := range channels.Channels {
if channel.State == "STATE_OPEN" {
return channel.Counterparty.ChannelID, nil
}
}
return "", nil
}
38 changes: 38 additions & 0 deletions cmd/relayer/start/get_dst_connection_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package start

import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
)

type RelayerConfigFile struct {
Paths map[string]Path `yaml:"paths"`
}

type Path struct {
Dst Destination `yaml:"dst"`
}

type Destination struct {
ConnectionID string `yaml:"connection-id"`
}

func GetDstConnectionIDFromYAMLFile(filename string) (string, error) {
/**
Returns the destination connection ID if it been created already, an empty string otherwise.
*/
data, err := ioutil.ReadFile(filename)
if err != nil {
return "", err
}
var config RelayerConfigFile
err = yaml.Unmarshal(data, &config)
if err != nil {
return "", err
}
for _, path := range config.Paths {
return path.Dst.ConnectionID, nil
}
return "", fmt.Errorf("No paths found in YAML data")
}
39 changes: 31 additions & 8 deletions cmd/relayer/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"path/filepath"
)

type RelayerConfig struct {
SrcChannelName string
}

func Start() *cobra.Command {
registerCmd := &cobra.Command{
Use: "start",
Expand All @@ -17,21 +21,40 @@ func Start() *cobra.Command {
home := cmd.Flag(utils.FlagNames.Home).Value.String()
rollappConfig, err := utils.LoadConfigFromTOML(home)
utils.PrettifyErrorIfExists(err)
fmt.Println(rollappConfig)
createChannelCmd := getCreateChannelCmd(rollappConfig)
err = utils.ExecBashCommandWithOSOutput(createChannelCmd)
srcChannelId, err := createIBCChannelIfNeeded(rollappConfig)
utils.PrettifyErrorIfExists(err)
updateClientsCmd := getUpdateClientsCmd(rollappConfig)
utils.RunCommandEvery(updateClientsCmd.Path, updateClientsCmd.Args[1:], 60)
relayPacketsCmd := getRelayPacketsCmd(rollappConfig, srcChannelId)
utils.RunCommandEvery(relayPacketsCmd.Path, relayPacketsCmd.Args[1:], 30)
startCmd := getRlyStartCmd(rollappConfig)
utils.RunBashCmdAsync(startCmd, func() {
fmt.Printf("💈 The relayer is running successfully on you local machine on channel %s!", srcChannelId)
}, parseError)
},
}
utils.AddGlobalFlags(registerCmd)
return registerCmd
}

func getCreateChannelCmd(rollappConfig utils.RollappConfig) *exec.Cmd {
fmt.Println("Creating IBC channel...")
relayerHome := filepath.Join(rollappConfig.Home, consts.ConfigDirName.Relayer)
defaultRlyArgs := utils.GetRelayerDefaultFlags(relayerHome)
args := []string{"transact", "link", "-t300s", consts.DefaultRelayerPath}
func parseError(errStr string) string {
// TODO
return errStr
}

func getRlyStartCmd(config utils.RollappConfig) *exec.Cmd {
return exec.Command(consts.Executables.Relayer, "start", consts.DefaultRelayerPath, "-l", "1", "--home",
filepath.Join(config.Home, consts.ConfigDirName.Relayer))
}

func getUpdateClientsCmd(config utils.RollappConfig) *exec.Cmd {
defaultRlyArgs := getRelayerDefaultArgs(config)
args := []string{"tx", "update-clients"}
args = append(args, defaultRlyArgs...)
return exec.Command(consts.Executables.Relayer, args...)
}

func getRelayPacketsCmd(config utils.RollappConfig, srcChannel string) *exec.Cmd {
return exec.Command(consts.Executables.Relayer, "tx", "relay-packets", consts.DefaultRelayerPath, srcChannel,
"-l", "1", "--home", filepath.Join(config.Home, consts.ConfigDirName.Relayer))
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Execute() {
func init() {
rootCmd.AddCommand(config.ConfigCmd())
rootCmd.AddCommand(version.VersionCmd())
rootCmd.AddCommand(register.RegisterCmd())
rootCmd.AddCommand(register.Cmd())
rootCmd.AddCommand(da_light_client.DALightClientCmd())
rootCmd.AddCommand(sequencer.SequencerCmd())
rootCmd.AddCommand(relayer.Cmd())
Expand Down
2 changes: 1 addition & 1 deletion cmd/sequencer/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func getStartRollapCmd(rollappConfig utils.RollappConfig, lightNodeEndpoint stri
"--dymint.da_config", daConfig,
"--dymint.settlement_layer", "dymension",
"--dymint.settlement_config", settlementConfig,
"--dymint.block_batch_size", "1200",
"--dymint.block_batch_size", "50",
"--dymint.namespace_id", "000000000000ffff",
"--dymint.block_time", "0.2s",
"--home", rollappConfigDir,
Expand Down
Loading

0 comments on commit 66597b4

Please sign in to comment.