Skip to content

Commit

Permalink
feat: improve rollapp initialization (#817)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs authored Jul 18, 2024
1 parent 34a8182 commit d7db004
Show file tree
Hide file tree
Showing 23 changed files with 240 additions and 146 deletions.
4 changes: 2 additions & 2 deletions cmd/config/export/bech32.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
)

func getBech32Prefix(rlpCfg config.RollappConfig) (string, error) {
rollappSeqAddr, err := utils.GetAddressBinary(utils.KeyConfig{
rollappSeqAddrInfo, err := utils.GetAddressBinary(utils.KeyConfig{
Dir: filepath.Join(rlpCfg.Home, consts.ConfigDirName.Rollapp),
ID: consts.KeysIds.RollappSequencer,
}, rlpCfg.RollappBinary)
if err != nil {
return "", err
}
return strings.Split(rollappSeqAddr, "1")[0], nil
return strings.Split(rollappSeqAddrInfo.Address, "1")[0], nil
}
11 changes: 10 additions & 1 deletion cmd/config/init/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
func AddFlags(cmd *cobra.Command) error {
cmd.Flags().
StringP(FlagNames.HubID, "", consts.LocalHubName, fmt.Sprintf("The ID of the Dymension hub. %s", getAvailableHubsMessage()))

cmd.Flags().
StringP(FlagNames.RollappBinary, "", consts.Executables.RollappEVM, "The rollapp binary. Should be passed only if you built a custom rollapp")
cmd.Flags().
Expand All @@ -41,6 +42,7 @@ func AddFlags(cmd *cobra.Command) error {

func GetInitConfig(
initCmd *cobra.Command,
withMockSettlement bool,
) (*config.RollappConfig, error) {
var cfg config.RollappConfig
rollerConfigFilePath := filepath.Join(utils.GetRollerRootDir(), config.RollerConfigFileName)
Expand All @@ -57,7 +59,14 @@ func GetInitConfig(
// cfg.TokenSupply = initCmd.Flag(FlagNames.TokenSupply).Value.String()
cfg.DA = config.DAType(strings.ToLower(string(cfg.DA)))

hubID := initCmd.Flag(FlagNames.HubID).Value.String()
var hubID string

// TODO: hub id (and probably the rest of settlement config) should come from roller config
if withMockSettlement {
hubID = "mock"
} else {
hubID = initCmd.Flag(FlagNames.HubID).Value.String()
}

hub, ok := consts.Hubs[hubID]

Expand Down
6 changes: 3 additions & 3 deletions cmd/config/init/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (

func formatAddresses(
rollappConfig config.RollappConfig,
addresses []utils.AddressData,
) []utils.AddressData {
addresses []utils.KeyInfo,
) []utils.KeyInfo {
damanager := datalayer.NewDAManager(rollappConfig.DA, rollappConfig.Home)
requireFundingKeys := map[string]string{
consts.KeysIds.HubSequencer: fmt.Sprintf("Sequencer, %s Hub", rollappConfig.HubData.ID),
consts.KeysIds.HubRelayer: fmt.Sprintf("Relayer, %s Hub", rollappConfig.HubData.ID),
damanager.GetKeyName(): fmt.Sprintf("DA, %s Network", damanager.GetNetworkName()),
}
filteredAddresses := make([]utils.AddressData, 0)
filteredAddresses := make([]utils.KeyInfo, 0)
for _, address := range addresses {
if newName, ok := requireFundingKeys[address.Name]; ok {
address.Name = newName
Expand Down
1 change: 0 additions & 1 deletion cmd/config/init/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func UpdateGenesisParams(home string) error {
"test",
)

fmt.Println(addGenAccountCmd.String())
_, err = utils.ExecBashCommandWithStdout(addGenAccountCmd)
if err != nil {
return err
Expand Down
54 changes: 29 additions & 25 deletions cmd/config/init/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,57 @@ import (
"github.com/dymensionxyz/roller/config"
)

func GenerateKeys(rollappConfig config.RollappConfig) ([]utils.AddressData, error) {
func GenerateKeys(rollappConfig config.RollappConfig) ([]utils.KeyInfo, error) {
// var addresses []utils.KeyInfo

sequencerAddresses, err := generateSequencersKeys(rollappConfig)
if err != nil {
fmt.Println("failed to generate sequencerAddresses")
return nil, err
}

// addresses = append(addresses, sequencerAddresses...)

// relayerAddresses, err := generateRelayerKeys(rollappConfig)
// if err != nil {
// return nil, err
// }
// addresses = append(addresses, relayerAddresses...)

return sequencerAddresses, nil
}

func generateSequencersKeys(initConfig config.RollappConfig) ([]utils.AddressData, error) {
func generateSequencersKeys(initConfig config.RollappConfig) ([]utils.KeyInfo, error) {
keys := getSequencerKeysConfig(initConfig)
addresses := make([]utils.AddressData, 0)
addresses := make([]utils.KeyInfo, 0)
for _, key := range keys {
var address string
var address *utils.KeyInfo
var err error
address, err = CreateAddressBinary(key, initConfig.Home)
if err != nil {
return nil, err
}
addresses = append(addresses, utils.AddressData{
Addr: address,
Name: key.ID,
addresses = append(addresses, utils.KeyInfo{
Address: address.Address,
Name: key.ID,
Mnemonic: address.Mnemonic,
})
}
return addresses, nil
}

func getSequencerKeysConfig(rollappConfig config.RollappConfig) []utils.KeyConfig {
fmt.Println(rollappConfig.HubData)
if rollappConfig.HubData.ID == consts.MockHubID {
return []utils.KeyConfig{
{
Dir: consts.ConfigDirName.Rollapp,
ID: consts.KeysIds.RollappSequencer,
ChainBinary: rollappConfig.RollappBinary,
Type: rollappConfig.VMType,
},
}
}
return []utils.KeyConfig{
{
Dir: consts.ConfigDirName.HubKeys,
Expand Down Expand Up @@ -76,36 +95,21 @@ func getSequencerKeysConfig(rollappConfig config.RollappConfig) []utils.KeyConfi
// }
// }

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",
}
createKeyCommand := exec.Command(keyConfig.ChainBinary, args...)
out, err := utils.ExecBashCommandWithStdout(createKeyCommand)
if err != nil {
return "", err
}
return utils.ParseAddressFromOutput(out)
}

func CreateAddressBinaryWithSensitiveOutput(
func CreateAddressBinary(
keyConfig utils.KeyConfig,
home string,
) (*utils.SensitiveKeyInfo, error) {
) (*utils.KeyInfo, error) {
args := []string{
"keys", "add", keyConfig.ID, "--keyring-backend", "test",
"--keyring-dir", filepath.Join(home, keyConfig.Dir),
"--output", "json",
}
createKeyCommand := exec.Command(keyConfig.ChainBinary, args...)
fmt.Println(createKeyCommand.String())
out, err := utils.ExecBashCommandWithStdout(createKeyCommand)
if err != nil {
return nil, err
}
return utils.ParseAddressFromOutputWithSensisiveOutput(out)
return utils.ParseAddressFromOutput(out)
}

// func generateRelayerKeys(rollappConfig config.RollappConfig) ([]utils.AddressData, error) {
Expand Down
16 changes: 13 additions & 3 deletions cmd/config/init/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package initconfig
import (
"fmt"

"github.com/pterm/pterm"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/config"
)
Expand All @@ -19,7 +22,7 @@ func NewOutputHandler(noOutput bool) *OutputHandler {

func (o *OutputHandler) PrintInitOutput(
rollappConfig config.RollappConfig,
addresses []utils.AddressData,
addresses []utils.KeyInfo,
rollappId string,
) {
if o.NoOutput {
Expand All @@ -31,9 +34,16 @@ func (o *OutputHandler) PrintInitOutput(
)
fmt.Println(FormatTokenSupplyLine(rollappConfig))
fmt.Println()
utils.PrintAddressesWithTitle(formatAddresses(rollappConfig, addresses))
utils.PrintAddressesWithTitle(addresses)

fmt.Printf("\n🔔 Please fund these addresses to register and run the rollapp.\n")
if rollappConfig.HubData.ID != consts.MockHubID {
pterm.DefaultSection.WithIndentCharacter("🔔").
Println("Please fund the addresses below to register and run the rollapp.")
fa := formatAddresses(rollappConfig, addresses)
for _, v := range fa {
v.Print(utils.WithName())
}
}
}

func (o *OutputHandler) PromptOverwriteConfig(home string) (bool, error) {
Expand Down
15 changes: 8 additions & 7 deletions cmd/config/set/da.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func updateDaConfig(rlpCfg config.RollappConfig, newDa config.DAType) error {
return err
}
daManager := datalayer.NewDAManager(newDa, rlpCfg.Home)
if err := daManager.InitializeLightNodeConfig(); err != nil {
_, err = daManager.InitializeLightNodeConfig()
if err != nil {
return err
}
rlpCfg.DA = newDa
Expand All @@ -54,18 +55,18 @@ func updateDaConfig(rlpCfg config.RollappConfig, newDa config.DAType) error {
}
fmt.Printf("💈 RollApp DA has been successfully set to '%s'\n\n", newDa)
if newDa != config.Local {
addresses := make([]utils.AddressData, 0)
addresses := make([]utils.KeyInfo, 0)
damanager := datalayer.NewDAManager(newDa, rlpCfg.Home)
daAddress, err := damanager.GetDAAccountAddress()
if err != nil {
return err
}
addresses = append(addresses, utils.AddressData{
Name: damanager.GetKeyName(),
Addr: daAddress,
addresses = append(addresses, utils.KeyInfo{
Name: damanager.GetKeyName(),
Address: daAddress.Address,
})
fmt.Printf("🔑 Address:\n\n")
utils.PrintAddresses(addresses)

utils.PrintAddressesWithTitle(addresses)
fmt.Printf("\n🔔 Please fund this address to run the DA light client.\n")
}
return nil
Expand Down
14 changes: 13 additions & 1 deletion cmd/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var KeysIds = struct {
HubRelayer string
}{
HubSequencer: "hub_sequencer",
RollappSequencer: "rollapp_sequencer",
RollappSequencer: "rollapp_genesis_account",
RollappRelayer: "relayer-rollapp-key",
HubRelayer: "relayer-hub-key",
}
Expand Down Expand Up @@ -116,20 +116,32 @@ var LocalHubData = config.HubData{
SEQ_MIN_BOND: "100dym",
}

var MockHubData = config.HubData{
API_URL: "",
ID: MockHubID,
RPC_URL: "",
ARCHIVE_RPC_URL: "",
GAS_PRICE: "",
SEQ_MIN_BOND: "",
}

// TODO(#112): The available hub networks should be read from YAML file
var Hubs = map[string]config.HubData{
MockHubName: MockHubData,
LocalHubName: LocalHubData,
TestnetHubName: TestnetHubData,
MainnetHubName: MainnetHubData,
}

const (
MockHubName = "mock"
LocalHubName = "local"
TestnetHubName = "testnet"
MainnetHubName = "mainnet"
)

const (
MockHubID = "mock"
LocalHubID = "dymension_100-1"
TestnetHubID = "blumbus_111-1"
MainnetHubID = "dymension_1100-1"
Expand Down
2 changes: 1 addition & 1 deletion cmd/eibc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func ensureWhaleAccount() error {
_, err := utils.GetAddressBinary(kc, consts.Executables.Dymension)
if err != nil {
fmt.Println("whale account not found in the keyring, creating it now")
addressInfo, err := initconfig.CreateAddressBinaryWithSensitiveOutput(kc, home)
addressInfo, err := initconfig.CreateAddressBinary(kc, home)
if err != nil {
return err
}
Expand Down
47 changes: 26 additions & 21 deletions cmd/keys/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,49 +27,54 @@ 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)
addresses := make([]utils.KeyInfo, 0)
damanager := datalayer.NewDAManager(rollappConfig.DA, rollappConfig.Home)

daAddr, err := damanager.DataLayer.GetDAAccountAddress()
utils.PrettifyErrorIfExists(err)
if daAddr != "" {
addresses = append(addresses, utils.AddressData{
Addr: daAddr,
Name: damanager.GetKeyName(),
if daAddr != nil {
addresses = append(addresses, utils.KeyInfo{
Address: daAddr.Address,
Name: damanager.GetKeyName(),
})
}
hubSeqAddr, err := utils.GetAddressBinary(utils.KeyConfig{

hubSeqInfo, err := utils.GetAddressBinary(utils.KeyConfig{
Dir: filepath.Join(rollappConfig.Home, consts.ConfigDirName.HubKeys),
ID: consts.KeysIds.HubSequencer,
}, consts.Executables.Dymension)
utils.PrettifyErrorIfExists(err)
addresses = append(addresses, utils.AddressData{
Addr: hubSeqAddr,
Name: consts.KeysIds.HubSequencer,
addresses = append(addresses, utils.KeyInfo{
Address: hubSeqInfo.Address,
Name: consts.KeysIds.HubSequencer,
})
rollappSeqAddr, err := utils.GetAddressBinary(utils.KeyConfig{

raSeqInfo, err := utils.GetAddressBinary(utils.KeyConfig{
Dir: filepath.Join(rollappConfig.Home, consts.ConfigDirName.Rollapp),
ID: consts.KeysIds.RollappSequencer,
}, rollappConfig.RollappBinary)
utils.PrettifyErrorIfExists(err)
addresses = append(addresses, utils.AddressData{
Addr: rollappSeqAddr,
Name: consts.KeysIds.RollappSequencer,
addresses = append(addresses, utils.KeyInfo{
Address: raSeqInfo.Address,
Name: consts.KeysIds.RollappSequencer,
})

hubRlyAddr, err := utils.GetRelayerAddress(rollappConfig.Home, rollappConfig.HubData.ID)
utils.PrettifyErrorIfExists(err)
addresses = append(addresses, utils.AddressData{
Addr: hubRlyAddr,
Name: consts.KeysIds.HubRelayer,
addresses = append(addresses, utils.KeyInfo{
Address: hubRlyAddr,
Name: consts.KeysIds.HubRelayer,
})

rollappRlyAddr, err := utils.GetRelayerAddress(
rollappConfig.Home,
rollappConfig.RollappID,
)
utils.PrettifyErrorIfExists(err)
addresses = append(addresses, utils.AddressData{
Addr: rollappRlyAddr,
Name: consts.KeysIds.RollappRelayer,

addresses = append(addresses, utils.KeyInfo{
Address: rollappRlyAddr,
Name: consts.KeysIds.RollappRelayer,
})
outputType := cmd.Flag(flagNames.outputType).Value.String()
if outputType == "json" {
Expand All @@ -83,10 +88,10 @@ func Cmd() *cobra.Command {
return cmd
}

func printAsJSON(addresses []utils.AddressData) error {
func printAsJSON(addresses []utils.KeyInfo) error {
addrMap := make(map[string]string)
for _, addrData := range addresses {
addrMap[addrData.Name] = addrData.Addr
addrMap[addrData.Name] = addrData.Address
}
data, err := json.MarshalIndent(addrMap, "", " ")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/migrate/v1_0_05.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (v *VersionMigratorV1005) PerformMigration(rlpCfg config.RollappConfig) err
}
// re-init the light node and ask the user to fund the address
celestiaClient := celestia.NewCelestia(rlpCfg.Home)
err := celestiaClient.InitializeLightNodeConfig()
_, err := celestiaClient.InitializeLightNodeConfig()
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit d7db004

Please sign in to comment.