Skip to content

Commit

Permalink
feat: fetch DA information from roller.toml for easy endpoint chang…
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs authored Aug 26, 2024
1 parent 207b17a commit 1d66c64
Show file tree
Hide file tree
Showing 21 changed files with 144 additions and 103 deletions.
2 changes: 1 addition & 1 deletion cmd/config/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func Cmd() *cobra.Command {
Rpc: "",
}
}
if rlpCfg.DA == consts.Avail {
if rlpCfg.DA.Backend == consts.Avail {
networkJson.Da = Avail
} else {
networkJson.Da = Celestia
Expand Down
2 changes: 1 addition & 1 deletion cmd/config/init/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func GetInitConfig(

// token supply is provided in the pre-created genesis
// cfg.TokenSupply = initCmd.Flag(FlagNames.TokenSupply).Value.String()
cfg.DA = consts.DAType(strings.ToLower(string(cfg.DA)))
cfg.DA.Backend = consts.DAType(strings.ToLower(string(cfg.DA.Backend)))

var hubID string

Expand Down
2 changes: 1 addition & 1 deletion cmd/config/init/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func formatAddresses(
rollappConfig config.RollappConfig,
addresses []utils.KeyInfo,
) []utils.KeyInfo {
damanager := datalayer.NewDAManager(rollappConfig.DA, rollappConfig.Home)
damanager := datalayer.NewDAManager(rollappConfig.DA.Backend, 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),
Expand Down
14 changes: 7 additions & 7 deletions cmd/config/set/da.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import (
datalayer "github.com/dymensionxyz/roller/data_layer"
"github.com/dymensionxyz/roller/sequencer"
globalutils "github.com/dymensionxyz/roller/utils"
config2 "github.com/dymensionxyz/roller/utils/config"
configutils "github.com/dymensionxyz/roller/utils/config"
"github.com/dymensionxyz/roller/utils/config/tomlconfig"
)

func setDA(rlpCfg config2.RollappConfig, value string) error {
func setDA(rlpCfg configutils.RollappConfig, value string) error {
daValue := consts.DAType(value)
if daValue == rlpCfg.DA {
if daValue == rlpCfg.DA.Backend {
return nil
}

if !config2.IsValidDAType(value) {
return fmt.Errorf("invalid DA type. Supported types are: %v", config2.SupportedDas)
if !configutils.IsValidDAType(value) {
return fmt.Errorf("invalid DA type. Supported types are: %v", configutils.SupportedDas)
}
return updateDaConfig(rlpCfg, daValue)
}

func updateDaConfig(rlpCfg config2.RollappConfig, newDa consts.DAType) error {
func updateDaConfig(rlpCfg configutils.RollappConfig, newDa consts.DAType) error {
daCfgDirPath := filepath.Join(rlpCfg.Home, consts.ConfigDirName.DALightNode)
dirExist, err := globalutils.DirNotEmpty(daCfgDirPath)
if err != nil {
Expand All @@ -50,7 +50,7 @@ func updateDaConfig(rlpCfg config2.RollappConfig, newDa consts.DAType) error {
return err
}

rlpCfg.DA = newDa
rlpCfg.DA.Backend = newDa
if err := sequencer.UpdateDymintDAConfig(rlpCfg); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/config/set/lc_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func setLCGatewayPort(cfg config.RollappConfig, value string) error {
if err := validatePort(value); err != nil {
return err
}
if cfg.DA != consts.Celestia {
if cfg.DA.Backend != consts.Celestia {
return errors.New("setting the LC RPC port is only supported for Celestia")
}
if err := utils.UpdateFieldInToml(
Expand All @@ -30,7 +30,7 @@ func setLCRPCPort(cfg config.RollappConfig, value string) error {
if err := validatePort(value); err != nil {
return err
}
if cfg.DA != consts.Celestia {
if cfg.DA.Backend != consts.Celestia {
return errors.New("setting the LC RPC port is only supported for Celestia")
}
if err := utils.UpdateFieldInToml(
Expand Down
42 changes: 30 additions & 12 deletions cmd/consts/da.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ var DaAuthTokenType = struct {
Read: "read",
}

const (
DefaultCelestiaRestApiEndpoint = "https://api.celestia-mocha.com"
DefaultCelestiaRPC = "http://mocha-4-consensus.mesa.newmetric.xyz:26657"

// https://docs.celestia.org/nodes/mocha-testnet#community-data-availability-da-grpc-endpoints-for-state-access
DefaultCelestiaStateNode = "rpc-mocha.pops.one"
DefaultCelestiaNetwork = "mocha-4"
)

type DAType string

const (
Expand All @@ -16,19 +25,28 @@ const (
Avail DAType = "avail"
)

var CelestiaNetworks = map[string]HubData{
"mocha": {
API_URL: "celestia-testnet-consensus.itrocket.net",
ID: "mocha-4",
RPC_URL: "celestia-testnet-consensus.itrocket.net",
ARCHIVE_RPC_URL: "",
GAS_PRICE: "",
var DaNetworks = map[string]DaData{
"mock": {
Backend: "mock",
ApiUrl: "",
ID: "mock",
RpcUrl: "",
StateNode: "",
GasPrice: "",
},
"mocha-4": {
Backend: Celestia,
ApiUrl: DefaultCelestiaRestApiEndpoint,
ID: "mocha-4",
RpcUrl: DefaultCelestiaRPC,
StateNode: DefaultCelestiaStateNode,
GasPrice: "0.02",
},
"celestia": {
API_URL: "api-celestia.mzonder.com",
ID: "celestia",
RPC_URL: "rpc-celestia.mzonder.com",
ARCHIVE_RPC_URL: "",
GAS_PRICE: "",
ApiUrl: "api-celestia.mzonder.com",
ID: "celestia",
RpcUrl: "rpc-celestia.mzonder.com",
StateNode: "",
GasPrice: "0.002",
},
}
9 changes: 9 additions & 0 deletions cmd/consts/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ type HubData = struct {
ARCHIVE_RPC_URL string `toml:"archive_rpc_url"`
GAS_PRICE string `toml:"gas_price"`
}

type DaData = struct {
Backend DAType `toml:"backend"`
ID string `toml:"id"`
ApiUrl string `toml:"api_url"`
RpcUrl string `toml:"rpc_url"`
StateNode string `toml:"state_node"`
GasPrice string `toml:"gas_price"`
}
15 changes: 7 additions & 8 deletions cmd/da-light-client/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"errors"
"fmt"

"github.com/pterm/pterm"
"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
datalayer "github.com/dymensionxyz/roller/data_layer"
"github.com/dymensionxyz/roller/data_layer/celestia"
"github.com/dymensionxyz/roller/utils/bash"
"github.com/dymensionxyz/roller/utils/config/tomlconfig"
"github.com/dymensionxyz/roller/utils/errorhandling"
Expand All @@ -31,28 +31,27 @@ func Cmd() *cobra.Command {
Short: "Runs the DA light client.",
Run: func(cmd *cobra.Command, args []string) {
home := cmd.Flag(utils.FlagNames.Home).Value.String()
pterm.Info.Println("loading roller config file")
rollappConfig, err := tomlconfig.LoadRollerConfig(home)
errorhandling.PrettifyErrorIfExists(err)

// TODO: refactor the version comparison for migrations
// errorhandling.RequireMigrateIfNeeded(rollappConfig)

metricsEndpoint := cmd.Flag(metricsEndpointFlag).Value.String()
if metricsEndpoint != "" && rollappConfig.DA != consts.Celestia {
if metricsEndpoint != "" && rollappConfig.DA.Backend != consts.Celestia {
errorhandling.PrettifyErrorIfExists(
errors.New("metrics endpoint can only be set for celestia"),
)
}
damanager := datalayer.NewDAManager(rollappConfig.DA, rollappConfig.Home)
damanager := datalayer.NewDAManager(rollappConfig.DA.Backend, rollappConfig.Home)

pterm.Info.Println("checking for da address balance")
insufficientBalances, err := damanager.CheckDABalance()
errorhandling.PrettifyErrorIfExists(err)
utils.PrintInsufficientBalancesIfAny(insufficientBalances)

rpcEndpoint := cmd.Flag(rpcEndpointFlag).Value.String()
if rpcEndpoint != "" {
damanager.SetRPCEndpoint(rpcEndpoint)
}
damanager.SetRPCEndpoint(rollappConfig.DA.StateNode)
if metricsEndpoint != "" {
damanager.SetMetricsEndpoint(metricsEndpoint)
}
Expand Down Expand Up @@ -89,7 +88,7 @@ func Cmd() *cobra.Command {

func addFlags(cmd *cobra.Command) {
cmd.Flags().
StringP(rpcEndpointFlag, "", celestia.DefaultCelestiaStateNode, "The DA rpc endpoint to connect to.")
StringP(rpcEndpointFlag, "", consts.DefaultCelestiaStateNode, "The DA rpc endpoint to connect to.")
cmd.Flags().
StringP(metricsEndpointFlag, "", "", "The OTEL collector metrics endpoint to connect to.")
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/keys/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Cmd() *cobra.Command {
consts.KeysIds.HubSequencer,
consts.KeysIds.RollappSequencer,
}
damanager := datalayer.NewDAManager(rlpCfg.DA, rlpCfg.Home)
damanager := datalayer.NewDAManager(rlpCfg.DA.Backend, rlpCfg.Home)
if damanager.GetKeyName() != "" {
supportedKeys = append(supportedKeys, damanager.GetKeyName())
}
Expand All @@ -52,9 +52,9 @@ func Cmd() *cobra.Command {
} else if keyID != "" && keyID == damanager.GetKeyName() {
privateKey, err := damanager.GetPrivateKey()
errorhandling.PrettifyErrorIfExists(err)
if rlpCfg.DA == consts.Celestia {
if rlpCfg.DA.Backend == consts.Celestia {
printHexKeyOutput(privateKey)
} else if rlpCfg.DA == consts.Avail {
} else if rlpCfg.DA.Backend == consts.Avail {
printMnemonicKeyOutput(privateKey)
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion cmd/keys/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func Cmd() *cobra.Command {
rollappConfig, err := tomlconfig.LoadRollerConfig(home)
errorhandling.PrettifyErrorIfExists(err)
addresses := make([]utils.KeyInfo, 0)
damanager := datalayer.NewDAManager(rollappConfig.DA, rollappConfig.Home)
damanager := datalayer.NewDAManager(rollappConfig.DA.Backend, rollappConfig.Home)

daAddr, err := damanager.DataLayer.GetDAAccountAddress()
errorhandling.PrettifyErrorIfExists(err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/migrate/v1_0_05.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func (v *VersionMigratorV1005) ShouldMigrate(prevVersion VersionData) bool {

func (v *VersionMigratorV1005) PerformMigration(rlpCfg config.RollappConfig) error {
// If the DA is not celestia, no-op
if rlpCfg.DA != consts.Celestia {
if rlpCfg.DA.Backend != consts.Celestia {
return nil
}
// Update dymint config with celestia new config
dymintTomlPath := sequencer.GetDymintFilePath(rlpCfg.Home)
da := datalayer.NewDAManager(rlpCfg.DA, rlpCfg.Home)
da := datalayer.NewDAManager(rlpCfg.DA.Backend, rlpCfg.Home)
sequencerDaConfig := da.GetSequencerDAConfig(consts.NodeType.Sequencer)
if sequencerDaConfig == "" {
return nil
Expand Down
8 changes: 4 additions & 4 deletions cmd/migrate/v_0_1_12.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ func (v *VersionMigratorV0112) ShouldMigrate(prevVersion VersionData) bool {

func (v *VersionMigratorV0112) PerformMigration(rlpCfg config2.RollappConfig) error {
dymintTomlPath := sequencer.GetDymintFilePath(rlpCfg.Home)
if rlpCfg.DA == "mock" {
rlpCfg.DA = consts.Local
if rlpCfg.DA.Backend == "mock" {
rlpCfg.DA.Backend = consts.Local
return tomlconfig.Write(rlpCfg)
}
if rlpCfg.DA == consts.Avail {
if rlpCfg.DA.Backend == consts.Avail {
availNewCfgPath := avail.GetCfgFilePath(rlpCfg.Home)
if err := utils.MoveFile(filepath.Join(rlpCfg.Home, avail.ConfigFileName), availNewCfgPath); err != nil {
return err
}
}
da := datalayer.NewDAManager(rlpCfg.DA, rlpCfg.Home)
da := datalayer.NewDAManager(rlpCfg.DA.Backend, rlpCfg.Home)
sequencerDaConfig := da.GetSequencerDAConfig(consts.NodeType.Sequencer)
if sequencerDaConfig == "" {
return nil
Expand Down
33 changes: 17 additions & 16 deletions cmd/rollapp/init/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import (
"strconv"
"strings"

toml "github.com/pelletier/go-toml/v2"
"github.com/dymensionxyz/roller/utils/config"
"github.com/pelletier/go-toml/v2"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
"gopkg.in/yaml.v2"

initconfig "github.com/dymensionxyz/roller/cmd/config/init"
"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
cmdutils "github.com/dymensionxyz/roller/cmd/utils"
datalayer "github.com/dymensionxyz/roller/data_layer"
"github.com/dymensionxyz/roller/data_layer/celestia"
globalutils "github.com/dymensionxyz/roller/utils"
"github.com/dymensionxyz/roller/utils/bash"
"github.com/dymensionxyz/roller/utils/config/tomlconfig"
Expand Down Expand Up @@ -113,6 +113,7 @@ func runInit(cmd *cobra.Command, env string, raID string) error {
}

hd := consts.Hubs[env]
mochaData := consts.DaNetworks[consts.DefaultCelestiaNetwork]
rollerTomlData := map[string]string{
"rollapp_id": raID,
"rollapp_binary": strings.ToLower(consts.Executables.RollappEVM),
Expand All @@ -124,8 +125,12 @@ func runInit(cmd *cobra.Command, env string, raID string) error {
"HubData.archive_rpc_url": hd.ARCHIVE_RPC_URL,
"HubData.gas_price": hd.GAS_PRICE,

// TODO: create a separate config section for DA, similar to HubData
"da": string(consts.Celestia),
"DA.backend": string(mochaData.Backend),
"DA.id": mochaData.ID,
"DA.api_url": mochaData.ApiUrl,
"DA.rpc_url": mochaData.RpcUrl,
"DA.state_node": mochaData.StateNode,
"DA.gas_price": "0.02",
}

for key, value := range rollerTomlData {
Expand All @@ -152,16 +157,12 @@ func runInit(cmd *cobra.Command, env string, raID string) error {
initConfig := *initConfigPtr

errorhandling.RunOnInterrupt(outputHandler.StopSpinner)
// daSpinner.Start(consts.SpinnerMsgs.UniqueIdVerification)
err = initConfig.Validate()
if err != nil {
errorhandling.PrettifyErrorIfExists(err)
return err
}

// TODO: create all dirs here
// nolint:errcheck

/* ------------------------------ Generate keys ----------------------------- */
addresses, err := initconfig.GenerateSequencersKeys(initConfig)
if err != nil {
Expand All @@ -173,7 +174,7 @@ func runInit(cmd *cobra.Command, env string, raID string) error {
if env != "mock" {
daSpinner, _ := pterm.DefaultSpinner.Start("initializing da light client")

damanager := datalayer.NewDAManager(initConfig.DA, initConfig.Home)
damanager := datalayer.NewDAManager(initConfig.DA.Backend, initConfig.Home)
mnemonic, err := damanager.InitializeLightNodeConfig()
if err != nil {
return err
Expand All @@ -184,7 +185,7 @@ func runInit(cmd *cobra.Command, env string, raID string) error {
return err
}

latestHeight, latestBlockIdHash, err := GetLatestDABlock()
latestHeight, latestBlockIdHash, err := GetLatestDABlock(initConfig)
if err != nil {
return err
}
Expand Down Expand Up @@ -257,7 +258,7 @@ func runInit(cmd *cobra.Command, env string, raID string) error {
return err
}

height, hash, err := GetDABlockByHeight(h)
height, hash, err := GetDABlockByHeight(h, initConfig)
if err != nil {
pterm.Error.Println("failed to retrieve DA height: ", err)
return err
Expand Down Expand Up @@ -403,10 +404,10 @@ func UpdateCelestiaConfig(file, hash string, height int) error {
// It executes the CelestiaApp command "q block --node" to retrieve the block data.
// It then extracts the block height and block ID hash from the JSON response.
// Returns the block height, block ID hash, and any error encountered during the process.
func GetLatestDABlock() (string, string, error) {
func GetLatestDABlock(raCfg config.RollappConfig) (string, string, error) {
cmd := exec.Command(
consts.Executables.CelestiaApp,
"q", "block", "--node", celestia.DefaultCelestiaRPC,
"q", "block", "--node", raCfg.DA.RpcUrl, "--chain-id", raCfg.DA.ID,
)

out, err := bash.ExecCommandWithStdout(cmd)
Expand Down Expand Up @@ -450,10 +451,10 @@ func GetLatestDABlock() (string, string, error) {
// where <height> is the input parameter.
// It then extracts the block height and block ID hash from the JSON response.
// Returns the block height, block ID hash, and any error encountered during the process.
func GetDABlockByHeight(h string) (string, string, error) {
func GetDABlockByHeight(h string, raCfg config.RollappConfig) (string, string, error) {
cmd := exec.Command(
consts.Executables.CelestiaApp,
"q", "block", h, "--node", celestia.DefaultCelestiaRPC,
"q", "block", h, "--node", raCfg.DA.RpcUrl, "--chain-id", raCfg.DA.ID,
)

out, err := bash.ExecCommandWithStdout(cmd)
Expand Down
Loading

0 comments on commit 1d66c64

Please sign in to comment.