Skip to content

Commit

Permalink
Implemented ability to run da light node
Browse files Browse the repository at this point in the history
  • Loading branch information
ItayLevyOfficial committed Jun 20, 2023
1 parent 825a385 commit 6b73731
Show file tree
Hide file tree
Showing 11 changed files with 248 additions and 10 deletions.
1 change: 1 addition & 0 deletions cmd/config/init/DALightClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package initconfig
import (
"os/exec"
"path/filepath"

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

Expand Down
16 changes: 12 additions & 4 deletions cmd/consts/consts.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package consts

import "fmt"

const binsDir = "/usr/local/bin"

var internalBinsDir = fmt.Sprintf("%s/roller_bins", binsDir)

var Executables = struct {
Celestia string
RollappEVM string
Relayer string
Dymension string
CelKey string
}{
Celestia: "/usr/local/bin/roller_bins/celestia",
RollappEVM: "/usr/local/bin/rollapp_evm",
Relayer: "/usr/local/bin/roller_bins/rly",
Dymension: "/usr/local/bin/roller_bins/dymd",
Celestia: fmt.Sprintf("%s/celestia", internalBinsDir),
CelKey: fmt.Sprintf("%s/cel-key", internalBinsDir),
RollappEVM: fmt.Sprintf("%s/rollapp_evm", binsDir),
Relayer: fmt.Sprintf("%s/rly", internalBinsDir),
Dymension: fmt.Sprintf("%s/dymd", internalBinsDir),
}

var KeyNames = struct {
Expand Down
18 changes: 18 additions & 0 deletions cmd/da-light-client/da_light_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package da_light_client

import (
da_start "github.com/dymensionxyz/roller/cmd/da-light-client/start"
"github.com/spf13/cobra"
)

func DALightClientCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "da-light-client",
Short: "Commands for running and managing the data availability light client.",

Run: func(cmd *cobra.Command, args []string) {
},
}
cmd.AddCommand(da_start.StartCmd())
return cmd
}
49 changes: 49 additions & 0 deletions cmd/da-light-client/start/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package start

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

initconfig "github.com/dymensionxyz/roller/cmd/config/init"
"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/spf13/cobra"
)

func StartCmd() *cobra.Command {
runCmd := &cobra.Command{
Use: "start",
Short: "Runs the rollapp sequencer.",
Run: func(cmd *cobra.Command, args []string) {
home := cmd.Flag(utils.FlagNames.Home).Value.String()
rollappConfig, err := initconfig.LoadConfigFromTOML(home)
utils.PrettifyErrorIfExists(err)
startRollappCmd := getCelestiaCmd(rollappConfig)
utils.RunBashCmdAsync(startRollappCmd, printOutput, parseError)
},
}
utils.AddGlobalFlags(runCmd)
return runCmd
}

func printOutput() {
fmt.Println("💈 The data availability light node is running on your local machine!")
fmt.Println("💈 Light node endpoint: http://0.0.0.0:26659")
}

func parseError(errMsg string) string {
return errMsg
}

func getCelestiaCmd(rollappConfig initconfig.InitConfig) *exec.Cmd {
return exec.Command(
consts.Executables.Celestia, "light", "start",
"--core.ip", "consensus-full-arabica-8.celestia-arabica.com",
"--node.store", filepath.Join(rollappConfig.Home, consts.ConfigDirName.DALightNode),
"--gateway",
"--gateway.addr", "127.0.0.1",
"--gateway.port", "26659",
"--p2p.network", "arabica",
)
}
6 changes: 4 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"os"

