Skip to content

Commit

Permalink
Implemented channel creation for roller relayer start
Browse files Browse the repository at this point in the history
  • Loading branch information
ItayLevyOfficial committed Jun 21, 2023
1 parent eb18261 commit 1545477
Show file tree
Hide file tree
Showing 28 changed files with 368 additions and 107 deletions.
3 changes: 2 additions & 1 deletion cmd/config/init/DALightClient.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package initconfig

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

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

func initializeLightNodeConfig(initConfig InitConfig) error {
func initializeLightNodeConfig(initConfig utils.RollappConfig) error {
initLightNodeCmd := exec.Command(consts.Executables.Celestia, "light", "init", "--p2p.network", "arabica", "--node.store", filepath.Join(initConfig.Home, consts.ConfigDirName.DALightNode))
err := initLightNodeCmd.Run()
if err != nil {
Expand Down
13 changes: 3 additions & 10 deletions cmd/config/init/consts.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package initconfig

import "github.com/dymensionxyz/roller/cmd/utils"

var FlagNames = struct {
Home string
Decimals string
Expand All @@ -16,7 +18,7 @@ const TestnetHubID = "35-C"
const StagingHubID = "internal-devnet"
const LocalHubID = "local"

var Hubs = map[string]HubData{
var Hubs = map[string]utils.HubData{
TestnetHubID: {
API_URL: "https://rest-hub-35c.dymension.xyz",
ID: "35-C",
Expand All @@ -34,13 +36,4 @@ var Hubs = map[string]HubData{
},
}

type HubData = struct {
API_URL string
ID string
RPC_URL string
}

const defaultRollappRPC = "http://localhost:26657"

const KeysDirName = "keys"
const RollerConfigFileName = "config.toml"
4 changes: 2 additions & 2 deletions cmd/config/init/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ func getRollappBinaryPath(cmd *cobra.Command) string {
return rollappBinaryPath
}

func GetInitConfig(initCmd *cobra.Command, args []string) InitConfig {
func GetInitConfig(initCmd *cobra.Command, args []string) utils.RollappConfig {
rollappId := args[0]
denom := args[1]
home := initCmd.Flag(utils.FlagNames.Home).Value.String()
rollappBinaryPath := getRollappBinaryPath(initCmd)
decimals := getDecimals(initCmd)
hubID := initCmd.Flag(FlagNames.HubID).Value.String()
return InitConfig{
return utils.RollappConfig{
Home: home,
RollappID: rollappId,
RollappBinary: rollappBinaryPath,
Expand Down
5 changes: 3 additions & 2 deletions cmd/config/init/genesis.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package initconfig

import (
"github.com/dymensionxyz/roller/cmd/utils"
"io/ioutil"

"fmt"
"os/exec"
"path/filepath"

"github.com/tidwall/sjson"
"github.com/dymensionxyz/roller/cmd/consts"
"github.com/tidwall/sjson"
)

func initializeRollappGenesis(initConfig InitConfig) error {
func initializeRollappGenesis(initConfig utils.RollappConfig) error {
zeros := initConfig.Decimals + 9
tokenAmount := "1" + fmt.Sprintf("%0*d", zeros, 0) + initConfig.Denom
rollappConfigDirPath := filepath.Join(initConfig.Home, consts.ConfigDirName.Rollapp)
Expand Down
26 changes: 10 additions & 16 deletions cmd/config/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ import (
"github.com/spf13/cobra"
)

type InitConfig struct {
Home string
RollappID string
RollappBinary string
Denom string
Decimals uint64
HubData HubData
}

func InitCmd() *cobra.Command {
initCmd := &cobra.Command{
Use: "init <rollapp-id> <denom>",
Expand All @@ -36,16 +27,13 @@ func InitCmd() *cobra.Command {
utils.PrettifyErrorIfExists(err)
if shouldOverwrite {
utils.PrettifyErrorIfExists(os.RemoveAll(initConfig.Home))
utils.PrettifyErrorIfExists(os.MkdirAll(initConfig.Home, 0755))
} else {
os.Exit(0)
}
} else {
utils.PrettifyErrorIfExists(os.MkdirAll(initConfig.Home, 0755))
}

addresses, err := generateKeys(initConfig)
utils.PrettifyErrorIfExists(err)
utils.PrettifyErrorIfExists(initializeLightNodeConfig(initConfig))
initializeRollappConfig(initConfig)
utils.PrettifyErrorIfExists(initializeRollappGenesis(initConfig))
utils.PrettifyErrorIfExists(initializeRelayerConfig(ChainConfig{
ID: initConfig.RollappID,
RPC: defaultRollappRPC,
Expand All @@ -57,7 +45,13 @@ func InitCmd() *cobra.Command {
Denom: "udym",
AddressPrefix: consts.AddressPrefixes.Hub,
}, initConfig))
utils.PrettifyErrorIfExists(WriteConfigToTOML(initConfig))
addresses, err := generateKeys(initConfig)
utils.PrettifyErrorIfExists(err)
utils.PrettifyErrorIfExists(initializeLightNodeConfig(initConfig))
initializeRollappConfig(initConfig)
utils.PrettifyErrorIfExists(initializeRollappGenesis(initConfig))

utils.PrettifyErrorIfExists(utils.WriteConfigToTOML(initConfig))
printInitOutput(addresses, initConfig.RollappID)
},
Args: cobra.ExactArgs(2),
Expand Down
101 changes: 86 additions & 15 deletions cmd/config/init/keys.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
package initconfig

import (
"os/exec"
"path"
"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"
)

func generateKeys(initConfig InitConfig, excludeKeys ...string) (map[string]string, error) {
keys := getDefaultKeysConfig(initConfig)
excludeKeysMap := make(map[string]struct{})
for _, key := range excludeKeys {
excludeKeysMap[key] = struct{}{}
func generateKeys(rollappConfig utils.RollappConfig) (map[string]string, error) {
sequencerAddresses, err := generateSequencersKeys(rollappConfig)
if err != nil {
return nil, err
}
relayerAddresses, err := generateRelayerKeys(rollappConfig)
if err != nil {
return nil, err
}
return utils.MergeMaps(sequencerAddresses, relayerAddresses), nil
}

func generateSequencersKeys(initConfig utils.RollappConfig) (map[string]string, error) {
keys := getSequencerKeysConfig()
addresses := make(map[string]string)
for _, key := range keys {
if _, exists := excludeKeysMap[key.ID]; !exists {
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
} else {
keyInfo, err := createKey(key, initConfig.Home)
if err != nil {
return nil, err
Expand Down Expand Up @@ -51,7 +67,7 @@ func createKey(keyConfig utils.KeyConfig, home string) (keyring.Info, error) {
return info, nil
}

func getDefaultKeysConfig(initConfig InitConfig) []utils.KeyConfig {
func getSequencerKeysConfig() []utils.KeyConfig {
return []utils.KeyConfig{
{
Dir: consts.ConfigDirName.Rollapp,
Expand All @@ -65,17 +81,72 @@ func getDefaultKeysConfig(initConfig InitConfig) []utils.KeyConfig {
CoinType: consts.CoinTypes.EVM,
Prefix: consts.AddressPrefixes.Rollapp,
},
{
Dir: path.Join(consts.ConfigDirName.Relayer, KeysDirName, initConfig.HubData.ID),
ID: consts.KeyNames.HubRelayer,
CoinType: consts.CoinTypes.Cosmos,
Prefix: consts.AddressPrefixes.Hub,
},
{
Dir: path.Join(consts.ConfigDirName.Relayer, KeysDirName, initConfig.RollappID),
}
}

func getRelayerKeysConfig(rollappConfig utils.RollappConfig) map[string]utils.KeyConfig {
return map[string]utils.KeyConfig{
consts.KeyNames.RollappRelayer: {
Dir: path.Join(rollappConfig.Home, consts.ConfigDirName.Relayer),
ID: consts.KeyNames.RollappRelayer,
CoinType: consts.CoinTypes.EVM,
Prefix: consts.AddressPrefixes.Rollapp,
},
consts.KeyNames.HubRelayer: {
Dir: path.Join(rollappConfig.Home, consts.ConfigDirName.Relayer),
ID: consts.KeyNames.HubRelayer,
CoinType: consts.CoinTypes.Cosmos,
Prefix: consts.AddressPrefixes.Hub,
},
}
}

func createAddressBinary(keyConfig utils.KeyConfig, binaryPath string, home string) (string, error) {
createKeyCommand := exec.Command(binaryPath, "keys", "add", keyConfig.ID, "--keyring-backend", "test",
"--keyring-dir", filepath.Join(home, keyConfig.Dir), "--output", "json")
out, err := utils.ExecBashCommand(createKeyCommand)
if err != nil {
return "", err
}
return utils.ParseAddressFromOutput(out)
}

func generateRelayerKeys(rollappConfig utils.RollappConfig) (map[string]string, error) {
relayerAddresses := make(map[string]string)
keys := getRelayerKeysConfig(rollappConfig)
createRollappKeyCmd := getAddRlyKeyCmd(keys[consts.KeyNames.RollappRelayer], rollappConfig.RollappID)
createHubKeyCmd := getAddRlyKeyCmd(keys[consts.KeyNames.HubRelayer], rollappConfig.HubData.ID)
out, err := utils.ExecBashCommand(createRollappKeyCmd)
if err != nil {
return nil, err
}
relayerRollappAddress, err := utils.ParseAddressFromOutput(out)
if err != nil {
return nil, err
}
relayerAddresses[consts.KeyNames.RollappRelayer] = relayerRollappAddress
out, err = utils.ExecBashCommand(createHubKeyCmd)
if err != nil {
return nil, err
}
relayerHubAddress, err := utils.ParseAddressFromOutput(out)
if err != nil {
return nil, err
}
relayerAddresses[consts.KeyNames.HubRelayer] = relayerHubAddress
return relayerAddresses, err
}

func getAddRlyKeyCmd(keyConfig utils.KeyConfig, chainID string) *exec.Cmd {
return exec.Command(
consts.Executables.Relayer,
consts.KeysDirName,
"add",
chainID,
keyConfig.ID,
"--home",
keyConfig.Dir,
"--coin-type",
strconv.Itoa(int(keyConfig.CoinType)),
)
}
5 changes: 3 additions & 2 deletions cmd/config/init/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"os"

"github.com/olekukonko/tablewriter"
"github.com/dymensionxyz/roller/cmd/consts"
"github.com/olekukonko/tablewriter"
)

func printInitOutput(addresses map[string]string, rollappId string) {
Expand All @@ -16,7 +16,8 @@ func printInitOutput(addresses map[string]string, rollappId string) {
data := [][]string{
{"Celestia", addresses[consts.KeyNames.DALightNode]},
{"Sequencer", addresses[consts.KeyNames.HubSequencer]},
{"Relayer", addresses[consts.KeyNames.HubRelayer]},
{"Relayer, Hub", addresses[consts.KeyNames.HubRelayer]},
{"Relayer, RollApp", addresses[consts.KeyNames.RollappRelayer]},
}

table := tablewriter.NewWriter(os.Stdout)
Expand Down
10 changes: 6 additions & 4 deletions cmd/config/init/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package initconfig

import (
"encoding/json"
"github.com/dymensionxyz/roller/cmd/utils"
"io/ioutil"
"os"
"os/exec"
Expand Down Expand Up @@ -102,7 +103,7 @@ func addChainsConfig(rollappConfig ChainConfig, hubConfig ChainConfig, relayerHo

relayerHubConfig := getRelayerFileChainConfig(RelayerChainConfig{
ChainConfig: hubConfig,
GasPrices: "0.25" + hubConfig.Denom,
GasPrices: "0.0" + hubConfig.Denom,
ClientType: "07-tendermint",
KeyName: consts.KeyNames.HubRelayer,
})
Expand All @@ -121,15 +122,16 @@ func setupPath(rollappConfig ChainConfig, hubConfig ChainConfig, relayerHome str
if err := setSettlementCmd.Run(); err != nil {
return err
}
relayerPath := "hub-rollapp"
newPathCmd := exec.Command(consts.Executables.Relayer, "paths", "new", rollappConfig.ID, hubConfig.ID, relayerPath, "--src-port", "transfer", "--dst-port", "transfer", "--version", "ics20-1", "--home", relayerHome)
args := []string{"paths", "new", rollappConfig.ID, hubConfig.ID, consts.DefaultRelayerPath}
args = append(args, utils.GetRelayerDefaultFlags(relayerHome)...)
newPathCmd := exec.Command(consts.Executables.Relayer, args...)
if err := newPathCmd.Run(); err != nil {
return err
}
return nil
}

func initializeRelayerConfig(rollappConfig ChainConfig, hubConfig ChainConfig, initConfig InitConfig) error {
func initializeRelayerConfig(rollappConfig ChainConfig, hubConfig ChainConfig, initConfig utils.RollappConfig) error {
relayerHome := filepath.Join(initConfig.Home, consts.ConfigDirName.Relayer)
if err := initRelayer(relayerHome); err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions cmd/config/init/rollapp.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package initconfig

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

toml "github.com/pelletier/go-toml"
"github.com/dymensionxyz/roller/cmd/consts"
toml "github.com/pelletier/go-toml"
)

func initializeRollappConfig(initConfig InitConfig) {
func initializeRollappConfig(initConfig utils.RollappConfig) {
initRollappCmd := exec.Command(initConfig.RollappBinary, "init", consts.KeyNames.HubSequencer, "--chain-id", initConfig.RollappID, "--home", filepath.Join(initConfig.Home, consts.ConfigDirName.Rollapp))
err := initRollappCmd.Run()
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions cmd/config/init/unique_rollapp_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package initconfig

import (
"fmt"
"github.com/dymensionxyz/roller/cmd/utils"
"net/http"
)

func IsRollappIDUnique(rollappID string, initConfig InitConfig) (bool, error) {
func IsRollappIDUnique(rollappID string, initConfig utils.RollappConfig) (bool, error) {
url := initConfig.HubData.API_URL + "/dymensionxyz/dymension/rollapp/rollapp/" + rollappID

req, err := http.NewRequest("GET", url, nil)
Expand All @@ -31,7 +32,7 @@ func IsRollappIDUnique(rollappID string, initConfig InitConfig) (bool, error) {
}
}

func VerifyUniqueRollappID(rollappID string, initConfig InitConfig) error {
func VerifyUniqueRollappID(rollappID string, initConfig utils.RollappConfig) error {
isUniqueRollapp, err := IsRollappIDUnique(rollappID, initConfig)
if err != nil {
return err
Expand Down
5 changes: 4 additions & 1 deletion cmd/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var AddressPrefixes = struct {
Rollapp string
DA string
}{
Rollapp: "rol",
Rollapp: "ethm",
Hub: "dym",
DA: "celestia",
}
Expand All @@ -61,3 +61,6 @@ var CoinTypes = struct {
Cosmos: 118,
EVM: 60,
}

const KeysDirName = "keys"
const DefaultRelayerPath = "hub-rollapp"
Loading

0 comments on commit 1545477

Please sign in to comment.