Skip to content

Commit

Permalink
feat: finalize full node flow (#836)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs authored Aug 18, 2024
1 parent db99b6c commit ae3000b
Show file tree
Hide file tree
Showing 8 changed files with 418 additions and 75 deletions.
6 changes: 3 additions & 3 deletions cmd/eibc/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ func initCmd() *cobra.Command {
eibcConfigPath := filepath.Join(eibcHome, "config.yaml")
data, err := os.ReadFile(eibcConfigPath)
if err != nil {
fmt.Printf("Error reading file: %v\n", err)
return // Assume this is in a function that returns an error
pterm.Error.Printf("Error reading file: %v\n", err)
return
}

// Parse the YAML
var node yaml.Node
err = yaml.Unmarshal(data, &node)
if err != nil {
fmt.Printf("Error parsing YAML: %v\n", err)
pterm.Error.Println("failed to unmarshal config.yaml")
return
}

Expand Down
84 changes: 67 additions & 17 deletions cmd/relayer/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
comettypes "github.com/cometbft/cometbft/types"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v3"

initconfig "github.com/dymensionxyz/roller/cmd/config/init"
"github.com/dymensionxyz/roller/cmd/consts"
Expand All @@ -22,6 +23,7 @@ import (
"github.com/dymensionxyz/roller/utils/bash"
config2 "github.com/dymensionxyz/roller/utils/config"
"github.com/dymensionxyz/roller/utils/config/tomlconfig"
"github.com/dymensionxyz/roller/utils/config/yamlconfig"
dymintutils "github.com/dymensionxyz/roller/utils/dymint"
"github.com/dymensionxyz/roller/utils/errorhandling"
genesisutils "github.com/dymensionxyz/roller/utils/genesis"
Expand Down Expand Up @@ -116,25 +118,30 @@ func Cmd() *cobra.Command {

if !isRelayerInitialized || shouldOverwrite {
// preflight checks
blockInformation, err := rollapputils.GetCurrentHeight()
if err != nil {
pterm.Error.Printf("failed to get current block height: %v\n", err)
return
}
currentHeight, err := strconv.Atoi(
blockInformation.Block.Header.Height,
)
if err != nil {
pterm.Error.Printf("failed to get current block height: %v\n", err)
return
}
if currentHeight <= 2 {
pterm.Warning.Println("current height is too low, updating dymint config")
err := dymintutils.UpdateDymintConfigForIBC(home, "5s")
for {
blockInformation, err := rollapputils.GetCurrentHeight()
if err != nil {
pterm.Error.Printf("failed to get current block height: %v\n", err)
return
}
currentHeight, err := strconv.Atoi(
blockInformation.Block.Header.Height,
)
if err != nil {
pterm.Error.Println("failed to update dymint config")
pterm.Error.Printf("failed to get current block height: %v\n", err)
return
}

if currentHeight <= 2 {
pterm.Warning.Println("current height is too low, updating dymint config")
err = dymintutils.UpdateDymintConfigForIBC(home, "5s")
if err != nil {
pterm.Error.Println("failed to update dymint config")
return
}
} else {
break
}
}

rollappPrefix := rollappChainData.Bech32Prefix
Expand All @@ -150,7 +157,7 @@ func Cmd() *cobra.Command {
RPC: consts.DefaultRollappRPC,
Denom: rollappDenom,
AddressPrefix: rollappPrefix,
GasPrices: "1000000000",
GasPrices: "2000000000",
}, relayer.ChainConfig{
ID: rollappConfig.HubData.ID,
RPC: rollappConfig.HubData.RPC_URL,
Expand All @@ -167,6 +174,49 @@ func Cmd() *cobra.Command {
return
}

pterm.Info.Println("updating application relayer config")
path := filepath.Join(relayerHome, "config")
data, err := os.ReadFile(filepath.Join(path, "config.yaml"))
if err != nil {
fmt.Printf("Error reading file: %v\n", err)
}

// Parse the YAML
var node yaml.Node
err = yaml.Unmarshal(data, &node)
if err != nil {
pterm.Error.Println("failed to unmarshal config.yaml")
return
}

contentNode := &node
if node.Kind == yaml.DocumentNode && len(node.Content) > 0 {
contentNode = node.Content[0]
}

err = yamlconfig.UpdateNestedYAML(
contentNode,
[]string{"chains", rollappConfig.RollappID, "value", "gas-adjustment"},
1.3,
)
if err != nil {
fmt.Printf("Error updating YAML: %v\n", err)
return
}

updatedData, err := yaml.Marshal(&node)
if err != nil {
fmt.Printf("Error marshaling YAML: %v\n", err)
return
}

// Write the updated YAML back to the original file
err = os.WriteFile(filepath.Join(path, "config.yaml"), updatedData, 0o644)
if err != nil {
fmt.Printf("Error writing file: %v\n", err)
return
}

pterm.Info.Println(
"updating dymint config to 5s block time for relayer configuration",
)
Expand Down
118 changes: 108 additions & 10 deletions cmd/rollapp/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,30 +321,108 @@ func Cmd() *cobra.Command {
pterm.Info.Println("retrieving the latest available snapshot")
si, err := sequencerutils.GetLatestSnapshot(rollappConfig.RollappID, hd)
if err != nil {
pterm.Error.Println("failed to retrieve ")
pterm.Error.Println("failed to retrieve latest snapshot")
}

if si == nil {
pterm.Warning.Printf(
"no snapshots were found for %s\n",
"no snapshots were found for %s, the node will sync from genesis block\n",
rollappConfig.RollappID,
)
} else {
fmt.Printf(
"found a snapshot for height %s\nchecksum: %s\nurl: %s",
"found a snapshot for height %s\nchecksum: %s\nurl: %s\n",
si.Height,
si.Checksum,
si.SnapshotUrl,
)
}

options := []string{"p2p", "da"}
syncMode, _ := pterm.DefaultInteractiveSelect.
WithDefaultText("select the desired syncing mode").
WithOptions(options).
Show()
// look for p2p bootstrap nodes, if there are no nodes available, the rollapp
// defaults to syncing only from the DA
peers, err := sequencerutils.GetAllP2pPeers(rollappConfig.RollappID, hd)
if err != nil {
pterm.Error.Println("failed to retrieve p2p peers ")
}

if len(peers) == 0 {
pterm.Warning.Println(
"none of the sequencers provide p2p seed nodes this node will sync only from DA",
)
}

// approve the data directory deletion before downloading the snapshot,
dataDir := filepath.Join(RollappDirPath, "data")
if fi, err := os.Stat(dataDir); err == nil && fi.IsDir() {
dataDirNotEmpty, err := globalutils.DirNotEmpty(dataDir)
if err != nil {
pterm.Error.Printf("failed to check if data directory is empty: %v\n", err)
os.Exit(1)
}

var replaceExistingData bool
if dataDirNotEmpty {
pterm.Warning.Println("the ~/.roller/rollapp/data directory is not empty.")
replaceExistingData, _ = pterm.DefaultInteractiveConfirm.Show(
"Do you want to replace its contents?",
)
if !replaceExistingData {
pterm.Info.Println(
"operation cancelled, node will be synced from genesis block ",
)
}
}

if !dataDirNotEmpty || replaceExistingData {
// TODO: this should be a util "RecreateDir"
err = os.RemoveAll(dataDir)
if err != nil {
pterm.Error.Printf("failed to remove %s dir: %v", dataDir, err)
return
}

err = os.MkdirAll(dataDir, 0o755)
if err != nil {
pterm.Error.Printf("failed to create %s: %v\n", dataDir, err)
return
}

tmpDir, err := os.MkdirTemp("", "download-*")
if err != nil {
fmt.Printf("Error creating temp directory: %v\n", err)
return
}

fmt.Printf("the node is set to sync from %s\n", syncMode)
// Print the path of the temporary directory
fmt.Printf("Temporary directory created: %s\n", tmpDir)

// The directory will be deleted when the program exits
// nolint:errcheck,gosec
defer os.RemoveAll(tmpDir)
archivePath := filepath.Join(tmpDir, "backup.tar.gz")
spinner, _ := pterm.DefaultSpinner.Start("downloading file...")
downloadedFileHash, err := globalutils.DownloadAndSaveArchive(
si.SnapshotUrl,
archivePath,
)
if err != nil {
spinner.Fail(fmt.Sprintf("error downloading file: %v", err))
os.Exit(1)
}
spinner.Success("file downloaded successfully")

// compare the checksum
if downloadedFileHash != si.Checksum {
pterm.Error.Println()
}

err = globalutils.ExtractTarGz(archivePath, filepath.Join(RollappDirPath))
if err != nil {
pterm.Error.Println("failed to extract snapshot: ", err)
return
}
}
}
}

// DA
Expand Down Expand Up @@ -507,6 +585,7 @@ func Cmd() *cobra.Command {
}

var daConfig string
dymintConfigPath := sequencer.GetDymintFilePath(home)

switch nodeType {
case "sequencer":
Expand All @@ -516,6 +595,16 @@ func Cmd() *cobra.Command {
pterm.Error.Println("failed to check balance", err)
}

err = globalutils.UpdateFieldInToml(
dymintConfigPath,
"p2p_advertising_enabled",
"false",
)
if err != nil {
pterm.Error.Println("failed to update `p2p_advertising_enabled`")
return
}

utils.PrintInsufficientBalancesIfAny(insufficientBalances)

// TODO: daconfig should be a struct
Expand All @@ -527,13 +616,22 @@ func Cmd() *cobra.Command {
daConfig = damanager.DataLayer.GetSequencerDAConfig(
consts.NodeType.FullNode,
)

err = globalutils.UpdateFieldInToml(
dymintConfigPath,
"p2p_advertising_enabled",
"true",
)
if err != nil {
pterm.Error.Println("failed to update `p2p_advertising_enabled`")
return
}
default:
pterm.Error.Println("unsupported node type")
return

}

dymintConfigPath := sequencer.GetDymintFilePath(home)
daNamespace := damanager.DataLayer.GetNamespaceID()
if daNamespace == "" {
pterm.Error.Println("failed to retrieve da namespace id")
Expand Down
19 changes: 10 additions & 9 deletions utils/archives/archives.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@ func ExtractFileFromNestedTar(sourceZipFilePath, fileName, outputDir string) err
if err != nil {
return err
}
// nolint errcheck

// nolint:errcheck
defer os.RemoveAll(tmpDir)

zipReader, err := zip.OpenReader(sourceZipFilePath)
if err != nil {
return err
}
// nolint errcheck
// nolint:errcheck
defer zipReader.Close()

var tarFilePath string

for _, f := range zipReader.File {
if filepath.Ext(f.Name) == ".tar" {
// nolint gosec
// nolint:gosec
tarFilePath = filepath.Join(tmpDir, f.Name)
if err := extractFileFromZip(f, tarFilePath); err != nil {
return fmt.Errorf("failed to extract .tar file %s: %w", tarFilePath, err)
Expand All @@ -59,7 +60,7 @@ func TraverseTARFile(tarFile, fileName, outputDir string) error {
if err != nil {
return fmt.Errorf("failed to open tar file: %w", err)
}
// nolint errcheck
// nolint:errcheck
defer file.Close()
tarReader := tar.NewReader(file)

Expand Down Expand Up @@ -107,11 +108,11 @@ func createFileFromArchive(outputPath string, tarReader *tar.Reader) error {
}

if _, err := io.Copy(outFile, tarReader); err != nil {
// nolint errcheck
// nolint:errcheck
outFile.Close()
return fmt.Errorf("ExtractTar: Copy() failed: %w", err)
}
// nolint errcheck
// nolint:errcheck
outFile.Close()
return nil
}
Expand All @@ -122,17 +123,17 @@ func extractFileFromZip(f *zip.File, outputPath string) error {
if err != nil {
return fmt.Errorf("failed to open file in zip: %w", err)
}
// nolint errcheck
// nolint:errcheck
defer rc.Close()

outFile, err := os.Create(outputPath)
if err != nil {
return fmt.Errorf("failed to create output file: %w", err)
}
// nolint errcheck
// nolint:errcheck
defer outFile.Close()

// nolint gosec
// nolint:gosec
if _, err := io.Copy(outFile, rc); err != nil {
return fmt.Errorf("failed to copy file contents: %w", err)
}
Expand Down
Loading

0 comments on commit ae3000b

Please sign in to comment.