"github.com/dymensionxyz/roller/cmd/config"
da_light_client "github.com/dymensionxyz/roller/cmd/da-light-client"
"github.com/dymensionxyz/roller/cmd/register"
"github.com/dymensionxyz/roller/cmd/run"
"github.com/dymensionxyz/roller/cmd/sequencer"
"github.com/dymensionxyz/roller/cmd/version"
"github.com/spf13/cobra"
)
Expand All @@ -29,5 +30,6 @@ func init() {
rootCmd.AddCommand(config.ConfigCmd())
rootCmd.AddCommand(version.VersionCmd())
rootCmd.AddCommand(register.RegisterCmd())
rootCmd.AddCommand(run.RunCmd())
rootCmd.AddCommand(da_light_client.DALightClientCmd())
rootCmd.AddCommand(sequencer.SequencerCmd())
}
16 changes: 14 additions & 2 deletions cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
"os/exec"
"path/filepath"

"errors"

"bytes"

initconfig "github.com/dymensionxyz/roller/cmd/config/init"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/spf13/cobra"
Expand All @@ -22,18 +26,26 @@ func RunCmd() *cobra.Command {
rollappConfig, err := initconfig.LoadConfigFromTOML(home)
utils.PrettifyErrorIfExists(err)
startRollappCmd := getStartRollapCmd(rollappConfig, cmd.Flag(daLightClientEndpointFlag).Value.String())
var stderr bytes.Buffer
startRollappCmd.Stderr = &stderr
startRollappErr := startRollappCmd.Start()
if startRollappErr != nil {
utils.PrettifyErrorIfExists(errors.New(stderr.String()))
}
utils.PrettifyErrorIfExists(startRollappErr)
fmt.Println("💈 The Rollapp sequencer is running on your local machine!")
fmt.Println("💈 EVM RPC: http://0.0.0.0:8545")
fmt.Println("💈 Node RPC: http://0.0.0.0:26657")
fmt.Println("💈 Rest API: http://0.0.0.0:1317")
err = startRollappCmd.Wait()
utils.PrettifyErrorIfExists(err)
if err != nil {
errMsg := stderr.String()
utils.PrettifyErrorIfExists(errors.New(errMsg))
}
},
}
addFlags(runCmd)
utils.AddGlobalFlags(runCmd)
addFlags(runCmd)
return runCmd
}

Expand Down
15 changes: 15 additions & 0 deletions cmd/sequencer/sequencer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package sequencer

import (
sequnecer_start "github.com/dymensionxyz/roller/cmd/sequencer/start"
"github.com/spf13/cobra"
)

func SequencerCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "sequencer",
Short: "Commands for running and managing the RollApp sequnecer.",
}
cmd.AddCommand(sequnecer_start.StartCmd())
return cmd
}
81 changes: 81 additions & 0 deletions cmd/sequencer/start/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package sequnecer_start

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

"strings"

initconfig "github.com/dymensionxyz/roller/cmd/config/init"
"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/spf13/cobra"
)

func StartCmd() *cobra.Command {
runCmd := &cobra.Command{
Use: "start",
Short: "Runs the rollapp sequencer.",
Run: func(cmd *cobra.Command, args []string) {
home := cmd.Flag(utils.FlagNames.Home).Value.String()
rollappConfig, err := initconfig.LoadConfigFromTOML(home)
utils.PrettifyErrorIfExists(err)
LightNodeEndpoint := cmd.Flag(FlagNames.DAEndpoint).Value.String()
startRollappCmd := getStartRollapCmd(rollappConfig, LightNodeEndpoint)
utils.RunBashCmdAsync(startRollappCmd, printOutput, parseError)
},
}
utils.AddGlobalFlags(runCmd)
runCmd.Flags().StringP(FlagNames.DAEndpoint, "", "http://localhost:26659", "The data availability light node endpoint.")
return runCmd
}

var FlagNames = struct {
DAEndpoint string
}{
DAEndpoint: "da-endpoint",
}

func printOutput() {
fmt.Println("💈 The Rollapp sequencer is running on your local machine!")
fmt.Println("💈 EVM RPC: http://0.0.0.0:8545")
fmt.Println("💈 Node RPC: http://0.0.0.0:26657")
fmt.Println("💈 Rest API: http://0.0.0.0:1317")
}

