Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for a custom RDK binary #181

Merged
merged 4 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions cmd/config/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package initconfig

import (
"fmt"
"os"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/config"
datalayer "github.com/dymensionxyz/roller/data_layer"
"github.com/spf13/cobra"
"os"
)

func InitCmd() *cobra.Command {
Expand Down Expand Up @@ -99,11 +100,13 @@ func runInit(cmd *cobra.Command, args []string) error {
spin.Suffix = " Initializing RollApp configuration files..."
spin.Restart()
/* ---------------------------- Initialize relayer --------------------------- */
rollappPrefix, err := utils.GetAddressPrefix(initConfig.RollappBinary)
utils.PrettifyErrorIfExists(err)
err = initializeRelayerConfig(ChainConfig{
ID: initConfig.RollappID,
RPC: consts.DefaultRollappRPC,
Denom: initConfig.Denom,
AddressPrefix: consts.AddressPrefixes.Rollapp,
AddressPrefix: rollappPrefix,
}, ChainConfig{
ID: initConfig.HubData.ID,
RPC: initConfig.HubData.RPC_URL,
Expand Down
78 changes: 38 additions & 40 deletions cmd/config/init/keys.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package initconfig

import (
"os/exec"
"path"
"path/filepath"
"strconv"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/config"
"os/exec"
"path"
"path/filepath"
)

func generateKeys(rollappConfig config.RollappConfig) ([]utils.AddressData, error) {
Expand All @@ -24,16 +22,12 @@ func generateKeys(rollappConfig config.RollappConfig) ([]utils.AddressData, erro
}

func generateSequencersKeys(initConfig config.RollappConfig) ([]utils.AddressData, error) {
keys := getSequencerKeysConfig()
keys := getSequencerKeysConfig(initConfig)
addresses := make([]utils.AddressData, 0)
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)
} else {
address, err = createAddressBinary(key, consts.Executables.Dymension, initConfig.Home)
}
address, err = createAddressBinary(key, initConfig.Home)
if err != nil {
return nil, err
}
Expand All @@ -45,47 +39,46 @@ func generateSequencersKeys(initConfig config.RollappConfig) ([]utils.AddressDat
return addresses, nil
}

func getSequencerKeysConfig() []utils.CreateKeyConfig {
return []utils.CreateKeyConfig{
func getSequencerKeysConfig(rollappConfig config.RollappConfig) []utils.KeyConfig {
return []utils.KeyConfig{
{
Dir: consts.ConfigDirName.HubKeys,
ID: consts.KeysIds.HubSequencer,
CoinType: consts.CoinTypes.EVM,
Algo: consts.AlgoTypes.Secp256k1,
Prefix: consts.AddressPrefixes.Hub,
Dir: consts.ConfigDirName.HubKeys,
ID: consts.KeysIds.HubSequencer,
ChainBinary: consts.Executables.Dymension,
},
{
Dir: consts.ConfigDirName.Rollapp,
ID: consts.KeysIds.RollappSequencer,
CoinType: consts.CoinTypes.EVM,
Algo: consts.AlgoTypes.Ethsecp256k1,
Prefix: consts.AddressPrefixes.Rollapp,
Dir: consts.ConfigDirName.Rollapp,
ID: consts.KeysIds.RollappSequencer,
ChainBinary: rollappConfig.RollappBinary,
},
}
}

func getRelayerKeysConfig(rollappConfig config.RollappConfig) map[string]utils.CreateKeyConfig {
return map[string]utils.CreateKeyConfig{
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),
ID: consts.KeysIds.RollappRelayer,
CoinType: consts.CoinTypes.EVM,
Algo: consts.AlgoTypes.Ethsecp256k1,
Prefix: consts.AddressPrefixes.Rollapp,
Dir: path.Join(rollappConfig.Home, consts.ConfigDirName.Relayer),
ID: consts.KeysIds.RollappRelayer,
ChainBinary: rollappConfig.RollappBinary,
},
consts.KeysIds.HubRelayer: {
Dir: path.Join(rollappConfig.Home, consts.ConfigDirName.Relayer),
ID: consts.KeysIds.HubRelayer,
CoinType: consts.CoinTypes.Cosmos,
Algo: consts.AlgoTypes.Secp256k1,
Prefix: consts.AddressPrefixes.Hub,
Dir: path.Join(rollappConfig.Home, consts.ConfigDirName.Relayer),
ID: consts.KeysIds.HubRelayer,
ChainBinary: consts.Executables.Dymension,
},
}
}

func createAddressBinary(keyConfig utils.CreateKeyConfig, binaryPath string, home string) (string, error) {
createKeyCommand := exec.Command(binaryPath, "keys", "add", keyConfig.ID, "--keyring-backend", "test",
"--keyring-dir", filepath.Join(home, keyConfig.Dir), "--algo", keyConfig.Algo, "--output", "json")
func createAddressBinary(keyConfig utils.KeyConfig, home string) (string, error) {
args := []string{
"keys", "add", keyConfig.ID, "--keyring-backend", "test",
"--keyring-dir", filepath.Join(home, keyConfig.Dir),
"--output", "json",
}
if keyConfig.ChainBinary == consts.Executables.Dymension {
args = append(args, "--algo", consts.AlgoTypes.Secp256k1)
}
createKeyCommand := exec.Command(keyConfig.ChainBinary, args...)
out, err := utils.ExecBashCommand(createKeyCommand)
if err != nil {
return "", err
Expand Down Expand Up @@ -125,7 +118,12 @@ func generateRelayerKeys(rollappConfig config.RollappConfig) ([]utils.AddressDat
return relayerAddresses, err
}

func getAddRlyKeyCmd(keyConfig utils.CreateKeyConfig, chainID string) *exec.Cmd {
func getAddRlyKeyCmd(keyConfig utils.KeyConfig, chainID string) *exec.Cmd {
// TODO: Add support for custom EVM rollapp binaries (#196)
var coinType = "118"
if keyConfig.ChainBinary == consts.Executables.RollappEVM {
ItayLevyOfficial marked this conversation as resolved.
Show resolved Hide resolved
coinType = "60"
}
return exec.Command(
consts.Executables.Relayer,
"keys",
Expand All @@ -135,6 +133,6 @@ func getAddRlyKeyCmd(keyConfig utils.CreateKeyConfig, chainID string) *exec.Cmd
"--home",
keyConfig.Dir,
"--coin-type",
strconv.Itoa(int(keyConfig.CoinType)),
coinType,
)
}
16 changes: 2 additions & 14 deletions cmd/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,9 @@ var KeysIds = struct {
}

var AddressPrefixes = struct {
Hub string
Rollapp string
DA string
Hub string
}{
Rollapp: "ethm",
Hub: "dym",
DA: "celestia",
Hub: "dym",
}

var ConfigDirName = struct {
Expand All @@ -61,14 +57,6 @@ var ConfigDirName = struct {
HubKeys: "hub-keys",
}

var CoinTypes = struct {
Cosmos uint32
EVM uint32
}{
Cosmos: 118,
EVM: 60,
}

var AlgoTypes = struct {
Secp256k1 string
Ethsecp256k1 string
Expand Down
7 changes: 3 additions & 4 deletions cmd/keys/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func Cmd() *cobra.Command {
home := cmd.Flag(utils.FlagNames.Home).Value.String()
rollappConfig, err := config.LoadConfigFromTOML(home)
utils.PrettifyErrorIfExists(err)

addresses := make([]utils.AddressData, 0)
damanager := datalayer.NewDAManager(rollappConfig.DA, rollappConfig.Home)

Expand All @@ -30,7 +29,7 @@ func Cmd() *cobra.Command {
Name: consts.KeysIds.DALightNode,
})
}
hubSeqAddr, err := utils.GetAddressBinary(utils.GetKeyConfig{
hubSeqAddr, err := utils.GetAddressBinary(utils.KeyConfig{
Dir: filepath.Join(rollappConfig.Home, consts.ConfigDirName.HubKeys),
ID: consts.KeysIds.HubSequencer,
}, consts.Executables.Dymension)
Expand All @@ -39,10 +38,10 @@ func Cmd() *cobra.Command {
Addr: hubSeqAddr,
Name: consts.KeysIds.HubSequencer,
})
rollappSeqAddr, err := utils.GetAddressBinary(utils.GetKeyConfig{
rollappSeqAddr, err := utils.GetAddressBinary(utils.KeyConfig{
Dir: filepath.Join(rollappConfig.Home, consts.ConfigDirName.Rollapp),
ID: consts.KeysIds.RollappSequencer,
}, consts.Executables.RollappEVM)
}, rollappConfig.RollappBinary)
utils.PrettifyErrorIfExists(err)
addresses = append(addresses, utils.AddressData{
Addr: rollappSeqAddr,
Expand Down
2 changes: 1 addition & 1 deletion cmd/utils/fetch_accounts_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func GetHubRlyAccData(cfg config.RollappConfig) (*AccountData, error) {
}

func GetSequencerData(cfg config.RollappConfig) ([]AccountData, error) {
sequencerAddress, err := GetAddressBinary(GetKeyConfig{
sequencerAddress, err := GetAddressBinary(KeyConfig{
ID: consts.KeysIds.HubSequencer,
Dir: filepath.Join(cfg.Home, consts.ConfigDirName.HubKeys),
}, consts.Executables.Dymension)
Expand Down
45 changes: 21 additions & 24 deletions cmd/utils/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,13 @@ func ParseAddressFromOutput(output bytes.Buffer) (string, error) {
return key.Address, nil
}

type GetKeyConfig struct {
Dir string
ID string
type KeyConfig struct {
Dir string
ID string
ChainBinary string
}

type CreateKeyConfig struct {
Dir string
ID string
CoinType uint32
Algo string
Prefix string
}

func GetAddressBinary(keyConfig GetKeyConfig, binaryPath string) (string, error) {
func GetAddressBinary(keyConfig KeyConfig, binaryPath string) (string, error) {
showKeyCommand := exec.Command(
binaryPath, "keys", "show", keyConfig.ID, "--keyring-backend", "test", "--keyring-dir", keyConfig.Dir,
"--output", "json",
Expand All @@ -53,18 +46,6 @@ func GetAddressBinary(keyConfig GetKeyConfig, binaryPath string) (string, error)
return ParseAddressFromOutput(output)
}

func MergeMaps(map1, map2 map[string]string) map[string]string {
result := make(map[string]string)
for key, value := range map1 {
result[key] = value
}
for key, value := range map2 {
result[key] = value
}

return result
}

func GetRelayerAddress(home string, chainID string) (string, error) {
showKeyCmd := exec.Command(
consts.Executables.Relayer, "keys", "show", chainID, "--home", filepath.Join(home, consts.ConfigDirName.Relayer),
Expand Down Expand Up @@ -106,3 +87,19 @@ func GetSequencerPubKey(rollappConfig config.RollappConfig) (string, error) {
}
return strings.ReplaceAll(strings.ReplaceAll(string(out), "\n", ""), "\\", ""), nil
}

func GetAddressPrefix(binaryPath string) (string, error) {
cmd := exec.Command(binaryPath, "debug", "addr", "ffffffffffffff")
out, err := ExecBashCommand(cmd)
if err != nil {
return "", err
}
lines := strings.Split(out.String(), "\n")
for _, line := range lines {
if strings.HasPrefix(line, "Bech32 Acc:") {
prefix := strings.Split(strings.TrimSpace(strings.Split(line, ":")[1]), "1")[0]
return strings.TrimSpace(prefix), nil
}
}
return "", fmt.Errorf("could not find address prefix in binary debug command output")
}