Skip to content

Commit

Permalink
fix: improve relayer flow and address handling (#869)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs authored Aug 30, 2024
1 parent dd2064d commit dcbe2d1
Show file tree
Hide file tree
Showing 13 changed files with 222 additions and 569 deletions.
50 changes: 30 additions & 20 deletions cmd/config/init/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func getSequencerKeysConfig(rollappConfig config.RollappConfig) []utils.KeyConfi
}
}

func getRelayerKeysConfig(rollappConfig config.RollappConfig) map[string]utils.KeyConfig {
func GetRelayerKeysConfig(rollappConfig config.RollappConfig) map[string]utils.KeyConfig {
return map[string]utils.KeyConfig{
consts.KeysIds.RollappRelayer: {
Dir: path.Join(rollappConfig.Home, consts.ConfigDirName.Relayer),
Expand Down Expand Up @@ -101,7 +101,7 @@ func CreateAddressBinary(
func GetRelayerKeys(rollappConfig config.RollappConfig) ([]utils.KeyInfo, error) {
pterm.Info.Println("getting relayer keys")
relayerAddresses := make([]utils.KeyInfo, 0)
keys := getRelayerKeysConfig(rollappConfig)
keys := GetRelayerKeysConfig(rollappConfig)

showRollappKeyCmd := getShowRlyKeyCmd(
keys[consts.KeysIds.RollappRelayer],
Expand Down Expand Up @@ -142,38 +142,48 @@ func GetRelayerKeys(rollappConfig config.RollappConfig) ([]utils.KeyInfo, error)
return relayerAddresses, nil
}

func AddRlyKey(kc utils.KeyConfig, chainID string) (*utils.KeyInfo, error) {
addKeyCmd := getAddRlyKeyCmd(
kc,
chainID,
)

fmt.Println(addKeyCmd.String())

out, err := bash.ExecCommandWithStdout(addKeyCmd)
if err != nil {
return nil, err
}

ki, err := utils.ParseAddressFromOutput(out)
if err != nil {
return nil, err
}
ki.Name = kc.ID

return ki, nil
}

func GenerateRelayerKeys(rollappConfig config.RollappConfig) ([]utils.KeyInfo, error) {
pterm.Info.Println("creating relayer keys")
relayerAddresses := make([]utils.KeyInfo, 0)
keys := getRelayerKeysConfig(rollappConfig)
var relayerAddresses []utils.KeyInfo
keys := GetRelayerKeysConfig(rollappConfig)

createRollappKeyCmd := getAddRlyKeyCmd(
pterm.Info.Println("creating relayer rollapp key")
relayerRollappAddress, err := AddRlyKey(
keys[consts.KeysIds.RollappRelayer],
rollappConfig.RollappID,
)
createHubKeyCmd := getAddRlyKeyCmd(keys[consts.KeysIds.HubRelayer], rollappConfig.HubData.ID)

pterm.Info.Println("creating relayer rollapp key")
out, err := bash.ExecCommandWithStdout(createRollappKeyCmd)
if err != nil {
return nil, err
}
relayerRollappAddress, err := utils.ParseAddressFromOutput(out)
relayerRollappAddress.Name = consts.KeysIds.RollappRelayer
if err != nil {
return nil, err
}

relayerAddresses = append(
relayerAddresses, *relayerRollappAddress,
)

pterm.Info.Println("creating relayer hub key")
out, err = bash.ExecCommandWithStdout(createHubKeyCmd)
if err != nil {
return nil, err
}
relayerHubAddress, err := utils.ParseAddressFromOutput(out)
relayerHubAddress.Name = consts.KeysIds.HubRelayer
relayerHubAddress, err := AddRlyKey(keys[consts.KeysIds.HubRelayer], rollappConfig.HubData.ID)
if err != nil {
return nil, err
}
Expand Down
4 changes: 0 additions & 4 deletions cmd/config/init/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"os/exec"
"path/filepath"

"github.com/pterm/pterm"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/relayer"
"github.com/dymensionxyz/roller/utils/config"
Expand Down Expand Up @@ -139,8 +137,6 @@ func InitializeRelayerConfig(
hubConfig relayer.ChainConfig,
initConfig config.RollappConfig,
) error {
pterm.Info.Println("initializing relayer config")

relayerHome := filepath.Join(initConfig.Home, consts.ConfigDirName.Relayer)

if err := initRelayer(relayerHome); err != nil {
Expand Down
213 changes: 93 additions & 120 deletions cmd/relayer/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ 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"
yaml "gopkg.in/yaml.v3"
"gopkg.in/yaml.v3"

initconfig "github.com/dymensionxyz/roller/cmd/config/init"
"github.com/dymensionxyz/roller/cmd/consts"
Expand All @@ -21,7 +20,7 @@ import (
"github.com/dymensionxyz/roller/sequencer"
globalutils "github.com/dymensionxyz/roller/utils"
"github.com/dymensionxyz/roller/utils/bash"
config2 "github.com/dymensionxyz/roller/utils/config"
configutils "github.com/dymensionxyz/roller/utils/config"
"github.com/dymensionxyz/roller/utils/config/tomlconfig"
"github.com/dymensionxyz/roller/utils/config/yamlconfig"
dymintutils "github.com/dymensionxyz/roller/utils/dymint"
Expand All @@ -31,11 +30,6 @@ import (
)

// TODO: Test relaying on 35-C and update the prices
var (
oneDayRelayPriceHub = big.NewInt(1)
oneDayRelayPriceRollapp = big.NewInt(1)
)

const (
flagOverride = "override"
)
Expand Down Expand Up @@ -118,30 +112,26 @@ func Cmd() *cobra.Command {

if !isRelayerInitialized || shouldOverwrite {
// preflight checks
for {
blockInformation, err := rollapputils.GetCurrentHeight()
if err != nil {
pterm.Error.Printf("failed to get current block height: %v\n", err)
return
}
currentHeight, err := strconv.Atoi(
blockInformation.Block.Header.Height,
)
blockInformation, err := rollapputils.GetCurrentHeight()
if err != nil {
pterm.Error.Printf("failed to get current block height: %v\n", err)
return
}
currentHeight, err := strconv.Atoi(
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, "5s", false)
if err != nil {
pterm.Error.Printf("failed to get current block height: %v\n", err)
pterm.Error.Println("failed to update dymint config")
return
}

if currentHeight <= 2 {
pterm.Warning.Println("current height is too low, updating dymint config")
err = dymintutils.UpdateDymintConfigForIBC(home, "5s", false)
if err != nil {
pterm.Error.Println("failed to update dymint config")
return
}
} else {
break
}
}

rollappPrefix := rollappChainData.Bech32Prefix
Expand Down Expand Up @@ -174,6 +164,27 @@ func Cmd() *cobra.Command {
return
}

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

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

pterm.Info.Println("please fund the keys below with 20 <tokens> respectively: ")
for _, k := range keys {
k.Print(utils.WithName())
}
interactiveContinue, _ := pterm.DefaultInteractiveConfirm.WithDefaultText(
"Press enter when the keys are funded: ",
).WithDefaultValue(true).Show()
if !interactiveContinue {
return
}

pterm.Info.Println("updating application relayer config")
path := filepath.Join(relayerHome, "config")
data, err := os.ReadFile(filepath.Join(path, "config.yaml"))
Expand Down Expand Up @@ -235,6 +246,56 @@ func Cmd() *cobra.Command {
}
}

if isRelayerInitialized && !shouldOverwrite {
pterm.Info.Println("ensuring relayer keys are present")
kc := initconfig.GetRelayerKeysConfig(rollappConfig)

for k, v := range kc {
pterm.Info.Printf("checking %s\n", k)

switch v.ID {
case consts.KeysIds.RollappRelayer:
chainId := rollappConfig.RollappID
isPresent, err := utils.IsRlyAddressWithNameInKeyring(v, chainId)
if err != nil {
pterm.Error.Printf("failed to check address: %v\n", err)
return
}

if !isPresent {
key, err := initconfig.AddRlyKey(v, rollappConfig.RollappID)
if err != nil {
pterm.Error.Printf("failed to add key: %v\n", err)
}

key.Print(utils.WithMnemonic(), utils.WithName())
}
case consts.KeysIds.HubRelayer:
chainId := rollappConfig.HubData.ID
isPresent, err := utils.IsRlyAddressWithNameInKeyring(v, chainId)
if err != nil {
pterm.Error.Printf("failed to check address: %v\n", err)
return
}
if !isPresent {
key, err := initconfig.AddRlyKey(v, rollappConfig.HubData.ID)
if err != nil {
pterm.Error.Printf("failed to add key: %v\n", err)
}

key.Print(utils.WithMnemonic(), utils.WithName())
}
default:
pterm.Error.Println("invalid key name", err)
return
}
}
}

err = verifyRelayerBalances(rollappConfig)
if err != nil {
return
}
rly := relayer.NewRelayer(
rollappConfig.Home,
rollappConfig.RollappID,
Expand Down Expand Up @@ -285,30 +346,8 @@ func Cmd() *cobra.Command {

// TODO: look up relayer keys
if createIbcChannels || shouldOverwrite {
if shouldOverwrite {
keys, err := initconfig.GenerateRelayerKeys(rollappConfig)
if err != nil {
pterm.Error.Printf("failed to create relayer keys: %v\n", err)
return
}

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

pterm.Info.Println("please fund the keys below with X <tokens> respectively: ")
for _, k := range keys {
k.Print(utils.WithName())
}
interactiveContinue, _ := pterm.DefaultInteractiveConfirm.WithDefaultText(
"Press enter when the keys are funded: ",
).WithDefaultValue(true).Show()
if !interactiveContinue {
return
}
}

err = VerifyRelayerBalances(rollappConfig)
err = verifyRelayerBalances(rollappConfig)
if err != nil {
pterm.Error.Printf("failed to verify relayer balances: %v\n", err)
return
Expand Down Expand Up @@ -361,78 +400,12 @@ func Cmd() *cobra.Command {
return relayerStartCmd
}

func VerifyRelayerBalances(rolCfg config2.RollappConfig) error {
insufficientBalances, err := GetRelayerInsufficientBalances(rolCfg)
func verifyRelayerBalances(rolCfg configutils.RollappConfig) error {
insufficientBalances, err := relayer.GetRelayerInsufficientBalances(rolCfg)
if err != nil {
return err
}
utils.PrintInsufficientBalancesIfAny(insufficientBalances)

return nil
}

func GetRlyHubInsufficientBalances(
config config2.RollappConfig,
) ([]utils.NotFundedAddressData, error) {
HubRlyAddr, err := utils.GetRelayerAddress(config.Home, config.HubData.ID)
if err != nil {
pterm.Error.Printf("failed to get relayer address: %v", err)
return nil, err
}

HubRlyBalance, err := utils.QueryBalance(
utils.ChainQueryConfig{
RPC: config.HubData.RPC_URL,
Denom: consts.Denoms.Hub,
Binary: consts.Executables.Dymension,
}, HubRlyAddr,
)
if err != nil {
pterm.Error.Printf("failed to query %s balances: %v", HubRlyAddr, err)
return nil, err
}

insufficientBalances := make([]utils.NotFundedAddressData, 0)
if HubRlyBalance.Amount.Cmp(oneDayRelayPriceHub) < 0 {
insufficientBalances = append(
insufficientBalances, utils.NotFundedAddressData{
KeyName: consts.KeysIds.HubRelayer,
Address: HubRlyAddr,
CurrentBalance: HubRlyBalance.Amount,
RequiredBalance: oneDayRelayPriceHub,
Denom: consts.Denoms.Hub,
Network: config.HubData.ID,
},
)
}
return insufficientBalances, nil
}

func GetRelayerInsufficientBalances(
config config2.RollappConfig,
) ([]utils.NotFundedAddressData, error) {
insufficientBalances, err := GetRlyHubInsufficientBalances(config)
if err != nil {
return insufficientBalances, err
}

rolRlyData, err := relayer.GetRolRlyAccData(config)
if err != nil {
return insufficientBalances, err
}

if rolRlyData.Balance.Amount.Cmp(oneDayRelayPriceRollapp) < 0 {
insufficientBalances = append(
insufficientBalances, utils.NotFundedAddressData{
KeyName: consts.KeysIds.RollappRelayer,
Address: rolRlyData.Address,
CurrentBalance: rolRlyData.Balance.Amount,
RequiredBalance: oneDayRelayPriceRollapp,
Denom: config.Denom,
Network: config.RollappID,
},
)
}

return insufficientBalances, nil
}
Loading

0 comments on commit dcbe2d1

Please sign in to comment.