func parseError(errMsg string) string {
lines := strings.Split(errMsg, "\n")
if len(lines) > 0 && lines[0] == "Error: failed to initialize database: resource temporarily unavailable" {
return "The Rollapp sequencer is already running. Only one sequencer can run on the machine at any given time."
}
return errMsg
}

func getStartRollapCmd(rollappConfig initconfig.InitConfig, lightNodeEndpoint string) *exec.Cmd {
daConfig := fmt.Sprintf(`{"base_url": "%s", "timeout": 60000000000, "fee":20000, "gas_limit": 20000000, "namespace_id":[0,0,0,0,0,0,255,255]}`,
lightNodeEndpoint)
rollappConfigDir := filepath.Join(rollappConfig.Home, consts.ConfigDirName.Rollapp)

// TODO: Update the gas_fees to 2000000udym before 35-c launch.
settlementConfig := fmt.Sprintf(`{"node_address": "%s", "rollapp_id": "%s", "dym_account_name": "%s", "keyring_home_dir": "%s", "keyring_backend":"test", "gas_fees": "0udym"}`, rollappConfig.HubData.RPC_URL, rollappConfig.RollappID, consts.KeyNames.HubSequencer, rollappConfigDir)

return exec.Command(
rollappConfig.RollappBinary, "start",
"--dymint.aggregator",
"--json-rpc.enable",
"--json-rpc.api", "eth,txpool,personal,net,debug,web3,miner",
"--dymint.da_layer", "celestia",
"--dymint.da_config", daConfig,
"--dymint.settlement_layer", "dymension",
"--dymint.settlement_config", settlementConfig,
"--dymint.block_batch_size", "1200",
"--dymint.namespace_id", "000000000000ffff",
"--dymint.block_time", "0.2s",
"--home", rollappConfigDir,
"--log_level", "debug",
"--log-file", filepath.Join(rollappConfigDir, "rollapp.log"),
"--max-log-size", "2000",
"--module-log-level-override", "",
)
}
19 changes: 19 additions & 0 deletions cmd/utils/error_handling.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package utils

import (
"os"
"os/exec"

"github.com/fatih/color"
"errors"
"bytes"
)

func PrettifyErrorIfExists(err error) {
Expand All @@ -17,3 +20,19 @@ func PrettifyErrorIfExists(err error) {
panic(err)
}
}

func RunBashCmdAsync(cmd *exec.Cmd, printOutput func(), parseError func(errMsg string) string) {
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Start()
if err != nil {
errMsg := parseError(stderr.String())
PrettifyErrorIfExists(errors.New(errMsg))
}
printOutput()
err = cmd.Wait()
if err != nil {
errMsg := parseError(stderr.String())
PrettifyErrorIfExists(errors.New(errMsg))
}
}
36 changes: 35 additions & 1 deletion cmd/utils/keys.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,46 @@
package utils

import (
"bytes"
"encoding/json"
"errors"
"os/exec"

"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
"github.com/dymensionxyz/roller/cmd/consts"
)

type KeyInfo struct {
Address string `json:"address"`
}

func GetCelestiaAddress(keyringDir string) (string, error) {
cmd := exec.Command(
consts.Executables.CelKey,
"show", consts.KeyNames.DALightNode, "--node.type", "light", "--keyring-dir", keyringDir, "--keyring-backend", "test", "--output", "json",
)

var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr

err := cmd.Run()
if err != nil {
return "", errors.New(stderr.String())
}

var key = &KeyInfo{}
err = json.Unmarshal(out.Bytes(), key)
if err != nil {
return "", err
}

return key.Address, nil
}

type KeyConfig struct {
Dir string
ID string
Expand Down Expand Up @@ -41,6 +76,5 @@ func GetAddress(keyConfig KeyConfig) (string, error) {
if err != nil {
return "", err
}

return formattedAddress, nil
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ require (
github.com/keybase/go-keychain v0.0.0-20190712205309-48d3d31d256d // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/manifoldco/promptui v0.9.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
Expand Down

0 comments on commit 6b73731

Please sign in to comment.