From de70a6435756b6194251050562f9633d91038d25 Mon Sep 17 00:00:00 2001 From: Snobbish Bee <125891987+snobbee@users.noreply.github.com> Date: Fri, 8 Dec 2023 08:08:42 +0100 Subject: [PATCH] feat: chain-initiator uses snapshot url and export genesis file from there --- scripts/chain-initiator/export.go | 26 ++++++++++++++++++++ scripts/chain-initiator/initiator.go | 21 +++++++++++++--- scripts/chain-initiator/retrieve-snapshot.go | 20 +++++++++++++++ 3 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 scripts/chain-initiator/export.go create mode 100644 scripts/chain-initiator/retrieve-snapshot.go diff --git a/scripts/chain-initiator/export.go b/scripts/chain-initiator/export.go new file mode 100644 index 0000000000..f030bf3258 --- /dev/null +++ b/scripts/chain-initiator/export.go @@ -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) + if err != nil { + log.Fatalf("Failed to write output to file: %v", err) + } + + log.Printf("Output successfully written to %s", genesisFilePath) +} diff --git a/scripts/chain-initiator/initiator.go b/scripts/chain-initiator/initiator.go index eca05f7725..a1dc365b5d 100644 --- a/scripts/chain-initiator/initiator.go +++ b/scripts/chain-initiator/initiator.go @@ -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) @@ -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) diff --git a/scripts/chain-initiator/retrieve-snapshot.go b/scripts/chain-initiator/retrieve-snapshot.go new file mode 100644 index 0000000000..c4f8fbd4ec --- /dev/null +++ b/scripts/chain-initiator/retrieve-snapshot.go @@ -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) +}