Skip to content

Commit

Permalink
feat: improved chain initiator
Browse files Browse the repository at this point in the history
  • Loading branch information
snobbee committed Dec 11, 2023
1 parent 7707aed commit 7d4bcbe
Show file tree
Hide file tree
Showing 14 changed files with 396 additions and 64 deletions.
87 changes: 87 additions & 0 deletions scripts/chain-initiator/download-and-run-version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package main

import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"regexp"
"strings"
)

func downloadAndRunVersion(binaryURL string, skipDownload bool) (path string, version string, err error) {
if skipDownload {
// Extract version from the URL
re := regexp.MustCompile(`v[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9]+)?`)
versionMatches := re.FindStringSubmatch(binaryURL)
if len(versionMatches) == 0 {
err = errors.New("no version found in URL")
return
}
version = versionMatches[0]

// Remove the "v" prefix if present
if strings.HasPrefix(version, "v") {
version = strings.TrimPrefix(version, "v")
}

// Set the binary path based on the version
path = "/tmp/sifnoded-" + version

// Check if the path exists
if _, err = os.Stat(path); os.IsNotExist(err) {
err = errors.New(fmt.Sprintf("binary file does not exist at the specified path: %v", path))
}

return
}

// Download the binary
resp, err := http.Get(binaryURL)

Check failure on line 43 in scripts/chain-initiator/download-and-run-version.go

View workflow job for this annotation

GitHub Actions / lint

G107: Potential HTTP request made with variable url (gosec)
if err != nil {
return
}
defer resp.Body.Close()

// Create a temporary file
tmpFile, err := ioutil.TempFile("", "binary-*")
if err != nil {
return
}
tmpFilePath := tmpFile.Name()
defer os.Remove(tmpFilePath) // Clean up

// Write the downloaded content to the file
_, err = io.Copy(tmpFile, resp.Body)
tmpFile.Close()
if err != nil {
return
}

// Make the file executable
err = os.Chmod(tmpFilePath, 0755)
if err != nil {
return
}

// Run the command 'binary version'
cmd := exec.Command(tmpFilePath, "version")
versionOutput, err := cmd.CombinedOutput()
if err != nil {
return
}
version = strings.TrimSpace(string(versionOutput))

// Rename the temporary file
newFilePath := "/tmp/sifnoded-" + version
err = os.Rename(tmpFilePath, newFilePath)
if err != nil {
return
}
path = newFilePath

return

Check failure on line 86 in scripts/chain-initiator/download-and-run-version.go

View workflow job for this annotation

GitHub Actions / lint

naked return in func `downloadAndRunVersion` with 72 lines of code (nakedret)
}
13 changes: 9 additions & 4 deletions scripts/chain-initiator/get-args.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ import (
"log"
)

