Skip to content

Commit

Permalink
feat: chain-initiator uses snapshot url and export genesis file from …
Browse files Browse the repository at this point in the history
…there
  • Loading branch information
snobbee committed Dec 8, 2023
1 parent 50cc47d commit de70a64
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
26 changes: 26 additions & 0 deletions scripts/chain-initiator/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"io/ioutil"
"log"
"os/exec"
)

func export(cmdPath, homePath, genesisFilePath string) {
// Command and arguments
args := []string{"export", "--home", homePath}

// Execute the command and capture the output
output, err := exec.Command(cmdPath, args...).CombinedOutput()
if err != nil {
log.Fatalf("Command execution failed: %v", err)
}

// Write the output to the specified file
err = ioutil.WriteFile(genesisFilePath, output, 0644)

Check failure on line 20 in scripts/chain-initiator/export.go

View workflow job for this annotation

GitHub Actions / lint

G306: Expect WriteFile permissions to be 0600 or less (gosec)
if err != nil {
log.Fatalf("Failed to write output to file: %v", err)
}

log.Printf("Output successfully written to %s", genesisFilePath)
}
21 changes: 17 additions & 4 deletions scripts/chain-initiator/initiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ const (
keyringBackend = "test"
validatorKeyName = "validator"
validatorBalance = "4000000000000000000000000000"
genesisFilePath = "/tmp/genesis.json"
)

func main() {
var rootCmd = &cobra.Command{
Use: "initiator [cmd_path] [home_path] [genesis_file_path]",
Use: "initiator [cmd_path] [home_path] [snapshot_url]]",
Short: "Chain Initiator is a tool for modifying genesis files",
Long: `A tool for performing various operations on genesis files of a blockchain setup.`,
Args: cobra.ExactArgs(3), // Expect exactly two arguments
Run: func(cmd *cobra.Command, args []string) {
cmdPath := args[0] // sifnoded
homePath := args[1] // /tmp/node
genesisFilePath := args[2] // /tmp/genesis.json
cmdPath := args[0] // sifnoded
homePath := args[1] // /tmp/node
snapshotUrl := args[2] // https://snapshots.polkachu.com/snapshots/sifchain/sifchain_15048938.tar.lz4

// set address prefix
app.SetConfig(false)
Expand All @@ -37,6 +38,18 @@ func main() {
// init chain
initChain(cmdPath, moniker, chainId, homePath)

// retrieve the snapshot
retrieveSnapshot(snapshotUrl, homePath)

// export genesis file
export(cmdPath, homePath, genesisFilePath)

// remove home path
removeHome(homePath)

// init chain
initChain(cmdPath, moniker, chainId, homePath)

// add validator key
validatorAddress := addKey(cmdPath, validatorKeyName, homePath, keyringBackend)

Expand Down
20 changes: 20 additions & 0 deletions scripts/chain-initiator/retrieve-snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"log"
"os/exec"
)

func retrieveSnapshot(snapshotUrl, homePath string) {
// Construct the command string
cmdString := "curl -o - -L " + snapshotUrl + " | lz4 -c -d - | tar -x -C " + homePath

// Execute the command using /bin/sh
cmd := exec.Command("/bin/sh", "-c", cmdString)
if err := cmd.Run(); err != nil {
log.Fatalf("Command execution failed: %v", err)
}

// If execution reaches here, the command was successful
log.Printf("Snapshot retrieved and extracted to path: %s", homePath)
}

0 comments on commit de70a64

Please sign in to comment.