func getArgs(args []string) (snapshotUrl, newVersion string) {
func getArgs(args []string) (snapshotUrl, oldBinaryUrl, newBinaryUrl string) {
snapshotUrl = args[0] // https://snapshots.polkachu.com/snapshots/sifchain/sifchain_15048938.tar.lz4
if snapshotUrl == "" {
log.Fatalf(Red + "snapshot url is required")
}

newVersion = args[1] // v0.1.0
if newVersion == "" {
log.Fatalf(Red + "new version is required")
oldBinaryUrl = args[1] // https://github.com/Sifchain/sifnode/releases/download/v1.2.0-beta/sifnoded-v1.2.0-beta-darwin-arm64
if oldBinaryUrl == "" {
log.Fatalf(Red + "old binary url is required")
}

newBinaryUrl = args[2] // https://github.com/Sifchain/sifnode/releases/download/v1.3.0-beta/sifnoded-v1.3.0-beta-darwin-arm64
if newBinaryUrl == "" {
log.Fatalf(Red + "new binary url is required")
}

return
Expand Down
82 changes: 71 additions & 11 deletions scripts/chain-initiator/get-flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,29 @@ import (
)

const (
flagHome = "home"
flagCmd = "cmd"
flagSkipSnapshot = "skip-snapshot"
flagSkipChainInit = "skip-chain-init"
flagSkipNodeStart = "skip-node-start"
flagHome = "home"
flagSkipSnapshot = "skip-snapshot"
flagSkipChainInit = "skip-chain-init"
flagSkipNodeStart = "skip-node-start"
flagSkipProposal = "skip-proposal"
flagSkipBinary = "skip-binary"
flagMoniker = "moniker"
flagChainId = "chain-id"
flagKeyringBackend = "keyring-backend"
flagValidatorKeyName = "validator-key-name"
flagValidatorBalance = "validator-balance"
flagValidatorSelfDelegation = "validator-self-delegation"
flagGenesisFilePath = "genesis-file-path"
flagNode = "node"
flagBroadcastMode = "broadcast-mode"
)

func getFlags(cmd *cobra.Command) (homePath, cmdPath string, skipSnapshot, skipChainInit, skipNodeStart bool) {
func getFlags(cmd *cobra.Command) (homePath string, skipSnapshot, skipChainInit, skipNodeStart, skipProposal, skipBinary bool, moniker, chainId, keyringBackend, validatorKeyName, validatorBalance, validatorSelfDelegation, genesisFilePath, node, broadcastMode string) {
homePath, _ = cmd.Flags().GetString(flagHome)
if homePath == "" {
log.Fatalf(Red + "home path is required")
}

cmdPath, _ = cmd.Flags().GetString(flagCmd)
if cmdPath == "" {
log.Fatalf(Red + "cmd path is required")
}

skipSnapshot, _ = cmd.Flags().GetBool(flagSkipSnapshot)
if skipSnapshot {
log.Printf(Yellow + "skipping snapshot retrieval")
Expand All @@ -40,5 +45,60 @@ func getFlags(cmd *cobra.Command) (homePath, cmdPath string, skipSnapshot, skipC
log.Printf(Yellow + "skipping node start")
}

skipProposal, _ = cmd.Flags().GetBool(flagSkipProposal)
if skipProposal {
log.Printf(Yellow + "skipping proposal")
}

skipBinary, _ = cmd.Flags().GetBool(flagSkipBinary)
if skipBinary {
log.Printf(Yellow + "skipping binary download")
}

moniker, _ = cmd.Flags().GetString(flagMoniker)
if moniker == "" {
log.Fatalf(Red + "moniker is required")
}

chainId, _ = cmd.Flags().GetString(flagChainId)
if chainId == "" {
log.Fatalf(Red + "chain id is required")
}

keyringBackend, _ = cmd.Flags().GetString(flagKeyringBackend)
if keyringBackend == "" {
log.Fatalf(Red + "keyring backend is required")
}

validatorKeyName, _ = cmd.Flags().GetString(flagValidatorKeyName)
if validatorKeyName == "" {
log.Fatalf(Red + "validator key name is required")
}

validatorBalance, _ = cmd.Flags().GetString(flagValidatorBalance)
if validatorBalance == "" {
log.Fatalf(Red + "validator balance is required")
}

validatorSelfDelegation, _ = cmd.Flags().GetString(flagValidatorSelfDelegation)
if validatorSelfDelegation == "" {
log.Fatalf(Red + "validator self delegation is required")
}

genesisFilePath, _ = cmd.Flags().GetString(flagGenesisFilePath)
if genesisFilePath == "" {
log.Fatalf(Red + "genesis file path is required")
}

node, _ = cmd.Flags().GetString(flagNode)
if node == "" {
log.Fatalf(Red + "node is required")
}

broadcastMode, _ = cmd.Flags().GetString(flagBroadcastMode)
if broadcastMode == "" {
log.Fatalf(Red + "broadcast mode is required")
}

return

Check failure on line 103 in scripts/chain-initiator/get-flags.go

View workflow job for this annotation

GitHub Actions / lint

naked return in func `getFlags` with 77 lines of code (nakedret)
}
Loading

0 comments on commit 7d4bcbe

Please sign in to comment.